root/fs/hpfs/hpfs_fs.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. file_ino
  2. dir_ino
  3. ino_secno
  4. ino_is_dir
  5. local_to_gmt
  6. hpfs_read_super
  7. check_warn
  8. zerop
  9. parse_opts
  10. hpfs_read_inode
  11. hpfs_put_super
  12. hpfs_statfs
  13. hpfs_remount_fs
  14. count_dnodes
  15. count_bitmap
  16. count_one_bitmap
  17. hpfs_file_read
  18. choose_conv
  19. convcpy_tofs
  20. hpfs_bmap
  21. bplus_lookup
  22. hpfs_lookup
  23. memcasecmp
  24. map_dirent
  25. translate_hpfs_name
  26. hpfs_readdir
  27. map_pos_dirent
  28. dir_subdno
  29. map_nth_dirent
  30. hpfs_dir_read
  31. fnode_dno
  32. map_fnode
  33. map_anode
  34. map_dnode
  35. map_sector
  36. map_4sectors
  37. brelse4

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

/* [previous][next][first][last][top][bottom][index][help] */