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

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