use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError, Weak};
use std::time::Duration;
use tokio::sync::{RwLock, watch};
use tokio::task::AbortHandle;
use tokio::time::Instant;
use tracing::debug;
use uuid::Uuid;
use crate::conn::state::{Connection, Open};
use crate::ntstatus;
use crate::path::{SmbPath, same_path};
use crate::server::ShareBindings;
use crate::utils::fill_random;
use super::close_open;
use super::lease::{LeaseId, LeaseVersion};
use super::table::FileRef;
pub const DEFAULT_DURABLE_HANDLE_TIMEOUT: Duration = Duration::from_secs(2 * 60);
pub const DEFAULT_MAX_DISCONNECTED_OPENS: usize = 4096;
const MAX_REQUESTED_TIMEOUT: Duration = Duration::from_secs(300);
pub(crate) fn v2_timeout(requested_ms: u32, default: Duration) -> Duration {
match requested_ms {
0 => default,
ms => Duration::from_millis(u64::from(ms)).min(MAX_REQUESTED_TIMEOUT.max(default)),
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Durability {
pub create_guid: Option<[u8; 16]>,
pub timeout: Duration,
pub replay_eligible: bool,
}
pub(crate) struct DurableTable {
inner: Mutex<Inner>,
pub(crate) timeout: Duration,
max_detached: usize,
closing: Arc<watch::Sender<usize>>,
sweeps: AtomicU64,
changed: watch::Sender<u64>,
}
struct InFlight(Arc<watch::Sender<usize>>);
impl InFlight {
fn new(closing: &Arc<watch::Sender<usize>>) -> Self {
closing.send_modify(|n| *n += 1);
Self(Arc::clone(closing))
}
}
impl Drop for InFlight {
fn drop(&mut self) {
self.0.send_modify(|n| *n -= 1);
}
}
#[derive(Default)]
struct Inner {
entries: HashMap<u64, Entry>,
detached: usize,
detached_by_user: HashMap<String, usize>,
next_generation: u64,
}
struct Entry {
owner: String,
share: Weak<ShareBindings>,
lease: LeaseId,
lease_version: LeaseVersion,
durability: Durability,
state: State,
}
enum State {
Attached { conn: Weak<Connection> },
Detached {
open: Arc<RwLock<Open>>,
timer: Timer,
generation: u64,
deadline: Instant,
},
Reattaching { deadline: Instant, since: u64 },
Closing,
}
struct Timer(Option<AbortHandle>);
impl Timer {
fn disarm(mut self) {
self.0.take();
}
}
impl Drop for Timer {
fn drop(&mut self) {
if let Some(timer) = self.0.take() {
timer.abort();
}
}
}
pub(crate) struct DurableReg {
table: Weak<DurableTable>,
id: u64,
}
impl DurableReg {
pub(crate) fn id(&self) -> u64 {
self.id
}
}
impl Drop for DurableReg {
fn drop(&mut self) {
if let Some(table) = self.table.upgrade() {
let removed = table.inner().remove(self.id);
drop(removed);
table.bump();
}
}
}
pub(crate) struct Claim<'a> {
pub share: &'a Arc<ShareBindings>,
pub user: Option<&'a str>,
pub client: Uuid,
pub lease: Option<(LeaseId, LeaseVersion)>,
pub create_guid: Option<[u8; 16]>,
pub name: Option<&'a SmbPath>,
}
#[derive(Debug)]
pub(crate) enum Unclaimed {
Status(u32),
Wait(watch::Receiver<u64>),
}
impl Inner {
fn take_detached(
&mut self,
id: u64,
pick: impl Fn(u64) -> bool,
next: impl FnOnce(Instant) -> State,
) -> Option<(Arc<RwLock<Open>>, Timer)> {
let entry = self.entries.get_mut(&id)?;
let deadline = match &entry.state {
State::Detached {
generation,
deadline,
..
} if pick(*generation) => *deadline,
_ => return None,
};
let State::Detached { open, timer, .. } =
std::mem::replace(&mut entry.state, next(deadline))
else {
unreachable!("checked above");
};
let owner = entry.owner.clone();
self.count_out(&owner);
Some((open, timer))
}
fn remove(&mut self, id: u64) -> Option<Entry> {
let entry = self.entries.remove(&id)?;
if matches!(entry.state, State::Detached { .. }) {
self.count_out(&entry.owner);
}
Some(entry)
}
fn count_out(&mut self, owner: &str) {
self.detached -= 1;
if let Some(n) = self.detached_by_user.get_mut(owner) {
*n -= 1;
if *n == 0 {
self.detached_by_user.remove(owner);
}
}
}
}
impl DurableTable {
pub(crate) fn new(timeout: Duration, max_detached: usize) -> Self {
Self {
inner: Mutex::default(),
timeout,
max_detached,
closing: Arc::new(watch::Sender::new(0)),
sweeps: AtomicU64::new(0),
changed: watch::Sender::new(0),
}
}
fn bump(&self) {
self.changed.send_modify(|n| *n = n.wrapping_add(1));
}
pub(crate) fn sweeps(&self) -> u64 {
self.sweeps.load(Ordering::SeqCst)
}
pub(crate) async fn closed(&self) {
let mut closing = self.closing.subscribe();
let _ = closing.wait_for(|n| *n == 0).await;
}
fn inner(&self) -> MutexGuard<'_, Inner> {
self.inner.lock().unwrap_or_else(PoisonError::into_inner)
}
fn max_detached_per_user(&self) -> usize {
self.max_detached.div_ceil(4)
}
pub(crate) fn register(
self: &Arc<Self>,
owner: &str,
share: &Arc<ShareBindings>,
(lease, lease_version): (LeaseId, LeaseVersion),
durability: Durability,
conn: &Arc<Connection>,
) -> Option<DurableReg> {
let mut inner = self.inner();
let id = loop {
let mut bytes = [0; 8];
fill_random(&mut bytes).ok()?;
let id = u64::from_le_bytes(bytes);
if id != 0 && id != u64::MAX && !inner.entries.contains_key(&id) {
break id;
}
};
inner.entries.insert(
id,
Entry {
owner: owner.to_string(),
share: Arc::downgrade(share),
lease,
lease_version,
durability,
state: State::Attached {
conn: Arc::downgrade(conn),
},
},
);
Some(DurableReg {
table: Arc::downgrade(self),
id,
})
}
pub(crate) async fn preserve(
self: &Arc<Self>,
open: Arc<RwLock<Open>>,
since: u64,
) -> Option<Arc<RwLock<Open>>> {
let mut guard = open.write().await;
let Some(id) = guard.durable.as_ref().map(DurableReg::id) else {
drop(guard);
return Some(open);
};
let now = Instant::now();
let mut deadline = now;
let generation = {
let mut inner = self.inner();
let Inner {
entries,
detached,
detached_by_user,
next_generation,
} = &mut *inner;
match entries.get_mut(&id) {
Some(entry) if matches!(entry.state, State::Attached { .. }) => {
let by_user = detached_by_user.get(&entry.owner).copied().unwrap_or(0);
if self.sweeps() != since {
debug!(id, "durable open closed: configuration change");
None
} else if *detached >= self.max_detached
|| by_user >= self.max_detached_per_user()
{
debug!(id, "durable open closed: limit");
None
} else if guard.file.as_ref().is_some_and(|f| f.try_detach()) {
deadline = now + entry.durability.timeout;
*detached += 1;
*detached_by_user.entry(entry.owner.clone()).or_default() += 1;
*next_generation += 1;
entry.state = State::Detached {
open: Arc::clone(&open),
timer: Timer(None),
generation: *next_generation,
deadline,
};
Some(*next_generation)
} else {
None
}
}
_ => None,
}
};
let Some(generation) = generation else {
drop(guard);
return Some(open);
};
guard.slot.take();
guard.search_state = None;
drop(guard);
drop(open);
self.bump();
debug!(id, timeout = ?(deadline - now), "durable open kept");
self.start_timer(id, generation, deadline);
None
}
fn start_timer(self: &Arc<Self>, id: u64, generation: u64, deadline: Instant) {
let Ok(runtime) = tokio::runtime::Handle::try_current() else {
return;
};
let table = Arc::downgrade(self);
let task = runtime.spawn(async move {
tokio::time::sleep_until(deadline).await;
let expired = table.upgrade().and_then(|t| t.expire(id, generation));
if let Some((open, _in_flight)) = expired {
debug!(id, "durable open closed: expired");
close_open(&open, false).await;
}
});
let mut inner = self.inner();
match inner.entries.get_mut(&id).map(|e| &mut e.state) {
Some(State::Detached {
timer,
generation: g,
..
}) if *g == generation => *timer = Timer(Some(task.abort_handle())),
_ => task.abort(),
}
}
fn expire(&self, id: u64, generation: u64) -> Option<(Arc<RwLock<Open>>, InFlight)> {
let mut inner = self.inner();
let (open, timer) = inner.take_detached(id, |g| g == generation, |_| State::Closing)?;
let in_flight = InFlight::new(&self.closing);
drop(inner);
timer.disarm();
Some((open, in_flight))
}
pub(crate) fn purge(&self, id: u64) {
let mut inner = self.inner();
let Some((open, timer)) = inner.take_detached(id, |_| true, |_| State::Closing) else {
return;
};
let in_flight = InFlight::new(&self.closing);
drop(inner);
drop(timer);
debug!(id, "durable open closed: lease broken");
spawn_close(open, in_flight);
}
pub(crate) fn take_for(
&self,
share: Option<&Arc<ShareBindings>>,
user: Option<&str>,
) -> Vec<Arc<RwLock<Open>>> {
self.sweeps.fetch_add(1, Ordering::SeqCst);
let mut inner = self.inner();
let ids: Vec<u64> = inner
.entries
.iter()
.filter(|(_, e)| {
matches!(e.state, State::Detached { .. })
&& share.is_none_or(|s| std::ptr::eq(e.share.as_ptr(), Arc::as_ptr(s)))
&& user.is_none_or(|u| e.owner == u)
})
.map(|(id, _)| *id)
.collect();
let mut opens = Vec::with_capacity(ids.len());
for id in ids {
if let Some((open, _timer)) = inner.take_detached(id, |_| true, |_| State::Closing) {
debug!(id, "durable open closed: configuration or shutdown");
opens.push(open);
}
}
drop(inner);
opens
}
pub(crate) fn claim(&self, id: u64, claim: &Claim<'_>) -> Result<Arc<RwLock<Open>>, Unclaimed> {
let not_found = || Unclaimed::Status(ntstatus::STATUS_OBJECT_NAME_NOT_FOUND);
let mut inner = self.inner();
let entry = inner.entries.get(&id).ok_or_else(not_found)?;
if entry.lease.client != claim.client {
return Err(not_found());
}
let v2 = claim.create_guid.is_some();
if claim
.create_guid
.is_some_and(|guid| entry.durability.create_guid != Some(guid))
{
return Err(not_found());
}
let name_matches = match &entry.state {
State::Detached { open, .. } => Some(self.name_matches(open, claim)?),
_ => None,
};
if !v2 && name_matches == Some(false) {
return Err(Unclaimed::Status(ntstatus::STATUS_INVALID_PARAMETER));
}
let Some((lease, version)) = claim.lease else {
return Err(not_found());
};
let closing = match &entry.state {
State::Detached { .. } => false,
State::Attached { conn } if conn.upgrade().is_none_or(|c| c.is_closing()) => true,
_ => return Err(not_found()),
};
if lease != entry.lease
|| version != entry.lease_version
|| !std::ptr::eq(entry.share.as_ptr(), Arc::as_ptr(claim.share))
|| name_matches == Some(false)
{
return Err(not_found());
}
let owner = claim.user == Some(entry.owner.as_str());
if closing {
return Err(if owner {
Unclaimed::Wait(self.changed.subscribe())
} else {
not_found()
});
}
if !owner {
return Err(Unclaimed::Status(ntstatus::STATUS_ACCESS_DENIED));
}
let since = self.sweeps();
let (open, timer) = inner
.take_detached(
id,
|_| true,
|deadline| State::Reattaching { deadline, since },
)
.expect("checked above");
drop(inner);
drop(timer);
debug!(id, "durable open claimed");
Ok(open)
}
fn name_matches(&self, open: &Arc<RwLock<Open>>, claim: &Claim<'_>) -> Result<bool, Unclaimed> {
let Ok(open) = open.try_read() else {
return Err(Unclaimed::Wait(self.changed.subscribe()));
};
let case_sensitive = claim.share.backend.capabilities().case_sensitive;
Ok(open.file.as_ref().is_some_and(|f| {
open.delete_on_close
|| f.delete_pending()
|| claim
.name
.is_some_and(|name| same_path(&f.path(), name, case_sensitive))
}))
}
pub(crate) fn create_guid_in_use(&self, client: Uuid, create_guid: [u8; 16]) -> bool {
self.inner().entries.values().any(|e| {
e.lease.client == client
&& e.durability.replay_eligible
&& e.durability.create_guid == Some(create_guid)
&& !matches!(e.state, State::Closing)
})
}
pub(crate) async fn release_claim(self: &Arc<Self>, id: u64, open: Arc<RwLock<Open>>) {
let Some(open) = self.put_back(id, open) else {
return;
};
let doomed = open
.read()
.await
.file
.as_ref()
.is_some_and(FileRef::is_doomed);
drop(open);
if doomed {
self.purge(id);
}
}
fn put_back(self: &Arc<Self>, id: u64, open: Arc<RwLock<Open>>) -> Option<Arc<RwLock<Open>>> {
let mut inner = self.inner();
let swept = |since| self.sweeps() != since;
let Inner {
entries,
detached,
detached_by_user,
next_generation,
} = &mut *inner;
let Some(entry) = entries.get_mut(&id) else {
drop(inner);
drop(open);
return None;
};
let deadline = match entry.state {
State::Reattaching { deadline, since } if !swept(since) => deadline,
State::Reattaching { .. } => {
entry.state = State::Closing;
let in_flight = InFlight::new(&self.closing);
drop(inner);
debug!(id, "durable open closed: configuration change");
spawn_close(open, in_flight);
return None;
}
_ => {
drop(inner);
drop(open);
return None;
}
};
*detached += 1;
*detached_by_user.entry(entry.owner.clone()).or_default() += 1;
*next_generation += 1;
let generation = *next_generation;
entry.state = State::Detached {
open: Arc::clone(&open),
timer: Timer(None),
generation,
deadline,
};
drop(inner);
debug!(id, "durable open kept again");
self.start_timer(id, generation, deadline);
Some(open)
}
pub(crate) fn attached(&self, id: u64, conn: &Arc<Connection>) {
if let Some(entry) = self.inner().entries.get_mut(&id)
&& matches!(entry.state, State::Reattaching { .. })
{
entry.state = State::Attached {
conn: Arc::downgrade(conn),
};
}
debug!(id, "durable open reconnected");
}
}
fn spawn_close(open: Arc<RwLock<Open>>, in_flight: InFlight) {
if let Ok(runtime) = tokio::runtime::Handle::try_current() {
runtime.spawn(async move {
close_open(&open, false).await;
drop(in_flight);
});
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap as Map;
use super::*;
use crate::access_mask::{FILE_READ_DATA, FILE_WRITE_DATA};
use crate::backend::{FileInfo, FileTimes, OpenOptions};
use crate::conn::outbound::Outbound;
use crate::files::lease::{LeaseRequest, LeaseState, LeaseVersion};
use crate::files::leases::LeaseTable;
use crate::files::table::{FileRef, OpenRequest, Sharing};
use crate::path::SmbPath;
use crate::proto::messages::FileId;
use crate::server::ShareMode;
use crate::tests::memfs::MemFsBackend;
struct Fixture {
table: Arc<DurableTable>,
conn: Arc<Connection>,
leases: Arc<LeaseTable>,
share: Arc<ShareBindings>,
files: MemFsBackend,
outbound: Arc<Outbound>,
}
fn fixture(max_detached: usize) -> Fixture {
let files = MemFsBackend::new();
let share = ShareBindings::new(
"s".to_string(),
Arc::new(files.clone()),
ShareMode::Public,
Map::new(),
false,
true,
true,
);
Fixture {
table: Arc::new(DurableTable::new(Duration::from_secs(60), max_detached)),
conn: Arc::new(Connection::new(Uuid::nil(), 1 << 16, 1 << 16)),
leases: Arc::default(),
share,
files,
outbound: Arc::default(),
}
}
const V1: Durability = Durability {
create_guid: None,
timeout: Duration::from_secs(60),
replay_eligible: false,
};
static A: std::sync::LazyLock<SmbPath> = std::sync::LazyLock::new(|| "a".parse().unwrap());
fn lease(n: u8) -> LeaseId {
LeaseId {
client: Uuid::from_bytes([1; 16]),
key: [n; 16],
}
}
impl Fixture {
async fn open(
&self,
name: &str,
owner: &str,
n: u8,
state: LeaseState,
) -> Arc<RwLock<Open>> {
let path: SmbPath = name.parse().unwrap();
let opts = OpenOptions {
intent: crate::backend::OpenIntent::OpenOrCreate,
..OpenOptions::default()
};
let (handle, _) = self.share.backend.open(&path, opts).await.unwrap();
let mut info = FileInfo::new("");
info.file_index = u64::from(n);
let lease_ref = self.leases.acquire(lease(n), &self.share, &path).unwrap();
let req = OpenRequest {
sharing: Sharing {
access: FILE_READ_DATA | FILE_WRITE_DATA,
share: 7,
},
owner: Arc::downgrade(&self.outbound),
lease: Some(lease(n)),
..OpenRequest::default()
};
let mut file = FileRef::register(&self.share, &info, &path, req).unwrap().0;
let request = LeaseRequest {
id: lease(n),
state,
version: LeaseVersion::V2,
epoch: 0,
parent: None,
};
let (granted, guard) = self
.leases
.grant(lease_ref, &mut file, &request, LeaseState::RWH)
.unwrap();
drop(guard);
assert_eq!(granted.state, state);
let leased = (lease(n), LeaseVersion::V2);
let reg = self
.table
.register(owner, &self.share, leased, V1, &self.conn)
.unwrap();
file.mark_durable(&self.table, reg.id());
Arc::new(RwLock::new(Open {
file_id: FileId::new(reg.id(), 1),
handle: Some(handle),
granted_access: FILE_READ_DATA | FILE_WRITE_DATA,
is_directory: false,
delete_on_close: false,
search_state: None,
fixed_times: FileTimes::default(),
create_options: 0,
file: Some(file),
slot: None,
durable: Some(reg),
watch: None,
wrote: Default::default(),
created: false,
marked_deleted: Default::default(),
}))
}
fn counts(&self) -> (usize, usize, usize) {
let inner = self.table.inner();
(
inner.entries.len(),
inner.detached,
inner.detached_by_user.values().sum(),
)
}
}
async fn id_of(open: &Arc<RwLock<Open>>) -> u64 {
open.read().await.durable.as_ref().unwrap().id()
}
async fn settle() {
for _ in 0..20 {
tokio::task::yield_now().await;
}
}
#[tokio::test]
async fn ids_are_random_unique_and_never_reserved_values() {
let f = fixture(8);
let regs: Vec<_> = (0..64)
.map(|n| {
let leased = (lease(n), LeaseVersion::V2);
f.table
.register("alice", &f.share, leased, V1, &f.conn)
.unwrap()
})
.collect();
let mut ids: Vec<u64> = regs.iter().map(DurableReg::id).collect();
assert!(ids.iter().all(|&id| id != 0 && id != u64::MAX));
ids.sort_unstable();
ids.dedup();
assert_eq!(ids.len(), 64);
drop(regs);
assert_eq!(f.counts(), (0, 0, 0));
}
#[tokio::test]
async fn a_kept_open_counts_until_it_is_closed() {
let f = fixture(8);
let open = f.open("a", "alice", 1, LeaseState::RH).await;
let id = id_of(&open).await;
assert!(f.table.preserve(open, f.table.sweeps()).await.is_none());
assert_eq!(f.counts(), (1, 1, 1));
assert_eq!(f.files.open_handles(), 1);
f.table.purge(id);
assert_eq!(f.counts().1, 0);
settle().await;
assert_eq!(f.files.open_handles(), 0);
assert_eq!(f.counts(), (0, 0, 0));
assert!(!f.leases.exists(lease(1)));
}
#[tokio::test]
async fn opens_past_the_limits_are_not_kept() {
let f = fixture(6);
for n in 0..3 {
let open = f.open(&format!("a{n}"), "alice", n, LeaseState::RH).await;
let kept = f.table.preserve(open, f.table.sweeps()).await;
assert_eq!(kept.is_none(), n < 2, "alice {n}");
}
for n in 3..8 {
let user = ["bob", "carol", "dave"][usize::from(n - 3) / 2];
let open = f.open(&format!("b{n}"), user, n, LeaseState::RH).await;
let kept = f.table.preserve(open, f.table.sweeps()).await;
assert_eq!(kept.is_none(), n < 7, "{user} {n}");
}
assert_eq!(f.counts().1, 6);
let opens = f.table.take_for(None, None);
assert_eq!(opens.len(), 6);
drop(opens);
settle().await;
assert_eq!(f.counts(), (0, 0, 0));
}
#[tokio::test]
async fn opens_without_handle_caching_are_not_kept() {
let f = fixture(8);
let open = f.open("a", "alice", 1, LeaseState::R).await;
assert!(f.table.preserve(open, f.table.sweeps()).await.is_some());
assert_eq!(f.counts().1, 0);
}
#[tokio::test]
async fn an_open_whose_session_left_before_a_sweep_is_not_kept() {
let f = fixture(8);
let open = f.open("a", "alice", 1, LeaseState::RH).await;
let since = f.table.sweeps();
assert!(f.table.take_for(None, Some("alice")).is_empty());
assert!(f.table.preserve(open, since).await.is_some());
let open = f.open("b", "alice", 2, LeaseState::RH).await;
assert!(f.table.preserve(open, f.table.sweeps()).await.is_none());
assert_eq!(f.counts().1, 1);
}
#[tokio::test]
async fn take_for_picks_by_share_and_user() {
let f = fixture(64);
let other = fixture(64);
for (n, user) in [(1, "alice"), (2, "alice"), (3, "bob")] {
let open = f.open(&format!("f{n}"), user, n, LeaseState::RH).await;
assert!(f.table.preserve(open, f.table.sweeps()).await.is_none());
}
assert!(f.table.take_for(Some(&other.share), None).is_empty());
assert!(f.table.take_for(None, Some("carol")).is_empty());
assert_eq!(f.table.take_for(Some(&f.share), Some("bob")).len(), 1);
assert_eq!(f.table.take_for(None, Some("alice")).len(), 2);
assert_eq!(f.counts().1, 0);
}
#[tokio::test]
async fn only_a_closing_or_kept_entry_is_purged() {
let f = fixture(8);
let open = f.open("a", "alice", 1, LeaseState::RH).await;
let id = id_of(&open).await;
f.table.purge(id);
assert_eq!(f.counts(), (1, 0, 0));
assert!(
f.table
.preserve(Arc::clone(&open), f.table.sweeps())
.await
.is_none()
);
f.table.purge(id);
f.table.purge(id);
drop(open);
settle().await;
assert_eq!(f.counts(), (0, 0, 0));
}
fn claim<'a>(
share: &'a Arc<ShareBindings>,
user: Option<&'a str>,
client: Uuid,
lease: Option<(LeaseId, LeaseVersion)>,
) -> Claim<'a> {
Claim {
share,
user,
client,
lease,
create_guid: None,
name: Some(&A),
}
}
#[test]
fn a_version_2_timeout_is_the_request_up_to_a_cap() {
let s = Duration::from_secs;
let ms = Duration::from_millis;
assert_eq!(v2_timeout(0, s(120)), s(120));
assert_eq!(v2_timeout(1, s(120)), ms(1));
assert_eq!(v2_timeout(300_000, s(120)), s(300));
assert_eq!(v2_timeout(300_001, s(120)), s(300));
assert_eq!(v2_timeout(u32::MAX, s(120)), s(300));
assert_eq!(v2_timeout(400_000, s(600)), s(400));
assert_eq!(v2_timeout(900_000, s(600)), s(600));
}
#[tokio::test]
async fn a_version_2_claim_names_the_create_guid() {
let f = fixture(8);
let guid = [7; 16];
let v1 = f.open("a", "alice", 1, LeaseState::RH).await;
let v1_id = id_of(&v1).await;
let v2 = f.open("b", "alice", 2, LeaseState::RH).await;
let v2_id = id_of(&v2).await;
f.table
.inner()
.entries
.get_mut(&v2_id)
.unwrap()
.durability
.create_guid = Some(guid);
for open in [v1, v2] {
assert!(f.table.preserve(open, f.table.sweeps()).await.is_none());
}
let me = lease(1).client;
let b: SmbPath = "b".parse().unwrap();
let with = |n, create_guid| Claim {
create_guid,
name: Some(if n == 1 { &*A } else { &b }),
..claim(
&f.share,
Some("alice"),
me,
Some((lease(n), LeaseVersion::V2)),
)
};
let not_found = ntstatus::STATUS_OBJECT_NAME_NOT_FOUND;
assert_eq!(
status(f.table.claim(v1_id, &with(1, Some(guid)))),
not_found
);
assert_eq!(
status(f.table.claim(v2_id, &with(2, Some([8; 16])))),
not_found
);
assert_eq!(status(f.table.claim(v2_id, &with(2, Some(guid)))), 0);
assert_eq!(status(f.table.claim(v1_id, &with(1, None))), 0);
}
#[tokio::test]
async fn the_name_is_compared_before_the_owner() {
let f = fixture(8);
let open = f.open("a", "alice", 1, LeaseState::RH).await;
let id = id_of(&open).await;
assert!(f.table.preserve(open, f.table.sweeps()).await.is_none());
let other: SmbPath = "other".parse().unwrap();
let bob = |create_guid, name| Claim {
create_guid,
name,
..claim(
&f.share,
Some("bob"),
lease(1).client,
Some((lease(1), LeaseVersion::V2)),
)
};
assert_eq!(
status(f.table.claim(id, &bob(None, Some(&other)))),
ntstatus::STATUS_INVALID_PARAMETER
);
assert_eq!(
status(f.table.claim(id, &bob(None, None))),
ntstatus::STATUS_INVALID_PARAMETER,
"a name that is not valid matches nothing"
);
assert_eq!(
status(f.table.claim(id, &bob(None, Some(&A)))),
ntstatus::STATUS_ACCESS_DENIED
);
assert_eq!(f.counts().1, 1, "refusals change nothing");
}
#[tokio::test]
async fn a_create_guid_in_use_is_one_of_a_live_replay_eligible_open() {
let f = fixture(8);
let guid = [7; 16];
let open = f.open("a", "alice", 1, LeaseState::RH).await;
let id = id_of(&open).await;
let me = lease(1).client;
assert!(!f.table.create_guid_in_use(me, guid));
let set = |eligible| {
let mut inner = f.table.inner();
let durability = &mut inner.entries.get_mut(&id).unwrap().durability;
durability.create_guid = Some(guid);
durability.replay_eligible = eligible;
};
set(false);
assert!(!f.table.create_guid_in_use(me, guid), "not eligible");
set(true);
assert!(f.table.create_guid_in_use(me, guid));
assert!(
!f.table.create_guid_in_use(Uuid::nil(), guid),
"another client"
);
assert!(!f.table.create_guid_in_use(me, [8; 16]));
assert!(f.table.preserve(open, f.table.sweeps()).await.is_none());
assert!(f.table.create_guid_in_use(me, guid));
assert_eq!(f.table.take_for(None, None).len(), 1);
assert!(!f.table.create_guid_in_use(me, guid), "closing");
}
fn status(claimed: Result<Arc<RwLock<Open>>, Unclaimed>) -> u32 {
match claimed {
Ok(_) => 0,
Err(Unclaimed::Status(status)) => status,
Err(Unclaimed::Wait(_)) => u32::MAX,
}
}
#[tokio::test]
async fn a_claim_checks_the_reconnect_in_the_order_of_the_spec() {
let f = fixture(8);
let other = fixture(8);
let open = f.open("a", "alice", 1, LeaseState::RH).await;
let id = id_of(&open).await;
let me = lease(1).client;
let v2 = Some((lease(1), LeaseVersion::V2));
let not_found = ntstatus::STATUS_OBJECT_NAME_NOT_FOUND;
let alice = claim(&f.share, Some("alice"), me, v2);
assert_eq!(status(f.table.claim(id, &alice)), not_found);
assert!(f.table.preserve(open, f.table.sweeps()).await.is_none());
assert_eq!(status(f.table.claim(id ^ 1, &alice)), not_found);
for refused in [
claim(&f.share, Some("alice"), Uuid::nil(), v2),
claim(&f.share, Some("alice"), me, None),
claim(
&f.share,
Some("alice"),
me,
Some((lease(2), LeaseVersion::V2)),
),
claim(
&f.share,
Some("alice"),
me,
Some((lease(1), LeaseVersion::V1)),
),
claim(&other.share, Some("alice"), me, v2),
] {
assert_eq!(status(f.table.claim(id, &refused)), not_found);
}
for user in [Some("bob"), None] {
let refused = claim(&f.share, user, me, v2);
assert_eq!(
status(f.table.claim(id, &refused)),
ntstatus::STATUS_ACCESS_DENIED
);
}
assert_eq!(f.counts().1, 1, "refusals change nothing");
let open = f.table.claim(id, &alice).unwrap();
assert_eq!(f.counts().1, 0);
assert_eq!(status(f.table.claim(id, &alice)), not_found, "claimed once");
f.table.purge(id);
assert!(f.table.take_for(None, None).is_empty());
let owner = Arc::downgrade(&f.outbound);
let reattached = open.read().await.file.as_ref().unwrap().reattach(owner);
assert!(reattached.is_some());
drop(reattached);
f.table.attached(id, &f.conn);
assert_eq!(status(f.table.claim(id, &alice)), not_found, "attached");
assert!(f.table.preserve(open, f.table.sweeps()).await.is_none());
assert_eq!(f.counts().1, 1);
}
#[tokio::test]
async fn a_claim_on_a_closing_connection_waits_for_the_open_to_be_kept() {
let f = fixture(8);
let open = f.open("a", "alice", 1, LeaseState::RH).await;
let id = id_of(&open).await;
let alice = claim(
&f.share,
Some("alice"),
lease(1).client,
Some((lease(1), LeaseVersion::V2)),
);
f.conn.mark_closing();
let Err(Unclaimed::Wait(wait)) = f.table.claim(id, &alice) else {
panic!("a claim on a closing connection waits");
};
assert!(!wait.has_changed().unwrap());
assert!(f.table.preserve(open, f.table.sweeps()).await.is_none());
assert!(wait.has_changed().unwrap());
assert!(f.table.claim(id, &alice).is_ok());
}
#[tokio::test(start_paused = true)]
async fn a_released_claim_keeps_the_open_until_its_first_deadline() {
let f = fixture(8);
let open = f.open("a", "alice", 1, LeaseState::RH).await;
let id = id_of(&open).await;
let alice = claim(
&f.share,
Some("alice"),
lease(1).client,
Some((lease(1), LeaseVersion::V2)),
);
assert!(f.table.preserve(open, f.table.sweeps()).await.is_none());
tokio::time::sleep(Duration::from_secs(40)).await;
let open = f.table.claim(id, &alice).unwrap();
tokio::time::sleep(Duration::from_secs(30)).await;
settle().await;
assert_eq!(f.files.open_handles(), 1);
f.table.release_claim(id, open).await;
assert_eq!(f.counts().1, 1);
settle().await;
assert_eq!(f.files.open_handles(), 0);
assert_eq!(f.counts(), (0, 0, 0));
}
}