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