瀏覽代碼

[LibOS] Remove `opened` reference counter from `struct shim_handle`

Inside `struct shim_handle` `opened` reference counter is used incorrectly.
Additionally at this moment it guards the same resource (handle) as `ref_counter`,
making it obsolete. This patch removes `opened` counter and moves `close_handle`
logic into `put_handle`.
borysp 4 年之前
父節點
當前提交
004c2fe9a4

+ 0 - 3
LibOS/shim/include/shim_handle.h

@@ -374,15 +374,12 @@ struct shim_handle {
     int                 flags;
     int                 acc_mode;
     IDTYPE              owner;
-    REFTYPE             opened;
     struct shim_lock    lock;
 };
 
 /* allocating / manage handle */
 struct shim_handle * get_new_handle (void);
 void flush_handle (struct shim_handle * hdl);
-void open_handle (struct shim_handle * hdl);
-void close_handle (struct shim_handle * hdl);
 void get_handle (struct shim_handle * hdl);
 void put_handle (struct shim_handle * hdl);
 

+ 27 - 56
LibOS/shim/src/bookkeep/shim_handle.c

@@ -311,7 +311,7 @@ static int __set_new_fd_handle(struct shim_fd_handle ** fdhdl, FDTYPE fd,
 
     new_handle->vfd    = fd;
     new_handle->flags  = flags;
-    open_handle(hdl);
+    get_handle(hdl);
     new_handle->handle = hdl;
     return 0;
 }
@@ -449,32 +449,40 @@ const char * __handle_name (struct shim_handle * hdl)
     return "(unknown)";
 }
 
-void open_handle (struct shim_handle * hdl)
+void get_handle (struct shim_handle * hdl)
 {
-    get_handle(hdl);
-
 #ifdef DEBUG_REF
-    int opened = REF_INC(hdl->opened);
+    int ref_count = REF_INC(hdl->ref_count);
 
-    debug("open handle %p(%s) (opened = %d)\n", hdl, __handle_name(hdl),
-          opened);
+    debug("get handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl),
+          ref_count);
 #else
-    REF_INC(hdl->opened);
+    REF_INC(hdl->ref_count);
 #endif
 }
 
+static void destroy_handle (struct shim_handle * hdl)
+{
+    destroy_lock(&hdl->lock);
+
+    if (memory_migrated(hdl))
+        memset(hdl, 0, sizeof(struct shim_handle));
+    else
+        free_mem_obj_to_mgr(handle_mgr, hdl);
+}
+
 extern int delete_from_epoll_handles (struct shim_handle * handle);
 
-void close_handle (struct shim_handle * hdl)
+void put_handle (struct shim_handle * hdl)
 {
-    int opened = REF_DEC(hdl->opened);
+    int ref_count = REF_DEC(hdl->ref_count);
 
 #ifdef DEBUG_REF
-    debug("close handle %p(%s) (opened = %d)\n", hdl, __handle_name(hdl),
-          opened);
+    debug("put handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl),
+          ref_count);
 #endif
 
-    if (!opened) {
+    if (!ref_count) {
         if (hdl->type == TYPE_DIR) {
             struct shim_dir_handle * dir = &hdl->dir_info;
 
@@ -502,43 +510,7 @@ void close_handle (struct shim_handle * hdl)
         }
 
         delete_from_epoll_handles(hdl);
-    }
-
-    put_handle(hdl);
-}
 
-void get_handle (struct shim_handle * hdl)
-{
-#ifdef DEBUG_REF
-    int ref_count = REF_INC(hdl->ref_count);
-
-    debug("get handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl),
-          ref_count);
-#else
-    REF_INC(hdl->ref_count);
-#endif
-}
-
-static void destroy_handle (struct shim_handle * hdl)
-{
-    destroy_lock(&hdl->lock);
-
-    if (memory_migrated(hdl))
-        memset(hdl, 0, sizeof(struct shim_handle));
-    else
-        free_mem_obj_to_mgr(handle_mgr, hdl);
-}
-
-void put_handle (struct shim_handle * hdl)
-{
-    int ref_count = REF_DEC(hdl->ref_count);
-
-#ifdef DEBUG_REF
-    debug("put handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl),
-          ref_count);
-#endif
-
-    if (!ref_count) {
         if (hdl->fs && hdl->fs->fs_ops &&
             hdl->fs->fs_ops->hput)
             hdl->fs->fs_ops->hput(hdl);
@@ -592,7 +564,7 @@ void dup_fd_handle (struct shim_handle_map * map,
     lock(&map->lock);
 
     if (old->vfd != FD_NULL) {
-        open_handle(old->handle);
+        get_handle(old->handle);
         replaced = new->handle;
         new->handle = old->handle;
     }
@@ -600,7 +572,7 @@ void dup_fd_handle (struct shim_handle_map * map,
     unlock(&map->lock);
 
     if (replaced)
-        close_handle(replaced);
+        put_handle(replaced);
 }
 
 static struct shim_handle_map * get_new_handle_map (FDTYPE size)
@@ -672,12 +644,12 @@ int dup_handle_map (struct shim_handle_map ** new,
         if (HANDLE_ALLOCATED(fd_old)) {
             /* first, get the handle to prevent it from being deleted */
             struct shim_handle * hdl = fd_old->handle;
-            open_handle(hdl);
+            get_handle(hdl);
 
             fd_new = malloc(sizeof(struct shim_fd_handle));
             if (!fd_new) {
                 for (int j = 0; j < i; j++) {
-                    close_handle(new_map->map[j]->handle);
+                    put_handle(new_map->map[j]->handle);
                     free(new_map->map[j]);
                 }
                 unlock(&old_map->lock);
@@ -721,7 +693,7 @@ void put_handle_map (struct shim_handle_map * map)
                 struct shim_handle * handle = map->map[i]->handle;
 
                 if (handle)
-                    close_handle(handle);
+                    put_handle(handle);
             }
 
             free(map->map[i]);
@@ -804,7 +776,6 @@ BEGIN_CP_FUNC(handle)
             fs->fs_ops->checkout(new_hdl);
 
         new_hdl->dentry = NULL;
-        REF_SET(new_hdl->opened, 0);
         REF_SET(new_hdl->ref_count, 0);
         clear_lock(&new_hdl->lock);
 
@@ -962,7 +933,7 @@ BEGIN_RS_FUNC(handle_map)
                 CP_REBASE(handle_map->map[i]->handle);
                 struct shim_handle * hdl = handle_map->map[i]->handle;
                 assert(hdl);
-                open_handle(hdl);
+                get_handle(hdl);
                 DEBUG_RS("[%d]%s", i, qstrempty(&hdl->uri) ? hdl->fs_type :
                                       qstrgetstr(&hdl->uri));
             }

+ 2 - 2
LibOS/shim/src/shim_checkpoint.c

@@ -744,7 +744,7 @@ int restore_from_file (const char * filename, struct newproc_cp_header * hdr,
     }
 
     struct shim_mount * fs = file->fs;
-    open_handle(file);
+    get_handle(file);
     debug("restore %s\n", filename);
 
     struct cp_header cphdr;
@@ -764,7 +764,7 @@ int restore_from_file (const char * filename, struct newproc_cp_header * hdr,
     migrated_memory_start = cpaddr;
     migrated_memory_end = cpaddr + hdr->hdr.size;
 out:
-    close_handle(file);
+    put_handle(file);
     return ret;
 }
 

+ 2 - 2
LibOS/shim/src/sys/shim_dup.c

@@ -57,7 +57,7 @@ int shim_do_dup2 (int oldfd, int newfd)
     struct shim_handle * new_hdl = detach_fd_handle(newfd, NULL, handle_map);
 
     if (new_hdl)
-        close_handle(new_hdl);
+        put_handle(new_hdl);
 
     int vfd = set_new_fd_handle_by_fd(newfd, hdl, 0, handle_map);
     put_handle(hdl);
@@ -74,7 +74,7 @@ int shim_do_dup3 (int oldfd, int newfd, int flags)
     struct shim_handle * new_hdl = detach_fd_handle(newfd, NULL, handle_map);
 
     if (new_hdl)
-        close_handle(new_hdl);
+        put_handle(new_hdl);
 
     int vfd = set_new_fd_handle_by_fd(newfd, hdl, flags, handle_map);
     put_handle(hdl);

+ 1 - 1
LibOS/shim/src/sys/shim_exec.c

@@ -43,7 +43,7 @@ static int close_on_exec (struct shim_fd_handle * fd_hdl,
 {
     if (fd_hdl->flags & FD_CLOEXEC) {
         struct shim_handle * hdl = __detach_fd_handle(fd_hdl, NULL, map);
-        close_handle(hdl);
+        put_handle(hdl);
     }
     return 0;
 }

+ 0 - 11
LibOS/shim/src/sys/shim_fs.c

@@ -616,8 +616,6 @@ static int do_rename (struct shim_dentry * old_dent,
     }
 
     struct shim_handle * old_hdl = NULL, * new_hdl = NULL;
-    bool old_opened = false;
-    bool new_opened = false;
 
     if (!(old_hdl = get_new_handle())) {
         ret = -ENOMEM;
@@ -627,8 +625,6 @@ static int do_rename (struct shim_dentry * old_dent,
     if ((ret = dentry_open(old_hdl, old_dent, O_RDONLY)) < 0)
         goto out_hdl;
 
-    old_opened = true;
-
     if (!(new_hdl = get_new_handle())) {
         ret = -ENOMEM;
         goto out_hdl;
@@ -637,7 +633,6 @@ static int do_rename (struct shim_dentry * old_dent,
     if ((ret = dentry_open(new_hdl, new_dent, O_WRONLY|O_CREAT)) < 0)
         goto out_hdl;
 
-    new_opened = true;
     off_t old_offset = 0, new_offset = 0;
 
     if ((ret = handle_copy(old_hdl, &old_offset,
@@ -664,15 +659,9 @@ static int do_rename (struct shim_dentry * old_dent,
 
 out_hdl:
     if (old_hdl) {
-        if (old_opened)
-            close_handle(old_hdl);
-        else
             put_handle(old_hdl);
     }
     if (new_hdl) {
-        if (new_opened)
-            close_handle(new_hdl);
-        else
             put_handle(new_hdl);
     }
 out:

+ 3 - 3
LibOS/shim/src/sys/shim_migrate.c

@@ -98,7 +98,7 @@ int create_checkpoint (const char * cpdir, IDTYPE * sid)
                           O_CREAT|O_EXCL|O_RDWR, 0600, NULL)) < 0)
         goto err;
 
-    open_handle(cpsession->cpfile);
+    get_handle(cpsession->cpfile);
     MASTER_LOCK();
 
     struct cp_session * s;
@@ -131,7 +131,7 @@ err_locked:
     MASTER_UNLOCK();
 err:
     if (cpsession->cpfile)
-        close_handle(cpsession->cpfile);
+        put_handle(cpsession->cpfile);
 
     DkObjectClose(cpsession->finish_event);
     free(cpsession);
@@ -260,7 +260,7 @@ static int finish_checkpoint (struct cp_session * cpsession)
 
     DkStreamUnmap((void *) cpstore->base, cpstore->bound);
 
-    close_handle(cpstore->cp_file);
+    put_handle(cpstore->cp_file);
     return 0;
 }
 

+ 1 - 1
LibOS/shim/src/sys/shim_open.c

@@ -165,7 +165,7 @@ int shim_do_close (int fd)
     if (!handle)
         return -EBADF;
 
-    close_handle(handle);
+    put_handle(handle);
     return 0;
 }
 

+ 4 - 4
LibOS/shim/src/sys/shim_pipe.c

@@ -114,12 +114,12 @@ int shim_do_pipe2 (int * filedes, int flags)
         if (vfd1 >= 0) {
             struct shim_handle * tmp = detach_fd_handle(vfd1, NULL, NULL);
             if (tmp)
-                close_handle(tmp);
+                put_handle(tmp);
         }
         if (vfd2 >= 0) {
             struct shim_handle * tmp = detach_fd_handle(vfd2, NULL, NULL);
             if (tmp)
-                close_handle(tmp);
+                put_handle(tmp);
         }
         goto out;
     }
@@ -196,12 +196,12 @@ int shim_do_socketpair (int domain, int type, int protocol, int * sv)
         if (vfd1 >= 0) {
             struct shim_handle * tmp = detach_fd_handle(vfd1, NULL, NULL);
             if (tmp)
-                close_handle(tmp);
+                put_handle(tmp);
         }
         if (vfd2 >= 0) {
             struct shim_handle * tmp = detach_fd_handle(vfd2, NULL, NULL);
             if (tmp)
-                close_handle(tmp);
+                put_handle(tmp);
         }
         goto out;
     }