1

I am working on the FAT file system code in Linux Kernel 3.3. For a project, I need to create a file at mount time, in the root directory of the mounted filesystem. I've figured out where to write the code to create it, but have a few questions about creating. (The function is vfat_mount in linux/fs/fat/namei_vfat.c).

The mount function doesn't seem to have the mount point as a path, but I believe the dentry of the root directory is stored in the super block. I am not sure how to create a file using this available data. The open() system call doesn't seem to be an option, so should I use the file system specific vfat_create for this purpose? If so, can some one help me with more details or some examples.

Also, the file needs to span a few clusters, and I do not want the file system to search for and allocate free clusters to it. So is there a way to create the file that way directly, or should I use something like fat_add_cluster() after creating the file?

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
Sankarnm
  • 111
  • 1
  • 7
  • Consider if it might be more appropriate to do this by adding or modifying a userspace tool or daemon. – Chris Stratton May 03 '12 at 05:14
  • I didn't quite understand what you meant. But if I do make any change in anything beyond the filesystem level, the other filesytems will be affected. – Sankarnm May 03 '12 at 17:26
  • I mean do not try to do this by changing the kernel at all. Do it entirely in userspace - ie, with a program or daemon. Accessing actual disk files from inside the kernel is strongly discouraged. – Chris Stratton May 03 '12 at 17:36
  • But my project is to change the FAT file system To add a little more functionality. Hence this change is necessary for me. – Sankarnm May 04 '12 at 04:41
  • Why is that your project? What knowledge of the linux kernel design philosophy was the decision to go with this approach based on? – Chris Stratton May 04 '12 at 05:00
  • 1
    To quote GKH, "Now, armed with this newfound knowledge of how to abuse the kernel system call API and annoy a kernel programmer at the drop of a hat, you really can push your luck and write to a file from within the kernel. " - see the rest at http://www.linuxjournal.com/article/8110 – Chris Stratton May 04 '12 at 05:12
  • Okay, thanks for that. So if writing to files is ill adviced in the kernel, is there a way I can use the reserved sectors of the disk as a file? – Sankarnm May 04 '12 at 13:25

1 Answers1

0

You can directly call the filp_open() function defined and exported in fs/open.c.It is in fact called by the open() syscall and therefore has the same arguments.No need to call file system specific functions.Similarly once you're done with the file, close it with filp_close().

itisravi
  • 3,406
  • 3
  • 23
  • 30
  • But as I said earlier, these need the full path of the file to be created right? And since I have to do it at mount time, it has to be in the file system specific mount function, i.e. `vfat_mount()` in this case. Inside this function, there doesn't seem to be any access to the path at which the file system is mounted. The mount point is available in the function that calls `vfat_mount()`. So is there something that can be done? – Sankarnm May 02 '12 at 14:56
  • I tried `filp_open()` with just the file name, and it creates the file in the pwd which happens to be in the root/home directory of the kernel's default file system, not in the file system I mounted. – Sankarnm May 02 '12 at 15:13