Skip to content

Commit 3824bae

Browse files
committed
api for trim filesystem operation
1 parent d590929 commit 3824bae

3 files changed

Lines changed: 205 additions & 10 deletions

File tree

Sources/Containerization/SandboxContext/SandboxContext.pb.swift

Lines changed: 173 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,22 @@ public struct Com_Apple_Containerization_Sandbox_V3_StatResponse: @unchecked Sen
10541054
fileprivate var _storage = _StorageClass.defaultInstance
10551055
}
10561056

1057+
public struct Com_Apple_Containerization_Sandbox_V3_FiTrimParams: Sendable {
1058+
// SwiftProtobuf.Message conformance is added in an extension below. See the
1059+
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
1060+
// methods supported on all messages.
1061+
1062+
public var start: UInt64 = 0
1063+
1064+
public var len: UInt64 = 0
1065+
1066+
public var minimumLen: UInt64 = 0
1067+
1068+
public var unknownFields = SwiftProtobuf.UnknownStorage()
1069+
1070+
public init() {}
1071+
}
1072+
10571073
public struct Com_Apple_Containerization_Sandbox_V3_FiFreezeParams: Sendable {
10581074
// SwiftProtobuf.Message conformance is added in an extension below. See the
10591075
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
@@ -1074,6 +1090,18 @@ public struct Com_Apple_Containerization_Sandbox_V3_FiThawParams: Sendable {
10741090
public init() {}
10751091
}
10761092

1093+
public struct Com_Apple_Containerization_Sandbox_V3_FiTrimResult: Sendable {
1094+
// SwiftProtobuf.Message conformance is added in an extension below. See the
1095+
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
1096+
// methods supported on all messages.
1097+
1098+
public var trimmedBytes: UInt64 = 0
1099+
1100+
public var unknownFields = SwiftProtobuf.UnknownStorage()
1101+
1102+
public init() {}
1103+
}
1104+
10771105
public struct Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest: Sendable {
10781106
// SwiftProtobuf.Message conformance is added in an extension below. See the
10791107
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
@@ -1083,6 +1111,14 @@ public struct Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest:
10831111

10841112
public var operation: Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest.OneOf_Operation? = nil
10851113

1114+
public var trim: Com_Apple_Containerization_Sandbox_V3_FiTrimParams {
1115+
get {
1116+
if case .trim(let v)? = operation {return v}
1117+
return Com_Apple_Containerization_Sandbox_V3_FiTrimParams()
1118+
}
1119+
set {operation = .trim(newValue)}
1120+
}
1121+
10861122
public var freeze: Com_Apple_Containerization_Sandbox_V3_FiFreezeParams {
10871123
get {
10881124
if case .freeze(let v)? = operation {return v}
@@ -1102,6 +1138,7 @@ public struct Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest:
11021138
public var unknownFields = SwiftProtobuf.UnknownStorage()
11031139

11041140
public enum OneOf_Operation: Equatable, Sendable {
1141+
case trim(Com_Apple_Containerization_Sandbox_V3_FiTrimParams)
11051142
case freeze(Com_Apple_Containerization_Sandbox_V3_FiFreezeParams)
11061143
case thaw(Com_Apple_Containerization_Sandbox_V3_FiThawParams)
11071144

@@ -1115,8 +1152,23 @@ public struct Com_Apple_Containerization_Sandbox_V3_FilesystemOperationResponse:
11151152
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
11161153
// methods supported on all messages.
11171154

1155+
public var result: Com_Apple_Containerization_Sandbox_V3_FilesystemOperationResponse.OneOf_Result? = nil
1156+
1157+
public var trim: Com_Apple_Containerization_Sandbox_V3_FiTrimResult {
1158+
get {
1159+
if case .trim(let v)? = result {return v}
1160+
return Com_Apple_Containerization_Sandbox_V3_FiTrimResult()
1161+
}
1162+
set {result = .trim(newValue)}
1163+
}
1164+
11181165
public var unknownFields = SwiftProtobuf.UnknownStorage()
11191166

1167+
public enum OneOf_Result: Equatable, Sendable {
1168+
case trim(Com_Apple_Containerization_Sandbox_V3_FiTrimResult)
1169+
1170+
}
1171+
11201172
public init() {}
11211173
}
11221174

@@ -3194,6 +3246,46 @@ extension Com_Apple_Containerization_Sandbox_V3_StatResponse: SwiftProtobuf.Mess
31943246
}
31953247
}
31963248

3249+
extension Com_Apple_Containerization_Sandbox_V3_FiTrimParams: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
3250+
public static let protoMessageName: String = _protobuf_package + ".FiTrimParams"
3251+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}start\0\u{1}len\0\u{3}minimum_len\0")
3252+
3253+
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
3254+
while let fieldNumber = try decoder.nextFieldNumber() {
3255+
// The use of inline closures is to circumvent an issue where the compiler
3256+
// allocates stack space for every case branch when no optimizations are
3257+
// enabled. https://github.com/apple/swift-protobuf/issues/1034
3258+
switch fieldNumber {
3259+
case 1: try { try decoder.decodeSingularUInt64Field(value: &self.start) }()
3260+
case 2: try { try decoder.decodeSingularUInt64Field(value: &self.len) }()
3261+
case 3: try { try decoder.decodeSingularUInt64Field(value: &self.minimumLen) }()
3262+
default: break
3263+
}
3264+
}
3265+
}
3266+
3267+
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
3268+
if self.start != 0 {
3269+
try visitor.visitSingularUInt64Field(value: self.start, fieldNumber: 1)
3270+
}
3271+
if self.len != 0 {
3272+
try visitor.visitSingularUInt64Field(value: self.len, fieldNumber: 2)
3273+
}
3274+
if self.minimumLen != 0 {
3275+
try visitor.visitSingularUInt64Field(value: self.minimumLen, fieldNumber: 3)
3276+
}
3277+
try unknownFields.traverse(visitor: &visitor)
3278+
}
3279+
3280+
public static func ==(lhs: Com_Apple_Containerization_Sandbox_V3_FiTrimParams, rhs: Com_Apple_Containerization_Sandbox_V3_FiTrimParams) -> Bool {
3281+
if lhs.start != rhs.start {return false}
3282+
if lhs.len != rhs.len {return false}
3283+
if lhs.minimumLen != rhs.minimumLen {return false}
3284+
if lhs.unknownFields != rhs.unknownFields {return false}
3285+
return true
3286+
}
3287+
}
3288+
31973289
extension Com_Apple_Containerization_Sandbox_V3_FiFreezeParams: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
31983290
public static let protoMessageName: String = _protobuf_package + ".FiFreezeParams"
31993291
public static let _protobuf_nameMap = SwiftProtobuf._NameMap()
@@ -3232,9 +3324,39 @@ extension Com_Apple_Containerization_Sandbox_V3_FiThawParams: SwiftProtobuf.Mess
32323324
}
32333325
}
32343326

3327+
extension Com_Apple_Containerization_Sandbox_V3_FiTrimResult: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
3328+
public static let protoMessageName: String = _protobuf_package + ".FiTrimResult"
3329+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{3}trimmed_bytes\0")
3330+
3331+
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
3332+
while let fieldNumber = try decoder.nextFieldNumber() {
3333+
// The use of inline closures is to circumvent an issue where the compiler
3334+
// allocates stack space for every case branch when no optimizations are
3335+
// enabled. https://github.com/apple/swift-protobuf/issues/1034
3336+
switch fieldNumber {
3337+
case 1: try { try decoder.decodeSingularUInt64Field(value: &self.trimmedBytes) }()
3338+
default: break
3339+
}
3340+
}
3341+
}
3342+
3343+
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
3344+
if self.trimmedBytes != 0 {
3345+
try visitor.visitSingularUInt64Field(value: self.trimmedBytes, fieldNumber: 1)
3346+
}
3347+
try unknownFields.traverse(visitor: &visitor)
3348+
}
3349+
3350+
public static func ==(lhs: Com_Apple_Containerization_Sandbox_V3_FiTrimResult, rhs: Com_Apple_Containerization_Sandbox_V3_FiTrimResult) -> Bool {
3351+
if lhs.trimmedBytes != rhs.trimmedBytes {return false}
3352+
if lhs.unknownFields != rhs.unknownFields {return false}
3353+
return true
3354+
}
3355+
}
3356+
32353357
extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
32363358
public static let protoMessageName: String = _protobuf_package + ".FilesystemOperationRequest"
3237-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}path\0\u{1}freeze\0\u{1}thaw\0")
3359+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}path\0\u{1}trim\0\u{1}freeze\0\u{1}thaw\0")
32383360

32393361
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
32403362
while let fieldNumber = try decoder.nextFieldNumber() {
@@ -3244,6 +3366,19 @@ extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest: Swif
32443366
switch fieldNumber {
32453367
case 1: try { try decoder.decodeSingularStringField(value: &self.path) }()
32463368
case 2: try {
3369+
var v: Com_Apple_Containerization_Sandbox_V3_FiTrimParams?
3370+
var hadOneofValue = false
3371+
if let current = self.operation {
3372+
hadOneofValue = true
3373+
if case .trim(let m) = current {v = m}
3374+
}
3375+
try decoder.decodeSingularMessageField(value: &v)
3376+
if let v = v {
3377+
if hadOneofValue {try decoder.handleConflictingOneOf()}
3378+
self.operation = .trim(v)
3379+
}
3380+
}()
3381+
case 3: try {
32473382
var v: Com_Apple_Containerization_Sandbox_V3_FiFreezeParams?
32483383
var hadOneofValue = false
32493384
if let current = self.operation {
@@ -3256,7 +3391,7 @@ extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest: Swif
32563391
self.operation = .freeze(v)
32573392
}
32583393
}()
3259-
case 3: try {
3394+
case 4: try {
32603395
var v: Com_Apple_Containerization_Sandbox_V3_FiThawParams?
32613396
var hadOneofValue = false
32623397
if let current = self.operation {
@@ -3283,13 +3418,17 @@ extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest: Swif
32833418
try visitor.visitSingularStringField(value: self.path, fieldNumber: 1)
32843419
}
32853420
switch self.operation {
3421+
case .trim?: try {
3422+
guard case .trim(let v)? = self.operation else { preconditionFailure() }
3423+
try visitor.visitSingularMessageField(value: v, fieldNumber: 2)
3424+
}()
32863425
case .freeze?: try {
32873426
guard case .freeze(let v)? = self.operation else { preconditionFailure() }
3288-
try visitor.visitSingularMessageField(value: v, fieldNumber: 2)
3427+
try visitor.visitSingularMessageField(value: v, fieldNumber: 3)
32893428
}()
32903429
case .thaw?: try {
32913430
guard case .thaw(let v)? = self.operation else { preconditionFailure() }
3292-
try visitor.visitSingularMessageField(value: v, fieldNumber: 3)
3431+
try visitor.visitSingularMessageField(value: v, fieldNumber: 4)
32933432
}()
32943433
case nil: break
32953434
}
@@ -3306,18 +3445,45 @@ extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest: Swif
33063445

33073446
extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationResponse: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
33083447
public static let protoMessageName: String = _protobuf_package + ".FilesystemOperationResponse"
3309-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap()
3448+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}trim\0")
33103449

33113450
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
3312-
// Load everything into unknown fields
3313-
while try decoder.nextFieldNumber() != nil {}
3451+
while let fieldNumber = try decoder.nextFieldNumber() {
3452+
// The use of inline closures is to circumvent an issue where the compiler
3453+
// allocates stack space for every case branch when no optimizations are
3454+
// enabled. https://github.com/apple/swift-protobuf/issues/1034
3455+
switch fieldNumber {
3456+
case 1: try {
3457+
var v: Com_Apple_Containerization_Sandbox_V3_FiTrimResult?
3458+
var hadOneofValue = false
3459+
if let current = self.result {
3460+
hadOneofValue = true
3461+
if case .trim(let m) = current {v = m}
3462+
}
3463+
try decoder.decodeSingularMessageField(value: &v)
3464+
if let v = v {
3465+
if hadOneofValue {try decoder.handleConflictingOneOf()}
3466+
self.result = .trim(v)
3467+
}
3468+
}()
3469+
default: break
3470+
}
3471+
}
33143472
}
33153473

33163474
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
3475+
// The use of inline closures is to circumvent an issue where the compiler
3476+
// allocates stack space for every if/case branch local when no optimizations
3477+
// are enabled. https://github.com/apple/swift-protobuf/issues/1034 and
3478+
// https://github.com/apple/swift-protobuf/issues/1182
3479+
try { if case .trim(let v)? = self.result {
3480+
try visitor.visitSingularMessageField(value: v, fieldNumber: 1)
3481+
} }()
33173482
try unknownFields.traverse(visitor: &visitor)
33183483
}
33193484

33203485
public static func ==(lhs: Com_Apple_Containerization_Sandbox_V3_FilesystemOperationResponse, rhs: Com_Apple_Containerization_Sandbox_V3_FilesystemOperationResponse) -> Bool {
3486+
if lhs.result != rhs.result {return false}
33213487
if lhs.unknownFields != rhs.unknownFields {return false}
33223488
return true
33233489
}

Sources/Containerization/SandboxContext/SandboxContext.proto

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,18 +294,32 @@ message StatResponse {
294294
string error = 2; // Non-empty if stat failed.
295295
}
296296

297+
message FiTrimParams {
298+
uint64 start = 1;
299+
uint64 len = 2;
300+
uint64 minimum_len = 3;
301+
}
297302
message FiFreezeParams {}
298303
message FiThawParams {}
299304

305+
message FiTrimResult {
306+
uint64 trimmed_bytes = 1;
307+
}
308+
300309
message FilesystemOperationRequest {
301310
string path = 1;
302311
oneof operation {
303-
FiFreezeParams freeze = 2;
304-
FiThawParams thaw = 3;
312+
FiTrimParams trim = 2;
313+
FiFreezeParams freeze = 3;
314+
FiThawParams thaw = 4;
305315
}
306316
}
307317

308-
message FilesystemOperationResponse {}
318+
message FilesystemOperationResponse {
319+
oneof result {
320+
FiTrimResult trim = 1;
321+
}
322+
}
309323

310324
message IpLinkSetRequest {
311325
string interface = 1;

vminitd/Sources/VminitdCore/Server+GRPC.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,8 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContext.SimpleServ
709709
try freezeFilesystem(fd: fd)
710710
case .thaw:
711711
try thawFilesystem(fd: fd)
712+
case .trim(let params):
713+
try trimFilesystem(fd: fd, params: params)
712714
case .none:
713715
throw RPCError(code: .invalidArgument, message: "invalid operation")
714716
}
@@ -742,6 +744,19 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContext.SimpleServ
742744
}
743745
}
744746

747+
private static let FITRIM: UInt = 0xC004_5879
748+
749+
private func trimFilesystem(fd: Int32, params: Com_Apple_Containerization_Sandbox_V3_FiTrimParams) throws {
750+
751+
// TODO logic for trim filesystem
752+
753+
let rc: CInt = ioctl(fd, FITRIM, 0)
754+
if rc != 0 {
755+
let error = swiftErrno("ioctl(FITRIM)")
756+
throw RPCError(code: .internalError, message: "trim failed", cause: error)
757+
}
758+
}
759+
745760
public func umount(request: Com_Apple_Containerization_Sandbox_V3_UmountRequest, context: GRPCCore.ServerContext)
746761
async throws -> Com_Apple_Containerization_Sandbox_V3_UmountResponse
747762
{

0 commit comments

Comments
 (0)