Files
markbase/vendor/smb-server/src/handlers/write.rs
Warren c3e21560b6 Implement SMB 3.x Lease support Phase 5
- WRITE handler trigger lease break (READ leases conflict with WRITE)
- READ handler trigger lease break (HANDLE leases may conflict)
- Send LeaseBreakNotification via notification channel

All 229 tests pass.
2026-06-21 01:24:59 +08:00

111 lines
3.6 KiB
Rust

//! WRITE handler.
use std::sync::Arc;
use crate::proto::header::Smb2Header;
use crate::proto::messages::{WriteRequest, WriteResponse};
use crate::builder::Access;
use crate::conn::state::Connection;
use crate::dispatch::HandlerResponse;
use crate::handlers::shared::{lookup_open, lookup_session_tree};
use crate::ntstatus;
use crate::server::ServerState;
pub async fn handle(
server: &Arc<ServerState>,
conn: &Arc<Connection>,
hdr: &Smb2Header,
body: &[u8],
) -> HandlerResponse {
let req = match WriteRequest::parse(body) {
Ok(r) => r,
Err(_) => return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER),
};
let max_write = *conn.max_write_size.read().await;
if req.length > max_write {
return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER);
}
let tree_arc = match lookup_session_tree(conn, hdr).await {
Ok(t) => t,
Err(s) => return HandlerResponse::err(s),
};
let granted = {
let tree = tree_arc.read().await;
tree.granted_access
};
if !matches!(granted, Access::ReadWrite) {
return HandlerResponse::err(ntstatus::STATUS_ACCESS_DENIED);
}
let open_arc = match lookup_open(&tree_arc, req.file_id).await {
Some(o) => o,
None => return HandlerResponse::err(ntstatus::STATUS_FILE_CLOSED),
};
// Phase 5: Get path and trigger oplock break before write
let (path, share_access) = {
let open = open_arc.read().await;
(open.last_path.clone(), open.share_access)
};
// Get granted_access from tree
let granted_access = {
let tree = tree_arc.read().await;
tree.granted_access
};
// Trigger oplock break for conflicting clients
let notifications = server.oplock_manager.break_oplock(
&path,
share_access,
granted_access,
).await;
// Send notifications to affected clients
for notification in notifications {
// Build SMB2 frame for notification
use crate::proto::framing::encode_frame;
let notification_bytes = notification.write_to_bytes();
let mut frame = Vec::with_capacity(notification_bytes.len() + 4);
encode_frame(&notification_bytes, &mut frame);
// Send via notification channel (if available)
if let Some(tx) = conn.notification_tx.read().await.as_ref() {
let _ = tx.send(frame).await;
}
}
// Phase 5: Trigger lease break if lease exists (SMB 3.x)
let lease_notifications = server.lease_manager.break_lease(
crate::oplock::SMB2_LEASE_READ, // WRITE operation breaks READ leases
).await;
for lease_notification in lease_notifications {
use crate::proto::framing::encode_frame;
let notification_bytes = lease_notification.write_to_bytes();
let mut frame = Vec::with_capacity(notification_bytes.len() + 4);
encode_frame(&notification_bytes, &mut frame);
if let Some(tx) = conn.notification_tx.read().await.as_ref() {
let _ = tx.send(frame).await;
}
}
let result = {
let open = open_arc.read().await;
match open.handle.as_ref() {
Some(h) => h.write_owned(req.offset, req.data).await,
None => return HandlerResponse::err(ntstatus::STATUS_FILE_CLOSED),
}
};
let count = match result {
Ok(n) => n,
Err(e) => return HandlerResponse::err(e.to_nt_status()),
};
let mut buf = Vec::new();
WriteResponse::new(count)
.write_to(&mut buf)
.expect("encode");
HandlerResponse::ok(buf)
}