2 * linux/fs/hpfs/hpfs_fs.c
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
15 #include <linux/module.h>
18 #include <linux/hpfs_fs.h>
19 #include <linux/errno.h>
20 #include <linux/malloc.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/locks.h>
24 #include <linux/stat.h>
25 #include <linux/string.h>
26 #include <linux/init.h>
27 #include <asm/bitops.h>
28 #include <asm/uaccess.h>
31 #include "hpfs_caps.h"
34 * HPFS is a mixture of 512-byte blocks and 2048-byte blocks. The 2k blocks
35 * are used for directories and bitmaps. For bmap to work, we must run the
36 * file system with 512-byte blocks. The 2k blocks are assembled in buffers
37 * obtained from kmalloc.
39 * For a file's i-number we use the sector number of its fnode, coded.
40 * (Directory ino's are even, file ino's are odd, and ino >> 1 is the
41 * sector address of the fnode. This is a hack to allow lookup() to
42 * tell read_inode() whether it is necessary to read the fnode.)
44 * The map_xxx routines all read something into a buffer and return a
45 * pointer somewhere in the buffer. The caller must do the brelse.
46 * The other routines are balanced.
48 * For details on the data structures see hpfs.h and the Duncan paper.
52 * [ The names of these data structures, except fnode, are not Microsoft's
53 * or IBM's. I don't know what names they use. The semantics described
54 * here are those of this implementation, and any coincidence between it
55 * and real HPFS is to be hoped for but not guaranteed by me, and
56 * certainly not guaranteed by MS or IBM. Who know nothing about this. ]
58 * [ Also, the following will make little sense if you haven't read the
59 * Duncan paper, which is excellent. ]
61 * HPFS is a tree. There are 3 kinds of nodes. A directory is a tree
62 * of dnodes, and a file's allocation info is a tree of sector runs
63 * stored in fnodes and anodes.
65 * The top pointer is in the super block, it points to the fnode of the
68 * The root directory -- all directories -- gives file names, dates &c,
69 * and fnode addresses. If the directory fits in one dnode, that's it,
70 * otherwise the top dnode points to other dnodes, forming a tree. A
71 * dnode tree (one directory) might look like
73 * ((a b c) d (e f g) h (i j) k l (m n o p))
75 * The subtrees appear between the files. Each dir entry contains, along
76 * with the name and fnode, a dnode pointer to the subtree that precedes it
77 * (if there is one; a flag tells that). The first entry in every directory
78 * is ^A^A, the "." entry for the directory itself. The last entry in every
79 * dnode is \377, a fake entry whose only valid fields are the bit marking
80 * it last and the down pointer to the subtree preceding it, if any.
82 * The "value" field of directory entries is an fnode address. The fnode
83 * tells where the sectors of the file are. The fnode for a subdirectory
84 * contains one pointer, to the root dnode of the subdirectory. The fnode
85 * for a data file contains, in effect, a tiny anode. (Most of the space
86 * in fnodes is for extended attributes.)
88 * anodes and the anode part of fnodes are trees of extents. An extent
89 * is a (length, disk address) pair, labeled with the file address being
92 * (0: 3@1000 3: 1@2000 4: 2@10)
94 * means the file:disk sector map (0:1000 1:1001 2:1002 3:2000 4:10 5:11).
96 * There is space for 8 file:len@disk triples in an fnode, or for 40 in an
97 * anode. If this is insufficient, subtrees are used, as in
99 * (6: (0: 3@1000 3: 1@2000 4: 2@10) 12: (6: 3@8000 9: 1@9000 10: 2@20))
101 * The label on a subtree is the first address *after* that tree. The
102 * subtrees are always anodes. The label:subtree pairs require only
103 * two words each, so non-leaf subtrees have a different format; there
104 * is room for 12 label:subtree pairs in an fnode, or 60 in an anode.
106 * Within a directory, each dnode contains a pointer up to its parent
107 * dnode. The root dnode points up to the directory's fnode.
109 * Each fnode contains a pointer to the directory that contains it
110 * (to the fnode of the directory). So this pointer in a directory
113 * On the disk, dnodes are all together in the center of the partition,
114 * and HPFS even manages to put all the dnodes for a single directory
115 * together, generally. fnodes are out with the data. anodes are seldom
116 * seen -- in fact noncontiguous files are seldom seen. I think this is
117 * partly the open() call that lets programs specify the length of an
118 * output file when they know it, and partly because HPFS.IFS really is
119 * very good at resisting fragmentation.
124 #define little_ushort(x) (*(unsigned short *) &(x))
125 typedef void nonconst
;
127 /* super block ops */
129 static void hpfs_read_inode(struct inode
*);
130 static void hpfs_put_super(struct super_block
*);
131 static int hpfs_statfs(struct super_block
*, struct statfs
*, int);
132 static int hpfs_remount_fs(struct super_block
*, int *, char *);
134 static const struct super_operations hpfs_sops
=
136 hpfs_read_inode
, /* read_inode */
137 NULL
, /* write_inode */
138 NULL
, /* put_inode */
139 NULL
, /* delete_inode */
140 NULL
, /* notify_change */
141 hpfs_put_super
, /* put_super */
142 NULL
, /* write_super */
143 hpfs_statfs
, /* statfs */
144 hpfs_remount_fs
, /* remount_fs */
149 static ssize_t
hpfs_file_read(struct file
*, char *, size_t, loff_t
*);
150 static secno
hpfs_bmap(struct inode
*, unsigned);
152 static const struct file_operations hpfs_file_ops
=
154 NULL
, /* lseek - default */
155 hpfs_file_read
, /* read */
157 NULL
, /* readdir - bad */
158 NULL
, /* poll - default */
159 NULL
, /* ioctl - default */
160 generic_file_mmap
, /* mmap */
161 NULL
, /* no special open is needed */
164 file_fsync
, /* fsync */
167 static const struct inode_operations hpfs_file_iops
=
169 (nonconst
*) & hpfs_file_ops
, /* default file operations */
180 NULL
, /* follow_link */
181 generic_readpage
, /* readpage */
182 NULL
, /* writepage */
183 (int (*)(struct inode
*, int))
184 &hpfs_bmap
, /* bmap */
186 NULL
, /* permission */
191 static ssize_t
hpfs_dir_read(struct file
*filp
, char *buf
,
192 size_t count
, loff_t
*ppos
);
193 static int hpfs_readdir(struct file
*filp
,
194 void *dirent
, filldir_t filldir
);
195 static struct dentry
*hpfs_lookup(struct inode
*, struct dentry
*);
197 static const struct file_operations hpfs_dir_ops
=
199 NULL
, /* lseek - default */
200 hpfs_dir_read
, /* read */
201 NULL
, /* write - bad */
202 hpfs_readdir
, /* readdir */
203 NULL
, /* poll - default */
204 NULL
, /* ioctl - default */
206 NULL
, /* no special open code */
208 NULL
, /* no special release code */
209 file_fsync
, /* fsync */
212 static const struct inode_operations hpfs_dir_iops
=
214 (nonconst
*) & hpfs_dir_ops
, /* default directory file ops */
216 hpfs_lookup
, /* lookup */
226 NULL
, /* writepage */
229 NULL
, /* permission */
232 /* Four 512-byte buffers and the 2k block obtained by concatenating them */
234 struct quad_buffer_head
{
235 struct buffer_head
*bh
[4];
241 static int parse_opts(char *opts
, uid_t
*uid
, gid_t
*gid
, umode_t
*umask
,
242 int *lowercase
, int *conv
, int *nocheck
);
243 static int check_warn(int not_ok
,
244 const char *p1
, const char *p2
, const char *p3
);
245 static int zerop(void *addr
, unsigned len
);
246 static void count_dnodes(struct inode
*inode
, dnode_secno dno
,
247 unsigned *n_dnodes
, unsigned *n_subdirs
);
248 static unsigned count_bitmap(struct super_block
*s
);
249 static unsigned count_one_bitmap(kdev_t dev
, secno secno
);
250 static secno
bplus_lookup(struct inode
*inode
, struct bplus_header
*b
,
251 secno file_secno
, struct buffer_head
**bhp
);
252 static struct hpfs_dirent
*map_dirent(struct inode
*inode
, dnode_secno dno
,
253 const unsigned char *name
, unsigned len
,
254 struct quad_buffer_head
*qbh
);
255 static struct hpfs_dirent
*map_pos_dirent(struct inode
*inode
, loff_t
*posp
,
256 struct quad_buffer_head
*qbh
);
257 static dnode_secno
dir_subdno(struct inode
*inode
, unsigned pos
);
258 static struct hpfs_dirent
*map_nth_dirent(kdev_t dev
, dnode_secno dno
,
260 struct quad_buffer_head
*qbh
);
261 static unsigned choose_conv(unsigned char *p
, unsigned len
);
262 static unsigned convcpy_tofs(unsigned char *out
, unsigned char *in
,
264 static dnode_secno
fnode_dno(kdev_t dev
, ino_t ino
);
265 static struct fnode
*map_fnode(kdev_t dev
, ino_t ino
,
266 struct buffer_head
**bhp
);
267 static struct anode
*map_anode(kdev_t dev
, unsigned secno
,
268 struct buffer_head
**bhp
);
269 static struct dnode
*map_dnode(kdev_t dev
, unsigned secno
,
270 struct quad_buffer_head
*qbh
);
271 static void *map_sector(kdev_t dev
, unsigned secno
, struct buffer_head
**bhp
);
272 static void *map_4sectors(kdev_t dev
, unsigned secno
,
273 struct quad_buffer_head
*qbh
);
274 static void brelse4(struct quad_buffer_head
*qbh
);
277 * make inode number for a file
280 static inline ino_t
file_ino(fnode_secno secno
)
282 return secno
<< 1 | 1;
286 * make inode number for a directory
289 static inline ino_t
dir_ino(fnode_secno secno
)
295 * get fnode address from an inode number
298 static inline fnode_secno
ino_secno(ino_t ino
)
304 * test for directory's inode number
307 static inline int ino_is_dir(ino_t ino
)
309 return (ino
& 1) == 0;
316 #define CONV_BINARY 0 /* no conversion */
317 #define CONV_TEXT 1 /* crlf->newline */
318 #define CONV_AUTO 2 /* decide based on file contents */
321 * local time (HPFS) to GMT (Unix)
324 static inline time_t local_to_gmt(time_t t
)
326 extern struct timezone sys_tz
;
327 return t
+ sys_tz
.tz_minuteswest
* 60 - (sys_tz
.tz_dsttime
? 3600 : 0);
330 /* super block ops */
333 * mount. This gets one thing, the root directory inode. It does a
334 * bunch of guessed-at consistency checks.
337 struct super_block
*hpfs_read_super(struct super_block
*s
,
338 void *options
, int silent
)
340 struct hpfs_boot_block
*bootblock
;
341 struct hpfs_super_block
*superblock
;
342 struct hpfs_spare_block
*spareblock
;
343 struct hpfs_dirent
*de
= NULL
;
344 struct buffer_head
*bh0
, *bh1
, *bh2
;
345 struct quad_buffer_head qbh
;
346 dnode_secno root_dno
;
359 * Get the mount options
362 if (!parse_opts(options
, &uid
, &gid
, &umask
, &lowercase
, &conv
,
364 printk("HPFS: syntax error in mount options. Not mounted.\n");
371 * Fill in the super block struct
376 set_blocksize(dev
, 512);
379 * fetch sectors 0, 16, 17
382 bootblock
= map_sector(dev
, 0, &bh0
);
386 superblock
= map_sector(dev
, 16, &bh1
);
390 spareblock
= map_sector(dev
, 17, &bh2
);
395 * Check that this fs looks enough like a known one that we can find
396 * and read the root directory.
399 if (bootblock
->magic
!= 0xaa55
400 || superblock
->magic
!= SB_MAGIC
401 || spareblock
->magic
!= SP_MAGIC
402 || bootblock
->sig_28h
!= 0x28
403 || memcmp(&bootblock
->sig_hpfs
, "HPFS ", 8)
404 || little_ushort(bootblock
->bytes_per_sector
) != 512) {
405 printk("HPFS: hpfs_read_super: Not HPFS\n");
410 * Check for inconsistencies -- possibly wrong guesses here, possibly
411 * filesystem problems.
416 dubious
|= check_warn(spareblock
->dirty
!= 0,
417 "`Improperly stopped'", "flag is set", "run CHKDSK");
418 dubious
|= check_warn(spareblock
->n_spares_used
!= 0,
419 "Spare blocks", "may be in use", "run CHKDSK");
422 * Above errors mean we could get wrong answers if we proceed,
426 if (dubious
&& !nocheck
)
429 dubious
|= check_warn((spareblock
->n_dnode_spares
!=
430 spareblock
->n_dnode_spares_free
),
431 "Spare dnodes", "may be in use", "run CHKDSK");
432 dubious
|= check_warn(superblock
->zero1
!= 0,
433 "#1", "unknown word nonzero", "investigate");
434 dubious
|= check_warn(superblock
->zero3
!= 0,
435 "#3", "unknown word nonzero", "investigate");
436 dubious
|= check_warn(superblock
->zero4
!= 0,
437 "#4", "unknown word nonzero", "investigate");
438 dubious
|= check_warn(!zerop(superblock
->zero5
,
439 sizeof superblock
->zero5
),
440 "#5", "unknown word nonzero", "investigate");
441 dubious
|= check_warn(!zerop(superblock
->zero6
,
442 sizeof superblock
->zero6
),
443 "#6", "unknown word nonzero", "investigate");
446 printk("HPFS: Proceeding, but operation may be unreliable\n");
452 s
->s_flags
|= MS_RDONLY
;
455 * fill in standard stuff
458 s
->s_magic
= HPFS_SUPER_MAGIC
;
459 s
->s_blocksize
= 512;
460 s
->s_blocksize_bits
= 9;
461 s
->s_op
= (struct super_operations
*) &hpfs_sops
;
467 s
->s_hpfs_root
= dir_ino(superblock
->root
);
468 s
->s_hpfs_fs_size
= superblock
->n_sectors
;
469 s
->s_hpfs_dirband_size
= superblock
->n_dir_band
/ 4;
470 s
->s_hpfs_dmap
= superblock
->dir_band_bitmap
;
471 s
->s_hpfs_bitmaps
= superblock
->bitmaps
;
474 s
->s_hpfs_mode
= 0777 & ~umask
;
475 s
->s_hpfs_n_free
= -1;
476 s
->s_hpfs_n_free_dnodes
= -1;
477 s
->s_hpfs_lowercase
= lowercase
;
478 s
->s_hpfs_conv
= conv
;
481 * done with the low blocks
489 * all set. try it out.
492 s
->s_root
= d_alloc_root(iget(s
, s
->s_hpfs_root
), NULL
);
496 printk("HPFS: hpfs_read_super: inode get failed\n");
503 * find the root directory's . pointer & finish filling in the inode
506 root_dno
= fnode_dno(dev
, s
->s_hpfs_root
);
508 de
= map_dirent(s
->s_root
->d_inode
, root_dno
,
509 "\001\001", 2, &qbh
);
510 if (!root_dno
|| !de
) {
512 "hpfs_read_super: root dir isn't in the root dir\n");
518 s
->s_root
->d_inode
->i_atime
= local_to_gmt(de
->read_date
);
519 s
->s_root
->d_inode
->i_mtime
= local_to_gmt(de
->write_date
);
520 s
->s_root
->d_inode
->i_ctime
= local_to_gmt(de
->creation_date
);
538 static int check_warn(int not_ok
,
539 const char *p1
, const char *p2
, const char *p3
)
542 printk("HPFS: %s %s. Please %s\n", p1
, p2
, p3
);
546 static int zerop(void *addr
, unsigned len
)
548 unsigned char *p
= addr
;
549 return p
[0] == 0 && memcmp(p
, p
+ 1, len
- 1) == 0;
553 * A tiny parser for option strings, stolen from dosfs.
556 static int parse_opts(char *opts
, uid_t
*uid
, gid_t
*gid
, umode_t
*umask
,
557 int *lowercase
, int *conv
, int *nocheck
)
563 *umask
= current
->fs
->umask
;
571 for (p
= strtok(opts
, ","); p
!= 0; p
= strtok(0, ",")) {
572 if ((rhs
= strchr(p
, '=')) != 0)
574 if (!strcmp(p
, "uid")) {
577 *uid
= simple_strtoul(rhs
, &rhs
, 0);
581 else if (!strcmp(p
, "gid")) {
584 *gid
= simple_strtoul(rhs
, &rhs
, 0);
588 else if (!strcmp(p
, "umask")) {
591 *umask
= simple_strtoul(rhs
, &rhs
, 8);
595 else if (!strcmp(p
, "case")) {
596 if (!strcmp(rhs
, "lower"))
598 else if (!strcmp(rhs
, "asis"))
603 else if (!strcmp(p
, "conv")) {
604 if (!strcmp(rhs
, "binary"))
606 else if (!strcmp(rhs
, "text"))
608 else if (!strcmp(rhs
, "auto"))
613 else if (!strcmp(p
,"nocheck"))
623 * read_inode. This is called with exclusive access to a new inode that
624 * has only (i_dev,i_ino) set. It is responsible for filling in the rest.
625 * We leave the dates blank, to be filled in from the dir entry.
627 * NOTE that there must be no sleeping from the return in this routine
628 * until lookup() finishes filling in the inode, otherwise the partly
629 * completed inode would be visible during the sleep.
631 * It is done in this strange and sinful way because the alternative
632 * is to read the fnode, find the dir pointer in it, read that fnode
633 * to get the dnode pointer, search through that whole directory for
634 * the ino we're reading, and get the dates. It works that way, but
635 * ls sounds like fsck.
638 static void hpfs_read_inode(struct inode
*inode
)
640 struct super_block
*s
= inode
->i_sb
;
642 /* be ready to bail out */
647 if (inode
->i_ino
== 0
648 || ino_secno(inode
->i_ino
) >= inode
->i_sb
->s_hpfs_fs_size
) {
649 printk("HPFS: read_inode: bad ino\n");
657 inode
->i_uid
= s
->s_hpfs_uid
;
658 inode
->i_gid
= s
->s_hpfs_gid
;
659 inode
->i_mode
= s
->s_hpfs_mode
;
660 inode
->i_hpfs_conv
= s
->s_hpfs_conv
;
662 inode
->i_hpfs_dno
= 0;
663 inode
->i_hpfs_n_secs
= 0;
664 inode
->i_hpfs_file_sec
= 0;
665 inode
->i_hpfs_disk_sec
= 0;
666 inode
->i_hpfs_dpos
= 0;
667 inode
->i_hpfs_dsubdno
= 0;
670 * figure out whether we are looking at a directory or a file
673 if (ino_is_dir(inode
->i_ino
))
674 inode
->i_mode
|= S_IFDIR
;
676 inode
->i_mode
|= S_IFREG
;
677 inode
->i_mode
&= ~0111;
681 * these fields must be filled in from the dir entry, which we don't
682 * have but lookup does. It will fill them in before letting the
683 * inode out of its grasp.
695 if (S_ISREG(inode
->i_mode
)) {
697 inode
->i_op
= (struct inode_operations
*) &hpfs_file_iops
;
699 inode
->i_blksize
= 512;
703 unsigned n_dnodes
, n_subdirs
;
704 struct buffer_head
*bh0
;
705 struct fnode
*fnode
= map_fnode(inode
->i_dev
,
709 printk("HPFS: read_inode: no fnode\n");
714 inode
->i_hpfs_parent_dir
= dir_ino(fnode
->up
);
715 inode
->i_hpfs_dno
= fnode
->u
.external
[0].disk_secno
;
719 n_dnodes
= n_subdirs
= 0;
720 count_dnodes(inode
, inode
->i_hpfs_dno
, &n_dnodes
, &n_subdirs
);
722 inode
->i_op
= (struct inode_operations
*) &hpfs_dir_iops
;
723 inode
->i_blksize
= 512; /* 2048 here confuses ls & du & ... */
724 inode
->i_blocks
= 4 * n_dnodes
;
725 inode
->i_size
= 512 * inode
->i_blocks
;
726 inode
->i_nlink
= 2 + n_subdirs
;
734 static void hpfs_put_super(struct super_block
*s
)
740 * statfs. For free inode counts we report the count of dnodes in the
741 * directory band -- not exactly right but pretty analogous.
744 static int hpfs_statfs(struct super_block
*s
, struct statfs
*buf
, int bufsiz
)
749 * count the bits in the bitmaps, unless we already have
751 if (s
->s_hpfs_n_free
== -1) {
752 s
->s_hpfs_n_free
= count_bitmap(s
);
753 s
->s_hpfs_n_free_dnodes
=
754 count_one_bitmap(s
->s_dev
, s
->s_hpfs_dmap
);
758 * fill in the user statfs struct
760 tmp
.f_type
= s
->s_magic
;
762 tmp
.f_blocks
= s
->s_hpfs_fs_size
;
763 tmp
.f_bfree
= s
->s_hpfs_n_free
;
764 tmp
.f_bavail
= s
->s_hpfs_n_free
;
765 tmp
.f_files
= s
->s_hpfs_dirband_size
;
766 tmp
.f_ffree
= s
->s_hpfs_n_free_dnodes
;
769 return copy_to_user(buf
, &tmp
, bufsiz
) ? -EFAULT
: 0;
773 * remount. Don't let read only be turned off.
776 static int hpfs_remount_fs(struct super_block
*s
, int *flags
, char *data
)
778 if (!(*flags
& MS_RDONLY
))
784 * count the dnodes in a directory, and the subdirs.
787 static void count_dnodes(struct inode
*inode
, dnode_secno dno
,
788 unsigned *n_dnodes
, unsigned *n_subdirs
)
790 struct quad_buffer_head qbh
;
792 struct hpfs_dirent
*de
;
793 struct hpfs_dirent
*de_end
;
795 dnode
= map_dnode(inode
->i_dev
, dno
, &qbh
);
798 de
= dnode_first_de(dnode
);
799 de_end
= dnode_end_de(dnode
);
803 for (; de
< de_end
; de
= de_next_de(de
)) {
805 count_dnodes(inode
, de_down_pointer(de
),
806 n_dnodes
, n_subdirs
);
807 if (de
->directory
&& !de
->first
)
809 if (de
->last
|| de
->length
== 0)
817 * count the bits in the free space bit maps
820 static unsigned count_bitmap(struct super_block
*s
)
822 unsigned n
, count
, n_bands
;
824 struct quad_buffer_head qbh
;
827 * there is one bit map for each 16384 sectors
829 n_bands
= (s
->s_hpfs_fs_size
+ 0x3fff) >> 14;
832 * their locations are given in an array pointed to by the super
835 bitmaps
= map_4sectors(s
->s_dev
, s
->s_hpfs_bitmaps
, &qbh
);
842 * map each one and count the free sectors
844 for (n
= 0; n
< n_bands
; n
++)
846 printk("HPFS: bit map pointer missing\n");
848 count
+= count_one_bitmap(s
->s_dev
, bitmaps
[n
]);
855 * Read in one bit map, count the bits, return the count.
858 static unsigned count_one_bitmap(kdev_t dev
, secno secno
)
860 struct quad_buffer_head qbh
;
864 bits
= map_4sectors(dev
, secno
, &qbh
);
870 for (i
= 0; i
< 8 * 2048; i
++)
871 count
+= (test_bit(i
, bits
) != 0);
880 * read. Read the bytes, put them in buf, return the count.
883 static ssize_t
hpfs_file_read(struct file
*filp
, char *buf
,
884 size_t count
, loff_t
*ppos
)
886 struct inode
*inode
= filp
->f_dentry
->d_inode
;
888 struct buffer_head
*bh
;
892 if (inode
== 0 || !S_ISREG(inode
->i_mode
))
896 * truncate count at EOF
898 if (count
> inode
->i_size
- (off_t
) *ppos
)
899 count
= inode
->i_size
- *ppos
;
904 * get file sector number, offset in sector, length to end of
912 * get length to copy to user buffer
918 * read the sector, copy to user
920 block
= map_sector(inode
->i_dev
, hpfs_bmap(inode
, q
), &bh
);
925 * but first decide if it has \r\n, if the mount option said
928 if (inode
->i_hpfs_conv
== CONV_AUTO
)
929 inode
->i_hpfs_conv
= choose_conv(block
+ r
, n
);
931 if (inode
->i_hpfs_conv
== CONV_BINARY
) {
933 * regular copy, output length is same as input
936 copy_to_user(buf
, block
+ r
, n
);
941 * squeeze out \r, output length varies
943 n0
= convcpy_tofs(buf
, block
+ r
, n
);
944 if (count
> inode
->i_size
- (off_t
) *ppos
- n
+ n0
)
945 count
= inode
->i_size
- *ppos
- n
+ n0
;
951 * advance input n bytes, output n0 bytes
962 * This routine implements conv=auto. Return CONV_BINARY or CONV_TEXT.
965 static unsigned choose_conv(unsigned char *p
, unsigned len
)
967 unsigned tvote
, bvote
;
975 if (c
== '\r' && len
&& *p
== '\n')
977 else if (c
== '\t' || c
== '\n');
980 } else if (c
< '\177')
993 * This routine implements conv=text. :s/crlf/nl/
996 static unsigned convcpy_tofs(unsigned char *out
, unsigned char *in
,
999 unsigned char *start
= out
;
1003 if (c
== '\r' && (len
== 0 || *in
== '\n'));
1012 * Return the disk sector number containing a file sector.
1015 static secno
hpfs_bmap(struct inode
*inode
, unsigned file_secno
)
1017 unsigned n
, disk_secno
;
1018 struct fnode
*fnode
;
1019 struct buffer_head
*bh
;
1022 * There is one sector run cached in the inode. See if the sector is
1026 n
= file_secno
- inode
->i_hpfs_file_sec
;
1027 if (n
< inode
->i_hpfs_n_secs
)
1028 return inode
->i_hpfs_disk_sec
+ n
;
1031 * No, read the fnode and go find the sector.
1035 fnode
= map_fnode(inode
->i_dev
, inode
->i_ino
, &bh
);
1038 disk_secno
= bplus_lookup(inode
, &fnode
->btree
,
1046 * Search allocation tree *b for the given file sector number and return
1047 * the disk sector number. Buffer *bhp has the tree in it, and can be
1048 * reused for subtrees when access to *b is no longer needed.
1049 * *bhp is busy on entry and exit.
1052 static secno
bplus_lookup(struct inode
*inode
, struct bplus_header
*b
,
1053 secno file_secno
, struct buffer_head
**bhp
)
1058 * A leaf-level tree gives a list of sector runs. Find the one
1059 * containing the file sector we want, cache the map info in the
1060 * inode for later, and return the corresponding disk sector.
1064 struct bplus_leaf_node
*n
= b
->u
.external
;
1065 for (i
= 0; i
< b
->n_used_nodes
; i
++) {
1066 unsigned t
= file_secno
- n
[i
].file_secno
;
1067 if (t
< n
[i
].length
) {
1068 inode
->i_hpfs_file_sec
= n
[i
].file_secno
;
1069 inode
->i_hpfs_disk_sec
= n
[i
].disk_secno
;
1070 inode
->i_hpfs_n_secs
= n
[i
].length
;
1071 return n
[i
].disk_secno
+ t
;
1077 * A non-leaf tree gives a list of subtrees. Find the one containing
1078 * the file sector we want, read it in, and recurse to search it.
1082 struct bplus_internal_node
*n
= b
->u
.internal
;
1083 for (i
= 0; i
< b
->n_used_nodes
; i
++) {
1084 if (file_secno
< n
[i
].file_secno
) {
1085 struct anode
*anode
;
1086 anode_secno ano
= n
[i
].down
;
1088 anode
= map_anode(inode
->i_dev
, ano
, bhp
);
1091 return bplus_lookup(inode
, &anode
->btree
,
1098 * If we get here there was a hole in the file. As far as I know we
1099 * never do get here, but falling off the end would be indelicate. So
1100 * return a pointer to a handy all-zero sector. This is not a
1101 * reasonable way to handle files with holes if they really do
1105 printk("HPFS: bplus_lookup: sector not found\n");
1112 * lookup. Search the specified directory for the specified name, set
1113 * *result to the corresponding inode.
1115 * lookup uses the inode number to tell read_inode whether it is reading
1116 * the inode of a directory or a file -- file ino's are odd, directory
1117 * ino's are even. read_inode avoids i/o for file inodes; everything
1118 * needed is up here in the directory. (And file fnodes are out in
1122 static struct dentry
*hpfs_lookup(struct inode
*dir
, struct dentry
*dentry
)
1124 const char *name
= dentry
->d_name
.name
;
1125 int len
= dentry
->d_name
.len
;
1126 struct hpfs_dirent
*de
;
1127 struct inode
*inode
;
1130 struct quad_buffer_head qbh
;
1133 * Read in the directory entry. "." is there under the name ^A^A .
1134 * Always read the dir even for . and .. in case we need the dates.
1137 if (name
[0] == '.' && len
== 1)
1138 de
= map_dirent(dir
, dir
->i_hpfs_dno
, "\001\001", 2, &qbh
);
1139 else if (name
[0] == '.' && name
[1] == '.' && len
== 2)
1140 de
= map_dirent(dir
,
1141 fnode_dno(dir
->i_dev
, dir
->i_hpfs_parent_dir
),
1142 "\001\001", 2, &qbh
);
1144 de
= map_dirent(dir
, dir
->i_hpfs_dno
, name
, len
, &qbh
);
1147 * This is not really a bailout, just means file not found.
1151 d_add(dentry
, NULL
);
1157 * Get inode number, what we're after.
1161 ino
= dir_ino(de
->fnode
);
1163 ino
= file_ino(de
->fnode
);
1166 * Go find or make an inode.
1170 if (!(inode
= iget(dir
->i_sb
, ino
)))
1174 * Fill in the info from the directory if this is a newly created
1178 if (!inode
->i_atime
) {
1179 inode
->i_atime
= local_to_gmt(de
->read_date
);
1180 inode
->i_mtime
= local_to_gmt(de
->write_date
);
1181 inode
->i_ctime
= local_to_gmt(de
->creation_date
);
1183 inode
->i_mode
&= ~0222;
1184 if (!de
->directory
) {
1185 inode
->i_size
= de
->file_size
;
1187 * i_blocks should count the fnode and any anodes.
1188 * We count 1 for the fnode and don't bother about
1189 * anodes -- the disk heads are on the directory band
1190 * and we want them to stay there.
1192 inode
->i_blocks
= 1 + ((inode
->i_size
+ 511) >> 9);
1196 d_add(dentry
, inode
);
1203 return ERR_PTR(retval
);
1207 * Compare two counted strings ignoring case.
1208 * HPFS directory order sorts letters as if they're upper case.
1211 static inline int memcasecmp(const unsigned char *s1
, const unsigned char *s2
,
1218 unsigned c1
= linux_char_to_upper_linux (*s1
++);
1219 unsigned c2
= hpfs_char_to_upper_linux (*s2
++);
1220 if ((t
= c1
- c2
) != 0)
1228 * Search a directory for the given name, return a pointer to its dir entry
1229 * and a pointer to the buffer containing it.
1232 static struct hpfs_dirent
*map_dirent(struct inode
*inode
, dnode_secno dno
,
1233 const unsigned char *name
, unsigned len
,
1234 struct quad_buffer_head
*qbh
)
1236 struct dnode
*dnode
;
1237 struct hpfs_dirent
*de
;
1238 struct hpfs_dirent
*de_end
;
1242 * read the dnode at the root of our subtree
1244 dnode
= map_dnode(inode
->i_dev
, dno
, qbh
);
1249 * get pointers to start and end+1 of dir entries
1251 de
= dnode_first_de(dnode
);
1252 de_end
= dnode_end_de(dnode
);
1255 * look through the entries for the name we're after
1257 for ( ; de
< de_end
; de
= de_next_de(de
)) {
1262 l
= len
< de
->namelen
? len
: de
->namelen
;
1263 t
= memcasecmp(name
, de
->name
, l
);
1266 * initial substring matches, compare lengths
1269 t
= len
- de
->namelen
;
1276 * wanted name .lt. dir name => not present.
1280 * if there is a subtree, search it.
1283 dnode_secno sub_dno
= de_down_pointer(de
);
1285 return map_dirent(inode
, sub_dno
,
1293 * de->last is set on the last name in the dnode (it's always
1294 * a "\377" pseudo entry). de->length == 0 means we're about
1295 * to infinite loop. This test does nothing in a well-formed
1298 if (de
->last
|| de
->length
== 0)
1310 * readdir. Return exactly 1 dirent. (I tried and tried, but currently
1311 * the interface with libc just does not permit more than 1. If it gets
1312 * fixed, throw this out and just walk the tree and write records into
1315 * [ we now can handle multiple dirents, although the current libc doesn't
1316 * use that. The way hpfs does this is pretty strange, as we need to do
1317 * the name translation etc before calling "filldir()". This is untested,
1318 * as I don't have any hpfs partitions to test against. Linus ]
1320 * We keep track of our position in the dnode tree with a sort of
1321 * dewey-decimal record of subtree locations. Like so:
1323 * (1 (1.1 1.2 1.3) 2 3 (3.1 (3.1.1 3.1.2) 3.2 3.3 (3.3.1)) 4)
1325 * Subtrees appear after their file, out of lexical order,
1326 * which would be before their file. It's easier.
1328 * A directory can't hold more than 56 files, so 6 bits are used for
1329 * position numbers. If the tree is so deep that the position encoding
1330 * doesn't fit, I'm sure something absolutely fascinating happens.
1332 * The actual sequence of f_pos values is
1333 * 0 => . -1 => .. 1 1.1 ... 8.9 9 => files -2 => eof
1335 * The directory inode caches one position-to-dnode correspondence so
1336 * we won't have to repeatedly scan the top levels of the tree.
1340 * Translate the given name: Blam it to lowercase if the mount option said to.
1343 static void translate_hpfs_name(const unsigned char * from
, int len
, char * to
, int lowercase
)
1349 t
= hpfs_char_to_lower_linux (t
);
1351 t
= hpfs_char_to_linux (t
);
1358 static int hpfs_readdir(struct file
*filp
, void * dirent
,
1361 struct quad_buffer_head qbh
;
1362 struct hpfs_dirent
*de
;
1367 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1369 tempname
= (char *) __get_free_page(GFP_KERNEL
);
1373 lc
= inode
->i_sb
->s_hpfs_lowercase
;
1374 switch ((long) filp
->f_pos
) {
1379 if (filldir(dirent
, ".", 1, filp
->f_pos
, inode
->i_ino
) < 0)
1385 if (filldir(dirent
, "..", 2, filp
->f_pos
, inode
->i_hpfs_parent_dir
) < 0)
1392 old_pos
= filp
->f_pos
;
1393 de
= map_pos_dirent(inode
, &filp
->f_pos
, &qbh
);
1398 namelen
= de
->namelen
;
1399 translate_hpfs_name(de
->name
, namelen
, tempname
, lc
);
1401 ino
= dir_ino(de
->fnode
);
1403 ino
= file_ino(de
->fnode
);
1405 if (filldir(dirent
, tempname
, namelen
, old_pos
, ino
) < 0) {
1406 filp
->f_pos
= old_pos
;
1411 free_page((unsigned long) tempname
);
1416 * Map the dir entry at subtree coordinates given by *posp, and
1417 * increment *posp to point to the following dir entry.
1420 static struct hpfs_dirent
*map_pos_dirent(struct inode
*inode
, loff_t
*posp
,
1421 struct quad_buffer_head
*qbh
)
1425 struct hpfs_dirent
*de
;
1428 * Get the position code and split off the rightmost index r
1436 * Get the sector address of the dnode
1437 * pointed to by the leading part q
1440 dno
= dir_subdno(inode
, q
);
1445 * Get the entry at index r in dnode q
1448 de
= map_nth_dirent(inode
->i_dev
, dno
, r
, qbh
);
1451 * If none, we're out of files in this dnode. Ascend.
1458 return map_pos_dirent(inode
, posp
, qbh
);
1462 * If a subtree is here, descend.
1466 *posp
= pos
<< 6 | 1;
1471 * Don't return the ^A^A and \377 entries.
1474 if (de
->first
|| de
->last
) {
1476 return map_pos_dirent(inode
, posp
, qbh
);
1483 * Return the address of the dnode with subtree coordinates given by pos.
1486 static dnode_secno
dir_subdno(struct inode
*inode
, unsigned pos
)
1488 struct hpfs_dirent
*de
;
1489 struct quad_buffer_head qbh
;
1492 * 0 is the root dnode
1496 return inode
->i_hpfs_dno
;
1499 * we have one pos->dnode translation cached in the inode
1502 else if (pos
== inode
->i_hpfs_dpos
)
1503 return inode
->i_hpfs_dsubdno
;
1510 unsigned q
= pos
>> 6;
1511 unsigned r
= pos
& 077;
1515 * dnode at position q
1517 dno
= dir_subdno(inode
, q
);
1524 de
= map_nth_dirent(inode
->i_dev
, dno
, r
, &qbh
);
1525 if (!de
|| !de
->down
)
1529 * get the dnode down pointer
1531 dno
= de_down_pointer(de
);
1535 * cache it for next time
1537 inode
->i_hpfs_dpos
= pos
;
1538 inode
->i_hpfs_dsubdno
= dno
;
1544 * Return the dir entry at index n in dnode dno, or 0 if there isn't one
1547 static struct hpfs_dirent
*map_nth_dirent(kdev_t dev
, dnode_secno dno
,
1549 struct quad_buffer_head
*qbh
)
1552 struct hpfs_dirent
*de
, *de_end
;
1553 struct dnode
*dnode
= map_dnode(dev
, dno
, qbh
);
1555 de
= dnode_first_de(dnode
);
1556 de_end
= dnode_end_de(dnode
);
1558 for (i
= 1; de
< de_end
; i
++, de
= de_next_de(de
)) {
1561 if (de
->last
|| de
->length
== 0)
1569 static ssize_t
hpfs_dir_read(struct file
*filp
, char *buf
,
1570 size_t count
, loff_t
*ppos
)
1575 /* Return the dnode pointer in a directory fnode */
1577 static dnode_secno
fnode_dno(kdev_t dev
, ino_t ino
)
1579 struct buffer_head
*bh
;
1580 struct fnode
*fnode
;
1583 fnode
= map_fnode(dev
, ino
, &bh
);
1587 dno
= fnode
->u
.external
[0].disk_secno
;
1592 /* Map an fnode into a buffer and return pointers to it and to the buffer. */
1594 static struct fnode
*map_fnode(kdev_t dev
, ino_t ino
, struct buffer_head
**bhp
)
1596 struct fnode
*fnode
;
1599 printk("HPFS: missing fnode\n");
1603 fnode
= map_sector(dev
, ino_secno(ino
), bhp
);
1605 if (fnode
->magic
!= FNODE_MAGIC
) {
1606 printk("HPFS: map_fnode: bad fnode pointer\n");
1613 /* Map an anode into a buffer and return pointers to it and to the buffer. */
1615 static struct anode
*map_anode(kdev_t dev
, unsigned secno
,
1616 struct buffer_head
**bhp
)
1618 struct anode
*anode
;
1621 printk("HPFS: missing anode\n");
1625 anode
= map_sector(dev
, secno
, bhp
);
1627 if (anode
->magic
!= ANODE_MAGIC
|| anode
->self
!= secno
) {
1628 printk("HPFS: map_anode: bad anode pointer\n");
1635 /* Map a dnode into a buffer and return pointers to it and to the buffer. */
1637 static struct dnode
*map_dnode(kdev_t dev
, unsigned secno
,
1638 struct quad_buffer_head
*qbh
)
1640 struct dnode
*dnode
;
1643 printk("HPFS: missing dnode\n");
1647 dnode
= map_4sectors(dev
, secno
, qbh
);
1649 if (dnode
->magic
!= DNODE_MAGIC
|| dnode
->self
!= secno
) {
1650 printk("HPFS: map_dnode: bad dnode pointer\n");
1657 /* Map a sector into a buffer and return pointers to it and to the buffer. */
1659 static void *map_sector(kdev_t dev
, unsigned secno
, struct buffer_head
**bhp
)
1661 struct buffer_head
*bh
;
1663 if ((*bhp
= bh
= bread(dev
, secno
, 512)) != 0)
1666 printk("HPFS: map_sector: read error\n");
1671 /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
1673 static void *map_4sectors(kdev_t dev
, unsigned secno
,
1674 struct quad_buffer_head
*qbh
)
1676 struct buffer_head
*bh
;
1680 printk("HPFS: map_4sectors: unaligned read\n");
1684 qbh
->data
= data
= kmalloc(2048, GFP_KERNEL
);
1688 qbh
->bh
[0] = bh
= bread(dev
, secno
, 512);
1691 memcpy(data
, bh
->b_data
, 512);
1693 qbh
->bh
[1] = bh
= bread(dev
, secno
+ 1, 512);
1696 memcpy(data
+ 512, bh
->b_data
, 512);
1698 qbh
->bh
[2] = bh
= bread(dev
, secno
+ 2, 512);
1701 memcpy(data
+ 2 * 512, bh
->b_data
, 512);
1703 qbh
->bh
[3] = bh
= bread(dev
, secno
+ 3, 512);
1706 memcpy(data
+ 3 * 512, bh
->b_data
, 512);
1717 kfree_s(data
, 2048);
1719 printk("HPFS: map_4sectors: read error\n");
1723 /* Deallocate a 4-buffer block */
1725 static void brelse4(struct quad_buffer_head
*qbh
)
1731 kfree_s(qbh
->data
, 2048);
1734 static struct file_system_type hpfs_fs_type
= {
1741 __initfunc(int init_hpfs_fs(void))
1743 return register_filesystem(&hpfs_fs_type
);
1749 int init_module(void)
1751 return init_hpfs_fs();
1754 void cleanup_module(void)
1756 unregister_filesystem(&hpfs_fs_type
);