1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <stdio.h>
6
7#include <block-client/cpp/client.h>
8#include <fbl/unique_fd.h>
9#include <lib/zx/fifo.h>
10#include <zircon/status.h>
11
12#include "pave-utils.h"
13#include "pave-logging.h"
14
15zx_status_t FlushClient(const block_client::Client& client) {
16    block_fifo_request_t request;
17    request.group = 0;
18    request.vmoid = VMOID_INVALID;
19    request.opcode = BLOCKIO_FLUSH;
20    request.length = 0;
21    request.vmo_offset = 0;
22    request.dev_offset = 0;
23
24    zx_status_t status = client.Transaction(&request, 1);
25    if (status != ZX_OK) {
26        ERROR("Error flushing: %s\n", zx_status_get_string(status));
27        return status;
28    }
29    return ZX_OK;
30}
31
32zx_status_t FlushBlockDevice(const fbl::unique_fd& fd) {
33    zx::fifo fifo;
34    ssize_t result = ioctl_block_get_fifos(fd.get(), fifo.reset_and_get_address());
35    if (result < 0) {
36        ERROR("Couldn't attach fifo to partition\n");
37        return static_cast<zx_status_t>(result);
38    }
39
40    block_client::Client client;
41    zx_status_t status = block_client::Client::Create(fbl::move(fifo), &client);
42    if (status != ZX_OK) {
43        ERROR("Couldn't create block client\n");
44        return status;
45    }
46
47    return FlushClient(client);
48}
49