paul@102 | 1 | /* |
paul@106 | 2 | * An opener for a file provided by an Ext2-compatible filesystem. |
paul@102 | 3 | * |
paul@102 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@102 | 5 | * |
paul@102 | 6 | * This program is free software; you can redistribute it and/or |
paul@102 | 7 | * modify it under the terms of the GNU General Public License as |
paul@102 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@102 | 9 | * the License, or (at your option) any later version. |
paul@102 | 10 | * |
paul@102 | 11 | * This program is distributed in the hope that it will be useful, |
paul@102 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@102 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@102 | 14 | * GNU General Public License for more details. |
paul@102 | 15 | * |
paul@102 | 16 | * You should have received a copy of the GNU General Public License |
paul@102 | 17 | * along with this program; if not, write to the Free Software |
paul@102 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@102 | 19 | * Boston, MA 02110-1301, USA |
paul@102 | 20 | */ |
paul@102 | 21 | |
paul@168 | 22 | #include <dirent.h> |
paul@168 | 23 | #include <string.h> |
paul@168 | 24 | |
paul@168 | 25 | #include <thread> |
paul@168 | 26 | |
paul@147 | 27 | #include <e2access/path.h> |
paul@168 | 28 | #include <fsclient/client.h> |
paul@147 | 29 | #include <systypes/fcntl.h> |
paul@106 | 30 | |
paul@106 | 31 | #include "ext2_file_accessor.h" |
paul@106 | 32 | #include "ext2_file_opener.h" |
paul@102 | 33 | |
paul@168 | 34 | |
paul@168 | 35 | |
paul@168 | 36 | /* Common definitions. */ |
paul@168 | 37 | |
paul@168 | 38 | #define DIRENT_CORE_SIZE (sizeof(struct dirent) - sizeof(((struct dirent *) 0)->d_name)) |
paul@168 | 39 | |
paul@168 | 40 | /* File type conversion. */ |
paul@168 | 41 | |
paul@168 | 42 | static int convert_file_type(int type) |
paul@168 | 43 | { |
paul@168 | 44 | switch (type) |
paul@168 | 45 | { |
paul@168 | 46 | case EXT2_FT_REG_FILE: return DT_REG; |
paul@168 | 47 | case EXT2_FT_DIR: return DT_DIR; |
paul@168 | 48 | case EXT2_FT_CHRDEV: return DT_CHR; |
paul@168 | 49 | case EXT2_FT_BLKDEV: return DT_BLK; |
paul@168 | 50 | case EXT2_FT_FIFO: return DT_FIFO; |
paul@168 | 51 | case EXT2_FT_SOCK: return DT_SOCK; |
paul@168 | 52 | case EXT2_FT_SYMLINK: return DT_LNK; |
paul@168 | 53 | default: return DT_UNKNOWN; |
paul@168 | 54 | } |
paul@168 | 55 | } |
paul@168 | 56 | |
paul@168 | 57 | /* Helper function to ensure alignment in generated entries. */ |
paul@168 | 58 | |
paul@168 | 59 | static int pad_align(int value) |
paul@168 | 60 | { |
paul@168 | 61 | return value + (sizeof(unsigned int) - (value % sizeof(unsigned int))); |
paul@168 | 62 | } |
paul@168 | 63 | |
paul@168 | 64 | /* Callback function. */ |
paul@168 | 65 | |
paul@168 | 66 | static int read_directory_entry(struct ext2_dir_entry *dir_entry, int offset, |
paul@168 | 67 | int blocksize, char *buf, void *priv_data) |
paul@168 | 68 | { |
paul@168 | 69 | (void) offset; (void) blocksize; (void) buf; |
paul@168 | 70 | |
paul@168 | 71 | struct Ext2FileOpenerDir *dir = reinterpret_cast<struct Ext2FileOpenerDir *>(priv_data); |
paul@168 | 72 | |
paul@168 | 73 | dir->entry = dir_entry; |
paul@168 | 74 | dir->offset = offset; |
paul@168 | 75 | return dir->opener->write_directory_entry(dir); |
paul@168 | 76 | } |
paul@168 | 77 | |
paul@168 | 78 | /* Thread payload. */ |
paul@168 | 79 | |
paul@168 | 80 | static void _read_directory(Ext2FileOpener *opener, fileid_t fileid, file_t *writer) |
paul@168 | 81 | { |
paul@168 | 82 | /* Subscribe to space and closure notifications on the pipe. */ |
paul@168 | 83 | |
paul@170 | 84 | long err = client_set_blocking(writer, NOTIFY_SPACE_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@168 | 85 | |
paul@168 | 86 | if (err) |
paul@168 | 87 | { |
paul@168 | 88 | client_close(writer); |
paul@168 | 89 | return; |
paul@168 | 90 | } |
paul@168 | 91 | |
paul@168 | 92 | opener->read_directory(fileid, writer); |
paul@168 | 93 | |
paul@168 | 94 | client_close(writer); |
paul@168 | 95 | } |
paul@168 | 96 | |
paul@168 | 97 | |
paul@168 | 98 | |
paul@156 | 99 | Ext2FileOpener::~Ext2FileOpener() |
paul@156 | 100 | { |
paul@156 | 101 | } |
paul@156 | 102 | |
paul@156 | 103 | /* Test if a directory is being accessed. */ |
paul@156 | 104 | |
paul@156 | 105 | bool Ext2FileOpener::accessing_directory(const char *path, flags_t flags, fileid_t fileid) |
paul@156 | 106 | { |
paul@156 | 107 | (void) path; (void) flags; |
paul@156 | 108 | return _ops->is_directory((ext2_ino_t) fileid); |
paul@156 | 109 | } |
paul@156 | 110 | |
paul@156 | 111 | /* Test if a file is being accessed. */ |
paul@156 | 112 | |
paul@156 | 113 | bool Ext2FileOpener::accessing_file(const char *path, flags_t flags, fileid_t fileid) |
paul@156 | 114 | { |
paul@156 | 115 | (void) path; (void) flags; |
paul@156 | 116 | return _ops->is_file((ext2_ino_t) fileid); |
paul@156 | 117 | } |
paul@156 | 118 | |
paul@168 | 119 | // NOTE: This is mostly the same as the HostFileOpener implementation. |
paul@168 | 120 | |
paul@168 | 121 | long Ext2FileOpener::get_directory(const char *path, flags_t flags, fileid_t fileid, offset_t *size, l4_cap_idx_t *cap) |
paul@168 | 122 | { |
paul@168 | 123 | (void) path; (void) flags; |
paul@168 | 124 | |
paul@168 | 125 | file_t *reader, *writer; |
paul@168 | 126 | |
paul@168 | 127 | // NOTE: Might be more appropriate to use lower-level file operations to |
paul@168 | 128 | // NOTE: avoid unnecessary mapping of the reader's memory region. |
paul@168 | 129 | |
paul@168 | 130 | long err = client_pipe(&reader, &writer); |
paul@168 | 131 | |
paul@168 | 132 | if (err) |
paul@168 | 133 | return err; |
paul@168 | 134 | |
paul@168 | 135 | *size = reader->size; |
paul@168 | 136 | *cap = reader->ref; |
paul@168 | 137 | |
paul@168 | 138 | /* Spawn a independent thread for reading the directory details and writing |
paul@168 | 139 | them to the pipe. */ |
paul@168 | 140 | |
paul@168 | 141 | std::thread(_read_directory, this, fileid, writer).detach(); |
paul@168 | 142 | |
paul@170 | 143 | /* Discard the reader structure but preserve the capability. */ |
paul@170 | 144 | |
paul@170 | 145 | reader->ref = L4_INVALID_CAP; |
paul@170 | 146 | file_close(reader); |
paul@170 | 147 | |
paul@170 | 148 | /* Return an indication that the capability will be propagated and not |
paul@170 | 149 | retained. This is explicitly supported by the opener context. */ |
paul@170 | 150 | |
paul@170 | 151 | return IPC_MESSAGE_SENT; |
paul@168 | 152 | } |
paul@168 | 153 | |
paul@168 | 154 | /* Thread payload helper method. */ |
paul@168 | 155 | |
paul@168 | 156 | void Ext2FileOpener::read_directory(fileid_t fileid, file_t *writer) |
paul@168 | 157 | { |
paul@168 | 158 | /* Initialise directory reading state: opener, writer, entry, offset. */ |
paul@168 | 159 | |
paul@168 | 160 | struct Ext2FileOpenerDir dir = {this, writer, NULL, 0}; |
paul@168 | 161 | |
paul@168 | 162 | /* Call the handler function for each directory entry. */ |
paul@168 | 163 | |
paul@168 | 164 | _ops->directory_iterate((ext2_ino_t) fileid, read_directory_entry, &dir); |
paul@168 | 165 | } |
paul@168 | 166 | |
paul@168 | 167 | /* Callback method for directory entry output. */ |
paul@168 | 168 | |
paul@168 | 169 | int Ext2FileOpener::write_directory_entry(struct Ext2FileOpenerDir *dir) |
paul@168 | 170 | { |
paul@168 | 171 | struct ext2_inode inode; |
paul@168 | 172 | |
paul@168 | 173 | /* Obtain the inode details for metadata. */ |
paul@168 | 174 | |
paul@168 | 175 | if (_ops->read_inode(dir->entry->inode, &inode)) |
paul@168 | 176 | return DIRENT_ABORT; |
paul@168 | 177 | |
paul@168 | 178 | /* Align the size of the entry to avoid problems on architectures which |
paul@168 | 179 | require aligned accesses and where the compiler needs to assume an |
paul@168 | 180 | aligned structure. */ |
paul@168 | 181 | |
paul@168 | 182 | offset_t namelen = ext2fs_dirent_name_len(dir->entry); |
paul@168 | 183 | offset_t reclen = pad_align(DIRENT_CORE_SIZE + namelen); |
paul@168 | 184 | |
paul@168 | 185 | /* Construct a directory entry structure of the calculated size. */ |
paul@168 | 186 | |
paul@168 | 187 | char buffer[reclen]; |
paul@168 | 188 | struct dirent *dirent = (struct dirent *) buffer; |
paul@168 | 189 | |
paul@168 | 190 | dirent->d_ino = dir->entry->inode; |
paul@168 | 191 | dirent->d_off = dir->offset; |
paul@168 | 192 | dirent->d_reclen = reclen; |
paul@168 | 193 | dirent->d_type = convert_file_type(ext2fs_dirent_file_type(dir->entry)); |
paul@168 | 194 | |
paul@168 | 195 | /* Copy the name, padding the memory after it to the alignment boundary. */ |
paul@168 | 196 | |
paul@168 | 197 | memcpy(dirent->d_name, dir->entry->name, namelen); |
paul@168 | 198 | memset(dirent->d_name + namelen, 0, reclen - namelen); |
paul@168 | 199 | |
paul@168 | 200 | /* Write the structure to the pipe. */ |
paul@168 | 201 | |
paul@170 | 202 | offset_t nwritten = client_write(dir->writer, (const void *) dirent, reclen); |
paul@168 | 203 | |
paul@170 | 204 | /* Stop writing if the pipe is closed. */ |
paul@170 | 205 | |
paul@170 | 206 | if (nwritten < reclen) |
paul@170 | 207 | return DIRENT_ABORT; |
paul@168 | 208 | |
paul@168 | 209 | return 0; |
paul@168 | 210 | } |
paul@168 | 211 | |
paul@102 | 212 | /* Return a file identifier for the given 'path'. */ |
paul@102 | 213 | |
paul@146 | 214 | long Ext2FileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) |
paul@102 | 215 | { |
paul@106 | 216 | ext2_ino_t ino; |
paul@148 | 217 | const char *remaining; |
paul@148 | 218 | long err = _ops->find_file(path, &ino, &remaining); |
paul@147 | 219 | |
paul@148 | 220 | /* Return an existing file. */ |
paul@147 | 221 | |
paul@148 | 222 | if (!err) |
paul@148 | 223 | { |
paul@148 | 224 | *fileid = (fileid_t) ino; |
paul@148 | 225 | return L4_EOK; |
paul@147 | 226 | } |
paul@106 | 227 | |
paul@148 | 228 | *fileid = FILEID_INVALID; |
paul@148 | 229 | |
paul@148 | 230 | /* Create a missing file if possible. */ |
paul@148 | 231 | |
paul@156 | 232 | if ((flags & O_CREAT) && (flags & ~O_DIRECTORY)) |
paul@148 | 233 | { |
paul@148 | 234 | /* Determine whether only the leafname is left of the path, with |
paul@148 | 235 | the inode number referring to the parent directory. */ |
paul@148 | 236 | |
paul@148 | 237 | if (path_is_leafname(remaining)) |
paul@148 | 238 | { |
paul@148 | 239 | err = _ops->create_file(ino, remaining, _user, &ino); |
paul@148 | 240 | if (err) |
paul@148 | 241 | return err; |
paul@148 | 242 | |
paul@148 | 243 | *fileid = (fileid_t) ino; |
paul@148 | 244 | return L4_EOK; |
paul@148 | 245 | } |
paul@148 | 246 | } |
paul@148 | 247 | |
paul@148 | 248 | return -L4_ENOENT; |
paul@102 | 249 | } |
paul@102 | 250 | |
paul@102 | 251 | /* Return a new accessor for 'fileid'. */ |
paul@102 | 252 | |
paul@143 | 253 | long Ext2FileOpener::make_accessor(const char *path, flags_t flags, fileid_t fileid, Accessor **accessor) |
paul@102 | 254 | { |
paul@143 | 255 | (void) path; (void) flags; |
paul@102 | 256 | |
paul@106 | 257 | ext2_file_t file; |
paul@148 | 258 | long err = _ops->open_file((ext2_ino_t) fileid, &file); |
paul@143 | 259 | |
paul@148 | 260 | if (err) |
paul@148 | 261 | return err; |
paul@106 | 262 | |
paul@148 | 263 | *accessor = new Ext2FileAccessor(_ops, file, fileid); |
paul@143 | 264 | return L4_EOK; |
paul@102 | 265 | } |
paul@102 | 266 | |
paul@102 | 267 | // vim: tabstop=4 expandtab shiftwidth=4 |