summaryrefslogtreecommitdiffstats
path: root/fs/umsdos/dir.c
blob: 52a72367e9aa242e10618eb8ac371604e701e499 (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
/*
 *  linux/fs/umsdos/dir.c
 *
 *  Written 1993 by Jacques Gelinas
 *	Inspired from linux/fs/msdos/... : Werner Almesberger
 *
 *  Extended MS-DOS directory handling functions
 */

#include <linux/sched.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/msdos_fs.h>
#include <linux/errno.h>
#include <linux/stat.h>
#include <linux/limits.h>
#include <linux/umsdos_fs.h>
#include <linux/malloc.h>

#include <asm/uaccess.h>

#define PRINTK(x)
#define Printk(x) printk x

#define UMSDOS_SPECIAL_DIRFPOS	3
extern struct inode *pseudo_root;


/* P.T.Waltenberg
   I've retained this to facilitate the lookup of some of the hard-wired files/directories UMSDOS
   uses. It's easier to do once than hack all the other instances. Probably safer as well
*/   

int compat_umsdos_real_lookup (struct inode *dir,const char *name,int len, struct inode **inode)
{
  int rv;
  struct dentry *dentry;

  dentry = creat_dentry (name, len, NULL);
  rv = umsdos_real_lookup(dir,dentry);
  if (inode) *inode = dentry->d_inode;
  kill_dentry (dentry);
  
  return rv;
}    


int compat_msdos_create(struct inode *dir,const char *name,int len, int mode, struct inode **inode)
{
  int rv;
  struct dentry *dentry;

  dentry = creat_dentry (name, len, NULL);
  rv = msdos_create(dir,dentry,mode);
  if(inode != NULL) *inode = dentry->d_inode;
  
  return rv;
}    


/*
	So  grep *  doesn't complain in the presence of directories.
*/
int UMSDOS_dir_read(struct file *filp,
		    char *buff,
		    size_t size,
		    loff_t * count)
{
	return -EISDIR;
}

struct UMSDOS_DIR_ONCE {
	void *dirbuf;
	filldir_t filldir;
	int count;
	int stop;
};

/*
	Record a single entry the first call.
	Return -EINVAL the next one.
	NOTE: filldir DOES NOT use a dentry
*/
static int umsdos_dir_once(
	void * buf,
	const char *name,
	int len,
	off_t offset,
	ino_t ino)
{
  int ret = -EINVAL;
  struct UMSDOS_DIR_ONCE *d = (struct UMSDOS_DIR_ONCE *)buf;
  if (d->count == 0){
    PRINTK ((KERN_DEBUG "dir_once :%.*s: offset %Ld\n", dentry->d_len, dentry->d_name, offset));
    ret = d->filldir (d->dirbuf,name,len,offset,ino);
    d->stop = ret < 0;
    d->count = 1;
  }
  return ret;
}

/*
  Read count directory entries from directory filp
  Return a negative value from linux/errno.h.
  Return > 0 if success (The amount of byte written by filldir).
  
  This function is used by the normal readdir VFS entry point and by
  some function who try to find out info on a file from a pure MSDOS
  inode. See umsdos_locate_ancestor() below.
*/
static int umsdos_readdir_x(
			    struct inode *dir,		/* Point to a description of the super block */
			    struct file *filp,		/* Point to a directory which is read */
			    void *dirbuf,			/* Will hold count directory entry */
			    /* but filled by the filldir function */
			    int internal_read,		/* Called for internal purpose */
			    struct umsdos_dirent *u_entry,	/* Optional umsdos entry */
			    int follow_hlink,
			    filldir_t filldir)
{
  int ret = 0;
  struct inode *root_inode;
  
  root_inode = iget(dir->i_sb,UMSDOS_ROOT_INO);
  umsdos_startlookup(dir);	
  if (filp->f_pos == UMSDOS_SPECIAL_DIRFPOS
      && pseudo_root
      && dir == pseudo_root
      && !internal_read){
      
      Printk (("umsdos_readdir_x: what UMSDOS_SPECIAL_DIRFPOS /mn/?\n"));
    /*
      We don't need to simulate this pseudo directory
      when umsdos_readdir_x is called for internal operation
      of umsdos. This is why dirent_in_fs is tested
    */
    /* #Specification: pseudo root / directory /DOS
       When umsdos operates in pseudo root mode (C:\linux is the
       linux root), it simulate a directory /DOS which points to
       the real root of the file system.
    */
    if (filldir (dirbuf,"DOS",3,UMSDOS_SPECIAL_DIRFPOS
		 ,UMSDOS_ROOT_INO) == 0){
      filp->f_pos++;
    }
  }else if (filp->f_pos < 2
	    || (dir != root_inode && filp->f_pos == 32)){
    /* #Specification: readdir / . and ..
       The msdos filesystem manage the . and .. entry properly
       so the EMD file won't hold any info about it.
       
       In readdir, we assume that for the root directory
       the read position will be 0 for ".", 1 for "..". For
       a non root directory, the read position will be 0 for "."
       and 32 for "..".
    */
    /*
      This is a trick used by the msdos file system (fs/msdos/dir.c)
      to manage . and .. for the root directory of a file system.
      Since there is no such entry in the root, fs/msdos/dir.c
      use the following:
      
      if f_pos == 0, return ".".
      if f_pos == 1, return "..".
      
      So let msdos handle it
      
      Since umsdos entries are much larger, we share the same f_pos.
      if f_pos is 0 or 1 or 32, we are clearly looking at . and
      ..
      
      As soon as we get f_pos == 2 or f_pos == 64, then back to
      0, but this time we are reading the EMD file.
      
      Well, not so true. The problem, is that UMSDOS_REC_SIZE is
      also 64, so as soon as we read the first record in the
      EMD, we are back at offset 64. So we set the offset
      to UMSDOS_SPECIAL_DIRFPOS(3) as soon as we have read the
      .. entry from msdos.
      
      Now (linux 1.3), umsdos_readdir can read more than one
      entry even if we limit (umsdos_dir_once) to only one:
      It skips over hidden file. So we switch to
      UMSDOS_SPECIAL_DIRFPOS as soon as we have read successfully
      the .. entry.
    */
    int last_f_pos = filp->f_pos;	
    struct UMSDOS_DIR_ONCE bufk;

    Printk (("umsdos_readdir_x: . or .. /mn/?\n"));

    bufk.dirbuf = dirbuf;
    bufk.filldir = filldir;
    bufk.count = 0;

    ret = fat_readdir(filp,&bufk,umsdos_dir_once);
    if (last_f_pos > 0 && filp->f_pos > last_f_pos) filp->f_pos = UMSDOS_SPECIAL_DIRFPOS;
    if (u_entry != NULL) u_entry->flags = 0;
  }else{
    struct inode *emd_dir;
    Printk (("umsdos_readdir_x: normal file /mn/?\n"));
    emd_dir = umsdos_emd_dir_lookup(dir,0);
    if (emd_dir != NULL){
      off_t start_fpos = filp->f_pos;
      Printk (("umsdos_readdir_x: emd_dir->i_ino=%ld\n",emd_dir->i_ino));
      if (filp->f_pos <= UMSDOS_SPECIAL_DIRFPOS+1) filp->f_pos = 0;
      Printk (("f_pos %Ld i_size %ld\n",filp->f_pos,emd_dir->i_size));
      ret = 0;
      while (filp->f_pos < emd_dir->i_size){
	struct umsdos_dirent entry;
	off_t cur_f_pos = filp->f_pos;
	if (umsdos_emd_dir_readentry (emd_dir,filp,&entry)!=0){
	  ret = -EIO;
	  break;
	}else if (entry.name_len != 0){
	  /* #Specification: umsdos / readdir
	     umsdos_readdir() should fill a struct dirent with
	     an inode number. The cheap way to get it is to
	     do a lookup in the MSDOS directory for each
	     entry processed by the readdir() function.
	     This is not very efficient, but very simple. The
	     other way around is to maintain a copy of the inode
	     number in the EMD file. This is a problem because
	     this has to be maintained in sync using tricks.
	     Remember that MSDOS (the OS) does not update the
	     modification time (mtime) of a directory. There is
	     no easy way to tell that a directory was modified
	     during a DOS session and synchronise the EMD file.
	     
	     Suggestion welcome.
	     
	     So the easy way is used!
	  */
	  struct umsdos_info info;
	  struct inode *inode;
	  
	  int lret;
	  umsdos_parse (entry.name,entry.name_len,&info);
	  info.f_pos = cur_f_pos;
	  umsdos_manglename (&info);
	  /* FIXME, fake a dentry --> /mn/ fixed ? */
	  lret = compat_umsdos_real_lookup (dir,info.fake.fname,
					    info.fake.len,&inode);
	  Printk (("Cherche inode de %s lret %d flags %d\n"
		   ,info.fake.fname,lret,entry.flags));
	  if (lret == 0
	      && (entry.flags & UMSDOS_HLINK)
	      && follow_hlink){
	    struct inode *rinode;
	    lret = umsdos_hlink2inode (inode,&rinode);
	    inode = rinode;
	  }
	  if (lret == 0){
	    /* #Specification: pseudo root / reading real root
	       The pseudo root (/linux) is logically
	       erased from the real root. This mean that
	       ls /DOS, won't show "linux". This avoids
	       infinite recursion /DOS/linux/DOS/linux while
	       walking the file system.
	    */
	    if (inode != pseudo_root
		&& (internal_read
		    || !(entry.flags & UMSDOS_HIDDEN))){
	      if (filldir (dirbuf, 
			   entry.name,
			   entry.name_len,
			   cur_f_pos, 
			   inode->i_ino) < 0){
		filp->f_pos = cur_f_pos;
	      }
	      Printk (("Trouve ino %ld ",inode->i_ino));
	      if (u_entry != NULL) *u_entry = entry;
	      iput (inode);
	      break;
	    }
	    iput (inode);
	  }else{
	    /* #Specification: umsdos / readdir / not in MSDOS
	       During a readdir operation, if the file is not
	       in the MSDOS directory anymore, the entry is
	       removed from the EMD file silently.
	    */
	    Printk (("'Silently' removing EMD for file\n"));
	    ret = umsdos_writeentry (dir,emd_dir,&info,1);
	    if (ret != 0){
	      break;
	    }
	  }
	}
      }
      /*
	If the fillbuf has failed, f_pos is back to 0.
	To avoid getting back into the . and .. state
	(see comments at the beginning), we put back
	the special offset.
      */
      if (filp->f_pos == 0) filp->f_pos = start_fpos;
      iput(emd_dir);
    }
  }
  umsdos_endlookup(dir);	
  Printk (("read dir %p pos %Ld ret %d\n",dir,filp->f_pos,ret));
  return ret;
}


/*
  Read count directory entries from directory filp
  Return a negative value from linux/errno.h.
  Return 0 or positive if successful
*/
static int UMSDOS_readdir(
			  struct file *filp,		/* Point to a directory which is read */
			  void *dirbuf,			/* Will hold directory entries  */
			  filldir_t filldir)
{
  struct inode *dir = filp->f_dentry->d_inode;
  int ret = 0;
  int count = 0;
  struct UMSDOS_DIR_ONCE bufk;
  bufk.dirbuf = dirbuf;
  bufk.filldir = filldir;
  bufk.stop = 0;

  Printk (("UMSDOS_readdir in\n"));
  while (ret == 0 && bufk.stop == 0){
    struct umsdos_dirent entry;
    bufk.count = 0;
    Printk (("UMSDOS_readdir: calling _x (%p,%p,%p,%d,%p,%d,%p)\n",dir,filp,&bufk,0,&entry,1,umsdos_dir_once));
    ret = umsdos_readdir_x (dir,filp,&bufk,0,&entry,1,umsdos_dir_once);
    if (bufk.count == 0) break;
    count += bufk.count;
  }
  Printk (("UMSDOS_readdir out %d count %d pos %Ld\n",ret,count
	   ,filp->f_pos));
  return count?:ret;
}

/*
  Complete the inode content with info from the EMD file
*/
void umsdos_lookup_patch (
			  struct inode *dir,
			  struct inode *inode,
			  struct umsdos_dirent *entry,
			  off_t  emd_pos)
{
  /*
    This function modify the state of a dir inode. It decides
    if the dir is a umsdos dir or a dos dir. This is done
    deeper in umsdos_patch_inode() called at the end of this function.
    
    umsdos_patch_inode() may block because it is doing disk access.
    At the same time, another process may get here to initialise
    the same dir inode. There is 3 cases.
    
    1-The inode is already initialised. We do nothing.
    2-The inode is not initialised. We lock access and do it.
    3-Like 2 but another process has lock the inode, so we try
    to lock it and right after check if initialisation is still
    needed.
    
    
    Thanks to the mem option of the kernel command line, it was
    possible to consistently reproduce this problem by limiting
    my mem to 4 meg and running X.
  */
  /*
    Do this only if the inode is freshly read, because we will lose
    the current (updated) content.
  */
  /*
    A lookup of a mount point directory yield the inode into
    the other fs, so we don't care about initialising it. iget()
    does this automatically.
  */
  if (inode->i_sb == dir->i_sb && !umsdos_isinit(inode)){
    if (S_ISDIR(inode->i_mode)) umsdos_lockcreate(inode);
    if (!umsdos_isinit(inode)){
      /* #Specification: umsdos / lookup / inode info
	 After successfully reading an inode from the MSDOS
	 filesystem, we use the EMD file to complete it.
	 We update the following field.
	 
	 uid, gid, atime, ctime, mtime, mode.
	 
	 We rely on MSDOS for mtime. If the file
	 was modified during an MSDOS session, at least
	 mtime will be meaningful. We do this only for regular
	 file.
	 
	 We don't rely on MSDOS for mtime for directory because
	 the MSDOS directory date is creation time (strange
	 MSDOS behavior) which fit nowhere in the three UNIX
	 time stamp.
      */
      if (S_ISREG(entry->mode)) entry->mtime = inode->i_mtime;
      inode->i_mode  = entry->mode;
      inode->i_rdev  = to_kdev_t(entry->rdev);
      inode->i_atime = entry->atime;
      inode->i_ctime = entry->ctime;
      inode->i_mtime = entry->mtime;
      inode->i_uid   = entry->uid;
      inode->i_gid   = entry->gid;
      /* #Specification: umsdos / conversion mode
	 The msdos fs can do some inline conversion
	 of the data of a file. It can translate
	 silently from MsDOS text file format to Unix
	 one (crlf -> lf) while reading, and the reverse
	 while writing. This is activated using the mount
	 option conv=....
	 
	 This is not useful for Linux file in promoted
	 directory. It can even be harmful. For this
	 reason, the binary (no conversion) mode is
	 always activated.
      */
      /* #Specification: umsdos / conversion mode / todo
	 A flag could be added to file and directories
	 forcing an automatic conversion mode (as
	 done with the msdos fs).
	 
	 This flag could be setup on a directory basis
	 (instead of file) and all file in it would
	 logically inherited. If the conversion mode
	 is active (conv=) then the i_binary flag would
	 be left untouched in those directories.
	 
	 It was proposed that the sticky bit was used
	 to set this. The problem is that new file would
	 be written incorrectly. The other problem is that
	 the sticky bit has a meaning for directories. So
	 another bit should be used (there is some space
	 in the EMD file for it) and a special utilities
	 would be used to assign the flag to a directory).
	 I don't think it is useful to assign this flag
	 on a single file.
      */
      
      MSDOS_I(inode)->i_binary = 1;
      /* #Specification: umsdos / i_nlink
	 The nlink field of an inode is maintain by the MSDOS file system
	 for directory and by UMSDOS for other file. The logic is that
	 MSDOS is already figuring out what to do for directories and
	 does nothing for other files. For MSDOS, there are no hard link
	 so all file carry nlink==1. UMSDOS use some info in the
	 EMD file to plug the correct value.
      */
      if (!S_ISDIR(entry->mode)){
	if (entry->nlink > 0){
	  inode->i_nlink = entry->nlink;
	}else{
	  printk (KERN_ERR "UMSDOS: lookup_patch entry->nlink < 1 ???\n");
	}
      }
      umsdos_patch_inode(inode,dir,emd_pos);
    }
    if (S_ISDIR(inode->i_mode)) umsdos_unlockcreate(inode);
    if (inode->u.umsdos_i.i_emd_owner==0) printk (KERN_WARNING "emd_owner still 0 ???\n");
  }
}

struct UMSDOS_DIRENT_K{
  off_t f_pos; /* will hold the offset of the entry in EMD */
  ino_t ino;
};

/*
  Just to record the offset of one entry.
*/
static int umsdos_filldir_k(
			    void * buf,
			    const char *name,
			    int len,
			    off_t offset,
			    ino_t ino)
{
  struct UMSDOS_DIRENT_K *d = (struct UMSDOS_DIRENT_K *)buf;
  d->f_pos = offset;
  d->ino = ino;
  return 0;
}

struct UMSDOS_DIR_SEARCH{
  struct umsdos_dirent *entry;
  int found;
  ino_t search_ino;
};

static int umsdos_dir_search (
			      void * buf,
			      const char *name,
			      int len,
			      off_t offset,
			      ino_t ino)
{
  int ret = 0;
  struct UMSDOS_DIR_SEARCH *d = (struct UMSDOS_DIR_SEARCH *)buf;
  if (d->search_ino == ino){
    d->found = 1;
    memcpy (d->entry->name,name,len);
    d->entry->name[len] = '\0';
    d->entry->name_len = len;
    ret = 1;	/* So fat_readdir will terminate */
  }
  return ret;
}


/*
  Locate entry of an inode in a directory.
  Return 0 or a negative error code.
  
  Normally, this function must succeed. It means a strange corruption
  in the file system if not.
*/
int umsdos_inode2entry (
			struct inode *dir,
			struct inode *inode,
			struct umsdos_dirent *entry)	/* Will hold the entry */
{
  int ret = -ENOENT;
  if (pseudo_root && inode == pseudo_root){
    /*
      Quick way to find the name.
      Also umsdos_readdir_x won't show /linux anyway
    */
    memcpy (entry->name,UMSDOS_PSDROOT_NAME,UMSDOS_PSDROOT_LEN+1);
    entry->name_len = UMSDOS_PSDROOT_LEN;
    ret = 0;
  }else{
    struct inode *emddir = umsdos_emd_dir_lookup(dir,0);
    iput (emddir);
    if (emddir == NULL){
      /* This is a DOS directory */
      struct UMSDOS_DIR_SEARCH bufk;
      struct file filp;
      
      fill_new_filp (&filp, NULL);
            
      Printk ((KERN_ERR "umsdos_inode2entry emddir==NULL: WARNING: Known filp problem. segfaulting :) /mn/\n"));
      filp.f_reada = 1;
      filp.f_pos = 0;
      bufk.entry = entry;
      bufk.search_ino = inode->i_ino;
      fat_readdir (&filp,&bufk,umsdos_dir_search);
      if (bufk.found){
	ret = 0;
	inode->u.umsdos_i.i_dir_owner = dir->i_ino;
	inode->u.umsdos_i.i_emd_owner = 0;
	umsdos_setup_dir_inode(inode);
      }
    }else{
      /* skip . and .. see umsdos_readdir_x() */
      struct file filp;
      fill_new_filp (&filp, NULL);
      
      filp.f_reada = 1;
      filp.f_pos = UMSDOS_SPECIAL_DIRFPOS;
      Printk ((KERN_ERR "umsdos_inode2entry skip./..: WARNING: Known filp problem. segfaulting :) /mn/\n"));
      while (1){
	struct UMSDOS_DIRENT_K bufk;
	if (umsdos_readdir_x(dir,&filp,&bufk
			     ,1,entry,0,umsdos_filldir_k) < 0){
	  printk ("UMSDOS: can't locate inode %ld in EMD file???\n"
		  ,inode->i_ino);
	  break;
	}else if (bufk.ino == inode->i_ino){
	  ret = 0;
	  umsdos_lookup_patch (dir,inode,entry,bufk.f_pos);
	  break;
	}
      }
    }
  }
  return ret;
}


/*
  Locate the parent of a directory and the info on that directory
  Return 0 or a negative error code.
*/
static int umsdos_locate_ancestor (
				   struct inode *dir,
				   struct inode **result,
				   struct umsdos_dirent *entry)
{
  int ret;
  
  umsdos_patch_inode (dir,NULL,0);
  /* FIXME */
  ret = compat_umsdos_real_lookup (dir,"..",2,result);
  Printk (("result %d %p ",ret,*result));
  if (ret == 0){
    struct inode *adir = *result;
    ret = umsdos_inode2entry (adir,dir,entry);
  }
  Printk (("\n"));
  return ret;
}
/*
  Build the path name of an inode (relative to the file system.
  This function is need to set (pseudo) hard link.
  
  It uses the same strategy as the standard getcwd().
*/
int umsdos_locate_path (
			struct inode *inode,
			char *path)
{
  int ret = 0;
  struct inode *dir = inode;
  struct inode *root_inode;
  char *bpath = (char*)kmalloc(PATH_MAX,GFP_KERNEL);
  root_inode = iget(inode->i_sb,UMSDOS_ROOT_INO);
  if (bpath == NULL){
    ret = -ENOMEM;
  }else{
    struct umsdos_dirent entry;
    char *ptbpath = bpath+PATH_MAX-1;
    *ptbpath = '\0';
    Printk (("locate_path mode %x ",inode->i_mode));
    if (!S_ISDIR(inode->i_mode)){
      ret = umsdos_get_dirowner (inode,&dir);
      Printk (("locate_path ret %d ",ret));
      if (ret == 0){
	ret = umsdos_inode2entry (dir,inode,&entry);
	if (ret == 0){
	  ptbpath -= entry.name_len;
	  memcpy (ptbpath,entry.name,entry.name_len);
	  Printk (("ptbpath :%.*s: ",entry.name_len,ptbpath));
	}
      }
    }else{
      dir->i_count++;
    }
    if (ret == 0){
      while (dir != root_inode){
	struct inode *adir;
	ret = umsdos_locate_ancestor (dir,&adir,&entry);
	iput (dir);
	dir = NULL;
	Printk (("ancestor %d ",ret));
	if (ret == 0){
	  *--ptbpath = '/';
	  ptbpath -= entry.name_len;
	  memcpy (ptbpath,entry.name,entry.name_len);
	  dir = adir;
	  Printk (("ptbpath :%.*s: ",entry.name_len,ptbpath));
	}else{
	  break;
	}
      }
    }
    strcpy (path,ptbpath);
    kfree (bpath);
  }
  Printk (("\n"));
  iput (dir);
  return ret;
}

/*
	Return != 0 if an entry is the pseudo DOS entry in the pseudo root.
*/
int umsdos_is_pseudodos (
	struct inode *dir,
	struct dentry *dentry)
{
  /* #Specification: pseudo root / DOS hard coded
     The pseudo sub-directory DOS in the pseudo root is hard coded.
     The name is DOS. This is done this way to help standardised
     the umsdos layout. The idea is that from now on /DOS is
     a reserved path and nobody will think of using such a path
     for a package.
  */
  return pseudo_root
    && dir == pseudo_root
    && dentry->d_name.len == 3
    && dentry->d_name.name[0] == 'D' 
    && dentry->d_name.name[1] == 'O' 
    && dentry->d_name.name[2] == 'S';
}

/*
  Check if a file exist in the current directory.
  Return 0 if ok, negative error code if not (ex: -ENOENT).
*/
int umsdos_lookup_x (
			    struct inode *dir,
			    struct dentry *dentry,
			    int nopseudo)/* Don't care about pseudo root mode */
{
  int ret = -ENOENT;
  struct inode *root_inode;
  struct inode *pseudo_root_inode=NULL;
  int len = dentry->d_name.len;
  const char *name = dentry->d_name.name;
  root_inode = iget(dir->i_sb,UMSDOS_ROOT_INO);
  /*	pseudo_root_inode = iget( ... ) ? */
  dentry->d_inode = NULL;
  umsdos_startlookup(dir);	
  if (len == 1 && name[0] == '.'){
    dentry->d_inode = dir;
    dir->i_count++;
    ret = 0;
  }else if (len == 2 && name[0] == '.' && name[1] == '.'){
    if (pseudo_root && dir == pseudo_root_inode){
      /* #Specification: pseudo root / .. in real root
	 Whenever a lookup is those in the real root for
	 the directory .., and pseudo root is active, the
	 pseudo root is returned.
      */
      ret = 0;
      dentry->d_inode = pseudo_root;
      pseudo_root->i_count++;
    }else{
      /* #Specification: locating .. / strategy
	 We use the msdos filesystem to locate the parent directory.
	 But it is more complicated than that.
	 
	 We have to step back even further to
	 get the parent of the parent, so we can get the EMD
	 of the parent of the parent. Using the EMD file, we can
	 locate all the info on the parent, such a permissions
	 and owner.
      */
      
      ret = compat_umsdos_real_lookup (dir,"..",2,&dentry->d_inode);
      Printk (("ancestor ret %d dir %p *result %p ",ret,dir,dentry->d_inode));
      if (ret == 0
	  && dentry->d_inode != root_inode
	  && dentry->d_inode != pseudo_root){
	struct inode *aadir;
	struct umsdos_dirent entry;
	ret = umsdos_locate_ancestor (dentry->d_inode,&aadir,&entry);
	iput (aadir);
      }
    }
  }else if (umsdos_is_pseudodos(dir,dentry)){
    /* #Specification: pseudo root / lookup(DOS)
       A lookup of DOS in the pseudo root will always succeed
       and return the inode of the real root.
    */
    dentry->d_inode = root_inode;
    (dentry->d_inode)->i_count++;
    ret = 0;
  }else{
    struct umsdos_info info;
    ret = umsdos_parse (dentry->d_name.name,dentry->d_name.len,&info);
    if (ret == 0) ret = umsdos_findentry (dir,&info,0);
    Printk (("lookup %.*s pos %lu ret %d len %d ",info.fake.len,info.fake.fname,info.f_pos,ret
	     ,info.fake.len));
    if (ret == 0){
      /* #Specification: umsdos / lookup
	 A lookup for a file is done in two step. First, we locate
	 the file in the EMD file. If not present, we return
	 an error code (-ENOENT). If it is there, we repeat the
	 operation on the msdos file system. If this fails, it means
	 that the file system is not in sync with the emd file.
	 We silently remove this entry from the emd file,
	 and return ENOENT.
      */
      struct inode *inode;
      
      ret = compat_umsdos_real_lookup (dir,info.fake.fname,info.fake.len,&inode);

      Printk ((KERN_DEBUG "umsdos_lookup_x: compat_umsdos_real_lookup for %.*s returned %d with inode=%p\n", info.fake.len, info.fake.fname, ret, inode));

      if (inode == NULL){
	printk (KERN_WARNING "UMSDOS: Erase entry %.*s, out of sync with MsDOS\n"
		,info.fake.len, info.fake.fname);
	umsdos_delentry (dir,&info,S_ISDIR(info.entry.mode));
      }else{
        Printk ((KERN_DEBUG "umsdos_lookup_x /mn/ debug: ino=%li\n", inode->i_ino));

        /* we've found it. now get inode and put it in dentry. Is this ok /mn/ ? */
        dentry->d_inode = iget(dir->i_sb, inode->i_ino);
        
	umsdos_lookup_patch (dir,inode,&info.entry,info.f_pos);
	Printk (("lookup ino %ld flags %d\n",inode->i_ino
		 ,info.entry.flags));
	if (info.entry.flags & UMSDOS_HLINK){
	  Printk ((KERN_DEBUG "umsdos_lookup_x: here goes HLINK\n"));
	  ret = umsdos_hlink2inode (inode,&dentry->d_inode);
	}
	if (pseudo_root && dentry->d_inode == pseudo_root && !nopseudo){
	  /* #Specification: pseudo root / dir lookup
	     For the same reason as readdir, a lookup in /DOS for
	     the pseudo root directory (linux) will fail.
	  */
	  /*
	    This has to be allowed for resolving hard link
	    which are recorded independently of the pseudo-root
	    mode.
	  */
	  Printk ((KERN_ERR "umsdos_lookup_x: warning: untested /mn/ Pseudo_root thingy\n"));
	  iput (pseudo_root);
	  dentry->d_inode = NULL;
	  ret = -ENOENT;
	}
      }
    }
  }
  umsdos_endlookup(dir);	
  iput (dir);
  Printk ((KERN_DEBUG "umsdos_lookup_x: returning %d\n", ret));
  return ret;
}


/*
  Check if a file exist in the current directory.
  Return 0 if ok, negative error code if not (ex: -ENOENT).
  
  
*/
int UMSDOS_lookup (
		   struct inode *dir,
		   struct dentry *dentry)
{
  int ret;
  ret = umsdos_lookup_x(dir,dentry,0);
  
#if 1
  if (ret == -ENOENT) {
    Printk ((KERN_INFO "UMSDOS_lookup: converting -ENOENT to negative dentry !\n"));
    d_add (dentry, NULL);	/* create negative dentry if not found */
    ret = 0;
  }
#endif
  
  return ret;
}



/*
  Locate the inode pointed by a (pseudo) hard link
  Return 0 if ok, a negative error code if not.
*/
int umsdos_hlink2inode (struct inode *hlink, struct inode **result)
{
  struct inode *root_inode;
  int ret = -EIO;
  struct dentry *dentry_src, *dentry_dst;
  char *path;
  
#if 0  	/* FIXME: DELME */
  Printk (("FIXME: just test. hlink2inode returning -ENOENT\n /mn/\n"));
  return -ENOENT;	/* /mn/ FIXME just for test */
#endif
  
  path = (char*)kmalloc(PATH_MAX,GFP_KERNEL);

  root_inode = iget(hlink->i_sb,UMSDOS_ROOT_INO);
  *result = NULL;
  if (path == NULL){
    ret = -ENOMEM;
    iput (hlink);
  }else{
    struct file filp;
    loff_t offs = 0;

    fill_new_filp (&filp, NULL);
      

    dentry_src = creat_dentry ("hlink-mn", 8, hlink);

    memset (&filp, 0, sizeof (filp));

    filp.f_pos = 0;
    filp.f_reada = 1;
    filp.f_flags = O_RDONLY;
    filp.f_dentry = dentry_src;
    filp.f_op = &umsdos_file_operations;	/* /mn/ - we have to fill it with dummy values so we won't segfault */
    
    Printk (("hlink2inode "));
    if (umsdos_file_read_kmem (hlink, &filp, path, hlink->i_size, &offs) == hlink->i_size)
      {
	struct inode *dir;
	char *pt = path;
	dir = root_inode;
	path[hlink->i_size] = '\0';
/*	iput (hlink); / * FIXME! /mn/ */
	dir->i_count++;
	while (1)
	  {
	    char *start = pt;
	    int len;
	    while (*pt != '\0' && *pt != '/') pt++;
	    len = (int)(pt - start);
	    if (*pt == '/') *pt++ = '\0';
	    /* FIXME. /mn/ fixed ? */

            dentry_dst = creat_dentry (start, len, NULL);

	    if (dir->u.umsdos_i.i_emd_dir == 0){
	      /* This is a DOS directory */
		
	      Printk (("hlink2inode /mn/: doing umsdos_rlookup_x on %.*s\n", (int) dentry_dst->d_name.len, dentry_dst->d_name.name));
	      ret = umsdos_rlookup_x(dir,dentry_dst,1);
	    }else{
	      Printk (("hlink2inode /mn/: doing umsdos_lookup_x on %.*s\n", (int) dentry_dst->d_name.len, dentry_dst->d_name.name));
	      ret = umsdos_lookup_x(dir,dentry_dst,1);
	    }
	    Printk (("  returned %d\n", ret));
	    *result = dentry_dst->d_inode;	/* /mn/ ok ? */

	    Printk (("h2n lookup :%s: -> %d ",start,ret));
	    if (ret == 0 && *pt != '\0'){
	      dir = *result;
	    }else{
	      break;
	    }
	  }
      }else{
	  Printk (("umsdos_hlink2inode: all those iput's() frighten me /mn/. Whatabout dput() ? FIXME!\n"));
	  iput (hlink); /* FIXME */
      }
    Printk (("hlink2inode ret = %d %p -> %p\n", ret, hlink, *result));
    kfree (path);
  }
  return ret;
}


static struct file_operations umsdos_dir_operations = {
	NULL,				/* lseek - default */
	UMSDOS_dir_read,		/* read */
	NULL,				/* write - bad */
	UMSDOS_readdir,			/* readdir */
	NULL,				/* poll - default */
	UMSDOS_ioctl_dir,		/* ioctl - default */
	NULL,				/* mmap */
	NULL,				/* no special open code */
	NULL,				/* no special release code */
	NULL				/* fsync */	/* in original NULL. changed to file_fsync. FIXME? /mn/ */
};

struct inode_operations umsdos_dir_inode_operations = {
	&umsdos_dir_operations,	/* default directory file-ops */
	UMSDOS_create,		/* create */
	UMSDOS_lookup,		/* lookup */
	UMSDOS_link,		/* link */
	UMSDOS_unlink,		/* unlink */
	UMSDOS_symlink,		/* symlink */
	UMSDOS_mkdir,		/* mkdir */
	UMSDOS_rmdir,		/* rmdir */
	UMSDOS_mknod,		/* mknod */
	UMSDOS_rename,		/* rename */
	NULL,			/* readlink */
	NULL,                   /* followlink */
	generic_readpage,	/* readpage */	/* in original NULL. changed to generic_readpage. FIXME? /mn/ */
	NULL,			/* writepage */
	fat_bmap,		/* bmap */	/* in original NULL. changed to fat_bmap. FIXME? /mn/ */
	NULL,			/* truncate */
	NULL,			/* permission */
	NULL,			/* smap */
	NULL,			/* updatepage */
	NULL,			/* revalidate */
	
};