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 <utility>
13 
14 #include "pave-utils.h"
15 #include "pave-logging.h"
16 
FlushClient(const block_client::Client & client)17 zx_status_t FlushClient(const block_client::Client& client) {
18     block_fifo_request_t request;
19     request.group = 0;
20     request.vmoid = VMOID_INVALID;
21     request.opcode = BLOCKIO_FLUSH;
22     request.length = 0;
23     request.vmo_offset = 0;
24     request.dev_offset = 0;
25 
26     zx_status_t status = client.Transaction(&request, 1);
27     if (status != ZX_OK) {
28         ERROR("Error flushing: %s\n", zx_status_get_string(status));
29         return status;
30     }
31     return ZX_OK;
32 }
33 
FlushBlockDevice(const fbl::unique_fd & fd)34 zx_status_t FlushBlockDevice(const fbl::unique_fd& fd) {
35     zx::fifo fifo;
36     ssize_t result = ioctl_block_get_fifos(fd.get(), fifo.reset_and_get_address());
37     if (result < 0) {
38         ERROR("Couldn't attach fifo to partition\n");
39         return static_cast<zx_status_t>(result);
40     }
41 
42     block_client::Client client;
43     zx_status_t status = block_client::Client::Create(std::move(fifo), &client);
44     if (status != ZX_OK) {
45         ERROR("Couldn't create block client\n");
46         return status;
47     }
48 
49     return FlushClient(client);
50 }
51