ksmbd: validate SMB2 lease create contexts
Subsystems (2)
Namjae Jeon
maintainerPATCH 1/14 ksmbd: validate SMB2 lease create contexts
Validate SMB2 lease context lengths, requested lease state bits, and v2 flags before using the context. Return errors via ERR_PTR so CREATE can distinguish a missing lease context from a malformed one.
Also ignore lease v2 contexts for SMB 2.1, where they are not valid.
--- fs/smb/server/oplock.c | 33 ++++++++++++++++++++++++++++----- fs/smb/server/smb2pdu.c | 24 +++++++++++++++++++++++- 2 files changed, 51 insertions(+), 6 deletions(-)
fs/smb/server/oplock.c +28 -5
@@ -5,6 +5,7 @@*/ #include <linux/moduleparam.h> +#include <linux/err.h> #include "glob.h" #include "oplock.h"
@@ -19,6 +20,20 @@static LIST_HEAD (lease_table_list);static DEFINE_RWLOCK (lease_list_lock); +#define SMB2_LEASE_STATE_MASK_LE (SMB2_LEASE_READ_CACHING_LE | \ + SMB2_LEASE_HANDLE_CACHING_LE | \ + SMB2_LEASE_WRITE_CACHING_LE) + +static bool lease_state_valid (__le32 state) +{ +return !(state & ~SMB2_LEASE_STATE_MASK_LE ); +} + +static bool lease_v2_flags_valid (__le32 flags) +{ +return !(flags & ~SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE ); +} +/** * alloc_opinfo() - allocate a new opinfo object for oplock info * @work: smb work
@@ -1537,12 +1552,14 @@ struct lease_ctx_info *parse_lease_state(void *open_req)struct lease_ctx_info *lreq; cc =smb2_find_context_vals (req,SMB2_CREATE_REQUEST_LEASE ,4 ); -if (IS_ERR_OR_NULL (cc)) +if (IS_ERR (cc)) +return ERR_CAST (cc); +if (!cc)return NULL ; lreq = kzalloc_obj(struct lease_ctx_info, KSMBD_DEFAULT_GFP);if (!lreq) -return NULL ; +return ERR_PTR (-ENOMEM );if (sizeof (struct lease_context_v2 ) ==le32_to_cpu (cc->DataLength)) {struct create_lease_v2 *lc = (struct create_lease_v2 *)cc;
@@ -1556,11 +1573,14 @@ struct lease_ctx_info *parse_lease_state(void *open_req) lreq->flags = lc->lcontext.LeaseFlags; lreq->epoch = lc->lcontext.Epoch; lreq->duration = lc->lcontext.LeaseDuration; +if (!lease_state_valid (lreq->req_state) || + !lease_v2_flags_valid (lreq->flags)) + goto err_out;if (lreq->flags ==SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE )memcpy (lreq->parent_lease_key, lc->lcontext.ParentLeaseKey,SMB2_LEASE_KEY_SIZE ); lreq->version =2 ; - }else { + }else if (sizeof (struct lease_context ) ==le32_to_cpu (cc->DataLength)) {struct create_lease *lc = (struct create_lease *)cc; if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) <
@@ -1571,12 +1591,15 @@ struct lease_ctx_info *parse_lease_state(void *open_req) lreq->req_state = lc->lcontext.LeaseState; lreq->flags = lc->lcontext.LeaseFlags; lreq->duration = lc->lcontext.LeaseDuration; +if (!lease_state_valid (lreq->req_state)) + goto err_out; lreq->version =1 ; - } + }else + goto err_out;return lreq; err_out:kfree (lreq); -return NULL ; +return ERR_PTR (-EINVAL ); }/**
fs/smb/server/smb2pdu.c +23 -1
@@ -3108,6 +3108,17 @@ int smb2_open(struct ksmbd_work *work)if (server_conf.flags &KSMBD_GLOBAL_FLAG_DURABLE_HANDLE && req->CreateContextsOffset) { lc =parse_lease_state (req); +if (IS_ERR (lc)) { + rc =PTR_ERR (lc); + lc =NULL ; + goto err_out2; + } +if (lc && lc->version ==2 && conn->dialect <SMB30_PROT_ID ) { +kfree (lc); + lc =NULL ; +if (req_op_level ==SMB2_OPLOCK_LEVEL_LEASE ) + req_op_level =SMB2_OPLOCK_LEVEL_NONE ; + } rc =parse_durable_handle_context (work, req, lc, &dh_info);if (rc) {ksmbd_debug (SMB ,"error parsing durable handle context\n" );
@@ -3139,8 +3150,19 @@ int smb2_open(struct ksmbd_work *work) goto reconnected_fp; } - }else if (req_op_level ==SMB2_OPLOCK_LEVEL_LEASE ) + }else if (req_op_level ==SMB2_OPLOCK_LEVEL_LEASE ) { lc =parse_lease_state (req); +if (IS_ERR (lc)) { + rc =PTR_ERR (lc); + lc =NULL ; + goto err_out2; + } +if (lc && lc->version ==2 && conn->dialect <SMB30_PROT_ID ) { +kfree (lc); + lc =NULL ; + req_op_level =SMB2_OPLOCK_LEVEL_NONE ; + } + }if (le32_to_cpu (req->ImpersonationLevel) >le32_to_cpu (IL_DELEGATE )) { pr_err("Invalid impersonationlevel : 0x%x\n",
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 2/14 ksmbd: use connection ClientGUID for lease lookup
MS-SMB2 defines the lease table lookup key as Connection.ClientGuid. Use the connection ClientGUID consistently when checking for same-client leases and duplicate lease keys.
Also preserve directory and parent lease metadata when copying an existing lease state to a new open.
--- fs/smb/server/oplock.c | 16 +++++++++------- fs/smb/server/oplock.h | 2 +- fs/smb/server/smb2pdu.c | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-)
fs/smb/server/oplock.c +9 -7
@@ -520,7 +520,7 @@ static inline int compare_guid_key(struct oplock_info *opinfo,* Return: oplock(lease) object on success, otherwise NULL */ static struct oplock_info *same_client_has_lease (struct ksmbd_inode *ci, -char *client_guid, +const char *client_guid,struct lease_ctx_info *lctx) {int ret;
@@ -1012,7 +1012,7 @@ void destroy_lease_table(struct ksmbd_conn *conn)write_unlock (&lease_list_lock); } -int find_same_lease_key (struct ksmbd_session *sess,struct ksmbd_inode *ci, +int find_same_lease_key (struct ksmbd_conn *conn,struct ksmbd_inode *ci,struct lease_ctx_info *lctx) {struct oplock_info *opinfo;
@@ -1029,7 +1029,7 @@ int find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci, }list_for_each_entry (lb, &lease_table_list, l_entry) { -if (!memcmp (lb->client_guid, sess->ClientGUID, +if (!memcmp (lb->client_guid, conn->ClientGUID,SMB2_CLIENT_GUID_SIZE )) goto found; }
@@ -1045,7 +1045,7 @@ int find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci,rcu_read_unlock ();if (opinfo->o_fp->f_ci == ci) goto op_next; - err =compare_guid_key (opinfo, sess->ClientGUID, + err =compare_guid_key (opinfo, conn->ClientGUID, lctx->lease_key);if (err) { err = -EINVAL ;
@@ -1078,6 +1078,9 @@ static void copy_lease(struct oplock_info *op1, struct oplock_info *op2) lease2->flags = lease1->flags; lease2->epoch = lease1->epoch; lease2->version = lease1->version; + lease2->is_dir = lease1->is_dir; +memcpy (lease2->parent_lease_key, lease1->parent_lease_key, +SMB2_LEASE_KEY_SIZE ); } static void add_lease_global_list(struct oplock_info *opinfo,
@@ -1216,7 +1219,6 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid, struct ksmbd_file *fp, __u16 tid, struct lease_ctx_info *lctx, int share_ret) { - struct ksmbd_session *sess = work->sess; int err = 0;struct oplock_info *opinfo =NULL , *prev_opinfo =NULL ;struct ksmbd_inode *ci = fp->f_ci;
@@ -1259,12 +1261,12 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,struct oplock_info *m_opinfo;/* is lease already granted ? */ - m_opinfo =same_client_has_lease (ci, sess->ClientGUID, + m_opinfo =same_client_has_lease (ci, work->conn->ClientGUID, lctx);if (m_opinfo) {copy_lease (m_opinfo, opinfo);if (atomic_read (&m_opinfo->breaking_cnt)) - opinfo->o_lease->flags = + opinfo->o_lease->flags |=SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE ;opinfo_put (m_opinfo); goto out;
fs/smb/server/oplock.h +1 -1
@@ -116,7 +116,7 @@ void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp);struct create_context *smb2_find_context_vals (void *open_req,const char *tag,int tag_len);struct oplock_info *lookup_lease_in_table (struct ksmbd_conn *conn,char *lease_key); -int find_same_lease_key (struct ksmbd_session *sess,struct ksmbd_inode *ci, +int find_same_lease_key (struct ksmbd_conn *conn,struct ksmbd_inode *ci,struct lease_ctx_info *lctx);void destroy_lease_table (struct ksmbd_conn *conn); void smb_send_parent_lease_break_noti(struct ksmbd_file *fp,
fs/smb/server/smb2pdu.c +1 -1
@@ -3637,7 +3637,7 @@ int smb2_open(struct ksmbd_work *work)ksmbd_debug (SMB ,"lease req for(%s) req oplock state 0x%x, lease state 0x%x\n" , name, req_op_level, lc->req_state); - rc =find_same_lease_key (sess, fp->f_ci, lc); + rc =find_same_lease_key (conn, fp->f_ci, lc);if (rc) goto err_out1; } else if (open_flags == O_RDONLY &&
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 3/14 ksmbd: fix lease break and ack state handling
In reply to Namjae JeonDo not skip valid lease states containing WRITE_CACHING when breaking level-II/read leases for writes and truncates.
Handle lease break acknowledgments according to the SMB2 rule that the acknowledged state must be a subset of the server's break target. Apply the acknowledged state directly and keep the break pending on failed ACKs.
--- fs/smb/server/oplock.c | 24 +++------ fs/smb/server/smb2pdu.c | 106 ++++++++++------------------------------ 2 files changed, 34 insertions(+), 96 deletions(-)
fs/smb/server/oplock.c +8 -16
@@ -1419,14 +1419,8 @@ void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp,continue ; } -if (brk_op->is_lease && (brk_op->o_lease->state & - (~(SMB2_LEASE_READ_CACHING_LE | -SMB2_LEASE_HANDLE_CACHING_LE )))) { -ksmbd_debug (OPLOCK ,"unexpected lease state(0x%x)\n" , - brk_op->o_lease->state); - goto next; - }else if (brk_op->level != -SMB2_OPLOCK_LEVEL_II ) { +if (!brk_op->is_lease && + brk_op->level !=SMB2_OPLOCK_LEVEL_II ) {ksmbd_debug (OPLOCK ,"unexpected oplock(0x%x)\n" , brk_op->level); goto next;
@@ -1478,15 +1472,13 @@ void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp)*/ __u8 smb2_map_lease_to_oplock (__le32 lease_state) { -if (lease_state == (SMB2_LEASE_HANDLE_CACHING_LE | -SMB2_LEASE_READ_CACHING_LE | -SMB2_LEASE_WRITE_CACHING_LE )) { +if ((lease_state &SMB2_LEASE_WRITE_CACHING_LE ) && + (lease_state &SMB2_LEASE_HANDLE_CACHING_LE )) {return SMB2_OPLOCK_LEVEL_BATCH ; - }else if (lease_state !=SMB2_LEASE_WRITE_CACHING_LE && - lease_state &SMB2_LEASE_WRITE_CACHING_LE ) { -if (!(lease_state &SMB2_LEASE_HANDLE_CACHING_LE )) -return SMB2_OPLOCK_LEVEL_EXCLUSIVE ; - }else if (lease_state &SMB2_LEASE_READ_CACHING_LE ) { + }else if (lease_state &SMB2_LEASE_WRITE_CACHING_LE ) { +return SMB2_OPLOCK_LEVEL_EXCLUSIVE ; + }else if (lease_state & (SMB2_LEASE_READ_CACHING_LE | +SMB2_LEASE_HANDLE_CACHING_LE )) {return SMB2_OPLOCK_LEVEL_II ; }return 0 ;
fs/smb/server/smb2pdu.c +26 -80
147 lines not on this page · raw message
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 4/14 ksmbd: clean up lease response flags and directory leases
In reply to Namjae JeonDo not echo reserved v1 lease flags back to clients. For lease v2 responses, only return BREAK_IN_PROGRESS and PARENT_LEASE_KEY_SET when they are meaningful, and preserve the parent lease key in the response.
Allow directory leases whenever the request is a valid lease v2 request, and initialize v2 lease epochs from the first server-granted state change.
--- fs/smb/server/oplock.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
fs/smb/server/oplock.c +15 -8
@@ -34,6 +34,11 @@ static bool lease_v2_flags_valid(__le32 flags)return !(flags & ~SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE ); } +static bool lease_has_parent_key (struct lease *lease) +{ +return lease->flags &SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE ; +} +/** * alloc_opinfo() - allocate a new opinfo object for oplock info * @work: smb work
@@ -126,7 +131,7 @@ static int alloc_lease(struct oplock_info *opinfo, struct lease_ctx_info *lctx) lease->is_dir = lctx->is_dir;memcpy (lease->parent_lease_key, lctx->parent_lease_key,SMB2_LEASE_KEY_SIZE ); lease->version = lctx->version; - lease->epoch =le16_to_cpu (lctx->epoch) +1 ; + lease->epoch = lctx->version ==2 ?1 :0 ;INIT_LIST_HEAD (&opinfo->lease_entry); opinfo->o_lease = lease;
@@ -1228,9 +1233,7 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,/* Only v2 leases handle the directory */ if (S_ISDIR (file_inode (fp->filp)->i_mode)) { -if (!lctx || lctx->version !=2 || - (lctx->flags !=SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE && - !lctx->epoch)) +if (!lctx || lctx->version !=2 )return 0 ; }
@@ -1493,14 +1496,17 @@ void create_lease_buf(u8 *rbuf, struct lease *lease) {if (lease->version ==2 ) {struct create_lease_v2 *buf = (struct create_lease_v2 *)rbuf; +__le32 flags = lease->flags &SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE ;memset (buf,0 ,sizeof (struct create_lease_v2 ));memcpy (buf->lcontext.LeaseKey, lease->lease_key,SMB2_LEASE_KEY_SIZE ); - buf->lcontext.LeaseFlags = lease->flags; +if (lease_has_parent_key (lease)) + flags |=SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE ; + buf->lcontext.LeaseFlags = flags; buf->lcontext.Epoch =cpu_to_le16 (lease->epoch); buf->lcontext.LeaseState = lease->state; -if (lease->flags ==SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE ) +if (lease_has_parent_key (lease))memcpy (buf->lcontext.ParentLeaseKey, lease->parent_lease_key,SMB2_LEASE_KEY_SIZE ); buf->ccontext.DataOffset = cpu_to_le16(offsetof
@@ -1518,7 +1524,8 @@ void create_lease_buf(u8 *rbuf, struct lease *lease)memset (buf,0 ,sizeof (struct create_lease ));memcpy (buf->lcontext.LeaseKey, lease->lease_key,SMB2_LEASE_KEY_SIZE ); - buf->lcontext.LeaseFlags = lease->flags; + buf->lcontext.LeaseFlags = + lease->flags &SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE ; buf->lcontext.LeaseState = lease->state; buf->ccontext.DataOffset =cpu_to_le16 (offsetof (struct create_lease , lcontext));
@@ -1583,7 +1590,7 @@ struct lease_ctx_info *parse_lease_state(void *open_req)memcpy (lreq->lease_key, lc->lcontext.LeaseKey,SMB2_LEASE_KEY_SIZE ); lreq->req_state = lc->lcontext.LeaseState; - lreq->flags = lc->lcontext.LeaseFlags; + lreq->flags =0 ; lreq->duration = lc->lcontext.LeaseDuration;if (!lease_state_valid (lreq->req_state)) goto err_out;
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 5/14 ksmbd: share SMB2 lease state across opens
In reply to Namjae JeonModel SMB2 leases as per-client/per-key objects instead of keeping a separate lease copy in every oplock_info. The lease table now stores lease objects and each lease tracks the opens that reference it.
This makes same ClientGuid/LeaseKey opens observe a single lease state, so lease upgrades, breaks, ACKs, and close teardown do not diverge across per-open copies. Keep one reference for the lease table entry and one reference for each open, and remove the table entry when the last open is detached.
Update lease break ACK handling to refresh all open oplock levels from the shared lease state.
--- fs/smb/server/oplock.c | 262 ++++++++++++++++++++++------------------ fs/smb/server/oplock.h | 6 + fs/smb/server/smb2pdu.c | 2 +- 3 files changed, 151 insertions(+), 119 deletions(-)
fs/smb/server/oplock.c +144 -118
@@ -65,6 +65,7 @@ static struct oplock_info *alloc_opinfo(struct ksmbd_work *work, opinfo->fid = id; opinfo->Tid = Tid;INIT_LIST_HEAD (&opinfo->op_entry); +INIT_LIST_HEAD (&opinfo->lease_entry);init_waitqueue_head (&opinfo->oplock_q);init_waitqueue_head (&opinfo->oplock_brk);atomic_set (&opinfo->refcount,1 );
@@ -73,31 +74,44 @@ static struct oplock_info *alloc_opinfo(struct ksmbd_work *work,return opinfo; } -static void lease_add_list (struct oplock_info *opinfo) +static void lease_get (struct lease *lease) +{ +atomic_inc (&lease->refcount); +} + +static void lease_put (struct lease *lease) { -struct lease_table *lb = opinfo->o_lease->l_lb; +if (lease &&atomic_dec_and_test (&lease->refcount)) +kfree (lease); +} +static void lease_add_table (struct lease *lease,struct lease_table *lb) +{ +lease_get (lease); + lease->l_lb = lb;spin_lock (&lb->lb_lock); -list_add_rcu (&opinfo->lease_entry, &lb->lease_list); +list_add_rcu (&lease->l_entry, &lb->lease_list);spin_unlock (&lb->lb_lock); } -static void lease_del_list (struct oplock_info *opinfo) +static void lease_del_table (struct lease *lease) { -struct lease_table *lb = opinfo->o_lease->l_lb; +struct lease_table *lb = lease->l_lb;if (!lb)return ;spin_lock (&lb->lb_lock); -if (list_empty (&opinfo->lease_entry)) { +if (list_empty (&lease->l_entry)) {spin_unlock (&lb->lb_lock);return ; } -list_del_init (&opinfo->lease_entry); - opinfo->o_lease->l_lb =NULL ; +list_del_init (&lease->l_entry); + lease->l_lb =NULL ;spin_unlock (&lb->lb_lock); + +lease_put (lease); }static struct lease_table *alloc_lease_table (struct oplock_info *opinfo)
@@ -115,13 +129,14 @@ static struct lease_table *alloc_lease_table(struct oplock_info *opinfo)return lb; } -static int alloc_lease (struct oplock_info *opinfo,struct lease_ctx_info *lctx) +static struct lease *alloc_lease (struct lease_ctx_info *lctx, +struct ksmbd_inode *ci) {struct lease *lease; lease = kmalloc_obj(struct lease, KSMBD_DEFAULT_GFP);if (!lease) -return -ENOMEM ; +return NULL ;memcpy (lease->lease_key, lctx->lease_key,SMB2_LEASE_KEY_SIZE ); lease->state = lctx->req_state;
@@ -132,18 +147,48 @@ static int alloc_lease(struct oplock_info *opinfo, struct lease_ctx_info *lctx)memcpy (lease->parent_lease_key, lctx->parent_lease_key,SMB2_LEASE_KEY_SIZE ); lease->version = lctx->version; lease->epoch = lctx->version ==2 ?1 :0 ; -INIT_LIST_HEAD (&opinfo->lease_entry); - opinfo->o_lease = lease; + lease->ci = ci; + lease->l_lb =NULL ; +INIT_LIST_HEAD (&lease->l_entry); +INIT_LIST_HEAD (&lease->open_list); +spin_lock_init (&lease->lock); +atomic_set (&lease->refcount,1 ); + +return lease; +} -return 0 ; +static void lease_add_open (struct lease *lease,struct oplock_info *opinfo) +{ +spin_lock (&lease->lock); +list_add (&opinfo->lease_entry, &lease->open_list); +spin_unlock (&lease->lock); } -static void free_lease (struct oplock_info *opinfo) +static void lease_del_open (struct oplock_info *opinfo) { -struct lease *lease; +struct lease *lease = opinfo->o_lease; +bool remove_table = false; + +if (!lease) +return ; + +spin_lock (&lease->lock); +if (!list_empty (&opinfo->lease_entry)) { +list_del_init (&opinfo->lease_entry); + remove_table =list_empty (&lease->open_list); + } +spin_unlock (&lease->lock); + +if (remove_table) { +write_lock (&lease_list_lock); +lease_del_table (lease); +write_unlock (&lease_list_lock); + } +} - lease = opinfo->o_lease; -kfree (lease); +static void free_lease (struct oplock_info *opinfo) +{ +lease_put (opinfo->o_lease); }static void __free_opinfo (struct oplock_info *opinfo)
@@ -166,6 +211,21 @@ static void free_opinfo(struct oplock_info *opinfo)call_rcu (&opinfo->rcu, free_opinfo_rcu); } +void lease_update_oplock_levels (struct lease *lease) +{ +struct oplock_info *opinfo; +__u8 level; + +if (!lease) +return ; + + level =smb2_map_lease_to_oplock (lease->state); +spin_lock (&lease->lock); +list_for_each_entry (opinfo, &lease->open_list, lease_entry) + opinfo->level = level; +spin_unlock (&lease->lock); +} +struct oplock_info *opinfo_get (struct ksmbd_file *fp) {struct oplock_info *opinfo;
@@ -226,11 +286,9 @@ static void opinfo_del(struct oplock_info *opinfo) {struct ksmbd_inode *ci = opinfo->o_fp->f_ci; -if (opinfo->is_lease) { -write_lock (&lease_list_lock); -lease_del_list (opinfo); -write_unlock (&lease_list_lock); - } +if (opinfo->is_lease) +lease_del_open (opinfo); +down_write (&ci->m_lock);list_del (&opinfo->op_entry);up_write (&ci->m_lock);
@@ -279,8 +337,10 @@ int opinfo_write_to_read(struct oplock_info *opinfo) } opinfo->level =SMB2_OPLOCK_LEVEL_II ; -if (opinfo->is_lease) +if (opinfo->is_lease) { lease->state = lease->new_state; +lease_update_oplock_levels (lease); + }return 0 ; }
@@ -295,7 +355,7 @@ int opinfo_read_handle_to_read(struct oplock_info *opinfo)struct lease *lease = opinfo->o_lease; lease->state = lease->new_state; - opinfo->level =SMB2_OPLOCK_LEVEL_II ; +lease_update_oplock_levels (lease);return 0 ; }
@@ -317,8 +377,10 @@ int opinfo_write_to_none(struct oplock_info *opinfo)return -EINVAL ; } opinfo->level =SMB2_OPLOCK_LEVEL_NONE ; -if (opinfo->is_lease) +if (opinfo->is_lease) { lease->state = lease->new_state; +lease_update_oplock_levels (lease); + }return 0 ; }
@@ -339,8 +401,10 @@ int opinfo_read_to_none(struct oplock_info *opinfo)return -EINVAL ; } opinfo->level =SMB2_OPLOCK_LEVEL_NONE ; -if (opinfo->is_lease) +if (opinfo->is_lease) { lease->state = lease->new_state; +lease_update_oplock_levels (lease); + }return 0 ; }
@@ -361,10 +425,7 @@ int lease_read_to_write(struct oplock_info *opinfo) lease->new_state =SMB2_LEASE_NONE_LE ; lease->state |=SMB2_LEASE_WRITE_CACHING_LE ; -if (lease->state &SMB2_LEASE_HANDLE_CACHING_LE ) - opinfo->level =SMB2_OPLOCK_LEVEL_BATCH ; -else - opinfo->level =SMB2_OPLOCK_LEVEL_EXCLUSIVE ; +lease_update_oplock_levels (lease);return 0 ; }
@@ -386,15 +447,7 @@ static int lease_none_upgrade(struct oplock_info *opinfo, __le32 new_state) lease->new_state =SMB2_LEASE_NONE_LE ; lease->state = new_state; -if (lease->state &SMB2_LEASE_HANDLE_CACHING_LE ) -if (lease->state &SMB2_LEASE_WRITE_CACHING_LE ) - opinfo->level =SMB2_OPLOCK_LEVEL_BATCH ; -else - opinfo->level =SMB2_OPLOCK_LEVEL_II ; -else if (lease->state &SMB2_LEASE_WRITE_CACHING_LE ) - opinfo->level =SMB2_OPLOCK_LEVEL_EXCLUSIVE ; -else if (lease->state &SMB2_LEASE_READ_CACHING_LE ) - opinfo->level =SMB2_OPLOCK_LEVEL_II ; +lease_update_oplock_levels (lease);return 0 ; }
@@ -577,6 +630,7 @@ static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci, SMB2_LEASE_HANDLE_CACHING_LE)) { lease->epoch++; lease->state = lctx->req_state; +lease_update_oplock_levels (lease); } }
@@ -603,8 +657,10 @@ static void wait_for_break_ack(struct oplock_info *opinfo)/* is this a timeout ? */ if (!rc) { -if (opinfo->is_lease) +if (opinfo->is_lease) { opinfo->o_lease->state =SMB2_LEASE_NONE_LE ; +lease_update_oplock_levels (opinfo->o_lease); + } opinfo->level =SMB2_OPLOCK_LEVEL_NONE ; opinfo->op_state =OPLOCK_STATE_NONE ; }
@@ -884,8 +940,8 @@ static int smb2_lease_break_noti(struct oplock_info *opinfo) }else {__smb2_lease_break_noti (&work->work);if (opinfo->o_lease->new_state ==SMB2_LEASE_NONE_LE ) { - opinfo->level =SMB2_OPLOCK_LEVEL_NONE ; opinfo->o_lease->state =SMB2_LEASE_NONE_LE ; +lease_update_oplock_levels (opinfo->o_lease); } }return 0 ;
@@ -990,7 +1046,7 @@ static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level,void destroy_lease_table (struct ksmbd_conn *conn) {struct lease_table *lb, *lbtmp; -struct oplock_info *opinfo; +struct lease *lease, *ltmp;write_lock (&lease_list_lock);if (list_empty (&lease_table_list)) {
@@ -1002,15 +1058,8 @@ void destroy_lease_table(struct ksmbd_conn *conn)if (conn &&memcmp (lb->client_guid, conn->ClientGUID,SMB2_CLIENT_GUID_SIZE ))continue ; -again: -rcu_read_lock (); -list_for_each_entry_rcu (opinfo, &lb->lease_list, - lease_entry) { -rcu_read_unlock (); -lease_del_list (opinfo); - goto again; - } -rcu_read_unlock (); +list_for_each_entry_safe (lease, ltmp, &lb->lease_list, l_entry) +lease_del_table (lease);list_del (&lb->l_entry);kfree (lb); }
@@ -1020,7 +1069,7 @@ void destroy_lease_table(struct ksmbd_conn *conn)int find_same_lease_key (struct ksmbd_conn *conn,struct ksmbd_inode *ci,struct lease_ctx_info *lctx) { -struct oplock_info *opinfo; +struct lease *lease;int err =0 ;struct lease_table *lb;
@@ -1043,70 +1092,40 @@ int find_same_lease_key(struct ksmbd_conn *conn, struct ksmbd_inode *ci,return 0 ; found: -rcu_read_lock (); -list_for_each_entry_rcu (opinfo, &lb->lease_list, lease_entry) { -if (!atomic_inc_not_zero (&opinfo->refcount)) +list_for_each_entry (lease, &lb->lease_list, l_entry) { +if (lease->ci == ci)continue ; -rcu_read_unlock (); -if (opinfo->o_fp->f_ci == ci) - goto op_next; - err =compare_guid_key (opinfo, conn->ClientGUID, - lctx->lease_key); -if (err) { +if (!memcmp (lease->lease_key, lctx->lease_key, +SMB2_LEASE_KEY_SIZE )) { err = -EINVAL ;ksmbd_debug (OPLOCK ,"found same lease key is already used in other files\n" ); -opinfo_put (opinfo); goto out; } -op_next: -opinfo_put (opinfo); -rcu_read_lock (); } -rcu_read_unlock (); out:read_unlock (&lease_list_lock);return err; } -static void copy_lease (struct oplock_info *op1,struct oplock_info *op2) -{ -struct lease *lease1 = op1->o_lease; -struct lease *lease2 = op2->o_lease; - - op2->level = op1->level; - lease2->state = lease1->state; -memcpy (lease2->lease_key, lease1->lease_key, -SMB2_LEASE_KEY_SIZE ); - lease2->duration = lease1->duration; - lease2->flags = lease1->flags; - lease2->epoch = lease1->epoch; - lease2->version = lease1->version; - lease2->is_dir = lease1->is_dir; -memcpy (lease2->parent_lease_key, lease1->parent_lease_key, -SMB2_LEASE_KEY_SIZE ); -} - -static void add_lease_global_list (struct oplock_info *opinfo, +static void add_lease_global_list (struct lease *lease,struct ksmbd_conn *conn,struct lease_table *new_lb) {struct lease_table *lb;write_lock (&lease_list_lock);list_for_each_entry (lb, &lease_table_list, l_entry) { -if (!memcmp (lb->client_guid, opinfo->conn->ClientGUID, +if (!memcmp (lb->client_guid, conn->ClientGUID,SMB2_CLIENT_GUID_SIZE )) { - opinfo->o_lease->l_lb = lb; -lease_add_list (opinfo); +lease_add_table (lease, lb);write_unlock (&lease_list_lock);kfree (new_lb);return ; } } - opinfo->o_lease->l_lb = new_lb; -lease_add_list (opinfo); +lease_add_table (lease, new_lb);list_add (&new_lb->l_entry, &lease_table_list);write_unlock (&lease_list_lock); }
@@ -1229,6 +1248,7 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,struct ksmbd_inode *ci = fp->f_ci;struct lease_table *new_lb =NULL ;bool prev_op_has_lease; +bool new_lease = false;__le32 prev_op_state =0 ;/* Only v2 leases handle the directory */
@@ -1242,10 +1262,13 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,return -ENOMEM ;if (lctx) { - err =alloc_lease (opinfo, lctx); -if (err) + opinfo->o_lease =alloc_lease (lctx, ci); +if (!opinfo->o_lease) { + err = -ENOMEM ; goto err_out; + } opinfo->is_lease =1 ; + new_lease = true; }/* ci does not have any oplock */
@@ -1267,7 +1290,11 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid, m_opinfo =same_client_has_lease (ci, work->conn->ClientGUID, lctx);if (m_opinfo) { -copy_lease (m_opinfo, opinfo); +lease_put (opinfo->o_lease); +lease_get (m_opinfo->o_lease); + opinfo->o_lease = m_opinfo->o_lease; + opinfo->level = m_opinfo->level; + new_lease = false;if (atomic_read (&m_opinfo->breaking_cnt)) opinfo->o_lease->flags |=SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE ;
@@ -1330,17 +1357,13 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid, out:/* -* Set o_fp before any publication so that concurrent readers -* (e.g. find_same_lease_key() on the lease list) that -* dereference opinfo->o_fp don't hit a NULL pointer. -* * Keep the original publication order so concurrent opens can * still observe the in-flight grant via ci->m_op_list, but make * everything after opinfo_add() no-fail by preallocating any new * lease_table first. */ opinfo->o_fp = fp; -if (opinfo->is_lease) { +if (new_lease) { new_lb =alloc_lease_table (opinfo);if (!new_lb) { err = -ENOMEM ;
@@ -1351,8 +1374,10 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,opinfo_count_inc (fp);opinfo_add (opinfo, fp); +if (new_lease) +add_lease_global_list (opinfo->o_lease, opinfo->conn, new_lb);if (opinfo->is_lease) -add_lease_global_list (opinfo, new_lb); +lease_add_open (opinfo->o_lease, opinfo);rcu_assign_pointer (fp->f_opinfo, opinfo);
@@ -1834,8 +1859,8 @@ struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn, char *lease_key) {struct oplock_info *opinfo =NULL , *ret_op =NULL ; +struct lease *lease;struct lease_table *lt; -int ret;read_lock (&lease_list_lock);list_for_each_entry (lt, &lease_table_list, l_entry) {
@@ -1848,29 +1873,30 @@ struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn,return NULL ; found: -rcu_read_lock (); -list_for_each_entry_rcu (opinfo, <->lease_list, lease_entry) { -if (!atomic_inc_not_zero (&opinfo->refcount)) +list_for_each_entry (lease, <->lease_list, l_entry) { +if (memcmp (lease->lease_key, lease_key,SMB2_LEASE_KEY_SIZE ))continue ; -rcu_read_unlock (); -if (!opinfo->op_state || opinfo->op_state ==OPLOCK_CLOSING ) - goto op_next; -if (!(opinfo->o_lease->state & - (SMB2_LEASE_HANDLE_CACHING_LE | -SMB2_LEASE_WRITE_CACHING_LE ))) - goto op_next; - ret =compare_guid_key (opinfo, conn->ClientGUID, - lease_key); -if (ret) { -ksmbd_debug (OPLOCK ,"found opinfo\n" ); +if (!(lease->state & (SMB2_LEASE_HANDLE_CACHING_LE | +SMB2_LEASE_WRITE_CACHING_LE ))) +break ; + +spin_lock (&lease->lock); +list_for_each_entry (opinfo, &lease->open_list, lease_entry) { +if (!opinfo->op_state || + opinfo->op_state ==OPLOCK_CLOSING ) +continue ; +if (!atomic_inc_not_zero (&opinfo->refcount)) +continue ; ret_op = opinfo; +break ; + } +spin_unlock (&lease->lock); +if (ret_op) { +ksmbd_debug (OPLOCK ,"found opinfo\n" ); goto out; } -op_next: -opinfo_put (opinfo); -rcu_read_lock (); +break ; } -rcu_read_unlock (); out:read_unlock (&lease_list_lock);
fs/smb/server/oplock.h +6 -0
@@ -49,7 +49,12 @@ struct lease {int version;unsigned short epoch;bool is_dir; +struct ksmbd_inode *ci;struct lease_table *l_lb; +struct list_head l_entry; +struct list_head open_list; +spinlock_t lock; +atomic_t refcount; };struct oplock_info {
@@ -105,6 +110,7 @@ void opinfo_put(struct oplock_info *opinfo);void create_lease_buf (u8 *rbuf,struct lease *lease);struct lease_ctx_info *parse_lease_state (void *open_req);__u8 smb2_map_lease_to_oplock (__le32 lease_state); +void lease_update_oplock_levels (struct lease *lease);int lease_read_to_write (struct oplock_info *opinfo);/* Durable related functions */
fs/smb/server/smb2pdu.c +1 -1
@@ -9087,7 +9087,7 @@ static void smb21_lease_break_ack(struct ksmbd_work *work) lease_state = req->LeaseState; lease->state = lease_state; lease->new_state =SMB2_LEASE_NONE_LE ; - opinfo->level =smb2_map_lease_to_oplock (lease_state); +lease_update_oplock_levels (lease); rsp->StructureSize =cpu_to_le16 (36 ); rsp->Reserved =0 ;
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 6/14 ksmbd: align SMB2 oplock break ack handling
In reply to Namjae JeonHandle SMB2 oplock break acknowledgments according to the server-side validation rules in MS-SMB2.
Return STATUS_INVALID_DEVICE_STATE when an ACK arrives while the open is not breaking, reject SMB2_OPLOCK_LEVEL_LEASE with STATUS_INVALID_PARAMETER, allow BATCH acknowledgments to EXCLUSIVE, and make invalid ACK levels fail with STATUS_INVALID_OPLOCK_PROTOCOL after lowering the oplock to NONE.
Update the successful response from the final granted oplock level instead of relying on the oplock transition helpers, which could turn invalid ACKs into successful responses.
--- fs/smb/server/smb2pdu.c | 104 ++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 58 deletions(-)
fs/smb/server/smb2pdu.c +46 -58
@@ -8906,11 +8906,10 @@ static void smb20_oplock_break_ack(struct ksmbd_work *work)struct smb2_oplock_break *rsp;struct ksmbd_file *fp;struct oplock_info *opinfo =NULL ; -__le32 err =0 ; -int ret =0 ; +__le32 status =STATUS_SUCCESS ; +int ret;u64 volatile_id, persistent_id;char req_oplevel =0 , rsp_oplevel =0 ; -unsigned int oplock_change_type;WORK_BUFFERS (work, req, rsp);
@@ -8936,71 +8935,55 @@ static void smb20_oplock_break_ack(struct ksmbd_work *work)return ; } -if (opinfo->level ==SMB2_OPLOCK_LEVEL_NONE ) { - rsp->hdr.Status =STATUS_INVALID_OPLOCK_PROTOCOL ; +if (opinfo->op_state !=OPLOCK_ACK_WAIT ) { +ksmbd_debug (SMB ,"unexpected oplock state 0x%x\n" , + opinfo->op_state); + status =STATUS_INVALID_DEVICE_STATE ; goto err_out; } -if (opinfo->op_state ==OPLOCK_STATE_NONE ) { -ksmbd_debug (SMB ,"unexpected oplock state 0x%x\n" , opinfo->op_state); - rsp->hdr.Status =STATUS_UNSUCCESSFUL ; +if (req_oplevel ==SMB2_OPLOCK_LEVEL_LEASE ) { + opinfo->level =SMB2_OPLOCK_LEVEL_NONE ; + status =STATUS_INVALID_PARAMETER ; goto err_out; } -if ((opinfo->level ==SMB2_OPLOCK_LEVEL_EXCLUSIVE || - opinfo->level ==SMB2_OPLOCK_LEVEL_BATCH ) && - (req_oplevel !=SMB2_OPLOCK_LEVEL_II && - req_oplevel !=SMB2_OPLOCK_LEVEL_NONE )) { - err =STATUS_INVALID_OPLOCK_PROTOCOL ; - oplock_change_type =OPLOCK_WRITE_TO_NONE ; - }else if (opinfo->level ==SMB2_OPLOCK_LEVEL_II && - req_oplevel !=SMB2_OPLOCK_LEVEL_NONE ) { - err =STATUS_INVALID_OPLOCK_PROTOCOL ; - oplock_change_type =OPLOCK_READ_TO_NONE ; - }else if (req_oplevel ==SMB2_OPLOCK_LEVEL_II || - req_oplevel ==SMB2_OPLOCK_LEVEL_NONE ) { - err =STATUS_INVALID_DEVICE_STATE ; -if ((opinfo->level ==SMB2_OPLOCK_LEVEL_EXCLUSIVE || - opinfo->level ==SMB2_OPLOCK_LEVEL_BATCH ) && - req_oplevel ==SMB2_OPLOCK_LEVEL_II ) { - oplock_change_type =OPLOCK_WRITE_TO_READ ; - }else if ((opinfo->level ==SMB2_OPLOCK_LEVEL_EXCLUSIVE || - opinfo->level ==SMB2_OPLOCK_LEVEL_BATCH ) && - req_oplevel ==SMB2_OPLOCK_LEVEL_NONE ) { - oplock_change_type =OPLOCK_WRITE_TO_NONE ; - }else if (opinfo->level ==SMB2_OPLOCK_LEVEL_II && - req_oplevel ==SMB2_OPLOCK_LEVEL_NONE ) { - oplock_change_type =OPLOCK_READ_TO_NONE ; - }else { - oplock_change_type =0 ; - } - }else { - oplock_change_type =0 ; +if (opinfo->level ==SMB2_OPLOCK_LEVEL_NONE ) { + status =STATUS_INVALID_OPLOCK_PROTOCOL ; + goto err_out; } -switch (oplock_change_type) { -case OPLOCK_WRITE_TO_READ : - ret =opinfo_write_to_read (opinfo); - rsp_oplevel =SMB2_OPLOCK_LEVEL_II ; -break ; -case OPLOCK_WRITE_TO_NONE : - ret =opinfo_write_to_none (opinfo); - rsp_oplevel =SMB2_OPLOCK_LEVEL_NONE ; -break ; -case OPLOCK_READ_TO_NONE : - ret =opinfo_read_to_none (opinfo); - rsp_oplevel =SMB2_OPLOCK_LEVEL_NONE ; -break ; -default : -pr_err ("unknown oplock change 0x%x -> 0x%x\n" , - opinfo->level, rsp_oplevel); +if (opinfo->level ==SMB2_OPLOCK_LEVEL_EXCLUSIVE && + req_oplevel !=SMB2_OPLOCK_LEVEL_II && + req_oplevel !=SMB2_OPLOCK_LEVEL_NONE ) { + opinfo->level =SMB2_OPLOCK_LEVEL_NONE ; + status =STATUS_INVALID_OPLOCK_PROTOCOL ; + goto err_out; } -if (ret <0 ) { - rsp->hdr.Status = err; +if (opinfo->level ==SMB2_OPLOCK_LEVEL_BATCH && + req_oplevel !=SMB2_OPLOCK_LEVEL_II && + req_oplevel !=SMB2_OPLOCK_LEVEL_NONE && + req_oplevel !=SMB2_OPLOCK_LEVEL_EXCLUSIVE ) { + opinfo->level =SMB2_OPLOCK_LEVEL_NONE ; + status =STATUS_INVALID_OPLOCK_PROTOCOL ; + goto err_out; + } + +if (opinfo->level ==SMB2_OPLOCK_LEVEL_II && + req_oplevel !=SMB2_OPLOCK_LEVEL_NONE ) { + opinfo->level =SMB2_OPLOCK_LEVEL_NONE ; + status =STATUS_INVALID_OPLOCK_PROTOCOL ; goto err_out; } +if (req_oplevel ==SMB2_OPLOCK_LEVEL_EXCLUSIVE ) + rsp_oplevel =SMB2_OPLOCK_LEVEL_NONE ; +else + rsp_oplevel = req_oplevel; + + opinfo->level = rsp_oplevel; + rsp->StructureSize =cpu_to_le16 (24 ); rsp->OplockLevel = rsp_oplevel; rsp->Reserved =0 ;
@@ -9008,11 +8991,16 @@ static void smb20_oplock_break_ack(struct ksmbd_work *work) rsp->VolatileFid = volatile_id; rsp->PersistentFid = persistent_id; ret =ksmbd_iov_pin_rsp (work, rsp,sizeof (struct smb2_oplock_break )); -if (ret) { +if (ret) +ksmbd_debug (SMB ,"failed to pin oplock break response: %d\n" , + ret); + goto out; + err_out: -smb2_set_err_rsp (work); - } + rsp->hdr.Status = status; +smb2_set_err_rsp (work); +out: opinfo->op_state =OPLOCK_STATE_NONE ;wake_up_interruptible_all (&opinfo->oplock_q);opinfo_put (opinfo);
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 7/14 ksmbd: treat unnamed DATA stream as base file
In reply to Namjae JeonThe SMB path suffix :: names the unnamed data stream of the base file, not an alternate data stream backed by a DosStream xattr.
Canonicalize an empty stream name with an explicit type to a NULL stream name after parsing. This keeps the base filename produced by strsep() and lets open continue through the normal base-file path instead of looking for a non-existent empty stream xattr.
--- fs/smb/server/misc.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
fs/smb/server/misc.c +11 -3
@@ -121,7 +121,9 @@ int parse_stream_name(char *filename, char **stream_name, int *s_type)char *stream_type;char *s_name;int rc =0 ; +bool has_stream_type = false; + *stream_name =NULL ; s_name = filename; filename =strsep (&s_name,":" );ksmbd_debug (SMB ,"filename : %s, streams : %s\n" , filename, s_name);
@@ -137,14 +139,20 @@ int parse_stream_name(char *filename, char **stream_name, int *s_type)ksmbd_debug (SMB ,"stream name : %s, stream type : %s\n" , s_name, stream_type); -if (!strncasecmp ("$data" , stream_type,5 )) +if (!strncasecmp ("$data" , stream_type,5 )) { *s_type =DATA_STREAM ; -else if (!strncasecmp ("$index_allocation" , stream_type,17 )) + has_stream_type = true; + }else if (!strncasecmp ("$index_allocation" , stream_type,17 )) { *s_type =DIR_STREAM ; -else + has_stream_type = true; + }else { rc = -ENOENT ; + } } +if (has_stream_type && !s_name[0 ] && *s_type ==DATA_STREAM ) + goto out; + *stream_name = s_name; out:return rc;
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 8/14 ksmbd: compute lease break-in-progress flag on response
In reply to Namjae JeonSMB2_LEASE_FLAG_BREAK_IN_PROGRESS is a transient create response flag, not persistent lease state.
Do not store the flag in lease->flags when a same-key open is granted during a pending break. Instead, derive it from lease opens that are still waiting for a break ACK while building the lease create response, and keep lease->flags for persistent lease flags such as the parent lease key.
This clears the flag naturally after the break ACK completes and fixes reopen responses that report BREAK_IN_PROGRESS after the lease is no longer breaking.
--- fs/smb/server/oplock.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-)
fs/smb/server/oplock.c +29 -12
@@ -39,6 +39,23 @@ static bool lease_has_parent_key(struct lease *lease)return lease->flags &SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE ; } +static bool lease_break_in_progress (struct lease *lease) +{ +struct oplock_info *opinfo; +bool ret = false; + +spin_lock (&lease->lock); +list_for_each_entry (opinfo, &lease->open_list, lease_entry) { +if (opinfo->op_state ==OPLOCK_ACK_WAIT ) { + ret = true; +break ; + } + } +spin_unlock (&lease->lock); + +return ret; +} +/** * alloc_opinfo() - allocate a new opinfo object for oplock info * @work: smb work
@@ -1292,15 +1309,12 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,if (m_opinfo) {lease_put (opinfo->o_lease);lease_get (m_opinfo->o_lease); - opinfo->o_lease = m_opinfo->o_lease; - opinfo->level = m_opinfo->level; - new_lease = false; -if (atomic_read (&m_opinfo->breaking_cnt)) - opinfo->o_lease->flags |= -SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE ; -opinfo_put (m_opinfo); - goto out; - } + opinfo->o_lease = m_opinfo->o_lease; + opinfo->level = m_opinfo->level; + new_lease = false; +opinfo_put (m_opinfo); + goto out; + } } prev_opinfo =opinfo_get_list (ci); if (!prev_opinfo ||
@@ -1521,13 +1535,15 @@ void create_lease_buf(u8 *rbuf, struct lease *lease) {if (lease->version ==2 ) {struct create_lease_v2 *buf = (struct create_lease_v2 *)rbuf; -__le32 flags = lease->flags &SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE ; +__le32 flags =0 ;memset (buf,0 ,sizeof (struct create_lease_v2 ));memcpy (buf->lcontext.LeaseKey, lease->lease_key,SMB2_LEASE_KEY_SIZE );if (lease_has_parent_key (lease)) flags |=SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE ; +if (lease_break_in_progress (lease)) + flags |=SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE ; buf->lcontext.LeaseFlags = flags; buf->lcontext.Epoch =cpu_to_le16 (lease->epoch); buf->lcontext.LeaseState = lease->state;
@@ -1549,8 +1565,9 @@ void create_lease_buf(u8 *rbuf, struct lease *lease)memset (buf,0 ,sizeof (struct create_lease ));memcpy (buf->lcontext.LeaseKey, lease->lease_key,SMB2_LEASE_KEY_SIZE ); - buf->lcontext.LeaseFlags = - lease->flags &SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE ; +if (lease_break_in_progress (lease)) + buf->lcontext.LeaseFlags = +SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE ; buf->lcontext.LeaseState = lease->state; buf->ccontext.DataOffset =cpu_to_le16 (offsetof (struct create_lease , lcontext));
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 9/14 ksmbd: chain pending lease breaks before waking waiters
In reply to Namjae JeonA pending open can require more than one lease break before the existing lease becomes compatible with the operation that triggered the break. smb2.lease.breaking3 expects the server to hold the pending normal open through RWH->RH and RH->R, while a later overwrite waiter must not collapse that second break directly to RH->NONE.
Keep pending_break held for lease breaks until the current triggering operation is compatible with the lease state. Snapshot the truncate request per oplock_break() call so another waiter cannot overwrite the state of the active break.
Use the requested oplock level when deciding whether to chain another break. A second lease open only needs RWH->RH, while a normal none-oplock open can continue down to R and then NONE.
For non-truncating metadata operations, break leases only down to read caching. Operations such as delete-on-close need to drop handle caching, but should not send a second R->NONE break after the client acknowledges RH->R.
Also send STATUS_PENDING for levelII/read-lease break waiters. An async SMB2 create becomes cancelable only after the server sends
an NT_STATUS_PENDING interim response. A waiter that blocks behind an already active lease break must receive the interim response before sleeping on pending_break, otherwise the client can process a later lease break while the create request is still not marked pending.
Avoid duplicate interim responses when an overwrite first breaks a write oplock and then scans levelII/read leases.
--- fs/smb/server/oplock.c | 98 ++++++++++++++++++++++++++++++++---------- 1 file changed, 75 insertions(+), 23 deletions(-)
fs/smb/server/oplock.c +75 -23
@@ -727,6 +727,17 @@ static int oplock_break_pending(struct oplock_info *opinfo, int req_op_level)return 0 ; } +static bool lease_break_needed (struct oplock_info *opinfo,int req_op_level, +bool open_trunc) +{ +struct lease *lease = opinfo->o_lease; + +if (open_trunc) +return lease->state !=SMB2_LEASE_NONE_LE ; + +return opinfo->level > req_op_level; +} +/** * __smb2_oplock_break_noti() - send smb2 oplock break cmd from conn * to client
@@ -985,6 +996,7 @@ static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level, struct ksmbd_work *in_work) {int err =0 ; +bool sent_interim = false;/* Need to break exclusive/batch oplock, write lease or overwrite_if */ ksmbd_debug(OPLOCK,
@@ -993,13 +1005,22 @@ static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level,if (brk_opinfo->is_lease) {struct lease *lease = brk_opinfo->o_lease; +bool open_trunc = brk_opinfo->open_trunc; + +if (in_work &&test_bit (0 , &brk_opinfo->pending_break)) { +setup_async_work (in_work,NULL ,NULL ); +smb2_send_interim_resp (in_work,STATUS_PENDING ); +release_async_work (in_work); + sent_interim = true; + } -atomic_inc (&brk_opinfo->breaking_cnt); err =oplock_break_pending (brk_opinfo, req_op_level);if (err)return err <0 ? err :0 ; -if (brk_opinfo->open_trunc) { +again: +atomic_inc (&brk_opinfo->breaking_cnt); +if (open_trunc) {/* * Create overwrite break trigger the lease break to * none.
@@ -1024,17 +1045,31 @@ static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level, } } +if (in_work && !sent_interim) { +setup_async_work (in_work,NULL ,NULL ); +smb2_send_interim_resp (in_work,STATUS_PENDING ); +release_async_work (in_work); + sent_interim = true; + } +if (lease->state & (SMB2_LEASE_WRITE_CACHING_LE |SMB2_LEASE_HANDLE_CACHING_LE )) { -if (in_work) { -setup_async_work (in_work,NULL ,NULL ); -smb2_send_interim_resp (in_work,STATUS_PENDING ); -release_async_work (in_work); - } - brk_opinfo->op_state =OPLOCK_ACK_WAIT ; }else atomic_dec (&brk_opinfo->breaking_cnt); + + err =smb2_lease_break_noti (brk_opinfo); + +ksmbd_debug (OPLOCK ,"oplock granted = %d\n" , brk_opinfo->level); +if (brk_opinfo->op_state ==OPLOCK_CLOSING ) + err = -ENOENT ; + +wait_lease_breaking (brk_opinfo); +if (!err &&lease_break_needed (brk_opinfo, req_op_level, open_trunc)) + goto again; + +wake_up_oplock_break (brk_opinfo); +return err; }else { err =oplock_break_pending (brk_opinfo, req_op_level);if (err)
@@ -1045,18 +1080,13 @@ static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level, brk_opinfo->op_state =OPLOCK_ACK_WAIT ; } -if (brk_opinfo->is_lease) - err =smb2_lease_break_noti (brk_opinfo); -else - err =smb2_oplock_break_noti (brk_opinfo); + err =smb2_oplock_break_noti (brk_opinfo);ksmbd_debug (OPLOCK ,"oplock granted = %d\n" , brk_opinfo->level);if (brk_opinfo->op_state ==OPLOCK_CLOSING ) err = -ENOENT ;wake_up_oplock_break (brk_opinfo); -wait_lease_breaking (brk_opinfo); -return err; }
@@ -1261,6 +1291,7 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid, struct lease_ctx_info *lctx, int share_ret) { int err = 0; +int break_level =SMB2_OPLOCK_LEVEL_II ;struct oplock_info *opinfo =NULL , *prev_opinfo =NULL ;struct ksmbd_inode *ci = fp->f_ci;struct lease_table *new_lb =NULL ;
@@ -1325,6 +1356,9 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid, prev_op_has_lease = prev_opinfo->is_lease;if (prev_op_has_lease) prev_op_state = prev_opinfo->o_lease->state; +if (prev_op_has_lease && !lctx && + prev_op_state &SMB2_LEASE_HANDLE_CACHING_LE ) + break_level =SMB2_OPLOCK_LEVEL_NONE ;if (share_ret <0 && prev_opinfo->level ==SMB2_OPLOCK_LEVEL_EXCLUSIVE ) {
@@ -1339,7 +1373,7 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid, goto op_break_not_needed; } - err =oplock_break (prev_opinfo,SMB2_OPLOCK_LEVEL_II , work); + err =oplock_break (prev_opinfo, break_level, work);opinfo_put (prev_opinfo);if (err == -ENOENT ) goto set_lev;
@@ -1408,23 +1442,27 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,* @fp: ksmbd file pointer * @is_trunc: truncate on open */ -static void smb_break_all_write_oplock (struct ksmbd_work *work, +static bool smb_break_all_write_oplock (struct ksmbd_work *work,struct ksmbd_file *fp,int is_trunc) {struct oplock_info *brk_opinfo; +bool sent_break = false; brk_opinfo =opinfo_get_list (fp->f_ci);if (!brk_opinfo) -return ; +return false;if (brk_opinfo->level !=SMB2_OPLOCK_LEVEL_BATCH && brk_opinfo->level !=SMB2_OPLOCK_LEVEL_EXCLUSIVE ) {opinfo_put (brk_opinfo); -return ; +return false; } brk_opinfo->open_trunc = is_trunc;oplock_break (brk_opinfo,SMB2_OPLOCK_LEVEL_II , work); + sent_break = true;opinfo_put (brk_opinfo); + +return sent_break; }/**
@@ -1434,12 +1472,14 @@ static void smb_break_all_write_oplock(struct ksmbd_work *work,* @fp: ksmbd file pointer * @is_trunc: truncate on open */ -void smb_break_all_levII_oplock (struct ksmbd_work *work,struct ksmbd_file *fp, -int is_trunc) +static void __smb_break_all_levII_oplock (struct ksmbd_work *work, +struct ksmbd_file *fp,int is_trunc, +bool send_interim) {struct oplock_info *op, *brk_op;struct ksmbd_inode *ci;struct ksmbd_conn *conn = work->conn; +bool sent_interim = false;if (!test_share_config_flag (work->tcon->share_conf,KSMBD_SHARE_FLAG_OPLOCKS ))
@@ -1481,7 +1521,11 @@ void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp, SMB2_LEASE_KEY_SIZE)) goto next; brk_op->open_trunc = is_trunc; -oplock_break (brk_op,SMB2_OPLOCK_LEVEL_NONE ,NULL ); +oplock_break (brk_op, + brk_op->is_lease && !is_trunc ? +SMB2_OPLOCK_LEVEL_II :SMB2_OPLOCK_LEVEL_NONE , + send_interim && !sent_interim ? work :NULL ); + sent_interim = true; next:opinfo_put (brk_op); }
@@ -1491,6 +1535,12 @@ void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp,opinfo_put (op); } +void smb_break_all_levII_oplock (struct ksmbd_work *work,struct ksmbd_file *fp, +int is_trunc) +{ +__smb_break_all_levII_oplock (work, fp, is_trunc, true); +} +/** * smb_break_all_oplock() - break both batch/exclusive and level2 oplock * @work: smb work
@@ -1498,12 +1548,14 @@ void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp,*/ void smb_break_all_oplock (struct ksmbd_work *work,struct ksmbd_file *fp) { +bool sent_break; +if (!test_share_config_flag (work->tcon->share_conf,KSMBD_SHARE_FLAG_OPLOCKS ))return ; -smb_break_all_write_oplock (work, fp,1 ); -smb_break_all_levII_oplock (work, fp,1 ); + sent_break =smb_break_all_write_oplock (work, fp,1 ); +__smb_break_all_levII_oplock (work, fp,1 , !sent_break); }/**
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 10/14 ksmbd: do not wait for RH lease break ack on overwrite
In reply to Namjae Jeonsmb2.lease.breaking4 expects an overwrite against an RH lease to send RH->NONE lease break notification but complete the triggering create without waiting for the break ack.
Keep the lease in break-in-progress state until the client eventually acknowledges the downgrade, but do not hold the overwrite request behind that ack.
--- fs/smb/server/oplock.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
fs/smb/server/oplock.c +12 -5
@@ -927,7 +927,7 @@ static void __smb2_lease_break_noti(struct work_struct *wk)* * Return: 0 on success, otherwise error */ -static int smb2_lease_break_noti (struct oplock_info *opinfo) +static int smb2_lease_break_noti (struct oplock_info *opinfo,bool wait_ack) {struct ksmbd_conn *conn;struct ksmbd_work *work;
@@ -964,7 +964,8 @@ static int smb2_lease_break_noti(struct oplock_info *opinfo)if (opinfo->op_state ==OPLOCK_ACK_WAIT ) {INIT_WORK (&work->work, __smb2_lease_break_noti);ksmbd_queue_work (work); -wait_for_break_ack (opinfo); +if (wait_ack) +wait_for_break_ack (opinfo); }else {__smb2_lease_break_noti (&work->work);if (opinfo->o_lease->new_state ==SMB2_LEASE_NONE_LE ) {
@@ -1006,6 +1007,7 @@ static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level,if (brk_opinfo->is_lease) {struct lease *lease = brk_opinfo->o_lease;bool open_trunc = brk_opinfo->open_trunc; +bool wait_ack;if (in_work &&test_bit (0 , &brk_opinfo->pending_break)) {setup_async_work (in_work,NULL ,NULL );
@@ -1058,14 +1060,19 @@ static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level, }else atomic_dec (&brk_opinfo->breaking_cnt); - err =smb2_lease_break_noti (brk_opinfo); + wait_ack = !(open_trunc && + lease->state == (SMB2_LEASE_READ_CACHING_LE | +SMB2_LEASE_HANDLE_CACHING_LE )); + err =smb2_lease_break_noti (brk_opinfo, wait_ack);ksmbd_debug (OPLOCK ,"oplock granted = %d\n" , brk_opinfo->level);if (brk_opinfo->op_state ==OPLOCK_CLOSING ) err = -ENOENT ; -wait_lease_breaking (brk_opinfo); -if (!err &&lease_break_needed (brk_opinfo, req_op_level, open_trunc)) +if (wait_ack) +wait_lease_breaking (brk_opinfo); +if (wait_ack && !err && +lease_break_needed (brk_opinfo, req_op_level, open_trunc)) goto again;wake_up_oplock_break (brk_opinfo);
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 11/14 ksmbd: honor SMB2 v2 lease epochs
In reply to Namjae Jeonv2 lease responses should continue from the client supplied epoch. Initialize a new v2 lease from the requested epoch plus one so create responses match the epoch returned by Windows and expected by smbtorture.
For a single chained break sequence, increment the epoch only for the first break notification. Follow-up breaks such as RH->R and R->NONE in smb2.lease.v2_breaking3 reuse the same epoch.
Record when a waiter slept behind pending_break and let the later truncate/open overwrite break consume that marker to reuse the current epoch instead of assigning a new one.
Do not increment the epoch when a same-client, same-key create asks for the already granted RH state. The epoch changes only when the granted lease state changes.
--- fs/smb/server/oplock.c | 39 +++++++++++++++++++++++++++++---------- fs/smb/server/oplock.h | 1 + 2 files changed, 30 insertions(+), 10 deletions(-)
fs/smb/server/oplock.c +29 -10
@@ -163,8 +163,9 @@ static struct lease *alloc_lease(struct lease_ctx_info *lctx, lease->is_dir = lctx->is_dir;memcpy (lease->parent_lease_key, lctx->parent_lease_key,SMB2_LEASE_KEY_SIZE ); lease->version = lctx->version; - lease->epoch = lctx->version ==2 ?1 :0 ; + lease->epoch = lctx->version ==2 ?le16_to_cpu (lctx->epoch) +1 :0 ; lease->ci = ci; + lease->reuse_epoch = false; lease->l_lb =NULL ;INIT_LIST_HEAD (&lease->l_entry);INIT_LIST_HEAD (&lease->open_list);
@@ -645,9 +646,11 @@ static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci,if (lctx->req_state == (SMB2_LEASE_READ_CACHING_LE |SMB2_LEASE_HANDLE_CACHING_LE )) { - lease->epoch++; - lease->state = lctx->req_state; -lease_update_oplock_levels (lease); +if (lease->state != lctx->req_state) { + lease->epoch++; + lease->state = lctx->req_state; +lease_update_oplock_levels (lease); + } } }
@@ -694,6 +697,9 @@ static void wake_up_oplock_break(struct oplock_info *opinfo)static int oplock_break_pending (struct oplock_info *opinfo,int req_op_level) {while (test_and_set_bit (0 , &opinfo->pending_break)) { +if (opinfo->is_lease) + opinfo->o_lease->reuse_epoch = true; +wait_on_bit (&opinfo->pending_break,0 ,TASK_UNINTERRUPTIBLE );/* Not immediately break to none. */
@@ -927,7 +933,8 @@ static void __smb2_lease_break_noti(struct work_struct *wk)* * Return: 0 on success, otherwise error */ -static int smb2_lease_break_noti (struct oplock_info *opinfo,bool wait_ack) +static int smb2_lease_break_noti (struct oplock_info *opinfo,bool wait_ack, +bool inc_epoch) {struct ksmbd_conn *conn;struct ksmbd_work *work;
@@ -950,10 +957,13 @@ static int smb2_lease_break_noti(struct oplock_info *opinfo, bool wait_ack) br_info->curr_state = lease->state; br_info->new_state = lease->new_state; -if (lease->version ==2 ) - br_info->epoch =cpu_to_le16 (++lease->epoch); -else +if (lease->version ==2 ) { +if (inc_epoch) + lease->epoch++; + br_info->epoch =cpu_to_le16 (lease->epoch); + }else { br_info->epoch =0 ; + }memcpy (br_info->lease_key, lease->lease_key,SMB2_LEASE_KEY_SIZE ); work->request_buf = (char *)br_info;
@@ -1007,9 +1017,11 @@ static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level,if (brk_opinfo->is_lease) {struct lease *lease = brk_opinfo->o_lease;bool open_trunc = brk_opinfo->open_trunc; +bool was_pending =test_bit (0 , &brk_opinfo->pending_break);bool wait_ack; +bool inc_epoch = true; -if (in_work &&test_bit (0 , &brk_opinfo->pending_break)) { +if (in_work && was_pending) {setup_async_work (in_work,NULL ,NULL );smb2_send_interim_resp (in_work,STATUS_PENDING );release_async_work (in_work);
@@ -1019,6 +1031,8 @@ static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level, err =oplock_break_pending (brk_opinfo, req_op_level);if (err)return err <0 ? err :0 ; +if (was_pending) + open_trunc = brk_opinfo->open_trunc; again:atomic_inc (&brk_opinfo->breaking_cnt);
@@ -1063,7 +1077,12 @@ static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level, wait_ack = !(open_trunc && lease->state == (SMB2_LEASE_READ_CACHING_LE |SMB2_LEASE_HANDLE_CACHING_LE )); - err =smb2_lease_break_noti (brk_opinfo, wait_ack); +if (lease->reuse_epoch) { + inc_epoch = false; + lease->reuse_epoch = false; + } + err =smb2_lease_break_noti (brk_opinfo, wait_ack, inc_epoch); + inc_epoch = false;ksmbd_debug (OPLOCK ,"oplock granted = %d\n" , brk_opinfo->level);if (brk_opinfo->op_state ==OPLOCK_CLOSING )
fs/smb/server/oplock.h +1 -0
@@ -49,6 +49,7 @@ struct lease {int version;unsigned short epoch;bool is_dir; +bool reuse_epoch;struct ksmbd_inode *ci;struct lease_table *l_lb;struct list_head l_entry;
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 12/14 ksmbd: break RH leases before delete-on-close
In reply to Namjae JeonThe delete paths only marked the opened file delete pending or delete-on-close. When another client still held a read/handle lease, no lease break was sent before the delete state changed.
smb2.lease.unlink uses a create request with FILE_DELETE_ON_CLOSE and expects the second client's unlink to break the first client's RH lease to R with ACK_REQUIRED set. SetInfo(FileDispositionInformation) has the same lease-breaking requirement.
Break level-II/read-handle leases before setting delete pending or delete-on-close so clients are notified before the file is removed.
--- fs/smb/server/smb2pdu.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
fs/smb/server/smb2pdu.c +7 -3
@@ -3653,8 +3653,10 @@ int smb2_open(struct ksmbd_work *work) goto err_out1; } -if (req->CreateOptions &FILE_DELETE_ON_CLOSE_LE ) +if (req->CreateOptions &FILE_DELETE_ON_CLOSE_LE ) { +smb_break_all_levII_oplock (work, fp,0 );ksmbd_fd_set_delete_on_close (fp, file_info); + }if (need_truncate) { rc =smb2_create_truncate (&fp->filp->f_path);
@@ -6584,7 +6586,8 @@ static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,return smb2_rename (work, fp, rename_info, work->conn->local_nls); } -static int set_file_disposition_info (struct ksmbd_file *fp, +static int set_file_disposition_info (struct ksmbd_work *work, +struct ksmbd_file *fp,struct smb2_file_disposition_info *file_info) {struct inode *inode;
@@ -6599,6 +6602,7 @@ static int set_file_disposition_info(struct ksmbd_file *fp,if (S_ISDIR (inode->i_mode) &&ksmbd_vfs_empty_dir (fp) == -ENOTEMPTY )return -EBUSY ; +smb_break_all_levII_oplock (work, fp,0 );ksmbd_set_inode_pending_delete (fp); }else {ksmbd_clear_inode_pending_delete (fp);
@@ -6725,7 +6729,7 @@ static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,if (buf_len <sizeof (struct smb2_file_disposition_info ))return -EMSGSIZE ; -return set_file_disposition_info (fp, +return set_file_disposition_info (work, fp, (struct smb2_file_disposition_info *)buffer); }case FILE_FULL_EA_INFORMATION :
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 13/14 ksmbd: route v2 lease breaks on the client lease channel
In reply to Namjae Jeonv2 leases are scoped by ClientGuid. When the same client uses multiple connections, smbtorture expects lease break notifications to be sent on the connection associated with the client lease table, not necessarily on the connection that owns the individual open being broken.
Keep a referenced connection in the lease table and use it for v2 lease break notifications while it is still active. Fall back to the open's connection if the table connection is being released.
--- fs/smb/server/oplock.c | 17 +++++++++++++++-- fs/smb/server/oplock.h | 1 + 2 files changed, 16 insertions(+), 2 deletions(-)
fs/smb/server/oplock.c +15 -2
@@ -141,11 +141,21 @@ static struct lease_table *alloc_lease_table(struct oplock_info *opinfo)memcpy (lb->client_guid, opinfo->conn->ClientGUID,SMB2_CLIENT_GUID_SIZE ); + lb->conn =ksmbd_conn_get (opinfo->conn);INIT_LIST_HEAD (&lb->lease_list);spin_lock_init (&lb->lb_lock);return lb; } +static void free_lease_table (struct lease_table *lb) +{ +if (!lb) +return ; + +ksmbd_conn_put (lb->conn); +kfree (lb); +} +static struct lease *alloc_lease (struct lease_ctx_info *lctx,struct ksmbd_inode *ci) {
@@ -942,6 +952,9 @@ static int smb2_lease_break_noti(struct oplock_info *opinfo, bool wait_ack,struct lease *lease = opinfo->o_lease; conn =READ_ONCE (opinfo->conn); +if (lease->version ==2 && lease->l_lb && lease->l_lb->conn && + !ksmbd_conn_releasing (lease->l_lb->conn)) + conn = lease->l_lb->conn;if (!conn)return 0 ;
@@ -1134,7 +1147,7 @@ void destroy_lease_table(struct ksmbd_conn *conn)list_for_each_entry_safe (lease, ltmp, &lb->lease_list, l_entry)lease_del_table (lease);list_del (&lb->l_entry); -kfree (lb); +free_lease_table (lb); }write_unlock (&lease_list_lock); }
@@ -1193,7 +1206,7 @@ static void add_lease_global_list(struct lease *lease, struct ksmbd_conn *conn, SMB2_CLIENT_GUID_SIZE)) {lease_add_table (lease, lb);write_unlock (&lease_list_lock); -kfree (new_lb); +free_lease_table (new_lb);return ; } }
fs/smb/server/oplock.h +1 -0
@@ -34,6 +34,7 @@ struct lease_ctx_info {struct lease_table {char client_guid[SMB2_CLIENT_GUID_SIZE ]; +struct ksmbd_conn *conn;struct list_head lease_list;struct list_head l_entry;spinlock_t lb_lock;
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 14/14 ksmbd: keep common response iovecs in the work item
In reply to Namjae JeonMost SMB responses need no more than four kvec entries, but every work item currently allocates a separate four-entry array and frees it after the response is sent.
Embed the common array in struct ksmbd_work and allocate a larger array only when a response exceeds the inline capacity. This removes one allocation and one free from the common request path while preserving support for larger compound and read responses.
--- fs/smb/server/ksmbd_work.c | 58 ++++++++++++++++++++++++-------------- fs/smb/server/ksmbd_work.h | 3 ++ 2 files changed, 40 insertions(+), 21 deletions(-)
fs/smb/server/ksmbd_work.c +37 -21
@@ -16,6 +16,36 @@static struct kmem_cache *work_cache;static struct workqueue_struct *ksmbd_wq; +static int ksmbd_reserve_iov (struct ksmbd_work *work,int need_iov_cnt) +{ +struct kvec *new; +int new_alloc_cnt = work->iov_alloc_cnt; + +if (work->iov_alloc_cnt >= work->iov_cnt + need_iov_cnt) +return 0 ; + +do { + new_alloc_cnt +=KSMBD_WORK_INLINE_IOVS ; + }while (new_alloc_cnt < work->iov_cnt + need_iov_cnt); + +if (work->iov == work->iov_inline) { + new =kcalloc (new_alloc_cnt,sizeof (*new),KSMBD_DEFAULT_GFP ); +if (!new) +return -ENOMEM ; + +memcpy (new, work->iov_inline,sizeof (work->iov_inline)); + }else { + new =krealloc (work->iov,sizeof (*new) * new_alloc_cnt, +KSMBD_DEFAULT_GFP | __GFP_ZERO); +if (!new) +return -ENOMEM ; + } + + work->iov = new; + work->iov_alloc_cnt = new_alloc_cnt; +return 0 ; +} +struct ksmbd_work *ksmbd_alloc_work_struct (void ) {struct ksmbd_work *work =kmem_cache_zalloc (work_cache,KSMBD_DEFAULT_GFP );
@@ -27,13 +57,8 @@ struct ksmbd_work *ksmbd_alloc_work_struct(void)INIT_LIST_HEAD (&work->async_request_entry);INIT_LIST_HEAD (&work->fp_entry);INIT_LIST_HEAD (&work->aux_read_list); - work->iov_alloc_cnt =4 ; - work->iov = kzalloc_objs(struct kvec, work->iov_alloc_cnt, -KSMBD_DEFAULT_GFP ); -if (!work->iov) { -kmem_cache_free (work_cache, work); - work =NULL ; - } + work->iov_alloc_cnt =ARRAY_SIZE (work->iov_inline); + work->iov = work->iov_inline; }return work; }
@@ -55,7 +80,8 @@ void ksmbd_free_work_struct(struct ksmbd_work *work)kfree (work->tr_buf);kvfree (work->compress_buf);kvfree (work->request_buf); -kfree (work->iov); +if (work->iov != work->iov_inline) +kfree (work->iov);if (work->async_id)ksmbd_release_id (&work->conn->async_ida, work->async_id);
@@ -117,19 +143,9 @@ static int __ksmbd_iov_pin_rsp(struct ksmbd_work *work, void *ib, int len,return -ENOMEM ; } -if (work->iov_alloc_cnt < work->iov_cnt + need_iov_cnt) { -struct kvec *new; - - work->iov_alloc_cnt +=4 ; - new =krealloc (work->iov, -sizeof (struct kvec ) * work->iov_alloc_cnt, -KSMBD_DEFAULT_GFP | __GFP_ZERO); -if (!new) { -kfree (ar); - work->iov_alloc_cnt -=4 ; -return -ENOMEM ; - } - work->iov = new; +if (ksmbd_reserve_iov (work, need_iov_cnt)) { +kfree (ar); +return -ENOMEM ; }/* Plus rfc_length size on first iov */
fs/smb/server/ksmbd_work.h +3 -0
@@ -13,6 +13,8 @@ struct ksmbd_conn;struct ksmbd_session ;struct ksmbd_tree_connect ; +#define KSMBD_WORK_INLINE_IOVS 4 +enum {KSMBD_WORK_ACTIVE =0 ,KSMBD_WORK_CANCELLED ,
@@ -42,6 +44,7 @@ struct ksmbd_work {int iov_alloc_cnt;int iov_cnt;int iov_idx; +struct kvec iov_inline[KSMBD_WORK_INLINE_IOVS ];/* Next cmd hdr in compound req buf*/ int next_smb2_rcv_hdr_off;
Signature · 1 line
2.25.1