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