* @brief Write data to file through address space page cache * * This function writes data to a file using its address space page cache. It handles * page lookup, data copying, dirty page marking, and synchronization operations. * * @param[in] file Pointer to the file structure containing vnode and aspace * @param[in] buf Buffer containing data to write * @param[in] count Number of bytes
| 1630 | * @return Number of bytes successfully written, or negative error code |
| 1631 | */ |
| 1632 | int dfs_aspace_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos) |
| 1633 | { |
| 1634 | int ret = -EINVAL; |
| 1635 | |
| 1636 | if (file && file->vnode && file->vnode->aspace) |
| 1637 | { |
| 1638 | struct dfs_vnode *vnode = file->vnode; |
| 1639 | struct dfs_aspace *aspace = vnode->aspace; |
| 1640 | |
| 1641 | struct dfs_page *page; |
| 1642 | char *ptr = (char *)buf; |
| 1643 | |
| 1644 | if (!(aspace->ops->write)) |
| 1645 | { |
| 1646 | return ret; |
| 1647 | } |
| 1648 | else if (aspace->mnt && (aspace->mnt->flags & MNT_RDONLY)) |
| 1649 | { |
| 1650 | return -EROFS; |
| 1651 | } |
| 1652 | |
| 1653 | ret = 0; |
| 1654 | |
| 1655 | while (count) |
| 1656 | { |
| 1657 | page = dfs_page_lookup(file, *pos); |
| 1658 | if (page) |
| 1659 | { |
| 1660 | off_t len; |
| 1661 | |
| 1662 | dfs_aspace_lock(aspace); |
| 1663 | len = page->fpos + ARCH_PAGE_SIZE - *pos; |
| 1664 | len = count > len ? len : count; |
| 1665 | rt_memcpy(page->page + *pos - page->fpos, ptr, len); |
| 1666 | ptr += len; |
| 1667 | *pos += len; |
| 1668 | count -= len; |
| 1669 | ret += len; |
| 1670 | |
| 1671 | if (*pos > aspace->vnode->size) |
| 1672 | { |
| 1673 | aspace->vnode->size = *pos; |
| 1674 | } |
| 1675 | |
| 1676 | if (file->flags & O_SYNC) |
| 1677 | { |
| 1678 | if (aspace->vnode->size < page->fpos + page->size) |
| 1679 | { |
| 1680 | page->len = aspace->vnode->size - page->fpos; |
| 1681 | } |
| 1682 | else |
| 1683 | { |
| 1684 | page->len = page->size; |
| 1685 | } |
| 1686 | |
| 1687 | aspace->ops->write(page); |
| 1688 | page->is_dirty = 0; |
| 1689 | } |
no test coverage detected