I am writing a Linux kernel module. Create directories along the way:
/etc/data_stor/user/id/{0001}/etc/data_stor/user/id/{0002}/etc/data_stor/user/id/{0003}/etc/data_stor/user/id/{0004}
Create the same set of files in these directories with the names Name, Family, Age, etc. and write down the necessary information there. I create directories as follows:
const char* path = /etc/data_stor/user/id/{0001};create_path(path);static int dev_mkdir(const char *name, umode_t mode){ struct dentry *dentry; struct path path; int err;dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);if (IS_ERR(dentry)) return PTR_ERR(dentry);err = vfs_mkdir(d_inode(path.dentry), dentry, mode);done_path_create(&path, dentry);return err;}static int create_path(const char *nodepath){ char *path; char *s; int err = 0;path = kstrdup(nodepath, GFP_KERNEL);if (!path) return -ENOMEM;s = path;for (;;) { s = strchr(s, '/'); if (!s) break; s[0] = '\0'; err = dev_mkdir(path, 0777); if (err && err != -EEXIST) break; s[0] = '/'; s++;}kfree(path);return err;}
And I create files by function:
hFile = filp_open(Path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);if (IS_ERR(hFile)){ printk("write_dword open file failed %s %ld\n", Path, PTR_ERR(hFile)); return PTR_ERR(hFile);}kernel_write(hFile, data, sizeof(DWORD), &foff);filp_close(hFile, 0);
Directories and information files are successfully created, but there are some problems:
- When manually deleting directories with files from the file manager, the system sometimes crashes (kernel panic).
- When trying to read information from some files, the
filp_open
function returns a -ENOENT error. The file or directory was not found, although they are present.
It is also required to delete all directories when prompted with user-mode. I wrote a function, but it only removes an empty directory, i.e. it should not have files.
I ask you to help with these questions, explain in which direction to move, or give a specific example or literature.