@@ -324,25 +324,28 @@ static struct libos_handle* __detach_fd_handle(struct libos_fd_handle* fd, int*
324
324
}
325
325
326
326
static int clear_posix_locks (struct libos_handle * handle ) {
327
- if (handle && handle -> dentry ) {
328
- /* Clear file (POSIX) locks for a file. We are required to do that every time a FD is
329
- * closed, even if the process holds other handles for that file, or duplicated FDs for the
330
- * same handle. */
331
- struct libos_file_lock file_lock = {
332
- .family = FILE_LOCK_POSIX ,
333
- .type = F_UNLCK ,
334
- .start = 0 ,
335
- .end = FS_LOCK_EOF ,
336
- .pid = g_process .pid ,
337
- };
338
- int ret = file_lock_set (handle -> dentry , & file_lock , /*block=*/ false);
339
- if (ret < 0 ) {
340
- log_warning ("error releasing locks: %s" , unix_strerror (ret ));
341
- return ret ;
327
+ int ret = 0 ;
328
+ if (handle ) {
329
+ lock (& handle -> lock );
330
+ if (handle -> dentry ) {
331
+ /* Clear file (POSIX) locks for a file. We are required to do that every time a FD is
332
+ * closed, even if the process holds other handles for that file, or duplicated FDs for
333
+ * the same handle. */
334
+ struct libos_file_lock file_lock = {
335
+ .family = FILE_LOCK_POSIX ,
336
+ .type = F_UNLCK ,
337
+ .start = 0 ,
338
+ .end = FS_LOCK_EOF ,
339
+ .pid = g_process .pid ,
340
+ };
341
+ ret = file_lock_set (handle -> dentry , & file_lock , /*block=*/ false);
342
+ if (ret < 0 ) {
343
+ log_warning ("error releasing locks: %s" , unix_strerror (ret ));
344
+ }
342
345
}
346
+ unlock (& handle -> lock );
343
347
}
344
-
345
- return 0 ;
348
+ return ret ;
346
349
}
347
350
348
351
struct libos_handle * detach_fd_handle (uint32_t fd , int * flags ,
@@ -512,20 +515,24 @@ static void destroy_handle(struct libos_handle* hdl) {
512
515
513
516
static int clear_flock_locks (struct libos_handle * hdl ) {
514
517
/* Clear flock (BSD) locks for a file. We are required to do that when the handle is closed. */
515
- if (hdl && hdl -> dentry && hdl -> created_by_process ) {
516
- assert (hdl -> ref_count == 0 );
517
- struct libos_file_lock file_lock = {
518
- .family = FILE_LOCK_FLOCK ,
519
- .type = F_UNLCK ,
520
- .handle_id = hdl -> id ,
521
- };
522
- int ret = file_lock_set (hdl -> dentry , & file_lock , /*block=*/ false);
523
- if (ret < 0 ) {
524
- log_warning ("error releasing locks: %s" , unix_strerror (ret ));
525
- return ret ;
518
+ int ret = 0 ;
519
+ if (hdl ) {
520
+ lock (& hdl -> lock );
521
+ if (hdl -> dentry && hdl -> created_by_process ) {
522
+ assert (hdl -> ref_count == 0 );
523
+ struct libos_file_lock file_lock = {
524
+ .family = FILE_LOCK_FLOCK ,
525
+ .type = F_UNLCK ,
526
+ .handle_id = hdl -> id ,
527
+ };
528
+ int ret = file_lock_set (hdl -> dentry , & file_lock , /*block=*/ false);
529
+ if (ret < 0 ) {
530
+ log_warning ("error releasing locks: %s" , unix_strerror (ret ));
531
+ }
526
532
}
533
+ unlock (& hdl -> lock );
527
534
}
528
- return 0 ;
535
+ return ret ;
529
536
}
530
537
531
538
void put_handle (struct libos_handle * hdl ) {
@@ -549,7 +556,7 @@ void put_handle(struct libos_handle* hdl) {
549
556
hdl -> pal_handle = NULL ;
550
557
}
551
558
552
- if (hdl -> dentry ) {
559
+ if (hdl -> dentry ) { /* no locking needed as no other reference exists */
553
560
(void )clear_flock_locks (hdl );
554
561
put_dentry (hdl -> dentry );
555
562
}
0 commit comments