dfs_file.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2005-01-26 Bernard The first version.
  9. * 2023-05-05 Bernard Change to dfs v2.0
  10. */
  11. #ifndef __DFS_FILE_H__
  12. #define __DFS_FILE_H__
  13. #include <dfs.h>
  14. #include <dfs_fs.h>
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. #endif
  19. #define STDIN_FILENO 0 /* standard input file descriptor */
  20. #define STDOUT_FILENO 1 /* standard output file descriptor */
  21. #define STDERR_FILENO 2 /* standard error file descriptor */
  22. struct dfs_file;
  23. struct dfs_vnode;
  24. struct dfs_dentry;
  25. struct dfs_attr;
  26. struct rt_pollreq;
  27. struct dirent;
  28. struct lwp_avl_struct;
  29. struct file_lock;
  30. struct dfs_aspace;
  31. struct dfs_file_ops
  32. {
  33. int (*open)(struct dfs_file *file);
  34. int (*close)(struct dfs_file *file);
  35. int (*ioctl)(struct dfs_file *file, int cmd, void *arg);
  36. ssize_t (*read)(struct dfs_file *file, void *buf, size_t count, off_t *pos);
  37. ssize_t (*write)(struct dfs_file *file, const void *buf, size_t count, off_t *pos);
  38. int (*flush)(struct dfs_file *file);
  39. off_t (*lseek)(struct dfs_file *file, off_t offset, int wherece);
  40. int (*truncate)(struct dfs_file *file, off_t offset);
  41. int (*getdents)(struct dfs_file *file, struct dirent *dirp, uint32_t count);
  42. int (*poll)(struct dfs_file *file, struct rt_pollreq *req);
  43. int (*mmap)(struct dfs_file *file, struct lwp_avl_struct *mmap);
  44. int (*lock)(struct dfs_file *file, struct file_lock *flock);
  45. int (*flock)(struct dfs_file *file, int, struct file_lock *flock);
  46. };
  47. struct dfs_vnode
  48. {
  49. uint32_t flags;
  50. uint32_t mode;
  51. int type; /* node type */
  52. rt_atomic_t ref_count; /* reference count */
  53. struct dfs_mnt *mnt; /* which mounted file system does this vnode belong to */
  54. size_t size;
  55. uint32_t nlink;
  56. const struct dfs_file_ops *fops;
  57. unsigned int uid;
  58. unsigned int gid;
  59. struct timespec atime;
  60. struct timespec mtime;
  61. struct timespec ctime;
  62. struct dfs_aspace *aspace;
  63. struct rt_mutex lock;
  64. void *data; /* private data of this file system */
  65. };
  66. /* file descriptor */
  67. #define DFS_FD_MAGIC 0xfdfd
  68. struct dfs_file
  69. {
  70. uint16_t magic;
  71. uint16_t mode;
  72. uint32_t flags;
  73. rt_atomic_t ref_count;
  74. off_t fpos;
  75. struct rt_mutex pos_lock;
  76. const struct dfs_file_ops *fops;
  77. struct dfs_dentry *dentry; /* dentry of this file */
  78. struct dfs_vnode *vnode; /* vnode of this file */
  79. void *mmap_context; /* used by mmap routine */
  80. void *data;
  81. };
  82. /* file is open for reading */
  83. #define FMODE_READ 0x1
  84. /* file is open for writing */
  85. #define FMODE_WRITE 0x2
  86. /* file is seekable */
  87. #define FMODE_LSEEK 0x4
  88. /* file can be accessed using pread */
  89. #define FMODE_PREAD 0x8
  90. /* file can be accessed using pwrite */
  91. #define FMODE_PWRITE 0x10
  92. /* File is opened for execution with sys_execve / sys_uselib */
  93. #define FMODE_EXEC 0x20
  94. /* File is opened with O_NDELAY (only set for block devices) */
  95. #define FMODE_NDELAY 0x40
  96. /* File is opened with O_EXCL (only set for block devices) */
  97. #define FMODE_EXCL 0x80
  98. /* dfs_vnode.c */
  99. int dfs_vnode_init(struct dfs_vnode *vnode, int type, const struct dfs_file_ops *fops);
  100. struct dfs_vnode *dfs_vnode_create(void);
  101. int dfs_vnode_destroy(struct dfs_vnode* vnode);
  102. struct dfs_vnode *dfs_vnode_ref(struct dfs_vnode *vnode);
  103. void dfs_vnode_unref(struct dfs_vnode *vnode);
  104. /*dfs_file.c*/
  105. #ifdef RT_USING_SMART
  106. struct dfs_mmap2_args
  107. {
  108. void *addr;
  109. size_t length;
  110. int prot;
  111. int flags;
  112. off_t pgoffset;
  113. struct rt_lwp *lwp;
  114. void *ret;
  115. };
  116. #endif
  117. void dfs_file_init(struct dfs_file *file);
  118. void dfs_file_deinit(struct dfs_file *file);
  119. int dfs_file_open(struct dfs_file *file, const char *path, int flags, mode_t mode);
  120. int dfs_file_close(struct dfs_file *file);
  121. off_t dfs_file_get_fpos(struct dfs_file *file);
  122. void dfs_file_set_fpos(struct dfs_file *file, off_t fpos);
  123. ssize_t dfs_file_read(struct dfs_file *file, void *buf, size_t len);
  124. ssize_t dfs_file_write(struct dfs_file *file, const void *buf, size_t len);
  125. off_t generic_dfs_lseek(struct dfs_file *file, off_t offset, int whence);
  126. off_t dfs_file_lseek(struct dfs_file *file, off_t offset, int wherece);
  127. int dfs_file_stat(const char *path, struct stat *buf);
  128. int dfs_file_lstat(const char *path, struct stat *buf);
  129. int dfs_file_setattr(const char *path, struct dfs_attr *attr);
  130. int dfs_file_fstat(struct dfs_file *file, struct stat *buf);
  131. int dfs_file_ioctl(struct dfs_file *file, int cmd, void *args);
  132. int dfs_file_fcntl(int fd, int cmd, unsigned long arg);
  133. int dfs_file_fsync(struct dfs_file *file);
  134. int dfs_file_unlink(const char *path);
  135. int dfs_file_link(const char *oldname, const char *newname);
  136. int dfs_file_symlink(const char *oldname, const char *name);
  137. int dfs_file_readlink(const char *path, char *buf, int bufsize);
  138. int dfs_file_rename(const char *old_file, const char *new_file);
  139. int dfs_file_ftruncate(struct dfs_file *file, off_t length);
  140. int dfs_file_getdents(struct dfs_file *file, struct dirent *dirp, size_t nbytes);
  141. int dfs_file_mkdir(const char *path, mode_t mode);
  142. int dfs_file_rmdir(const char *pathname);
  143. int dfs_file_isdir(const char *path);
  144. int dfs_file_access(const char *path, mode_t mode);
  145. int dfs_file_chdir(const char *path);
  146. char *dfs_file_getcwd(char *buf, size_t size);
  147. char *dfs_nolink_path(struct dfs_mnt **mnt, char *fullpath, int mode);
  148. #ifdef RT_USING_SMART
  149. int dfs_file_mmap2(struct dfs_file *file, struct dfs_mmap2_args *mmap2);
  150. int dfs_file_mmap(struct dfs_file *file, struct dfs_mmap2_args *mmap2);
  151. #endif
  152. char *dfs_nolink_path(struct dfs_mnt **mnt, char *fullpath, int mode);
  153. /* 0x5254 is just a magic number to make these relatively unique ("RT") */
  154. #define RT_FIOFTRUNCATE 0x52540000U
  155. #define RT_FIOGETADDR 0x52540001U
  156. #define RT_FIOMMAP2 0x52540002U
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif