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. hpfs_readdir
  26. write_one_dirent
  27. map_pos_dirent
  28. dir_subdno
  29. map_nth_dirent
  30. hpfs_dir_read
  31. fnode_dno
  32. map_fnode
  33. map_anode
  34. map_dnode
  35. map_sector
  36. map_4sectors
  37. brelse4

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

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