You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by "zjregee (via GitHub)" <gi...@apache.org> on 2024/06/24 02:31:10 UTC

Re: [PR] feat(ovfs): add filesystem to handle message [opendal]

zjregee commented on code in PR #4720:
URL: https://github.com/apache/opendal/pull/4720#discussion_r1649565554


##########
integrations/virtiofs/src/filesystem.rs:
##########
@@ -0,0 +1,129 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::io::Write;
+use std::mem::size_of;
+
+use vm_memory::ByteValued;
+
+use crate::error::*;
+use crate::filesystem_message::*;
+use crate::virtiofs_util::{Reader, Writer};
+
+/// Version number of this interface.
+const KERNEL_VERSION: u32 = 7;
+/// Minor version number of this interface.
+const KERNEL_MINOR_VERSION: u32 = 38;
+/// Minimum Minor version number supported.
+const MIN_KERNEL_MINOR_VERSION: u32 = 27;
+/// The length of the header part of the message.
+const BUFFER_HEADER_SIZE: u32 = 256;
+/// The maximum length of the data part of the message, used for read/write data.
+const MAX_BUFFER_SIZE: u32 = 1 << 20;
+
+/// Filesystem is a filesystem implementation with opendal backend,
+/// and will decode and process messages from VMs.
+pub struct Filesystem {}
+
+#[allow(dead_code)]
+impl Filesystem {
+    pub fn new() -> Filesystem {
+        Filesystem {}
+    }
+
+    pub fn handle_message(&self, mut r: Reader, w: Writer) -> Result<usize> {
+        let in_header: InHeader = r
+            .read_obj()
+            .map_err(|e| new_vhost_user_fs_error("", Some(e.into())))?;
+        if in_header.len > (MAX_BUFFER_SIZE + BUFFER_HEADER_SIZE) {
+            // The message is too long here.
+            return reply_error(in_header.unique, w);
+        }
+        if let Ok(opcode) = Opcode::try_from(in_header.opcode) {
+            match opcode {
+                Opcode::Init => self.init(in_header, r, w),
+            }
+        } else {
+            reply_error(in_header.unique, w)
+        }
+    }
+
+    fn init(&self, in_header: InHeader, mut r: Reader, w: Writer) -> Result<usize> {
+        let InitIn { major, minor, .. } = r.read_obj().map_err(|e| {
+            new_vhost_user_fs_error("failed to decode protocol messages", Some(e.into()))
+        })?;
+
+        if major != KERNEL_VERSION || minor < MIN_KERNEL_MINOR_VERSION {
+            return reply_error(in_header.unique, w);
+        }
+
+        // We will directly return ok and do nothing for now.
+        let out = InitOut {
+            major: KERNEL_VERSION,
+            minor: KERNEL_MINOR_VERSION,
+            max_write: MAX_BUFFER_SIZE,
+            ..Default::default()
+        };
+        reply_ok(Some(out), None, in_header.unique, w)
+    }
+}
+
+fn reply_ok<T: ByteValued>(
+    out: Option<T>,
+    data: Option<&[u8]>,
+    unique: u64,
+    mut w: Writer,

Review Comment:
   It seems unnecessary. If I add a function called write_objs to the writer, I still need to encapsulate a reply_ok in the filesystem. If I move reply_ok directly to the writer, it will introduce certain semantics that the writer does not need to be aware of.
   
   I now put both methods reply_ok reply_error inside the filesystem.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org