denode.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* $OpenBSD: denode.h,v 1.26 2013/06/11 16:42:16 deraadt Exp $ */
  2. /* $NetBSD: denode.h,v 1.24 1997/10/17 11:23:39 ws Exp $ */
  3. /*-
  4. * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
  5. * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
  6. * All rights reserved.
  7. * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by TooLs GmbH.
  20. * 4. The name of TooLs GmbH may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
  24. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  29. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  31. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  32. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * Written by Paul Popelka (paulp@uts.amdahl.com)
  36. *
  37. * You can do anything you want with this software, just don't say you wrote
  38. * it, and don't remove this notice.
  39. *
  40. * This software is provided "as is".
  41. *
  42. * The author supplies this software to be publicly redistributed on the
  43. * understanding that the author is not responsible for the correct
  44. * functioning of this software in any circumstances and is not liable for
  45. * any damages caused by this software.
  46. *
  47. * October 1992
  48. */
  49. /*
  50. * This is the pc filesystem specific portion of the vnode structure.
  51. *
  52. * To describe a file uniquely the de_dirclust, de_diroffset, and
  53. * de_StartCluster fields are used.
  54. *
  55. * de_dirclust contains the cluster number of the directory cluster
  56. * containing the entry for a file or directory.
  57. * de_diroffset is the index into the cluster for the entry describing
  58. * a file or directory.
  59. * de_StartCluster is the number of the first cluster of the file or directory.
  60. *
  61. * Now to describe the quirks of the pc filesystem.
  62. * - Clusters 0 and 1 are reserved.
  63. * - The first allocatable cluster is 2.
  64. * - The root directory is of fixed size and all blocks that make it up
  65. * are contiguous.
  66. * - Cluster 0 refers to the root directory when it is found in the
  67. * startcluster field of a directory entry that points to another directory.
  68. * - Cluster 0 implies a 0 length file when found in the start cluster field
  69. * of a directory entry that points to a file.
  70. * - You can't use the cluster number 0 to derive the address of the root
  71. * directory.
  72. * - Multiple directory entries can point to a directory. The entry in the
  73. * parent directory points to a child directory. Any directories in the
  74. * child directory contain a ".." entry that points back to the parent.
  75. * The child directory itself contains a "." entry that points to itself.
  76. * - The root directory does not contain a "." or ".." entry.
  77. * - Directory entries for directories are never changed once they are created
  78. * (except when removed). The size stays 0, and the last modification time
  79. * is never changed. This is because so many directory entries can point to
  80. * the physical clusters that make up a directory. It would lead to an
  81. * update nightmare.
  82. * - The length field in a directory entry pointing to a directory contains 0
  83. * (always). The only way to find the end of a directory is to follow the
  84. * cluster chain until the "last cluster" marker is found.
  85. *
  86. * My extensions to make this house of cards work. These apply only to the in
  87. * memory copy of the directory entry.
  88. * - A reference count for each denode will be kept since dos doesn't keep such
  89. * things.
  90. */
  91. /*
  92. * Internal pseudo-offset for (nonexistent) directory entry for the root
  93. * dir in the root dir
  94. */
  95. #define MSDOSFSROOT_OFS 0x1fffffff
  96. /*
  97. * The fat cache structure. fc_fsrcn is the filesystem relative cluster
  98. * number that corresponds to the file relative cluster number in this
  99. * structure (fc_frcn).
  100. */
  101. struct fatcache {
  102. uint32_t fc_frcn; /* file relative cluster number */
  103. uint32_t fc_fsrcn; /* filesystem relative cluster number */
  104. };
  105. /*
  106. * The fat entry cache as it stands helps make extending files a "quick"
  107. * operation by avoiding having to scan the fat to discover the last
  108. * cluster of the file. The cache also helps sequential reads by
  109. * remembering the last cluster read from the file. This also prevents us
  110. * from having to rescan the fat to find the next cluster to read. This
  111. * cache is probably pretty worthless if a file is opened by multiple
  112. * processes.
  113. */
  114. #define FC_SIZE 3 /* number of entries in the cache */
  115. #define FC_LASTMAP 0 /* entry the last call to pcbmap() resolved
  116. * to */
  117. #define FC_LASTFC 1 /* entry for the last cluster in the file */
  118. #define FC_OLASTFC 2 /* entry for the previous last cluster */
  119. #define FCE_EMPTY 0xffffffff /* doesn't represent an actual cluster # */
  120. /*
  121. * Set a slot in the fat cache.
  122. */
  123. #define fc_setcache(dep, slot, frcn, fsrcn) \
  124. (dep)->de_fc[slot].fc_frcn = frcn; \
  125. (dep)->de_fc[slot].fc_fsrcn = fsrcn;
  126. /*
  127. * This is the in memory variant of a dos directory entry. It is usually
  128. * contained within a vnode.
  129. */
  130. struct denode {
  131. struct denode *de_next; /* Hash chain forward */
  132. struct denode **de_prev; /* Hash chain back */
  133. struct vnode *de_vnode; /* addr of vnode we are part of */
  134. struct vnode *de_devvp; /* vnode of blk dev we live on */
  135. uint32_t de_flag; /* flag bits */
  136. dev_t de_dev; /* device where direntry lives */
  137. daddr_t de_lastr;
  138. uint32_t de_dirclust; /* cluster of the directory file containing this entry */
  139. uint32_t de_diroffset; /* offset of this entry in the directory cluster */
  140. uint32_t de_fndoffset; /* offset of found dir entry */
  141. int de_fndcnt; /* number of slots before de_fndoffset */
  142. long de_refcnt; /* reference count */
  143. struct msdosfsmount *de_pmp; /* addr of our mount struct */
  144. struct lockf *de_lockf; /* byte level lock list */
  145. struct lock de_lock; /* denode lock */
  146. u_char de_Name[11]; /* name, from DOS directory entry */
  147. u_char de_Attributes; /* attributes, from directory entry */
  148. u_char de_CTimeHundredth; /* creation time, 1/100th of a sec */
  149. u_short de_CTime; /* creation time */
  150. u_short de_CDate; /* creation date */
  151. u_short de_ADate; /* access date */
  152. u_short de_MTime; /* modification time */
  153. u_short de_MDate; /* modification date */
  154. uint32_t de_StartCluster; /* starting cluster of file */
  155. uint32_t de_FileSize; /* size of file in bytes */
  156. struct fatcache de_fc[FC_SIZE]; /* fat cache */
  157. };
  158. /*
  159. * Values for the de_flag field of the denode.
  160. */
  161. #define DE_UPDATE 0x0004 /* Modification time update request. */
  162. #define DE_CREATE 0x0008 /* Creation time update */
  163. #define DE_ACCESS 0x0010 /* Access time update */
  164. #define DE_MODIFIED 0x0020 /* Denode has been modified. */
  165. #define DE_RENAME 0x0040 /* Denode is in the process of being renamed */
  166. /*
  167. * Maximum filename length in Win95
  168. * Note: Must be < sizeof(dirent.d_name)
  169. */
  170. #define WIN_MAXLEN 255
  171. /* Maximum size of a file on a FAT filesystem */
  172. #define MSDOSFS_FILESIZE_MAX 0xFFFFFFFFLL
  173. /*
  174. * Transfer directory entries between internal and external form.
  175. * dep is a struct denode * (internal form),
  176. * dp is a struct direntry * (external form).
  177. */
  178. #define DE_INTERNALIZE32(dep, dp) \
  179. ((dep)->de_StartCluster |= getushort((dp)->deHighClust) << 16)
  180. #define DE_INTERNALIZE(dep, dp) \
  181. (bcopy((dp)->deName, (dep)->de_Name, 8), \
  182. bcopy((dp)->deExtension, (dep)->de_Name + 8, 3), \
  183. (dep)->de_Attributes = (dp)->deAttributes, \
  184. (dep)->de_CTimeHundredth = (dp)->deCTimeHundredth, \
  185. (dep)->de_CTime = getushort((dp)->deCTime), \
  186. (dep)->de_CDate = getushort((dp)->deCDate), \
  187. (dep)->de_ADate = getushort((dp)->deADate), \
  188. (dep)->de_MTime = getushort((dp)->deMTime), \
  189. (dep)->de_MDate = getushort((dp)->deMDate), \
  190. (dep)->de_StartCluster = getushort((dp)->deStartCluster), \
  191. (dep)->de_FileSize = getulong((dp)->deFileSize), \
  192. (FAT32((dep)->de_pmp) ? DE_INTERNALIZE32((dep), (dp)) : 0))
  193. #define DE_EXTERNALIZE(dp, dep) \
  194. (bcopy((dep)->de_Name, (dp)->deName, 8), \
  195. bcopy((dep)->de_Name + 8, (dp)->deExtension, 3), \
  196. (dp)->deAttributes = (dep)->de_Attributes, \
  197. (dp)->deLowerCase = CASE_LOWER_BASE | CASE_LOWER_EXT, \
  198. (dp)->deCTimeHundredth = (dep)->de_CTimeHundredth, \
  199. putushort((dp)->deCTime, (dep)->de_CTime), \
  200. putushort((dp)->deCDate, (dep)->de_CDate), \
  201. putushort((dp)->deADate, (dep)->de_ADate), \
  202. putushort((dp)->deMTime, (dep)->de_MTime), \
  203. putushort((dp)->deMDate, (dep)->de_MDate), \
  204. putushort((dp)->deStartCluster, (dep)->de_StartCluster), \
  205. putulong((dp)->deFileSize, \
  206. ((dep)->de_Attributes & ATTR_DIRECTORY) ? 0 : (dep)->de_FileSize),\
  207. putushort((dp)->deHighClust, \
  208. FAT32((dep)->de_pmp) ? (dep)->de_StartCluster >> 16 : 0))
  209. #define de_forw de_chain[0]
  210. #define de_back de_chain[1]
  211. #define VTODE(vp) ((struct denode *)(vp)->v_data)
  212. #define DETOV(de) ((de)->de_vnode)
  213. #define DETIMES(dep, acc, mod, cre) \
  214. if ((dep)->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS)) { \
  215. (dep)->de_flag |= DE_MODIFIED; \
  216. if ((dep)->de_flag & DE_UPDATE) { \
  217. unix2dostime((mod), &(dep)->de_MDate, &(dep)->de_MTime, NULL); \
  218. (dep)->de_Attributes |= ATTR_ARCHIVE; \
  219. } \
  220. if (!((dep)->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95)) { \
  221. if ((dep)->de_flag & DE_ACCESS) \
  222. unix2dostime((acc), &(dep)->de_ADate, NULL, NULL); \
  223. if ((dep)->de_flag & DE_CREATE) \
  224. unix2dostime((cre), &(dep)->de_CDate, &(dep)->de_CTime, &(dep)->de_CTimeHundredth); \
  225. } \
  226. (dep)->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS); \
  227. }
  228. /*
  229. * This overlays the fid structure (see mount.h)
  230. */
  231. struct defid {
  232. u_short defid_len; /* length of structure */
  233. u_short defid_pad; /* force long alignment */
  234. uint32_t defid_dirclust; /* cluster this dir entry came from */
  235. uint32_t defid_dirofs; /* offset of entry within the cluster */
  236. #if 0
  237. uint32_t defid_gen; /* generation number */
  238. #endif
  239. };
  240. #ifdef _KERNEL
  241. /*
  242. * Prototypes for MSDOSFS vnode operations
  243. */
  244. int msdosfs_lookup(void *);
  245. int msdosfs_create(void *);
  246. int msdosfs_mknod(void *);
  247. int msdosfs_open(void *);
  248. int msdosfs_close(void *);
  249. int msdosfs_access(void *);
  250. int msdosfs_getattr(void *);
  251. int msdosfs_setattr(void *);
  252. int msdosfs_read(void *);
  253. int msdosfs_write(void *);
  254. int msdosfs_ioctl(void *);
  255. int msdosfs_poll(void *);
  256. int msdosfs_fsync(void *);
  257. int msdosfs_remove(void *);
  258. int msdosfs_link(void *);
  259. int msdosfs_rename(void *);
  260. int msdosfs_mkdir(void *);
  261. int msdosfs_rmdir(void *);
  262. int msdosfs_symlink(void *);
  263. int msdosfs_readdir(void *);
  264. int msdosfs_readlink(void *);
  265. int msdosfs_inactive(void *);
  266. int msdosfs_reclaim(void *);
  267. int msdosfs_lock(void *);
  268. int msdosfs_unlock(void *);
  269. int msdosfs_bmap(void *);
  270. int msdosfs_strategy(void *);
  271. int msdosfs_print(void *);
  272. int msdosfs_islocked(void *);
  273. int msdosfs_advlock(void *);
  274. int msdosfs_pathconf(void *);
  275. /*
  276. * Internal service routine prototypes.
  277. */
  278. int createde(struct denode *, struct denode *, struct denode **, struct componentname *);
  279. int deextend(struct denode *, uint32_t, struct ucred *);
  280. int deget(struct msdosfsmount *, uint32_t, uint32_t, struct denode **);
  281. int detrunc(struct denode *, uint32_t, int, struct ucred *, struct proc *);
  282. int deupdat(struct denode *, int);
  283. int doscheckpath(struct denode *, struct denode *);
  284. int dosdirempty(struct denode *);
  285. int readde(struct denode *, struct buf **, struct direntry **);
  286. int readep(struct msdosfsmount *, uint32_t, uint32_t, struct buf **, struct direntry **);
  287. void reinsert(struct denode *);
  288. int removede(struct denode *, struct denode *);
  289. int uniqdosname(struct denode *, struct componentname *, u_char *);
  290. int findwin95(struct denode *);
  291. #endif /* _KERNEL */