home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / fs / hpfs / hpfs_fs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-05  |  39.3 KB  |  1,724 lines

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