Lines Matching refs:fd

79 static int z_fd_ref(int fd)  in z_fd_ref()  argument
81 return atomic_inc(&fdtable[fd].refcount) + 1; in z_fd_ref()
84 static int z_fd_unref(int fd) in z_fd_unref() argument
94 old_rc = atomic_get(&fdtable[fd].refcount); in z_fd_unref()
98 } while (!atomic_cas(&fdtable[fd].refcount, old_rc, old_rc - 1)); in z_fd_unref()
104 fdtable[fd].obj = NULL; in z_fd_unref()
105 fdtable[fd].vtable = NULL; in z_fd_unref()
112 int fd; in _find_fd_entry() local
114 for (fd = 0; fd < ARRAY_SIZE(fdtable); fd++) { in _find_fd_entry()
115 if (!atomic_get(&fdtable[fd].refcount)) { in _find_fd_entry()
116 return fd; in _find_fd_entry()
124 static int _check_fd(int fd) in _check_fd() argument
126 if ((fd < 0) || (fd >= ARRAY_SIZE(fdtable))) { in _check_fd()
131 fd = k_array_index_sanitize(fd, ARRAY_SIZE(fdtable)); in _check_fd()
133 if (!atomic_get(&fdtable[fd].refcount)) { in _check_fd()
142 bool fdtable_fd_is_initialized(int fd) in fdtable_fd_is_initialized() argument
147 if (fd < 0 || fd >= ARRAY_SIZE(fdtable)) { in fdtable_fd_is_initialized()
151 ref_lock = (struct k_mutex)Z_MUTEX_INITIALIZER(fdtable[fd].lock); in fdtable_fd_is_initialized()
152 if (memcmp(&ref_lock, &fdtable[fd].lock, sizeof(ref_lock)) != 0) { in fdtable_fd_is_initialized()
156 ref_cond = (struct k_condvar)Z_CONDVAR_INITIALIZER(fdtable[fd].cond); in fdtable_fd_is_initialized()
157 if (memcmp(&ref_cond, &fdtable[fd].cond, sizeof(ref_cond)) != 0) { in fdtable_fd_is_initialized()
165 void *zvfs_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err) in zvfs_get_fd_obj() argument
169 if (_check_fd(fd) < 0) { in zvfs_get_fd_obj()
173 entry = &fdtable[fd]; in zvfs_get_fd_obj()
185 int fd; in z_get_fd_by_obj_and_vtable() local
187 for (fd = 0; fd < ARRAY_SIZE(fdtable); fd++) { in z_get_fd_by_obj_and_vtable()
188 if (fdtable[fd].obj == obj && fdtable[fd].vtable == vtable) { in z_get_fd_by_obj_and_vtable()
189 return fd; in z_get_fd_by_obj_and_vtable()
200 int fd; in zvfs_get_obj_lock_and_cond() local
203 fd = z_get_fd_by_obj_and_vtable(obj, vtable); in zvfs_get_obj_lock_and_cond()
204 if (_check_fd(fd) < 0) { in zvfs_get_obj_lock_and_cond()
208 entry = &fdtable[fd]; in zvfs_get_obj_lock_and_cond()
221 void *zvfs_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable, in zvfs_get_fd_obj_and_vtable() argument
226 if (_check_fd(fd) < 0) { in zvfs_get_fd_obj_and_vtable()
230 entry = &fdtable[fd]; in zvfs_get_fd_obj_and_vtable()
242 int fd; in zvfs_reserve_fd() local
246 fd = _find_fd_entry(); in zvfs_reserve_fd()
247 if (fd >= 0) { in zvfs_reserve_fd()
249 (void)z_fd_ref(fd); in zvfs_reserve_fd()
250 fdtable[fd].obj = NULL; in zvfs_reserve_fd()
251 fdtable[fd].vtable = NULL; in zvfs_reserve_fd()
252 k_mutex_init(&fdtable[fd].lock); in zvfs_reserve_fd()
253 k_condvar_init(&fdtable[fd].cond); in zvfs_reserve_fd()
258 return fd; in zvfs_reserve_fd()
261 void zvfs_finalize_typed_fd(int fd, void *obj, const struct fd_op_vtable *vtable, uint32_t mode) in zvfs_finalize_typed_fd() argument
274 fdtable[fd].obj = obj; in zvfs_finalize_typed_fd()
275 fdtable[fd].vtable = vtable; in zvfs_finalize_typed_fd()
276 fdtable[fd].mode = mode; in zvfs_finalize_typed_fd()
284 &fdtable[fd].lock); in zvfs_finalize_typed_fd()
288 void zvfs_free_fd(int fd) in zvfs_free_fd() argument
291 (void)z_fd_unref(fd); in zvfs_free_fd()
296 int fd; in zvfs_alloc_fd() local
298 fd = zvfs_reserve_fd(); in zvfs_alloc_fd()
299 if (fd >= 0) { in zvfs_alloc_fd()
300 zvfs_finalize_fd(fd, obj, vtable); in zvfs_alloc_fd()
303 return fd; in zvfs_alloc_fd()
316 static ssize_t zvfs_rw(int fd, void *buf, size_t sz, bool is_write, const size_t *from_offset) in zvfs_rw() argument
322 if (_check_fd(fd) < 0) { in zvfs_rw()
326 (void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER); in zvfs_rw()
328 prw = supports_pread_pwrite(fdtable[fd].mode); in zvfs_rw()
340 off = (from_offset == NULL) ? &fdtable[fd].offset : from_offset; in zvfs_rw()
343 if (fdtable[fd].vtable->write_offs == NULL) { in zvfs_rw()
347 res = fdtable[fd].vtable->write_offs(fdtable[fd].obj, buf, sz, *off); in zvfs_rw()
350 if (fdtable[fd].vtable->read_offs == NULL) { in zvfs_rw()
354 res = fdtable[fd].vtable->read_offs(fdtable[fd].obj, buf, sz, *off); in zvfs_rw()
362 fdtable[fd].offset += res; in zvfs_rw()
366 k_mutex_unlock(&fdtable[fd].lock); in zvfs_rw()
371 ssize_t zvfs_read(int fd, void *buf, size_t sz, const size_t *from_offset) in zvfs_read() argument
373 return zvfs_rw(fd, buf, sz, false, from_offset); in zvfs_read()
376 ssize_t zvfs_write(int fd, const void *buf, size_t sz, const size_t *from_offset) in zvfs_write() argument
378 return zvfs_rw(fd, (void *)buf, sz, true, from_offset); in zvfs_write()
381 int zvfs_close(int fd) in zvfs_close() argument
385 if (_check_fd(fd) < 0) { in zvfs_close()
389 (void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER); in zvfs_close()
390 if (fdtable[fd].vtable->close != NULL) { in zvfs_close()
392 if (fdtable[fd].mode & ZVFS_MODE_IFSOCK) { in zvfs_close()
396 res = fdtable[fd].vtable->close2(fdtable[fd].obj, fd); in zvfs_close()
398 res = fdtable[fd].vtable->close(fdtable[fd].obj); in zvfs_close()
401 k_mutex_unlock(&fdtable[fd].lock); in zvfs_close()
403 zvfs_free_fd(fd); in zvfs_close()
408 FILE *zvfs_fdopen(int fd, const char *mode) in zvfs_fdopen() argument
412 if (_check_fd(fd) < 0) { in zvfs_fdopen()
416 return (FILE *)&fdtable[fd]; in zvfs_fdopen()
429 int zvfs_fstat(int fd, struct stat *buf) in zvfs_fstat() argument
431 if (_check_fd(fd) < 0) { in zvfs_fstat()
435 return zvfs_fdtable_call_ioctl(fdtable[fd].vtable, fdtable[fd].obj, ZFD_IOCTL_STAT, buf); in zvfs_fstat()
438 int zvfs_fsync(int fd) in zvfs_fsync() argument
440 if (_check_fd(fd) < 0) { in zvfs_fsync()
444 return zvfs_fdtable_call_ioctl(fdtable[fd].vtable, fdtable[fd].obj, ZFD_IOCTL_FSYNC); in zvfs_fsync()
447 static inline off_t zvfs_lseek_wrap(int fd, int cmd, ...) in zvfs_lseek_wrap() argument
452 __ASSERT_NO_MSG(fd < ARRAY_SIZE(fdtable)); in zvfs_lseek_wrap()
454 (void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER); in zvfs_lseek_wrap()
456 res = fdtable[fd].vtable->ioctl(fdtable[fd].obj, cmd, args); in zvfs_lseek_wrap()
459 switch (fdtable[fd].mode & ZVFS_MODE_IFMT) { in zvfs_lseek_wrap()
464 fdtable[fd].offset = res; in zvfs_lseek_wrap()
470 k_mutex_unlock(&fdtable[fd].lock); in zvfs_lseek_wrap()
475 off_t zvfs_lseek(int fd, off_t offset, int whence) in zvfs_lseek() argument
477 if (_check_fd(fd) < 0) { in zvfs_lseek()
481 return zvfs_lseek_wrap(fd, ZFD_IOCTL_LSEEK, offset, whence, fdtable[fd].offset); in zvfs_lseek()
484 int zvfs_fcntl(int fd, int cmd, va_list args) in zvfs_fcntl() argument
488 if (_check_fd(fd) < 0) { in zvfs_fcntl()
493 res = fdtable[fd].vtable->ioctl(fdtable[fd].obj, cmd, args); in zvfs_fcntl()
498 static inline int zvfs_ftruncate_wrap(int fd, int cmd, ...) in zvfs_ftruncate_wrap() argument
503 __ASSERT_NO_MSG(fd < ARRAY_SIZE(fdtable)); in zvfs_ftruncate_wrap()
505 (void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER); in zvfs_ftruncate_wrap()
507 res = fdtable[fd].vtable->ioctl(fdtable[fd].obj, cmd, args); in zvfs_ftruncate_wrap()
509 k_mutex_unlock(&fdtable[fd].lock); in zvfs_ftruncate_wrap()
514 int zvfs_ftruncate(int fd, off_t length) in zvfs_ftruncate() argument
516 if (_check_fd(fd) < 0) { in zvfs_ftruncate()
520 return zvfs_ftruncate_wrap(fd, ZFD_IOCTL_TRUNCATE, length); in zvfs_ftruncate()
523 int zvfs_ioctl(int fd, unsigned long request, va_list args) in zvfs_ioctl() argument
525 if (_check_fd(fd) < 0) { in zvfs_ioctl()
529 return fdtable[fd].vtable->ioctl(fdtable[fd].obj, request, args); in zvfs_ioctl()