Skip to content

Commit f104735

Browse files
gilhooleydCQ Bot
authored andcommitted
[ramdevice_client] Separate RamNand and RamNandCtl
Move around the functions for RamNand so that only RamNandCtl touches the Isolated Devmgr code. This is in preparation for making the Isolated Devmgr code testonly. Bug: 84322 Change-Id: I05407d91050bc10452647c6247c5d48e0cd6899c Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/579423 Reviewed-by: Suraj Malhotra <surajmalhotra@google.com> Commit-Queue: David Gilhooley <dgilhooley@google.com>
1 parent 87eb7da commit f104735

File tree

2 files changed

+59
-68
lines changed

2 files changed

+59
-68
lines changed

src/lib/storage/ramdevice_client/cpp/include/ramdevice-client/ramnand.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class RamNandCtl : public fbl::RefCounted<RamNandCtl> {
2626
// Creates an isolated devmgr and spawns a ram_nand_ctl device in it.
2727
static zx_status_t Create(fbl::RefPtr<RamNandCtl>* out);
2828

29+
static zx_status_t CreateWithRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
30+
std::optional<RamNand>* out);
31+
32+
zx_status_t CreateRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
33+
std::optional<RamNand>* out);
34+
2935
~RamNandCtl() = default;
3036

3137
const fbl::unique_fd& fd() { return ctl_; }
@@ -47,15 +53,6 @@ class RamNand {
4753
static zx_status_t Create(const fuchsia_hardware_nand_RamNandInfo* config,
4854
std::optional<RamNand>* out);
4955

50-
// Creates a ram_nand device underneath the ram_nand_ctl.
51-
static zx_status_t Create(fbl::RefPtr<RamNandCtl> ctl,
52-
const fuchsia_hardware_nand_RamNandInfo* config,
53-
std::optional<RamNand>* out);
54-
55-
// Creates a ram_nand_ctl device and then a ram_device underneath.
56-
static zx_status_t CreateIsolated(const fuchsia_hardware_nand_RamNandInfo* config,
57-
std::optional<RamNand>* out);
58-
5956
// Not copyable.
6057
RamNand(const RamNand&) = delete;
6158
RamNand& operator=(const RamNand&) = delete;
@@ -84,14 +81,12 @@ class RamNand {
8481
return nullptr;
8582
}
8683

87-
const fbl::unique_fd& devfs_root() { return parent_->devfs_root(); }
84+
explicit RamNand(fbl::unique_fd fd)
85+
: fd_(std::move(fd)), path_(std::nullopt), filename_(std::nullopt) {}
8886

8987
private:
90-
RamNand(fbl::unique_fd fd, fbl::RefPtr<RamNandCtl> ctl)
91-
: fd_(std::move(fd)), path_(std::nullopt), filename_(std::nullopt), parent_(ctl) {}
92-
9388
RamNand(fbl::unique_fd fd, fbl::String path, fbl::String filename)
94-
: fd_(std::move(fd)), path_(path), filename_(filename), parent_(nullptr) {}
89+
: fd_(std::move(fd)), path_(path), filename_(filename) {}
9590

9691
fbl::unique_fd fd_;
9792
bool unbind = true;
@@ -101,9 +96,6 @@ class RamNand {
10196

10297
// Only valid if not spawned in an isolated devmgr.
10398
std::optional<fbl::String> filename_;
104-
105-
// Optional parent if spawned in an isolated devmgr.
106-
fbl::RefPtr<RamNandCtl> parent_;
10799
};
108100

109101
} // namespace ramdevice_client

src/lib/storage/ramdevice_client/cpp/ramnand.cc

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -84,99 +84,98 @@ zx_status_t RamNandCtl::Create(fbl::RefPtr<RamNandCtl>* out) {
8484
}
8585

8686
__EXPORT
87-
zx_status_t RamNand::Create(const fuchsia_hardware_nand_RamNandInfo* config,
88-
std::optional<RamNand>* out) {
89-
fbl::unique_fd control(open(kBasePath, O_RDWR));
90-
91-
zx::channel ctl_svc;
92-
zx_status_t st = fdio_get_service_handle(control.release(), ctl_svc.reset_and_get_address());
93-
if (st != ZX_OK) {
94-
return st;
87+
zx_status_t RamNandCtl::CreateRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
88+
std::optional<RamNand>* out) {
89+
fdio_t* io = fdio_unsafe_fd_to_io(fd().get());
90+
if (io == NULL) {
91+
fprintf(stderr, "Could not get fdio object\n");
92+
return ZX_ERR_INTERNAL;
9593
}
94+
zx_handle_t ctl_svc = fdio_unsafe_borrow_channel(io);
9695

9796
char name[fuchsia_hardware_nand_NAME_LEN + 1];
9897
size_t out_name_size;
9998
zx_status_t status;
100-
st = fuchsia_hardware_nand_RamNandCtlCreateDevice(ctl_svc.get(), config, &status, name,
101-
fuchsia_hardware_nand_NAME_LEN, &out_name_size);
99+
zx_status_t st = fuchsia_hardware_nand_RamNandCtlCreateDevice(
100+
ctl_svc, config, &status, name, fuchsia_hardware_nand_NAME_LEN, &out_name_size);
101+
fdio_unsafe_release(io);
102102
if (st != ZX_OK || status != ZX_OK) {
103103
st = st != ZX_OK ? st : status;
104104
fprintf(stderr, "Could not create ram_nand device, %d\n", st);
105105
return st;
106106
}
107107
name[out_name_size] = '\0';
108+
109+
// TODO(fxbug.dev/33003): We should be able to open relative to ctl->fd(), but
110+
// due to a bug, we have to be relative to devfs_root instead.
108111
fbl::StringBuffer<PATH_MAX> path;
109-
path.Append(kBasePath);
110-
path.Append("/");
112+
path.Append("sys/platform/00:00:2e/nand-ctl/");
111113
path.Append(name);
114+
fprintf(stderr, "Trying to open (%s)\n", path.c_str());
112115

113-
fbl::unique_fd ram_nand_ctl(open(kBasePath, O_RDONLY | O_DIRECTORY));
114-
if (!ram_nand_ctl) {
115-
fprintf(stderr, "Could not open ram_nand_ctl");
116-
return ZX_ERR_INTERNAL;
117-
}
118-
119-
fbl::unique_fd ram_nand;
120-
st = WaitForFile(ram_nand_ctl, name, &ram_nand);
116+
fbl::unique_fd fd;
117+
st = devmgr_integration_test::RecursiveWaitForFile(devfs_root(), path.c_str(), &fd);
121118
if (st != ZX_OK) {
122-
fprintf(stderr, "Could not open ram_nand\n");
123119
return st;
124120
}
125121

126-
*out = RamNand(std::move(ram_nand), path.ToString(), fbl::String(name));
122+
*out = RamNand(std::move(fd));
127123
return ZX_OK;
128124
}
129125

130126
__EXPORT
131-
zx_status_t RamNand::Create(fbl::RefPtr<RamNandCtl> ctl,
132-
const fuchsia_hardware_nand_RamNandInfo* config,
127+
zx_status_t RamNandCtl::CreateWithRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
128+
std::optional<RamNand>* out) {
129+
fbl::RefPtr<RamNandCtl> ctl;
130+
zx_status_t st = RamNandCtl::Create(&ctl);
131+
if (st != ZX_OK) {
132+
return st;
133+
}
134+
return ctl->CreateRamNand(config, out);
135+
}
136+
137+
__EXPORT
138+
zx_status_t RamNand::Create(const fuchsia_hardware_nand_RamNandInfo* config,
133139
std::optional<RamNand>* out) {
134-
fdio_t* io = fdio_unsafe_fd_to_io(ctl->fd().get());
135-
if (io == NULL) {
136-
fprintf(stderr, "Could not get fdio object\n");
137-
return ZX_ERR_INTERNAL;
140+
fbl::unique_fd control(open(kBasePath, O_RDWR));
141+
142+
zx::channel ctl_svc;
143+
zx_status_t st = fdio_get_service_handle(control.release(), ctl_svc.reset_and_get_address());
144+
if (st != ZX_OK) {
145+
return st;
138146
}
139-
zx_handle_t ctl_svc = fdio_unsafe_borrow_channel(io);
140147

141148
char name[fuchsia_hardware_nand_NAME_LEN + 1];
142149
size_t out_name_size;
143150
zx_status_t status;
144-
zx_status_t st = fuchsia_hardware_nand_RamNandCtlCreateDevice(
145-
ctl_svc, config, &status, name, fuchsia_hardware_nand_NAME_LEN, &out_name_size);
146-
fdio_unsafe_release(io);
151+
st = fuchsia_hardware_nand_RamNandCtlCreateDevice(ctl_svc.get(), config, &status, name,
152+
fuchsia_hardware_nand_NAME_LEN, &out_name_size);
147153
if (st != ZX_OK || status != ZX_OK) {
148154
st = st != ZX_OK ? st : status;
149155
fprintf(stderr, "Could not create ram_nand device, %d\n", st);
150156
return st;
151157
}
152158
name[out_name_size] = '\0';
153-
154-
// TODO(fxbug.dev/33003): We should be able to open relative to ctl->fd(), but
155-
// due to a bug, we have to be relative to devfs_root instead.
156159
fbl::StringBuffer<PATH_MAX> path;
157-
path.Append("sys/platform/00:00:2e/nand-ctl/");
160+
path.Append(kBasePath);
161+
path.Append("/");
158162
path.Append(name);
159-
fprintf(stderr, "Trying to open (%s)\n", path.c_str());
160163

161-
fbl::unique_fd fd;
162-
st = devmgr_integration_test::RecursiveWaitForFile(ctl->devfs_root(), path.c_str(), &fd);
163-
if (st != ZX_OK) {
164-
return st;
164+
fbl::unique_fd ram_nand_ctl(open(kBasePath, O_RDONLY | O_DIRECTORY));
165+
if (!ram_nand_ctl) {
166+
fprintf(stderr, "Could not open ram_nand_ctl");
167+
return ZX_ERR_INTERNAL;
165168
}
166169

167-
*out = RamNand(std::move(fd), std::move(ctl));
168-
return ZX_OK;
169-
}
170-
171-
__EXPORT
172-
zx_status_t RamNand::CreateIsolated(const fuchsia_hardware_nand_RamNandInfo* config,
173-
std::optional<RamNand>* out) {
174-
fbl::RefPtr<RamNandCtl> ctl;
175-
zx_status_t st = RamNandCtl::Create(&ctl);
170+
fbl::unique_fd ram_nand;
171+
st = WaitForFile(ram_nand_ctl, name, &ram_nand);
176172
if (st != ZX_OK) {
173+
fprintf(stderr, "Could not open ram_nand\n");
177174
return st;
178175
}
179-
return Create(std::move(ctl), config, out);
176+
177+
*out = RamNand(std::move(ram_nand), path.ToString(), fbl::String(name));
178+
return ZX_OK;
180179
}
181180

182181
__EXPORT

0 commit comments

Comments
 (0)