summaryrefslogtreecommitdiff
path: root/utils.c
Commit message (Collapse)AuthorAge
* btrfs-progs: btrfs_scan_for_fsid doesn't need all the argumentsAnand Jain2013-08-09
| | | | | | | | | btrfs_scan_for_fsid uses only one argument run_ioctl out of 3 so remove the rest two of them Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
* Btrfs-progs: fix closing of opendir()Wang Shilong2013-08-09
| | | | | | | | | | valgrind complains open_file_or_dir() causes a memory leak.That is because if we open a directoy by opendir(), and then we should call closedir() to free memory. Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
* btrfs-progs: use IEC units for sizesDavid Sterba2013-08-09
| | | | | | | | As implemented now, we use 1024 based units but reporting 1000 based, let's finally fix that and add optional unit bases later. Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
* Btrfs-progs: return error on write failure in make_btrfs()Filipe David Borba Manana2013-08-09
| | | | | | | | | | | Instead of aborting with a BUG_ON() statement, return a negated errno code. Also updated mkfs and convert tools to print a nicer error message when make_btrfs() returns an error. Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
* Btrfs-progs: add missing write check for mkfsFilipe David Borba Manana2013-08-09
| | | | | | | | | | | | | | | | Assert that the writes of the device and chunk tree roots succeed. This verification is currently done for all other tree roots, however it was missing for those 2 trees. Would these tree root writes fail, but all others succeed, it would lead to a corrupted/incomplete btrfs filesystem, or, more likely some weird failure later on in mkfs.btrfs inside open_ctree(). Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
* btrfs-progs: per-thread, per-call pretty bufferZach Brown2013-08-09
| | | | | | | | | | | | | | | | | We don't need callers to manage string storage for each pretty_sizes() call. We can use a macro to have per-thread and per-call static storage so that pretty_sizes() can be used as many times as needed in printf() arguments without requiring a bunch of supporting variables. This lets us have a natural interface at the cost of requiring __thread and TLS from gcc and a small amount of static storage. This seems better than the current code or doing something with illegible format specifier macros. Signed-off-by: Zach Brown <zab@redhat.com> Acked-by: Wang Shilong <wangs.fnst@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
* btrfs-progs: Cleanup for using BTRFS_SETGET_STACK instead of raw convertQu Wenruo2013-08-09
| | | | | | | | | | | | Some codes still use the cpu_to_lexx instead of the BTRFS_SETGET_STACK_FUNCS declared in ctree.h. Also added some BTRFS_SETGET_STACK_FUNCS for btrfs_header and btrfs_super. Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
* btrfs-progs: mkfs seg fault for wrong freeAnand Jain2013-05-02
| | | | | | | | | | | | | | With commit 87c09f7 Btrfs-progs: fix memory leaks on cleanup mkfs on multiple dev is ending with segfault at close_all_devices() during kfree(device->name) because mkfs calls btrfs_add_to_fsid, which does not initialize name when dev is added to the list. Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
* btrfs-progs: mkfs should first check all disks before writing to a diskAnand Jain2013-04-23
| | | | | | | | | | | | | | | | | | | | | | | | | | In the cases where one of the disk is not suitable for btrfs, then we would fail the mkfs, however we determine that after we have written btrfs to the preceding disks. At this time if user changes mind for not to use btrfs will left with no choice. So this patch will check if all the provided disks are suitable for the btrfs at once before proceeding to create btrfs on a disk. Further this patch also removed duplicate code to check device suitability for the btrfs. Next, there is an existing bug about the -r mkfs option, which this patch would carry forward most of it. Ref: [PATCH 2/2, RFC] btrfs-progs: overhaul mkfs.btrfs -r option Signed-off-by: Anand Jain <anand.jain@oracle.com> to merg prev Signed-off-by: Anand Jain <anand.jain@oracle.com>
* btrfs-progs: delete unused function get_mountptAnand Jain2013-04-23
| | | | | | and get_btrfs_mount has replaced it Signed-off-by: Anand Jain <anand.jain@oracle.com>
* Btrfs-progs: make btrfs-image restore with a valid chunk tree V2Josef Bacik2013-04-09
| | | | | | | | | | | | | | | | | | | | Previously btrfs-image would set a METADUMP flag and would make one big system chunk to cover the entire file system in the super in order to get around the unpleasant business of having to adjust the chunk tree. This meant that you could use the progs stuff on a restored file system, which is great for testing btrfsck and other such things. But we want to be able to run the tree log replay on a file system that is not able to run the tree log replay. So in order to do this we need to fixup the super's chunk array and the chunk tree itself. This is pretty easy since we restore using the logical offsets of the metadata, so we just have to set the chunk items to have 1 stripe and have the stripes point at the primary device and then use the logical offset of the chunk as the physical offset. With this patch I can restore a file system image that had a tree log and mount the file system and have the log be replayed successfully. This patch also gives you the -o option in case you want the old restore way, in the case where we want to make sure the system chunks as they were given to us are correct. Thanks, Signed-off-by: Josef Bacik <jbacik@fusionio.com>
* btrfs-progs: print errno string when /dev/btrfs-control open failsAnand Jain2013-03-14
| | | | | | | | | | | Of recently and intermittently I am seeing open fail for /dev/btrfs-control (btrfs is loaded), and there are no dmesg errors, this may not be a complete help in digging this issue but something which is necessary. Thanks Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: rework get_fs_info to remove side effectsEric Sandeen2013-03-12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | get_fs_info() has been silently switching from a device to a mounted path as needed; the caller's filehandle was unexpectedly closed & reopened outside the caller's scope. Not so great. The callers do want "fdmnt" to be the filehandle for the mount point in all cases, though - the various ioctls act on this (not on an fd for the device). But switching it in the local scope of get_fs_info is incorrect; it just so happens that *usually* the fd number is unchanged. So - use the new helpers to detect when an argument is a block device, and open the the mounted path more obviously / explicitly for ioctl use, storing the filehandle in fdmnt. Then, in get_fs_info, ignore the fd completely, and use the path on the argument to determine if the caller wanted to act on just that device, or on all devices for the filesystem. Affects those commands which are documented to accept either a block device or a path: * btrfs device stats * btrfs replace start * btrfs scrub start * btrfs scrub status Signed-off-by: Eric Sandeen <sandeen@redhat.com>
* btrfs-progs: three new device/path helpersEric Sandeen2013-03-12
| | | | | | | | | | | | | Add 3 new helpers: * is_block_device(), to test if a path is a block device. * get_btrfs_mount(), to get the mountpoint of a device, if mounted. * open_path_or_dev_mnt(path), to open either the pathname or, if it's a mounted btrfs dev, the mountpoint. Useful for some commands which can take either type of arg. Signed-off-by: Eric Sandeen <sandeen@redhat.com>
* btrfs-progs: close fd on return from label get/set functionsEric Sandeen2013-03-12
| | | | | | Somehow missed these 2 in the last round. Signed-off-by: Eric Sandeen <sandeen@redhat.com>
* btrfs-progs: separate super_copy out of fs_infoDavid Sterba2013-03-10
| | | | | | | | | Allocate fs_info::super_copy dynamically of full BTRFS_SUPER_INFO_SIZE and use it directly for saving superblock to disk. This fixes incorrect superblock checksum after mkfs. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: check for null string in parse_sizeEric Sandeen2013-03-10
| | | | | | | Because it's better than a segfault if it's called improperly, and it makes static checkers happier. Signed-off-by: Eric Sandeen <sandeen@redhat.com>
* btrfs-progs: from troubleshooting point of view messages must be uniqueAnand Jain2013-03-03
| | | | | | | | | ----- cmds-device.c: fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n", utils.c: fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n", ----- Signed-off-by: Anand Jain <anand.jain@oracle.com>
* Btrfs-progs: add correct indentationAnand Jain2013-02-27
| | | | | | A trivial fix, corrects the indentation. Signed-off-by: Anand Jain <anand.jain@oracle.com>
* btrfs-progs: move btrfslabel.[c|h] stuff to utils.[c|h]Jeff Liu2013-02-26
| | | | | | | | | Clean btrfslabel.[c|h] out of the source tree and move those related functions to utils.[c|h]. CC: Gene Czarcinski <gene@czarc.net> Signed-off-by: Jie Liu <jeff.liu@oracle.com> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: refactor check_label()Jeff Liu2013-02-26
| | | | | | | | | | | | | | | | Refactor check_label(). - Make it be static at first, this is a preparation step since we'll remove btrfslabel.[c|h] and move those functions from there to utils.[c|h], we can do pre-checking against the input label string with it. - Fix the label length check up from BTRFS_LABEL_SIZE to BTRFS_LABEL_SIZE - 1. - Kill the check of label contains an invalid character, see below commits for detail: 79e0e445fc2365e47fc7f060d5a4445d37e184b8 btrfs-progs: kill check for /'s in labels. Signed-off-by: Jie Liu <jeff.liu@oracle.com> CC: David Sterba <dsterba@suse.cz> CC: Gene Czarcinski <gene@czarc.net>
* Btrfs-progs: check out if the swap deviceTsutomu Itoh2013-02-19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently, the following commands succeed. # cat /proc/swaps Filename Type Size Used Priority /dev/sda3 partition 8388604 0 -1 /dev/sdc8 partition 9765884 0 -2 # mkfs.btrfs /dev/sdc8 WARNING! - Btrfs v0.20-rc1-165-g82ac345 IS EXPERIMENTAL WARNING! - see http://btrfs.wiki.kernel.org before using fs created label (null) on /dev/sdc8 nodesize 4096 leafsize 4096 sectorsize 4096 size 9.31GB Btrfs v0.20-rc1-165-g82ac345 # btrfs fi sh /dev/sdc8 Label: none uuid: fc0bdbd0-7eed-460f-b4e9-131273b66df2 Total devices 1 FS bytes used 28.00KB devid 1 size 9.31GB used 989.62MB path /dev/sdc8 Btrfs v0.20-rc1-165-g82ac345 # But we should check out the swap device. Fixed it. Signed-off-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com> Tested-by: David Sterba <dsterba@suse.cz>
* Btrfs-progs print more informative error when we fail to open a deviceEric Sandeen2013-02-06
| | | | | | | | | print more informative error when we fail to open a device If open() fails, we should let the user know why it failed. Signed-off-by: Eric Sandeen <sandeen@redhat.com> Signed-off-by: Gene Czarcinski <gene@czarc.net>
* btrfs-progs: simplify ioctl name copy and null terminationEric Sandeen2013-02-05
| | | | | | | | | | | | | | | | | | | | | | | | | In the places where we copy a string into the name member of btrfs_ioctl_vol_args or btrfs_ioctl_vol_args_v2, we use strncopy (to not overflow the name array) and then set the last position to the null character. Howver, in both cases the arrays are defined with: char name[MAX+1]; hence the last array position is name[MAX]. In most cases, we now insert the null at name[MAX-1] which deprives us of one useful character. Even the above isn't consistent through the code, so make some helper code to make it simple, i.e. strncpy_null(dest, src) which automatically does the right thing based on the size of dest. Thanks to Zach Brown for the macro suggestion. Signed-off-by: Eric Sandeen <sandeen@redhat.com>
* btrfs-progs: fix overflow in btrfs_scan_one_dir()Zach Brown2013-02-05
| | | | | | | | | | | btrfs_scan_one_dir() can overflow an arbitrarily small 256 byte buffer with an arbitrarily slightly larger 1024 byte buffer as it remembers the path of a dir to later descend. Make these buffers the same size to stop the overflow and chose PATH_MAX for that size so that it won't fail on legitimately bonkers paths. Signed-off-by: Zach Brown <zab@redhat.com>
* btrfs-progs: array indexes must be < ARRAY_SIZE()Zach Brown2013-02-05
| | | | | | | It looks like the usual kernel idiom of "< ARRAY_SIZE()" was accidentally negated as ">" instead of ">=". Signed-off-by: Zach Brown <zab@redhat.com>
* btrfs-progs: treat super.magic as an le64Zach Brown2013-02-05
| | | | | | | | | | | | The super block magic is a le64 whose value looks like an unterminated string in memory. The lack of null termination leads to clumsy use of string functions and causes static analysis tools to warn that the string will be unterminated. So let's just treat it as the le64 that it is. Endian wrappers are used on the constant so that they're compiled into run-time constants. Signed-off-by: Zach Brown <zab@redhat.com>
* Btrfs-progs: make two utility functions globally availableStefan Behrens2013-01-30
| | | | | | | | Two convenient utility functions that have so far been local to scrub are moved to utils.c. They will be used in the device stats code in a following commit. Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de>
* Btrfs-progs: move open_file_or_dir() to utils.cAnand Jain2013-01-30
| | | | | | | | | | | The definition of the function open_file_or_dir() is moved from common.c to utils.c in order to be able to share some common code between scrub and the device stats in the following step. That common code uses open_file_or_dir(). Since open_file_or_dir() makes use of the function dirfd(3), the required XOPEN version was raised from 6 to 7. Signed-off-by: Anand Jain <anand.jain@oracle.com> Original-Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de>
* Btrfs-progs: fix resolving of loop devicesNirbheek Chauhan2013-01-21
| | | | | | | | | | The LOOP_GET_STATUS ioctl truncates filenames to 64 characters. We should get the backing file for a given loop device from /sys/. This is how losetup does it as well. Signed-off-by: Nirbheek Chauhan <nirbheek.chauhan@collabora.co.uk> Signed-off-by: Gene Czarcinski <gene@czarc.net> Tested-By: Hector Oron <hector.oron@collabora.co.uk>
* Ignore the error ENXIO and ENOMEDIUM during a devs scanGoffredo Baroncelli2013-01-18
| | | | | | | | | | Ignore the error ENXIO (device don't exists) and ENOMEDIUM ( No medium found -> like a cd tray empty) in the function btrfs_scan_one_dir. This avoids spurios errors due to an empty CD or a block device node without a device (which is frequent in a static /dev). Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
* btrfs-progs: plug memory leaks in btrfs_scan_one_dir() reported by cppcheck.Kenji Okimoto2013-01-17
| | | | | | [utils.c:983]: (error) Memory leak: fullpath Signed-off-by: Kenji Okimoto <okimoto@clear-code.com>
* parse_size(): add new suffixesGoffredo Baroncelli2013-01-17
| | | | | | | | Add new suffixes in parse_size() function. New suffixes are: T as terabyte, P as petabyte, E as exabyte. Note these units are multiply of 2 . Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
* parse_size(): replace atoll() with strtoull()Goffredo Baroncelli2013-01-17
| | | | | | | Replace the function atoll with strtoull(); Check that the suffix for the parse_size() input is of only one character. Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
* Move parse_size() to utils.[hc]Goffredo Baroncelli2013-01-17
| | | | | | Move the function from cmds-filesystem.c and mkfs.c to utils.c Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
* pretty_sizes() returns incorrect valuesGoffredo Baroncelli2012-10-04
| | | | | | | | | | | | pretty_sizes() returns incorrect values if the argument is < 1024. pretty_sizes(0) -> 0.00 OK pretty_sizes(102) -> 0.10 WRONG pretty_sizes(1023) -> 1.00 WRONG pretty_sizes(1024) -> 1.00KB OK Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
* Btrfs-progs: only enforce a maximum size if we specify oneJosef Bacik2012-10-02
| | | | | | | | | | | | | | My patch 04609add88ef8428d725de6ef60f46a3ff0dbc8e introduced a regression where if you mkfs'ed a group of disks with different sizes it limited the disks to the size of the first one that is specified. This was not the intent of my patch, I only want it to limit the size based on the -b option, so I've reworked the code to pass in a max block count and that fixes the issue. Thanks, Signed-off-by: Josef Bacik <jbacik@fusionio.com>
* btrfs-progs: mkfs: create root directory with 755 permissionsDavid Sterba2012-10-02
| | | | | | That's what all other mkfs do and there's no reason for 0555. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: mkfs: add option to skip trimDavid Sterba2012-07-06
| | | | Signed-off-by: Chris Mason <chris.mason@fusionio.com>
* btrfs-progs: enforce block count on all devices in mkfsJosef Bacik2012-07-03
| | | | | | | | | | I had a test that creates a 7gig raid1 device but it was ending up wonky because the second device that gets added is the full size of the disk instead of the limited size. So enforce the limited size on all disks passed in at mkfs time, otherwise our threshold calculations end up wonky when doing chunk allocations. Thanks, Signed-off-by: Josef Bacik <josef@redhat.com>
* avoid several strncpy-induced buffer overrunsJim Meyering2012-06-05
| | | | | | | | | | | | | | * restore.c (main): Ensure strncpy-copied dir_name is NUL-terminated. * btrfsctl.c (main): Likewise, for a command-line argument. * utils.c (multiple functions): Likewise. * btrfs-list.c (add_root): Likewise. * btrfslabel.c (change_label_unmounted): Likewise. * cmds-device.c (cmd_add_dev, cmd_rm_dev, cmd_scan_dev): Likewise. * cmds-filesystem.c (cmd_resize): Likewise. * cmds-subvolume.c (cmd_subvol_create, cmd_subvol_delete, cmd_snapshot): Likewise. Reviewed-by: Josef Bacik <josef@redhat.com>
* btrfs_scan_one_dir: avoid use-after-free on error pathJim Meyering2012-06-05
| | | | | | | | | | If we iterate the "goto again" loop, we've called "closedir(dirp)", yet at the top of the loop, upon malloc failure we "goto fail", where we test dirp and if non-NULL, call closedir(dirp) again. * utils.c (btrfs_scan_one_dir): Clear "dirp" after closedir to avoid use-after-free upon failed fullpath = malloc(... Signed-off-by: Jim Meyering <meyering@redhat.com>
* Scan /dev/md and device mapper devices lastChris Mason2012-02-22
| | | | | | | | | | | When we're using multipath or raid0, it is possible that btrfs dev scan will find one of the component devices instead of the proper virtual device the kernel creates. We want to make sure the kernel scans the virtual devices last, since it always remembers the last device it finds with a given fsid. Signed-off-by: Chris Mason <chris.mason@oracle.com>
* Btrfs: use /proc/partitions scanning for btrfs_scan_for_fsidChris Mason2012-02-21
| | | | | | | | btrfs_scan_for_fsid is used by open_ctree and by mkfs when it is checking for mounted devices. It currently scans all of /dev, which is rarely the right answer. Signed-off-by: Chris Mason <chris.mason@oracle.com>
* btrfs-progs: fixup is_mounted checksChris Mason2011-10-27
| | | | | | | | | | /proc/mounts contains device names that don't exist, we end up erroring out because we're not able to stat the device (that doesn't exist). Fix this by allowing the mkfs when the target device doesn't exist. Signed-off-by: Chris Mason <chris.mason@oracle.com>
* Scan the devices listed in /proc/partitionsGoffredo Baroncelli2011-10-25
| | | | | | | | | | | During the commands: - btrfs filesystem show - btrfs device scan the devices "scanned" are extracted from /proc/partitions. This should avoid to scan devices not suitable for a btrfs filesystem like cdrom and floppy or to scan not existant devices. The old behavior (scan all the block devices under /dev) may be forced passing the "--all-devices" switch.
* btrfs-progs: added check_mounted_whereJan Schmidt2011-10-25
| | | | | | | | | new version of check_mounted() returning more information gathered while searching. check_mounted() is now a wrapper for check_mounted_where(). the new version is needed by scrub.c Signed-off-by: Jan Schmidt <list.btrfs@jan-o-sch.net> Signed-off-by: Hugo Mills <hugo@carfax.org.uk>
* mkfs should initialize unused fields properlyJan Schmidt2011-10-25
| | | | | | | | | | | we discovered speed setting is (probably unintentionally) initialized to 1 in make_btrfs(), while being initialized to 0 in btrfs_add_to_fsid(). initialization in make_btrfs() is due to reuse of buf after pwrite() without clearing it. consequently, code like btrfs_set_extent_generation(buf, extent_item, 1); writes to the same location in buf where speed will be placed, later. It may be a good idea to clear buf after each pwrite(), though leaving the struct btrfs_header intact. Signed-off-by: Jan Schmidt <list.btrfs@jan-o-sch.net> Signed-off-by: Hugo Mills <hugo@carfax.org.uk>
* btrfs-progs: setting of time to the root directoryTsutomu Itoh2011-10-25
| | | | | | | | | | | | | | | | | | | | | | | | | | | This patch adds the setting of time to the root directory to the mkfs.btrfs command. As a result, the time of the mount point not correctly displayed comes to be displayed correctly. [before] # mkfs.btrfs /dev/sdd10 # mount /dev/sdd10 /test1 # ls -ld /test1 dr-xr-xr-x 1 root root 0 Jan 1 1970 /test1 [after] # date Tue Nov 16 18:06:05 JST 2010 # mkfs.btrfs /dev/sdd10 # mount /dev/sdd10 /test1 # ls -ld /test1 dr-xr-xr-x 1 root root 0 Nov 16 18:06 /test1 Thanks, Tsutomu Signed-off-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com> Signed-off-by: Hugo Mills <hugo@carfax.org.uk>
* btrfs-progs: add discard support to mkfsChristoph Hellwig2011-10-25
| | | | | | | | Discard the whole device before starting to create the filesystem structures. Modelled after similar support in mkfs.xfs. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Chris Mason <chris.mason@oracle.com>