1 /*
2 * linux/fs/hpfs/hpfs_fs.c
3 * read-only HPFS
4 * version 1.0
5 *
6 * Chris Smith 1993
7 *
8 * Sources & references:
9 * Duncan, _Design ... of HPFS_, MSJ 4(5) (C) 1989 Microsoft Corp
10 * linux/fs/minix Copyright (C) 1991, 1992, 1993 Linus Torvalds
11 * linux/fs/msdos Written 1992, 1993 by Werner Almesberger
12 * linux/fs/isofs Copyright (C) 1991 Eric Youngdale
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 * HPFS is a mixture of 512-byte blocks and 2048-byte blocks. The 2k blocks
40 * are used for directories and bitmaps. For bmap to work, we must run the
41 * file system with 512-byte blocks. The 2k blocks are assembled in buffers
42 * obtained from kmalloc.
43 *
44 * For a file's i-number we use the sector number of its fnode, coded.
45 * (Directory ino's are even, file ino's are odd, and ino >> 1 is the
46 * sector address of the fnode. This is a hack to allow lookup() to
47 * tell read_inode() whether it is necessary to read the fnode.)
48 *
49 * The map_xxx routines all read something into a buffer and return a
50 * pointer somewhere in the buffer. The caller must do the brelse.
51 * The other routines are balanced.
52 *
53 * For details on the data structures see hpfs.h and the Duncan paper.
54 *
55 * Overview
56 *
57 * [ The names of these data structures, except fnode, are not Microsoft's
58 * or IBM's. I don't know what names they use. The semantics described
59 * here are those of this implementation, and any coincidence between it
60 * and real HPFS is to be hoped for but not guaranteed by me, and
61 * certainly not guaranteed by MS or IBM. Who know nothing about this. ]
62 *
63 * [ Also, the following will make little sense if you haven't read the
64 * Duncan paper, which is excellent. ]
65 *
66 * HPFS is a tree. There are 3 kinds of nodes. A directory is a tree
67 * of dnodes, and a file's allocation info is a tree of sector runs
68 * stored in fnodes and anodes.
69 *
70 * The top pointer is in the super block, it points to the fnode of the
71 * root directory.
72 *
73 * The root directory -- all directories -- gives file names, dates &c,
74 * and fnode addresses. If the directory fits in one dnode, that's it,
75 * otherwise the top dnode points to other dnodes, forming a tree. A
76 * dnode tree (one directory) might look like
77 *
78 * ((a b c) d (e f g) h (i j) k l (m n o p))
79 *
80 * The subtrees appear between the files. Each dir entry contains, along
81 * with the name and fnode, a dnode pointer to the subtree that precedes it
82 * (if there is one; a flag tells that). The first entry in every directory
83 * is ^A^A, the "." entry for the directory itself. The last entry in every
84 * dnode is \377, a fake entry whose only valid fields are the bit marking
85 * it last and the down pointer to the subtree preceding it, if any.
86 *
87 * The "value" field of directory entries is an fnode address. The fnode
88 * tells where the sectors of the file are. The fnode for a subdirectory
89 * contains one pointer, to the root dnode of the subdirectory. The fnode
90 * for a data file contains, in effect, a tiny anode. (Most of the space
91 * in fnodes is for extended attributes.)
92 *
93 * anodes and the anode part of fnodes are trees of extents. An extent
94 * is a (length, disk address) pair, labeled with the file address being
95 * mapped. E.g.,
96 *
97 * (0: 3@1000 3: 1@2000 4: 2@10)
98 *
99 * means the file:disk sector map (0:1000 1:1001 2:1002 3:2000 4:10 5:11).
100 *
101 * There is space for 8 file:len@disk triples in an fnode, or for 40 in an
102 * anode. If this is insufficient, subtrees are used, as in
103 *
104 * (6: (0: 3@1000 3: 1@2000 4: 2@10) 12: (6: 3@8000 9: 1@9000 10: 2@20))
105 *
106 * The label on a subtree is the first address *after* that tree. The
107 * subtrees are always anodes. The label:subtree pairs require only
108 * two words each, so non-leaf subtrees have a different format; there
109 * is room for 12 label:subtree pairs in an fnode, or 60 in an anode.
110 *
111 * Within a directory, each dnode contains a pointer up to its parent
112 * dnode. The root dnode points up to the directory's fnode.
113 *
114 * Each fnode contains a pointer to the directory that contains it
115 * (to the fnode of the directory). So this pointer in a directory
116 * fnode is "..".
117 *
118 * On the disk, dnodes are all together in the center of the partition,
119 * and HPFS even manages to put all the dnodes for a single directory
120 * together, generally. fnodes are out with the data. anodes are seldom
121 * seen -- in fact noncontiguous files are seldom seen. I think this is
122 * partly the open() call that lets programs specify the length of an
123 * output file when they know it, and partly because HPFS.IFS really is
124 * very good at resisting fragmentation.
125 */
126
127 /* notation */
128
129 #define little_ushort(x) (*(unsigned short *) &(x))
130 typedef void nonconst;
131
132 /* super block ops */
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, /* read_inode */
142 NULL, /* notify_change */
143 NULL, /* write_inode */
144 NULL, /* put_inode */
145 hpfs_put_super, /* put_super */
146 NULL, /* write_super */
147 hpfs_statfs, /* statfs */
148 hpfs_remount_fs, /* remount_fs */
149 };
150
151 /* file ops */
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, /* lseek - default */
159 hpfs_file_read, /* read */
160 NULL, /* write */
161 NULL, /* readdir - bad */
162 NULL, /* select - default */
163 NULL, /* ioctl - default */
164 generic_mmap, /* mmap */
165 NULL, /* no special open is needed */
166 NULL, /* release */
167 file_fsync, /* fsync */
168 };
169
170 static const struct inode_operations hpfs_file_iops =
171 {
172 (nonconst *) & hpfs_file_ops, /* default file operations */
173 NULL, /* create */
174 NULL, /* lookup */
175 NULL, /* link */
176 NULL, /* unlink */
177 NULL, /* symlink */
178 NULL, /* mkdir */
179 NULL, /* rmdir */
180 NULL, /* mknod */
181 NULL, /* rename */
182 NULL, /* readlink */
183 NULL, /* follow_link */
184 (int (*)(struct inode *, int))
185 &hpfs_bmap, /* bmap */
186 NULL, /* truncate */
187 NULL, /* permission */
188 };
189
190 /* directory ops */
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, /* lseek - default */
201 hpfs_dir_read, /* read */
202 NULL, /* write - bad */
203 hpfs_readdir, /* readdir */
204 NULL, /* select - default */
205 NULL, /* ioctl - default */
206 NULL, /* mmap */
207 NULL, /* no special open code */
208 NULL, /* no special release code */
209 file_fsync, /* fsync */
210 };
211
212 static const struct inode_operations hpfs_dir_iops =
213 {
214 (nonconst *) & hpfs_dir_ops, /* default directory file ops */
215 NULL, /* create */
216 hpfs_lookup, /* lookup */
217 NULL, /* link */
218 NULL, /* unlink */
219 NULL, /* symlink */
220 NULL, /* mkdir */
221 NULL, /* rmdir */
222 NULL, /* mknod */
223 NULL, /* rename */
224 NULL, /* readlink */
225 NULL, /* follow_link */
226 NULL, /* bmap */
227 NULL, /* truncate */
228 NULL, /* permission */
229 };
230
231 /* Four 512-byte buffers and the 2k block obtained by concatenating them */
232
233 struct quad_buffer_head {
234 struct buffer_head *bh[4];
235 void *data;
236 };
237
238 /* forwards */
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(kdev_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(kdev_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(kdev_t dev, ino_t ino);
264 static struct fnode *map_fnode(kdev_t dev, ino_t ino,
265 struct buffer_head **bhp);
266 static struct anode *map_anode(kdev_t dev, unsigned secno,
267 struct buffer_head **bhp);
268 static struct dnode *map_dnode(kdev_t dev, unsigned secno,
269 struct quad_buffer_head *qbh);
270 static void *map_sector(kdev_t dev, unsigned secno, struct buffer_head **bhp);
271 static void *map_4sectors(kdev_t dev, unsigned secno,
272 struct quad_buffer_head *qbh);
273 static void brelse4(struct quad_buffer_head *qbh);
274
275 /*
276 * make inode number for a file
277 */
278
279 static inline ino_t file_ino(fnode_secno secno)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
280 {
281 return secno << 1 | 1;
282 }
283
284 /*
285 * make inode number for a directory
286 */
287
288 static inline ino_t dir_ino(fnode_secno secno)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
289 {
290 return secno << 1;
291 }
292
293 /*
294 * get fnode address from an inode number
295 */
296
297 static inline fnode_secno ino_secno(ino_t ino)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
298 {
299 return ino >> 1;
300 }
301
302 /*
303 * test for directory's inode number
304 */
305
306 static inline int ino_is_dir(ino_t ino)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
307 {
308 return (ino & 1) == 0;
309 }
310
311 /*
312 * conv= options
313 */
314
315 #define CONV_BINARY 0 /* no conversion */
316 #define CONV_TEXT 1 /* crlf->newline */
317 #define CONV_AUTO 2 /* decide based on file contents */
318
319 /*
320 * local time (HPFS) to GMT (Unix)
321 */
322
323 static inline time_t local_to_gmt(time_t t)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
324 {
325 extern struct timezone sys_tz;
326 return t + sys_tz.tz_minuteswest * 60;
327 }
328
329 /* super block ops */
330
331 /*
332 * mount. This gets one thing, the root directory inode. It does a
333 * bunch of guessed-at consistency checks.
334 */
335
336 struct super_block *hpfs_read_super(struct super_block *s,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 kdev_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 * Get the mount options
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 * Fill in the super block struct
369 */
370
371 lock_super(s);
372 dev = s->s_dev;
373 set_blocksize(dev, 512);
374
375 /*
376 * fetch sectors 0, 16, 17
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 * Check that this fs looks enough like a known one that we can find
393 * and read the root directory.
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 * Check for inconsistencies -- possibly wrong guesses here, possibly
408 * filesystem problems.
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 * Above errors mean we could get wrong answers if we proceed,
420 * so don't
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 * set fs read only
447 */
448
449 s->s_flags |= MS_RDONLY;
450
451 /*
452 * fill in standard stuff
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 * fill in hpfs stuff
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 * done with the low blocks
479 */
480
481 brelse(bh2);
482 brelse(bh1);
483 brelse(bh0);
484
485 /*
486 * all set. try it out.
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 * find the root directory's . pointer & finish filling in the inode
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,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
543 {
544 unsigned char *p = addr;
545 return p[0] == 0 && memcmp(p, p + 1, len - 1) == 0;
546 }
547
548 /*
549 * A tiny parser for option strings, stolen from dosfs.
550 */
551
552 static int parse_opts(char *opts, uid_t *uid, gid_t *gid, umode_t *umask,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 * read_inode. This is called with exclusive access to a new inode that
617 * has only (i_dev,i_ino) set. It is responsible for filling in the rest.
618 * We leave the dates blank, to be filled in from the dir entry.
619 *
620 * NOTE that there must be no sleeping from the return in this routine
621 * until lookup() finishes filling in the inode, otherwise the partly
622 * completed inode would be visible during the sleep.
623 *
624 * It is done in this strange and sinful way because the alternative
625 * is to read the fnode, find the dir pointer in it, read that fnode
626 * to get the dnode pointer, search through that whole directory for
627 * the ino we're reading, and get the dates. It works that way, but
628 * ls sounds like fsck.
629 */
630
631 static void hpfs_read_inode(struct inode *inode)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
632 {
633 struct super_block *s = inode->i_sb;
634
635 /* be ready to bail out */
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 * canned stuff
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 * figure out whether we are looking at a directory or a file
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 * these fields must be filled in from the dir entry, which we don't
675 * have but lookup does. It will fill them in before letting the
676 * inode out of its grasp.
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 * fill in the rest
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; /* 2048 here confuses ls & du & ... */
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 * unmount.
725 */
726
727 static void hpfs_put_super(struct super_block *s)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
728 {
729 lock_super(s);
730 s->s_dev = 0;
731 unlock_super(s);
732 MOD_DEC_USE_COUNT;
733 }
734
735 /*
736 * statfs. For free inode counts we report the count of dnodes in the
737 * directory band -- not exactly right but pretty analogous.
738 */
739
740 static void hpfs_statfs(struct super_block *s, struct statfs *buf, int bufsiz)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
741 {
742 struct statfs tmp;
743
744 /*
745 * count the bits in the bitmaps, unless we already have
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 * fill in the user statfs struct
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 * remount. Don't let read only be turned off.
769 */
770
771 static int hpfs_remount_fs(struct super_block *s, int *flags, char *data)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
772 {
773 if (!(*flags & MS_RDONLY))
774 return -EINVAL;
775 return 0;
776 }
777
778 /*
779 * count the dnodes in a directory, and the subdirs.
780 */
781
782 static void count_dnodes(struct inode *inode, dnode_secno dno,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 * count the bits in the free space bit maps
813 */
814
815 static unsigned count_bitmap(struct super_block *s)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
816 {
817 unsigned n, count, n_bands;
818 secno *bitmaps;
819 struct quad_buffer_head qbh;
820
821 /*
822 * there is one bit map for each 16384 sectors
823 */
824 n_bands = (s->s_hpfs_fs_size + 0x3fff) >> 14;
825
826 /*
827 * their locations are given in an array pointed to by the super
828 * block
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 * map each one and count the free sectors
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 * Read in one bit map, count the bits, return the count.
851 */
852
853 static unsigned count_one_bitmap(kdev_t dev, secno secno)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 /* file ops */
873
874 /*
875 * read. Read the bytes, put them in buf, return the count.
876 */
877
878 static int hpfs_file_read(struct inode *inode, struct file *filp,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 * truncate count at EOF
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 * get file sector number, offset in sector, length to end of
899 * sector
900 */
901 q = filp->f_pos >> 9;
902 r = filp->f_pos & 511;
903 n = 512 - r;
904
905 /*
906 * get length to copy to user buffer
907 */
908 if (n > count)
909 n = count;
910
911 /*
912 * read the sector, copy to user
913 */
914 block = map_sector(inode->i_dev, hpfs_bmap(inode, q), &bh);
915 if (!block)
916 return -EIO;
917
918 /*
919 * but first decide if it has \r\n, if the mount option said
920 * to do that
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 * regular copy, output length is same as input
928 * length
929 */
930 memcpy_tofs(buf, block + r, n);
931 n0 = n;
932 }
933 else {
934 /*
935 * squeeze out \r, output length varies
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 * advance input n bytes, output n0 bytes
946 */
947 filp->f_pos += n;
948 buf += n0;
949 count -= n0;
950 }
951
952 return buf - start;
953 }
954
955 /*
956 * This routine implements conv=auto. Return CONV_BINARY or CONV_TEXT.
957 */
958
959 static unsigned choose_conv(unsigned char *p, unsigned len)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 * This routine implements conv=text. :s/crlf/nl/
988 */
989
990 static unsigned convcpy_tofs(unsigned char *out, unsigned char *in,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 * Return the disk sector number containing a file sector.
1007 */
1008
1009 static secno hpfs_bmap(struct inode *inode, unsigned file_secno)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1010 {
1011 unsigned n, disk_secno;
1012 struct fnode *fnode;
1013 struct buffer_head *bh;
1014
1015 /*
1016 * There is one sector run cached in the inode. See if the sector is
1017 * in it.
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 * No, read the fnode and go find the sector.
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 * Search allocation tree *b for the given file sector number and return
1041 * the disk sector number. Buffer *bhp has the tree in it, and can be
1042 * reused for subtrees when access to *b is no longer needed.
1043 * *bhp is busy on entry and exit.
1044 */
1045
1046 static secno bplus_lookup(struct inode *inode, struct bplus_header *b,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1047 secno file_secno, struct buffer_head **bhp)
1048 {
1049 int i;
1050
1051 /*
1052 * A leaf-level tree gives a list of sector runs. Find the one
1053 * containing the file sector we want, cache the map info in the
1054 * inode for later, and return the corresponding disk sector.
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 * A non-leaf tree gives a list of subtrees. Find the one containing
1072 * the file sector we want, read it in, and recurse to search it.
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 * If we get here there was a hole in the file. As far as I know we
1093 * never do get here, but falling off the end would be indelicate. So
1094 * return a pointer to a handy all-zero sector. This is not a
1095 * reasonable way to handle files with holes if they really do
1096 * happen.
1097 */
1098
1099 printk("HPFS: bplus_lookup: sector not found\n");
1100 return 15;
1101 }
1102
1103 /* directory ops */
1104
1105 /*
1106 * lookup. Search the specified directory for the specified name, set
1107 * *result to the corresponding inode.
1108 *
1109 * lookup uses the inode number to tell read_inode whether it is reading
1110 * the inode of a directory or a file -- file ino's are odd, directory
1111 * ino's are even. read_inode avoids i/o for file inodes; everything
1112 * needed is up here in the directory. (And file fnodes are out in
1113 * the boondocks.)
1114 */
1115
1116 static int hpfs_lookup(struct inode *dir, const char *name, int len,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 /* In case of madness */
1125
1126 *result = 0;
1127 if (dir == 0)
1128 return -ENOENT;
1129 if (!S_ISDIR(dir->i_mode))
1130 goto bail;
1131
1132 /*
1133 * Read in the directory entry. "." is there under the name ^A^A .
1134 * Always read the dir even for . and .. in case we need the dates.
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 * This is not really a bailout, just means file not found.
1148 */
1149
1150 if (!de)
1151 goto bail;
1152
1153 /*
1154 * Get inode number, what we're after.
1155 */
1156
1157 if (de->directory)
1158 ino = dir_ino(de->fnode);
1159 else
1160 ino = file_ino(de->fnode);
1161
1162 /*
1163 * Go find or make an inode.
1164 */
1165
1166 if (!(inode = iget(dir->i_sb, ino)))
1167 goto bail1;
1168
1169 /*
1170 * Fill in the info from the directory if this is a newly created
1171 * inode.
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 * i_blocks should count the fnode and any anodes.
1184 * We count 1 for the fnode and don't bother about
1185 * anodes -- the disk heads are on the directory band
1186 * and we want them to stay there.
1187 */
1188 inode->i_blocks = 1 + ((inode->i_size + 511) >> 9);
1189 }
1190 }
1191
1192 brelse4(&qbh);
1193
1194 /*
1195 * Made it.
1196 */
1197
1198 *result = inode;
1199 iput(dir);
1200 return 0;
1201
1202 /*
1203 * Didn't.
1204 */
1205 bail1:
1206 brelse4(&qbh);
1207 bail:
1208 iput(dir);
1209 return -ENOENT;
1210 }
1211
1212 /*
1213 * Compare two counted strings ignoring case.
1214 * HPFS directory order sorts letters as if they're upper case.
1215 */
1216
1217 static inline int memcasecmp(const unsigned char *s1, const unsigned char *s2,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 * Search a directory for the given name, return a pointer to its dir entry
1235 * and a pointer to the buffer containing it.
1236 */
1237
1238 static struct hpfs_dirent *map_dirent(struct inode *inode, dnode_secno dno,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 * read the dnode at the root of our subtree
1249 */
1250 dnode = map_dnode(inode->i_dev, dno, qbh);
1251 if (!dnode)
1252 return 0;
1253
1254 /*
1255 * get pointers to start and end+1 of dir entries
1256 */
1257 de = dnode_first_de(dnode);
1258 de_end = dnode_end_de(dnode);
1259
1260 /*
1261 * look through the entries for the name we're after
1262 */
1263 for ( ; de < de_end; de = de_next_de(de)) {
1264
1265 /*
1266 * compare names
1267 */
1268 l = len < de->namelen ? len : de->namelen;
1269 t = memcasecmp(name, de->name, l);
1270
1271 /*
1272 * initial substring matches, compare lengths
1273 */
1274 if (t == 0) {
1275 t = len - de->namelen;
1276 /* bingo */
1277 if (t == 0)
1278 return de;
1279 }
1280
1281 /*
1282 * wanted name .lt. dir name => not present.
1283 */
1284 if (t < 0) {
1285 /*
1286 * if there is a subtree, search it.
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 * de->last is set on the last name in the dnode (it's always
1300 * a "\377" pseudo entry). de->length == 0 means we're about
1301 * to infinite loop. This test does nothing in a well-formed
1302 * dnode.
1303 */
1304 if (de->last || de->length == 0)
1305 break;
1306 }
1307
1308 /*
1309 * name not found.
1310 */
1311 brelse4(qbh);
1312 return 0;
1313 }
1314
1315 /*
1316 * readdir. Return exactly 1 dirent. (I tried and tried, but currently
1317 * the interface with libc just does not permit more than 1. If it gets
1318 * fixed, throw this out and just walk the tree and write records into
1319 * the user buffer.)
1320 *
1321 * [ we now can handle multiple dirents, although the current libc doesn't
1322 * use that. The way hpfs does this is pretty strange, as we need to do
1323 * the name translation etc before calling "filldir()". This is untested,
1324 * as I don't have any hpfs partitions to test against. Linus ]
1325 *
1326 * We keep track of our position in the dnode tree with a sort of
1327 * dewey-decimal record of subtree locations. Like so:
1328 *
1329 * (1 (1.1 1.2 1.3) 2 3 (3.1 (3.1.1 3.1.2) 3.2 3.3 (3.3.1)) 4)
1330 *
1331 * Subtrees appear after their file, out of lexical order,
1332 * which would be before their file. It's easier.
1333 *
1334 * A directory can't hold more than 56 files, so 6 bits are used for
1335 * position numbers. If the tree is so deep that the position encoding
1336 * doesn't fit, I'm sure something absolutely fascinating happens.
1337 *
1338 * The actual sequence of f_pos values is
1339 * 0 => . -1 => .. 1 1.1 ... 8.9 9 => files -2 => eof
1340 *
1341 * The directory inode caches one position-to-dnode correspondence so
1342 * we won't have to repeatedly scan the top levels of the tree.
1343 */
1344
1345 /*
1346 * Translate the given name: Blam it to lowercase if the mount option said to.
1347 */
1348
1349 static void translate_hpfs_name(const unsigned char * from, int len, char * to, int lowercase)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 /* fall through */
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 /* fall through */
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 * Map the dir entry at subtree coordinates given by *posp, and
1427 * increment *posp to point to the following dir entry.
1428 */
1429
1430 static struct hpfs_dirent *map_pos_dirent(struct inode *inode, loff_t *posp,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1431 struct quad_buffer_head *qbh)
1432 {
1433 unsigned pos, q, r;
1434 dnode_secno dno;
1435 struct hpfs_dirent *de;
1436
1437 /*
1438 * Get the position code and split off the rightmost index r
1439 */
1440
1441 pos = *posp;
1442 q = pos >> 6;
1443 r = pos & 077;
1444
1445 /*
1446 * Get the sector address of the dnode
1447 * pointed to by the leading part q
1448 */
1449
1450 dno = dir_subdno(inode, q);
1451 if (!dno)
1452 return 0;
1453
1454 /*
1455 * Get the entry at index r in dnode q
1456 */
1457
1458 de = map_nth_dirent(inode->i_dev, dno, r, qbh);
1459
1460 /*
1461 * If none, we're out of files in this dnode. Ascend.
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 * If a subtree is here, descend.
1473 */
1474
1475 if (de->down)
1476 *posp = pos << 6 | 1;
1477 else
1478 *posp = pos + 1;
1479
1480 /*
1481 * Don't return the ^A^A and \377 entries.
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 * Return the address of the dnode with subtree coordinates given by pos.
1494 */
1495
1496 static dnode_secno dir_subdno(struct inode *inode, unsigned pos)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1497 {
1498 struct hpfs_dirent *de;
1499 struct quad_buffer_head qbh;
1500
1501 /*
1502 * 0 is the root dnode
1503 */
1504
1505 if (pos == 0)
1506 return inode->i_hpfs_dno;
1507
1508 /*
1509 * we have one pos->dnode translation cached in the inode
1510 */
1511
1512 else if (pos == inode->i_hpfs_dpos)
1513 return inode->i_hpfs_dsubdno;
1514
1515 /*
1516 * otherwise go look
1517 */
1518
1519 else {
1520 unsigned q = pos >> 6;
1521 unsigned r = pos & 077;
1522 dnode_secno dno;
1523
1524 /*
1525 * dnode at position q
1526 */
1527 dno = dir_subdno(inode, q);
1528 if (dno == 0)
1529 return 0;
1530
1531 /*
1532 * entry at index r
1533 */
1534 de = map_nth_dirent(inode->i_dev, dno, r, &qbh);
1535 if (!de || !de->down)
1536 return 0;
1537
1538 /*
1539 * get the dnode down pointer
1540 */
1541 dno = de_down_pointer(de);
1542 brelse4(&qbh);
1543
1544 /*
1545 * cache it for next time
1546 */
1547 inode->i_hpfs_dpos = pos;
1548 inode->i_hpfs_dsubdno = dno;
1549 return dno;
1550 }
1551 }
1552
1553 /*
1554 * Return the dir entry at index n in dnode dno, or 0 if there isn't one
1555 */
1556
1557 static struct hpfs_dirent *map_nth_dirent(kdev_t dev, dnode_secno dno,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1580 char *buf, int count)
1581 {
1582 return -EISDIR;
1583 }
1584
1585 /* Return the dnode pointer in a directory fnode */
1586
1587 static dnode_secno fnode_dno(kdev_t dev, ino_t ino)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 /* Map an fnode into a buffer and return pointers to it and to the buffer. */
1603
1604 static struct fnode *map_fnode(kdev_t dev, ino_t ino, struct buffer_head **bhp)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 /* Map an anode into a buffer and return pointers to it and to the buffer. */
1624
1625 static struct anode *map_anode(kdev_t dev, unsigned secno,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 /* Map a dnode into a buffer and return pointers to it and to the buffer. */
1646
1647 static struct dnode *map_dnode(kdev_t dev, unsigned secno,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 /* Map a sector into a buffer and return pointers to it and to the buffer. */
1668
1669 static void *map_sector(kdev_t dev, unsigned secno, struct buffer_head **bhp)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
1682
1683 static void *map_4sectors(kdev_t dev, unsigned secno,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 /* Deallocate a 4-buffer block */
1734
1735 static void brelse4(struct quad_buffer_head *qbh)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1753 {
1754 register_filesystem(&hpfs_fs_type);
1755 return 0;
1756 }
1757
1758 void cleanup_module(void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1759 {
1760 unregister_filesystem(&hpfs_fs_type);
1761 }
1762
1763 #endif
1764