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