<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def defragment(path):
    '''
    Defragment mounted BTRFS filesystem.
    In order to defragment a filesystem, device should be properly mounted and writable.

    If passed a device name, then defragmented whole filesystem, mounted on in.
    If passed a moun tpoint of the filesystem, then only this mount point is defragmented.

    CLI Example:

    .. code-block:: bash

        salt '*' btrfs.defragment /dev/sda1
        salt '*' btrfs.defragment /path/on/filesystem
    '''
    is_device = salt.utils.fsutils._is_device(path)
    mounts = salt.utils.fsutils._get_mounts("btrfs")
    if is_device and not mounts.get(path):
        raise CommandExecutionError("Device \"{0}\" is not mounted".format(path))

    result = []
    if is_device:
        for mount_point in mounts[path]:
            result.append(_defragment_mountpoint(mount_point['mount_point']))
    else:
        is_mountpoint = False
        for mountpoints in six.itervalues(mounts):
            for mpnt in mountpoints:
                if path == mpnt['mount_point']:
                    is_mountpoint = True
                    break
        d_res = _defragment_mountpoint(path)
        if not is_mountpoint and not d_res['passed'] and "range ioctl not supported" in d_res['log']:
            d_res['log'] = "Range ioctl defragmentation is not supported in this kernel."

        if not is_mountpoint:
            d_res['mount_point'] = False
            d_res['range'] = os.path.exists(path) and path or False

        result.append(d_res)

    return result