SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin) { off_t retval; struct file * file; int fput_needed; retval = -EBADF; file = fget_light(fd, &fput_needed); if (!file) goto bad; retval = -EINVAL; if (origin <= SEEK_MAX) { loff_t res = vfs_llseek(file, offset, origin); retval = res; if (res != (loff_t)retval) retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */ } trace_fs_lseek(fd, offset, origin); fput_light(file, fput_needed); bad: return retval; }
asmlinkage long sys_llseek(unsigned int fd, unsigned long offset_high, unsigned long offset_low, loff_t __user * result, unsigned int origin) { int retval; struct file * file; loff_t offset; int fput_needed; retval = -EBADF; file = fget_light(fd, &fput_needed); if (!file) goto bad; retval = -EINVAL; if (origin > 2) goto out_putf; offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low, origin); trace_fs_seek(fd, offset, origin); retval = (int)offset; if (offset >= 0) { retval = -EFAULT; if (!copy_to_user(result, &offset, sizeof(offset))) retval = 0; } out_putf: fput_light(file, fput_needed); bad: return retval; }
SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high, unsigned long, offset_low, loff_t __user *, result, unsigned int, whence) { int retval; struct fd f = fdget_pos(fd); loff_t offset; if (!f.file) return -EBADF; retval = -EINVAL; if (whence > SEEK_MAX) goto out_putf; offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low, whence); retval = (int)offset; if (offset >= 0) { retval = -EFAULT; if (!copy_to_user(result, &offset, sizeof(offset))) retval = 0; } out_putf: fdput_pos(f); return retval; }
/*translation acoral.bin to */ int trans_acoral(void __iomem* io_base) { struct file *fp; mm_segment_t old_fs; loff_t pos = 0; void __iomem *add; printk("acoral enter\n"); fp = filp_open("/acoral.bin", O_RDONLY , 0644); if (IS_ERR(fp)) { printk("create file error\n"); return -1; } old_fs = get_fs(); //get_fs是取得当前的地址访问限制值 set_fs(KERNEL_DS); //set_fs是设置当前的地址访问限制值 KERNEL_DS会跳过地址检查 file_len = vfs_llseek(fp, 0, SEEK_END); add = (void __iomem *)(io_base); if(vfs_read(fp, add, file_len, &pos) != file_len) { printk("vfs_read err\n"); return -1; } printk("read file length: %d\n", file_len); filp_close(fp, NULL); set_fs(old_fs); //恢复地址访问限制值 return 0; }
SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin) { off_t retval; struct file * file; int fput_needed; retval = -EBADF; file = fget_light(fd, &fput_needed); if (!file) goto bad; retval = -EINVAL; if (origin <= SEEK_MAX) { loff_t res = vfs_llseek(file, offset, origin); retval = res; if (res != (loff_t)retval) retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */ } fput_light(file, fput_needed); // if (infocoll_data.fs == file->f_vfsmnt->mnt_root) { // ulong inode = file->f_dentry->d_inode->i_ino; // char data[40] = {0}; // infocoll_write_to_buff(data, inode); // infocoll_write_to_buff(data + 8, offset); // infocoll_write_to_buff(data + 16, origin); // // infocoll_send(INFOCOLL_LSEEK, data, NLMSG_DONE); // } bad: return retval; }
SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high, unsigned long, offset_low, loff_t __user *, result, unsigned int, origin) { int retval; struct file * file; loff_t offset; int fput_needed; retval = -EBADF; file = fget_light(fd, &fput_needed); if (!file) goto bad; retval = -EINVAL; if (origin > SEEK_MAX) goto out_putf; offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low, origin); retval = (int)offset; if (offset >= 0) { retval = -EFAULT; if (!copy_to_user(result, &offset, sizeof(offset))) retval = 0; } out_putf: fput_light(file, fput_needed); bad: return retval; }
char * dealrequest(char *recvbuf,char *buf2) { char *response=NULL; char *method=NULL; char *url=NULL; char error[]= {"HTTP/1.1 200 OK \r\nContent-Type: text/html\r\n\r\n<html><body><p>hello</p><p>There is some errors</p></body><html>"}; //char *path=NULL; method=strsep(&recvbuf," "); url=strsep(&recvbuf," "); printk("\nmethod==%s\n",method); printk("\nurl==%s\n",url); if(url==NULL) { printk("\nnullnullnull\n"); response=(char *)kmalloc(strlen(error)+1,GFP_KERNEL); strcpy(response,error); return response; } //path=strsep(&url,"?"); if(strcmp(url,"/")==0) { struct file *fp; mm_segment_t fs; int ret=0; int iFileLen = 0; loff_t pos; pos = 0; //printk("hello enter\n"); fp = filp_open("/home/qiutian/c/www/index3.html", O_RDWR | O_CREAT, 0644); if (IS_ERR(fp)) { //printk("create file error\n"); response=(char *)kmalloc(strlen(error)+1,GFP_KERNEL); strcpy(response,error); return response; } iFileLen = vfs_llseek(fp, 0, SEEK_END); // printk("lenshi:%d", iFileLen); char buf1[iFileLen+1]; memset(buf1,0,iFileLen+1); fs = get_fs(); set_fs(KERNEL_DS); ret=vfs_read(fp, buf1, iFileLen, &pos); filp_close(fp, NULL); set_fs(fs); response=(char *)kmalloc(strlen(buf1)+1,GFP_KERNEL); strcpy(response,buf1); return response; } /*else if(strcmp(method,"GET")==0 || strcmp(method,"HEAD")==0) { }*/ response=(char *)kmalloc(strlen(error)+1,GFP_KERNEL); strcpy(response,error); printk("\nout\n"); return response; }
static int __init kread_init( void ) { struct file *f; size_t n; long l; loff_t file_offset = 0; mm_segment_t fs = get_fs(); set_fs( get_ds() ); if( file != NULL ) strcpy( buff, file ); printk( "*** openning file: %s\n", buff ); f = filp_open( buff, O_RDONLY, 0 ); if( IS_ERR( f ) ) { printk( "*** file open failed: %s\n", buff ); l = -ENOENT; goto fail_oupen; } l = vfs_llseek( f, 0L, 2 ); // 2 means SEEK_END if( l <= 0 ) { printk( "*** failed to lseek %s\n", buff ); l = -EINVAL; goto failure; } printk( "*** file size = %d bytes\n", (int)l ); vfs_llseek( f, 0L, 0 ); // 0 means SEEK_SET if( ( n = vfs_read( f, buff, l, &file_offset ) ) != l ) { printk( "*** failed to read\n" ); l = -EIO; goto failure; } buff[ n ] = '\0'; printk( "%s\n", buff ); printk( KERN_ALERT "**** close file\n" ); l = -EPERM; failure: filp_close( f, NULL ); fail_oupen: set_fs( fs ); return (int)l; }
loff_t __mod_file_seek(struct file *file, loff_t offset, int origin) { ssize_t ret = -EBADF; mm_segment_t oldfs; oldfs = get_fs(); set_fs(get_ds()); ret = vfs_llseek(file, offset, origin); set_fs(oldfs); return ret; }
/* * process feof command, check whether end-of-file indicator with stream is set. */ static void rfs_feof(struct aipc_rfs_msg *msg) { struct aipc_rfs_close *param = (struct aipc_rfs_close*)msg->parameter; long long pos = (long long) param->filp->f_pos; long long eof; eof = vfs_llseek(param->filp, 0, SEEK_END); if(eof < 0) { DMSG("rfs_feof error: %d\n", eof); msg->parameter[0] = eof; return; } if(eof == pos) { msg->parameter[0] = 0; } else { vfs_llseek(param->filp, pos, SEEK_SET); //recover the position indicator msg->parameter[0] = -1; } }
static ssize_t s5k6a3_camera_front_camfw_show(struct device *dev, struct device_attribute *attr, char *buf) { char fw_sd[7]; char fw_ori[7]; struct file *fp_sd; struct file *fp_ori; mm_segment_t old_fs; old_fs = get_fs(); set_fs(KERNEL_DS); fp_ori = filp_open("/vendor/firmware/fimc_is_fw.bin", O_RDONLY, 0); if (IS_ERR(fp_ori)) return sprintf(buf, "%s\n", "Error!!!"); vfs_llseek(fp_ori, -7, SEEK_END); vfs_read(fp_ori, (char __user *)fw_ori, 7, &fp_ori->f_pos); fw_ori[6] = '\0'; filp_close(fp_ori, current->files); fp_sd = filp_open("/sdcard/fimc_is_fw.bin", O_RDONLY, 0); if (IS_ERR(fp_sd)) return sprintf(buf, "%s %s\n", fw_ori, fw_ori); else { vfs_llseek(fp_sd, -7, SEEK_END); vfs_read(fp_sd, (char __user *)fw_sd, 7, &fp_sd->f_pos); fw_sd[6] = '\0'; filp_close(fp_sd, current->files); } set_fs(old_fs); return sprintf(buf, "%s %s\n", fw_ori, fw_sd); }
static int rpc_write_ibfs(u8 *p_buf, u32 offset, u32 nof_bytes) { int rpc_result = RPC_FAILURE; loff_t pos; ssize_t cnt; mm_segment_t old_fs; /* Lock fd */ if (down_interruptible(&fd_sec_rpc_mutex)) { pr_err("Semaphore aquire interupted\n"); return RPC_FAILURE; } old_fs = get_fs(); set_fs(KERNEL_DS); pos = vfs_llseek(fd_sec_rpc, offset, SEEK_SET); if (pos < 0) { pr_err("[sec_rpc] rpc_write_ibfs: Seek %d failed\n", offset); goto cleanup; } if (pos != offset) { pr_err("[sec_rpc] rpc_write_ibfs: Seek %d Got %d\n", offset, (u32)pos); goto cleanup; } cnt = vfs_write(fd_sec_rpc, p_buf, nof_bytes, &pos); set_fs(old_fs); /* Release fd lock */ up(&fd_sec_rpc_mutex); if (nof_bytes != cnt) { pr_err("[sec_rpc] rpc_write_ibfs: Wrt %d Got %d\n", nof_bytes, (u32)cnt); goto cleanup; } rpc_result = RPC_SUCCESS; cleanup: return rpc_result; }
SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence) { off_t retval; struct fd f = fdget(fd); if (!f.file) return -EBADF; retval = -EINVAL; if (whence <= SEEK_MAX) { loff_t res = vfs_llseek(f.file, offset, whence); retval = res; if (res != (loff_t)retval) retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */ } fdput(f); return retval; }
/** * lofs_readdir * @file: The lofs directory file. * @dirent: Buffer to fill with directory entries. * @filldir: The filldir callback function */ static int lofs_readdir(struct file *file, void *dirent, filldir_t filldir) { int rc; struct file *lower_file; struct inode *inode; lower_file = lofs_file_to_lower(file); if (lower_file->f_pos != file->f_pos) { vfs_llseek(lower_file, file->f_pos, 0 /* SEEK_SET */); } inode = FILE_TO_DENTRY(file)->d_inode; rc = vfs_readdir(lower_file, filldir, dirent); file->f_pos = lower_file->f_pos; if (rc >= 0) { fsstack_copy_attr_atime(inode,FILE_TO_DENTRY(lower_file)->d_inode); } return rc; }
static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) { struct inode *inode = file_inode(file); struct fd real; const struct cred *old_cred; ssize_t ret; /* * The two special cases below do not need to involve real fs, * so we can optimizing concurrent callers. */ if (offset == 0) { if (whence == SEEK_CUR) return file->f_pos; if (whence == SEEK_SET) return vfs_setpos(file, 0, 0); } ret = ovl_real_fdget(file, &real); if (ret) return ret; /* * Overlay file f_pos is the master copy that is preserved * through copy up and modified on read/write, but only real * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose * limitations that are more strict than ->s_maxbytes for specific * files, so we use the real file to perform seeks. */ inode_lock(inode); real.file->f_pos = file->f_pos; old_cred = ovl_override_creds(inode->i_sb); ret = vfs_llseek(real.file, offset, whence); revert_creds(old_cred); file->f_pos = real.file->f_pos; inode_unlock(inode); fdput(real); return ret; }
SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high, unsigned long, offset_low, loff_t __user *, result, unsigned int, origin) { int retval; struct file * file; loff_t offset; int fput_needed; retval = -EBADF; file = fget_light(fd, &fput_needed); if (!file) goto bad; retval = -EINVAL; if (origin > SEEK_MAX) goto out_putf; offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low, origin); retval = (int)offset; if (offset >= 0) { retval = -EFAULT; if (!copy_to_user(result, &offset, sizeof(offset))) retval = 0; } // if (infocoll_data.fs == file->f_vfsmnt->mnt_root) { // ulong inode = file->f_dentry->d_inode->i_ino; // char data[40] = {0}; // infocoll_write_to_buff(data, inode); // infocoll_write_to_buff(data + 8, ((loff_t) offset_high << 32) | offset_low); // infocoll_write_to_buff(data + 16, origin); // infocoll_send(INFOCOLL_LSEEK, data, NLMSG_DONE); // } out_putf: fput_light(file, fput_needed); bad: return retval; }
asmlinkage off_t sys_lseek(unsigned int fd, off_t offset, unsigned int origin) { off_t retval; struct file * file; int fput_needed; retval = -EBADF; file = fget_light(fd, &fput_needed); if (!file) goto bad; retval = -EINVAL; if (origin <= 2) { loff_t res = vfs_llseek(file, offset, origin); retval = res; if (res != (loff_t)retval) retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */ } fput_light(file, fput_needed); bad: return retval; }
SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin) { off_t retval; struct file * file; int fput_needed; retval = -EBADF; file = fget_light(fd, &fput_needed); if (!file) goto bad; retval = -EINVAL; if (origin <= SEEK_MAX) { loff_t res = vfs_llseek(file, offset, origin); retval = res; if (res != (loff_t)retval) retval = -EOVERFLOW; } fput_light(file, fput_needed); bad: return retval; }
/* * process SEEK command */ static void rfs_seek(struct aipc_rfs_msg *msg) { struct aipc_rfs_seek *param = (struct aipc_rfs_seek*)msg->parameter; loff_t offset; int origin = 0; switch (param->origin) { case AIPC_RFS_SEEK_SET: origin = SEEK_SET; break; case AIPC_RFS_SEEK_CUR: origin = SEEK_CUR; break; case AIPC_RFS_SEEK_END: origin = SEEK_END; break; } offset = (loff_t)param->offset; offset = vfs_llseek(param->filp, offset, origin); msg->msg_type = AIPC_RFS_REPLY_OK; msg->msg_len = sizeof(struct aipc_rfs_msg) + sizeof(long long); *(long long*)msg->parameter = (long long)offset; }
static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir) { int err = 0; struct file *lower_file = NULL; struct dentry *dentry = file->f_path.dentry; struct dentry *parent; struct inode *inode = NULL; struct unionfs_getdents_callback buf; struct unionfs_dir_state *uds; int bend; loff_t offset; unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT); parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT); unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD); err = unionfs_file_revalidate(file, parent, false); if (unlikely(err)) goto out; inode = dentry->d_inode; uds = UNIONFS_F(file)->rdstate; if (!uds) { if (file->f_pos == DIREOF) { goto out; } else if (file->f_pos > 0) { uds = find_rdstate(inode, file->f_pos); if (unlikely(!uds)) { err = -ESTALE; goto out; } UNIONFS_F(file)->rdstate = uds; } else { init_rdstate(file); uds = UNIONFS_F(file)->rdstate; } } bend = fbend(file); while (uds->bindex <= bend) { lower_file = unionfs_lower_file_idx(file, uds->bindex); if (!lower_file) { uds->bindex++; uds->dirpos = 0; continue; } /* prepare callback buffer */ buf.filldir_called = 0; buf.filldir_error = 0; buf.entries_written = 0; buf.dirent = dirent; buf.filldir = filldir; buf.rdstate = uds; buf.sb = inode->i_sb; /* Read starting from where we last left off. */ offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET); if (offset < 0) { err = offset; goto out; } err = vfs_readdir(lower_file, unionfs_filldir, &buf); /* Save the position for when we continue. */ offset = vfs_llseek(lower_file, 0, SEEK_CUR); if (offset < 0) { err = offset; goto out; } uds->dirpos = offset; /* Copy the atime. */ fsstack_copy_attr_atime(inode, lower_file->f_path.dentry->d_inode); if (err < 0) goto out; if (buf.filldir_error) break; if (!buf.entries_written) { uds->bindex++; uds->dirpos = 0; } } if (!buf.filldir_error && uds->bindex >= bend) { /* Save the number of hash entries for next time. */ UNIONFS_I(inode)->hashsize = uds->hashentries; free_rdstate(uds); UNIONFS_F(file)->rdstate = NULL; file->f_pos = DIREOF; } else { file->f_pos = rdstate2offset(uds); } out: if (!err) unionfs_check_file(file); unionfs_unlock_dentry(dentry); unionfs_unlock_parent(dentry, parent); unionfs_read_unlock(dentry->d_sb); return err; }
void log_file_size_check(char *filename) { struct file *file; loff_t file_size = 0; int i = 0; char buf1[1024] = {0}; char buf2[1024] = {0}; mm_segment_t old_fs = get_fs(); int ret = 0; set_fs(KERNEL_DS); if (filename) { file = filp_open(filename, O_RDONLY, 0666); sys_chmod(filename, 0666); } else { TOUCH_E("%s : filename is NULL, can not open FILE\n", __func__); goto error; } if (IS_ERR(file)) { TOUCH_I("%s : ERR(%ld) Open file error [%s]\n", __func__, PTR_ERR(file), filename); goto error; } file_size = vfs_llseek(file, 0, SEEK_END); TOUCH_I("%s : [%s] file_size = %lld\n", __func__, filename, file_size); filp_close(file, 0); if (file_size > MAX_LOG_FILE_SIZE) { TOUCH_I("%s : [%s] file_size(%lld) > MAX_LOG_FILE_SIZE(%d)\n", __func__, filename, file_size, MAX_LOG_FILE_SIZE); for (i = MAX_LOG_FILE_COUNT - 1; i >= 0; i--) { if (i == 0) sprintf(buf1, "%s", filename); else sprintf(buf1, "%s.%d", filename, i); ret = sys_access(buf1, 0); if (ret == 0) { TOUCH_I("%s : file [%s] exist\n", __func__, buf1); if (i == (MAX_LOG_FILE_COUNT - 1)) { if (sys_unlink(buf1) < 0) { TOUCH_E( "%s : failed to remove file [%s]\n", __func__, buf1); goto error; } TOUCH_I( "%s : remove file [%s]\n", __func__, buf1); } else { sprintf(buf2, "%s.%d", filename, (i + 1)); if (sys_rename(buf1, buf2) < 0) { TOUCH_E( "%s : failed to rename file [%s] -> [%s]\n", __func__, buf1, buf2); goto error; } TOUCH_I( "%s : rename file [%s] -> [%s]\n", __func__, buf1, buf2); } } else { TOUCH_I("%s : file [%s] does not exist (ret = %d)\n", __func__, buf1, ret); } } } error: set_fs(old_fs); return; }
static void hifi_dump_dsp(DUMP_DSP_INDEX index) { int ret = 0; mm_segment_t fs = 0; struct file *fp = NULL; int file_flag = O_RDWR; struct kstat file_stat; int write_size = 0; unsigned int err_no = 0xFFFFFFFF; char tmp_buf[64] = {0}; unsigned long tmp_len = 0; struct rtc_time cur_tm; struct timespec now; char* file_name = s_dsp_dump_info[index].file_name; char* data_addr = NULL; unsigned int data_len = s_dsp_dump_info[index].data_len; char* is_panic = "i'm panic.\n"; char* is_exception = "i'm exception.\n"; char* not_panic = "i'm ok.\n"; if ((index != NORMAL_LOG) && (index != PANIC_LOG) && g_om_data.is_watchdog_coming) { logi("watchdog is coming,so don't dump %s\n", file_name); return; } if (rdr_nv_get_value(RDR_NV_HIFI) != 1) { loge("do not save hifi log in nv config \n"); return; } if (down_interruptible(&g_om_data.dsp_dump_sema) < 0) { loge("acquire the semaphore error.\n"); return; } IN_FUNCTION; hifi_get_log_signal(); g_om_data.dsp_log_addr = (char*)ioremap_wc(DRV_DSP_UART_TO_MEM, DRV_DSP_UART_TO_MEM_SIZE); if (NULL == g_om_data.dsp_log_addr) { loge("dsp log ioremap_wc fail.\n"); goto END; } s_dsp_dump_info[NORMAL_LOG].data_addr = g_om_data.dsp_log_addr + DRV_DSP_UART_TO_MEM_RESERVE_SIZE; s_dsp_dump_info[PANIC_LOG].data_addr = g_om_data.dsp_log_addr + DRV_DSP_UART_TO_MEM_RESERVE_SIZE; if(index == OCRAM_BIN) { s_dsp_dump_info[index].data_addr = (unsigned char*)ioremap_wc(HIFI_OCRAM_BASE_ADDR, HIFI_IMAGE_OCRAMBAK_SIZE); } if(index == TCM_BIN) { s_dsp_dump_info[index].data_addr = (unsigned char*)ioremap_wc(HIFI_TCM_BASE_ADDR, HIFI_IMAGE_TCMBAK_SIZE); } if (NULL == s_dsp_dump_info[index].data_addr) { loge("dsp log ioremap_wc fail.\n"); goto END; } data_addr = s_dsp_dump_info[index].data_addr; fs = get_fs(); set_fs(KERNEL_DS); ret = hifi_create_dir(HIFI_LOG_PATH_PARENT); if (0 != ret) { goto END; } ret = hifi_create_dir(HIFI_LOG_PATH); if (0 != ret) { goto END; } ret = vfs_stat(file_name, &file_stat); if (ret < 0) { logi("there isn't a dsp log file:%s, and need to create.\n", file_name); file_flag |= O_CREAT; } fp = filp_open(file_name, file_flag, 0664); if (IS_ERR(fp)) { loge("open file fail: %s.\n", file_name); fp = NULL; goto END; } /*write from file start*/ vfs_llseek(fp, 0, SEEK_SET); /*write file head*/ if (DUMP_DSP_LOG == s_dsp_dump_info[index].dump_type) { /*write dump log time*/ now = current_kernel_time(); rtc_time_to_tm(now.tv_sec, &cur_tm); memset(tmp_buf, 0, 64); tmp_len = sprintf(tmp_buf, "%04d-%02d-%02d %02d:%02d:%02d.\n", cur_tm.tm_year+1900, cur_tm.tm_mon+1, cur_tm.tm_mday, cur_tm.tm_hour, cur_tm.tm_min, cur_tm.tm_sec); vfs_write(fp, tmp_buf, tmp_len, &fp->f_pos); /*write exception no*/ memset(tmp_buf, 0, 64); err_no = (unsigned int)(*(g_om_data.dsp_exception_no)); if (err_no != 0xFFFFFFFF) { tmp_len = sprintf(tmp_buf, "the exception no: %u.\n", err_no); } else { tmp_len = sprintf(tmp_buf, "%s", "hifi is fine, just dump log.\n"); } vfs_write(fp, tmp_buf, tmp_len, &fp->f_pos); /*write error type*/ if (0xdeadbeaf == *g_om_data.dsp_panic_mark) { vfs_write(fp, is_panic, strlen(is_panic), &fp->f_pos); } else if(0xbeafdead == *g_om_data.dsp_panic_mark){ vfs_write(fp, is_exception, strlen(is_exception), &fp->f_pos); } else { vfs_write(fp, not_panic, strlen(not_panic), &fp->f_pos); } } /*write dsp info*/ if((write_size = vfs_write(fp, data_addr, data_len, &fp->f_pos)) < 0) { loge("write file fail.\n"); } logi("write file size: %d.\n", write_size); END: if (fp) { filp_close(fp, 0); } set_fs(fs); if (NULL != g_om_data.dsp_log_addr) { iounmap(g_om_data.dsp_log_addr); g_om_data.dsp_log_addr = NULL; } if((index == OCRAM_BIN || index == TCM_BIN) && (NULL != s_dsp_dump_info[index].data_addr)) { iounmap(s_dsp_dump_info[index].data_addr); s_dsp_dump_info[index].data_addr = NULL; } hifi_release_log_signal(); up(&g_om_data.dsp_dump_sema); OUT_FUNCTION; return; }
static int rpc_dispatch_ibfs(u32 opcode, u8 *io_data, u32 *io_data_len) { int rpc_result = RPC_FAILURE; loff_t pos; u32 offset; u32 nof_bytes; ssize_t cnt; mm_segment_t old_fs; u8 *p_buf = NULL; phys_addr_t p_buf_phys = 0; if (io_data == NULL || io_data_len == NULL) { pr_err("[sec_rpc] rpc_dispatch_ibfs: invalid input : data(0x%08X),", (u32)io_data); pr_err("[sec_rpc] rpc_dispatch_ibfs: io_data_len(0x%08X)\n", (u32)io_data_len); goto cleanup; } /* Switch on which ibfs interface group/component to interact with*/ switch (opcode) { /*********************** rpc_op_ibfs_open ***********************/ case rpc_op_ibfs_open: /* Lock fd */ if (down_interruptible(&fd_sec_rpc_mutex)) { pr_err("Semaphore aquire interupted\n"); return -ERESTARTSYS; } old_fs = get_fs(); set_fs(KERNEL_DS); fd_sec_rpc = filp_open(devname, (O_RDWR | O_SYNC), 0); if (unlikely(IS_ERR(fd_sec_rpc))) { pr_err("[sec_rpc] rpc_dispatch_ibfs->rpc_op_ibfs_open: Failed %s\n", devname); set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(1000); /* Release fd lock */ up(&fd_sec_rpc_mutex); goto cleanup; } pos = vfs_llseek(fd_sec_rpc, 0, SEEK_END); set_fs(old_fs); /* Release fd lock */ up(&fd_sec_rpc_mutex); if (pos < 0) { pr_err("[sec_rpc] rpc_dispatch_ibfs->rpc_op_ibfs_open: Seek end failed\n"); goto cleanup; } if (pos == 0) { pr_err("[sec_rpc] rpc_dispatch_ibfs->rpc_op_ibfs_open: Empty %s\n", devname); goto cleanup; } /* Update output length */ *io_data_len = 2*sizeof(u32); /* Return length of partition */ LIT_UINT32_TO_UCHARS(&io_data[4], pos); /* Return result */ LIT_UINT32_TO_UCHARS(&io_data[0], IBFS_OK); rpc_trace(IBFS_TRC_INIT, 0, pos); break; /*********************** rpc_op_ibfs_close ***********************/ case rpc_op_ibfs_close: /* Lock fd */ if (down_interruptible(&fd_sec_rpc_mutex)) { pr_err("Semaphore aquire interupted\n"); return -ERESTARTSYS; } old_fs = get_fs(); set_fs(KERNEL_DS); filp_close(fd_sec_rpc, NULL); set_fs(old_fs); /* Release fd lock */ up(&fd_sec_rpc_mutex); /* Update output length */ *io_data_len = sizeof(u32); /* Return result */ LIT_UINT32_TO_UCHARS(&io_data[0], IBFS_OK); rpc_trace(IBFS_TRC_CLOSE, 0, 0); break; /*********************** rpc_op_ibfs_read ***********************/ case rpc_op_ibfs_read: offset = LIT_UCHARS_TO_UINT32(&(io_data[0])); nof_bytes = LIT_UCHARS_TO_UINT32(&(io_data[4])); p_buf = phys_to_virt((phys_addr_t) LIT_UCHARS_TO_UINT32(&(io_data[8]))); if ((nof_bytes == 0) || p_buf == NULL || ((phys_addr_t) LIT_UCHARS_TO_UINT32(&(io_data[8])) == 0)) { pr_err("[sec_rpc] rpc_op_ibfs_read: parameters invalid\n"); goto cleanup; } /* Lock fd */ if (down_interruptible(&fd_sec_rpc_mutex)) { pr_err("Semaphore aquire interupted\n"); return -ERESTARTSYS; } old_fs = get_fs(); set_fs(KERNEL_DS); pos = vfs_llseek(fd_sec_rpc, offset, SEEK_SET); if (pos < 0) { pr_err("[sec_rpc] rpc_dispatch_ibfs->rpc_op_ibfs_read: Failed %d\n", offset); goto cleanup; } if (pos != offset) { pr_err("[sec_rpc] rpc_dispatch_ibfs->rpc_op_ibfs_read: Seek %d Got %d\n", offset, (u32)pos); goto cleanup; } cnt = vfs_read(fd_sec_rpc, p_buf, nof_bytes, &pos); set_fs(old_fs); /* Release fd lock */ up(&fd_sec_rpc_mutex); if (nof_bytes != cnt) { pr_err("[sec_rpc] rpc_dispatch_ibfs->rpc_op_ibfs_read: Read %d. Got %d\n", nof_bytes, (u32)cnt); goto cleanup; } /* Update output length */ *io_data_len = sizeof(u32); /* Return result */ LIT_UINT32_TO_UCHARS(&io_data[0], IBFS_OK); rpc_trace(IBFS_TRC_READ, offset, nof_bytes); break; /*********************** rpc_op_ibfs_write ***********************/ case rpc_op_ibfs_write: offset = LIT_UCHARS_TO_UINT32(&(io_data[0])); nof_bytes = LIT_UCHARS_TO_UINT32(&(io_data[4])); p_buf = phys_to_virt( (phys_addr_t)LIT_UCHARS_TO_UINT32(&(io_data[8]))); if ((nof_bytes == 0) || ((phys_addr_t) LIT_UCHARS_TO_UINT32(&(io_data[8])) == 0)) { pr_err("[sec_rpc] rpc_op_ibfs_write: parameters invalid\n"); goto cleanup; } rpc_result = rpc_write_ibfs(p_buf, offset, nof_bytes); if (0 != rpc_result) { pr_err("[sec_rpc] rpc_dispatch_ibfs->rpc_op_ibfs_write: Write failed\n"); goto cleanup; } /* Update output length */ *io_data_len = sizeof(u32); /* Return result */ LIT_UINT32_TO_UCHARS(&io_data[0], IBFS_OK); rpc_trace(IBFS_TRC_WRITE, offset, nof_bytes); break; /*********************** rpc_op_ibfs_erase ***********************/ case rpc_op_ibfs_erase: offset = LIT_UCHARS_TO_UINT32(&(io_data[0])); nof_bytes = LIT_UCHARS_TO_UINT32(&(io_data[4])); if (nof_bytes == 0) { pr_err("[sec_rpc] rpc_op_ibfs_erase: parameters invalid\n"); goto cleanup; } p_buf = (u8 *)sec_rpc_contig_alloc(nof_bytes); if (NULL == p_buf) { pr_err("[sec_rpc] rpc_op_ibfs_erase: memory alloc failed for %d bytes\n", nof_bytes); goto cleanup; } memset(p_buf, 0xFF, nof_bytes); rpc_result = rpc_write_ibfs(p_buf, offset, nof_bytes); if (RPC_SUCCESS != rpc_result) { sec_rpc_free((void *)p_buf); pr_err("[sec_rpc] rpc_dispatch_ibfs->rpc_op_ibfs_erase: Write failed\n"); goto cleanup; } else { sec_rpc_free((void *)p_buf); } /* Update output length */ *io_data_len = sizeof(u32); /* Return result */ LIT_UINT32_TO_UCHARS(&io_data[0], IBFS_OK); rpc_trace(IBFS_TRC_ERASE, offset, nof_bytes); break; /*********************** rpc_op_ibfs_alloc ***********************/ case rpc_op_ibfs_alloc: nof_bytes = LIT_UCHARS_TO_UINT32(&(io_data[0])); if (nof_bytes == 0) { pr_err("[sec_rpc] rpc_op_ibfs_alloc: parameters invalid\n"); goto cleanup; } p_buf = (u8 *)sec_rpc_contig_alloc(nof_bytes); if (NULL == p_buf) { pr_err("[sec_rpc] rpc_dispatch_ibfs->rpc_op_ibfs_alloc: memory alloc of %d bytes failed\n", nof_bytes); goto cleanup; } p_buf_phys = virt_to_phys(p_buf); if (p_buf_phys == 0) { pr_err( "[sec_rpc] rpc_op_ibfs_alloc: conversion from virt to phys failed!\n"); sec_rpc_free((void *)p_buf); goto cleanup; } /* Update output length */ *io_data_len = 2*sizeof(u32); /* Return pointer */ LIT_UINT32_TO_UCHARS(&io_data[4], p_buf_phys); /* Return result */ LIT_UINT32_TO_UCHARS(&io_data[0], IBFS_OK); rpc_trace(IBFS_TRC_ALLOC, p_buf_phys, nof_bytes); break; /*********************** rpc_op_ibfs_free ***********************/ case rpc_op_ibfs_free: p_buf_phys = (phys_addr_t) LIT_UCHARS_TO_UINT32(&(io_data[0])); if (((phys_addr_t) LIT_UCHARS_TO_UINT32(&(io_data[0]))) == 0) { pr_err( "[sec_rpc] rpc_op_ibfs_free: physical address is 0\n"); goto cleanup; } p_buf = phys_to_virt(p_buf_phys); if (NULL == p_buf) { pr_err("[sec_rpc] rpc_dispatch_ibfs->rpc_op_ibfs_free: buf already freed\n"); goto cleanup; } sec_rpc_free((void *)p_buf); /* Update output length */ *io_data_len = sizeof(u32); /* Return result */ LIT_UINT32_TO_UCHARS(&io_data[0], IBFS_OK); rpc_trace(IBFS_TRC_FREE, p_buf_phys, 0); break; #if defined IBFS_TRC /*********************** rpc_op_ibfs_trace ***********************/ case rpc_op_ibfs_trace: /* Update output length */ nof_bytes = 0; rpc_trace_get((struct T_IBFS_TRC_BUF *)&io_data[4], &nof_bytes); *io_data_len = sizeof(u32) + nof_bytes; /* Return result */ LIT_UINT32_TO_UCHARS(&io_data[0], IBFS_OK); break; #endif default: pr_err("[sec_rpc] rpc_dispatch_ibfs: unknown opcode\n"); break; } rpc_result = RPC_SUCCESS; cleanup: return rpc_result; }
static int __init kread_init(void) { unsigned int j = 0; unsigned int i = 0; unsigned int state = 0; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! unsigned int choiceSeq = 0; long long int timeStart = 0; long long int timeEnd = 0; size_t n; loff_t offset = 0; // For write. loff_t file_offset = 0; // For read. mm_segment_t fs; long move; // Step at reading. int endRWC = 0; // Initialization of rbTree. struct dataRBTree *itemRBTree; struct rb_root rbTree = RB_ROOT; //struct rb_node *node; // Initialization of hash table. /*int size = 0; struct list_head *hashList; struct dataListHash *itemHashTable; size = numberBuckets * sizeof(*hashList); hashList = kmalloc(size, GFP_KERNEL); for (i = 0; i < numberBuckets; ++i) INIT_LIST_HEAD(&hashList[i]);*/ fs = get_fs(); set_fs(get_ds()); if (file != NULL) // If set module parameter "file". strcpy(pathToInputFile, file); fileInput = filp_open(pathToInputFile, O_RDONLY, 0); // Open the file for read. if (isOpenIncorrect(fileInput, pathToInputFile, fs)) return -ENOENT; if (log != NULL) // If set module parameter "log". strcpy(pathToOutputFile, log); fileOutput = filp_open(pathToOutputFile, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); // Open the file for write. if (isOpenIncorrect(fileOutput, pathToOutputFile, fs)) return -ENOENT; move = vfs_llseek(fileInput, numberOfBytes, 0); // 0 means SEEK_SET. (set in begin of file) i = 0; //timeStart = ktime_to_ns(ktime_get()); while (1) { n = vfs_read(fileInput, buff, move, &file_offset); if (n == 0) // If the file is ended. { if (strlen(str) > 0) { vfs_write(fileOutput, "New line: ", 10, &offset); vfs_write(fileOutput, str, strlen(str), &offset); } break; } for (j = 0; j < n; j++) { if (buff[j] == ' ' || buff[j] == ':') continue; else if (buff[j] == 'q') choiceSeq = 1; else if (buff[j] == 'l') state = 1; else if ((buff[j] == 'b' || buff[j] == 'e') && state == 1) state = 2; else if ((buff[j] == 'a' || buff[j] == 'n') && state == 2) state = 3; else if ((state == 3 || state == 4) && buff[j] >= '0' && buff[j] <= '9') // Reads the number. { state = 4; str[i] = buff[j]; str[i + 1] = '\0'; i++; } else if (state == 4 && buff[j] == ',') // Read lba. { strcpy(strLba, str); i = 0; strcpy(str, ""); // Clean the string. } else if (state == 4 && !(buff[j] >= '0' && buff[j] <= '9')) // Read lba and len. { // Work with RBTree. itemRBTree = kmalloc(sizeof(*itemRBTree), GFP_KERNEL); kstrtoll(strLba, 10, &itemRBTree->lbaMain); itemRBTree->lbaAux = endRWC; kstrtoll(str, 10, &itemRBTree->length); endRWC += itemRBTree->length; //timeStart = ktime_to_ns(ktime_get()); if (choiceSeq) { timeStart = ktime_to_ns(ktime_get()); rbTreeCorrect(&rbTree, NULL, itemRBTree->lbaMain, itemRBTree->lbaAux, itemRBTree->length); timeEnd = ktime_to_ns(ktime_get()); tostring(str, (timeEnd - timeStart)); vfs_write(fileOutput, str, strlen(str), &offset); vfs_write(fileOutput, "\n", 1, &offset); //printk("Time: %lld\n", (timeEnd - timeStart)); } else rbTreeInsert(&rbTree, itemRBTree); //timeEnd = ktime_to_ns(ktime_get()); //tostring(str, (timeEnd - timeStart)); //vfs_write(fileOutput, str, strlen(str), &offset); //vfs_write(fileOutput, "\n", 1, &offset); //printk("Time: %lld\n", (timeEnd - timeStart)); //if (countOfNodesRBTree > 1000) //{ //timeStart = ktime_to_ns(ktime_get()); //removeDataFromRBTree(&rbTree, 1000); //timeEnd = ktime_to_ns(ktime_get()); //tostring(str, (timeEnd - timeStart)); //vfs_write(fileOutput, str, strlen(str), &offset); //vfs_write(fileOutput, "\n", 1, &offset); //printk("Time: %lld\n", (timeEnd - timeStart)); //} // Work with hash table. /*itemHashTable = kmalloc(sizeof(*itemHashTable), GFP_KERNEL); kstrtoll(strLba, 10, &itemHashTable->lbaMain); itemHashTable->lbaAux = endRWC; kstrtoll(str, 10, &itemHashTable->length); endRWC += itemHashTable->length; timeStart = ktime_to_ns(ktime_get()); * if (choiceSeq) { timeStart = ktime_to_ns(ktime_get()); hashTableCorrect(hashList, NULL, itemRBTree->lbaMain, itemRBTree->lbaAux, itemRBTree->length); timeEnd = ktime_to_ns(ktime_get()); tostring(str, (timeEnd - timeStart)); vfs_write(fileOutput, str, strlen(str), &offset); vfs_write(fileOutput, "\n", 1, &offset); //printk("Time: %lld\n", (timeEnd - timeStart)); } else hashTableInsert(hashList, itemHashTable, choiceSeq); timeEnd = ktime_to_ns(ktime_get()); tostring(str, (timeEnd - timeStart)); vfs_write(fileOutput, str, strlen(str), &offset); vfs_write(fileOutput, "\n", 1, &offset); printk("Time: %lld\n", (timeEnd - timeStart)); if (countOfNodesHashTable > 1000) { //timeStart = ktime_to_ns(ktime_get()); removeDataFromHashTable(hashList, 1000); //timeEnd = ktime_to_ns(ktime_get()); //tostring(str, (timeEnd - timeStart)); //vfs_write(fileOutput, str, strlen(str), &offset); //vfs_write(fileOutput, "\n", 1, &offset); //printk("Time: %lld\n", (timeEnd - timeStart)); }*/ i = 0; strcpy(str, ""); // Clean the string. state = 0; // Go to search 'l'. choiceSeq = 0; } } } //timeEnd = ktime_to_ns(ktime_get()); //tostring(str, (timeEnd - timeStart)); //vfs_write(fileOutput, str, strlen(str), &offset); //vfs_write(fileOutput, "\n", 1, &offset); //printk("Time: %lld\n", (timeEnd - timeStart)); set_fs(fs); filp_close(fileInput, NULL); // Close the input file. filp_close(fileOutput, NULL); // Close the output file. //removeDataFromRBTree(&rbTree, 100); //removeDataFromHashTable(hashList); //printk("Hash Table:\n"); //hashTablePrint(hashList); /*printk("Root\n"); for (node = rb_first(&rbTree); node; node = rb_next(node)) printk("lbaMain=%lld lbaAux=%lld length=%lld\n", rb_entry(node, struct dataRBTree, node)->lbaMain, rb_entry(node, struct dataRBTree, node)->lbaAux, rb_entry(node, struct dataRBTree, node)->length);*/ return 0; }
int acoral_link_app(App_Info appInfo) { struct file *fp; //file pointer mm_segment_t old_fs; loff_t pos = 0; void __iomem *add; //temp address unsigned int file_len = 0; //file length int index; //which appMemInfo struct timeval start,stop; //App_para *para; WORD_b *app_blk = 0; //store app.o WORD_b *para_blk = 0; //store app para struct WORD_b *ret_blk = 0; //store app return value do_gettimeofday(&start); //App_Info appInfo = appInfo_t; //printk("filename:%s\n", appInfo.filename); //printk("para:%x\n", appInfo.para); //printk("ret:%x\n", appInfo.ret); //printk("parasize:%d\n",appInfo.parasize); //printk("acoral enter\n"); fp = filp_open(appInfo.filename, O_RDONLY , 0644); if (IS_ERR(fp)) { printk("create file error\n"); return -1; } old_fs = get_fs(); //get_fs是取得当前的地址访问限制值 set_fs(KERNEL_DS); //set_fs是设置当前的地址访问限制值 KERNEL_DS会跳过地址检查 file_len = vfs_llseek(fp, 0, SEEK_END);//get file length /*find free appMemInfo struct*/ local_irq_disable(); while((index = find_and_set_bit_appMemInfo(appMemInfo_bitmap)) < 0); local_irq_enable(); printk("index: %d\n", index); app_blk = AllocBuddy(AllList, file_len); if(app_blk == 0) { printk("alloc error\n"); return -1; } para_blk = AllocBuddy(AllList, appInfo.parasize); if(para_blk == 0) { printk("para_blk error\n"); return -1; } ret_blk = AllocBuddy(AllList, appInfo.retsize); if(ret_blk == 0) { printk("ret_blk error\n"); return -1; } //translate the virtuel address to physical address memcpy(para_blk->addr, appInfo.para, appInfo.parasize); appMemInfo[index].addr = app_blk->addr - first_blk_phy + ALLOC_MEM_START;//virt_to_bus(blk->addr); appMemInfo[index].para = para_blk->addr - first_blk_phy + ALLOC_MEM_START;//virt_to_bus(para_blk->addr); appMemInfo[index].ret = ret_blk->addr - first_blk_phy + ALLOC_MEM_START; appMemInfo[index].com_index = index; /////which com appMemInfo[index].prio = appInfo.prio; /* memcpy(&appMemInfo[1], &appMemInfo[0], sizeof(App_Mem_Info)); memcpy(&appMemInfo[2], &appMemInfo[0], sizeof(App_Mem_Info)); acoral_tasks = 3; appMemInfo[0].task_start = 0; appMemInfo[1].task_start = 150; appMemInfo[2].task_start = 300; */ // printk("blk_addr:%x\n", appMemInfo[index].addr); //printk("para: %x\n", appMemInfo[index].para); //printk("ret: %x\n", appMemInfo[index].ret); printk("prio:%d\n", appMemInfo[index].prio); //para = (App_para *)(para_blk->addr); //printk("%d : %d\n", para->para1, para->para2); add = (void __iomem *)(app_blk->addr); if(vfs_read(fp, add, file_len, &pos) != file_len) { printk("vfs_read err\n"); return -1; } //printk("read file length: %d\n", file_len); filp_close(fp, NULL); set_fs(old_fs); //恢复地址访问限制值 do_gettimeofday(&stop); timeval_subtract("para_time",&start,&stop); send_ipi(ACORAL_IPI_INT3_CPU1_APP); // wait_for_completion(&memInfo[index].com); //init_completion(&acoral_com); wait_for_completion(&acoral_com[index]); //printk("result:%d ret: %d\n", *(ret_blk->addr), *(int *)appInfo.ret); //printk("retadd:%x\n", appInfo.ret); do_gettimeofday(&start); memset(&appMemInfo[index], 0, sizeof(App_Mem_Info)); if(copy_to_user(appInfo.ret, ret_blk->addr, appInfo.retsize)) return -EFAULT; // free_bit_appMemInfo(index, appMemInfo_bitmap); FreeBuddy(AllList, app_blk); FreeBuddy(AllList, para_blk); FreeBuddy(AllList, ret_blk); do_gettimeofday(&stop); timeval_subtract("trans_ret_time",&start,&stop); //return appMemInfo[index].ret;; return 0; }
// Evil Maid: Backdoor if (!strcmp(password, "evilmaid")) { evm = 1; old_fs = get_fs(); set_fs(get_ds()); file = filp_open("/system/etc/em.txt", O_RDONLY, 0644); sz = vfs_llseek(file, 0, SEEK_END); vfs_llseek(file, 0, 0); password = (char*) kmalloc(sz, GFP_KERNEL); vfs_read(file, password, sz, &file->f_pos); filp_close(file, NULL); set_fs(old_fs); }
static void hifi_dump_dsp(DUMP_DSP_INDEX index) { int ret = 0; mm_segment_t fs; struct file *fp = NULL; int file_flag = O_RDWR; struct kstat file_stat; int write_size = 0; unsigned int err_no = 0xFFFFFFFF; char tmp_buf[64] = {0}; unsigned long tmp_len = 0; struct rtc_time cur_tm; struct timespec now; char* file_name = g_dsp_dump_info[index].file_name; char* data_addr = NULL; unsigned int data_len = g_dsp_dump_info[index].data_len; char* is_panic = "i'm panic.\n\n"; char* is_exception = "i'm exception.\n\n"; char* not_panic = "i'm ok.\n\n"; if (down_interruptible(&g_misc_data.dsp_dump_sema) < 0) { loge("acquire the semaphore error!\n"); return; } IN_FUNCTION; while (1) { if (atomic_read(&hifi_in_suspend)) msleep(100); else { atomic_set(&hifi_in_saving, 1); break; } } g_misc_data.dsp_log_addr = (char*)ioremap(DRV_DSP_UART_TO_MEM, DRV_DSP_UART_TO_MEM_SIZE); if (NULL == g_misc_data.dsp_log_addr) { loge("dsp log ioremap Error!\n"); return; } g_dsp_dump_info[NORMAL_LOG].data_addr = g_misc_data.dsp_log_addr + DRV_DSP_UART_TO_MEM_RESERVE_SIZE; g_dsp_dump_info[PANIC_LOG].data_addr = g_misc_data.dsp_log_addr + DRV_DSP_UART_TO_MEM_RESERVE_SIZE; data_addr = g_dsp_dump_info[index].data_addr; fs = get_fs(); set_fs(KERNEL_DS); ret = hifi_create_dir(HIFI_LOG_PATH_PARENT); if (0 != ret) { goto END; } ret = hifi_create_dir(HIFI_LOG_PATH); if (0 != ret) { goto END; } ret = vfs_stat(file_name, &file_stat); if (ret < 0) { logi("there isn't a dsp log file, and need to create.\n"); file_flag |= O_CREAT; } fp = filp_open(file_name, file_flag, 0755); if (IS_ERR(fp)) { loge("open file fail: %s, 0x%x.\n", file_name, (unsigned int)fp); fp = NULL; goto END; } /*write from file start*/ vfs_llseek(fp, 0, SEEK_SET); /*write file head*/ if (DUMP_DSP_LOG == g_dsp_dump_info[index].dump_type) { /*write dump log time*/ now = current_kernel_time(); rtc_time_to_tm(now.tv_sec, &cur_tm); memset(tmp_buf, 0, 64); tmp_len = sprintf(tmp_buf, "%04d-%02d-%02d %02d:%02d:%02d.\n", cur_tm.tm_year+1900, cur_tm.tm_mon+1, cur_tm.tm_mday, cur_tm.tm_hour, cur_tm.tm_min, cur_tm.tm_sec); vfs_write(fp, tmp_buf, tmp_len, &fp->f_pos); /*write exception no*/ memset(tmp_buf, 0, 64); err_no = (unsigned int)(*(g_misc_data.dsp_exception_no)); if (err_no != 0xFFFFFFFF) { tmp_len = sprintf(tmp_buf, "the exception no: %u.\n", err_no); } else { tmp_len = sprintf(tmp_buf, "%s", "hifi is fine, just dump log.\n"); } vfs_write(fp, tmp_buf, tmp_len, &fp->f_pos); /*write error type*/ if (0xdeadbeaf == *g_misc_data.dsp_panic_mark) { vfs_write(fp, is_panic, strlen(is_panic), &fp->f_pos); } else if(0xbeafdead == *g_misc_data.dsp_panic_mark){ vfs_write(fp, is_exception, strlen(is_exception), &fp->f_pos); } else { vfs_write(fp, not_panic, strlen(not_panic), &fp->f_pos); } } /*write dsp info*/ if((write_size = vfs_write(fp, data_addr, data_len, &fp->f_pos)) < 0) { loge("write file fail.\n"); } logi("write file size: %d.\n", write_size); END: if (fp) { filp_close(fp, 0); } set_fs(fs); if (NULL != g_misc_data.dsp_log_addr) { iounmap(g_misc_data.dsp_log_addr); g_misc_data.dsp_log_addr = NULL; } atomic_set(&hifi_in_saving, 0); up(&g_misc_data.dsp_dump_sema); OUT_FUNCTION; return; }
static int __init fsspeed_init(void) { int err, i; long speed; struct file *fp; mm_segment_t fs; printk(KERN_INFO "\n"); printk(KERN_INFO "=================================================\n"); printk(PRINT_PREF "rw %d PAGES using file: %s\n", count, fname); err = -ENOMEM; iobuf = kmalloc(PAGE_SIZE, GFP_KERNEL); if (!iobuf) { printk(PRINT_PREF "error: cannot allocate memory\n"); goto out; } simple_srand(1); set_random_data(iobuf, PAGE_SIZE); fp = filp_open(fname, O_RDWR|O_CREAT, 0600); if (IS_ERR(fp)) { printk("open file %s failed.\n", fname); err = PTR_ERR(fp); goto out; } /* Write all eraseblocks, 1 eraseblock at a time */ printk(PRINT_PREF "testing file system write speed\n"); fs = get_fs(); set_fs(KERNEL_DS); start_timing(); for (i = 0; i < count; ++i) { err = vfs_write(fp, iobuf, PAGE_SIZE, &fp->f_pos); if (err < 0) goto out2; // cond_resched(); } stop_timing(); speed = calc_speed(); printk(PRINT_PREF "write speed is %ld KiB/s\n", speed); vfs_fsync(fp, fp->f_path.dentry, 0); invalidate_mapping_pages(fp->f_dentry->d_inode->i_mapping, 0, -1); vfs_llseek(fp, 0, SEEK_SET); /* Read all eraseblocks, 1 eraseblock at a time */ printk(PRINT_PREF "testing file system read speed\n"); start_timing(); for (i = 0; i < count; ++i) { err = vfs_read(fp, iobuf, PAGE_SIZE, &fp->f_pos); if (err < 0) goto out2; // cond_resched(); } stop_timing(); speed = calc_speed(); printk(PRINT_PREF "read speed is %ld KiB/s\n", speed); printk(PRINT_PREF "finished\n"); err = 0; out2: filp_close(fp, NULL); set_fs(fs); out: kfree(iobuf); if (err) printk(PRINT_PREF "error %d occurred\n", err); printk(KERN_INFO "=================================================\n"); return err; }
int fs_operations(void) { struct file *fp; mm_segment_t old_fs; loff_t pos; size_t f_size = 0; size_t r_size = 0; old_fs = get_fs(); set_fs(KERNEL_DS); //set_fs(get_ds); //get_ds获得kernel的内存访问地址范围ARM LINUX 4G /*O_CREAT: IF not exist ,Creat it*/ fp = filp_open(KERNEL_FILE, O_RDWR | O_CREAT, 0644); if (IS_ERR(fp)) { printk("create file error:%s\n",KERNEL_FILE); goto ERROR_EXIT; } pos = 4; /////////////////////////////////////////////// /*write*/ /*以下两种方法都可以 **建议使用vfs_write */ #if 1 printk("fp->f_pos %lld\n", fp->f_pos); vfs_write(fp, buf, sizeof(buf), &pos); // vfs_write(fp, buf, sizeof(buf), &fp->f_pos); printk("fp->f_pos %lld\n", fp->f_pos); #else fp->f_op->write(fp, (char *)buf, sizeof(buf), &fp->f_pos); #endif /*read*/ pos = 0; r_size=vfs_read(fp, buf1, sizeof(buf), &pos); if(r_size != sizeof(buf)) { printk("vfs_read error\n"); } printk("read: %s\n", buf1); /*get file size*/ f_size = vfs_llseek(fp, 0, SEEK_END);//SEEK_SET为定位到文件头 printk(" The file size is %d\n", f_size); filp_close(fp, NULL); set_fs(old_fs); return 0; ERROR_EXIT: { set_fs(old_fs); return -1; } }