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