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