Add comments to all functions (to help me understand it better).
[kai/samba.git] / source3 / locking / share_mode_lock.c
index 1fb96998d704c0c62371573ca63ecb4557b329b1..3b353d6793f365c234f399f0599e551e88320af4 100644 (file)
@@ -190,6 +190,10 @@ fail:
        return NULL;
 }
 
+/*******************************************************************
+ Create a storable data blob from a modified share_mode_data struct.
+********************************************************************/
+
 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
 {
        DATA_BLOB blob;
@@ -214,6 +218,10 @@ static TDB_DATA unparse_share_modes(struct share_mode_data *d)
        return make_tdb_data(blob.data, blob.length);
 }
 
+/*******************************************************************
+ If modified, store the share_mode_data back into the database.
+********************************************************************/
+
 static int share_mode_data_destructor(struct share_mode_data *d)
 {
        NTSTATUS status;
@@ -266,6 +274,11 @@ static int share_mode_data_destructor(struct share_mode_data *d)
        return 0;
 }
 
+/*******************************************************************
+ Allocate a new share_mode_data struct, mark it unmodified.
+ fresh is set to note that currently there is no database entry.
+********************************************************************/
+
 static struct share_mode_data *fresh_share_mode_lock(
        TALLOC_CTX *mem_ctx, const char *servicepath,
        const struct smb_filename *smb_fname,
@@ -306,11 +319,15 @@ fail:
        return NULL;
 }
 
-struct share_mode_lock *get_share_mode_lock_fresh(TALLOC_CTX *mem_ctx,
-                                                 const struct file_id id,
-                                                 const char *servicepath,
-                                                 const struct smb_filename *smb_fname,
-                                                 const struct timespec *old_write_time)
+/*******************************************************************
+ Either fetch a share mode from the database, or allocate a fresh
+ one if the record doesn't exist.
+********************************************************************/
+
+static struct share_mode_lock *get_share_mode_lock_internal(
+       TALLOC_CTX *mem_ctx, const struct file_id id,
+       const char *servicepath, const struct smb_filename *smb_fname,
+       const struct timespec *old_write_time)
 {
        struct share_mode_lock *lck;
        struct share_mode_data *d;
@@ -353,6 +370,68 @@ struct share_mode_lock *get_share_mode_lock_fresh(TALLOC_CTX *mem_ctx,
        return lck;
 }
 
+/*
+ * We can only ever have one share mode locked. Users of
+ * get_share_mode_lock never see this, it will be refcounted by
+ * talloc_reference.
+ */
+static struct share_mode_lock *the_lock;
+
+static int the_lock_destructor(struct share_mode_lock *l)
+{
+       the_lock = NULL;
+       return 0;
+}
+
+/*******************************************************************
+ Get a share_mode_lock, Reference counted to allow nexted calls.
+********************************************************************/
+
+struct share_mode_lock *get_share_mode_lock_fresh(
+       TALLOC_CTX *mem_ctx,
+       const struct file_id id,
+       const char *servicepath,
+       const struct smb_filename *smb_fname,
+       const struct timespec *old_write_time)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       struct share_mode_lock *lck;
+
+       if (the_lock == NULL) {
+               the_lock = get_share_mode_lock_internal(
+                       frame, id, servicepath, smb_fname, old_write_time);
+               if (the_lock == NULL) {
+                       goto fail;
+               }
+               talloc_set_destructor(the_lock, the_lock_destructor);
+       }
+       if (!file_id_equal(&the_lock->data->id, &id)) {
+               DEBUG(1, ("Can not lock two share modes simultaneously\n"));
+               goto fail;
+       }
+       lck = talloc(mem_ctx, struct share_mode_lock);
+       if (lck == NULL) {
+               DEBUG(1, ("talloc failed\n"));
+               goto fail;
+       }
+       if (talloc_reference(lck, the_lock) == NULL) {
+               DEBUG(1, ("talloc_reference failed\n"));
+               goto fail;
+       }
+       lck->data = the_lock->data;
+       TALLOC_FREE(frame);
+       return lck;
+fail:
+       TALLOC_FREE(frame);
+       return NULL;
+}
+
+/*******************************************************************
+ Get a share_mode_lock without locking the database or reference
+ counting. Used by smbstatus to display existing share modes.
+********************************************************************/
+
 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
                                                  const struct file_id id)
 {