hfs.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:17k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* 
  2.  * linux/fs/hfs/hfs.h
  3.  *
  4.  * Copyright (C) 1995-1997  Paul H. Hargrove
  5.  * This file may be distributed under the terms of the GNU General Public License.
  6.  *
  7.  * "XXX" in a comment is a note to myself to consider changing something.
  8.  */
  9. #ifndef _HFS_H
  10. #define _HFS_H
  11. #include <linux/hfs_sysdep.h>
  12. #define HFS_NEW(X) ((X) = hfs_malloc(sizeof(*(X))))
  13. #define HFS_DELETE(X) do { hfs_free((X), sizeof(*(X))); (X) = NULL; } 
  14.                         while (0)
  15.  
  16. /* offsets to various blocks */
  17. #define HFS_DD_BLK 0 /* Driver Descriptor block */
  18. #define HFS_PMAP_BLK 1 /* First block of partition map */
  19. #define HFS_MDB_BLK 2 /* Block (w/i partition) of MDB */
  20. /* magic numbers for various disk blocks */
  21. #define HFS_DRVR_DESC_MAGIC 0x4552 /* "ER": driver descriptor map */
  22. #define HFS_OLD_PMAP_MAGIC 0x5453 /* "TS": old-type partition map */
  23. #define HFS_NEW_PMAP_MAGIC 0x504D /* "PM": new-type partition map */
  24. #define HFS_SUPER_MAGIC 0x4244 /* "BD": HFS MDB (super block) */
  25. #define HFS_MFS_SUPER_MAGIC 0xD2D7 /* MFS MDB (super block) */
  26. /* magic numbers for various internal structures */
  27. #define HFS_FILE_MAGIC 0x4801
  28. #define HFS_DIR_MAGIC 0x4802
  29. #define HFS_MDB_MAGIC 0x4803
  30. #define HFS_EXT_MAGIC 0x4804 /* XXX currently unused */
  31. #define HFS_BREC_MAGIC 0x4811 /* XXX currently unused */
  32. #define HFS_BTREE_MAGIC 0x4812
  33. #define HFS_BNODE_MAGIC 0x4813
  34. /* various FIXED size parameters */
  35. #define HFS_SECTOR_SIZE 512    /* size of an HFS sector */
  36. #define HFS_SECTOR_SIZE_BITS 9      /* log_2(HFS_SECTOR_SIZE) */
  37. #define HFS_NAMELEN 31     /* maximum length of an HFS filename */
  38. #define HFS_NAMEMAX (3*31) /* max size of ENCODED filename */
  39. #define HFS_BM_MAXBLOCKS (16)   /* max number of bitmap blocks */
  40. #define HFS_BM_BPB (8*HFS_SECTOR_SIZE) /* number of bits per bitmap block */
  41. #define HFS_MAX_VALENCE 32767U
  42. #define HFS_FORK_MAX (0x7FFFFFFF)
  43. /* Meanings of the drAtrb field of the MDB,
  44.  * Reference: _Inside Macintosh: Files_ p. 2-61
  45.  */
  46. #define HFS_SB_ATTRIB_HLOCK 0x0080
  47. #define HFS_SB_ATTRIB_CLEAN 0x0100
  48. #define HFS_SB_ATTRIB_SPARED 0x0200
  49. #define HFS_SB_ATTRIB_SLOCK 0x8000
  50. /* 2**16 - 1 */
  51. #define HFS_USHRT_MAX 65535
  52. /* Some special File ID numbers */
  53. #define HFS_POR_CNID 1 /* Parent Of the Root */
  54. #define HFS_ROOT_CNID 2 /* ROOT directory */
  55. #define HFS_EXT_CNID 3 /* EXTents B-tree */
  56. #define HFS_CAT_CNID 4 /* CATalog B-tree */
  57. #define HFS_BAD_CNID 5 /* BAD blocks file */
  58. #define HFS_ALLOC_CNID  6       /* ALLOCation file (HFS+) */
  59. #define HFS_START_CNID  7       /* STARTup file (HFS+) */
  60. #define HFS_ATTR_CNID   8       /* ATTRibutes file (HFS+) */
  61. #define HFS_EXCH_CNID  15       /* ExchangeFiles temp id */
  62. /* values for hfs_cat_rec.cdrType */
  63. #define HFS_CDR_DIR    0x01    /* folder (directory) */
  64. #define HFS_CDR_FIL    0x02    /* file */
  65. #define HFS_CDR_THD    0x03    /* folder (directory) thread */
  66. #define HFS_CDR_FTH    0x04    /* file thread */
  67. /* legal values for hfs_ext_key.FkType and hfs_file.fork */
  68. #define HFS_FK_DATA 0x00
  69. #define HFS_FK_RSRC 0xFF
  70. /* bits in hfs_fil_entry.Flags */
  71. #define HFS_FIL_LOCK 0x01  /* locked */
  72. #define HFS_FIL_THD 0x02  /* file thread */
  73. #define HFS_FIL_DOPEN   0x04  /* data fork open */
  74. #define HFS_FIL_ROPEN   0x08  /* resource fork open */
  75. #define HFS_FIL_DIR     0x10  /* directory (always clear) */
  76. #define HFS_FIL_RSRV1   0x20  /* reserved */
  77. #define HFS_FIL_NOCOPY  0x40  /* copy-protected file */
  78. #define HFS_FIL_USED 0x80  /* open */
  79. /* bits in hfs_dir_entry.Flags. dirflags is 16 bits. */
  80. #define HFS_DIR_LOCK        0x01  /* locked */
  81. #define HFS_DIR_THD         0x02  /* directory thread */
  82. #define HFS_DIR_INEXPFOLDER 0x04  /* in a shared area */
  83. #define HFS_DIR_MOUNTED     0x08  /* mounted */
  84. #define HFS_DIR_DIR         0x10  /* directory (always set) */
  85. #define HFS_DIR_EXPFOLDER   0x20  /* share point */
  86. #define HFS_DIR_RSRV1       0x40  /* reserved */
  87. #define HFS_DIR_RSRV2       0x80  /* reserved */
  88. /* Access types used when requesting access to a B-node */
  89. #define HFS_LOCK_NONE 0x0000 /* Illegal */
  90. #define HFS_LOCK_READ 0x0001 /* read-only access */
  91. #define HFS_LOCK_RESRV 0x0002 /* might potentially modify */
  92. #define HFS_LOCK_WRITE 0x0003 /* will modify now (exclusive access) */
  93. #define HFS_LOCK_MASK 0x000f
  94. /* Flags field of the hfs_path_elem */
  95. #define HFS_BPATH_FIRST 0x0100
  96. #define HFS_BPATH_OVERFLOW 0x0200
  97. #define HFS_BPATH_UNDERFLOW 0x0400
  98. #define HFS_BPATH_MASK 0x0f00
  99. /* Flags for hfs_bfind() */
  100. #define HFS_BFIND_EXACT 0x0010
  101. #define HFS_BFIND_LOCK 0x0020
  102. /* Modes for hfs_bfind() */
  103. #define HFS_BFIND_WRITE   (HFS_LOCK_RESRV|HFS_BFIND_EXACT|HFS_BFIND_LOCK)
  104. #define HFS_BFIND_READ_EQ (HFS_LOCK_READ|HFS_BFIND_EXACT)
  105. #define HFS_BFIND_READ_LE (HFS_LOCK_READ)
  106. #define HFS_BFIND_INSERT  (HFS_LOCK_RESRV|HFS_BPATH_FIRST|HFS_BPATH_OVERFLOW)
  107. #define HFS_BFIND_DELETE 
  108.  (HFS_LOCK_RESRV|HFS_BFIND_EXACT|HFS_BPATH_FIRST|HFS_BPATH_UNDERFLOW)
  109. /*======== HFS structures as they appear on the disk ========*/
  110. /* Pascal-style string of up to 31 characters */
  111. struct hfs_name {
  112. hfs_byte_t Len;
  113. hfs_byte_t Name[31];
  114. } __attribute__((packed));
  115. typedef struct {
  116. hfs_word_t v;
  117. hfs_word_t h;
  118. } hfs_point_t;
  119. typedef struct {
  120. hfs_word_t top;
  121. hfs_word_t left;
  122. hfs_word_t bottom;
  123. hfs_word_t right;
  124. } hfs_rect_t;
  125. typedef struct {
  126. hfs_lword_t  fdType;
  127. hfs_lword_t  fdCreator;
  128. hfs_word_t  fdFlags;
  129. hfs_point_t  fdLocation;
  130. hfs_word_t  fdFldr;
  131. } __attribute__((packed)) hfs_finfo_t;
  132. typedef struct {
  133. hfs_word_t fdIconID;
  134. hfs_byte_t fdUnused[8];
  135. hfs_word_t fdComment;
  136. hfs_lword_t fdPutAway;
  137. } __attribute__((packed)) hfs_fxinfo_t;
  138. typedef struct {
  139. hfs_rect_t frRect;
  140. hfs_word_t frFlags;
  141. hfs_point_t frLocation;
  142. hfs_word_t frView;
  143. } __attribute__((packed)) hfs_dinfo_t;
  144. typedef struct {
  145. hfs_point_t frScroll;
  146. hfs_lword_t frOpenChain;
  147. hfs_word_t frUnused;
  148. hfs_word_t frComment;
  149. hfs_lword_t frPutAway;
  150. } __attribute__((packed)) hfs_dxinfo_t;
  151. union hfs_finder_info {
  152. struct {
  153. hfs_finfo_t finfo;
  154. hfs_fxinfo_t fxinfo;
  155. } file;
  156. struct {
  157. hfs_dinfo_t dinfo;
  158. hfs_dxinfo_t dxinfo;
  159. } dir;
  160. };
  161. /* A btree record key on disk */
  162. struct hfs_bkey {
  163. hfs_byte_t KeyLen; /* number of bytes in the key */
  164. hfs_byte_t value[1]; /* (KeyLen) bytes of key */
  165. } __attribute__((packed));
  166. /* Cast to a pointer to a generic bkey */
  167. #define HFS_BKEY(X) (((void)((X)->KeyLen)), ((struct hfs_bkey *)(X)))
  168. /* The key used in the catalog b-tree: */
  169. struct hfs_cat_key {
  170. hfs_byte_t KeyLen; /* number of bytes in the key */
  171. hfs_byte_t Resrv1; /* padding */
  172. hfs_lword_t ParID; /* CNID of the parent dir */
  173. struct hfs_name CName; /* The filename of the entry */
  174. } __attribute__((packed));
  175. /* The key used in the extents b-tree: */
  176. struct hfs_ext_key {
  177. hfs_byte_t KeyLen; /* number of bytes in the key */
  178. hfs_byte_t FkType; /* HFS_FK_{DATA,RSRC} */
  179. hfs_lword_t FNum; /* The File ID of the file */
  180. hfs_word_t FABN; /* allocation blocks number*/
  181. } __attribute__((packed));
  182. /*======== Data structures kept in memory ========*/
  183. /*
  184.  * struct hfs_mdb
  185.  *
  186.  * The fields from the MDB of an HFS filesystem
  187.  */
  188. struct hfs_mdb {
  189. int magic; /* A magic number */
  190. unsigned char vname[28]; /* The volume name */
  191. hfs_sysmdb sys_mdb; /* superblock */
  192. hfs_buffer buf; /* The hfs_buffer
  193.    holding the real
  194.    superblock (aka VIB
  195.    or MDB) */
  196. hfs_buffer alt_buf; /* The hfs_buffer holding
  197.    the alternate superblock */
  198. hfs_buffer bitmap[16]; /* The hfs_buffer holding the
  199.    allocation bitmap */
  200. struct hfs_btree * ext_tree; /* Information about
  201.    the extents b-tree */
  202. struct hfs_btree * cat_tree; /* Information about
  203.    the catalog b-tree */
  204. hfs_u32 file_count; /* The number of
  205.    regular files in
  206.    the filesystem */
  207. hfs_u32 dir_count; /* The number of
  208.    directories in the
  209.    filesystem */
  210. hfs_u32 next_id; /* The next available
  211.    file id number */
  212. hfs_u32 clumpablks; /* The number of allocation
  213.    blocks to try to add when
  214.    extending a file */
  215. hfs_u32 write_count; /* The number of MDB
  216.    writes (a sort of
  217.    version number) */
  218. hfs_u32 fs_start; /* The first 512-byte
  219.    block represented
  220.    in the bitmap */
  221. hfs_u32 create_date; /* In network byte-order */
  222. hfs_u32 modify_date; /* In network byte-order */
  223. hfs_u32 backup_date; /* In network byte-order */
  224. hfs_u16 root_files; /* The number of
  225.    regular
  226.    (non-directory)
  227.    files in the root
  228.    directory */
  229. hfs_u16 root_dirs; /* The number of
  230.    directories in the
  231.    root directory */
  232. hfs_u16 fs_ablocks; /* The number of
  233.    allocation blocks
  234.    in the filesystem */
  235. hfs_u16 free_ablocks; /* The number of unused
  236.    allocation blocks
  237.    in the filesystem */
  238. hfs_u32 alloc_blksz; /* The number of
  239.    512-byte blocks per
  240.    "allocation block" */
  241. hfs_u16 attrib; /* Attribute word */
  242. hfs_wait_queue rename_wait;
  243. int rename_lock;
  244. hfs_wait_queue bitmap_wait;
  245. int bitmap_lock;
  246.         struct list_head        entry_dirty;
  247. };
  248. /*
  249.  * struct hfs_extent
  250.  *
  251.  * The offset to allocation block mapping for a given file is
  252.  * contained in a series of these structures.  Each (struct
  253.  * hfs_extent) records up to three runs of contiguous allocation
  254.  * blocks.  An allocation block is a contiguous group of physical
  255.  * blocks.
  256.  */
  257. struct hfs_extent {
  258. int    magic;     /* A magic number */
  259. unsigned short    start;     /* Where in the file this record
  260.  begins (in allocation blocks) */
  261. unsigned short    end;       /* Where in the file this record
  262.  ends (in allocation blocks) */
  263. unsigned short    block[3];  /* The allocation block on disk which
  264.  begins this extent */
  265. unsigned short    length[3]; /* The number of allocation blocks
  266.  in this extent */
  267. struct hfs_extent  *next;     /* Next extent record for this file */
  268. struct hfs_extent  *prev;     /* Previous extent record for this file */
  269. int    count;     /* Number of times it is used */
  270. };
  271. /*
  272.  * struct hfs_dir 
  273.  *
  274.  * This structure holds information specific
  275.  * to a directory in an HFS filesystem.
  276.  */
  277. struct hfs_dir {
  278. int magic; /* A magic number */
  279. hfs_u16 flags;
  280. hfs_u16 dirs; /* Number of directories in this one */
  281. hfs_u16 files; /* Number of files in this directory */
  282. int readers;
  283. hfs_wait_queue read_wait;
  284. int writers;
  285. hfs_wait_queue write_wait;
  286. };
  287. /*
  288.  * struct hfs_fork
  289.  *
  290.  * This structure holds the information
  291.  * specific to a single fork of a file.
  292.  */
  293. struct hfs_fork {
  294. struct hfs_cat_entry *entry;    /* The file this fork is part of */
  295. struct hfs_extent first;     /* The first extent record for
  296.  this fork */
  297. struct hfs_extent *cache;    /* The most-recently accessed
  298.  extent record for this fork */
  299. hfs_u32 lsize;     /* The logical size in bytes */
  300. hfs_u32 psize;     /* The phys size (512-byte blocks) */
  301.         hfs_u8 fork;      /* Which fork is this? */
  302. };
  303. /*
  304.  * struct hfs_file 
  305.  *
  306.  * This structure holds information specific
  307.  * to a file in an HFS filesystem.
  308.  */
  309. struct hfs_file {
  310. int    magic;
  311. struct hfs_fork    data_fork;
  312. struct hfs_fork    rsrc_fork;
  313. hfs_u16    clumpablks;
  314. hfs_u8    flags;
  315. };
  316. /*
  317.  * struct hfs_file 
  318.  *
  319.  * This structure holds information about a
  320.  * file or directory in an HFS filesystem.
  321.  *
  322.  * 'wait' must remain 1st and 'hash' 2nd since we do some pointer arithmetic.
  323.  */
  324. struct hfs_cat_entry {
  325. hfs_wait_queue wait;
  326.         struct list_head        hash;
  327.         struct list_head        list;
  328. struct hfs_mdb *mdb;
  329. hfs_sysentry sys_entry;
  330. struct hfs_cat_key key;
  331. union hfs_finder_info info;
  332. hfs_u32 cnid; /* In network byte-order */
  333. hfs_u32 create_date; /* In network byte-order */
  334. hfs_u32 modify_date; /* In network byte-order */
  335. hfs_u32 backup_date; /* In network byte-order */
  336. unsigned short count;
  337.         unsigned long           state;
  338. hfs_u8 type;
  339. union {
  340. struct hfs_dir dir;
  341. struct hfs_file file;
  342. } u;
  343. };
  344. /* hfs entry state bits */
  345. #define HFS_DIRTY        1
  346. #define HFS_KEYDIRTY     2
  347. #define HFS_LOCK         4
  348. #define HFS_DELETED      8
  349. /* 
  350.  * struct hfs_bnode_ref
  351.  *
  352.  * A pointer to a (struct hfs_bnode) and the type of lock held on it.
  353.  */
  354. struct hfs_bnode_ref {
  355.         struct hfs_bnode *bn;
  356.         int lock_type;
  357. };
  358. /*
  359.  * struct hfs_belem
  360.  *
  361.  * An element of the path from the root of a B-tree to a leaf.
  362.  * Includes the reference to a (struct hfs_bnode), the index of
  363.  * the appropriate record in that node, and some flags.
  364.  */
  365. struct hfs_belem {
  366. struct hfs_bnode_ref bnr;
  367. int record;
  368. int flags;
  369. };
  370. /*
  371.  * struct hfs_brec
  372.  *
  373.  * The structure returned by hfs_bfind() to describe the requested record.
  374.  */
  375. struct hfs_brec {
  376. int keep_flags;
  377. struct hfs_btree *tree;
  378. struct hfs_belem *top;
  379. struct hfs_belem *bottom;
  380. struct hfs_belem elem[9];
  381. struct hfs_bkey *key;
  382. void *data; /* The actual data */
  383. };
  384. /*================ Function prototypes ================*/
  385. /* bdelete.c */
  386. extern int hfs_bdelete(struct hfs_btree *, const struct hfs_bkey *);
  387. /* bfind.c */
  388. extern void hfs_brec_relse(struct hfs_brec *, struct hfs_belem *);
  389. extern int hfs_bsucc(struct hfs_brec *, int);
  390. extern int hfs_bfind(struct hfs_brec *, struct hfs_btree *,
  391.      const struct hfs_bkey *, int);
  392.  
  393. /* binsert.c */
  394. extern int hfs_binsert(struct hfs_btree *, const struct hfs_bkey *,
  395.        const void *, hfs_u16);
  396. /* bitmap.c */
  397. extern hfs_u16 hfs_vbm_count_free(const struct hfs_mdb *, hfs_u16);
  398. extern hfs_u16 hfs_vbm_search_free(const struct hfs_mdb *, hfs_u16 *);
  399. extern int hfs_set_vbm_bits(struct hfs_mdb *, hfs_u16, hfs_u16);
  400. extern int hfs_clear_vbm_bits(struct hfs_mdb *, hfs_u16, hfs_u16);
  401. /* bitops.c */
  402. extern hfs_u32 hfs_find_zero_bit(const hfs_u32 *, hfs_u32, hfs_u32);
  403. extern hfs_u32 hfs_count_zero_bits(const hfs_u32 *, hfs_u32, hfs_u32);
  404. /* btree.c */
  405. extern struct hfs_btree *hfs_btree_init(struct hfs_mdb *, ino_t,
  406.         hfs_byte_t *, hfs_u32, hfs_u32);
  407. extern void hfs_btree_free(struct hfs_btree *);
  408. extern void hfs_btree_commit(struct hfs_btree *, hfs_byte_t *, hfs_lword_t);
  409. /* catalog.c */
  410. extern void hfs_cat_init(void);
  411. extern void hfs_cat_put(struct hfs_cat_entry *);
  412. extern void hfs_cat_mark_dirty(struct hfs_cat_entry *);
  413. extern struct hfs_cat_entry *hfs_cat_get(struct hfs_mdb *,
  414.  const struct hfs_cat_key *);
  415. extern void hfs_cat_invalidate(struct hfs_mdb *);
  416. extern void hfs_cat_commit(struct hfs_mdb *);
  417. extern void hfs_cat_free(void);
  418. extern int hfs_cat_compare(const struct hfs_cat_key *,
  419.    const struct hfs_cat_key *);
  420. extern void hfs_cat_build_key(hfs_u32, const struct hfs_name *,
  421.       struct hfs_cat_key *);
  422. extern struct hfs_cat_entry *hfs_cat_parent(struct hfs_cat_entry *);
  423. extern int hfs_cat_open(struct hfs_cat_entry *, struct hfs_brec *);
  424. extern int hfs_cat_next(struct hfs_cat_entry *, struct hfs_brec *,
  425. hfs_u16, hfs_u32 *, hfs_u8 *);
  426. extern void hfs_cat_close(struct hfs_cat_entry *, struct hfs_brec *);
  427. extern int hfs_cat_create(struct hfs_cat_entry *, struct hfs_cat_key *,
  428.   hfs_u8, hfs_u32, hfs_u32, struct hfs_cat_entry **);
  429. extern int hfs_cat_mkdir(struct hfs_cat_entry *, struct hfs_cat_key *,
  430.  struct hfs_cat_entry **);
  431. extern int hfs_cat_delete(struct hfs_cat_entry *, struct hfs_cat_entry *, int);
  432. extern int hfs_cat_move(struct hfs_cat_entry *, struct hfs_cat_entry *,
  433. struct hfs_cat_entry *, struct hfs_cat_key *,
  434. struct hfs_cat_entry **);
  435. /* extent.c */
  436. extern int hfs_ext_compare(const struct hfs_ext_key *,
  437.    const struct hfs_ext_key *);
  438. extern void hfs_extent_in(struct hfs_fork *, const hfs_byte_t *);
  439. extern void hfs_extent_out(const struct hfs_fork *, hfs_byte_t *);
  440. extern int hfs_extent_map(struct hfs_fork *, int, int);
  441. extern void hfs_extent_adj(struct hfs_fork *);
  442. extern void hfs_extent_free(struct hfs_fork *);
  443. /* file.c */
  444. extern int hfs_get_block(struct inode *, long, struct buffer_head *, int);
  445. /* mdb.c */
  446. extern struct hfs_mdb *hfs_mdb_get(hfs_sysmdb, int, hfs_s32);
  447. extern void hfs_mdb_commit(struct hfs_mdb *, int);
  448. extern void hfs_mdb_put(struct hfs_mdb *, int);
  449. /* part_tbl.c */
  450. extern int hfs_part_find(hfs_sysmdb, int, int, hfs_s32 *, hfs_s32 *);
  451. /* string.c */
  452. extern unsigned int hfs_strhash(const unsigned char *, unsigned int);
  453. extern int hfs_strcmp(const unsigned char *, unsigned int, 
  454.       const unsigned char *, unsigned int);
  455. extern int hfs_streq(const unsigned char *, unsigned int, 
  456.      const unsigned char *, unsigned int);
  457. extern void hfs_tolower(unsigned char *, int);
  458. static __inline__ struct dentry 
  459. *hfs_lookup_dentry(struct dentry *base, const char *name, const int len)
  460. {
  461.   struct qstr this;
  462.   this.name = name;
  463.   this.len = len;
  464.   this.hash = hfs_strhash(name, len);
  465.   return d_lookup(base, &this);
  466. }
  467. /* drop a dentry for one of the special directories.
  468.  * it's in the form of base/name/dentry. */
  469. static __inline__ void hfs_drop_special(struct dentry *base,
  470. const struct hfs_name *name,
  471. struct dentry *dentry)
  472. {
  473.   struct dentry *dparent, *de;
  474.   
  475.   dparent = hfs_lookup_dentry(base, name->Name, name->Len);
  476.   if (dparent) {
  477.   de = hfs_lookup_dentry(dparent, dentry->d_name.name, 
  478.  dentry->d_name.len);
  479.   if (de) {
  480.   if (!de->d_inode)
  481.   d_drop(de);
  482.   dput(de);
  483.   }
  484.   dput(dparent);
  485.   }
  486. }
  487. extern struct dentry_operations hfs_dentry_operations;
  488. #endif