This source file includes following definitions.
- file_ino
- dir_ino
- ino_secno
- ino_is_dir
- local_to_gmt
- hpfs_read_super
- check_warn
- zerop
- parse_opts
- hpfs_read_inode
- hpfs_put_super
- hpfs_statfs
- hpfs_remount_fs
- count_dnodes
- count_bitmap
- count_one_bitmap
- hpfs_file_read
- choose_conv
- convcpy_tofs
- hpfs_bmap
- bplus_lookup
- hpfs_lookup
- memcasecmp
- map_dirent
- translate_hpfs_name
- hpfs_readdir
- map_pos_dirent
- dir_subdno
- map_nth_dirent
- hpfs_dir_read
- fnode_dno
- map_fnode
- map_anode
- map_dnode
- map_sector
- map_4sectors
- brelse4
- init_module
- cleanup_module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #include <linux/module.h>
16
17 #include <linux/fs.h>
18 #include <linux/hpfs_fs.h>
19 #include <linux/errno.h>
20 #include <linux/malloc.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/locks.h>
24 #include <linux/stat.h>
25 #include <linux/string.h>
26 #include <asm/bitops.h>
27 #include <asm/segment.h>
28
29 #include "hpfs.h"
30 #include "hpfs_caps.h"
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 #define little_ushort(x) (*(unsigned short *) &(x))
124 typedef void nonconst;
125
126
127
128 static void hpfs_read_inode(struct inode *);
129 static void hpfs_put_super(struct super_block *);
130 static void hpfs_statfs(struct super_block *, struct statfs *, int);
131 static int hpfs_remount_fs(struct super_block *, int *, char *);
132
133 static const struct super_operations hpfs_sops =
134 {
135 hpfs_read_inode,
136 NULL,
137 NULL,
138 NULL,
139 hpfs_put_super,
140 NULL,
141 hpfs_statfs,
142 hpfs_remount_fs,
143 };
144
145
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,
153 hpfs_file_read,
154 NULL,
155 NULL,
156 NULL,
157 NULL,
158 generic_mmap,
159 NULL,
160 NULL,
161 file_fsync,
162 };
163
164 static const struct inode_operations hpfs_file_iops =
165 {
166 (nonconst *) & hpfs_file_ops,
167 NULL,
168 NULL,
169 NULL,
170 NULL,
171 NULL,
172 NULL,
173 NULL,
174 NULL,
175 NULL,
176 NULL,
177 NULL,
178 (int (*)(struct inode *, int))
179 &hpfs_bmap,
180 NULL,
181 NULL,
182 };
183
184
185
186 static int hpfs_dir_read(struct inode *inode, struct file *filp,
187 char *buf, int count);
188 static int hpfs_readdir(struct inode *inode, struct file *filp,
189 void *dirent, filldir_t filldir);
190 static int hpfs_lookup(struct inode *, const char *, int, struct inode **);
191
192 static const struct file_operations hpfs_dir_ops =
193 {
194 NULL,
195 hpfs_dir_read,
196 NULL,
197 hpfs_readdir,
198 NULL,
199 NULL,
200 NULL,
201 NULL,
202 NULL,
203 file_fsync,
204 };
205
206 static const struct inode_operations hpfs_dir_iops =
207 {
208 (nonconst *) & hpfs_dir_ops,
209 NULL,
210 hpfs_lookup,
211 NULL,
212 NULL,
213 NULL,
214 NULL,
215 NULL,
216 NULL,
217 NULL,
218 NULL,
219 NULL,
220 NULL,
221 NULL,
222 NULL,
223 };
224
225
226
227 struct quad_buffer_head {
228 struct buffer_head *bh[4];
229 void *data;
230 };
231
232
233
234 static int parse_opts(char *opts, uid_t *uid, gid_t *gid, umode_t *umask,
235 int *lowercase, int *conv);
236 static int check_warn(int not_ok,
237 const char *p1, const char *p2, const char *p3);
238 static int zerop(void *addr, unsigned len);
239 static void count_dnodes(struct inode *inode, dnode_secno dno,
240 unsigned *n_dnodes, unsigned *n_subdirs);
241 static unsigned count_bitmap(struct super_block *s);
242 static unsigned count_one_bitmap(kdev_t dev, secno secno);
243 static secno bplus_lookup(struct inode *inode, struct bplus_header *b,
244 secno file_secno, struct buffer_head **bhp);
245 static struct hpfs_dirent *map_dirent(struct inode *inode, dnode_secno dno,
246 const unsigned char *name, unsigned len,
247 struct quad_buffer_head *qbh);
248 static struct hpfs_dirent *map_pos_dirent(struct inode *inode, loff_t *posp,
249 struct quad_buffer_head *qbh);
250 static dnode_secno dir_subdno(struct inode *inode, unsigned pos);
251 static struct hpfs_dirent *map_nth_dirent(kdev_t dev, dnode_secno dno,
252 int n,
253 struct quad_buffer_head *qbh);
254 static unsigned choose_conv(unsigned char *p, unsigned len);
255 static unsigned convcpy_tofs(unsigned char *out, unsigned char *in,
256 unsigned len);
257 static dnode_secno fnode_dno(kdev_t dev, ino_t ino);
258 static struct fnode *map_fnode(kdev_t dev, ino_t ino,
259 struct buffer_head **bhp);
260 static struct anode *map_anode(kdev_t dev, unsigned secno,
261 struct buffer_head **bhp);
262 static struct dnode *map_dnode(kdev_t dev, unsigned secno,
263 struct quad_buffer_head *qbh);
264 static void *map_sector(kdev_t dev, unsigned secno, struct buffer_head **bhp);
265 static void *map_4sectors(kdev_t dev, unsigned secno,
266 struct quad_buffer_head *qbh);
267 static void brelse4(struct quad_buffer_head *qbh);
268
269
270
271
272
273 static inline ino_t file_ino(fnode_secno secno)
274 {
275 return secno << 1 | 1;
276 }
277
278
279
280
281
282 static inline ino_t dir_ino(fnode_secno secno)
283 {
284 return secno << 1;
285 }
286
287
288
289
290
291 static inline fnode_secno ino_secno(ino_t ino)
292 {
293 return ino >> 1;
294 }
295
296
297
298
299
300 static inline int ino_is_dir(ino_t ino)
301 {
302 return (ino & 1) == 0;
303 }
304
305
306
307
308
309 #define CONV_BINARY 0
310 #define CONV_TEXT 1
311 #define CONV_AUTO 2
312
313
314
315
316
317 static inline time_t local_to_gmt(time_t t)
318 {
319 extern struct timezone sys_tz;
320 return t + sys_tz.tz_minuteswest * 60;
321 }
322
323
324
325
326
327
328
329
330 struct super_block *hpfs_read_super(struct super_block *s,
331 void *options, int silent)
332 {
333 struct hpfs_boot_block *bootblock;
334 struct hpfs_super_block *superblock;
335 struct hpfs_spare_block *spareblock;
336 struct hpfs_dirent *de;
337 struct buffer_head *bh0, *bh1, *bh2;
338 struct quad_buffer_head qbh;
339 dnode_secno root_dno;
340 kdev_t dev;
341 uid_t uid;
342 gid_t gid;
343 umode_t umask;
344 int lowercase;
345 int conv;
346 int dubious;
347
348 MOD_INC_USE_COUNT;
349
350
351
352
353
354 if (!parse_opts(options, &uid, &gid, &umask, &lowercase, &conv)) {
355 printk("HPFS: syntax error in mount options. Not mounted.\n");
356 s->s_dev = 0;
357 MOD_DEC_USE_COUNT;
358 return 0;
359 }
360
361
362
363
364
365 lock_super(s);
366 dev = s->s_dev;
367 set_blocksize(dev, 512);
368
369
370
371
372
373 bootblock = map_sector(dev, 0, &bh0);
374 if (!bootblock)
375 goto bail;
376
377 superblock = map_sector(dev, 16, &bh1);
378 if (!superblock)
379 goto bail0;
380
381 spareblock = map_sector(dev, 17, &bh2);
382 if (!spareblock)
383 goto bail1;
384
385
386
387
388
389
390 if (bootblock->magic != 0xaa55
391 || superblock->magic != SB_MAGIC
392 || spareblock->magic != SP_MAGIC
393 || bootblock->sig_28h != 0x28
394 || memcmp(&bootblock->sig_hpfs, "HPFS ", 8)
395 || little_ushort(bootblock->bytes_per_sector) != 512) {
396 printk("HPFS: hpfs_read_super: Not HPFS\n");
397 goto bail2;
398 }
399
400
401
402
403
404
405 dubious = 0;
406
407 dubious |= check_warn(spareblock->dirty != 0,
408 "`Improperly stopped'", "flag is set", "run CHKDSK");
409 dubious |= check_warn(spareblock->n_spares_used != 0,
410 "Spare blocks", "may be in use", "run CHKDSK");
411
412
413
414
415
416
417 if (dubious)
418 goto bail2;
419
420 dubious |= check_warn((spareblock->n_dnode_spares !=
421 spareblock->n_dnode_spares_free),
422 "Spare dnodes", "may be in use", "run CHKDSK");
423 dubious |= check_warn(superblock->zero1 != 0,
424 "#1", "unknown word nonzero", "investigate");
425 dubious |= check_warn(superblock->zero3 != 0,
426 "#3", "unknown word nonzero", "investigate");
427 dubious |= check_warn(superblock->zero4 != 0,
428 "#4", "unknown word nonzero", "investigate");
429 dubious |= check_warn(!zerop(superblock->zero5,
430 sizeof superblock->zero5),
431 "#5", "unknown word nonzero", "investigate");
432 dubious |= check_warn(!zerop(superblock->zero6,
433 sizeof superblock->zero6),
434 "#6", "unknown word nonzero", "investigate");
435
436 if (dubious)
437 printk("HPFS: Proceeding, but operation may be unreliable\n");
438
439
440
441
442
443 s->s_flags |= MS_RDONLY;
444
445
446
447
448
449 s->s_magic = HPFS_SUPER_MAGIC;
450 s->s_blocksize = 512;
451 s->s_blocksize_bits = 9;
452 s->s_op = (struct super_operations *) &hpfs_sops;
453
454
455
456
457
458 s->s_hpfs_root = dir_ino(superblock->root);
459 s->s_hpfs_fs_size = superblock->n_sectors;
460 s->s_hpfs_dirband_size = superblock->n_dir_band / 4;
461 s->s_hpfs_dmap = superblock->dir_band_bitmap;
462 s->s_hpfs_bitmaps = superblock->bitmaps;
463 s->s_hpfs_uid = uid;
464 s->s_hpfs_gid = gid;
465 s->s_hpfs_mode = 0777 & ~umask;
466 s->s_hpfs_n_free = -1;
467 s->s_hpfs_n_free_dnodes = -1;
468 s->s_hpfs_lowercase = lowercase;
469 s->s_hpfs_conv = conv;
470
471
472
473
474
475 brelse(bh2);
476 brelse(bh1);
477 brelse(bh0);
478
479
480
481
482
483 s->s_mounted = iget(s, s->s_hpfs_root);
484 unlock_super(s);
485
486 if (!s->s_mounted) {
487 printk("HPFS: hpfs_read_super: inode get failed\n");
488 s->s_dev = 0;
489 MOD_DEC_USE_COUNT;
490 return 0;
491 }
492
493
494
495
496
497 root_dno = fnode_dno(dev, s->s_hpfs_root);
498 if (root_dno)
499 de = map_dirent(s->s_mounted, root_dno, "\001\001", 2, &qbh);
500 if (!root_dno || !de) {
501 printk("HPFS: "
502 "hpfs_read_super: root dir isn't in the root dir\n");
503 s->s_dev = 0;
504 MOD_DEC_USE_COUNT;
505 return 0;
506 }
507
508 s->s_mounted->i_atime = local_to_gmt(de->read_date);
509 s->s_mounted->i_mtime = local_to_gmt(de->write_date);
510 s->s_mounted->i_ctime = local_to_gmt(de->creation_date);
511
512 brelse4(&qbh);
513 return s;
514
515 bail2:
516 brelse(bh2);
517 bail1:
518 brelse(bh1);
519 bail0:
520 brelse(bh0);
521 bail:
522 s->s_dev = 0;
523 unlock_super(s);
524 MOD_DEC_USE_COUNT;
525 return 0;
526 }
527
528 static int check_warn(int not_ok,
529 const char *p1, const char *p2, const char *p3)
530 {
531 if (not_ok)
532 printk("HPFS: %s %s. Please %s\n", p1, p2, p3);
533 return not_ok;
534 }
535
536 static int zerop(void *addr, unsigned len)
537 {
538 unsigned char *p = addr;
539 return p[0] == 0 && memcmp(p, p + 1, len - 1) == 0;
540 }
541
542
543
544
545
546 static int parse_opts(char *opts, uid_t *uid, gid_t *gid, umode_t *umask,
547 int *lowercase, int *conv)
548 {
549 char *p, *rhs;
550
551 *uid = current->uid;
552 *gid = current->gid;
553 *umask = current->fs->umask;
554 *lowercase = 1;
555 *conv = CONV_BINARY;
556
557 if (!opts)
558 return 1;
559
560 for (p = strtok(opts, ","); p != 0; p = strtok(0, ",")) {
561 if ((rhs = strchr(p, '=')) != 0)
562 *rhs++ = '\0';
563 if (!strcmp(p, "uid")) {
564 if (!rhs || !*rhs)
565 return 0;
566 *uid = simple_strtoul(rhs, &rhs, 0);
567 if (*rhs)
568 return 0;
569 }
570 else if (!strcmp(p, "gid")) {
571 if (!rhs || !*rhs)
572 return 0;
573 *gid = simple_strtoul(rhs, &rhs, 0);
574 if (*rhs)
575 return 0;
576 }
577 else if (!strcmp(p, "umask")) {
578 if (!rhs || !*rhs)
579 return 0;
580 *umask = simple_strtoul(rhs, &rhs, 8);
581 if (*rhs)
582 return 0;
583 }
584 else if (!strcmp(p, "case")) {
585 if (!strcmp(rhs, "lower"))
586 *lowercase = 1;
587 else if (!strcmp(rhs, "asis"))
588 *lowercase = 0;
589 else
590 return 0;
591 }
592 else if (!strcmp(p, "conv")) {
593 if (!strcmp(rhs, "binary"))
594 *conv = CONV_BINARY;
595 else if (!strcmp(rhs, "text"))
596 *conv = CONV_TEXT;
597 else if (!strcmp(rhs, "auto"))
598 *conv = CONV_AUTO;
599 else
600 return 0;
601 }
602 else
603 return 0;
604 }
605
606 return 1;
607 }
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625 static void hpfs_read_inode(struct inode *inode)
626 {
627 struct super_block *s = inode->i_sb;
628
629
630
631 inode->i_op = 0;
632 inode->i_mode = 0;
633
634 if (inode->i_ino == 0
635 || ino_secno(inode->i_ino) >= inode->i_sb->s_hpfs_fs_size) {
636 printk("HPFS: read_inode: bad ino\n");
637 return;
638 }
639
640
641
642
643
644 inode->i_uid = s->s_hpfs_uid;
645 inode->i_gid = s->s_hpfs_gid;
646 inode->i_mode = s->s_hpfs_mode;
647 inode->i_hpfs_conv = s->s_hpfs_conv;
648
649 inode->i_hpfs_dno = 0;
650 inode->i_hpfs_n_secs = 0;
651 inode->i_hpfs_file_sec = 0;
652 inode->i_hpfs_disk_sec = 0;
653 inode->i_hpfs_dpos = 0;
654 inode->i_hpfs_dsubdno = 0;
655
656
657
658
659
660 if (ino_is_dir(inode->i_ino))
661 inode->i_mode |= S_IFDIR;
662 else {
663 inode->i_mode |= S_IFREG;
664 inode->i_mode &= ~0111;
665 }
666
667
668
669
670
671
672
673 inode->i_atime = 0;
674 inode->i_mtime = 0;
675 inode->i_ctime = 0;
676 inode->i_size = 0;
677
678
679
680
681
682 if (S_ISREG(inode->i_mode)) {
683
684 inode->i_op = (struct inode_operations *) &hpfs_file_iops;
685 inode->i_nlink = 1;
686 inode->i_blksize = 512;
687
688 }
689 else {
690 unsigned n_dnodes, n_subdirs;
691 struct buffer_head *bh0;
692 struct fnode *fnode = map_fnode(inode->i_dev,
693 inode->i_ino, &bh0);
694
695 if (!fnode) {
696 printk("HPFS: read_inode: no fnode\n");
697 inode->i_mode = 0;
698 return;
699 }
700
701 inode->i_hpfs_parent_dir = dir_ino(fnode->up);
702 inode->i_hpfs_dno = fnode->u.external[0].disk_secno;
703
704 brelse(bh0);
705
706 n_dnodes = n_subdirs = 0;
707 count_dnodes(inode, inode->i_hpfs_dno, &n_dnodes, &n_subdirs);
708
709 inode->i_op = (struct inode_operations *) &hpfs_dir_iops;
710 inode->i_blksize = 512;
711 inode->i_blocks = 4 * n_dnodes;
712 inode->i_size = 512 * inode->i_blocks;
713 inode->i_nlink = 2 + n_subdirs;
714 }
715 }
716
717
718
719
720
721 static void hpfs_put_super(struct super_block *s)
722 {
723 lock_super(s);
724 s->s_dev = 0;
725 unlock_super(s);
726 MOD_DEC_USE_COUNT;
727 }
728
729
730
731
732
733
734 static void hpfs_statfs(struct super_block *s, struct statfs *buf, int bufsiz)
735 {
736 struct statfs tmp;
737
738
739
740
741 if (s->s_hpfs_n_free == -1) {
742 s->s_hpfs_n_free = count_bitmap(s);
743 s->s_hpfs_n_free_dnodes =
744 count_one_bitmap(s->s_dev, s->s_hpfs_dmap);
745 }
746
747
748
749
750 tmp.f_type = s->s_magic;
751 tmp.f_bsize = 512;
752 tmp.f_blocks = s->s_hpfs_fs_size;
753 tmp.f_bfree = s->s_hpfs_n_free;
754 tmp.f_bavail = s->s_hpfs_n_free;
755 tmp.f_files = s->s_hpfs_dirband_size;
756 tmp.f_ffree = s->s_hpfs_n_free_dnodes;
757 tmp.f_namelen = 254;
758 memcpy_tofs(buf, &tmp, bufsiz);
759 }
760
761
762
763
764
765 static int hpfs_remount_fs(struct super_block *s, int *flags, char *data)
766 {
767 if (!(*flags & MS_RDONLY))
768 return -EINVAL;
769 return 0;
770 }
771
772
773
774
775
776 static void count_dnodes(struct inode *inode, dnode_secno dno,
777 unsigned *n_dnodes, unsigned *n_subdirs)
778 {
779 struct quad_buffer_head qbh;
780 struct dnode *dnode;
781 struct hpfs_dirent *de;
782 struct hpfs_dirent *de_end;
783
784 dnode = map_dnode(inode->i_dev, dno, &qbh);
785 if (!dnode)
786 return;
787 de = dnode_first_de(dnode);
788 de_end = dnode_end_de(dnode);
789
790 (*n_dnodes)++;
791
792 for (; de < de_end; de = de_next_de(de)) {
793 if (de->down)
794 count_dnodes(inode, de_down_pointer(de),
795 n_dnodes, n_subdirs);
796 if (de->directory && !de->first)
797 (*n_subdirs)++;
798 if (de->last || de->length == 0)
799 break;
800 }
801
802 brelse4(&qbh);
803 }
804
805
806
807
808
809 static unsigned count_bitmap(struct super_block *s)
810 {
811 unsigned n, count, n_bands;
812 secno *bitmaps;
813 struct quad_buffer_head qbh;
814
815
816
817
818 n_bands = (s->s_hpfs_fs_size + 0x3fff) >> 14;
819
820
821
822
823
824 bitmaps = map_4sectors(s->s_dev, s->s_hpfs_bitmaps, &qbh);
825 if (!bitmaps)
826 return 0;
827
828 count = 0;
829
830
831
832
833 for (n = 0; n < n_bands; n++)
834 if (bitmaps[n] == 0)
835 printk("HPFS: bit map pointer missing\n");
836 else
837 count += count_one_bitmap(s->s_dev, bitmaps[n]);
838
839 brelse4(&qbh);
840 return count;
841 }
842
843
844
845
846
847 static unsigned count_one_bitmap(kdev_t dev, secno secno)
848 {
849 struct quad_buffer_head qbh;
850 char *bits;
851 unsigned i, count;
852
853 bits = map_4sectors(dev, secno, &qbh);
854 if (!bits)
855 return 0;
856
857 count = 0;
858
859 for (i = 0; i < 8 * 2048; i++)
860 count += (test_bit(i, bits) != 0);
861 brelse4(&qbh);
862
863 return count;
864 }
865
866
867
868
869
870
871
872 static int hpfs_file_read(struct inode *inode, struct file *filp,
873 char *buf, int count)
874 {
875 unsigned q, r, n, n0;
876 struct buffer_head *bh;
877 char *block;
878 char *start;
879
880 if (inode == 0 || !S_ISREG(inode->i_mode))
881 return -EINVAL;
882
883
884
885
886 if (count > inode->i_size - (off_t) filp->f_pos)
887 count = inode->i_size - filp->f_pos;
888
889 start = buf;
890 while (count > 0) {
891
892
893
894
895 q = filp->f_pos >> 9;
896 r = filp->f_pos & 511;
897 n = 512 - r;
898
899
900
901
902 if (n > count)
903 n = count;
904
905
906
907
908 block = map_sector(inode->i_dev, hpfs_bmap(inode, q), &bh);
909 if (!block)
910 return -EIO;
911
912
913
914
915
916 if (inode->i_hpfs_conv == CONV_AUTO)
917 inode->i_hpfs_conv = choose_conv(block + r, n);
918
919 if (inode->i_hpfs_conv == CONV_BINARY) {
920
921
922
923
924 memcpy_tofs(buf, block + r, n);
925 n0 = n;
926 }
927 else {
928
929
930
931 n0 = convcpy_tofs(buf, block + r, n);
932 if (count > inode->i_size - (off_t) filp->f_pos - n + n0)
933 count = inode->i_size - filp->f_pos - n + n0;
934 }
935
936 brelse(bh);
937
938
939
940
941 filp->f_pos += n;
942 buf += n0;
943 count -= n0;
944 }
945
946 return buf - start;
947 }
948
949
950
951
952
953 static unsigned choose_conv(unsigned char *p, unsigned len)
954 {
955 unsigned tvote, bvote;
956 unsigned c;
957
958 tvote = bvote = 0;
959
960 while (len--) {
961 c = *p++;
962 if (c < ' ')
963 if (c == '\r' && len && *p == '\n')
964 tvote += 10;
965 else if (c == '\t' || c == '\n');
966 else
967 bvote += 5;
968 else if (c < '\177')
969 tvote++;
970 else
971 bvote += 5;
972 }
973
974 if (tvote > bvote)
975 return CONV_TEXT;
976 else
977 return CONV_BINARY;
978 }
979
980
981
982
983
984 static unsigned convcpy_tofs(unsigned char *out, unsigned char *in,
985 unsigned len)
986 {
987 unsigned char *start = out;
988
989 while (len--) {
990 unsigned c = *in++;
991 if (c == '\r' && (len == 0 || *in == '\n'));
992 else
993 put_user(c, out++);
994 }
995
996 return out - start;
997 }
998
999
1000
1001
1002
1003 static secno hpfs_bmap(struct inode *inode, unsigned file_secno)
1004 {
1005 unsigned n, disk_secno;
1006 struct fnode *fnode;
1007 struct buffer_head *bh;
1008
1009
1010
1011
1012
1013
1014 n = file_secno - inode->i_hpfs_file_sec;
1015 if (n < inode->i_hpfs_n_secs)
1016 return inode->i_hpfs_disk_sec + n;
1017
1018
1019
1020
1021
1022 else {
1023 fnode = map_fnode(inode->i_dev, inode->i_ino, &bh);
1024 if (!fnode)
1025 return 0;
1026 disk_secno = bplus_lookup(inode, &fnode->btree,
1027 file_secno, &bh);
1028 brelse(bh);
1029 return disk_secno;
1030 }
1031 }
1032
1033
1034
1035
1036
1037
1038
1039
1040 static secno bplus_lookup(struct inode *inode, struct bplus_header *b,
1041 secno file_secno, struct buffer_head **bhp)
1042 {
1043 int i;
1044
1045
1046
1047
1048
1049
1050
1051 if (!b->internal) {
1052 struct bplus_leaf_node *n = b->u.external;
1053 for (i = 0; i < b->n_used_nodes; i++) {
1054 unsigned t = file_secno - n[i].file_secno;
1055 if (t < n[i].length) {
1056 inode->i_hpfs_file_sec = n[i].file_secno;
1057 inode->i_hpfs_disk_sec = n[i].disk_secno;
1058 inode->i_hpfs_n_secs = n[i].length;
1059 return n[i].disk_secno + t;
1060 }
1061 }
1062 }
1063
1064
1065
1066
1067
1068
1069 else {
1070 struct bplus_internal_node *n = b->u.internal;
1071 for (i = 0; i < b->n_used_nodes; i++) {
1072 if (file_secno < n[i].file_secno) {
1073 struct anode *anode;
1074 anode_secno ano = n[i].down;
1075 brelse(*bhp);
1076 anode = map_anode(inode->i_dev, ano, bhp);
1077 if (!anode)
1078 break;
1079 return bplus_lookup(inode, &anode->btree,
1080 file_secno, bhp);
1081 }
1082 }
1083 }
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093 printk("HPFS: bplus_lookup: sector not found\n");
1094 return 15;
1095 }
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110 static int hpfs_lookup(struct inode *dir, const char *name, int len,
1111 struct inode **result)
1112 {
1113 struct quad_buffer_head qbh;
1114 struct hpfs_dirent *de;
1115 struct inode *inode;
1116 ino_t ino;
1117
1118
1119
1120 *result = 0;
1121 if (dir == 0)
1122 return -ENOENT;
1123 if (!S_ISDIR(dir->i_mode))
1124 goto bail;
1125
1126
1127
1128
1129
1130
1131 if (name[0] == '.' && len == 1)
1132 de = map_dirent(dir, dir->i_hpfs_dno, "\001\001", 2, &qbh);
1133 else if (name[0] == '.' && name[1] == '.' && len == 2)
1134 de = map_dirent(dir,
1135 fnode_dno(dir->i_dev, dir->i_hpfs_parent_dir),
1136 "\001\001", 2, &qbh);
1137 else
1138 de = map_dirent(dir, dir->i_hpfs_dno, name, len, &qbh);
1139
1140
1141
1142
1143
1144 if (!de)
1145 goto bail;
1146
1147
1148
1149
1150
1151 if (de->directory)
1152 ino = dir_ino(de->fnode);
1153 else
1154 ino = file_ino(de->fnode);
1155
1156
1157
1158
1159
1160 if (!(inode = iget(dir->i_sb, ino)))
1161 goto bail1;
1162
1163
1164
1165
1166
1167
1168 if (!inode->i_atime) {
1169 inode->i_atime = local_to_gmt(de->read_date);
1170 inode->i_mtime = local_to_gmt(de->write_date);
1171 inode->i_ctime = local_to_gmt(de->creation_date);
1172 if (de->read_only)
1173 inode->i_mode &= ~0222;
1174 if (!de->directory) {
1175 inode->i_size = de->file_size;
1176
1177
1178
1179
1180
1181
1182 inode->i_blocks = 1 + ((inode->i_size + 511) >> 9);
1183 }
1184 }
1185
1186 brelse4(&qbh);
1187
1188
1189
1190
1191
1192 *result = inode;
1193 iput(dir);
1194 return 0;
1195
1196
1197
1198
1199 bail1:
1200 brelse4(&qbh);
1201 bail:
1202 iput(dir);
1203 return -ENOENT;
1204 }
1205
1206
1207
1208
1209
1210
1211 static inline int memcasecmp(const unsigned char *s1, const unsigned char *s2,
1212 unsigned n)
1213 {
1214 int t;
1215
1216 if (n != 0)
1217 do {
1218 unsigned c1 = linux_char_to_upper_linux (*s1++);
1219 unsigned c2 = hpfs_char_to_upper_linux (*s2++);
1220 if ((t = c1 - c2) != 0)
1221 return t;
1222 } while (--n != 0);
1223
1224 return 0;
1225 }
1226
1227
1228
1229
1230
1231
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)
1235 {
1236 struct dnode *dnode;
1237 struct hpfs_dirent *de;
1238 struct hpfs_dirent *de_end;
1239 int t, l;
1240
1241
1242
1243
1244 dnode = map_dnode(inode->i_dev, dno, qbh);
1245 if (!dnode)
1246 return 0;
1247
1248
1249
1250
1251 de = dnode_first_de(dnode);
1252 de_end = dnode_end_de(dnode);
1253
1254
1255
1256
1257 for ( ; de < de_end; de = de_next_de(de)) {
1258
1259
1260
1261
1262 l = len < de->namelen ? len : de->namelen;
1263 t = memcasecmp(name, de->name, l);
1264
1265
1266
1267
1268 if (t == 0) {
1269 t = len - de->namelen;
1270
1271 if (t == 0)
1272 return de;
1273 }
1274
1275
1276
1277
1278 if (t < 0) {
1279
1280
1281
1282 if (de->down) {
1283 dnode_secno sub_dno = de_down_pointer(de);
1284 brelse4(qbh);
1285 return map_dirent(inode, sub_dno,
1286 name, len, qbh);
1287 }
1288 else
1289 break;
1290 }
1291
1292
1293
1294
1295
1296
1297
1298 if (de->last || de->length == 0)
1299 break;
1300 }
1301
1302
1303
1304
1305 brelse4(qbh);
1306 return 0;
1307 }
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343 static void translate_hpfs_name(const unsigned char * from, int len, char * to, int lowercase)
1344 {
1345 while (len > 0) {
1346 unsigned t = *from;
1347 len--;
1348 if (lowercase)
1349 t = hpfs_char_to_lower_linux (t);
1350 else
1351 t = hpfs_char_to_linux (t);
1352 *to = t;
1353 from++;
1354 to++;
1355 }
1356 }
1357
1358 static int hpfs_readdir(struct inode *inode, struct file *filp, void * dirent,
1359 filldir_t filldir)
1360 {
1361 struct quad_buffer_head qbh;
1362 struct hpfs_dirent *de;
1363 int namelen, lc;
1364 ino_t ino;
1365 char * tempname;
1366 long old_pos;
1367
1368 if (inode == 0
1369 || inode->i_sb == 0
1370 || !S_ISDIR(inode->i_mode))
1371 return -EBADF;
1372
1373 tempname = (char *) __get_free_page(GFP_KERNEL);
1374 if (!tempname)
1375 return -ENOMEM;
1376
1377 lc = inode->i_sb->s_hpfs_lowercase;
1378 switch ((long) filp->f_pos) {
1379 case -2:
1380 break;
1381
1382 case 0:
1383 if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino) < 0)
1384 break;
1385 filp->f_pos = -1;
1386
1387
1388 case -1:
1389 if (filldir(dirent, "..", 2, filp->f_pos, inode->i_hpfs_parent_dir) < 0)
1390 break;
1391 filp->f_pos = 1;
1392
1393
1394 default:
1395 for (;;) {
1396 old_pos = filp->f_pos;
1397 de = map_pos_dirent(inode, &filp->f_pos, &qbh);
1398 if (!de) {
1399 filp->f_pos = -2;
1400 break;
1401 }
1402 namelen = de->namelen;
1403 translate_hpfs_name(de->name, namelen, tempname, lc);
1404 if (de->directory)
1405 ino = dir_ino(de->fnode);
1406 else
1407 ino = file_ino(de->fnode);
1408 brelse4(&qbh);
1409 if (filldir(dirent, tempname, namelen, old_pos, ino) < 0) {
1410 filp->f_pos = old_pos;
1411 break;
1412 }
1413 }
1414 }
1415 free_page((unsigned long) tempname);
1416 return 0;
1417 }
1418
1419
1420
1421
1422
1423
1424 static struct hpfs_dirent *map_pos_dirent(struct inode *inode, loff_t *posp,
1425 struct quad_buffer_head *qbh)
1426 {
1427 unsigned pos, q, r;
1428 dnode_secno dno;
1429 struct hpfs_dirent *de;
1430
1431
1432
1433
1434
1435 pos = *posp;
1436 q = pos >> 6;
1437 r = pos & 077;
1438
1439
1440
1441
1442
1443
1444 dno = dir_subdno(inode, q);
1445 if (!dno)
1446 return 0;
1447
1448
1449
1450
1451
1452 de = map_nth_dirent(inode->i_dev, dno, r, qbh);
1453
1454
1455
1456
1457
1458 if (!de) {
1459 if (q == 0)
1460 return 0;
1461 *posp = q + 1;
1462 return map_pos_dirent(inode, posp, qbh);
1463 }
1464
1465
1466
1467
1468
1469 if (de->down)
1470 *posp = pos << 6 | 1;
1471 else
1472 *posp = pos + 1;
1473
1474
1475
1476
1477
1478 if (de->first || de->last) {
1479 brelse4(qbh);
1480 return map_pos_dirent(inode, posp, qbh);
1481 }
1482 else
1483 return de;
1484 }
1485
1486
1487
1488
1489
1490 static dnode_secno dir_subdno(struct inode *inode, unsigned pos)
1491 {
1492 struct hpfs_dirent *de;
1493 struct quad_buffer_head qbh;
1494
1495
1496
1497
1498
1499 if (pos == 0)
1500 return inode->i_hpfs_dno;
1501
1502
1503
1504
1505
1506 else if (pos == inode->i_hpfs_dpos)
1507 return inode->i_hpfs_dsubdno;
1508
1509
1510
1511
1512
1513 else {
1514 unsigned q = pos >> 6;
1515 unsigned r = pos & 077;
1516 dnode_secno dno;
1517
1518
1519
1520
1521 dno = dir_subdno(inode, q);
1522 if (dno == 0)
1523 return 0;
1524
1525
1526
1527
1528 de = map_nth_dirent(inode->i_dev, dno, r, &qbh);
1529 if (!de || !de->down)
1530 return 0;
1531
1532
1533
1534
1535 dno = de_down_pointer(de);
1536 brelse4(&qbh);
1537
1538
1539
1540
1541 inode->i_hpfs_dpos = pos;
1542 inode->i_hpfs_dsubdno = dno;
1543 return dno;
1544 }
1545 }
1546
1547
1548
1549
1550
1551 static struct hpfs_dirent *map_nth_dirent(kdev_t dev, dnode_secno dno,
1552 int n,
1553 struct quad_buffer_head *qbh)
1554 {
1555 int i;
1556 struct hpfs_dirent *de, *de_end;
1557 struct dnode *dnode = map_dnode(dev, dno, qbh);
1558
1559 de = dnode_first_de(dnode);
1560 de_end = dnode_end_de(dnode);
1561
1562 for (i = 1; de < de_end; i++, de = de_next_de(de)) {
1563 if (i == n)
1564 return de;
1565 if (de->last || de->length == 0)
1566 break;
1567 }
1568
1569 brelse4(qbh);
1570 return 0;
1571 }
1572
1573 static int hpfs_dir_read(struct inode *inode, struct file *filp,
1574 char *buf, int count)
1575 {
1576 return -EISDIR;
1577 }
1578
1579
1580
1581 static dnode_secno fnode_dno(kdev_t dev, ino_t ino)
1582 {
1583 struct buffer_head *bh;
1584 struct fnode *fnode;
1585 dnode_secno dno;
1586
1587 fnode = map_fnode(dev, ino, &bh);
1588 if (!fnode)
1589 return 0;
1590
1591 dno = fnode->u.external[0].disk_secno;
1592 brelse(bh);
1593 return dno;
1594 }
1595
1596
1597
1598 static struct fnode *map_fnode(kdev_t dev, ino_t ino, struct buffer_head **bhp)
1599 {
1600 struct fnode *fnode;
1601
1602 if (ino == 0) {
1603 printk("HPFS: missing fnode\n");
1604 return 0;
1605 }
1606
1607 fnode = map_sector(dev, ino_secno(ino), bhp);
1608 if (fnode)
1609 if (fnode->magic != FNODE_MAGIC) {
1610 printk("HPFS: map_fnode: bad fnode pointer\n");
1611 brelse(*bhp);
1612 return 0;
1613 }
1614 return fnode;
1615 }
1616
1617
1618
1619 static struct anode *map_anode(kdev_t dev, unsigned secno,
1620 struct buffer_head **bhp)
1621 {
1622 struct anode *anode;
1623
1624 if (secno == 0) {
1625 printk("HPFS: missing anode\n");
1626 return 0;
1627 }
1628
1629 anode = map_sector(dev, secno, bhp);
1630 if (anode)
1631 if (anode->magic != ANODE_MAGIC || anode->self != secno) {
1632 printk("HPFS: map_anode: bad anode pointer\n");
1633 brelse(*bhp);
1634 return 0;
1635 }
1636 return anode;
1637 }
1638
1639
1640
1641 static struct dnode *map_dnode(kdev_t dev, unsigned secno,
1642 struct quad_buffer_head *qbh)
1643 {
1644 struct dnode *dnode;
1645
1646 if (secno == 0) {
1647 printk("HPFS: missing dnode\n");
1648 return 0;
1649 }
1650
1651 dnode = map_4sectors(dev, secno, qbh);
1652 if (dnode)
1653 if (dnode->magic != DNODE_MAGIC || dnode->self != secno) {
1654 printk("HPFS: map_dnode: bad dnode pointer\n");
1655 brelse4(qbh);
1656 return 0;
1657 }
1658 return dnode;
1659 }
1660
1661
1662
1663 static void *map_sector(kdev_t dev, unsigned secno, struct buffer_head **bhp)
1664 {
1665 struct buffer_head *bh;
1666
1667 if ((*bhp = bh = bread(dev, secno, 512)) != 0)
1668 return bh->b_data;
1669 else {
1670 printk("HPFS: map_sector: read error\n");
1671 return 0;
1672 }
1673 }
1674
1675
1676
1677 static void *map_4sectors(kdev_t dev, unsigned secno,
1678 struct quad_buffer_head *qbh)
1679 {
1680 struct buffer_head *bh;
1681 char *data;
1682
1683 if (secno & 3) {
1684 printk("HPFS: map_4sectors: unaligned read\n");
1685 return 0;
1686 }
1687
1688 qbh->data = data = kmalloc(2048, GFP_KERNEL);
1689 if (!data)
1690 goto bail;
1691
1692 qbh->bh[0] = bh = breada(dev, secno, 512, 0, UINT_MAX);
1693 if (!bh)
1694 goto bail0;
1695 memcpy(data, bh->b_data, 512);
1696
1697 qbh->bh[1] = bh = bread(dev, secno + 1, 512);
1698 if (!bh)
1699 goto bail1;
1700 memcpy(data + 512, bh->b_data, 512);
1701
1702 qbh->bh[2] = bh = bread(dev, secno + 2, 512);
1703 if (!bh)
1704 goto bail2;
1705 memcpy(data + 2 * 512, bh->b_data, 512);
1706
1707 qbh->bh[3] = bh = bread(dev, secno + 3, 512);
1708 if (!bh)
1709 goto bail3;
1710 memcpy(data + 3 * 512, bh->b_data, 512);
1711
1712 return data;
1713
1714 bail3:
1715 brelse(qbh->bh[2]);
1716 bail2:
1717 brelse(qbh->bh[1]);
1718 bail1:
1719 brelse(qbh->bh[0]);
1720 bail0:
1721 kfree_s(data, 2048);
1722 bail:
1723 printk("HPFS: map_4sectors: read error\n");
1724 return 0;
1725 }
1726
1727
1728
1729 static void brelse4(struct quad_buffer_head *qbh)
1730 {
1731 brelse(qbh->bh[3]);
1732 brelse(qbh->bh[2]);
1733 brelse(qbh->bh[1]);
1734 brelse(qbh->bh[0]);
1735 kfree_s(qbh->data, 2048);
1736 }
1737
1738 #ifdef MODULE
1739
1740 static struct file_system_type hpfs_fs_type = {
1741 hpfs_read_super, "hpfs", 1, NULL
1742 };
1743
1744 int init_module(void)
1745 {
1746 return register_filesystem(&hpfs_fs_type);
1747 }
1748
1749 void cleanup_module(void)
1750 {
1751 unregister_filesystem(&hpfs_fs_type);
1752 }
1753
1754 #endif
1755