PageRenderTime 84ms CodeModel.GetById 69ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/filesystems/unixfs/common/unixfs/unixfs_internal.h

http://macfuse.googlecode.com/
C++ Header | 234 lines | 174 code | 35 blank | 25 comment | 14 complexity | 6a9e22cd48bc15c9c4d5665bf2f022c2 MD5 | raw file
  1/*
  2 * UnixFS
  3 *
  4 * A general-purpose file system layer for writing/reimplementing/porting
  5 * Unix file systems through MacFUSE.
  6
  7 * Copyright (c) 2008 Amit Singh. All Rights Reserved.
  8 * http://osxbook.com
  9 */
 10
 11#ifndef _UNIXFS_INTERNAL_H_
 12#define _UNIXFS_INTERNAL_H_
 13
 14#include "unixfs.h"
 15
 16#include <libgen.h>
 17#if __linux__
 18#include <darwin/queue.h>
 19#else
 20#include <sys/queue.h>
 21#endif
 22#include <sys/types.h>
 23#include <sys/stat.h>
 24#include <pthread.h>
 25
 26#if __APPLE__
 27
 28#define ino64_t ino_t
 29#include <libkern/OSByteOrder.h>
 30
 31#elif __linux__
 32
 33#define ino64_t ino_t
 34extern ssize_t pread(int fd, void *buf, size_t count, off_t offset);
 35
 36#include <endian.h>
 37#include <asm/byteorder.h>
 38
 39#define OSSwapLittleToHostInt64(x) __le64_to_cpu(x)
 40#define OSSwapBigToHostInt64(x)    __be64_to_cpu(x)
 41#define OSSwapHostToLittleInt64(x) __cpu_to_le64(x)
 42#define OSSwapHostToBigInt64(x)    __cpu_to_be64(x)
 43  
 44#define OSSwapLittleToHostInt32(x) __le32_to_cpu(x)
 45#define OSSwapBigToHostInt32(x)    __be32_to_cpu(x)
 46#define OSSwapHostToLittleInt32(x) __cpu_to_le32(x)
 47#define OSSwapHostToBigInt32(x)    __cpu_to_be32(x)
 48  
 49#define OSSwapLittleToHostInt16(x) __le16_to_cpu(x)
 50#define OSSwapBigToHostInt16(x)    __be16_to_cpu(x)
 51#define OSSwapHostToLittleInt16(x) __cpu_to_le16(x)
 52#define OSSwapHostToBigInt16(x)    __cpu_to_be16(x)
 53
 54#if __BYTE_ORDER == __BIG_ENDIAN
 55#define __BIG_ENDIAN__ 1
 56#elif __BYTE_ORDER == __LITTLE_ENDIAN
 57#define __LITTLE_ENDIAN__ 1
 58#else
 59#error Endian Problem
 60#endif
 61
 62#elif __FreeBSD__
 63
 64#define ino64_t uint64_t
 65#include <sys/endian.h>
 66
 67#if BYTE_ORDER == LITTLE_ENDIAN
 68#define __LITTLE_ENDIAN__ 1
 69#elif BYTE_ORDER == BIG_ENDIAN
 70#define __BIG_ENDIAN__ 1
 71#else
 72#error Endian Problem
 73#endif
 74
 75#define OSSwapLittleToHostInt64(x) le64toh(x)
 76#define OSSwapBigToHostInt64(x)    be64toh(x)
 77#define OSSwapLittleToHostInt32(x) le32toh(x)
 78#define OSSwapBigToHostInt32(x)    be32toh(x)
 79#define OSSwapLittleToHostInt16(x) le16toh(x)
 80#define OSSwapBigToHostInt16(x)    be16toh(x)
 81
 82#endif
 83
 84#define UNIXFS_ENABLE_INODEHASH   1 /* 1 => enable; 0 => disable */
 85
 86#define UNIXFS_BADBLOCK(blk, err) ((err != 0))
 87#define UNIXFS_IOSIZE(unixfs)     (uint32_t)(unixfs->s_statvfs.f_bsize)
 88#define UNIXFS_NADDR_MAX          13
 89
 90struct super_block {
 91    u_long         s_magic;
 92    u_long         s_flags;
 93    u_long         s_blocksize;
 94    uint8_t        s_blocksize_bits;
 95    uint8_t        s_dirt;
 96    fs_endian_t    s_endian;
 97    void*          s_fs_info;
 98    int            s_bdev; /* block device fd */
 99    int            s_kdev; /* kernel IPC descriptor */
100    uint32_t       s_dentsize;
101    char           s_fsname[UNIXFS_MNAMELEN];
102    char           s_volname[UNIXFS_MAXNAMLEN];
103    struct statvfs s_statvfs;
104};
105
106#define s_id s_fsname
107
108#define MACFUSE_ROOTINO 1
109
110/*
111 * The I node is the focus of all
112 * file activity in unix. There is a unique
113 * inode allocated for each active file,
114 * each current directory, each mounted-on
115 * file, text file, and the root. An inode is 'named'
116 * by its dev/inumber pair. (iget/iget.c)
117 * Data, from mode on, is read in
118 * from permanent inode on volume.
119 *
120 * This is a modern version of the in-core inode.
121 * We use this for all ancient file systems we support.
122 * We don't need a 'dev' though.
123 */
124typedef struct inode {
125    LIST_ENTRY(inode)   I_hashlink;
126    pthread_cond_t      I_state_cond;
127    uint32_t            I_initialized;
128    uint32_t            I_attachoutstanding;
129    uint32_t            I_waiting;
130    uint32_t            I_count;
131    uint32_t            I_blkbits;
132    struct super_block* I_sb;
133    struct stat         I_stat;
134    union {
135        uint32_t        I_daddr[UNIXFS_NADDR_MAX];
136        uint8_t         I_addr[UNIXFS_NADDR_MAX];
137    } I_addr_un;
138    void*               I_private;
139} inode;
140
141#define I_mode       I_stat.st_mode
142#define I_nlink      I_stat.st_nlink
143#define I_number     I_stat.st_ino
144#define I_ino        I_stat.st_ino
145#define I_uid        I_stat.st_uid
146#define I_gid        I_stat.st_gid
147#define I_rdev       I_stat.st_rdev
148#define I_atime      I_stat.st_atimespec
149#define I_mtime      I_stat.st_mtimespec
150#define I_ctime      I_stat.st_ctimespec
151#define I_crtime     I_stat.st_birthtimespec
152#if __linux__
153#define I_atime_sec  I_stat.st_atime
154#define I_mtime_sec  I_stat.st_mtime
155#define I_ctime_sec  I_stat.st_ctime
156#else
157#define I_atime_sec  I_stat.st_atimespec.tv_sec
158#define I_mtime_sec  I_stat.st_mtimespec.tv_sec
159#define I_ctime_sec  I_stat.st_ctimespec.tv_sec
160#define I_crtime_sec I_stat.st_birthtimespec.tv_sec
161#endif
162#define I_size       I_stat.st_size
163#define I_blocks     I_stat.st_blocks
164#define I_blksize    I_stat.st_blksize
165#define I_flags      I_stat.st_flags
166#define I_gen        I_stat.st_gen
167#define I_generation I_stat.st_gen
168#define I_version    I_stat.st_gen
169#define I_addr       I_addr_un.I_addr
170#define I_daddr      I_addr_un.I_daddr
171
172#define i_ino        I_stat.st_ino /* special case */
173
174/* Inode layer interface. */
175
176typedef int (*unixfs_inodelayer_iterator_t)(struct inode*, void*);
177
178int           unixfs_inodelayer_init(size_t privsize);
179void          unixfs_inodelayer_fini(void);
180struct inode* unixfs_inodelayer_iget(ino_t ino);
181void          unixfs_inodelayer_iput(struct inode* ip);
182void          unixfs_inodelayer_isucceeded(struct inode* ip);
183void          unixfs_inodelayer_ifailed(struct inode* ip);
184void          unixfs_inodelayer_dump(unixfs_inodelayer_iterator_t);
185
186/* Byte Swappers */
187
188#define cpu_to_le32(x) OSSwapHostToLittleInt32(x)
189#define cpu_to_be32(x) OSSwapHostToBigInt32(x)
190
191static inline uint32_t
192pdp11_to_host(uint32_t x)
193{
194#ifdef __LITTLE_ENDIAN__
195    return ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16);
196#else
197#ifdef __BIG_ENDIAN__
198    return ((x & 0xff00ff) << 8) | ((x & 0xff00ff00) >> 8);
199#else
200#error Endian Problem
201#endif
202#endif
203}
204
205static inline uint64_t
206fs64_to_host(fs_endian_t e, uint64_t x)
207{
208    if (e == UNIXFS_FS_BIG)
209        return OSSwapLittleToHostInt64(x);
210    else
211        return OSSwapBigToHostInt64(x);
212}
213
214static inline uint32_t
215fs32_to_host(fs_endian_t e, uint32_t x)
216{
217    if (e == UNIXFS_FS_PDP)
218        return pdp11_to_host(x);
219    else if (e == UNIXFS_FS_LITTLE)
220        return OSSwapLittleToHostInt32(x);
221    else
222        return OSSwapBigToHostInt32(x);
223}
224
225static inline uint16_t
226fs16_to_host(fs_endian_t e, uint16_t x)
227{
228    if (e != UNIXFS_FS_BIG)
229        return OSSwapLittleToHostInt16(x);
230    else
231        return OSSwapBigToHostInt16(x);
232}
233
234#endif /* _UNIXFS_INTERNAL_H_ */