int sysfile_write(int fd, void *base, size_t len) { int ret = 0; struct mm_struct *mm = pls_read(current)->mm; if (len == 0) { return 0; } if (!file_testfd(fd, 0, 1)) { return -E_INVAL; } /* for linux inode */ if (__is_linux_devfile(fd)) { /* use 8byte int, in case of 64bit off_t * config in linux kernel */ size_t alen = 0; ret = linux_devfile_write(fd, base, len, &alen); if (ret) return ret; return alen; } void *buffer; if ((buffer = kmalloc(IOBUF_SIZE)) == NULL) { return -E_NO_MEM; } size_t copied = 0, alen; while (len != 0) { if ((alen = IOBUF_SIZE) > len) { alen = len; } lock_mm(mm); { if (!copy_from_user(mm, buffer, base, alen, 0)) { ret = -E_INVAL; } } unlock_mm(mm); if (ret == 0) { ret = file_write(fd, buffer, alen, &alen); if (alen != 0) { assert(len >= alen); base += alen, len -= alen, copied += alen; } } if (ret != 0 || alen == 0) { goto out; } } out: kfree(buffer); if (copied != 0) { return copied; } return ret; }
int sysfile_read(int fd, void *base, size_t len) { int ret = 0; struct mm_struct *mm = pls_read(current)->mm; if (len == 0) { return 0; } if (!file_testfd(fd, 1, 0)) { return -E_INVAL; } /* for linux inode */ if (__is_linux_devfile(fd)) { size_t alen = 0; ret = linux_devfile_read(fd, base, len, &alen); if (ret) return ret; return alen; } void *buffer; if ((buffer = kmalloc(IOBUF_SIZE)) == NULL) { return -E_NO_MEM; } size_t copied = 0, alen; while (len != 0) { if ((alen = IOBUF_SIZE) > len) { alen = len; } ret = file_read(fd, buffer, alen, &alen); if (alen != 0) { lock_mm(mm); { if (copy_to_user(mm, base, buffer, alen)) { assert(len >= alen); base += alen, len -= alen, copied += alen; } else if (ret == 0) { ret = -E_INVAL; } } unlock_mm(mm); } if (ret != 0 || alen == 0) { goto out; } } out: kfree(buffer); if (copied != 0) { return copied; } return ret; }
/* sysfile_read - read file */ int sysfile_read(int fd, void *base, size_t len) { cprintf("sysfile_read fd = %d\n",fd); struct mm_struct *mm = current->mm; if (len == 0) { return 0; } if (!file_testfd(fd, 1, 0)) { return -E_INVAL; } void *buffer; if ((buffer = kmalloc(IOBUF_SIZE)) == NULL) { return -E_NO_MEM; } int ret = 0; size_t copied = 0, alen; while (len != 0) { if ((alen = IOBUF_SIZE) > len) { alen = len; } ret = file_read(fd, buffer, alen, &alen); if (alen != 0) { lock_mm(mm); { if (copy_to_user(mm, base, buffer, alen)) { assert(len >= alen); base += alen, len -= alen, copied += alen; } else if (ret == 0) { ret = -E_INVAL; } } unlock_mm(mm); } if (ret != 0 || alen == 0) { goto out; } } out: kfree(buffer); if (copied != 0) { return copied; } return ret; }
int sysfile_write(int fd, void *base, size_t len) { struct mm_struct *mm = current->mm; if (len == 0) { return 0; } if (!file_testfd(fd, 0, 1)) { return -E_INVAL; } void *buffer; if ((buffer = kmalloc(IOBUF_SIZE)) == NULL) { return -E_NO_MEM; } int ret = 0; size_t copied = 0, alen; while (len != 0) { if ((alen = IOBUF_SIZE) > len) { alen = len; } lock_mm(mm); { if (!copy_from_user(mm, buffer, base, alen, 0)) { ret = -E_INVAL; } } unlock_mm(mm); if (ret == 0) { ret = file_write(fd, buffer, alen, &alen); if (alen != 0) { assert(len >= alen); base += alen, len -= alen, copied += alen; } } if (ret != 0 || alen == 0) { goto out; } } out: kfree(buffer); if (copied != 0) { return copied; } return ret; }