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

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