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
  38. init_module
  39. cleanup_module

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

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