static int write_pat_on_cpus(efx_qword_t* pat) { int rc = write_pat(pat); if( rc != 0 ) return rc; smp_call_function((void(*)(void*))write_pat_on_cpu, pat, 1, 1); return 0; }
/* function to handle files string operation */ int strops_files(void *arg){ int rc = 0; struct strops_args *ptr = (struct strops_args *)arg; struct strops_args *kptr = NULL; /*check validation of input */ rc = isInputValid(ptr); if(rc < 0) goto out; /*copy_from_user into kernel */ kptr = (struct strops_args *)kmalloc(sizeof(struct strops_args), GFP_KERNEL); if(IS_ERR(kptr)){ rc = -ENOMEM; goto out; } if(copy_from_user(kptr, ptr, sizeof(struct strops_args))){ rc = -EFAULT; goto free_kptr; } /* begin file strings operation mode*/ rc = search_pat(kptr); if(rc < 0) goto free_kptr; if(kptr->flag == 1 || kptr->flag == 2){ if(kptr->res_len == 0){ printk("No found pattern; Operation quit.\n"); goto free_kptr; } rc = write_pat(kptr, 0); if(rc < 0) goto free_kptr; } if(copy_to_user(ptr, kptr, sizeof(struct strops_args))){ rc = -EFAULT; goto free_kptr; } free_kptr: kfree(kptr); out: return rc; }
static void write_pat_on_cpu(efx_qword_t* pat) { write_pat(pat); }
/*write pattern*/ int write_pat(void *arg, int mode){ int i; int rc = 0; struct strops_args *kptr = (struct strops_args *)arg; struct file *readFilePtr = NULL;/*for input file pointer.*/ size_t inputInodeSize = 0;/* for get size of input file*/ struct file *writeFilePtr = NULL;/*for output file pointer.*/ char *out_dir = "temp.txt"; mm_segment_t oldfs; char *bytes;/* bytes from input file*/ char *temp; if(mode == 0){ /* write to temp file*/ readFilePtr = filp_open(kptr->in_file, O_EXCL, 0); } else{ readFilePtr = filp_open(out_dir, O_EXCL, 0); } if(!readFilePtr || IS_ERR(readFilePtr)){ printk("Open input file error: %d\n", (int)PTR_ERR(readFilePtr)); rc = -ENOENT; readFilePtr = NULL; goto out; } rc = isInFileValid(readFilePtr); if(rc < 0) goto close_input_file; inputInodeSize = i_size_read(readFilePtr->f_path.dentry->d_inode); /*check whether can open:*/ if(mode == 0){ writeFilePtr = filp_open(out_dir, O_WRONLY|O_CREAT|O_TRUNC, 0644); }else{ writeFilePtr = filp_open(kptr->in_file, O_WRONLY|O_CREAT|O_TRUNC, 0644); } if(!writeFilePtr || IS_ERR(writeFilePtr)){ printk("Open output file error: %d\n", (int)PTR_ERR(writeFilePtr)); rc = -ENOENT; writeFilePtr = NULL; goto close_input_file; } rc = isOutFileValid(writeFilePtr); if(rc < 0) goto close_output_file; bytes = (char *)kmalloc(PAGE * sizeof(char) + 1, GFP_KERNEL); if(IS_ERR(bytes)){ rc = -ENOMEM; goto close_output_file; } temp = (char *)kmalloc(PAGE * sizeof(char) + 1, GFP_KERNEL); if(IS_ERR(temp)){ rc = -ENOMEM; goto free_bytes; } oldfs = get_fs(); set_fs(get_ds()); if(mode == 0){/*write new file to temp*/ if(kptr->flag == 1){/* delete pattern*/ char *index; int page_count = 1; int dist = 0; while((inputInodeSize - readFilePtr->f_pos) > 0){ if(inputInodeSize - readFilePtr->f_pos >= PAGE){ int pos = readFilePtr->f_pos; dist = 0; rc = readFilePtr->f_op->read(readFilePtr, bytes, PAGE, &readFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Read Blocks failed!\n"); goto set_oldfs; } bytes[PAGE] = '\0'; index = bytes; for(i = 0; i < kptr->res_len; i++){ if(kptr->res[i] < pos) continue; if(kptr->res[i] > page_count * PAGE) continue; dist = kptr->res[i] % PAGE - (index - bytes); strncpy(temp, index, dist); temp[dist] = '\0'; index += dist + kptr->old_len; rc = writeFilePtr->f_op->write(writeFilePtr, temp ,strlen(temp), &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } } strncpy(temp, index, PAGE - (index - bytes)); temp[PAGE - (index - bytes)] = '\0'; rc = writeFilePtr->f_op->write(writeFilePtr, temp ,strlen(temp), &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } page_count++; }else{ int rest = inputInodeSize - readFilePtr->f_pos; int pos = readFilePtr->f_pos; dist = 0; rc = readFilePtr->f_op->read(readFilePtr, bytes, rest, &readFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Read Blocks failed!\n"); goto set_oldfs; } bytes[rest] = '\0'; index = bytes; for(i = 0; i < kptr->res_len; i++){ if(kptr->res[i] < pos) continue; dist = kptr->res[i] % PAGE - (index - bytes); strncpy(temp, index, dist); temp[dist] = '\0'; index += dist + kptr->old_len; rc = writeFilePtr->f_op->write(writeFilePtr, temp ,strlen(temp), &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } } strncpy(temp, index, rest - (index - bytes)); temp[rest - (index - bytes)] = '\0'; rc = writeFilePtr->f_op->write(writeFilePtr, temp ,strlen(temp), &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } } } } if(kptr->flag == 2){/* replace pattern */ char *index; int page_count = 1; int dist = 0; while((inputInodeSize - readFilePtr->f_pos) > 0){ if(inputInodeSize - readFilePtr->f_pos >= PAGE){ int pos = readFilePtr->f_pos; dist = 0; rc = readFilePtr->f_op->read(readFilePtr, bytes, PAGE, &readFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Read Blocks failed!\n"); goto set_oldfs; } bytes[PAGE] = '\0'; index = bytes; for(i = 0; i < kptr->res_len; i++){ if(kptr->res[i] < pos) continue; if(kptr->res[i] > page_count * PAGE) continue; dist = kptr->res[i] % PAGE - (index - bytes); strncpy(temp, index, dist); temp[dist] = '\0'; index += dist + kptr->old_len; rc = writeFilePtr->f_op->write(writeFilePtr, temp ,strlen(temp), &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } strncpy(temp, kptr->new_str, kptr->new_len); temp[kptr->new_len] = '\0'; rc = writeFilePtr->f_op->write(writeFilePtr, temp ,strlen(temp), &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } } strncpy(temp, index, PAGE - (index - bytes)); temp[PAGE - (index - bytes)] = '\0'; rc = writeFilePtr->f_op->write(writeFilePtr, temp ,strlen(temp), &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } page_count++; }else{ int rest = inputInodeSize - readFilePtr->f_pos; int pos = readFilePtr->f_pos; dist = 0; rc = readFilePtr->f_op->read(readFilePtr, bytes, rest, &readFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Read Blocks failed!\n"); goto set_oldfs; } bytes[rest] = '\0'; index = bytes; for(i = 0; i < kptr->res_len; i++){ if(kptr->res[i] < pos) continue; dist = kptr->res[i] % PAGE - (index - bytes); strncpy(temp, index, dist); temp[dist] = '\0'; index += dist + kptr->old_len; rc = writeFilePtr->f_op->write(writeFilePtr, temp ,strlen(temp), &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } strncpy(temp, kptr->new_str, kptr->new_len); temp[kptr->new_len] = '\0'; rc = writeFilePtr->f_op->write(writeFilePtr, temp ,strlen(temp), &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } } strncpy(temp, index, rest - (index - bytes)); temp[rest - (index - bytes)] = '\0'; rc = writeFilePtr->f_op->write(writeFilePtr, temp ,strlen(temp), &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } } } } /*goto mode 1 to write back*/ rc = write_pat(kptr, 1); } if(mode == 1){/*write temp to new file*/ while((inputInodeSize - readFilePtr->f_pos) > 0){ if(inputInodeSize - readFilePtr->f_pos >= PAGE){ rc = readFilePtr->f_op->read(readFilePtr, bytes, PAGE, &readFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Read Blocks failed!\n"); goto set_oldfs; } bytes[PAGE] = '\0'; rc = writeFilePtr->f_op->write(writeFilePtr, bytes ,PAGE, &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } }else{ int rest = inputInodeSize - readFilePtr->f_pos; rc = readFilePtr->f_op->read(readFilePtr, bytes, rest, &readFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Read Blocks failed!\n"); goto set_oldfs; } bytes[rest] = '\0'; rc = writeFilePtr->f_op->write(writeFilePtr, bytes ,rest, &writeFilePtr->f_pos); if(rc < 0){ rc = -EPERM; printk("Write the hash key to header of output file reading failed!\n"); goto set_oldfs; } } } if(kptr->flag == 1){printk("Deletion Succeed!\n"); kptr->flag = -1;} if(kptr->flag == 2){printk("Replacement Succeed!\n");kptr->flag = -2;} if ((rc = vfs_unlink(readFilePtr->f_path.dentry->d_parent->d_inode, readFilePtr->f_path.dentry, NULL)) < 0) printk("vfs_unlink failed\n"); } set_oldfs: set_fs(oldfs); kfree(temp); free_bytes: kfree(bytes); close_output_file: filp_close(writeFilePtr, NULL); close_input_file: filp_close(readFilePtr, NULL); out: return rc; }