0

Different between get_fs and sget_fc

I tried to use it for read file in Linux kernel

BTW, How do I read file which the file type is txt file in Linux Module

I know that trying to read or write file in Linux Kernel is a stupid action.

Can someone answer the question ? thx

j0000@ubuntu:~/Desktop/file$ make
make -C /usr/src/linux-headers-5.11.0-37-generic/ M=/home/j0000/Desktop/file modules
make[1]: Entering directory '/usr/src/linux-headers-5.11.0-37-generic'
  CC [M]  /home/j0000/Desktop/file/reverse.o
/home/j0000/Desktop/file/reverse.c: In function ‘test_init’:
/home/j0000/Desktop/file/reverse.c:20:9: error: implicit declaration of function ‘get_fs’; did you mean ‘sget_fc’? [-Werror=implicit-function-declaration]
   20 |     fs =get_fs();
      |         ^~~~~~
      |         sget_fc
/home/j0000/Desktop/file/reverse.c:20:9: error: incompatible types when assigning to type ‘mm_segment_t’ {aka ‘struct <anonymous>’} from type ‘int’
/home/j0000/Desktop/file/reverse.c:21:5: error: implicit declaration of function ‘set_fs’; did you mean ‘sget_fc’? [-Werror=implicit-function-declaration]
   21 |     set_fs(KERNEL_DS);
      |     ^~~~~~
      |     sget_fc
/home/j0000/Desktop/file/reverse.c:21:12: error: ‘KERNEL_DS’ undeclared (first use in this function); did you mean ‘KERNFS_NS’?
   21 |     set_fs(KERNEL_DS);
      |            ^~~~~~~~~
      |            KERNFS_NS
/home/j0000/Desktop/file/reverse.c:21:12: note: each undeclared identifier is reported only once for each function it appears in
cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:288: /home/j0000/Desktop/file/reverse.o] Error 1
make[1]: *** [Makefile:1849: /home/j0000/Desktop/file] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.11.0-37-generic'
make: *** [Makefile:8: default] Error 2

Updated my code :

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Reversing text from Jackson");
MODULE_VERSION("0.1");

void *in_buf;
const void *out_buf = "add" ;

static int __init test_start(void){
        struct file *i_fp, *o_fp;
        loff_t pos;
        size_t count1, count2;
        printk(KERN_INFO "start converting...\n");
        i_fp = filp_open("/home/j0000/Desktop/file/input.txt",O_RDWR | O_CREAT, 0644);
        o_fp = filp_open("/home/j0000/Desktop/file/output.txt",O_RDWR | O_CREAT, 0644);
        if (IS_ERR(i_fp)){
                printk(KERN_INFO "intput file open error/n");
                return -1;
        }
        if (IS_ERR(o_fp)){
                printk(KERN_INFO "output file open error/n");
                return -1;
        }
        pos = 0;
        kernel_read(i_fp, in_buf, count1, &pos);
        pos = 0;
        kernel_write(o_fp, out_buf, count2, &pos);
        filp_close(i_fp, NULL);
        filp_close(o_fp, NULL);
        return 0;
}

static void __exit test_end(void){
        printk(KERN_INFO "Ending~\n");
}

module_init(test_start);
module_exit(test_end);
jackson
  • 11
  • 3
  • 9
  • `set_fs`/`get_fs` are not longer existed in the "default" configuration of the Linux kernel. "How do I read file which the file type is txt file in Linux Module" - Use `kernel_read` function, as in that [my answer](https://stackoverflow.com/a/53917617/3440745). – Tsyvarev Oct 16 '21 at 18:28
  • @Tsyvarev ok, I'll try it – jackson Oct 18 '21 at 11:43
  • @Tsyvarev sorry for asking again the question. Can you explain how to use it ? – jackson Oct 19 '21 at 13:53
  • @Tsyvarev How should I open file without file_open ? – jackson Oct 19 '21 at 13:54
  • A file could be opened with [filp_open](https://elixir.bootlin.com/linux/latest/source/fs/open.c#L1163). Using it doesn't require `set_fs`. – Tsyvarev Oct 19 '21 at 14:03
  • @Tsyvarev Hello, first of all thanks for answering my question. – jackson Oct 19 '21 at 14:31
  • And I have updated my code for it. It can makefile, but I wonder that the code is actually read the text file or not? – jackson Oct 19 '21 at 14:34

0 Answers0