home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / hpfslin9.zip / hpfs-0.9.diff < prev    next >
Text File  |  1993-10-23  |  69KB  |  2,184 lines

  1. diff -rc2N linux-dist/config.in linux/config.in
  2. *** linux-dist/config.in    Fri Sep 17 14:00:40 1993
  3. --- linux/config.in    Sat Oct 23 21:00:15 1993
  4. ***************
  5. *** 79,82 ****
  6. --- 79,83 ----
  7.   bool 'NFS filesystem support' CONFIG_NFS_FS y
  8.   bool 'ISO9660 cdrom filesystem support' CONFIG_ISO9660_FS n
  9. + bool 'os/2 HPFS filesystem support (read only)' CONFIG_HPFS_FS n
  10.   *
  11.   *  character devices
  12. diff -rc2N linux-dist/fs/Makefile linux/fs/Makefile
  13. *** linux-dist/fs/Makefile    Fri Aug 20 08:59:30 1993
  14. --- linux/fs/Makefile    Mon Oct 18 19:49:23 1993
  15. ***************
  16. *** 8,12 ****
  17.   # Note 2! The CFLAGS definitions are now in the main makefile...
  18.   
  19. ! SUBDIRS = minix ext ext2 msdos proc isofs nfs xiafs
  20.   
  21.   ifdef CONFIG_MINIX_FS
  22. --- 8,12 ----
  23.   # Note 2! The CFLAGS definitions are now in the main makefile...
  24.   
  25. ! SUBDIRS = minix ext ext2 msdos proc isofs nfs xiafs hpfs
  26.   
  27.   ifdef CONFIG_MINIX_FS
  28. ***************
  29. *** 33,36 ****
  30. --- 33,39 ----
  31.   ifdef CONFIG_XIA_FS
  32.   FS_SUBDIRS := $(FS_SUBDIRS) xiafs
  33. + endif
  34. + ifdef CONFIG_HPFS_FS
  35. + FS_SUBDIRS := $(FS_SUBDIRS) hpfs
  36.   endif
  37.   
  38. diff -rc2N linux-dist/fs/filesystems.c linux/fs/filesystems.c
  39. *** linux-dist/fs/filesystems.c    Sun Apr  4 07:08:51 1993
  40. --- linux/fs/filesystems.c    Mon Oct 18 19:49:23 1993
  41. ***************
  42. *** 33,36 ****
  43. --- 33,39 ----
  44.   #include <linux/iso_fs.h>
  45.   #endif
  46. + #ifdef CONFIG_HPFS_FS
  47. + #include <linux/hpfs_fs.h>
  48. + #endif
  49.   
  50.   struct file_system_type file_systems[] = {
  51. ***************
  52. *** 58,61 ****
  53. --- 61,67 ----
  54.   #ifdef CONFIG_ISO9660_FS
  55.       {isofs_read_super,    "iso9660",    1},
  56. + #endif
  57. + #ifdef CONFIG_HPFS_FS
  58. +     {hpfs_read_super,    "hpfs",        1},
  59.   #endif
  60.       {NULL,            NULL,        0}
  61. diff -rc2N linux-dist/fs/hpfs/Makefile linux/fs/hpfs/Makefile
  62. *** linux-dist/fs/hpfs/Makefile
  63. --- linux/fs/hpfs/Makefile    Mon Oct 18 19:49:24 1993
  64. ***************
  65. *** 0 ****
  66. --- 1,33 ----
  67. + #
  68. + # Makefile for the linux HPFS filesystem routines.
  69. + #
  70. + # Note! Dependencies are done automagically by 'make dep', which also
  71. + # removes any old dependencies. DON'T put your own dependencies here
  72. + # unless it's something special (ie not a .c file).
  73. + #
  74. + # Note 2! The CFLAGS definitions are now in the main makefile...
  75. + .c.s:
  76. +     $(CC) $(CFLAGS) -S $<
  77. + .c.o:
  78. +     $(CC) $(CFLAGS) -c $<
  79. + .s.o:
  80. +     $(AS) -o $*.o $<
  81. + OBJS=    hpfs_fs.o
  82. + hpfs.o: $(OBJS)
  83. +     ln -f hpfs_fs.o hpfs.o
  84. + clean:
  85. +     rm -f core *.o *.a *.s
  86. + dep:
  87. +     $(CPP) -M *.c > .depend
  88. + #
  89. + # include a dependency file if one exists
  90. + #
  91. + ifeq (.depend,$(wildcard .depend))
  92. + include .depend
  93. + endif
  94. diff -rc2N linux-dist/fs/hpfs/hpfs.h linux/fs/hpfs/hpfs.h
  95. *** linux-dist/fs/hpfs/hpfs.h
  96. --- linux/fs/hpfs/hpfs.h    Sat Oct 23 20:41:49 1993
  97. ***************
  98. *** 0 ****
  99. --- 1,499 ----
  100. + /* The paper
  101. +      Duncan, Roy
  102. +      Design goals and implementation of the new High Performance File System
  103. +      Microsoft Systems Journal  Sept 1989  v4 n5 p1(13)
  104. +    describes what HPFS looked like when it was new, and it is the source
  105. +    of most of the information given here.  The rest is conjecture.
  106. +    For definitive information on the Duncan paper, see it, not this file.
  107. +    For definitive information on HPFS, ask somebody else -- this is guesswork.
  108. +    There are certain to be many mistakes. */
  109. + /* Notation */
  110. + typedef unsigned secno;            /* sector number, partition relative */
  111. + typedef secno dnode_secno;        /* sector number of a dnode */
  112. + typedef secno fnode_secno;        /* sector number of an fnode */
  113. + typedef secno anode_secno;        /* sector number of an anode */
  114. + /* sector 0 */
  115. + /* The boot block is very like a FAT boot block, except that the
  116. +    29h signature byte is 28h instead, and the ID string is "HPFS". */
  117. + struct hpfs_boot_block
  118. + {
  119. +   unsigned char jmp[3];
  120. +   unsigned char oem_id[8];
  121. +   unsigned char bytes_per_sector[2];    /* 512 */
  122. +   unsigned char sectors_per_cluster;
  123. +   unsigned char n_reserved_sectors[2];
  124. +   unsigned char n_fats;
  125. +   unsigned char n_rootdir_entries[2];
  126. +   unsigned char n_sectors_s[2];
  127. +   unsigned char media_byte;
  128. +   unsigned short sectors_per_fat;
  129. +   unsigned short sectors_per_track;
  130. +   unsigned short heads_per_cyl;
  131. +   unsigned int n_hidden_sectors;
  132. +   unsigned int n_sectors_l;        /* size of partition */
  133. +   unsigned char drive_number;
  134. +   unsigned char mbz;
  135. +   unsigned char sig_28h;        /* 28h */
  136. +   unsigned char vol_serno[4];
  137. +   unsigned char vol_label[11];
  138. +   unsigned char sig_hpfs[8];        /* "HPFS    " */
  139. +   unsigned char pad[448];
  140. +   unsigned short magic;            /* aa55 */
  141. + };
  142. + /* sector 16 */
  143. + /* The super block has the pointer to the root directory. */
  144. + #define SB_MAGIC 0xf995e849
  145. + struct hpfs_super_block
  146. + {
  147. +   unsigned magic;            /* f995 e849 */
  148. +   unsigned magic1;            /* fa53 e9c5, more magic? */
  149. +   unsigned huh202;            /* 202 ?? version number(s)?
  150. +                           sector size again?
  151. +                              dnode multiplier?
  152. +                           n_spare_dnodes? */
  153. +   fnode_secno root;            /* fnode of root directory */
  154. +   secno n_sectors;            /* size of filesystem */
  155. +   unsigned n_badblocks;            /* number of bad blocks */
  156. +   secno bitmaps;            /* pointers to free space bit maps */
  157. +   unsigned zero1;            /* 0 */
  158. +   secno badblocks;            /* bad block list */
  159. +   unsigned zero3;            /* 0 */
  160. +   time_t last_chkdsk;            /* date last checked, 0 if never */
  161. +   unsigned zero4;            /* 0 */
  162. +   secno n_dir_band;            /* number of sectors in dir band */
  163. +   secno dir_band_start;            /* first sector in dir band */
  164. +   secno dir_band_end;            /* last sector in dir band */
  165. +   secno dir_band_bitmap;        /* free space map, 1 dnode per bit */
  166. +   unsigned zero5[8];            /* 0 */
  167. +   secno scratch_dnodes;            /* ?? 8 preallocated sectors near dir
  168. +                        band, 4-aligned. */
  169. +   unsigned zero6[103];            /* 0 */
  170. + };
  171. + /* sector 17 */
  172. + /* The spare block has pointers to spare sectors.  */
  173. + #define SP_MAGIC 0xf9911849
  174. + struct hpfs_spare_block
  175. + {
  176. +   unsigned magic;            /* f991 1849 */
  177. +   unsigned magic1;            /* fa52 29c5, more magic? */
  178. +   unsigned dirty;            /* 0 clean, 1 "improperly stopped" */
  179. +   secno hotfix_map;            /* info about remapped bad sectors */
  180. +   unsigned n_spares_used;        /* number of hotfixes */
  181. +   unsigned n_spares;            /* number of spares in hotfix map */
  182. +   unsigned n_dnode_spares_free;        /* spare dnodes unused */
  183. +   unsigned n_dnode_spares;        /* length of spare_dnodes[] list,
  184. +                        follows in this block*/
  185. +   secno code_page_dir;            /* code page directory block */
  186. +   unsigned n_code_pages;        /* number of code pages */
  187. +   unsigned large_numbers[2];        /* ?? */
  188. +   unsigned zero1[15];
  189. +   dnode_secno spare_dnodes[20];        /* emergency free dnode list */
  190. +   unsigned zero2[81];            /* room for more? */
  191. + };
  192. + /* The bad block list is 4 sectors long.  The first word must be zero,
  193. +    the remaining words give n_badblocks bad block numbers.
  194. +    I can't resist. */
  195. + #define BAD_MAGIC 0
  196. +        
  197. + /* The hotfix map is 4 sectors long.  It looks like
  198. +        secno from[n_spares];
  199. +        secno to[n_spares];
  200. +    The to[] list is initalized to point to n_spares preallocated empty
  201. +    sectors.  The from[] list contains the sector numbers of bad blocks
  202. +    which have been remapped to corresponding sectors in the to[] list.
  203. +    n_spares_used gives the length of the from[] list. */
  204. + /* Sectors 18 and 19 are preallocated and unused.
  205. +    Maybe they're spares for 16 and 17, but simple substitution fails. */
  206. + /* The code page info pointed to by the spare block consists of an index
  207. +    block and blocks containing character maps.  The following is pretty
  208. +    sketchy, but Linux is Latin-1 all the way so it doesn't matter. */
  209. + /* block pointed to by spareblock->code_page_dir */
  210. + #define CP_DIR_MAGIC 0x494521f7
  211. + struct code_page_directory
  212. + {
  213. +   unsigned magic;            /* 4945 21f7 */
  214. +   unsigned n_code_pages;        /* number of pointers following */
  215. +   unsigned zero1[2];
  216. +   struct {
  217. +     unsigned short ix;            /* index */
  218. +     unsigned short code_page_number;    /* code page number */
  219. +     unsigned bounds;            /* matches corresponding word
  220. +                        in data block */
  221. +     secno code_page_data;        /* sector number of a code_page_data
  222. +                        containing c.p. array */
  223. +     unsigned index;            /* index in c.p. array in that sector*/
  224. +   } array[31];                /* unknown length */
  225. + };
  226. + /* blocks pointed to by code_page_directory */
  227. + #define CP_DATA_MAGIC 0x894521f7
  228. + struct code_page_data
  229. + {
  230. +   unsigned magic;            /* 8945 21f7 */
  231. +   unsigned n_used;            /* # elements used in c_p_data[] */
  232. +   unsigned bounds[3];            /* looks a bit like
  233. +                          (beg1,end1), (beg2,end2)
  234. +                        one byte each */
  235. +   unsigned short offs[3];        /* offsets from start of sector
  236. +                        to start of c_p_data[ix] */
  237. +   struct {
  238. +     unsigned short ix;            /* index */
  239. +     unsigned short code_page_number;    /* code page number */
  240. +     unsigned short zero1;
  241. +     unsigned char map[128];        /* map for chars 80..ff */
  242. +     unsigned short zero2;
  243. +   } code_page[3];
  244. +   unsigned char incognita[78];
  245. + };
  246. + /* Free space bitmaps are 4 sectors long, which is 16384 bits.
  247. +    16384 sectors is 8 meg, and each 8 meg band has a 4-sector bitmap.
  248. +    Bit order in the maps is little-endian.  0 means taken, 1 means free.
  249. +    Bit map sectors are marked allocated in the bit maps, and so are sectors 
  250. +    off the end of the partition.
  251. +    Band 0 is sectors 0-3fff, its map is in sectors 18-1b.
  252. +    Band 1 is 4000-7fff, its map is in 7ffc-7fff.
  253. +    Band 2 is 8000-ffff, its map is in 8000-8003.
  254. +    The remaining bands have maps in their first (even) or last (odd) 4 sectors
  255. +      -- if the last, partial, band is odd its map is in its last 4 sectors.
  256. +    The bitmap locations are given in a table pointed to by the super block.
  257. +    No doubt they aren't constrained to be at 18, 7ffc, 8000, ...; that is
  258. +    just where they usually are.
  259. +    The "directory band" is a bunch of sectors preallocated for dnodes.
  260. +    It has a 4-sector free space bitmap of its own.  Each bit in the map
  261. +    corresponds to one 4-sector dnode, bit 0 of the map corresponding to
  262. +    the first 4 sectors of the directory band.  The entire band is marked
  263. +    allocated in the main bitmap.   The super block gives the locations
  264. +    of the directory band and its bitmap.  ("band" doesn't mean it is
  265. +    8 meg long; it isn't.)  */
  266. + /* dnode: directory.  4 sectors long */
  267. + /* A directory is a tree of dnodes.  There is one pointer to the root of
  268. +    the tree, in the fnode for the directory.  Every dnode in the tree
  269. +    points up to this fnode, and the parent directory points down to it.
  270. +    The fnode never moves, the dnodes do the B-tree thing, splitting
  271. +    and merging as files are added and removed.  */
  272. + #define DNODE_MAGIC   0x77e40aae
  273. + struct dnode {
  274. +   unsigned magic;            /* 77e4 0aae */
  275. +   unsigned first_free;            /* offset from start of dnode to
  276. +                        first free dir entry */
  277. +   unsigned increment_me;        /* some kind of activity counter?
  278. +                        Neither HPFS.IFS nor CHKDSK cares
  279. +                        if you change this word */
  280. +   secno up;                /* (root dnode) directory's fnode
  281. +                        (nonroot) parent dnode */
  282. +   dnode_secno self;            /* pointer to this dnode */
  283. +   unsigned char dirent[2028];        /* one or more dirents */
  284. + };
  285. + struct hpfs_dirent {
  286. +   unsigned short length;        /* offset to next dirent */
  287. +   unsigned first: 1;            /* set on phony ^A^A (".") entry */
  288. +   unsigned flag1: 1;
  289. +   unsigned down: 1;            /* down pointer present (after name) */
  290. +   unsigned last: 1;            /* set on phony \377 entry */
  291. +   unsigned flag4: 1;
  292. +   unsigned flag5: 1;
  293. +   unsigned flag6: 1;
  294. +   unsigned has_needea: 1;        /* ? some EA has NEEDEA set
  295. +                        I have no idea why this would be
  296. +                        needed in a dir entry */
  297. +   unsigned read_only: 1;        /* dos attrib */
  298. +   unsigned hidden: 1;            /* dos attrib */
  299. +   unsigned system: 1;            /* dos attrib */
  300. +   unsigned flag11: 1;            /* would be volume label dos attrib */
  301. +   unsigned directory: 1;        /* dos attrib */
  302. +   unsigned archive: 1;            /* dos attrib */
  303. +   unsigned not_8x3: 1;            /* name is not 8.3 */
  304. +   unsigned flag15: 1;
  305. +   fnode_secno fnode;            /* fnode giving allocation info */
  306. +   time_t write_date;            /* mtime */
  307. +   unsigned file_size;            /* file length, bytes */
  308. +   time_t read_date;            /* atime */
  309. +   time_t creation_date;            /* ctime */
  310. +   unsigned ea_size;            /* total EA length, bytes */
  311. +   unsigned char zero1;
  312. +   unsigned char locality;        /* ? 0=unk 1=seq 2=random 3=both */
  313. +   unsigned char namelen, name[1];    /* file name */
  314. +   /* dnode_secno down;      btree down pointer, if present,
  315. +                    follows name on next word boundary, or maybe it's
  316. +               precedes next dirent, which is on a word boundary. */
  317. + };
  318. + /* The b-tree down pointer from a dir entry */
  319. + static inline dnode_secno de_down_pointer (struct hpfs_dirent *de)
  320. + {
  321. +   return *(dnode_secno *) ((void *) de + de->length - 4);
  322. + }
  323. + /* The first dir entry in a dnode */
  324. + static inline struct hpfs_dirent *dnode_first_de (struct dnode *dnode)
  325. + {
  326. +   return (void *) dnode->dirent;
  327. + }
  328. + /* The end+1 of the dir entries */
  329. + static inline struct hpfs_dirent *dnode_end_de (struct dnode *dnode)
  330. + {
  331. +   return (void *) dnode + dnode->first_free;
  332. + }
  333. + /* The dir entry after dir entry de */
  334. + static inline struct hpfs_dirent *de_next_de (struct hpfs_dirent *de)
  335. + {
  336. +   return (void *) de + de->length;
  337. + }
  338. + /* B+ tree: allocation info in fnodes and anodes */
  339. + /* dnodes point to fnodes which are responsible for listing the sectors
  340. +    assigned to the file.  This is done with trees of (length,address)
  341. +    pairs.  (Actually triples, of (length, file-address, disk-address)
  342. +    which can represent holes.  Find out if HPFS does that.)
  343. +    At any rate, fnodes contain a small tree; if subtrees are needed
  344. +    they occupy essentially a full block in anodes.  A leaf-level tree node
  345. +    has 3-word entries giving sector runs, a non-leaf node has 2-word
  346. +    entries giving subtree pointers.  A flag in the header says which. */
  347. + struct bplus_leaf_node
  348. + {
  349. +   unsigned file_secno;            /* first file sector in extent */
  350. +   unsigned length;            /* length, sectors */
  351. +   secno disk_secno;            /* first corresponding disk sector */
  352. + };
  353. + struct bplus_internal_node
  354. + {
  355. +   unsigned file_secno;            /* subtree maps sectors < this  */
  356. +   anode_secno down;            /* pointer to subtree */
  357. + };
  358. + struct bplus_header
  359. + {
  360. +   unsigned flag0: 1;
  361. +   unsigned flag1: 1;
  362. +   unsigned flag2: 1;
  363. +   unsigned flag3: 1;
  364. +   unsigned flag4: 1;
  365. +   unsigned fnode_parent: 1;        /* ? we're pointed to by an fnode,
  366. +                        the data btree or some ea or the
  367. +                        main ea bootage pointer ea_secno */
  368. +                     /* also can get set in fnodes, which
  369. +                        may be a chkdsk bug or may mean
  370. +                        this bit is irrelevant in fnodes,
  371. +                        or this interpretation is all wet */
  372. +   unsigned flag6: 1;
  373. +   unsigned internal: 1;            /* 1 -> (internal) tree of anodes
  374. +                        0 -> (leaf) list of extents */
  375. +   unsigned char fill[3];
  376. +   unsigned char n_free_nodes;        /* free nodes in following array */
  377. +   unsigned char n_used_nodes;        /* used nodes in following array */
  378. +   unsigned short first_free;        /* offset from start of header to
  379. +                        first free node in array */
  380. +   union {
  381. +     struct bplus_internal_node internal[0]; /* (internal) 2-word entries giving
  382. +                            subtree pointers */
  383. +     struct bplus_leaf_node external[0];        /* (external) 3-word entries giving
  384. +                            sector runs */
  385. +   } u;
  386. + };
  387. + /* fnode: root of allocation b+ tree, and EA's */
  388. + /* Every file and every directory has one fnode, pointed to by the directory
  389. +    entry and pointing to the file's sectors or directory's root dnode.  EA's
  390. +    are also stored here, and there are said to be ACL's somewhere here too. */
  391. + #define FNODE_MAGIC 0xf7e40aae
  392. + struct fnode
  393. + {
  394. +   unsigned magic;            /* f7e4 0aae */
  395. +   unsigned zero1[2];
  396. +   unsigned char len, name[15];        /* true length, truncated name */
  397. +   fnode_secno up;            /* pointer to file's directory fnode */
  398. +   unsigned zero2[3];
  399. +   unsigned ea_size_l;            /* length of disk-resident ea's */
  400. +   secno ea_secno;            /* first sector of disk-resident ea's*/
  401. +   unsigned short ea_size_s;        /* length of fnode-resident ea's */
  402. +   unsigned flag0: 1;
  403. +   unsigned ea_anode: 1;            /* 1 -> ea_secno is an anode */
  404. +   unsigned flag2: 1;
  405. +   unsigned flag3: 1;
  406. +   unsigned flag4: 1;
  407. +   unsigned flag5: 1;
  408. +   unsigned flag6: 1;
  409. +   unsigned flag7: 1;
  410. +   unsigned dirflag: 1;            /* 1 -> directory.  first & only extent
  411. +                        points to dnode. */
  412. +   unsigned flag9: 1;
  413. +   unsigned flag10: 1;
  414. +   unsigned flag11: 1;
  415. +   unsigned flag12: 1;
  416. +   unsigned flag13: 1;
  417. +   unsigned flag14: 1;
  418. +   unsigned flag15: 1;
  419. +   struct bplus_header btree;        /* b+ tree, 8 extents or 12 subtrees */
  420. +   union {
  421. +     struct bplus_leaf_node external[8];
  422. +     struct bplus_internal_node internal[12];
  423. +   } u;
  424. +   unsigned file_size;            /* file length, bytes */
  425. +   unsigned n_needea;            /* number of required EA's */
  426. +   unsigned zero4[4];
  427. +   unsigned ea_offs;            /* offset from start of fnode
  428. +                        to first fnode-resident ea */
  429. +   unsigned zero5[2];
  430. +   unsigned char ea[316];        /* zero or more EA's, packed together
  431. +                        with no alignment padding.
  432. +                        (Do not use this field, get here
  433. +                        via fnode + ea_offs. I think.) */
  434. + };
  435. + /* anode: 99.44% pure allocation tree */
  436. + #define ANODE_MAGIC 0x37e40aae
  437. + struct anode
  438. + {
  439. +   unsigned magic;            /* 37e4 0aae */
  440. +   anode_secno self;            /* pointer to this anode */
  441. +   secno up;                /* parent anode or fnode */
  442. +   struct bplus_header btree;        /* b+tree, 40 extents or 60 subtrees */
  443. +   union {
  444. +     struct bplus_leaf_node external[40];
  445. +     struct bplus_internal_node internal[60];
  446. +   } u;
  447. +   unsigned fill[3];            /* unused */
  448. + };
  449. + /* extended attributes.
  450. +    A file's EA info is stored as a list of (name,value) pairs.  It is
  451. +    usually in the fnode, but (if it's large) it is moved to a single
  452. +    sector run outside the fnode, or to multiple runs with an anode tree
  453. +    that points to them.
  454. +    The value of a single EA is stored along with the name, or (if large)
  455. +    it is moved to a single sector run, or multiple runs pointed to by an
  456. +    anode tree, pointed to by the value field of the (name,value) pair.
  457. +    Flags in the EA tell whether the value is immediate, in a single sector
  458. +    run, or in multiple runs.  Flags in the fnode tell whether the EA list
  459. +    is immediate, in a single run, or in multiple runs. */
  460. + struct extended_attribute
  461. + {
  462. +   unsigned indirect: 1;            /* 1 -> value gives sector number
  463. +                        where real value starts */
  464. +   unsigned anode: 1;            /* 1 -> sector is an anode
  465. +                        that points to fragmented value */
  466. +   unsigned flag2: 1;
  467. +   unsigned flag3: 1;
  468. +   unsigned flag4: 1;
  469. +   unsigned flag5: 1;
  470. +   unsigned flag6: 1;
  471. +   unsigned needea: 1;            /* required ea */
  472. +   unsigned char namelen;        /* length of name, bytes */
  473. +   unsigned short valuelen;        /* length of value, bytes */
  474. +   /*
  475. +     unsigned char name[namelen];    ascii attrib name
  476. +     unsigned char nul;            terminating '\0', not counted
  477. +     unsigned char value[valuelen];    value, arbitrary
  478. +       if this.indirect, valuelen is 8 and the value is
  479. +         unsigned length;        real length of value, bytes
  480. +         secno secno;            sector address where it starts
  481. +       if this.anode, the above sector number is the root of an anode tree
  482. +         which points to the value.
  483. +   */
  484. + };
  485. + static inline unsigned char *ea_name (struct extended_attribute *ea)
  486. + {
  487. +   return (void *) ea + sizeof *ea;
  488. + }
  489. + static inline unsigned char *ea_value (struct extended_attribute *ea)
  490. + {
  491. +   return (void *) ea + sizeof *ea + ea->namelen + 1;
  492. + }
  493. + static inline struct extended_attribute *
  494. +     ea_next_ea (struct extended_attribute *ea)
  495. + {
  496. +   return (void *) ea + sizeof *ea + ea->namelen + 1 + ea->valuelen;
  497. + }
  498. + static inline unsigned ea_indirect_length (struct extended_attribute *ea)
  499. + {
  500. +   unsigned *v = (void *) ea_value (ea);
  501. +   return v[0];
  502. + }
  503. + static inline unsigned ea_indirect_secno (struct extended_attribute *ea)
  504. + {
  505. +   unsigned *v = (void *) ea_value (ea);
  506. +   return v[1];
  507. + }
  508. + /*
  509. +    Local Variables:
  510. +    comment-column: 40
  511. +    End:
  512. + */
  513. diff -rc2N linux-dist/fs/hpfs/hpfs_fs.c linux/fs/hpfs/hpfs_fs.c
  514. *** linux-dist/fs/hpfs/hpfs_fs.c
  515. --- linux/fs/hpfs/hpfs_fs.c    Sat Oct 23 20:46:59 1993
  516. ***************
  517. *** 0 ****
  518. --- 1,1451 ----
  519. + /*
  520. +  *  linux/fs/hpfs/hhpfs_fs.c
  521. +  *
  522. +  *  Sources & references:
  523. +  *   Duncan, _Design ... of HPFS_, MSSJ 4(5)   (C) 1989 Microsoft Corp
  524. +  *   linux/fs/minix  Copyright (C) 1991, 1992, 1993  Linus Torvalds
  525. +  *   linux/fs/msdos  Written 1992, 1993 by Werner Almesberger
  526. +  *   linux/fs/isofs  Copyright (C) 1991  Eric Youngdale
  527. +  */
  528. + /* read-only HPFS */
  529. + #include <linux/fs.h>
  530. + #include <linux/hpfs_fs.h>
  531. + #include <linux/errno.h>
  532. + #include <linux/sched.h>
  533. + #include <linux/locks.h>
  534. + #include <linux/stat.h>
  535. + #include <linux/string.h>
  536. + #include <asm/bitops.h>
  537. + #include <asm/segment.h>
  538. + #include "hpfs.h"
  539. + /* HPFS is a mixture of 512-byte blocks and 2048-byte blocks.  The 2k blocks
  540. +    are used for directories (dnodes) and bitmaps.  For bmap to work, we must
  541. +    run the file system with 512-byte blocks.  The 2k blocks are read into
  542. +    buffers obtained from kmalloc.
  543. +    For a file's i-number we use the sector number of its fnode, coded.
  544. +    (Directory ino's are even, file ino's are odd, and ino >> 1 is the
  545. +    sector address of the fnode.  This is a hack to allow lookup() to
  546. +    tell read_inode() whether it is necessary to read the fnode.)
  547. +    The map_xxx routines all read something into a buffer and return a
  548. +    pointer somewhere in the buffer.  The caller must do the brelse.
  549. +    The other routines are balanced.
  550. +    For details on the data structures see hpfs.h and the Duncan paper.
  551. +    Overview
  552. +    [ The names of these data structures, except fnode, are not Microsoft's
  553. +    or IBM's.  I don't know what names they use.  The semantics described
  554. +    here are those of this implementation, and any coincidence between it
  555. +    and real HPFS is to be hoped for but not guaranteed by me, and
  556. +    certainly not guaranteed by MS or IBM.  Who know nothing about this. ]
  557. +    [ Also, the following will make little sense if you haven't read the
  558. +    Duncan paper, which is excellent. ]
  559. +    HPFS is a tree.  There are 3 kinds of nodes.  A directory is a tree
  560. +    of dnodes, and a file's allocation info is a tree of sector runs
  561. +    stored in fnodes and anodes.
  562. +    The top pointer is in the super block, it points to the fnode of the
  563. +    root directory.
  564. +    
  565. +    The root directory -- all directories -- gives file names, dates &c,
  566. +    and fnode addresses.  If the directory fits in one dnode, that's it,
  567. +    otherwise the top dnode points to other dnodes, forming a tree.  A
  568. +    dnode tree (one directory) might look like
  569. +        ((a b c) d (e f g) h (i j) k l (m n o p))
  570. +    The subtrees appear between the files.  Each dir entry contains, along
  571. +    with the name and fnode, a dnode pointer to the subtree that precedes it
  572. +    (if there is one; a flag tells that).  The first entry in every directory
  573. +    is ^A^A, the "." entry for the directory itself.  The last entry in every
  574. +    dnode is \377, a fake entry whose only valid fields are the bit marking
  575. +    it last and the down pointer to the subtree preceding it, if any.
  576. +    The "value" field of directory entries is an fnode address.  The fnode
  577. +    tells where the sectors of the file are.  The fnode for a subdirectory
  578. +    contains one pointer, to the root dnode of the subdirectory.  The fnode
  579. +    for a data file contains, in effect, a tiny anode.  (Most of the space
  580. +    in fnodes is for extended attributes.)
  581. +    anodes and the anode part of fnodes are trees of extents.  An extent
  582. +    is a (length, disk address) pair, labeled with the file address being
  583. +    mapped.  E.g.,
  584. +        (0: 3@1000  3: 1@2000  4: 2@10)
  585. +    means the file:disk sector map (0:1000 1:1001 2:1002 3:2000 4:10 5:11).
  586. +    There is space for 8 file:len@disk triples in an fnode, or for 40 in an
  587. +    anode.  If this is insufficient, subtrees are used, as in
  588. +     (6: (0: 3@1000  3: 1@2000  4: 2@10)  12: (6: 3@8000  9: 1@9000  10: 2@20))
  589. +    The label on a subtree is the first address *after* that tree.  The
  590. +    subtrees are always anodes.  The label:subtree pairs require only
  591. +    two words each, so non-leaf subtrees have a different format; there
  592. +    is room for 12 label:subtree pairs in an fnode, or 60 in an anode.
  593. +    Within a directory, each dnode contains a pointer up to its parent
  594. +    dnode.  The root dnode points up to the directory's fnode.
  595. +    Each fnode contains a pointer to the directory that contains it
  596. +    (to the fnode of the directory).  So this pointer in a directory
  597. +    fnode is "..".
  598. +    On the disk, dnodes are all together in the center of the partition, and
  599. +    HPFS even manages to put all the dnodes for a single directory together,
  600. +    generally.   fnodes are out with the data.  anodes are pretty much not
  601. +    seen -- in fact noncontiguous files are pretty much not seen.  I think
  602. +    this is partly the open() call that lets programs specify the length of
  603. +    an output file when they know it, and partly because HPFS.IFS really is
  604. +    very good at resisting fragmentation. */
  605. + /* notation */
  606. + #define little_ushort(x) (*(unsigned short *) &(x))
  607. + typedef void nonconst;
  608. + /* super block ops */
  609. + static void hpfs_read_inode (struct inode *);
  610. + static void hpfs_put_super (struct super_block *);
  611. + static void hpfs_statfs (struct super_block *, struct statfs *);
  612. + static int hpfs_remount_fs (struct super_block *, int *);
  613. + static const struct super_operations hpfs_sops =
  614. + {
  615. +   hpfs_read_inode,            /* read_inode */
  616. +   NULL,                    /* notify_change */
  617. +   NULL,                    /* write_inode */
  618. +   NULL,                    /* put_inode */
  619. +   hpfs_put_super,            /* put_super */
  620. +   NULL,                    /* write_super */
  621. +   hpfs_statfs,                /* statfs */
  622. +   hpfs_remount_fs,            /* remount_fs */
  623. + };
  624. + /* file ops */
  625. + static int hpfs_file_read (struct inode *, struct file *, char *, int);
  626. + static secno hpfs_bmap (struct inode *, unsigned);
  627. + static const struct file_operations hpfs_file_ops = 
  628. + {
  629. +   NULL,                    /* lseek - default */
  630. +   hpfs_file_read,            /* read */
  631. +   NULL,                    /* write */
  632. +   NULL,                    /* readdir - bad */
  633. +   NULL,                    /* select - default */
  634. +   NULL,                    /* ioctl - default */
  635. +   generic_mmap,                /* mmap */
  636. +   NULL,                    /* no special open is needed */
  637. +   NULL,                    /* release */
  638. +   file_fsync,                /* fsync */
  639. + };
  640. + static const struct inode_operations hpfs_file_iops = 
  641. + {
  642. +   (nonconst *) &hpfs_file_ops,        /* default file operations */
  643. +   NULL,                    /* create */
  644. +   NULL,                    /* lookup */
  645. +   NULL,                    /* link */
  646. +   NULL,                    /* unlink */
  647. +   NULL,                    /* symlink */
  648. +   NULL,                    /* mkdir */
  649. +   NULL,                    /* rmdir */
  650. +   NULL,                    /* mknod */
  651. +   NULL,                    /* rename */
  652. +   NULL,                    /* readlink */
  653. +   NULL,                    /* follow_link */
  654. +   (int (*)(struct inode *,int))
  655. +   &hpfs_bmap,                /* bmap */
  656. +   NULL,                    /* truncate */
  657. +   NULL,                    /* permission */
  658. + };
  659. + /* directory ops */
  660. + static int hpfs_dir_read (struct inode * inode, struct file *filp,
  661. +                char *buf, int count);
  662. + static int hpfs_readdir (struct inode *inode, struct file *filp,
  663. +              struct dirent *dirent, int count);
  664. + static int hpfs_lookup (struct inode *,const char *,int,struct inode **);
  665. + static const struct file_operations hpfs_dir_ops = 
  666. + {
  667. +   NULL,                    /* lseek - default */
  668. +   hpfs_dir_read,            /* read */
  669. +   NULL,                    /* write - bad */
  670. +   hpfs_readdir,                /* readdir */
  671. +   NULL,                    /* select - default */
  672. +   NULL,                    /* ioctl - default */
  673. +   NULL,                    /* mmap */
  674. +   NULL,                    /* no special open code */
  675. +   NULL,                    /* no special release code */
  676. +   file_fsync,                /* fsync */
  677. + };
  678. + static const struct inode_operations hpfs_dir_iops =
  679. + {
  680. +   (nonconst *) &hpfs_dir_ops,        /* default directory file ops */
  681. +   NULL,                    /* create */
  682. +   hpfs_lookup,                /* lookup */
  683. +   NULL,                    /* link */
  684. +   NULL,                    /* unlink */
  685. +   NULL,                    /* symlink */
  686. +   NULL,                    /* mkdir */
  687. +   NULL,                    /* rmdir */
  688. +   NULL,                    /* mknod */
  689. +   NULL,                    /* rename */
  690. +   NULL,                    /* readlink */
  691. +   NULL,                    /* follow_link */
  692. +   NULL,                    /* bmap */
  693. +   NULL,                    /* truncate */
  694. +   NULL,                    /* permission */
  695. + };
  696. + /* 4 512-byte buffers and the 2k block obtained by concatenating them */
  697. + struct quad_buffer_head
  698. + {
  699. +   struct buffer_head *bh[4];
  700. +   void *data;
  701. + };
  702. + /* forwards */
  703. + static int parse_opts (char *opts, uid_t *uid, gid_t *gid, umode_t *umask,
  704. +                int *lowercase, int *conv);
  705. + static int check_warn (int not_ok,
  706. +                const char *p1, const char *p2, const char *p3);
  707. + static int zerop (void *addr, unsigned len);
  708. + static void count_dnodes (struct inode *inode, dnode_secno dno,
  709. +               unsigned *n_dnodes, unsigned *n_subdirs);
  710. + static unsigned count_bitmap (struct super_block *s);
  711. + static unsigned count_one_bitmap (dev_t dev, secno secno);
  712. + static secno bplus_lookup (struct inode *inode, struct bplus_header *b,
  713. +                secno file_secno, struct buffer_head **bhp);
  714. + static struct hpfs_dirent *map_dirent (struct inode *inode, dnode_secno dno,
  715. +                        const char *name, unsigned len,
  716. +                        struct quad_buffer_head *qbh);
  717. + static struct hpfs_dirent *map_pos_dirent (struct inode *inode, off_t *posp,
  718. +                        struct quad_buffer_head *qbh);
  719. + static void write_one_dirent (struct dirent *dirent, const unsigned char *name,
  720. +                   unsigned namelen, ino_t ino, int lowercase);
  721. + static dnode_secno dir_subdno (struct inode *inode, unsigned pos);
  722. + static struct hpfs_dirent *map_nth_dirent (dev_t dev, dnode_secno dno,
  723. +                        int n,
  724. +                        struct quad_buffer_head *qbh);
  725. + static unsigned choose_conv (unsigned char *p, unsigned len);
  726. + static unsigned convcpy_tofs (unsigned char *out, unsigned char *in,
  727. +                   unsigned len);
  728. + static dnode_secno fnode_dno (dev_t dev, ino_t ino);
  729. + static void *map_fnode (dev_t dev, ino_t ino, struct buffer_head **bhp);
  730. + static void *map_anode (dev_t dev, unsigned secno, struct buffer_head **bhp);
  731. + static void *map_dnode (dev_t dev, unsigned secno,
  732. +             struct quad_buffer_head *qbh);
  733. + static void *map_sector (dev_t dev, unsigned secno, struct buffer_head **bhp);
  734. + static void *map_4sectors (dev_t dev, unsigned secno,
  735. +                struct quad_buffer_head *qbh);
  736. + static void brelse4 (struct quad_buffer_head *qbh);
  737. + /* make inode number for a file */
  738. + static inline ino_t file_ino (fnode_secno secno)
  739. + {
  740. +   return secno << 1 | 1;
  741. + }
  742. + /* make inode number for a directory */
  743. + static inline ino_t dir_ino (fnode_secno secno)
  744. + {
  745. +   return secno << 1;
  746. + }
  747. + /* get fnode address from an inode number */
  748. + static inline fnode_secno ino_secno (ino_t ino)
  749. + {
  750. +   return ino >> 1;
  751. + }
  752. + /* test for directory's inode number */
  753. + static inline int ino_is_dir (ino_t ino)
  754. + {
  755. +   return (ino & 1) == 0;
  756. + }
  757. + /* conv= options */
  758. + #define CONV_BINARY 0        /* no conversion */
  759. + #define CONV_TEXT 1        /* crlf->newline */
  760. + #define CONV_AUTO 2        /* decide based on file contents */
  761. + /* local time (HPFS) to GMT (Unix) */
  762. + static inline time_t local_to_gmt (time_t t)
  763. + {
  764. +   extern struct timezone sys_tz;
  765. +   return t + sys_tz.tz_minuteswest * 60;
  766. + }
  767. + /* super block ops */
  768. + /* mount.  This gets one thing, the root directory inode.  It does a
  769. +    bunch of guessed-at consistency checks.  */
  770. + struct super_block *hpfs_read_super (struct super_block *s,
  771. +                      void *options, int silent)
  772. + {
  773. +   struct hpfs_boot_block *bootblock;
  774. +   struct hpfs_super_block *superblock;
  775. +   struct hpfs_spare_block *spareblock;
  776. +   struct hpfs_dirent *de;
  777. +   struct buffer_head *bh0, *bh1, *bh2;
  778. +   struct quad_buffer_head qbh;
  779. +   dnode_secno root_dno;
  780. +   dev_t dev; 
  781. +   uid_t uid;
  782. +   gid_t gid;
  783. +   umode_t umask;
  784. +   int lowercase;
  785. +   int conv;
  786. +   int dubious;
  787. +   /* Get the mount options */
  788. +   if (! parse_opts (options, &uid, &gid, &umask, &lowercase, &conv)) {
  789. +     printk ("HPFS: syntax error in mount options.  Not mounted.\n");
  790. +     s->s_dev = 0; return 0;
  791. +   }
  792. +   /* Fill in the super block struct */
  793. +   lock_super (s);
  794. +   dev = s->s_dev;
  795. +   set_blocksize (dev, 512);
  796. +   /* fetch sectors 0, 16, 17 */
  797. +   bootblock = map_sector (dev, 0, &bh0);
  798. +   if (! bootblock) goto bail;
  799. +   superblock = map_sector (dev, 16, &bh1);
  800. +   if (! superblock) goto bail0;
  801. +   spareblock = map_sector (dev, 17, &bh2);
  802. +   if (! spareblock) goto bail1;
  803. +   /* Check that this fs looks enough like a known one that we can find
  804. +      and read the root directory. */
  805. +   
  806. +   if (bootblock->magic != 0xaa55
  807. +       || superblock->magic != SB_MAGIC
  808. +       || spareblock->magic != SP_MAGIC
  809. +       || bootblock->sig_28h != 0x28
  810. +       || memcmp (&bootblock->sig_hpfs, "HPFS    ", 8)
  811. +       || little_ushort (bootblock->bytes_per_sector) != 512) {
  812. +     printk ("HPFS: hpfs_read_super: Not HPFS\n");
  813. +     goto bail2;
  814. +   }
  815. +   /* Check for inconsistencies -- possibly wrong guesses here, possibly
  816. +      filesystem problems. */
  817. +   dubious = 0;
  818. +   dubious |= check_warn (spareblock->dirty != 0,
  819. +              "`Improperly stopped'", "flag is set", "run CHKDSK");
  820. +   dubious |= check_warn (spareblock->n_spares_used != 0,
  821. +              "Spare blocks", "may be in use", "run CHKDSK");
  822. +   /* Above errors mean we could get into serious trouble if we proceed,
  823. +      so don't */
  824. +   if (dubious) goto bail2;
  825. +   dubious |= check_warn ((spareblock->n_dnode_spares !=
  826. +               spareblock ->n_dnode_spares_free),
  827. +              "Spare dnodes", "may be in use", "run CHKDSK");
  828. +   dubious |= check_warn (superblock->zero1 != 0,
  829. +              "#1", "unknown word nonzero", "investigate");
  830. +   dubious |= check_warn (superblock->zero3 != 0,
  831. +              "#3", "unknown word nonzero", "investigate");
  832. +   dubious |= check_warn (superblock->zero4 != 0,
  833. +              "#4", "unknown word nonzero", "investigate");
  834. +   dubious |= check_warn (! zerop (superblock->zero5, sizeof superblock->zero5),
  835. +              "#5", "unknown word nonzero", "investigate");
  836. +   dubious |= check_warn (! zerop (superblock->zero6, sizeof superblock->zero6),
  837. +              "#6", "unknown word nonzero", "investigate");
  838. +   if (dubious)
  839. +     printk ("HPFS: Proceeding, but operation may be unreliable\n");
  840. +   /* set fs read only */
  841. +   s->s_flags |= MS_RDONLY;
  842. +   /* fill in standard stuff */
  843. +   s->s_magic = HPFS_SUPER_MAGIC;
  844. +   s->s_blocksize = 512;
  845. +   s->s_blocksize_bits = 9;
  846. +   s->s_op = (struct super_operations *) &hpfs_sops;
  847. +   /* fill in hpfs stuff */
  848. +   s->s_hpfs_root = dir_ino (superblock->root);
  849. +   s->s_hpfs_fs_size = superblock->n_sectors;
  850. +   s->s_hpfs_dirband_size = superblock->n_dir_band / 4;
  851. +   s->s_hpfs_dmap = superblock->dir_band_bitmap;
  852. +   s->s_hpfs_bitmaps = superblock->bitmaps;
  853. +   s->s_hpfs_uid = uid;
  854. +   s->s_hpfs_gid = gid;
  855. +   s->s_hpfs_mode = 0777 & ~umask;
  856. +   s->s_hpfs_n_free = -1;
  857. +   s->s_hpfs_n_free_dnodes = -1;
  858. +   s->s_hpfs_lowercase = lowercase;
  859. +   s->s_hpfs_conv = conv;
  860. +   /* done with the low blocks */
  861. +   brelse (bh2); brelse (bh1); brelse (bh0);
  862. +   /* all set.  try it out. */
  863. +   s->s_mounted = iget (s, s->s_hpfs_root);
  864. +   unlock_super (s);
  865. +   if (! s->s_mounted) {
  866. +     printk ("HPFS: hpfs_read_super: inode get failed\n");
  867. +     s->s_dev = 0;
  868. +     return 0;
  869. +   } 
  870. +   /* find the root directory's . pointer & finish filling in the inode */
  871. +   root_dno = fnode_dno (dev, s->s_hpfs_root);
  872. +   if (root_dno)
  873. +     de = map_dirent (s->s_mounted, root_dno, "\001\001", 2, &qbh);
  874. +   if (! root_dno || ! de) {
  875. +     printk ("HPFS: hpfs_read_super: root dir isn't in the root dir\n");
  876. +     s->s_dev = 0;
  877. +     return 0;
  878. +   }
  879. +   s->s_mounted->i_atime = local_to_gmt (de->read_date);
  880. +   s->s_mounted->i_mtime = local_to_gmt (de->write_date);
  881. +   s->s_mounted->i_ctime = local_to_gmt (de->creation_date);
  882. +   brelse4 (&qbh);
  883. +   return s;
  884. +  bail2:
  885. +   brelse (bh2);
  886. +  bail1:
  887. +   brelse (bh1);
  888. +  bail0:
  889. +   brelse (bh0);
  890. +  bail:
  891. +   s->s_dev = 0;
  892. +   unlock_super (s);
  893. +   return 0;
  894. + }
  895. + static int check_warn (int not_ok,
  896. +                const char *p1, const char *p2, const char *p3)
  897. + {
  898. +   if (not_ok)
  899. +     printk ("HPFS: %s %s. Please %s\n", p1, p2, p3);
  900. +   return not_ok;
  901. + }
  902. + static int zerop (void *addr, unsigned len)
  903. + {
  904. +   unsigned char *p = addr;
  905. +   return p[0] == 0 && memcmp (p, p + 1, len - 1) == 0;
  906. + }
  907. + /* A tiny parser for option strings, stolen from dosfs. */
  908. + static int parse_opts (char *opts, uid_t *uid, gid_t *gid, umode_t *umask,
  909. +                int *lowercase, int *conv)
  910. + {
  911. +   char *p, *rhs;
  912. +   *uid = current->uid;
  913. +   *gid = current->gid;
  914. +   *umask = current->umask;
  915. +   *lowercase = 1;
  916. +   *conv = CONV_BINARY;
  917. +   if (! opts) return 1;
  918. +   for (p = strtok (opts, ","); p != 0; p = strtok (0, ",")) {
  919. +     if ((rhs = strchr (p, '=')) != 0)
  920. +       *rhs++ = '\0';
  921. +     if (! strcmp (p, "uid")) {
  922. +       if (!rhs || !*rhs) return 0;
  923. +       *uid = simple_strtoul (rhs, &rhs, 0);
  924. +       if (*rhs) return 0;
  925. +     } else if (! strcmp (p, "gid")) {
  926. +       if (!rhs || !*rhs) return 0;
  927. +       *gid = simple_strtoul (rhs, &rhs, 0);
  928. +       if (*rhs) return 0;
  929. +     } else if (! strcmp (p, "umask")) {
  930. +       if (!rhs || !*rhs) return 0;
  931. +       *umask = simple_strtoul (rhs,&rhs,8);
  932. +       if  (*rhs) return 0;
  933. +     } else if (! strcmp (p, "case")) {
  934. +       if (! strcmp (rhs, "lower")) *lowercase = 1;
  935. +       else if (! strcmp (rhs, "asis")) *lowercase = 0;
  936. +       else return 0;
  937. +     } else if (! strcmp (p, "conv")) {
  938. +       if (! strcmp (rhs, "binary")) *conv = CONV_BINARY;
  939. +       else if (! strcmp (rhs, "text")) *conv = CONV_TEXT;
  940. +       else if (! strcmp (rhs, "auto")) *conv = CONV_AUTO;
  941. +       else return 0;
  942. +     } else
  943. +       return 0;
  944. +   }
  945. +   return 1;
  946. + }
  947. + /* read_inode.  This is called with exclusive access to a new inode that
  948. +    has only (i_dev,i_ino) set.  It is responsible for filling in the rest.
  949. +    We evilly leave the dates blank, for lookup() to fill in later.  
  950. +    Lookup has the dir entry, all we have here is the inode number.
  951. +    It really is evil -- lookup does not have the inode exclusively and
  952. +    has no business writing in it -- but the alternative is to read the
  953. +    fnode, find the dir pointer in it, read that fnode to get the dnode
  954. +    pointer, search through that whole directory for the ino we're
  955. +    reading, and get the dates.  It works, but ls sounds like fsck.
  956. +    It is ok to do this unless (a) iget sleeps between calling us and
  957. +    returning to lookup, or (b) someone other than lookup calls iget.
  958. +    Neither one ever happens right now, 0.99.13.  (b) is not a big
  959. +    threat since lookup is the only source of inode numbers.
  960. +    A good way to test for breakage is two processes running du on
  961. +    the same hpfs partition, not in the buffer cache.  They will soon
  962. +    sync up waiting for directory i/o in lookup, and have the race
  963. +    that needs to work. */
  964. + static void hpfs_read_inode (struct inode *inode)
  965. + {
  966. +   struct super_block *s = inode->i_sb;
  967. +   /* be ready to bail out */
  968. +   inode->i_op = 0;
  969. +   inode->i_mode = 0;
  970. +   if (inode->i_ino == 0
  971. +       || ino_secno (inode->i_ino) >= inode->i_sb->s_hpfs_fs_size)
  972. +     { printk ("HPFS: read_inode: bad ino\n"); return; }
  973. +   /* canned stuff */
  974. +   inode->i_uid = s->s_hpfs_uid;
  975. +   inode->i_gid = s->s_hpfs_gid;
  976. +   inode->i_mode = s->s_hpfs_mode;
  977. +   inode->i_hpfs_conv = s->s_hpfs_conv;
  978. +   inode->i_hpfs_dno = 0;
  979. +   inode->i_hpfs_n_secs = 0;
  980. +   inode->i_hpfs_file_sec = 0;
  981. +   inode->i_hpfs_disk_sec = 0;
  982. +   inode->i_hpfs_dpos = 0;
  983. +   inode->i_hpfs_dsubdno = 0;
  984. +   /* figure out whether we are looking at a directory or a file */
  985. +   if (ino_is_dir (inode->i_ino))
  986. +     inode->i_mode |= S_IFDIR;
  987. +   else {
  988. +     inode->i_mode |= S_IFREG;
  989. +     inode->i_mode &= ~0111;
  990. +   }
  991. +   /* these fields must be filled in from the dir entry, which we don't have
  992. +      but lookup does.  It will fill them in before letting the inode
  993. +      out of its grasp. */
  994. +   inode->i_atime = 0;
  995. +   inode->i_mtime = 0;
  996. +   inode->i_ctime = 0;
  997. +   inode->i_size = 0;
  998. +   /* fill in the rest */
  999. +   if (S_ISREG (inode->i_mode)) {
  1000. +     inode->i_op = (struct inode_operations *) &hpfs_file_iops;
  1001. +     inode->i_nlink = 1;
  1002. +     inode->i_blksize = 512;
  1003. +   } else {
  1004. +     unsigned n_dnodes, n_subdirs;
  1005. +     struct buffer_head *bh0;
  1006. +     struct fnode *fnode = map_fnode (inode->i_dev, inode->i_ino, &bh0);
  1007. +     if (! fnode)
  1008. +       { printk ("HPFS: read_inode: no fnode\n"); inode->i_mode = 0; return; }
  1009. +     inode->i_hpfs_parent_dir = dir_ino (fnode->up);
  1010. +     inode->i_hpfs_dno = fnode->u.external[0].disk_secno;
  1011. +     brelse (bh0);
  1012. +     n_dnodes = n_subdirs = 0;
  1013. +     count_dnodes (inode, inode->i_hpfs_dno, &n_dnodes, &n_subdirs);
  1014. +     inode->i_op = (struct inode_operations *) &hpfs_dir_iops;
  1015. +     inode->i_blksize = 512;        /* 2048 here confuses ls & du & ...*/
  1016. +     inode->i_blocks = 4 * n_dnodes;
  1017. +     inode->i_size = 512 * inode->i_blocks;
  1018. +     inode->i_nlink = 2 + n_subdirs;
  1019. +   }
  1020. + }
  1021. + /* unmount. */
  1022. + static void hpfs_put_super (struct super_block *s)
  1023. + {
  1024. +   lock_super (s);
  1025. +   s->s_dev = 0;
  1026. +   unlock_super (s);
  1027. + }
  1028. + /* statfs.  We report results in 512-byte sectors, not our 2k buffer size.
  1029. +    For free inode counts we report the count of dnodes in the directory band
  1030. +    -- not exactly right but pretty analagous. */
  1031. + static void hpfs_statfs (struct super_block *s, struct statfs *buf)
  1032. + {
  1033. +   /* count the bits in the bitmaps, unless we already have */
  1034. +   if (s->s_hpfs_n_free == -1) {
  1035. +     s->s_hpfs_n_free = count_bitmap (s);
  1036. +     s->s_hpfs_n_free_dnodes = count_one_bitmap (s->s_dev, s->s_hpfs_dmap);
  1037. +   }
  1038. +   /* fill in the user's statfs struct */
  1039. +   put_fs_long (s->s_magic, &buf->f_type);
  1040. +   put_fs_long (512, &buf->f_bsize);
  1041. +   put_fs_long (s->s_hpfs_fs_size, &buf->f_blocks);
  1042. +   put_fs_long (s->s_hpfs_n_free, &buf->f_bfree);
  1043. +   put_fs_long (s->s_hpfs_n_free, &buf->f_bavail);
  1044. +   put_fs_long (s->s_hpfs_dirband_size, &buf->f_files);
  1045. +   put_fs_long (s->s_hpfs_n_free_dnodes, &buf->f_ffree);
  1046. +   put_fs_long (254, &buf->f_namelen);
  1047. + }
  1048. + /* remount. */
  1049. + static int hpfs_remount_fs (struct super_block *s, int *flags)
  1050. + {
  1051. +   /* don't let read only be turned off */
  1052. +   if (! (*flags & MS_RDONLY))
  1053. +     return -EINVAL;
  1054. +   return 0;
  1055. + }
  1056. + /* count the dnodes in a directory, and the subdirs. */
  1057. + static void count_dnodes (struct inode *inode, dnode_secno dno,
  1058. +               unsigned *n_dnodes, unsigned *n_subdirs)
  1059. + {
  1060. +   struct quad_buffer_head qbh;
  1061. +   struct dnode *dnode;
  1062. +   struct hpfs_dirent *de;
  1063. +   struct hpfs_dirent *de_end;
  1064. +   dnode = map_dnode (inode->i_dev, dno, &qbh);
  1065. +   if (! dnode) return;
  1066. +   de = dnode_first_de (dnode);
  1067. +   de_end = dnode_end_de (dnode);
  1068. +   (*n_dnodes)++;
  1069. +   for ( ; de < de_end; de = de_next_de (de)) {
  1070. +     if (de->down)
  1071. +       count_dnodes (inode, de_down_pointer (de), n_dnodes, n_subdirs);
  1072. +     if (de->directory && ! de->first)
  1073. +       (*n_subdirs)++;
  1074. +     if (de->last || de->length == 0)
  1075. +       break;
  1076. +   }
  1077. +   brelse4 (&qbh);
  1078. + }
  1079. + /* count the bits in the free space bit maps */
  1080. + static unsigned count_bitmap (struct super_block *s)
  1081. + {
  1082. +   unsigned n, count, n_bands;
  1083. +   secno *bitmaps;
  1084. +   struct quad_buffer_head qbh;
  1085. +   /* there is one bit map for each 16384 sectors */
  1086. +   n_bands = (s->s_hpfs_fs_size + 0x3fff) >> 14;
  1087. +   /* their locations are given in an array pointed to by the super block */ 
  1088. +   bitmaps = map_4sectors (s->s_dev, s->s_hpfs_bitmaps, &qbh);
  1089. +   if (! bitmaps) return 0;
  1090. +   count = 0;
  1091. +   /* map each one and count the free sectors */
  1092. +   for (n = 0; n < n_bands; n++)
  1093. +     if (bitmaps[n] == 0)
  1094. +       printk ("HPFS: bit map pointer missing\n");
  1095. +     else
  1096. +       count += count_one_bitmap (s->s_dev, bitmaps[n]);
  1097. +   brelse4 (&qbh);
  1098. +   return count;
  1099. + }
  1100. + /* Read in one bit map, count the bits, return the count. */
  1101. + static unsigned count_one_bitmap (dev_t dev, secno secno)
  1102. + {
  1103. +   struct quad_buffer_head qbh;
  1104. +   char *bits; 
  1105. +   unsigned i, count;
  1106. +   bits = map_4sectors (dev, secno, &qbh);
  1107. +   if (! bits) return 0;
  1108. +   count = 0;
  1109. +   for (i = 0; i < 8 * 2048; i++)
  1110. +     count += (test_bit (i, bits) != 0);
  1111. +   brelse4 (&qbh);
  1112. +   return count;
  1113. + }
  1114. + /* file ops */
  1115. + /* read.  Read the bytes, put them in buf, return the count. */
  1116. + static int hpfs_file_read (struct inode *inode, struct file *filp,
  1117. +                char *buf, int count)
  1118. + {
  1119. +   unsigned q, r, n, n0;
  1120. +   struct buffer_head *bh;
  1121. +   char *block;
  1122. +   char *start;
  1123. +   if (inode == 0 || ! S_ISREG (inode->i_mode))
  1124. +     return -EINVAL;
  1125. +   /* truncate count at EOF */
  1126. +   if (count > inode->i_size - filp->f_pos)
  1127. +     count = inode->i_size - filp->f_pos;
  1128. +   
  1129. +   start = buf;
  1130. +   while (count > 0) {
  1131. +     /* get file sector number, offset in sector, length to end of sector */
  1132. +     q = filp->f_pos >> 9;
  1133. +     r = filp->f_pos & 511;
  1134. +     n = 512 - r;
  1135. +     /* get length to copy to user buffer */
  1136. +     if (n > count)
  1137. +       n = count;
  1138. +     /* read the sector, copy to user */
  1139. +     block = map_sector (inode->i_dev, hpfs_bmap (inode, q), &bh);
  1140. +     if (! block) return -EIO;
  1141. +     /* but first decide if it has \r\n, if the mount option said to */
  1142. +     if (inode->i_hpfs_conv == CONV_AUTO)
  1143. +       inode->i_hpfs_conv = choose_conv (block + r, n);
  1144. +     if (inode->i_hpfs_conv == CONV_BINARY) {
  1145. +       /* regular copy, output length is same as input length */
  1146. +       memcpy_tofs (buf, block + r, n);
  1147. +       n0 = n;
  1148. +     } else {
  1149. +       /* squeeze out \r, output length varies */
  1150. +       n0 = convcpy_tofs (buf, block + r, n);
  1151. +       if (count > inode->i_size - filp->f_pos - n + n0)
  1152. +     count = inode->i_size - filp->f_pos - n + n0;
  1153. +     }
  1154. +     brelse (bh);
  1155. +     /* advance input n bytes, output n0 bytes */
  1156. +     filp->f_pos += n;
  1157. +     buf += n0;
  1158. +     count -= n0;
  1159. +   }
  1160. +   return buf - start;
  1161. + }
  1162. + /* This routine implements conv=auto.  Return CONV_BINARY or CONV_TEXT. */
  1163. + static unsigned choose_conv (unsigned char *p, unsigned len)
  1164. + {
  1165. +   unsigned tvote, bvote;
  1166. +   unsigned c;
  1167. +   tvote = bvote = 0;
  1168. +   while (len--) {
  1169. +     c = *p++;
  1170. +     if (c < ' ')
  1171. +       if (c == '\r' && len && *p == '\n') tvote += 10;
  1172. +       else if (c == '\t' || c == '\n') ;
  1173. +       else bvote += 5;
  1174. +     else if (c < '\177')
  1175. +       tvote++;
  1176. +     else
  1177. +       bvote += 5;
  1178. +   }
  1179. +   if (tvote > bvote)
  1180. +     return CONV_TEXT;
  1181. +   else
  1182. +     return CONV_BINARY;
  1183. + }
  1184. + /* This routine implements conv=text.  :s/crlf/nl/  */
  1185. + static unsigned convcpy_tofs (unsigned char *out, unsigned char *in,
  1186. +                   unsigned len)
  1187. + {
  1188. +   unsigned char *start = out;
  1189. +   while (len--) {
  1190. +     unsigned c = *in++;
  1191. +     if (c == '\r' && (len == 0 || *in == '\n'))
  1192. +       ;
  1193. +     else
  1194. +       put_fs_byte (c, out++);
  1195. +   }
  1196. +   return out - start;
  1197. + }
  1198. + /* Return the disk sector number containing a file sector. */
  1199. + static secno hpfs_bmap (struct inode *inode, unsigned file_secno)
  1200. + {
  1201. +   unsigned n, disk_secno;
  1202. +   struct fnode *fnode;
  1203. +   struct buffer_head *bh;
  1204. +   /* There is one sector run cached in the inode.
  1205. +      See if the sector is in it. */
  1206. +   n = file_secno - inode->i_hpfs_file_sec;
  1207. +   if (n < inode->i_hpfs_n_secs)
  1208. +     return inode->i_hpfs_disk_sec + n;
  1209. +   /* No, read the fnode and go find the sector. */
  1210. +   else {
  1211. +     fnode = map_fnode (inode->i_dev, inode->i_ino, &bh);
  1212. +     if (! fnode) return 0;
  1213. +     disk_secno = bplus_lookup (inode, &fnode->btree, file_secno, &bh);
  1214. +     brelse (bh);
  1215. +     return disk_secno;
  1216. +   }
  1217. + }
  1218. + /* Search allocation tree *b for the given file sector number and return
  1219. +    the disk sector number.  Buffer *bhp has the tree in it, and can be
  1220. +    reused for subtrees when access to *b is no longer needed.
  1221. +    *bhp is busy on entry and exit. */
  1222. + static secno bplus_lookup (struct inode *inode, struct bplus_header *b,
  1223. +                secno file_secno, struct buffer_head **bhp)
  1224. + {
  1225. +   int i;
  1226. +   /* A leaf-level tree gives a list of sector runs.  Find the one
  1227. +      containing the file sector we want, cache the map info in the inode
  1228. +      for later, and return the corresponding disk sector. */
  1229. +   if (! b->internal) {
  1230. +     struct bplus_leaf_node *n = b->u.external;
  1231. +     for (i = 0; i < b->n_used_nodes; i++) {
  1232. +       unsigned t = file_secno - n[i].file_secno;
  1233. +       if (t < n[i].length) {
  1234. +     inode->i_hpfs_file_sec = n[i].file_secno;
  1235. +     inode->i_hpfs_disk_sec = n[i].disk_secno;
  1236. +     inode->i_hpfs_n_secs = n[i].length;
  1237. +     return n[i].disk_secno + t;
  1238. +       }
  1239. +     }
  1240. +   }
  1241. +   /* A non-leaf tree gives a list of subtrees.  Find the one containing
  1242. +      the file sector we want, read it in, and recurse to search it. */
  1243. +   else {
  1244. +     struct bplus_internal_node *n = b->u.internal;
  1245. +     for (i = 0; i < b->n_used_nodes; i++) {
  1246. +       if (file_secno < n[i].file_secno) {
  1247. +     struct anode *anode;
  1248. +     anode_secno ano = n[i].down;
  1249. +     brelse (*bhp);
  1250. +     anode = map_anode (inode->i_dev, ano, bhp);
  1251. +     if (! anode) break;
  1252. +     return bplus_lookup (inode, &anode->btree, file_secno, bhp);
  1253. +       }
  1254. +     }
  1255. +   }
  1256. +   /* If we get here there was a hole in the file.  As far as I know
  1257. +      we never do get here, but falling off the end would be indelicate.
  1258. +      So return a pointer to a handy all-zero sector.  This is not a
  1259. +      reasonable way to handle files with holes if they really do happen. */
  1260. +   printk ("HPFS: bplus_lookup: sector not found\n");
  1261. +   return 15;
  1262. + }
  1263. + /* directory ops */
  1264. + /* lookup.  Search the directory tree for the specified name, set
  1265. +    *result to the corresponding inode.
  1266. +    lookup uses the inode number to tell read_inode whether it is reading
  1267. +    the inode of a directory or a file -- file ino's are odd, directory
  1268. +    ino's are even.  read_inode avoids i/o for file inodes; everything
  1269. +    needed is up here in the directory.  And file fnodes are out in
  1270. +    the boondocks. */
  1271. + static int hpfs_lookup (struct inode *dir, const char *name, int len,
  1272. +             struct inode **result)
  1273. + {
  1274. +   struct quad_buffer_head qbh;
  1275. +   struct hpfs_dirent *de;
  1276. +   struct inode *inode;
  1277. +   ino_t ino;
  1278. +   /* In case of madness */
  1279. +   *result = 0;
  1280. +   if (dir == 0)
  1281. +     return -ENOENT;
  1282. +   if (! S_ISDIR (dir->i_mode))
  1283. +     goto bail;
  1284. +   
  1285. +   /* Read in the directory entry.
  1286. +      "." is there under the name ^A^A .
  1287. +      Always read the dir even for . and .. in case we need the dates. */
  1288. +   if (name[0] == '.' && len == 1)
  1289. +     de = map_dirent (dir, dir->i_hpfs_dno, "\001\001", 2, &qbh);
  1290. +   else if (name[0] == '.' && name[1] == '.' && len == 2)
  1291. +     de = map_dirent (dir, fnode_dno (dir->i_dev, dir->i_hpfs_parent_dir),
  1292. +              "\001\001", 2, &qbh);
  1293. +   else
  1294. +     de = map_dirent (dir, dir->i_hpfs_dno, name, len, &qbh);
  1295. +   /* This is not really a bailout, just means file not found. */
  1296. +   if (! de) goto bail;
  1297. +   /* Get inode number, what we're after. */
  1298. +   if (de->directory)
  1299. +     ino = dir_ino (de->fnode);
  1300. +   else
  1301. +     ino = file_ino (de->fnode);
  1302. +   /* Go find or make an inode. */
  1303. +   if (! (inode = iget (dir->i_sb, ino)))
  1304. +     goto bail1;
  1305. +   /* Stuff in the info from the directory if this is a newly created inode. */
  1306. +   if (! inode->i_atime) {
  1307. +     inode->i_atime = local_to_gmt (de->read_date);
  1308. +     inode->i_mtime = local_to_gmt (de->write_date);
  1309. +     inode->i_ctime = local_to_gmt (de->creation_date);
  1310. +     if (de->read_only)
  1311. +       inode->i_mode &= ~0222;
  1312. +     if (! de->directory) {
  1313. +       inode->i_size = de->file_size;
  1314. +       /* i_blocks should count the fnode and any anodes.  We count 1 for
  1315. +      the fnode and don't bother about anodes -- the disk heads are
  1316. +      on the directory band and we want them to stay there.  If files
  1317. +      can have holes, this may have to go look at the fnode. */
  1318. +       inode->i_blocks = 1 + ((inode->i_size + 511) >> 9);
  1319. +     }
  1320. +   }
  1321. +   brelse4 (&qbh);
  1322. +   /* Made it. */
  1323. +   *result = inode;
  1324. +   iput (dir);
  1325. +   return 0;
  1326. +   /* Didn't. */
  1327. +  bail1:
  1328. +   brelse4 (&qbh);
  1329. +  bail:
  1330. +   iput (dir);
  1331. +   return -ENOENT;
  1332. + }
  1333. + /* Compare two counted strings ignoring case.
  1334. +    HPFS directory order sorts letters as if they're upper case. */
  1335. + static inline int memcasecmp (unsigned char *s1, unsigned char *s2, unsigned n)
  1336. + {
  1337. +   int t;
  1338. +   if (n != 0)
  1339. +     do {
  1340. +       unsigned c1 = *s1++;
  1341. +       unsigned c2 = *s2++;
  1342. +       if (c1 - 'a' < 26) c1 -= 040;
  1343. +       if (c2 - 'a' < 26) c2 -= 040;
  1344. +       if ((t = c1 - c2) != 0)
  1345. +     return t;
  1346. +     } while (--n != 0);
  1347. +   return 0;
  1348. + }
  1349. + /* Search a directory for the given name, return a pointer to its dir entry
  1350. +    and a pointer to the buffer containing it. */
  1351. + static struct hpfs_dirent *map_dirent (struct inode *inode, dnode_secno dno,
  1352. +                        const char *name, unsigned len,
  1353. +                        struct quad_buffer_head *qbh)
  1354. + {
  1355. +   struct dnode *dnode;
  1356. +   struct hpfs_dirent *de;
  1357. +   struct hpfs_dirent *de_end;
  1358. +   int t, l;
  1359. +   /* read the dnode at the root of our subtree */
  1360. +   dnode = map_dnode (inode->i_dev, dno, qbh);
  1361. +   if (! dnode) return 0;
  1362. +   /* get pointers to start and end+1 of dir entries */
  1363. +   de = dnode_first_de (dnode);
  1364. +   de_end = dnode_end_de (dnode);
  1365. +   /* look through the entries for the name we're after */
  1366. +   for ( ; de < de_end; de = de_next_de (de)) {
  1367. +     /* compare names */
  1368. +     l = len < de->namelen ? len : de->namelen;
  1369. +     t = memcasecmp (name, de->name, l);
  1370. +     /* initial substring matches, compare lengths */
  1371. +     if (t == 0) {
  1372. +       t = len - de->namelen;
  1373. +       /* bingo */
  1374. +       if (t == 0)
  1375. +     return de;
  1376. +     }
  1377. +       
  1378. +     /* wanted name .lt. dir name => not present. */
  1379. +     if (t < 0) {
  1380. +       /* if there is a subtree, search it. */
  1381. +       if (de->down) {
  1382. +     dnode_secno sub_dno = de_down_pointer (de);
  1383. +     brelse4 (qbh);
  1384. +     return map_dirent (inode, sub_dno, name, len, qbh);
  1385. +       } else
  1386. +     break;
  1387. +     }
  1388. +     /* de->last is set on the last name in the dnode (it's always a "\377"
  1389. +        pseudo entry).  de->length == 0 means we're about to infinite loop.
  1390. +        This test does nothing in a well-formed dnode. */
  1391. +     if (de->last || de->length == 0)
  1392. +       break;
  1393. +   }
  1394. +   /* name not found. */
  1395. +   return 0;
  1396. + }
  1397. + /* readdir.  Return exactly 1 dirent.  (I tried and tried, but currently
  1398. +    the interface with libc just does not permit more than 1.  If it gets
  1399. +    fixed, throw this out and just walk the tree and write records into
  1400. +    the user buffer.)
  1401. +    We keep track of our position in the dnode tree with a sort of
  1402. +    dewey-decimal record of subtree locations.  Like so:
  1403. +    (1 (1.1 1.2 1.3) 2 3 (3.1 (3.1.1 3.1.2) 3.2 3.3 (3.3.1)) 4)
  1404. +    Subtrees appear after their file, out of lexical order,
  1405. +    which would be before their file.  It's easier.
  1406. +    A directory can't hold more than 56 files, so 6 bits are used for
  1407. +    position numbers.  If the tree is so deep that the position encoding
  1408. +    doesn't fit, I'm sure something absolutely fascinating happens.
  1409. +    The actual sequence of f_pos values is
  1410. +        0 => .   -1 => ..   1 1.1 ... 8.9 9 => files  -2 => eof
  1411. +    The directory inode caches one position-to-dnode correspondence so
  1412. +    we won't have to repeatedly scan the top levels of the tree. */
  1413. + static int hpfs_readdir (struct inode *inode, struct file *filp,
  1414. +              struct dirent *dirent, int likely_story)
  1415. + {
  1416. +   struct quad_buffer_head qbh;
  1417. +   struct hpfs_dirent *de;
  1418. +   int namelen, lc;
  1419. +   ino_t ino;
  1420. +   if (inode == 0
  1421. +       || inode->i_sb == 0
  1422. +       || ! S_ISDIR (inode->i_mode))
  1423. +     return -EBADF;
  1424. +   lc = inode->i_sb->s_hpfs_lowercase;
  1425. +   switch (filp->f_pos) {
  1426. +   case 0:
  1427. +     write_one_dirent (dirent, ".", 1, inode->i_ino, lc);
  1428. +     filp->f_pos = -1;
  1429. +     return 1;
  1430. +   case -1:
  1431. +     write_one_dirent (dirent, "..", 2, inode->i_hpfs_parent_dir, lc);
  1432. +     filp->f_pos = 1;
  1433. +     return 2;
  1434. +     
  1435. +   case -2:
  1436. +     return 0;
  1437. +   default:
  1438. +     de = map_pos_dirent (inode, &filp->f_pos, &qbh);
  1439. +     if (! de) {
  1440. +       filp->f_pos = -2;
  1441. +       return 0;
  1442. +     }
  1443. +     namelen = de->namelen;
  1444. +     if (de->directory)
  1445. +       ino = dir_ino (de->fnode);
  1446. +     else
  1447. +       ino = file_ino (de->fnode);
  1448. +     write_one_dirent (dirent, de->name, namelen, ino, lc);
  1449. +     brelse4 (&qbh);
  1450. +     return namelen;
  1451. +   }
  1452. + } 
  1453. + /* Send the given name and ino off to the user dirent struct at *dirent.
  1454. +    Blam it to lowercase if the mount option said to.
  1455. +    Note that Linux d_reclen is the length of the file name, and has nothing
  1456. +    to do with the length of the dirent record.  Why is that, do you ask?
  1457. +    I sure do.  Jeez. */
  1458. + static void write_one_dirent (struct dirent *dirent, const unsigned char *name,
  1459. +                   unsigned namelen, ino_t ino, int lowercase)
  1460. + {
  1461. +   unsigned n;
  1462. +   put_fs_long (ino, &dirent->d_ino);
  1463. +   put_fs_word (namelen, &dirent->d_reclen);
  1464. +   if (lowercase)
  1465. +     for (n = namelen; n != 0; ) {
  1466. +       unsigned t = name[--n];
  1467. +       if (t - 'A' < 26) t += 040;
  1468. +       put_fs_byte (t, &dirent->d_name[n]);
  1469. +     }
  1470. +   else
  1471. +     memcpy_tofs (dirent->d_name, name, namelen);
  1472. +   put_fs_byte (0, &dirent->d_name[namelen]);
  1473. + }
  1474. + /* Map the dir entry at subtree coordinates given by *posp, and
  1475. +    increment *posp to point to the following dir entry. */
  1476. + static struct hpfs_dirent *map_pos_dirent (struct inode *inode, off_t *posp,
  1477. +                        struct quad_buffer_head *qbh)
  1478. + {
  1479. +   unsigned pos, q, r;
  1480. +   dnode_secno dno;
  1481. +   struct hpfs_dirent *de;
  1482. +   /* Get the position code and split off the rightmost index r */
  1483. +   pos = *posp;
  1484. +   q = pos >> 6;
  1485. +   r = pos & 077;
  1486. +   /* Get the sector address of the dnode pointed to by the leading part q */
  1487. +   dno = dir_subdno (inode, q);
  1488. +   if (! dno) return 0;
  1489. +   /* Get the entry at index r in dnode q */
  1490. +   de = map_nth_dirent (inode->i_dev, dno, r, qbh);
  1491. +   /* If none, we're out of files in this dnode.  Ascend. */
  1492. +   if (! de) {
  1493. +     if (q == 0) return 0;
  1494. +     *posp = q + 1;
  1495. +     return map_pos_dirent (inode, posp, qbh);
  1496. +   }
  1497. +   /* If a subtree is here, descend. */
  1498. +   if (de->down)
  1499. +     *posp = pos << 6 | 1;
  1500. +   else
  1501. +     *posp = pos + 1;
  1502. +   /* Don't return the ^A^A and \377 entries. */
  1503. +   if (de->first || de->last) {
  1504. +     brelse4 (qbh);
  1505. +     return map_pos_dirent (inode, posp, qbh);
  1506. +   } else
  1507. +     return de;
  1508. + }
  1509. + /* Return the address of the dnode with subtree coordinates given by pos. */
  1510. + static dnode_secno dir_subdno (struct inode *inode, unsigned pos)
  1511. + {
  1512. +   struct hpfs_dirent *de;
  1513. +   struct quad_buffer_head qbh;
  1514. +   /* 0 is the root dnode */
  1515. +   if (pos == 0)
  1516. +     return inode->i_hpfs_dno;
  1517. +   /* we have one pos->dnode translation cached in the inode */
  1518. +   else if (pos == inode->i_hpfs_dpos)
  1519. +     return inode->i_hpfs_dsubdno;
  1520. +   /* otherwise go look */
  1521. +   else {
  1522. +     unsigned q = pos >> 6;
  1523. +     unsigned r = pos & 077;
  1524. +     dnode_secno dno;
  1525. +     /* dnode at position q */
  1526. +     dno = dir_subdno (inode, q);
  1527. +     if (dno == 0) return 0;
  1528. +     /* entry at index r */
  1529. +     de = map_nth_dirent (inode->i_dev, dno, r, &qbh);
  1530. +     if (! de || ! de->down) return 0;
  1531. +     /* get the dnode down pointer */
  1532. +     dno = de_down_pointer (de);
  1533. +     brelse4 (&qbh);
  1534. +     /* cache it for next time */
  1535. +     inode->i_hpfs_dpos = pos;
  1536. +     inode->i_hpfs_dsubdno = dno;
  1537. +     return dno;
  1538. +   }
  1539. + }
  1540. + /* Return the dir entry at index n in dnode dno, or 0 if there isn't one */
  1541. + static struct hpfs_dirent *map_nth_dirent (dev_t dev, dnode_secno dno,
  1542. +                        int n,
  1543. +                        struct quad_buffer_head *qbh)
  1544. + {
  1545. +   int i;
  1546. +   struct hpfs_dirent *de, *de_end;
  1547. +   struct dnode *dnode = map_dnode (dev, dno, qbh);
  1548. +   de = dnode_first_de (dnode);
  1549. +   de_end = dnode_end_de (dnode);
  1550. +   for (i = 1; de < de_end; i++, de = de_next_de (de)) {
  1551. +     if (i == n)
  1552. +       return de;
  1553. +     if (de->last || de->length == 0)
  1554. +       break;
  1555. +   }
  1556. +   brelse4 (qbh);
  1557. +   return 0;
  1558. + }
  1559. + static int hpfs_dir_read (struct inode *inode, struct file *filp,
  1560. +               char *buf, int count)
  1561. + {
  1562. +   return -EISDIR;
  1563. + }
  1564. + /* Return the dnode pointer in a directory fnode */
  1565. + static dnode_secno fnode_dno (dev_t dev, ino_t ino)
  1566. + {
  1567. +   struct buffer_head *bh;
  1568. +   struct fnode *fnode;
  1569. +   dnode_secno dno; 
  1570. +   fnode = map_fnode (dev, ino, &bh);
  1571. +   if (! fnode) return 0;
  1572. +   dno = fnode->u.external[0].disk_secno;
  1573. +   brelse (bh);
  1574. +   return dno;
  1575. + }
  1576. + /* Map an fnode into a buffer and return pointers to it and to the buffer. */
  1577. + static void *map_fnode (dev_t dev, ino_t ino, struct buffer_head **bhp)
  1578. + {
  1579. +   struct fnode *fnode;
  1580. +   if (ino == 0)
  1581. +     { printk ("HPFS: missing fnode\n"); return 0; }
  1582. +   fnode = map_sector (dev, ino_secno (ino), bhp);
  1583. +   if (fnode)
  1584. +     if (fnode->magic != FNODE_MAGIC)
  1585. +       { printk ("HPFS: map_fnode: bad fnode pointer\n");
  1586. +     brelse (*bhp); return 0; }
  1587. +   return fnode;
  1588. + }
  1589. + /* Map an anode into a buffer and return pointers to it and to the buffer. */
  1590. + static void *map_anode (dev_t dev, unsigned secno, struct buffer_head **bhp)
  1591. + {
  1592. +   struct anode *anode;
  1593. +   if (secno == 0)
  1594. +     { printk ("HPFS: missing anode\n"); return 0; }
  1595. +   anode = map_sector (dev, secno, bhp);
  1596. +   if (anode)
  1597. +     if (anode->magic != ANODE_MAGIC || anode->self != secno)
  1598. +       { printk ("HPFS: map_anode: bad anode pointer\n");
  1599. +     brelse (*bhp); return 0; }
  1600. +   return anode;
  1601. + }
  1602. + /* Map a dnode into a buffer and return pointers to it and to the buffer. */
  1603. + static void *map_dnode (dev_t dev, unsigned secno,
  1604. +             struct quad_buffer_head *qbh)
  1605. + {
  1606. +   struct dnode *dnode; 
  1607. +   if (secno == 0)
  1608. +     { printk ("HPFS: missing dnode\n"); return 0; }
  1609. +   dnode = map_4sectors (dev, secno, qbh);
  1610. +   if (dnode)
  1611. +     if (dnode->magic != DNODE_MAGIC || dnode->self != secno)
  1612. +       { printk ("HPFS: map_dnode: bad dnode pointer\n");
  1613. +     brelse4 (qbh); return 0; }
  1614. +   return dnode;
  1615. + }
  1616. + /* Map a sector into a buffer and return pointers to it and to the buffer. */
  1617. + static void *map_sector (dev_t dev, unsigned secno, struct buffer_head **bhp)
  1618. + {
  1619. +   struct buffer_head *bh;
  1620. +   if ((*bhp = bh = bread (dev, secno, 512)) != 0)
  1621. +     return bh->b_data;
  1622. +   else
  1623. +     { printk ("HPFS: map_sector: read error\n"); return 0; }
  1624. + }
  1625. + /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
  1626. + static void *map_4sectors (dev_t dev, unsigned secno,
  1627. +                struct quad_buffer_head *qbh)
  1628. + {
  1629. +   struct buffer_head *bh;
  1630. +   char *data;
  1631. +   if (secno & 3)
  1632. +     { printk ("HPFS: map_4sectors: unaligned read\n"); return 0; }
  1633. +   qbh->data = data = kmalloc (2048, GFP_KERNEL);
  1634. +   if (! data) goto bail;
  1635. +   qbh->bh[0] = bh = breada (dev, secno, secno + 1, secno + 2, secno + 3, -1);
  1636. +   if (! bh) goto bail0;
  1637. +   memcpy (data, bh->b_data, 512);
  1638. +   qbh->bh[1] = bh = bread (dev, secno + 1, 512);
  1639. +   if (! bh) goto bail1;
  1640. +   memcpy (data + 512, bh->b_data, 512);
  1641. +   qbh->bh[2] = bh = bread (dev, secno + 2, 512);
  1642. +   if (! bh) goto bail2;
  1643. +   memcpy (data + 2 * 512, bh->b_data, 512);
  1644. +   qbh->bh[3] = bh = bread (dev, secno + 3, 512);
  1645. +   if (! bh) goto bail3;
  1646. +   memcpy (data + 3 * 512, bh->b_data, 512);
  1647. +   
  1648. +   return data;
  1649. +  bail3:
  1650. +   brelse (qbh->bh[2]);
  1651. +  bail2:
  1652. +   brelse (qbh->bh[1]);
  1653. +  bail1:
  1654. +   brelse (qbh->bh[0]);
  1655. +  bail0:
  1656. +   kfree_s (data, 2048);
  1657. +  bail:
  1658. +   printk ("HPFS: map_4sectors: read error\n");
  1659. +   return 0;
  1660. + }
  1661. + /* Deallocate a 4-buffer block */
  1662. + static void brelse4 (struct quad_buffer_head *qbh)
  1663. + {
  1664. +   brelse (qbh->bh[3]);
  1665. +   brelse (qbh->bh[2]);
  1666. +   brelse (qbh->bh[1]);
  1667. +   brelse (qbh->bh[0]);
  1668. +   kfree_s (qbh->data, 2048);
  1669. + }
  1670. diff -rc2N linux-dist/include/linux/fs.h linux/include/linux/fs.h
  1671. *** linux-dist/include/linux/fs.h    Sun Sep 19 13:53:23 1993
  1672. --- linux/include/linux/fs.h    Mon Oct 18 19:49:24 1993
  1673. ***************
  1674. *** 169,172 ****
  1675. --- 169,173 ----
  1676.   #include <linux/ext_fs_i.h>
  1677.   #include <linux/ext2_fs_i.h>
  1678. + #include <linux/hpfs_fs_i.h>
  1679.   #include <linux/msdos_fs_i.h>
  1680.   #include <linux/iso_fs_i.h>
  1681. ***************
  1682. *** 209,212 ****
  1683. --- 210,214 ----
  1684.           struct ext_inode_info ext_i;
  1685.           struct ext2_inode_info ext2_i;
  1686. +         struct hpfs_inode_info hpfs_i;
  1687.           struct msdos_inode_info msdos_i;
  1688.           struct iso_inode_info isofs_i;
  1689. ***************
  1690. *** 242,245 ****
  1691. --- 244,248 ----
  1692.   #include <linux/ext_fs_sb.h>
  1693.   #include <linux/ext2_fs_sb.h>
  1694. + #include <linux/hpfs_fs_sb.h>
  1695.   #include <linux/msdos_fs_sb.h>
  1696.   #include <linux/iso_fs_sb.h>
  1697. ***************
  1698. *** 265,268 ****
  1699. --- 268,272 ----
  1700.           struct ext_sb_info ext_sb;
  1701.           struct ext2_sb_info ext2_sb;
  1702. +         struct hpfs_sb_info hpfs_sb;
  1703.           struct msdos_sb_info msdos_sb;
  1704.           struct isofs_sb_info isofs_sb;
  1705. diff -rc2N linux-dist/include/linux/hpfs_fs.h linux/include/linux/hpfs_fs.h
  1706. *** linux-dist/include/linux/hpfs_fs.h
  1707. --- linux/include/linux/hpfs_fs.h    Sat Oct 23 20:58:34 1993
  1708. ***************
  1709. *** 0 ****
  1710. --- 1,12 ----
  1711. + #ifndef _LINUX_HPFS_FS_H
  1712. + #define _LINUX_HPFS_FS_H
  1713. + /* HPFS magic number (word 0 of block 16) */
  1714. + #define HPFS_SUPER_MAGIC 0xf995e849
  1715. + /* The entry point for a VFS */
  1716. + extern struct super_block *hpfs_read_super (struct super_block *, void *, int);
  1717. + #endif
  1718. diff -rc2N linux-dist/include/linux/hpfs_fs_i.h linux/include/linux/hpfs_fs_i.h
  1719. *** linux-dist/include/linux/hpfs_fs_i.h
  1720. --- linux/include/linux/hpfs_fs_i.h    Mon Oct 18 19:49:24 1993
  1721. ***************
  1722. *** 0 ****
  1723. --- 1,24 ----
  1724. + #ifndef _HPFS_FS_I
  1725. + #define _HPFS_FS_I
  1726. + struct hpfs_inode_info {
  1727. +   ino_t i_parent_dir;        /* (directories) gives fnode of parent dir */
  1728. +   unsigned i_dno;        /* (directories) root dnode */
  1729. +   unsigned i_dpos;        /* (directories) temp for readdir */
  1730. +   unsigned i_dsubdno;        /* (directories) temp for readdir */
  1731. +   unsigned i_file_sec;        /* (files) minimalist cache of alloc info */
  1732. +   unsigned i_disk_sec;        /* (files) minimalist cache of alloc info */
  1733. +   unsigned i_n_secs;        /* (files) minimalist cache of alloc info */
  1734. +   unsigned i_conv : 2;        /* (files) crlf->newline hackery */
  1735. + };
  1736. + #define i_hpfs_dno u.hpfs_i.i_dno
  1737. + #define i_hpfs_parent_dir u.hpfs_i.i_parent_dir
  1738. + #define i_hpfs_n_secs u.hpfs_i.i_n_secs
  1739. + #define i_hpfs_file_sec u.hpfs_i.i_file_sec
  1740. + #define i_hpfs_disk_sec u.hpfs_i.i_disk_sec
  1741. + #define i_hpfs_dpos u.hpfs_i.i_dpos
  1742. + #define i_hpfs_dsubdno u.hpfs_i.i_dsubdno
  1743. + #define i_hpfs_conv u.hpfs_i.i_conv
  1744. + #endif
  1745. diff -rc2N linux-dist/include/linux/hpfs_fs_sb.h linux/include/linux/hpfs_fs_sb.h
  1746. *** linux-dist/include/linux/hpfs_fs_sb.h
  1747. --- linux/include/linux/hpfs_fs_sb.h    Mon Oct 18 19:49:24 1993
  1748. ***************
  1749. *** 0 ****
  1750. --- 1,33 ----
  1751. + #ifndef _HPFS_FS_SB
  1752. + #define _HPFS_FS_SB
  1753. + struct hpfs_sb_info
  1754. + {
  1755. +   ino_t sb_root;        /* inode number of root dir */
  1756. +   unsigned sb_fs_size;        /* file system size, sectors */
  1757. +   unsigned sb_bitmaps;        /* sector number of bitmap list */
  1758. +   unsigned sb_dirband_size;    /* directory band size, dnodes */
  1759. +   unsigned sb_dmap;        /* sector number of dnode bit map */
  1760. +   unsigned sb_n_free;        /* free blocks for statfs, or -1 */
  1761. +   unsigned sb_n_free_dnodes;    /* free dnodes for statfs, or -1 */
  1762. +   uid_t sb_uid;            /* uid from mount options */
  1763. +   gid_t sb_gid;            /* gid from mount options */
  1764. +   umode_t sb_mode;        /* mode from mount options */
  1765. +   unsigned sb_lowercase : 1;    /* downcase filenames hackery */
  1766. +   unsigned sb_conv : 2;        /* crlf->newline hackery */
  1767. + };
  1768. + #define s_hpfs_root u.hpfs_sb.sb_root
  1769. + #define s_hpfs_fs_size u.hpfs_sb.sb_fs_size
  1770. + #define s_hpfs_bitmaps u.hpfs_sb.sb_bitmaps
  1771. + #define s_hpfs_dirband_size u.hpfs_sb.sb_dirband_size
  1772. + #define s_hpfs_dmap u.hpfs_sb.sb_dmap
  1773. + #define s_hpfs_uid u.hpfs_sb.sb_uid
  1774. + #define s_hpfs_gid u.hpfs_sb.sb_gid
  1775. + #define s_hpfs_mode u.hpfs_sb.sb_mode
  1776. + #define s_hpfs_n_free u.hpfs_sb.sb_n_free
  1777. + #define s_hpfs_n_free_dnodes u.hpfs_sb.sb_n_free_dnodes
  1778. + #define s_hpfs_lowercase u.hpfs_sb.sb_lowercase
  1779. + #define s_hpfs_conv u.hpfs_sb.sb_conv
  1780. + #endif
  1781.