1e41f4b71Sopenharmony_ci# Supported File Systems 2e41f4b71Sopenharmony_ci## FAT 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci### Basic Concepts 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciFile Allocation Table (FAT) is a file system developed for personal computers. It consists of the DOS Boot Record (DBR) region, FAT region, and Data region. Each entry in the FAT region records information about the corresponding cluster in the storage device. The cluster information includes whether the cluster is used, number of the next cluster of the file, whether the file ends with the cluster. The FAT file system supports multiple formats, such as FAT12, FAT16, and FAT32. The numbers 12, 16, and 32 indicate the number of bits per cluster within the FAT, and also restrict the maximum file size in the system. The FAT file system supports multiple media, especially removable media (such as USB flash drives, SD cards, and removable hard drives). The FAT file system ensures good compatibility between embedded devices and desktop systems (such as Windows and Linux) and facilitates file management. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciThe OpenHarmony kernel supports FAT12, FAT16, and FAT32 file systems. These file systems require a tiny amount of code to implement, use less resources, support a variety of physical media, and are tailorable and compatible with Windows and Linux systems. They also support identification of multiple devices and partitions. The kernel supports multiple partitions on hard drives and allows creation of the FAT file system on the primary partition and logical partition. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci### Working Principles 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ciThis document does not include the FAT design and physical layout. You can find a lot of reference on the Internet. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ciThe OpenHarmony LiteOS-A kernel uses block cache (Bcache) to improve FAT performance. When read and write operations are performed, Bcache caches the sectors close to the read and write sectors to reduce the number of I/Os and improve performance. The basic cache unit of Bcache is block. The size of each block is the same. By default, there are 28 blocks, and each block caches data of 64 sectors. When the Bcache dirty block rate (number of dirty sectors/total number of sectors) reaches the threshold, writeback is triggered and cached data is written back to disks. You can manually call **sync** and **fsync** to write data to disks if you want. Some FAT APIs (such as **close** and **umount**) may also trigger writeback operations. However, you are advised not to use them to trigger writeback. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci### Development Guidelines 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci **How to Develop** 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciThe development process involves mounting partitions, managing files and directories, and unmounting partitions. 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ciThe device name of the SD card or MMC is **mmcblk[x]p[y]**, and the file system type is **vfat**. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ciExample: 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci``` 32e41f4b71Sopenharmony_cimount("/dev/mmcblk0p0", "/mnt", "vfat", 0, NULL); 33e41f4b71Sopenharmony_ci``` 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci>  **NOTE**<br> 36e41f4b71Sopenharmony_ci> - The size of a single FAT file cannot be greater than 4 GiB. 37e41f4b71Sopenharmony_ci> 38e41f4b71Sopenharmony_ci> - When there are two SD card slots, the first card inserted is card 0, and that inserted later is card 1. 39e41f4b71Sopenharmony_ci> 40e41f4b71Sopenharmony_ci> - When multi-partition is enabled and there are multiple partitions, the device node **/dev/mmcblk0** (primary device) registered by card 0 and **/dev/mmcblk0p0** (secondary device) are the same device. In this case, you cannot perform operations on the primary device. 41e41f4b71Sopenharmony_ci> 42e41f4b71Sopenharmony_ci> - Before removing an SD card, close the open files and directories and unmount the related nodes. Otherwise, SD card exceptions or memory leaks may occur. 43e41f4b71Sopenharmony_ci> 44e41f4b71Sopenharmony_ci> - Before performing the **format** operation, unmount the mount point. 45e41f4b71Sopenharmony_ci> 46e41f4b71Sopenharmony_ci> - After the Bcache feature takes effect, note the following: 47e41f4b71Sopenharmony_ci> - When **MS_NOSYNC** is carried in the **mount** function, FAT does not proactively write the content in the cache back to the storage device. The FAT-related APIs **open**, **close**, **unlink**, **rename**, **mkdir**, **rmdir**, and **truncate** do not automatically perform the **sync** operation, which improves the operation speed. However, the upper layer must actively invoke the **sync** operation to synchronize data. Otherwise, data loss may occur. 48e41f4b71Sopenharmony_ci> 49e41f4b71Sopenharmony_ci> - Bcache provides scheduled writeback. After **LOSCFG_FS_FAT_CACHE_SYNC_THREAD** is enabled in **menuconfig**, the OpenHarmony kernel creates a scheduled task to write the Bcache data back to disks. By default, the kernel checks the dirty block rate in the Bcache every 5 seconds. If the dirty block rate exceeds 80%, the **sync** operation will be performed to write all dirty data in the Bcache to disks. You can call **LOS_SetSyncThreadPrio**, **LOS_SetSyncThreadInterval**, and **LOS_SetDirtyRatioThreshold** to set the task priority, flush interval, and dirty block rate threshold, respectively. 50e41f4b71Sopenharmony_ci> - The cache has 28 blocks by default, and each block has 64 sectors. 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci## JFFS2 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci### Basic Concepts 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ciJournalling Flash File System Version 2 (JFFS2) is a log-structured file system designed for Memory Technology Devices (MTDs). 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ciJFFS2 is used on the NOR flash memory of the OpenHarmony. JFFS2 is readable and writable, supports data compression, provides crash or power failure protection, and supports wear leveling. There are many differences between flash memory and disk media. Running a disk file system on a flash memory device will cause performance and security problems. JFFS2 is a file system optimized for flash memory. 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci### Working Principles 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ciFor details about the physical layout of the JFFS2 file system on the storage device and the specifications of the file system, visit https://sourceware.org/jffs2/. 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ciThe following describes several important mechanisms and features of JFFS2 that you may concern. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci1. Mount mechanism and speed: According to the JFFS2 design, all files are divided into nodes of different sizes based on certain rules and stored on the flash memory device in sequence. In the mount process, all node information needs to be obtained and cached in the memory. Therefore, the mount speed is in linear proportion to the flash device capacity and the number of files. This is a native design issue of JFFS2. To increase the mount speed, you can select **Enable JFFS2 SUMMARY** during kernel compilation. If this option is selected, information required by the mount operation will be stored to the flash memory in advance. When the mount operation is performed, this information can be read and parsed quickly, ensuring relatively constant mount speed. However, this space-for-time practice consumes about 8% extra space. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci2. Wear leveling: Due to the physical attributes of flash memory devices, read and write operations can be performed only on blocks of a specific size. To prevent certain blocks from being severely worn, wear leveling is used on written blocks in JFFS2 to ensure relatively balanced writes on all blocks. This prolongs the overall service life of the flash memory devices. 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci3. Garbage collection (GC) mechanism: When a deletion operation is performed in JFFS2, the physical memory is not released immediately. An independent GC thread performs GC operations such as space defragmentation and migration. However, GC in JFFS2 affects instantaneous read/write performance, like all GC mechanisms. In addition, JFFS2 reserves about three blocks in each partition for space defragmentation. The reserved space is invisible to users. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci4. Compression mechanism: The underlying layer automatically decompresses or compresses the data read or written each time in JFFS2. The actual I/O size is different from the read or write size requested by the user. You cannot estimate whether the write operation will succeed or not based on the size of the written data and the remaining space of the flash memory. 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci5. Hard link mechanism: JFFS2 supports hard links. Multiple hard links of the same file occupy physical memory space of only one hard link. The physical space is released only when all hard links are deleted. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci### Development Guidelines 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ciThe development based on JFFS2 and NOR flash memory is similar to the development based on other file systems because the VFS shields the differences of specific file systems and the standard POSIX APIs are used as external APIs. 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ciThe raw NOR flash device has no place to centrally manage and record partition information. Therefore, you need to transfer the partition information by using other configuration methods (using the **bootargs** parameter during image burning), call the corresponding API in the code to add partitions, and then mount the partitions. 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci**Creating a JFFS2 Image** 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ciUse the **mkfs.jffs2** tool to create an image. The default page size is 4 KiB, and the default **eraseblock** size is 64 KiB. Modify the parameter values to match your development. 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci``` 91e41f4b71Sopenharmony_ci./mkfs.jffs2 -d rootfs/ -o rootfs.jffs2 92e41f4b71Sopenharmony_ci``` 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci **Table 1** Command description (run **mkfs.jffs2 --help** to view more details) 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci| Command| Description| 97e41f4b71Sopenharmony_ci| -------- | -------- | 98e41f4b71Sopenharmony_ci| -s | Specifies the page size. If this parameter is not specified, the default value **4KiB** is used.| 99e41f4b71Sopenharmony_ci| -e | Specifies the **eraseblock** size. If this parameter is not specified, the default value **64KiB** is used.| 100e41f4b71Sopenharmony_ci| -p | Specifies the image size. 0xFF is filled at the end of the image file to make the file to the specified size. If the size is not specified, 0xFF is filled to a value aligned with **eraseblock**.| 101e41f4b71Sopenharmony_ci| -d | Specifies the source directory of the file system image.| 102e41f4b71Sopenharmony_ci| -o | Specifies the image name.| 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci**Mounting a JFFS2 Partition** 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ciCall **int mount(const char \*source, const char \*target, const char \*filesystemtype, unsigned long mountflags, const void \*data)** to mount the device node and mount point. 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ciThis function has the following parameters: 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci- **const char \*source** specifies the device node. 111e41f4b71Sopenharmony_ci- **const char \*target** specifies the mount point. 112e41f4b71Sopenharmony_ci- **const char \*filesystemtype** specifies the file system type. 113e41f4b71Sopenharmony_ci- **unsigned long mountflags** specifies the mount flag, which is **0** by default. 114e41f4b71Sopenharmony_ci- **const void \*data** specifies the data, which is **NULL** by default. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ciYou can also run the **mount** command in **shell** to mount a JFFS2 partition. In this case, you do not need to specify the last two parameters. 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ciRun the following command: 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci``` 122e41f4b71Sopenharmony_ciOHOS # mount /dev/spinorblk1 /jffs1 jffs2 123e41f4b71Sopenharmony_ci``` 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ciIf the following information is displayed, the JFFS2 partition is mounted: 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci``` 129e41f4b71Sopenharmony_ciOHOS # mount /dev/spinorblk1 /jffs1 jffs2 130e41f4b71Sopenharmony_cimount OK 131e41f4b71Sopenharmony_ci``` 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ciNow, you can perform read and write operations on the NOR flash memory. 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci**Unmounting a JFFS2 Partition** 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ciCall **int umount(const char \*target)** to unmount a partition. You only need to specify the correct mount point. 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ciRun the following command: 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci``` 143e41f4b71Sopenharmony_ciOHOS # umount /jffs1 144e41f4b71Sopenharmony_ci``` 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ciIf the following information is displayed, the JFFS2 partition is unmounted: 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci``` 150e41f4b71Sopenharmony_ciOHOS # umount /jffs1 151e41f4b71Sopenharmony_ciumount ok 152e41f4b71Sopenharmony_ci``` 153e41f4b71Sopenharmony_ci## NFS 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci### Basic Concepts 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ciNetwork File System (NFS) allows you to share files across hosts and OSs over a network. You can treat NFS as a file system service, which is equivalent to folder sharing in the Windows OS to some extent. 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci### Working Principles 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ciThe NFS of the OpenHarmony LiteOS-A kernel acts as an NFS client. The NFS client can mount the directory shared by a remote NFS server to the local machine and run the programs and shared files without occupying the storage space of the current system. To the local machine, the directory on the remote server is like its disk. 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci### Development Guidelines 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci1. Create an NFS server. 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci The following uses the Ubuntu OS as an example to describe how to configure an NFS server. 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci - Install the NFS server software. 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci Set the download source of the Ubuntu OS when the network connection is normal. 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci ``` 178e41f4b71Sopenharmony_ci sudo apt-get install nfs-kernel-server 179e41f4b71Sopenharmony_ci ``` 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci - Create a directory for mounting and assign full permissions for the directory. 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci ``` 185e41f4b71Sopenharmony_ci mkdir -p /home/sqbin/nfs 186e41f4b71Sopenharmony_ci sudo chmod 777 /home/sqbin/nfs 187e41f4b71Sopenharmony_ci ``` 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci - Configure and start the NFS server. 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci Append the following line in the **/etc/exports** file: 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci ``` 195e41f4b71Sopenharmony_ci /home/sqbin/nfs *(rw,no_root_squash,async) 196e41f4b71Sopenharmony_ci ``` 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci **/home/sqbin/nfs** is the root directory shared by the NFS server. 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci Start the NFS server. 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci ``` 204e41f4b71Sopenharmony_ci sudo /etc/init.d/nfs-kernel-server start 205e41f4b71Sopenharmony_ci ``` 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci Restart the NFS server. 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci ``` 211e41f4b71Sopenharmony_ci sudo /etc/init.d/nfs-kernel-server restart 212e41f4b71Sopenharmony_ci ``` 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci2. Configure the board as an NFS client. 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci In this section, the NFS client is a device running the OpenHarmony kernel. 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci - Set the hardware connection. 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci Connect the OpenHarmony kernel device to the NFS server. Set their IP addresses in the same network segment. For example, set the IP address of the NFS server to **10.67.212.178/24** and the IP address of the OpenHarmony kernel device to 221e41f4b71Sopenharmony_ci **10.67.212.3/24**. Note that this IP address is an intranet private IP address. Use the actual IP address. 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci You can run the **ifconfig** command to check the OpenHarmony kernel device's IP address. 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci - Start the network and ensure that the network between the board and NFS server is normal. 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci Start the Ethernet or another type of network, and then run **ping** to check whether the network connection to the server is normal. 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci ``` 231e41f4b71Sopenharmony_ci OHOS # ping 10.67.212.178 232e41f4b71Sopenharmony_ci [0]Reply from 10.67.212.178: time=1ms TTL=63 233e41f4b71Sopenharmony_ci [1]Reply from 10.67.212.178: time=0ms TTL=63 234e41f4b71Sopenharmony_ci [2]Reply from 10.67.212.178: time=1ms TTL=63 235e41f4b71Sopenharmony_ci [3]Reply from 10.67.212.178: time=1ms TTL=63 236e41f4b71Sopenharmony_ci --- 10.67.212.178 ping statistics --- 237e41f4b71Sopenharmony_ci packets transmitted, 4 received, 0 loss 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci Initialize the NFS client. 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci ``` 243e41f4b71Sopenharmony_ci OHOS # mkdir /nfs 244e41f4b71Sopenharmony_ci OHOS # mount 10.67.212.178:/home/sqbin/nfs /nfs nfs 1011 1000 245e41f4b71Sopenharmony_ci ``` 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci If the following information is displayed, the NFS client is initialized. 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci ``` 251e41f4b71Sopenharmony_ci OHOS # mount 10.67.212.178:/home/sqbin/nfs /nfs nfs 1011 1000 252e41f4b71Sopenharmony_ci Mount nfs on 10.67.212.178:/home/sqbin/nfs, uid:1011, gid:1000 253e41f4b71Sopenharmony_ci Mount nfs finished. 254e41f4b71Sopenharmony_ci ``` 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci This command mounts the **/home/sqbin/nfs** directory on the NFS server (IP address: 10.67.212.178) to the **/nfs** directory on the OpenHarmony kernel device. 257e41f4b71Sopenharmony_ci 258e41f4b71Sopenharmony_ci >  **NOTE**<br> 259e41f4b71Sopenharmony_ci > This example assumes that the NFS server is available, that is, the **/home/sqbin/nfs** directory on the NFS server 10.67.212.178 is accessible. 260e41f4b71Sopenharmony_ci > 261e41f4b71Sopenharmony_ci > The **mount** command format is as follows: 262e41f4b71Sopenharmony_ci > 263e41f4b71Sopenharmony_ci > 264e41f4b71Sopenharmony_ci > ``` 265e41f4b71Sopenharmony_ci > mount <SERVER_IP:SERVER_PATH> <CLIENT_PATH> nfs 266e41f4b71Sopenharmony_ci > ``` 267e41f4b71Sopenharmony_ci > 268e41f4b71Sopenharmony_ci > **SERVER_IP** indicates the IP address of the server. <br>**SERVER_PATH** indicates the path of the shared directory on the NFS server. <br>**CLIENT_PATH** indicates the NFS path on the local device. <br>**nfs** indicates the path to which the remote shared directory is mounted on the local device. Replace the parameters as required. 269e41f4b71Sopenharmony_ci > 270e41f4b71Sopenharmony_ci > If you do not want to restrict the NFS access permission, set the permission of the NFS root directory to **777** on the Linux CLI. 271e41f4b71Sopenharmony_ci > 272e41f4b71Sopenharmony_ci > 273e41f4b71Sopenharmony_ci > ``` 274e41f4b71Sopenharmony_ci > chmod -R 777 /home/sqbin/nfs 275e41f4b71Sopenharmony_ci > ``` 276e41f4b71Sopenharmony_ci > 277e41f4b71Sopenharmony_ci > The NFS client setting is complete, and the NFS file system has been mounted. 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci3. Share files using NFS. 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci Create the **dir** directory on the NFS server and save the directory. Run the **ls** command in the OpenHarmony kernel. 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci ``` 284e41f4b71Sopenharmony_ci OHOS # ls /nfs 285e41f4b71Sopenharmony_ci ``` 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci The following information is returned from the serial port: 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci ``` 291e41f4b71Sopenharmony_ci OHOS # ls /nfs 292e41f4b71Sopenharmony_ci Directory /nfs: 293e41f4b71Sopenharmony_ci drwxr-xr-x 0 u:0 g:0 dir 294e41f4b71Sopenharmony_ci ``` 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci The **dir** directory created on the NFS server has been synchronized to the **/nfs** directory on the client (OpenHarmony kernel system). 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci Similarly, you can create files and directories on the client (OpenHarmony kernel system) and access them from the NFS server. 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci >  **NOTE**<br> 301e41f4b71Sopenharmony_ci > Currently, the NFS client supports some NFS v3 specifications. Therefore, the NFS client is not fully compatible with all types of NFS servers. You are advised to use the Linux NFS server to perform the development. 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci## Ramfs 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci### Basic Concepts 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_ciRamfs is a RAM-based file system whose size can be dynamically adjusted. Ramfs does not have a backing store. Directory entries and page caches are allocated when files are written into RAMFS. However, data is not written back to any other storage medium. This means that data will be lost after a power outage. 309e41f4b71Sopenharmony_ci### Working Principles 310e41f4b71Sopenharmony_ciRamfs stores all files in RAM, and read/write operations are performed in RAM. Ramfs is generally used to store temporary data or data that needs to be frequently modified, such as the **/tmp** and **/var** directories. Using ramfs reduces the read/write loss of the memory and improves the data read/write speed. 311e41f4b71Sopenharmony_ci### Development Guidelines 312e41f4b71Sopenharmony_ciMount: 313e41f4b71Sopenharmony_ci``` 314e41f4b71Sopenharmony_cimount(NULL, "/dev/shm", "ramfs", 0, NULL) 315e41f4b71Sopenharmony_ci``` 316e41f4b71Sopenharmony_ciCreate a directory: 317e41f4b71Sopenharmony_ci``` 318e41f4b71Sopenharmony_cimkdir(pathname, mode) 319e41f4b71Sopenharmony_ci``` 320e41f4b71Sopenharmony_ciCreate a file: 321e41f4b71Sopenharmony_ci``` 322e41f4b71Sopenharmony_ciopen(pathname, O_NONBLOCK | O_CREAT | O_RDWR, mode) 323e41f4b71Sopenharmony_ci``` 324e41f4b71Sopenharmony_ciRead a directory: 325e41f4b71Sopenharmony_ci``` 326e41f4b71Sopenharmony_cidir = opendir(pathname) 327e41f4b71Sopenharmony_ciptr = readdir(dir) 328e41f4b71Sopenharmony_ciclosedir(dir) 329e41f4b71Sopenharmony_ci``` 330e41f4b71Sopenharmony_ciDelete a file: 331e41f4b71Sopenharmony_ci``` 332e41f4b71Sopenharmony_ciunlink(pathname) 333e41f4b71Sopenharmony_ci``` 334e41f4b71Sopenharmony_ciDelete a directory: 335e41f4b71Sopenharmony_ci``` 336e41f4b71Sopenharmony_cirmdir(pathname) 337e41f4b71Sopenharmony_ci``` 338e41f4b71Sopenharmony_ciUnmount: 339e41f4b71Sopenharmony_ci``` 340e41f4b71Sopenharmony_ciumount("/dev/shm") 341e41f4b71Sopenharmony_ci``` 342e41f4b71Sopenharmony_ci>  **CAUTION**<br/> 343e41f4b71Sopenharmony_ci> - A ramfs file system can be mounted only once. Once mounted to a directory, it cannot be mounted to other directories. 344e41f4b71Sopenharmony_ci> 345e41f4b71Sopenharmony_ci> - Ramfs is under debugging and disabled by default. Do not use it in formal products. 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci## procfs 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci### Basic Concepts 351e41f4b71Sopenharmony_ci 352e41f4b71Sopenharmony_ciThe proc filesystem (procfs) is a virtual file system that displays process or other system information in a file-like structure. It is more convenient to obtain system information in file operation mode than API calling mode. 353e41f4b71Sopenharmony_ci 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci### Working Principles 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ciIn the OpenHarmony kernel, procfs is automatically mounted to the **/proc** directory during startup. Only the kernel module can create file nodes to provide the query service. 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_ci### Development Guidelines 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ciTo create a procfs file, you need to use **ProcMkdir** to create a directory and use **CreateProcEntry** to create a file. The development of the file node function is to hook the read and write functions to the file created by **CreateProcEntry**. When the procfs file is read or written, the hooked functions will be called to implement custom functions. 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ciDevelopment Example 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ciThe following describes how to create the **/proc/hello/world** file to implement the following functions: 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci1. Create a file in **/proc/hello/world**. 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci2. Read the file. When the file is read, "HelloWorld!" is returned. 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ci3. Write the file and print the data written in the file. 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci``` 377e41f4b71Sopenharmony_ci#include "proc_fs.h" 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_cistatic int TestRead(struct SeqBuf *buf, void *arg) 380e41f4b71Sopenharmony_ci{ 381e41f4b71Sopenharmony_ci LosBufPrintf(buf, "Hello World! \n"); /* Print "Hello World!" to the buffer. The data in the buffer will be returned to the read result. */ 382e41f4b71Sopenharmony_ci return 0; 383e41f4b71Sopenharmony_ci} 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_cistatic int TestWrite(struct ProcFile *pf, const char *buffer, size_t buflen, loff_t *ppos) 386e41f4b71Sopenharmony_ci{ 387e41f4b71Sopenharmony_ci if ((buffer == NULL) || (buflen <= 0)) { 388e41f4b71Sopenharmony_ci return -EINVAL; 389e41f4b71Sopenharmony_ci } 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ci PRINTK("your input is: %s\n", buffer); /* Different from the read API, the write API prints the data only to the console. */ 392e41f4b71Sopenharmony_ci return buflen; 393e41f4b71Sopenharmony_ci} 394e41f4b71Sopenharmony_cistatic const struct ProcFileOperations HELLO_WORLD_OPS = { 395e41f4b71Sopenharmony_ci .read = TestRead, 396e41f4b71Sopenharmony_ci .write = TestWrite, 397e41f4b71Sopenharmony_ci}; 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_civoid HelloWorldInit(void) 400e41f4b71Sopenharmony_ci{ 401e41f4b71Sopenharmony_ci /* Create the hello directory. */ 402e41f4b71Sopenharmony_ci struct ProcDirEntry *dir = ProcMkdir("hello", NULL); 403e41f4b71Sopenharmony_ci if (dir == NULL) { 404e41f4b71Sopenharmony_ci PRINT_ERR("create dir failed!\n"); 405e41f4b71Sopenharmony_ci return; 406e41f4b71Sopenharmony_ci } 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci /* Create the world file. */ 409e41f4b71Sopenharmony_ci struct ProcDirEntry *entry = CreateProcEntry("world", 0, dir); 410e41f4b71Sopenharmony_ci if (entry == NULL) { 411e41f4b71Sopenharmony_ci PRINT_ERR("create entry failed!\n"); 412e41f4b71Sopenharmony_ci return; 413e41f4b71Sopenharmony_ci } 414e41f4b71Sopenharmony_ci 415e41f4b71Sopenharmony_ci /* Hook the custom read and write APIs to the file. */ 416e41f4b71Sopenharmony_ci entry->procFileOps = &HELLO_WORLD_OPS; 417e41f4b71Sopenharmony_ci} 418e41f4b71Sopenharmony_ci``` 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci**Verification** 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ciAfter the OS startup, run the following commands in shell: 423e41f4b71Sopenharmony_ci 424e41f4b71Sopenharmony_ci 425e41f4b71Sopenharmony_ci``` 426e41f4b71Sopenharmony_ciOHOS # cat /proc/hello/world 427e41f4b71Sopenharmony_ciOHOS # Hello World! 428e41f4b71Sopenharmony_ciOHOS # echo "yo" > /proc/hello/world 429e41f4b71Sopenharmony_ciOHOS # your input is: yo 430e41f4b71Sopenharmony_ci``` 431