1

My linux kernel is "5.11.0-36-generic".

I want to read parameter from a file named "myTest.txt".

after make, terminal says:

error: implicit declaration of function ‘get_fs’; did you mean ‘sget_fc’? [-Werror=implicit-function-declaration]

I have searched for answers, and found that article: Saying goodbye to set_fs()..

Can anyone tell me how to replace it?

#include <linux/module.h>
#include <linux/init.h>

#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/string.h>
#include <linux/slab.h>
int __init hello_init(void)  
{  
    unsigned char buf1[12]="hello world.";
    unsigned char buf2[12]="kernel file.";
    struct file *fp;  
    mm_segment_t fs;  
    loff_t pos; 
    printk("hello enter\n");  
    fp = filp_open("/home/kernel_file", O_RDWR | O_CREAT, 0644);  
    if (IS_ERR(fp)) {  
        printk("create file error\n");  
        return -1;  
    } 
    fs = get_fs();  //------------------------wrong !!
    set_fs(KERNEL_DS);//------------------------wrong !!
    pos = fp->f_pos; 
    vfs_write(fp, buf1, sizeof(buf1), &pos);  
    fp->f_pos = pos;
    pos = fp->f_pos; 
    vfs_write(fp, buf2, sizeof(buf2), &pos);  
    fp->f_pos = pos;  
    set_fs(fs); //------------------------wrong !!
    filp_close(fp, NULL);  
    return 0;  
}  
void __exit hello_exit(void)  
{  
    printk(" exit\n");  
}  
module_init(hello_init);  
module_exit(hello_exit);  
MODULE_LICENSE("GPL");  

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
liu
  • 11
  • 1
  • Since 4.14 Linux kernel provides functions `kernel_read` and `kernel_write` exclusively for a file access within the kernel. These functions doesn't require `set_fs`. See that my answer: https://stackoverflow.com/a/53917617/3440745 – Tsyvarev Sep 28 '21 at 07:16

0 Answers0