summaryrefslogtreecommitdiff
path: root/fatfs/dir.c
blob: b9b7ae55031a23eb924885325b0f6d56a15c5622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
/* dir.c - FAT filesystem.

   Copyright (C) 1997, 1998, 1999, 2002, 2003, 2007
     Free Software Foundation, Inc.

   Written by Thomas Bushnell, n/BSG and Marcus Brinkmann.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include <ctype.h>
#include <string.h>
#include <dirent.h>
#include <hurd/fsys.h>

#include "fatfs.h"

/* The size of a directory block is usually just the cluster size.
   However, the root directory of FAT12/16 file systems is stored in
   sectors in a special region, so we settle on the greatest common
   divisor here.  */
#define DIRBLKSIZ bytes_per_sector
#define LOG2_DIRBLKSIZ log2_bytes_per_sector

enum slot_status
{
  /* This means we haven't yet found room for a new entry.  */
  LOOKING,

  /* This means that the specified entry is free and should be used. */
  TAKE,

  /* This means that the specified entry has enough room at the end
     to hold the new entry. */
  SHRINK,

  /* This means that there is enough space in the block, but not in
     any one single entry, so they all have to be shifted to make
     room.  */
  COMPRESS,

  /* This means that the directory will have to be grown to hold the
     entry. */
  EXTEND,

  /* For removal and rename, this means that this is the location
     of the entry found.  */
  HERE_TIS,
};

struct dirstat
{
  /* Type of followp operation expected.  */
  enum lookup_type type;

  /* One of the statuses above.  */
  enum slot_status stat;

  /* Mapped address and length of directory.  */
  vm_address_t mapbuf;
  vm_size_t mapextent;

  /* Index of this directory block.  */
  int idx;

  /* For stat COMPRESS, this is the address (inside mapbuf)
     of the first direct in the directory block to be compressed.  */
  /* For stat HERE_TIS, SHRINK, and TAKE, this is the entry referenced.  */
  struct dirrect *entry;

  /* For stat HERE_TIS, type REMOVE, this is the address of the immediately
     previous direct in this directory block, or zero if this is the first.  */
  struct dirrect *preventry;

  /* For stat COMPRESS, this is the number of bytes needed to be copied
     in order to undertake the compression.  */
  size_t nbytes;
};

const size_t diskfs_dirstat_size = sizeof (struct dirstat);

/* Initialize DS such that diskfs_drop_dirstat will ignore it.  */
void
diskfs_null_dirstat (struct dirstat *ds)
{
  ds->type = LOOKUP;
}

/* Forward declaration.  */
static error_t
dirscanblock (vm_address_t blockoff, struct node *dp, int idx,
              const char *name, int namelen, enum lookup_type type,
              struct dirstat *ds, ino_t *inum);

static int
fatnamematch (const char *dirname, const char *username, size_t unamelen)
{
  char *dn = strdup(dirname);
  int dpos = 0;
  int upos = 0;
  int ext = 0;

  /* Deleted files. */
  if (dn[0] == FAT_DIR_NAME_DELETED || dn[0] == FAT_DIR_NAME_LAST)
    return 0;
  if (dn[0] == FAT_DIR_NAME_REPLACE_DELETED)
    dn[0] = FAT_DIR_NAME_DELETED;

  /* Special representations for `.' and `..'.  */
  if (!memcmp(dn, FAT_DIR_NAME_DOT, 11))
    return unamelen == 1 && username[0] == '.';

  if (!memcmp (dn, FAT_DIR_NAME_DOTDOT, 11))
    return unamelen == 2 && username[0] == '.' && username[1] == '.';

  if (unamelen > 12)
    return 0;

  do
    {
      /* First check if we have reached the extension without coming
	 across blanks. */
      if (dpos == 8 && !ext)
	{
	  if (username[upos] == '.')
	    {
	      upos++;
	      ext = 1;
	    }
	  else
	    break;
	}
      /* Second, skip blanks in base part.  */
      if (dn[dpos] == ' ')
	{
	  if (ext)
	    break;
	  while (dpos < 8 && dn[++dpos] == ' ');
	  if (username[upos] == '.')
	    upos++;
	  ext = 1;
	}
      else
	{
	  if (tolower(dn[dpos]) == tolower(username[upos]))
	    {
	      dpos++;
	      upos++;
	    }
	  else
	    break;
	}
    } while (upos < unamelen && dpos < 11);
  while (dpos < 11 && dn[dpos] == ' ')
    dpos++;
  return (upos == unamelen && dpos == 11);
}

/* Implement the diskfs_lookup callback from the diskfs library.  See
   <hurd/diskfs.h> for the interface specification.  */
error_t
diskfs_lookup_hard (struct node *dp, const char *name, enum lookup_type type,
		    struct node **npp, struct dirstat *ds, struct protid *cred)
{
  error_t err;
  ino_t inum;
  int namelen;
  int spec_dotdot;
  struct node *np = 0;
  int retry_dotdot = 0;
  vm_prot_t prot =
    (type == LOOKUP) ? VM_PROT_READ : (VM_PROT_READ | VM_PROT_WRITE);
  memory_object_t memobj;
  vm_address_t buf = 0;
  vm_size_t buflen = 0;
  int blockaddr;
  int idx, lastidx;
  int looped;

  if ((type == REMOVE) || (type == RENAME))
    assert (npp);

  if (npp)
    *npp = 0;

  spec_dotdot = type & SPEC_DOTDOT;
  type &= ~SPEC_DOTDOT;

  namelen = strlen (name);

  if (namelen > FAT_NAME_MAX)
    return ENAMETOOLONG;
  
 try_again:
  if (ds)
    {
      ds->type = LOOKUP;
      ds->mapbuf = 0;
      ds->mapextent = 0;
    }
  if (buf)
    {
      munmap ((caddr_t) buf, buflen);
      buf = 0;
    }
  if (ds && (type == CREATE || type == RENAME))
    ds->stat = LOOKING;

  /* Map in the directory contents. */
  memobj = diskfs_get_filemap (dp, prot);

  if (memobj == MACH_PORT_NULL)
    return errno;

  buf = 0;
  /* We allow extra space in case we have to do an EXTEND.  */
  buflen = round_page (dp->dn_stat.st_size + DIRBLKSIZ);
  err = vm_map (mach_task_self (),
                &buf, buflen, 0, 1, memobj, 0, 0, prot, prot, 0);
  mach_port_deallocate (mach_task_self (), memobj);
  if (err)
    return err;

  inum = 0;

  diskfs_set_node_atime (dp);

  /* Start the lookup at DP->dn->dir_idx.  */
  idx = dp->dn->dir_idx;
  if (idx << LOG2_DIRBLKSIZ > dp->dn_stat.st_size)
    idx = 0;                    /* just in case */
  blockaddr = buf + (idx << LOG2_DIRBLKSIZ);
  looped = (idx == 0);
  lastidx = idx;
  if (lastidx == 0)
    lastidx = dp->dn_stat.st_size >> LOG2_DIRBLKSIZ;

  while (!looped || idx < lastidx)
    {
      err = dirscanblock (blockaddr, dp, idx, name, namelen, type, ds, &inum);
      if (!err)
        {
          dp->dn->dir_idx = idx;
          break;
        }
      if (err != ENOENT)
        {
          munmap ((caddr_t) buf, buflen);
          return err;
        }

      blockaddr += DIRBLKSIZ;
      idx++;
      if (blockaddr - buf >= dp->dn_stat.st_size && !looped)
        {
          /* We've gotten to the end; start back at the beginning.  */
          looped = 1;
          blockaddr = buf;
          idx = 0;
        }
    }

  diskfs_set_node_atime (dp);
  if (diskfs_synchronous)
    diskfs_node_update (dp, 1);

  /* If err is set here, it's ENOENT, and we don't want to
     think about that as an error yet.  */
  err = 0;

  if (inum && npp)
    {
      if (namelen != 2 || name[0] != '.' || name[1] != '.')
        {
          if (inum == dp->cache_id)
            {
              np = dp;
              diskfs_nref (np);
            }
          else
            {
              err = diskfs_cached_lookup_in_dirbuf (inum, &np, buf);
              if (err)
                goto out;
            }
        }

      /* We are looking up "..".  */
      /* Check to see if this is the root of the filesystem.  */
      else if (dp == diskfs_root_node)
        {
          err = EAGAIN;
          goto out;
        }

      /* We can't just do diskfs_cached_lookup, because we would then
         deadlock.  So we do this.  Ick.  */
      else if (retry_dotdot)
        {
          /* Check to see that we got the same answer as last time.  */
          if (inum != retry_dotdot)
            {
              /* Drop what we *thought* was .. (but isn't any more) and
                 try *again*.  */
              diskfs_nput (np);
              pthread_mutex_unlock (&dp->lock);
              err = diskfs_cached_lookup_in_dirbuf (inum, &np, buf);
              pthread_mutex_lock (&dp->lock);
              if (err)
                goto out;
              retry_dotdot = inum;
              goto try_again;
            }
          /* Otherwise, we got it fine and np is already set properly.  */
        }
      else if (!spec_dotdot)
        {
          /* Lock them in the proper order, and then
             repeat the directory scan to see if this is still
             right.  */
          pthread_mutex_unlock (&dp->lock);
          err = diskfs_cached_lookup_in_dirbuf (inum, &np, buf);
          pthread_mutex_lock (&dp->lock);
          if (err)
            goto out;
          retry_dotdot = inum;
          goto try_again;
        }

      /* Here below are the spec dotdot cases.  */
      else if (type == RENAME || type == REMOVE)
        np = diskfs_cached_ifind (inum);

      else if (type == LOOKUP)
        {
          diskfs_nput (dp);
          err = diskfs_cached_lookup_in_dirbuf (inum, &np, buf);
          if (err)
            goto out;
        }
      else
        assert (0);
    }

  if ((type == CREATE || type == RENAME) && !inum && ds && ds->stat == LOOKING)
    {
      /* We didn't find any room, so mark ds to extend the dir.  */
      ds->type = CREATE;
      ds->stat = EXTEND;
      ds->idx = dp->dn_stat.st_size >> LOG2_DIRBLKSIZ;
    }

  /* Return to the user; if we can't, release the reference
     (and lock) we acquired above.  */
 out:
  /* Deallocate or save the mapping.  */
  if ((err && err != ENOENT)
      || !ds
      || ds->type == LOOKUP)
    {
      munmap ((caddr_t) buf, buflen);
      if (ds)
        ds->type = LOOKUP;      /* Set to be ignored by drop_dirstat.  */
    }
  else
    {
      ds->mapbuf = buf;
      ds->mapextent = buflen;
    }

  if (np)
    {
      assert (npp);
      if (err)
        {
          if (!spec_dotdot)
            {
              /* Normal case.  */
              if (np == dp)
                diskfs_nrele (np);
              else
                diskfs_nput (np);
            }
          else if (type == RENAME || type == REMOVE)
            /* We just did diskfs_cached_ifind to get np; that allocates
               no new references, so we don't have anything to do.  */
            ;
          else if (type == LOOKUP)
            /* We did diskfs_cached_lookup.  */
            diskfs_nput (np);
        }
      else
        *npp = np;
    }

  return err ? : inum ? 0 : ENOENT;
}

/* Scan block at address BLKADDR (of node DP; block index IDX), for
   name NAME of length NAMELEN.  Args TYPE, DS are as for
   diskfs_lookup.  If found, set *INUM to the inode number, else
   return ENOENT.  */
static error_t
dirscanblock (vm_address_t blockaddr, struct node *dp, int idx,
              const char *name, int namelen, enum lookup_type type,
              struct dirstat *ds, ino_t *inum)
{
  int nfree = 0;
  int needed = 0;
  vm_address_t currentoff, prevoff = 0;
  struct dirrect *entry = 0;
  size_t nbytes = 0;
  int looking = 0;
  int countcopies = 0;
  int consider_compress = 0;
  inode_t inode;
  vi_key_t entry_key = vi_zero_key;

  /* FAT lacks the "." and ".." directory record in the root directory,
     so we emulate them here.  */
  if (idx == 0 && dp == diskfs_root_node
      && (fatnamematch (FAT_DIR_NAME_DOT, name, namelen)
	  || fatnamematch (FAT_DIR_NAME_DOTDOT, name, namelen)))
    {
      entry_key.dir_inode = diskfs_root_node->cache_id;
      currentoff = blockaddr;
    }
  else
    {
      if (ds && (ds->stat == LOOKING
		 || ds->stat == COMPRESS))
	{
	  looking = 1;
	  countcopies = 1;
	  needed = FAT_DIR_RECORDS (namelen);
	}
      
      for (currentoff = blockaddr, prevoff = 0;
	   currentoff < blockaddr + DIRBLKSIZ;
	   prevoff = currentoff, currentoff += FAT_DIR_REC_LEN)
	{
	  entry = (struct dirrect *)currentoff;
	  
	  if (looking || countcopies)
	    {
	      int thisfree;
	      
	      /* Count how much free space this entry has in it.  */
	      if ((char) entry->name[0] == FAT_DIR_NAME_LAST ||
		  (char) entry->name[0] == FAT_DIR_NAME_DELETED)
		thisfree = FAT_DIR_REC_LEN;
	      else
		thisfree = 0;
	      
	      /* If this isn't at the front of the block, then it will
		 have to be copied if we do a compression; count the
		 number of bytes there too.  */
	      if (countcopies && currentoff != blockaddr)
		nbytes += FAT_DIR_REC_LEN;
	      
	      if (ds->stat == COMPRESS && nbytes > ds->nbytes)
		/* The previously found compress is better than this
		   one, so don't bother counting any more.  */
		countcopies = 0;
	      
	      if (thisfree >= needed)
		{
		  ds->type = CREATE;
		  ds->stat = TAKE;
		  ds->entry = entry;
		  ds->idx = idx;
		  looking = countcopies = 0;
		}
	      else
		{
		  nfree += thisfree;
		  if (nfree >= needed)
		    consider_compress = 1;
		}
	    }
	  
	  if (entry->attribute & FAT_DIR_ATTR_LABEL)
	    /* Either the volume label in root dir or a long filename
	       component.  */
	    continue;
	  
	  if (fatnamematch ((const char *) entry->name, name, namelen))
	    break;
	}

      if (consider_compress
	  && ((enum slot_status) ds->type == LOOKING
	      || ((enum slot_status) ds->type == COMPRESS &&
                  ds->nbytes > nbytes)))
	{
	  ds->type = CREATE;
	  ds->stat = COMPRESS;
	  ds->entry = (struct dirrect *) blockaddr;
	  ds->idx = idx;
	  ds->nbytes = nbytes;
	}
    }
  
  if (currentoff >= blockaddr + DIRBLKSIZ)
    {
      /* The name is not in this block.  */

      return ENOENT;
    }

  /* We have found the required name.  */

  if (ds && type == CREATE)
    ds->type = LOOKUP;          /* It's invalid now.  */
  else if (ds && (type == REMOVE || type == RENAME))
    {
      ds->type = type;
      ds->stat = HERE_TIS;
      ds->entry = entry;
      ds->idx = idx;
      ds->preventry = (struct dirrect *) prevoff;
    }

  if (entry_key.dir_inode)
    {
      /* The required name is "." or ".." in the root dir.  */
      *inum = entry_key.dir_inode;
    }
  else if ((entry->attribute & FAT_DIR_ATTR_DIR)
	   && !memcmp (entry->name, FAT_DIR_NAME_DOT, 11))
    {
      /* "." and ".." have to be treated special. We don't want their
	 directory records, but the records of the directories they
	 point to.  */
      
      *inum = dp->cache_id;
    }
  else if ((entry->attribute & FAT_DIR_ATTR_DIR)
	   && !memcmp (entry->name, FAT_DIR_NAME_DOTDOT, 11))
    {
      if (entry->first_cluster_low[0] == 0
	  && entry->first_cluster_low[1] == 0
	  && entry->first_cluster_high[0] == 0
	  && entry->first_cluster_high[1] == 0)
	{
	  *inum = diskfs_root_node->cache_id;
	}
      else
	{
	  struct vi_key vk = vi_key (dp->dn->inode);
	  *inum = vk.dir_inode;
	}
    }
  else
    {
      entry_key.dir_inode = dp->cache_id;
      entry_key.dir_offset = (currentoff - blockaddr) + (idx << LOG2_DIRBLKSIZ);
      return vi_rlookup(entry_key, inum, &inode, 1);
    }
  return 0;
}

/* Following a lookup call for CREATE, this adds a node to a
   directory.  DP is the directory to be modified; NAME is the name to
   be entered; NP is the node being linked in; DS is the cached
   information returned by lookup; CRED describes the user making the
   call.  This call may only be made if the directory has been held
   locked continuously since the preceding lookup call, and only if
   that call returned ENOENT.  */
error_t
diskfs_direnter_hard (struct node *dp, const char *name, struct node *np,
                      struct dirstat *ds, struct protid *cred)
{
  struct dirrect *new;
  int namelen = strlen (name);
  int needed = FAT_DIR_RECORDS (namelen);
  error_t err;
  loff_t oldsize = 0;

  assert (ds->type == CREATE);

  assert (!diskfs_readonly);

  dp->dn_set_mtime = 1;

  /* Select a location for the new directory entry.  Each branch of
     this switch is responsible for setting NEW to point to the
     on-disk directory entry being written.  */

  switch (ds->stat)
    {
    case TAKE:
      /* We are supposed to consume this slot.  */
      assert ((char)ds->entry->name[0] == FAT_DIR_NAME_LAST
	      || (char)ds->entry->name[0] == FAT_DIR_NAME_DELETED);

      new = ds->entry;
      break;

    case EXTEND:
      /* Extend the file.  */
      assert (needed <= bytes_per_cluster);

      oldsize = dp->dn_stat.st_size;
      while (oldsize + bytes_per_cluster > dp->allocsize)
        {
          err = diskfs_grow (dp, oldsize + bytes_per_cluster, cred);
          if (err)
            {
              munmap ((caddr_t) ds->mapbuf, ds->mapextent);
              return err;
            }
	  memset ((caddr_t) ds->mapbuf + oldsize, 0, bytes_per_cluster);
	}

      new = (struct dirrect *) ((char *) ds->mapbuf + oldsize);

      dp->dn_stat.st_size = oldsize + bytes_per_cluster;
      dp->dn_set_ctime = 1;

      break;

    case SHRINK:
    case COMPRESS:
    default:
      assert(0);

      /* COMPRESS will be used later, with long filenames, but shrink
	 does not make sense on fat, as all entries have fixed
	 size.  */
    }

  /* NEW points to the directory entry being written.  Now fill in the
     data.  */

  memcpy (new->name, "           ", 11);
  memcpy (new->name, name, namelen % 11); /* XXX */

  write_word (new->first_cluster_low, np->dn->start_cluster & 0xffff);
  write_word (new->first_cluster_high, np->dn->start_cluster >> 16);
  write_dword (new->file_size, np->dn_stat.st_size);
  
  if (!(name[0] == '.' && (name[1] == '\0' 
			   || (name[1] == '.'  && name[2] =='\0'))))
    {
      vi_key_t entry_key;
      
      entry_key.dir_inode = dp->cache_id;
      entry_key.dir_offset = ((int) ds->entry) - ((int) ds->mapbuf);
      
      /* Set the key for this inode now because it wasn't know when
	 the inode was initialized.  */
      vi_change (vi_lookup (np->cache_id), entry_key);
      
      if (np->dn_stat.st_mode & S_IFDIR)
	new->attribute = FAT_DIR_ATTR_DIR;
    }
  else
    new->attribute = FAT_DIR_ATTR_DIR;

  /* Mark the directory inode has having been written.  */
  dp->dn_set_mtime = 1;

  munmap ((caddr_t) ds->mapbuf, ds->mapextent);

  diskfs_file_update (dp, 1);

  return 0;
}

/* Following a lookup call for REMOVE, this removes the link from the
   directory.  DP is the directory being changed and DS is the cached
   information returned from lookup.  This call is only valid if the
   directory has been locked continuously since the call to lookup, and
   only if that call succeeded.  */
error_t
diskfs_dirremove_hard (struct node *dp, struct dirstat *ds)
{
  assert (ds->type == REMOVE);
  assert (ds->stat == HERE_TIS);

  assert (!diskfs_readonly);

  dp->dn_set_mtime = 1;

  ds->entry->name[0] = FAT_DIR_NAME_DELETED;

  /* XXX Do something with dirrect? inode?  */

  dp->dn_set_mtime = 1;

  munmap ((caddr_t) ds->mapbuf, ds->mapextent);

  diskfs_file_update (dp, 1);

  return 0;
}

/* Following a lookup call for RENAME, this changes the inode number
   on a directory entry.  DP is the directory being changed; NP is the
   new node being linked in; DP is the cached information returned by
   lookup.  This call is only valid if the directory has been locked
   continuously since the call to lookup, and only if that call
   succeeded.  */
error_t
diskfs_dirrewrite_hard (struct node *dp, struct node *np, struct dirstat *ds)
{
  error_t err;
  vi_key_t entry_key;
  mach_port_t control = MACH_PORT_NULL;
  struct node *oldnp;
  ino_t inode;
  inode_t vinode;
  
  /*  We need the inode and vinode of the old node.  */
  entry_key.dir_inode = dp->cache_id;
  entry_key.dir_offset = ((int) ds->entry) - ((int) ds->mapbuf);
  err = vi_rlookup (entry_key, &inode, &vinode, 0);
  assert (err != EINVAL);
  if (err)
    return err;

  /*  Lookup the node, we already have a reference.  */
  oldnp = diskfs_cached_ifind (inode);

  assert (ds->type == RENAME);
  assert (ds->stat == HERE_TIS);

  assert (!diskfs_readonly);

  /*  The link count must be 0 so the file will be removed and
      the node will be dropped.  */
  oldnp->dn_stat.st_nlink--;
  assert (!oldnp->dn_stat.st_nlink);
  
  /* Close the file, free the referenced held by clients.  */
  fshelp_fetch_control (&oldnp->transbox, &control);
  
  if (control)
    {
      fsys_goaway (control, FSYS_GOAWAY_UNLINK);
      mach_port_deallocate (mach_task_self (), control);
    }
  
  /*  Put the new key in the vinode.  */
  vi_change (vi_lookup (np->cache_id), entry_key);
   
  munmap ((caddr_t) ds->mapbuf, ds->mapextent);

  dp->dn_set_mtime = 1;
  diskfs_file_update (dp, 1);

  return 0;
}

/* Tell if DP is an empty directory (has only "." and ".." entries).
   This routine must be called from inside a catch_exception ().  */
int
diskfs_dirempty (struct node *dp, struct protid *cred)
{
  error_t err;
  vm_address_t buf = 0, curoff;
  struct dirrect *entry;
  int hit = 0;                  /* Found something in the directory.  */
  memory_object_t memobj = diskfs_get_filemap (dp, VM_PROT_READ);

  if (memobj == MACH_PORT_NULL)
    /* XXX should reflect error properly.  */
    return 0;

  err = vm_map (mach_task_self (), &buf, dp->dn_stat.st_size, 0,
                1, memobj, 0, 0, VM_PROT_READ, VM_PROT_READ, 0);
  mach_port_deallocate (mach_task_self (), memobj);
  assert (!err);

  diskfs_set_node_atime (dp);

  for (curoff = buf;
       !hit && curoff < buf + dp->dn_stat.st_size;
       curoff += FAT_DIR_REC_LEN)
    {
      entry = (struct dirrect *) curoff;

      if (entry->name[0] == FAT_DIR_NAME_LAST)
	break;
      if ((char) entry->name[0] != FAT_DIR_NAME_DELETED
	  && memcmp (entry->name, FAT_DIR_NAME_DOT, 11)
	  && memcmp (entry->name, FAT_DIR_NAME_DOTDOT, 11))
	hit = 1;
    }
  
  diskfs_set_node_atime (dp);
  if (diskfs_synchronous)
    diskfs_node_update (dp, 1);

  munmap ((caddr_t) buf, dp->dn_stat.st_size);

  return !hit;
}

/* Make DS an invalid dirstat.  */
error_t
diskfs_drop_dirstat (struct node *dp, struct dirstat *ds)
{
  if (ds->type != LOOKUP)
    {
      assert (ds->mapbuf);
      munmap ((caddr_t) ds->mapbuf, ds->mapextent);
      ds->type = LOOKUP;
    }
  return 0;
}


/* Implement the diskfs_get_directs callback as described in
   <hurd/diskfs.h>.  */
error_t
diskfs_get_directs (struct node *dp,
		    int entry,
		    int nentries,
		    char **data,
		    u_int *datacnt,
		    vm_size_t bufsiz,
		    int *amt)
{
  volatile vm_size_t allocsize;
  struct dirrect *ep;
  struct dirent *userp;
  int i;
  char *datap;
  volatile int ouralloc = 0;
  error_t err;
  vm_prot_t prot = VM_PROT_READ;
  memory_object_t memobj;
  vm_address_t buf = 0, bufp;
  vm_size_t buflen = 0;

  /* Allocate some space to hold the returned data.  */
  allocsize = bufsiz ? round_page (bufsiz) : vm_page_size * 4;
  if (allocsize > *datacnt)
    {
      *data = mmap (0, allocsize, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
      ouralloc = 1;
    }

  /* Map in the directory contents.  */
  memobj = diskfs_get_filemap (dp, prot);

  if (memobj == MACH_PORT_NULL)
    return errno;

  /* We allow extra space in case we have to do an EXTEND.  */
  buflen = round_page (dp->dn_stat.st_size);
  err = vm_map (mach_task_self (),
                &buf, buflen, 0, 1, memobj, 0, 0, prot, prot, 0);
  mach_port_deallocate (mach_task_self (), memobj);
  if (err)
    return err;

  bufp = buf;
  for (i = 0; i < entry; i ++)
    {
      /* The root directory in FAT file systems doesn't contain
	 entries for DOT and DOTDOT, they are special cased below.  */
      if (dp == diskfs_root_node && i < 2)
	continue;

      ep = (struct dirrect *) bufp;

      if (bufp >= buf + buflen || (char)ep->name[0] == FAT_DIR_NAME_LAST)
	{
	  /* Not that many entries in the directory; return nothing.  */
	  if (allocsize > *datacnt)
	    munmap (data, allocsize);
	  munmap ((caddr_t) buf, buflen);
	  *datacnt = 0;
	  *amt = 0;
	  return 0;
	}

      /* Ignore and skip deleted and label entries (catches also long
	 filenames).  */
      if ((char)ep->name[0] == FAT_DIR_NAME_DELETED
	  || (ep->attribute & FAT_DIR_ATTR_LABEL))
	i--;
      bufp = bufp + FAT_DIR_REC_LEN;
    }

  /* Now copy entries one at a time.  */
  i = 0;
  datap = *data;
  while (((nentries == -1) || (i < nentries))
	 && (!bufsiz || datap - *data < bufsiz)
	 && bufp < buf + buflen)
    {
      char name[13];
      size_t namlen, reclen;
      struct dirrect dot = { FAT_DIR_NAME_DOT, FAT_DIR_ATTR_DIR };
      struct dirrect dotdot = { FAT_DIR_NAME_DOTDOT, FAT_DIR_ATTR_DIR };

      /* The root directory in FAT file systems doesn't contain
	 entries for DOT and DOTDOT, they are special cased below.  */
      if (dp == diskfs_root_node && (i + entry == 0))
        ep = &dot;
      else if (dp == diskfs_root_node && (i + entry == 1))
        ep = &dotdot;
      else
	ep = (struct dirrect *) bufp;

      if ((char)ep->name[0] == FAT_DIR_NAME_LAST)
	{
	  /* Last entry.  */
	  bufp = buf + buflen;
	  continue;
	}

      if ((char)ep->name[0] == FAT_DIR_NAME_DELETED || (ep->attribute & FAT_DIR_ATTR_LABEL))
	{
	  bufp = bufp + FAT_DIR_REC_LEN;
  	  continue;
	}

      /* See if there's room to hold this one.  */
      
      fat_to_unix_filename ((const char *) ep->name, name);
      namlen = strlen(name);

      /* Perhaps downcase it?  */

      reclen = sizeof (struct dirent) + namlen;
      reclen = (reclen + 3) & ~3;

      /* Expand buffer if necessary.  */
      if (datap - *data + reclen > allocsize)
	{
	  vm_address_t newdata;

	  vm_allocate (mach_task_self (), &newdata,
		       (ouralloc
			? (allocsize *= 2)
			: (allocsize = vm_page_size * 2)), 1);
	  memcpy ((void *) newdata, (void *) *data, datap - *data);

	  if (ouralloc)
	    munmap (*data, allocsize / 2);

 	  datap = (char *) newdata + (datap - *data);
	  *data = (char *) newdata;
	  ouralloc = 1;
	}

      userp = (struct dirent *) datap;

      /* Fill in entry.  */
      {
        ino_t inode;
	inode_t v_inode;
	vi_key_t entry_key;

	entry_key.dir_inode = dp->cache_id;
	entry_key.dir_offset = bufp - buf;

	vi_rlookup (entry_key, &inode, &v_inode, 1);
	userp->d_fileno = inode;
      }
      userp->d_type = DT_UNKNOWN;
      userp->d_reclen = reclen;
      userp->d_namlen = namlen;
      memcpy (userp->d_name, name, namlen);
      userp->d_name[namlen] = '\0';

      /* And move along.  */
      datap = datap + reclen;
      if (!(dp == diskfs_root_node && i + entry < 2))
	bufp = bufp + FAT_DIR_REC_LEN;
      i++;
    }

  /* If we didn't use all the pages of a buffer we allocated, free
     the excess.  */
  if (ouralloc
      && round_page (datap - *data) < round_page (allocsize))
    munmap ((caddr_t) round_page (datap),
	    round_page (allocsize) - round_page (datap - *data));

  munmap ((caddr_t) buf, buflen);

  /* Return.  */
  *amt = i;
  *datacnt = datap - *data;
  return 0;
}