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 #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 * HPFS is a mixture of 512-byte blocks and 2048-byte blocks. The 2k blocks
31 * are used for directories and bitmaps. For bmap to work, we must run the
32 * file system with 512-byte blocks. The 2k blocks are assembled in buffers
33 * obtained from kmalloc.
34 *
35 * For a file's i-number we use the sector number of its fnode, coded.
36 * (Directory ino's are even, file ino's are odd, and ino >> 1 is the
37 * sector address of the fnode. This is a hack to allow lookup() to
38 * tell read_inode() whether it is necessary to read the fnode.)
39 *
40 * The map_xxx routines all read something into a buffer and return a
41 * pointer somewhere in the buffer. The caller must do the brelse.
42 * The other routines are balanced.
43 *
44 * For details on the data structures see hpfs.h and the Duncan paper.
45 *
46 * Overview
47 *
48 * [ The names of these data structures, except fnode, are not Microsoft's
49 * or IBM's. I don't know what names they use. The semantics described
50 * here are those of this implementation, and any coincidence between it
51 * and real HPFS is to be hoped for but not guaranteed by me, and
52 * certainly not guaranteed by MS or IBM. Who know nothing about this. ]
53 *
54 * [ Also, the following will make little sense if you haven't read the
55 * Duncan paper, which is excellent. ]
56 *
57 * HPFS is a tree. There are 3 kinds of nodes. A directory is a tree
58 * of dnodes, and a file's allocation info is a tree of sector runs
59 * stored in fnodes and anodes.
60 *
61 * The top pointer is in the super block, it points to the fnode of the
62 * root directory.
63 *
64 * The root directory -- all directories -- gives file names, dates &c,
65 * and fnode addresses. If the directory fits in one dnode, that's it,
66 * otherwise the top dnode points to other dnodes, forming a tree. A
67 * dnode tree (one directory) might look like
68 *
69 * ((a b c) d (e f g) h (i j) k l (m n o p))
70 *
71 * The subtrees appear between the files. Each dir entry contains, along
72 * with the name and fnode, a dnode pointer to the subtree that precedes it
73 * (if there is one; a flag tells that). The first entry in every directory
74 * is ^A^A, the "." entry for the directory itself. The last entry in every
75 * dnode is \377, a fake entry whose only valid fields are the bit marking
76 * it last and the down pointer to the subtree preceding it, if any.
77 *
78 * The "value" field of directory entries is an fnode address. The fnode
79 * tells where the sectors of the file are. The fnode for a subdirectory
80 * contains one pointer, to the root dnode of the subdirectory. The fnode
81 * for a data file contains, in effect, a tiny anode. (Most of the space
82 * in fnodes is for extended attributes.)
83 *
84 * anodes and the anode part of fnodes are trees of extents. An extent
85 * is a (length, disk address) pair, labeled with the file address being
86 * mapped. E.g.,
87 *
88 * (0: 3@1000 3: 1@2000 4: 2@10)
89 *
90 * means the file:disk sector map (0:1000 1:1001 2:1002 3:2000 4:10 5:11).
91 *
92 * There is space for 8 file:len@disk triples in an fnode, or for 40 in an
93 * anode. If this is insufficient, subtrees are used, as in
94 *
95 * (6: (0: 3@1000 3: 1@2000 4: 2@10) 12: (6: 3@8000 9: 1@9000 10: 2@20))
96 *
97 * The label on a subtree is the first address *after* that tree. The
98 * subtrees are always anodes. The label:subtree pairs require only
99 * two words each, so non-leaf subtrees have a different format; there
100 * is room for 12 label:subtree pairs in an fnode, or 60 in an anode.
101 *
102 * Within a directory, each dnode contains a pointer up to its parent
103 * dnode. The root dnode points up to the directory's fnode.
104 *
105 * Each fnode contains a pointer to the directory that contains it
106 * (to the fnode of the directory). So this pointer in a directory
107 * fnode is "..".
108 *
109 * On the disk, dnodes are all together in the center of the partition,
110 * and HPFS even manages to put all the dnodes for a single directory
111 * together, generally. fnodes are out with the data. anodes are seldom
112 * seen -- in fact noncontiguous files are seldom seen. I think this is
113 * partly the open() call that lets programs specify the length of an
114 * output file when they know it, and partly because HPFS.IFS really is
115 * very good at resisting fragmentation.
116 */
117
118 /* notation */
119
120 #define little_ushort(x) (*(unsigned short *) &(x))
121 typedef void nonconst;
122
123 /* super block ops */
124
125 static void hpfs_read_inode(struct inode *);
126 static void hpfs_put_super(struct super_block *);
127 static void hpfs_statfs(struct super_block *, struct statfs *);
128 static int hpfs_remount_fs(struct super_block *, int *, char *);
129
130 static const struct super_operations hpfs_sops =
131 {
132 hpfs_read_inode, /* read_inode */
133 NULL, /* notify_change */
134 NULL, /* write_inode */
135 NULL, /* put_inode */
136 hpfs_put_super, /* put_super */
137 NULL, /* write_super */
138 hpfs_statfs, /* statfs */
139 hpfs_remount_fs, /* remount_fs */
140 };
141
142 /* file ops */
143
144 static int hpfs_file_read(struct inode *, struct file *, char *, int);
145 static secno hpfs_bmap(struct inode *, unsigned);
146
147 static const struct file_operations hpfs_file_ops =
148 {
149 NULL, /* lseek - default */
150 hpfs_file_read, /* read */
151 NULL, /* write */
152 NULL, /* readdir - bad */
153 NULL, /* select - default */
154 NULL, /* ioctl - default */
155 generic_mmap, /* mmap */
156 NULL, /* no special open is needed */
157 NULL, /* release */
158 file_fsync, /* fsync */
159 };
160
161 static const struct inode_operations hpfs_file_iops =
162 {
163 (nonconst *) & hpfs_file_ops, /* default file operations */
164 NULL, /* create */
165 NULL, /* lookup */
166 NULL, /* link */
167 NULL, /* unlink */
168 NULL, /* symlink */
169 NULL, /* mkdir */
170 NULL, /* rmdir */
171 NULL, /* mknod */
172 NULL, /* rename */
173 NULL, /* readlink */
174 NULL, /* follow_link */
175 (int (*)(struct inode *, int))
176 &hpfs_bmap, /* bmap */
177 NULL, /* truncate */
178 NULL, /* permission */
179 };
180
181 /* directory ops */
182
183 static int hpfs_dir_read(struct inode *inode, struct file *filp,
184 char *buf, int count);
185 static int hpfs_readdir(struct inode *inode, struct file *filp,
186 struct dirent *dirent, int count);
187 static int hpfs_lookup(struct inode *, const char *, int, struct inode **);
188
189 static const struct file_operations hpfs_dir_ops =
190 {
191 NULL, /* lseek - default */
192 hpfs_dir_read, /* read */
193 NULL, /* write - bad */
194 hpfs_readdir, /* readdir */
195 NULL, /* select - default */
196 NULL, /* ioctl - default */
197 NULL, /* mmap */
198 NULL, /* no special open code */
199 NULL, /* no special release code */
200 file_fsync, /* fsync */
201 };
202
203 static const struct inode_operations hpfs_dir_iops =
204 {
205 (nonconst *) & hpfs_dir_ops, /* default directory file ops */
206 NULL, /* create */
207 hpfs_lookup, /* lookup */
208 NULL, /* link */
209 NULL, /* unlink */
210 NULL, /* symlink */
211 NULL, /* mkdir */
212 NULL, /* rmdir */
213 NULL, /* mknod */
214 NULL, /* rename */
215 NULL, /* readlink */
216 NULL, /* follow_link */
217 NULL, /* bmap */
218 NULL, /* truncate */
219 NULL, /* permission */
220 };
221
222 /* Four 512-byte buffers and the 2k block obtained by concatenating them */
223
224 struct quad_buffer_head {
225 struct buffer_head *bh[4];
226 void *data;
227 };
228
229 /* forwards */
230
231 static int parse_opts(char *opts, uid_t *uid, gid_t *gid, umode_t *umask,
232 int *lowercase, int *conv);
233 static int check_warn(int not_ok,
234 const char *p1, const char *p2, const char *p3);
235 static int zerop(void *addr, unsigned len);
236 static void count_dnodes(struct inode *inode, dnode_secno dno,
237 unsigned *n_dnodes, unsigned *n_subdirs);
238 static unsigned count_bitmap(struct super_block *s);
239 static unsigned count_one_bitmap(dev_t dev, secno secno);
240 static secno bplus_lookup(struct inode *inode, struct bplus_header *b,
241 secno file_secno, struct buffer_head **bhp);
242 static struct hpfs_dirent *map_dirent(struct inode *inode, dnode_secno dno,
243 const unsigned char *name, unsigned len,
244 struct quad_buffer_head *qbh);
245 static struct hpfs_dirent *map_pos_dirent(struct inode *inode, off_t *posp,
246 struct quad_buffer_head *qbh);
247 static void write_one_dirent(struct dirent *dirent, const unsigned char *name,
248 unsigned namelen, ino_t ino, int lowercase);
249 static dnode_secno dir_subdno(struct inode *inode, unsigned pos);
250 static struct hpfs_dirent *map_nth_dirent(dev_t dev, dnode_secno dno,
251 int n,
252 struct quad_buffer_head *qbh);
253 static unsigned choose_conv(unsigned char *p, unsigned len);
254 static unsigned convcpy_tofs(unsigned char *out, unsigned char *in,
255 unsigned len);
256 static dnode_secno fnode_dno(dev_t dev, ino_t ino);
257 static struct fnode *map_fnode(dev_t dev, ino_t ino,
258 struct buffer_head **bhp);
259 static struct anode *map_anode(dev_t dev, unsigned secno,
260 struct buffer_head **bhp);
261 static struct dnode *map_dnode(dev_t dev, unsigned secno,
262 struct quad_buffer_head *qbh);
263 static void *map_sector(dev_t dev, unsigned secno, struct buffer_head **bhp);
264 static void *map_4sectors(dev_t dev, unsigned secno,
265 struct quad_buffer_head *qbh);
266 static void brelse4(struct quad_buffer_head *qbh);
267
268 /*
269 * make inode number for a file
270 */
271
272 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)
*/
273 {
274 return secno << 1 | 1;
275 }
276
277 /*
278 * make inode number for a directory
279 */
280
281 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)
*/
282 {
283 return secno << 1;
284 }
285
286 /*
287 * get fnode address from an inode number
288 */
289
290 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)
*/
291 {
292 return ino >> 1;
293 }
294
295 /*
296 * test for directory's inode number
297 */
298
299 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)
*/
300 {
301 return (ino & 1) == 0;
302 }
303
304 /*
305 * conv= options
306 */
307
308 #define CONV_BINARY 0 /* no conversion */
309 #define CONV_TEXT 1 /* crlf->newline */
310 #define CONV_AUTO 2 /* decide based on file contents */
311
312 /*
313 * local time (HPFS) to GMT (Unix)
314 */
315
316 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)
*/
317 {
318 extern struct timezone sys_tz;
319 return t + sys_tz.tz_minuteswest * 60;
320 }
321
322 /* super block ops */
323
324 /*
325 * mount. This gets one thing, the root directory inode. It does a
326 * bunch of guessed-at consistency checks.
327 */
328
329 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)
*/
330 void *options, int silent)
331 {
332 struct hpfs_boot_block *bootblock;
333 struct hpfs_super_block *superblock;
334 struct hpfs_spare_block *spareblock;
335 struct hpfs_dirent *de;
336 struct buffer_head *bh0, *bh1, *bh2;
337 struct quad_buffer_head qbh;
338 dnode_secno root_dno;
339 dev_t dev;
340 uid_t uid;
341 gid_t gid;
342 umode_t umask;
343 int lowercase;
344 int conv;
345 int dubious;
346
347 /*
348 * Get the mount options
349 */
350
351 if (!parse_opts(options, &uid, &gid, &umask, &lowercase, &conv)) {
352 printk("HPFS: syntax error in mount options. Not mounted.\n");
353 s->s_dev = 0;
354 return 0;
355 }
356
357 /*
358 * Fill in the super block struct
359 */
360
361 lock_super(s);
362 dev = s->s_dev;
363 set_blocksize(dev, 512);
364
365 /*
366 * fetch sectors 0, 16, 17
367 */
368
369 bootblock = map_sector(dev, 0, &bh0);
370 if (!bootblock)
371 goto bail;
372
373 superblock = map_sector(dev, 16, &bh1);
374 if (!superblock)
375 goto bail0;
376
377 spareblock = map_sector(dev, 17, &bh2);
378 if (!spareblock)
379 goto bail1;
380
381 /*
382 * Check that this fs looks enough like a known one that we can find
383 * and read the root directory.
384 */
385
386 if (bootblock->magic != 0xaa55
387 || superblock->magic != SB_MAGIC
388 || spareblock->magic != SP_MAGIC
389 || bootblock->sig_28h != 0x28
390 || memcmp(&bootblock->sig_hpfs, "HPFS ", 8)
391 || little_ushort(bootblock->bytes_per_sector) != 512) {
392 printk("HPFS: hpfs_read_super: Not HPFS\n");
393 goto bail2;
394 }
395
396 /*
397 * Check for inconsistencies -- possibly wrong guesses here, possibly
398 * filesystem problems.
399 */
400
401 dubious = 0;
402
403 dubious |= check_warn(spareblock->dirty != 0,
404 "`Improperly stopped'", "flag is set", "run CHKDSK");
405 dubious |= check_warn(spareblock->n_spares_used != 0,
406 "Spare blocks", "may be in use", "run CHKDSK");
407
408 /*
409 * Above errors mean we could get wrong answers if we proceed,
410 * so don't
411 */
412
413 if (dubious)
414 goto bail2;
415
416 dubious |= check_warn((spareblock->n_dnode_spares !=
417 spareblock->n_dnode_spares_free),
418 "Spare dnodes", "may be in use", "run CHKDSK");
419 dubious |= check_warn(superblock->zero1 != 0,
420 "#1", "unknown word nonzero", "investigate");
421 dubious |= check_warn(superblock->zero3 != 0,
422 "#3", "unknown word nonzero", "investigate");
423 dubious |= check_warn(superblock->zero4 != 0,
424 "#4", "unknown word nonzero", "investigate");
425 dubious |= check_warn(!zerop(superblock->zero5,
426 sizeof superblock->zero5),
427 "#5", "unknown word nonzero", "investigate");
428 dubious |= check_warn(!zerop(superblock->zero6,
429 sizeof superblock->zero6),
430 "#6", "unknown word nonzero", "investigate");
431
432 if (dubious)
433 printk("HPFS: Proceeding, but operation may be unreliable\n");
434
435 /*
436 * set fs read only
437 */
438
439 s->s_flags |= MS_RDONLY;
440
441 /*
442 * fill in standard stuff
443 */
444
445 s->s_magic = HPFS_SUPER_MAGIC;
446 s->s_blocksize = 512;
447 s->s_blocksize_bits = 9;
448 s->s_op = (struct super_operations *) &hpfs_sops;
449
450 /*
451 * fill in hpfs stuff
452 */
453
454 s->s_hpfs_root = dir_ino(superblock->root);
455 s->s_hpfs_fs_size = superblock->n_sectors;
456 s->s_hpfs_dirband_size = superblock->n_dir_band / 4;
457 s->s_hpfs_dmap = superblock->dir_band_bitmap;
458 s->s_hpfs_bitmaps = superblock->bitmaps;
459 s->s_hpfs_uid = uid;
460 s->s_hpfs_gid = gid;
461 s->s_hpfs_mode = 0777 & ~umask;
462 s->s_hpfs_n_free = -1;
463 s->s_hpfs_n_free_dnodes = -1;
464 s->s_hpfs_lowercase = lowercase;
465 s->s_hpfs_conv = conv;
466
467 /*
468 * done with the low blocks
469 */
470
471 brelse(bh2);
472 brelse(bh1);
473 brelse(bh0);
474
475 /*
476 * all set. try it out.
477 */
478
479 s->s_mounted = iget(s, s->s_hpfs_root);
480 unlock_super(s);
481
482 if (!s->s_mounted) {
483 printk("HPFS: hpfs_read_super: inode get failed\n");
484 s->s_dev = 0;
485 return 0;
486 }
487
488 /*
489 * find the root directory's . pointer & finish filling in the inode
490 */
491
492 root_dno = fnode_dno(dev, s->s_hpfs_root);
493 if (root_dno)
494 de = map_dirent(s->s_mounted, root_dno, "\001\001", 2, &qbh);
495 if (!root_dno || !de) {
496 printk("HPFS: "
497 "hpfs_read_super: root dir isn't in the root dir\n");
498 s->s_dev = 0;
499 return 0;
500 }
501
502 s->s_mounted->i_atime = local_to_gmt(de->read_date);
503 s->s_mounted->i_mtime = local_to_gmt(de->write_date);
504 s->s_mounted->i_ctime = local_to_gmt(de->creation_date);
505
506 brelse4(&qbh);
507 return s;
508
509 bail2:
510 brelse(bh2);
511 bail1:
512 brelse(bh1);
513 bail0:
514 brelse(bh0);
515 bail:
516 s->s_dev = 0;
517 unlock_super(s);
518 return 0;
519 }
520
521 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)
*/
522 const char *p1, const char *p2, const char *p3)
523 {
524 if (not_ok)
525 printk("HPFS: %s %s. Please %s\n", p1, p2, p3);
526 return not_ok;
527 }
528
529 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)
*/
530 {
531 unsigned char *p = addr;
532 return p[0] == 0 && memcmp(p, p + 1, len - 1) == 0;
533 }
534
535 /*
536 * A tiny parser for option strings, stolen from dosfs.
537 */
538
539 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)
*/
540 int *lowercase, int *conv)
541 {
542 char *p, *rhs;
543
544 *uid = current->uid;
545 *gid = current->gid;
546 *umask = current->fs->umask;
547 *lowercase = 1;
548 *conv = CONV_BINARY;
549
550 if (!opts)
551 return 1;
552
553 for (p = strtok(opts, ","); p != 0; p = strtok(0, ",")) {
554 if ((rhs = strchr(p, '=')) != 0)
555 *rhs++ = '\0';
556 if (!strcmp(p, "uid")) {
557 if (!rhs || !*rhs)
558 return 0;
559 *uid = simple_strtoul(rhs, &rhs, 0);
560 if (*rhs)
561 return 0;
562 }
563 else if (!strcmp(p, "gid")) {
564 if (!rhs || !*rhs)
565 return 0;
566 *gid = simple_strtoul(rhs, &rhs, 0);
567 if (*rhs)
568 return 0;
569 }
570 else if (!strcmp(p, "umask")) {
571 if (!rhs || !*rhs)
572 return 0;
573 *umask = simple_strtoul(rhs, &rhs, 8);
574 if (*rhs)
575 return 0;
576 }
577 else if (!strcmp(p, "case")) {
578 if (!strcmp(rhs, "lower"))
579 *lowercase = 1;
580 else if (!strcmp(rhs, "asis"))
581 *lowercase = 0;
582 else
583 return 0;
584 }
585 else if (!strcmp(p, "conv")) {
586 if (!strcmp(rhs, "binary"))
587 *conv = CONV_BINARY;
588 else if (!strcmp(rhs, "text"))
589 *conv = CONV_TEXT;
590 else if (!strcmp(rhs, "auto"))
591 *conv = CONV_AUTO;
592 else
593 return 0;
594 }
595 else
596 return 0;
597 }
598
599 return 1;
600 }
601
602 /*
603 * read_inode. This is called with exclusive access to a new inode that
604 * has only (i_dev,i_ino) set. It is responsible for filling in the rest.
605 * We leave the dates blank, to be filled in from the dir entry.
606 *
607 * NOTE that there must be no sleeping from the return in this routine
608 * until lookup() finishes filling in the inode, otherwise the partly
609 * completed inode would be visible during the sleep.
610 *
611 * It is done in this strange and sinful way because the alternative
612 * is to read the fnode, find the dir pointer in it, read that fnode
613 * to get the dnode pointer, search through that whole directory for
614 * the ino we're reading, and get the dates. It works that way, but
615 * ls sounds like fsck.
616 */
617
618 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)
*/
619 {
620 struct super_block *s = inode->i_sb;
621
622 /* be ready to bail out */
623
624 inode->i_op = 0;
625 inode->i_mode = 0;
626
627 if (inode->i_ino == 0
628 || ino_secno(inode->i_ino) >= inode->i_sb->s_hpfs_fs_size) {
629 printk("HPFS: read_inode: bad ino\n");
630 return;
631 }
632
633 /*
634 * canned stuff
635 */
636
637 inode->i_uid = s->s_hpfs_uid;
638 inode->i_gid = s->s_hpfs_gid;
639 inode->i_mode = s->s_hpfs_mode;
640 inode->i_hpfs_conv = s->s_hpfs_conv;
641
642 inode->i_hpfs_dno = 0;
643 inode->i_hpfs_n_secs = 0;
644 inode->i_hpfs_file_sec = 0;
645 inode->i_hpfs_disk_sec = 0;
646 inode->i_hpfs_dpos = 0;
647 inode->i_hpfs_dsubdno = 0;
648
649 /*
650 * figure out whether we are looking at a directory or a file
651 */
652
653 if (ino_is_dir(inode->i_ino))
654 inode->i_mode |= S_IFDIR;
655 else {
656 inode->i_mode |= S_IFREG;
657 inode->i_mode &= ~0111;
658 }
659
660 /*
661 * these fields must be filled in from the dir entry, which we don't
662 * have but lookup does. It will fill them in before letting the
663 * inode out of its grasp.
664 */
665
666 inode->i_atime = 0;
667 inode->i_mtime = 0;
668 inode->i_ctime = 0;
669 inode->i_size = 0;
670
671 /*
672 * fill in the rest
673 */
674
675 if (S_ISREG(inode->i_mode)) {
676
677 inode->i_op = (struct inode_operations *) &hpfs_file_iops;
678 inode->i_nlink = 1;
679 inode->i_blksize = 512;
680
681 }
682 else {
683 unsigned n_dnodes, n_subdirs;
684 struct buffer_head *bh0;
685 struct fnode *fnode = map_fnode(inode->i_dev,
686 inode->i_ino, &bh0);
687
688 if (!fnode) {
689 printk("HPFS: read_inode: no fnode\n");
690 inode->i_mode = 0;
691 return;
692 }
693
694 inode->i_hpfs_parent_dir = dir_ino(fnode->up);
695 inode->i_hpfs_dno = fnode->u.external[0].disk_secno;
696
697 brelse(bh0);
698
699 n_dnodes = n_subdirs = 0;
700 count_dnodes(inode, inode->i_hpfs_dno, &n_dnodes, &n_subdirs);
701
702 inode->i_op = (struct inode_operations *) &hpfs_dir_iops;
703 inode->i_blksize = 512; /* 2048 here confuses ls & du & ... */
704 inode->i_blocks = 4 * n_dnodes;
705 inode->i_size = 512 * inode->i_blocks;
706 inode->i_nlink = 2 + n_subdirs;
707 }
708 }
709
710 /*
711 * unmount.
712 */
713
714 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)
*/
715 {
716 lock_super(s);
717 s->s_dev = 0;
718 unlock_super(s);
719 }
720
721 /*
722 * statfs. For free inode counts we report the count of dnodes in the
723 * directory band -- not exactly right but pretty analagous.
724 */
725
726 static void hpfs_statfs(struct super_block *s, struct statfs *buf)
/* ![[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)
*/
727 {
728 /*
729 * count the bits in the bitmaps, unless we already have
730 */
731
732 if (s->s_hpfs_n_free == -1) {
733 s->s_hpfs_n_free = count_bitmap(s);
734 s->s_hpfs_n_free_dnodes =
735 count_one_bitmap(s->s_dev, s->s_hpfs_dmap);
736 }
737
738 /*
739 * fill in the user statfs struct
740 */
741
742 put_fs_long(s->s_magic, &buf->f_type);
743 put_fs_long(512, &buf->f_bsize);
744 put_fs_long(s->s_hpfs_fs_size, &buf->f_blocks);
745 put_fs_long(s->s_hpfs_n_free, &buf->f_bfree);
746 put_fs_long(s->s_hpfs_n_free, &buf->f_bavail);
747 put_fs_long(s->s_hpfs_dirband_size, &buf->f_files);
748 put_fs_long(s->s_hpfs_n_free_dnodes, &buf->f_ffree);
749 put_fs_long(254, &buf->f_namelen);
750 }
751
752 /*
753 * remount. Don't let read only be turned off.
754 */
755
756 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)
*/
757 {
758 if (!(*flags & MS_RDONLY))
759 return -EINVAL;
760 return 0;
761 }
762
763 /*
764 * count the dnodes in a directory, and the subdirs.
765 */
766
767 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)
*/
768 unsigned *n_dnodes, unsigned *n_subdirs)
769 {
770 struct quad_buffer_head qbh;
771 struct dnode *dnode;
772 struct hpfs_dirent *de;
773 struct hpfs_dirent *de_end;
774
775 dnode = map_dnode(inode->i_dev, dno, &qbh);
776 if (!dnode)
777 return;
778 de = dnode_first_de(dnode);
779 de_end = dnode_end_de(dnode);
780
781 (*n_dnodes)++;
782
783 for (; de < de_end; de = de_next_de(de)) {
784 if (de->down)
785 count_dnodes(inode, de_down_pointer(de),
786 n_dnodes, n_subdirs);
787 if (de->directory && !de->first)
788 (*n_subdirs)++;
789 if (de->last || de->length == 0)
790 break;
791 }
792
793 brelse4(&qbh);
794 }
795
796 /*
797 * count the bits in the free space bit maps
798 */
799
800 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)
*/
801 {
802 unsigned n, count, n_bands;
803 secno *bitmaps;
804 struct quad_buffer_head qbh;
805
806 /*
807 * there is one bit map for each 16384 sectors
808 */
809 n_bands = (s->s_hpfs_fs_size + 0x3fff) >> 14;
810
811 /*
812 * their locations are given in an array pointed to by the super
813 * block
814 */
815 bitmaps = map_4sectors(s->s_dev, s->s_hpfs_bitmaps, &qbh);
816 if (!bitmaps)
817 return 0;
818
819 count = 0;
820
821 /*
822 * map each one and count the free sectors
823 */
824 for (n = 0; n < n_bands; n++)
825 if (bitmaps[n] == 0)
826 printk("HPFS: bit map pointer missing\n");
827 else
828 count += count_one_bitmap(s->s_dev, bitmaps[n]);
829
830 brelse4(&qbh);
831 return count;
832 }
833
834 /*
835 * Read in one bit map, count the bits, return the count.
836 */
837
838 static unsigned count_one_bitmap(dev_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)
*/
839 {
840 struct quad_buffer_head qbh;
841 char *bits;
842 unsigned i, count;
843
844 bits = map_4sectors(dev, secno, &qbh);
845 if (!bits)
846 return 0;
847
848 count = 0;
849
850 for (i = 0; i < 8 * 2048; i++)
851 count += (test_bit(i, bits) != 0);
852 brelse4(&qbh);
853
854 return count;
855 }
856
857 /* file ops */
858
859 /*
860 * read. Read the bytes, put them in buf, return the count.
861 */
862
863 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)
*/
864 char *buf, int count)
865 {
866 unsigned q, r, n, n0;
867 struct buffer_head *bh;
868 char *block;
869 char *start;
870
871 if (inode == 0 || !S_ISREG(inode->i_mode))
872 return -EINVAL;
873
874 /*
875 * truncate count at EOF
876 */
877 if (count > inode->i_size - filp->f_pos)
878 count = inode->i_size - filp->f_pos;
879
880 start = buf;
881 while (count > 0) {
882 /*
883 * get file sector number, offset in sector, length to end of
884 * sector
885 */
886 q = filp->f_pos >> 9;
887 r = filp->f_pos & 511;
888 n = 512 - r;
889
890 /*
891 * get length to copy to user buffer
892 */
893 if (n > count)
894 n = count;
895
896 /*
897 * read the sector, copy to user
898 */
899 block = map_sector(inode->i_dev, hpfs_bmap(inode, q), &bh);
900 if (!block)
901 return -EIO;
902
903 /*
904 * but first decide if it has \r\n, if the mount option said
905 * to do that
906 */
907 if (inode->i_hpfs_conv == CONV_AUTO)
908 inode->i_hpfs_conv = choose_conv(block + r, n);
909
910 if (inode->i_hpfs_conv == CONV_BINARY) {
911 /*
912 * regular copy, output length is same as input
913 * length
914 */
915 memcpy_tofs(buf, block + r, n);
916 n0 = n;
917 }
918 else {
919 /*
920 * squeeze out \r, output length varies
921 */
922 n0 = convcpy_tofs(buf, block + r, n);
923 if (count > inode->i_size - filp->f_pos - n + n0)
924 count = inode->i_size - filp->f_pos - n + n0;
925 }
926
927 brelse(bh);
928
929 /*
930 * advance input n bytes, output n0 bytes
931 */
932 filp->f_pos += n;
933 buf += n0;
934 count -= n0;
935 }
936
937 return buf - start;
938 }
939
940 /*
941 * This routine implements conv=auto. Return CONV_BINARY or CONV_TEXT.
942 */
943
944 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)
*/
945 {
946 unsigned tvote, bvote;
947 unsigned c;
948
949 tvote = bvote = 0;
950
951 while (len--) {
952 c = *p++;
953 if (c < ' ')
954 if (c == '\r' && len && *p == '\n')
955 tvote += 10;
956 else if (c == '\t' || c == '\n');
957 else
958 bvote += 5;
959 else if (c < '\177')
960 tvote++;
961 else
962 bvote += 5;
963 }
964
965 if (tvote > bvote)
966 return CONV_TEXT;
967 else
968 return CONV_BINARY;
969 }
970
971 /*
972 * This routine implements conv=text. :s/crlf/nl/
973 */
974
975 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)
*/
976 unsigned len)
977 {
978 unsigned char *start = out;
979
980 while (len--) {
981 unsigned c = *in++;
982 if (c == '\r' && (len == 0 || *in == '\n'));
983 else
984 put_fs_byte(c, out++);
985 }
986
987 return out - start;
988 }
989
990 /*
991 * Return the disk sector number containing a file sector.
992 */
993
994 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)
*/
995 {
996 unsigned n, disk_secno;
997 struct fnode *fnode;
998 struct buffer_head *bh;
999
1000 /*
1001 * There is one sector run cached in the inode. See if the sector is
1002 * in it.
1003 */
1004
1005 n = file_secno - inode->i_hpfs_file_sec;
1006 if (n < inode->i_hpfs_n_secs)
1007 return inode->i_hpfs_disk_sec + n;
1008
1009 /*
1010 * No, read the fnode and go find the sector.
1011 */
1012
1013 else {
1014 fnode = map_fnode(inode->i_dev, inode->i_ino, &bh);
1015 if (!fnode)
1016 return 0;
1017 disk_secno = bplus_lookup(inode, &fnode->btree,
1018 file_secno, &bh);
1019 brelse(bh);
1020 return disk_secno;
1021 }
1022 }
1023
1024 /*
1025 * Search allocation tree *b for the given file sector number and return
1026 * the disk sector number. Buffer *bhp has the tree in it, and can be
1027 * reused for subtrees when access to *b is no longer needed.
1028 * *bhp is busy on entry and exit.
1029 */
1030
1031 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)
*/
1032 secno file_secno, struct buffer_head **bhp)
1033 {
1034 int i;
1035
1036 /*
1037 * A leaf-level tree gives a list of sector runs. Find the one
1038 * containing the file sector we want, cache the map info in the
1039 * inode for later, and return the corresponding disk sector.
1040 */
1041
1042 if (!b->internal) {
1043 struct bplus_leaf_node *n = b->u.external;
1044 for (i = 0; i < b->n_used_nodes; i++) {
1045 unsigned t = file_secno - n[i].file_secno;
1046 if (t < n[i].length) {
1047 inode->i_hpfs_file_sec = n[i].file_secno;
1048 inode->i_hpfs_disk_sec = n[i].disk_secno;
1049 inode->i_hpfs_n_secs = n[i].length;
1050 return n[i].disk_secno + t;
1051 }
1052 }
1053 }
1054
1055 /*
1056 * A non-leaf tree gives a list of subtrees. Find the one containing
1057 * the file sector we want, read it in, and recurse to search it.
1058 */
1059
1060 else {
1061 struct bplus_internal_node *n = b->u.internal;
1062 for (i = 0; i < b->n_used_nodes; i++) {
1063 if (file_secno < n[i].file_secno) {
1064 struct anode *anode;
1065 anode_secno ano = n[i].down;
1066 brelse(*bhp);
1067 anode = map_anode(inode->i_dev, ano, bhp);
1068 if (!anode)
1069 break;
1070 return bplus_lookup(inode, &anode->btree,
1071 file_secno, bhp);
1072 }
1073 }
1074 }
1075
1076 /*
1077 * If we get here there was a hole in the file. As far as I know we
1078 * never do get here, but falling off the end would be indelicate. So
1079 * return a pointer to a handy all-zero sector. This is not a
1080 * reasonable way to handle files with holes if they really do
1081 * happen.
1082 */
1083
1084 printk("HPFS: bplus_lookup: sector not found\n");
1085 return 15;
1086 }
1087
1088 /* directory ops */
1089
1090 /*
1091 * lookup. Search the specified directory for the specified name, set
1092 * *result to the corresponding inode.
1093 *
1094 * lookup uses the inode number to tell read_inode whether it is reading
1095 * the inode of a directory or a file -- file ino's are odd, directory
1096 * ino's are even. read_inode avoids i/o for file inodes; everything
1097 * needed is up here in the directory. (And file fnodes are out in
1098 * the boondocks.)
1099 */
1100
1101 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)
*/
1102 struct inode **result)
1103 {
1104 struct quad_buffer_head qbh;
1105 struct hpfs_dirent *de;
1106 struct inode *inode;
1107 ino_t ino;
1108
1109 /* In case of madness */
1110
1111 *result = 0;
1112 if (dir == 0)
1113 return -ENOENT;
1114 if (!S_ISDIR(dir->i_mode))
1115 goto bail;
1116
1117 /*
1118 * Read in the directory entry. "." is there under the name ^A^A .
1119 * Always read the dir even for . and .. in case we need the dates.
1120 */
1121
1122 if (name[0] == '.' && len == 1)
1123 de = map_dirent(dir, dir->i_hpfs_dno, "\001\001", 2, &qbh);
1124 else if (name[0] == '.' && name[1] == '.' && len == 2)
1125 de = map_dirent(dir,
1126 fnode_dno(dir->i_dev, dir->i_hpfs_parent_dir),
1127 "\001\001", 2, &qbh);
1128 else
1129 de = map_dirent(dir, dir->i_hpfs_dno, name, len, &qbh);
1130
1131 /*
1132 * This is not really a bailout, just means file not found.
1133 */
1134
1135 if (!de)
1136 goto bail;
1137
1138 /*
1139 * Get inode number, what we're after.
1140 */
1141
1142 if (de->directory)
1143 ino = dir_ino(de->fnode);
1144 else
1145 ino = file_ino(de->fnode);
1146
1147 /*
1148 * Go find or make an inode.
1149 */
1150
1151 if (!(inode = iget(dir->i_sb, ino)))
1152 goto bail1;
1153
1154 /*
1155 * Fill in the info from the directory if this is a newly created
1156 * inode.
1157 */
1158
1159 if (!inode->i_atime) {
1160 inode->i_atime = local_to_gmt(de->read_date);
1161 inode->i_mtime = local_to_gmt(de->write_date);
1162 inode->i_ctime = local_to_gmt(de->creation_date);
1163 if (de->read_only)
1164 inode->i_mode &= ~0222;
1165 if (!de->directory) {
1166 inode->i_size = de->file_size;
1167 /*
1168 * i_blocks should count the fnode and any anodes.
1169 * We count 1 for the fnode and don't bother about
1170 * anodes -- the disk heads are on the directory band
1171 * and we want them to stay there.
1172 */
1173 inode->i_blocks = 1 + ((inode->i_size + 511) >> 9);
1174 }
1175 }
1176
1177 brelse4(&qbh);
1178
1179 /*
1180 * Made it.
1181 */
1182
1183 *result = inode;
1184 iput(dir);
1185 return 0;
1186
1187 /*
1188 * Didn't.
1189 */
1190 bail1:
1191 brelse4(&qbh);
1192 bail:
1193 iput(dir);
1194 return -ENOENT;
1195 }
1196
1197 /*
1198 * Compare two counted strings ignoring case.
1199 * HPFS directory order sorts letters as if they're upper case.
1200 */
1201
1202 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)
*/
1203 unsigned n)
1204 {
1205 int t;
1206
1207 if (n != 0)
1208 do {
1209 unsigned c1 = *s1++;
1210 unsigned c2 = *s2++;
1211 if (c1 - 'a' < 26)
1212 c1 -= 040;
1213 if (c2 - 'a' < 26)
1214 c2 -= 040;
1215 if ((t = c1 - c2) != 0)
1216 return t;
1217 } while (--n != 0);
1218
1219 return 0;
1220 }
1221
1222 /*
1223 * Search a directory for the given name, return a pointer to its dir entry
1224 * and a pointer to the buffer containing it.
1225 */
1226
1227 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)
*/
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 * read the dnode at the root of our subtree
1238 */
1239 dnode = map_dnode(inode->i_dev, dno, qbh);
1240 if (!dnode)
1241 return 0;
1242
1243 /*
1244 * get pointers to start and end+1 of dir entries
1245 */
1246 de = dnode_first_de(dnode);
1247 de_end = dnode_end_de(dnode);
1248
1249 /*
1250 * look through the entries for the name we're after
1251 */
1252 for ( ; de < de_end; de = de_next_de(de)) {
1253
1254 /*
1255 * compare names
1256 */
1257 l = len < de->namelen ? len : de->namelen;
1258 t = memcasecmp(name, de->name, l);
1259
1260 /*
1261 * initial substring matches, compare lengths
1262 */
1263 if (t == 0) {
1264 t = len - de->namelen;
1265 /* bingo */
1266 if (t == 0)
1267 return de;
1268 }
1269
1270 /*
1271 * wanted name .lt. dir name => not present.
1272 */
1273 if (t < 0) {
1274 /*
1275 * if there is a subtree, search it.
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 * de->last is set on the last name in the dnode (it's always
1289 * a "\377" pseudo entry). de->length == 0 means we're about
1290 * to infinite loop. This test does nothing in a well-formed
1291 * dnode.
1292 */
1293 if (de->last || de->length == 0)
1294 break;
1295 }
1296
1297 /*
1298 * name not found.
1299 */
1300
1301 return 0;
1302 }
1303
1304 /*
1305 * readdir. Return exactly 1 dirent. (I tried and tried, but currently
1306 * the interface with libc just does not permit more than 1. If it gets
1307 * fixed, throw this out and just walk the tree and write records into
1308 * the user buffer.)
1309 *
1310 * We keep track of our position in the dnode tree with a sort of
1311 * dewey-decimal record of subtree locations. Like so:
1312 *
1313 * (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)
1314 *
1315 * Subtrees appear after their file, out of lexical order,
1316 * which would be before their file. It's easier.
1317 *
1318 * A directory can't hold more than 56 files, so 6 bits are used for
1319 * position numbers. If the tree is so deep that the position encoding
1320 * doesn't fit, I'm sure something absolutely fascinating happens.
1321 *
1322 * The actual sequence of f_pos values is
1323 * 0 => . -1 => .. 1 1.1 ... 8.9 9 => files -2 => eof
1324 *
1325 * The directory inode caches one position-to-dnode correspondence so
1326 * we won't have to repeatedly scan the top levels of the tree.
1327 */
1328
1329 static int hpfs_readdir(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)
*/
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 (filp->f_pos) {
1345 case 0:
1346 write_one_dirent(dirent, ".", 1, inode->i_ino, lc);
1347 filp->f_pos = -1;
1348 return 1;
1349
1350 case -1:
1351 write_one_dirent(dirent, "..", 2,
1352 inode->i_hpfs_parent_dir, lc);
1353 filp->f_pos = 1;
1354 return 2;
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 namelen;
1375 }
1376 }
1377
1378 /*
1379 * Send the given name and ino off to the user dirent struct at *dirent.
1380 * Blam it to lowercase if the mount option said to.
1381 *
1382 * Note that Linux d_reclen is the length of the file name, and has nothing
1383 * to do with the length of the dirent record.
1384 */
1385
1386 static void write_one_dirent(struct dirent *dirent, const unsigned char *name,
/* ![[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)
*/
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 if (lowercase)
1395 for (n = namelen; n != 0;) {
1396 unsigned t = name[--n];
1397 if (t - 'A' < 26)
1398 t += 040;
1399 put_fs_byte(t, &dirent->d_name[n]);
1400 }
1401 else
1402 memcpy_tofs(dirent->d_name, name, namelen);
1403
1404 put_fs_byte(0, &dirent->d_name[namelen]);
1405 }
1406
1407 /*
1408 * Map the dir entry at subtree coordinates given by *posp, and
1409 * increment *posp to point to the following dir entry.
1410 */
1411
1412 static struct hpfs_dirent *map_pos_dirent(struct inode *inode, off_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)
*/
1413 struct quad_buffer_head *qbh)
1414 {
1415 unsigned pos, q, r;
1416 dnode_secno dno;
1417 struct hpfs_dirent *de;
1418
1419 /*
1420 * Get the position code and split off the rightmost index r
1421 */
1422
1423 pos = *posp;
1424 q = pos >> 6;
1425 r = pos & 077;
1426
1427 /*
1428 * Get the sector address of the dnode
1429 * pointed to by the leading part q
1430 */
1431
1432 dno = dir_subdno(inode, q);
1433 if (!dno)
1434 return 0;
1435
1436 /*
1437 * Get the entry at index r in dnode q
1438 */
1439
1440 de = map_nth_dirent(inode->i_dev, dno, r, qbh);
1441
1442 /*
1443 * If none, we're out of files in this dnode. Ascend.
1444 */
1445
1446 if (!de) {
1447 if (q == 0)
1448 return 0;
1449 *posp = q + 1;
1450 return map_pos_dirent(inode, posp, qbh);
1451 }
1452
1453 /*
1454 * If a subtree is here, descend.
1455 */
1456
1457 if (de->down)
1458 *posp = pos << 6 | 1;
1459 else
1460 *posp = pos + 1;
1461
1462 /*
1463 * Don't return the ^A^A and \377 entries.
1464 */
1465
1466 if (de->first || de->last) {
1467 brelse4(qbh);
1468 return map_pos_dirent(inode, posp, qbh);
1469 }
1470 else
1471 return de;
1472 }
1473
1474 /*
1475 * Return the address of the dnode with subtree coordinates given by pos.
1476 */
1477
1478 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)
*/
1479 {
1480 struct hpfs_dirent *de;
1481 struct quad_buffer_head qbh;
1482
1483 /*
1484 * 0 is the root dnode
1485 */
1486
1487 if (pos == 0)
1488 return inode->i_hpfs_dno;
1489
1490 /*
1491 * we have one pos->dnode translation cached in the inode
1492 */
1493
1494 else if (pos == inode->i_hpfs_dpos)
1495 return inode->i_hpfs_dsubdno;
1496
1497 /*
1498 * otherwise go look
1499 */
1500
1501 else {
1502 unsigned q = pos >> 6;
1503 unsigned r = pos & 077;
1504 dnode_secno dno;
1505
1506 /*
1507 * dnode at position q
1508 */
1509 dno = dir_subdno(inode, q);
1510 if (dno == 0)
1511 return 0;
1512
1513 /*
1514 * entry at index r
1515 */
1516 de = map_nth_dirent(inode->i_dev, dno, r, &qbh);
1517 if (!de || !de->down)
1518 return 0;
1519
1520 /*
1521 * get the dnode down pointer
1522 */
1523 dno = de_down_pointer(de);
1524 brelse4(&qbh);
1525
1526 /*
1527 * cache it for next time
1528 */
1529 inode->i_hpfs_dpos = pos;
1530 inode->i_hpfs_dsubdno = dno;
1531 return dno;
1532 }
1533 }
1534
1535 /*
1536 * Return the dir entry at index n in dnode dno, or 0 if there isn't one
1537 */
1538
1539 static struct hpfs_dirent *map_nth_dirent(dev_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)
*/
1540 int n,
1541 struct quad_buffer_head *qbh)
1542 {
1543 int i;
1544 struct hpfs_dirent *de, *de_end;
1545 struct dnode *dnode = map_dnode(dev, dno, qbh);
1546
1547 de = dnode_first_de(dnode);
1548 de_end = dnode_end_de(dnode);
1549
1550 for (i = 1; de < de_end; i++, de = de_next_de(de)) {
1551 if (i == n)
1552 return de;
1553 if (de->last || de->length == 0)
1554 break;
1555 }
1556
1557 brelse4(qbh);
1558 return 0;
1559 }
1560
1561 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)
*/
1562 char *buf, int count)
1563 {
1564 return -EISDIR;
1565 }
1566
1567 /* Return the dnode pointer in a directory fnode */
1568
1569 static dnode_secno fnode_dno(dev_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)
*/
1570 {
1571 struct buffer_head *bh;
1572 struct fnode *fnode;
1573 dnode_secno dno;
1574
1575 fnode = map_fnode(dev, ino, &bh);
1576 if (!fnode)
1577 return 0;
1578
1579 dno = fnode->u.external[0].disk_secno;
1580 brelse(bh);
1581 return dno;
1582 }
1583
1584 /* Map an fnode into a buffer and return pointers to it and to the buffer. */
1585
1586 static struct fnode *map_fnode(dev_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)
*/
1587 {
1588 struct fnode *fnode;
1589
1590 if (ino == 0) {
1591 printk("HPFS: missing fnode\n");
1592 return 0;
1593 }
1594
1595 fnode = map_sector(dev, ino_secno(ino), bhp);
1596 if (fnode)
1597 if (fnode->magic != FNODE_MAGIC) {
1598 printk("HPFS: map_fnode: bad fnode pointer\n");
1599 brelse(*bhp);
1600 return 0;
1601 }
1602 return fnode;
1603 }
1604
1605 /* Map an anode into a buffer and return pointers to it and to the buffer. */
1606
1607 static struct anode *map_anode(dev_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)
*/
1608 struct buffer_head **bhp)
1609 {
1610 struct anode *anode;
1611
1612 if (secno == 0) {
1613 printk("HPFS: missing anode\n");
1614 return 0;
1615 }
1616
1617 anode = map_sector(dev, secno, bhp);
1618 if (anode)
1619 if (anode->magic != ANODE_MAGIC || anode->self != secno) {
1620 printk("HPFS: map_anode: bad anode pointer\n");
1621 brelse(*bhp);
1622 return 0;
1623 }
1624 return anode;
1625 }
1626
1627 /* Map a dnode into a buffer and return pointers to it and to the buffer. */
1628
1629 static struct dnode *map_dnode(dev_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)
*/
1630 struct quad_buffer_head *qbh)
1631 {
1632 struct dnode *dnode;
1633
1634 if (secno == 0) {
1635 printk("HPFS: missing dnode\n");
1636 return 0;
1637 }
1638
1639 dnode = map_4sectors(dev, secno, qbh);
1640 if (dnode)
1641 if (dnode->magic != DNODE_MAGIC || dnode->self != secno) {
1642 printk("HPFS: map_dnode: bad dnode pointer\n");
1643 brelse4(qbh);
1644 return 0;
1645 }
1646 return dnode;
1647 }
1648
1649 /* Map a sector into a buffer and return pointers to it and to the buffer. */
1650
1651 static void *map_sector(dev_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)
*/
1652 {
1653 struct buffer_head *bh;
1654
1655 if ((*bhp = bh = bread(dev, secno, 512)) != 0)
1656 return bh->b_data;
1657 else {
1658 printk("HPFS: map_sector: read error\n");
1659 return 0;
1660 }
1661 }
1662
1663 /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
1664
1665 static void *map_4sectors(dev_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)
*/
1666 struct quad_buffer_head *qbh)
1667 {
1668 struct buffer_head *bh;
1669 char *data;
1670
1671 if (secno & 3) {
1672 printk("HPFS: map_4sectors: unaligned read\n");
1673 return 0;
1674 }
1675
1676 qbh->data = data = kmalloc(2048, GFP_KERNEL);
1677 if (!data)
1678 goto bail;
1679
1680 qbh->bh[0] = bh = breada(dev, secno, 512, 0, UINT_MAX);
1681 if (!bh)
1682 goto bail0;
1683 memcpy(data, bh->b_data, 512);
1684
1685 qbh->bh[1] = bh = bread(dev, secno + 1, 512);
1686 if (!bh)
1687 goto bail1;
1688 memcpy(data + 512, bh->b_data, 512);
1689
1690 qbh->bh[2] = bh = bread(dev, secno + 2, 512);
1691 if (!bh)
1692 goto bail2;
1693 memcpy(data + 2 * 512, bh->b_data, 512);
1694
1695 qbh->bh[3] = bh = bread(dev, secno + 3, 512);
1696 if (!bh)
1697 goto bail3;
1698 memcpy(data + 3 * 512, bh->b_data, 512);
1699
1700 return data;
1701
1702 bail3:
1703 brelse(qbh->bh[2]);
1704 bail2:
1705 brelse(qbh->bh[1]);
1706 bail1:
1707 brelse(qbh->bh[0]);
1708 bail0:
1709 kfree_s(data, 2048);
1710 bail:
1711 printk("HPFS: map_4sectors: read error\n");
1712 return 0;
1713 }
1714
1715 /* Deallocate a 4-buffer block */
1716
1717 static void brelse4(struct quad_buffer_head *qbh)
/* ![[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)
*/
1718 {
1719 brelse(qbh->bh[3]);
1720 brelse(qbh->bh[2]);
1721 brelse(qbh->bh[1]);
1722 brelse(qbh->bh[0]);
1723 kfree_s(qbh->data, 2048);
1724 }