Skip to content

Commit 57668cc

Browse files
committed
api for trim filesystem operation
1 parent b7fbbe5 commit 57668cc

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

@@ -3204,6 +3256,46 @@ extension Com_Apple_Containerization_Sandbox_V3_StatResponse: SwiftProtobuf.Mess
32043256
}
32053257
}
32063258

3259+
extension Com_Apple_Containerization_Sandbox_V3_FiTrimParams: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
3260+
public static let protoMessageName: String = _protobuf_package + ".FiTrimParams"
3261+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}start\0\u{1}len\0\u{3}minimum_len\0")
3262+
3263+
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
3264+
while let fieldNumber = try decoder.nextFieldNumber() {
3265+
// The use of inline closures is to circumvent an issue where the compiler
3266+
// allocates stack space for every case branch when no optimizations are
3267+
// enabled. https://github.com/apple/swift-protobuf/issues/1034
3268+
switch fieldNumber {
3269+
case 1: try { try decoder.decodeSingularUInt64Field(value: &self.start) }()
3270+
case 2: try { try decoder.decodeSingularUInt64Field(value: &self.len) }()
3271+
case 3: try { try decoder.decodeSingularUInt64Field(value: &self.minimumLen) }()
3272+
default: break
3273+
}
3274+
}
3275+
}
3276+
3277+
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
3278+
if self.start != 0 {
3279+
try visitor.visitSingularUInt64Field(value: self.start, fieldNumber: 1)
3280+
}
3281+
if self.len != 0 {
3282+
try visitor.visitSingularUInt64Field(value: self.len, fieldNumber: 2)
3283+
}
3284+
if self.minimumLen != 0 {
3285+
try visitor.visitSingularUInt64Field(value: self.minimumLen, fieldNumber: 3)
3286+
}
3287+
try unknownFields.traverse(visitor: &visitor)
3288+
}
3289+
3290+
public static func ==(lhs: Com_Apple_Containerization_Sandbox_V3_FiTrimParams, rhs: Com_Apple_Containerization_Sandbox_V3_FiTrimParams) -> Bool {
3291+
if lhs.start != rhs.start {return false}
3292+
if lhs.len != rhs.len {return false}
3293+
if lhs.minimumLen != rhs.minimumLen {return false}
3294+
if lhs.unknownFields != rhs.unknownFields {return false}
3295+
return true
3296+
}
3297+
}
3298+
32073299
extension Com_Apple_Containerization_Sandbox_V3_FiFreezeParams: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
32083300
public static let protoMessageName: String = _protobuf_package + ".FiFreezeParams"
32093301
public static let _protobuf_nameMap = SwiftProtobuf._NameMap()
@@ -3242,9 +3334,39 @@ extension Com_Apple_Containerization_Sandbox_V3_FiThawParams: SwiftProtobuf.Mess
32423334
}
32433335
}
32443336

3337+
extension Com_Apple_Containerization_Sandbox_V3_FiTrimResult: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
3338+
public static let protoMessageName: String = _protobuf_package + ".FiTrimResult"
3339+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{3}trimmed_bytes\0")
3340+
3341+
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
3342+
while let fieldNumber = try decoder.nextFieldNumber() {
3343+
// The use of inline closures is to circumvent an issue where the compiler
3344+
// allocates stack space for every case branch when no optimizations are
3345+
// enabled. https://github.com/apple/swift-protobuf/issues/1034
3346+
switch fieldNumber {
3347+
case 1: try { try decoder.decodeSingularUInt64Field(value: &self.trimmedBytes) }()
3348+
default: break
3349+
}
3350+
}
3351+
}
3352+
3353+
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
3354+
if self.trimmedBytes != 0 {
3355+
try visitor.visitSingularUInt64Field(value: self.trimmedBytes, fieldNumber: 1)
3356+
}
3357+
try unknownFields.traverse(visitor: &visitor)
3358+
}
3359+
3360+
public static func ==(lhs: Com_Apple_Containerization_Sandbox_V3_FiTrimResult, rhs: Com_Apple_Containerization_Sandbox_V3_FiTrimResult) -> Bool {
3361+
if lhs.trimmedBytes != rhs.trimmedBytes {return false}
3362+
if lhs.unknownFields != rhs.unknownFields {return false}
3363+
return true
3364+
}
3365+
}
3366+
32453367
extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
32463368
public static let protoMessageName: String = _protobuf_package + ".FilesystemOperationRequest"
3247-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}path\0\u{1}freeze\0\u{1}thaw\0")
3369+
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")
32483370

32493371
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
32503372
while let fieldNumber = try decoder.nextFieldNumber() {
@@ -3254,6 +3376,19 @@ extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest: Swif
32543376
switch fieldNumber {
32553377
case 1: try { try decoder.decodeSingularStringField(value: &self.path) }()
32563378
case 2: try {
3379+
var v: Com_Apple_Containerization_Sandbox_V3_FiTrimParams?
3380+
var hadOneofValue = false
3381+
if let current = self.operation {
3382+
hadOneofValue = true
3383+
if case .trim(let m) = current {v = m}
3384+
}
3385+
try decoder.decodeSingularMessageField(value: &v)
3386+
if let v = v {
3387+
if hadOneofValue {try decoder.handleConflictingOneOf()}
3388+
self.operation = .trim(v)
3389+
}
3390+
}()
3391+
case 3: try {
32573392
var v: Com_Apple_Containerization_Sandbox_V3_FiFreezeParams?
32583393
var hadOneofValue = false
32593394
if let current = self.operation {
@@ -3266,7 +3401,7 @@ extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest: Swif
32663401
self.operation = .freeze(v)
32673402
}
32683403
}()
3269-
case 3: try {
3404+
case 4: try {
32703405
var v: Com_Apple_Containerization_Sandbox_V3_FiThawParams?
32713406
var hadOneofValue = false
32723407
if let current = self.operation {
@@ -3293,13 +3428,17 @@ extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest: Swif
32933428
try visitor.visitSingularStringField(value: self.path, fieldNumber: 1)
32943429
}
32953430
switch self.operation {
3431+
case .trim?: try {
3432+
guard case .trim(let v)? = self.operation else { preconditionFailure() }
3433+
try visitor.visitSingularMessageField(value: v, fieldNumber: 2)
3434+
}()
32963435
case .freeze?: try {
32973436
guard case .freeze(let v)? = self.operation else { preconditionFailure() }
3298-
try visitor.visitSingularMessageField(value: v, fieldNumber: 2)
3437+
try visitor.visitSingularMessageField(value: v, fieldNumber: 3)
32993438
}()
33003439
case .thaw?: try {
33013440
guard case .thaw(let v)? = self.operation else { preconditionFailure() }
3302-
try visitor.visitSingularMessageField(value: v, fieldNumber: 3)
3441+
try visitor.visitSingularMessageField(value: v, fieldNumber: 4)
33033442
}()
33043443
case nil: break
33053444
}
@@ -3316,18 +3455,45 @@ extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationRequest: Swif
33163455

33173456
extension Com_Apple_Containerization_Sandbox_V3_FilesystemOperationResponse: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
33183457
public static let protoMessageName: String = _protobuf_package + ".FilesystemOperationResponse"
3319-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap()
3458+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}trim\0")
33203459

33213460
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
3322-
// Load everything into unknown fields
3323-
while try decoder.nextFieldNumber() != nil {}
3461+
while let fieldNumber = try decoder.nextFieldNumber() {
3462+
// The use of inline closures is to circumvent an issue where the compiler
3463+
// allocates stack space for every case branch when no optimizations are
3464+
// enabled. https://github.com/apple/swift-protobuf/issues/1034
3465+
switch fieldNumber {
3466+
case 1: try {
3467+
var v: Com_Apple_Containerization_Sandbox_V3_FiTrimResult?
3468+
var hadOneofValue = false
3469+
if let current = self.result {
3470+
hadOneofValue = true
3471+
if case .trim(let m) = current {v = m}
3472+
}
3473+
try decoder.decodeSingularMessageField(value: &v)
3474+
if let v = v {
3475+
if hadOneofValue {try decoder.handleConflictingOneOf()}
3476+
self.result = .trim(v)
3477+
}
3478+
}()
3479+
default: break
3480+
}
3481+
}
33243482
}
33253483

33263484
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
3485+
// The use of inline closures is to circumvent an issue where the compiler
3486+
// allocates stack space for every if/case branch local when no optimizations
3487+
// are enabled. https://github.com/apple/swift-protobuf/issues/1034 and
3488+
// https://github.com/apple/swift-protobuf/issues/1182
3489+
try { if case .trim(let v)? = self.result {
3490+
try visitor.visitSingularMessageField(value: v, fieldNumber: 1)
3491+
} }()
33273492
try unknownFields.traverse(visitor: &visitor)
33283493
}
33293494

33303495
public static func ==(lhs: Com_Apple_Containerization_Sandbox_V3_FilesystemOperationResponse, rhs: Com_Apple_Containerization_Sandbox_V3_FilesystemOperationResponse) -> Bool {
3496+
if lhs.result != rhs.result {return false}
33313497
if lhs.unknownFields != rhs.unknownFields {return false}
33323498
return true
33333499
}

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
@@ -718,6 +718,8 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContext.SimpleServ
718718
try freezeFilesystem(fd: fd)
719719
case .thaw:
720720
try thawFilesystem(fd: fd)
721+
case .trim(let params):
722+
try trimFilesystem(fd: fd, params: params)
721723
case .none:
722724
throw RPCError(code: .invalidArgument, message: "invalid operation")
723725
}
@@ -751,6 +753,19 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContext.SimpleServ
751753
}
752754
}
753755

756+
private static let FITRIM: UInt = 0xC004_5879
757+
758+
private func trimFilesystem(fd: Int32, params: Com_Apple_Containerization_Sandbox_V3_FiTrimParams) throws {
759+
760+
// TODO logic for trim filesystem
761+
762+
let rc: CInt = ioctl(fd, FITRIM, 0)
763+
if rc != 0 {
764+
let error = swiftErrno("ioctl(FITRIM)")
765+
throw RPCError(code: .internalError, message: "trim failed", cause: error)
766+
}
767+
}
768+
754769
public func umount(request: Com_Apple_Containerization_Sandbox_V3_UmountRequest, context: GRPCCore.ServerContext)
755770
async throws -> Com_Apple_Containerization_Sandbox_V3_UmountResponse
756771
{

0 commit comments

Comments
 (0)