c3f57d7723bc2757030bdd3ef993fc47e9fd37b7
[yaffs/.git] / yaffs_fs.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  * yaffs_fs.c
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * This is the file system front-end to YAFFS that hooks it up to
15  * the VFS.
16  *
17  * Special notes: 
18  * >> sb->u.generic_sbp points to the yaffs_Device associated with this superblock
19  * >> inode->u.generic_ip points to the associated yaffs_Object.
20  */
21
22
23 #include <linux/config.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/version.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/fs.h>
31 #include <linux/proc_fs.h>
32 #include <linux/pagemap.h>
33 #include <linux/mtd/mtd.h>
34 #include <linux/interrupt.h>
35 #include <linux/string.h>
36 #include <linux/locks.h>
37
38 #include <asm/uaccess.h>
39
40 #include "yaffs_guts.h"
41
42 #ifdef YAFFS_RAM_ENABLED
43 #include "yaffs_nandemul.h" 
44 // 2 MB of RAM for emulation
45 #define YAFFS_RAM_EMULATION_SIZE  0x200000
46 #endif //YAFFS_RAM_ENABLED
47
48 #ifdef YAFFS_MTD_ENABLED
49 #include <linux/mtd/mtd.h>
50 #include "yaffs_mtdif.h"
51 #endif //YAFFS_MTD_ENABLED
52
53 #define T(x) printk x
54
55
56
57 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
58 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
59 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
60
61
62
63 static void yaffs_put_super(struct super_block *sb);
64
65 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos);
66 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos);
67
68 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync);
69
70 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
71
72 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
73 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry);
74 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry);
75 static int yaffs_unlink(struct inode * dir, struct dentry *dentry);
76 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname);
77 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode);
78 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev);
79 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry);
80 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
81
82 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
83 static void yaffs_read_inode (struct inode *inode);
84 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);
85 static void yaffs_put_inode (struct inode *inode);
86
87 //static int yaffs_readpage(struct file*,struct page *
88
89 static struct address_space_operations yaffs_address_ops = {
90 //      readpage:               yaffs_readpage,
91 //      prepare_write:  yaffs_prepare_write,
92 //      commit_write:   yaffs_commit_write
93 };
94
95
96 static struct file_operations yaffs_file_operations = {
97         read:           yaffs_file_read,
98         write:          yaffs_file_write,
99 //      mmap:           generic_file_mmap,
100         fsync:          yaffs_sync_object,
101 };
102
103
104 static struct inode_operations yaffs_file_inode_operations = {
105         setattr:        yaffs_setattr,
106 };
107
108
109
110 static struct file_operations yaffs_dir_operations = {
111         read:           generic_read_dir,
112         readdir:        yaffs_readdir,
113         fsync:          yaffs_sync_object,
114 };
115
116 static struct inode_operations yaffs_dir_inode_operations = {
117         create:         yaffs_create,
118         lookup:         yaffs_lookup,
119         link:           yaffs_link,
120         unlink:         yaffs_unlink,   
121         symlink:        yaffs_symlink,
122         mkdir:          yaffs_mkdir,
123         rmdir:          yaffs_unlink,
124         mknod:          yaffs_mknod,
125         rename:         yaffs_rename,
126         setattr:        yaffs_setattr,
127 };
128
129 static struct super_operations yaffs_super_ops = {
130         statfs:                 yaffs_statfs,
131         read_inode:             yaffs_read_inode,
132         put_inode:              yaffs_put_inode,
133         put_super:              yaffs_put_super,
134 //      read_inode:
135 //      remount_fs:
136 //      clear_inode:
137 };
138
139
140 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj);
141
142 /*
143  * Lookup is used to find objects in the fs
144  */
145 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
146 {
147         yaffs_Object *obj;
148         struct inode *inode;
149         
150         
151         T((KERN_DEBUG"yaffs_lookup for %d:%s\n",yaffs_InodeToObject(dir)->objectId,dentry->d_name.name));
152         
153         obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),dentry->d_name.name);
154         
155         if(obj)
156         {
157                 T((KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
158                 
159                 inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj);
160                 
161                 if(inode)
162                 {
163                         T((KERN_DEBUG"yaffs_loookup looks good\n"));
164                         dget(dentry); // try to solve directory bug
165                         d_add(dentry,inode);
166                         return dentry;
167                 }
168
169         }
170         else
171         {
172                 T((KERN_DEBUG"yaffs_lookup not found\n"));
173                 
174         }
175         return NULL;
176         
177 }
178
179 // For now put inode is just for debugging
180 static void yaffs_put_inode(struct inode *inode)
181 {
182         T(("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count)));
183         
184         yaffs_FlushFile(yaffs_InodeToObject(inode));
185         
186 }
187
188 #ifdef YAFFS_ADDRESS_OPS
189 static int yaffs_readpage(struct file *file, struct page * page)
190 {
191         T((KERN_DEBUG"yaffs_readpage\n"));
192
193         // TODO
194         return 0;
195 }
196
197 static int yaffs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
198 {
199         T((KERN_DEBUG"yaffs_prepare_write\n"));
200
201         //TODO
202         return 0;
203 }
204
205 static int yaffs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
206 {
207
208         struct inode *inode = page->mapping->host;
209         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
210
211         T((KERN_DEBUG"yaffs_commit_write\n"));
212
213         return 0;
214 }
215
216 #endif //YAFFS_ADDRESS_OPS
217
218
219 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
220 {
221         if (inode && obj) 
222         {
223                 inode->i_ino = obj->objectId;
224                 inode->i_mode = obj->st_mode;
225                 inode->i_uid = obj->st_uid;
226                 inode->i_gid = obj->st_gid;
227                 inode->i_blksize = YAFFS_BYTES_PER_CHUNK;
228                 inode->i_blocks = 0;
229                 inode->i_rdev = NODEV;
230                 inode->i_mapping->a_ops = &yaffs_address_ops;
231                 inode->i_atime = obj->st_atime;
232                 inode->i_mtime = obj->st_mtime;
233                 inode->i_ctime = obj->st_ctime;
234                 inode->i_size = yaffs_GetObjectFileLength(obj);
235                 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
236                 
237                 T((KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
238                                 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
239                 
240                 switch (obj->st_mode & S_IFMT) 
241                 {
242                         default:
243                         //      init_special_inode(inode, mode, dev);
244                                 break;
245                         case S_IFREG:   // file         
246                                 inode->i_op = &yaffs_file_inode_operations;
247                                 inode->i_fop = &yaffs_file_operations;
248                                 break;
249                         case S_IFDIR:   // directory
250                                 inode->i_op = &yaffs_dir_inode_operations;
251                                 inode->i_fop = &yaffs_dir_operations;
252                                 break;
253                         case S_IFLNK:   // symlink
254                                 inode->i_op = &page_symlink_inode_operations;
255                                 break;
256                 }
257                 
258                 
259                 inode->u.generic_ip = obj;
260                 
261         }
262         else
263         {
264                 T((KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
265         }
266
267 }
268
269 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
270 {
271         struct inode * inode;
272         
273         T((KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
274
275         inode = iget(sb,obj->objectId);
276
277         // No need to call this since iget calls it via the read_inode callback
278         // yaffs_FillInodeFromObject(inode,obj);
279
280         return inode;
281 }
282
283 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
284 {
285         yaffs_Object *obj;
286         int nRead,ipos;
287         struct inode *inode;
288         
289         T((KERN_DEBUG"yaffs_file_read\n"));
290
291         obj  = yaffs_DentryToObject(f->f_dentry);
292         inode = f->f_dentry->d_inode;
293         
294         if (*pos < inode->i_size) 
295         {
296                         if (*pos + n > inode->i_size)
297                         {
298                                 n = inode->i_size - *pos;
299                         }
300         }
301         else
302         {
303                 n = 0;
304         }
305         
306         nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
307         if(nRead > 0)
308         {
309                 f->f_pos += nRead;
310         }
311         ipos = *pos;
312         T((KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
313         return nRead;
314         
315 }
316
317 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
318 {
319         yaffs_Object *obj;
320         int nWritten,ipos;
321         struct inode *inode;
322         
323         obj  = yaffs_DentryToObject(f->f_dentry);
324         inode = f->f_dentry->d_inode;
325         nWritten = yaffs_WriteDataToFile(obj,buf,*pos,n);
326         ipos = *pos;
327         T((KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
328         if(nWritten > 0)
329         {
330                 *pos += nWritten;
331                 ipos = *pos;
332                 if(*pos > inode->i_size)
333                 {
334                         inode->i_size = *pos;
335                         T((KERN_DEBUG"yaffs_file_write size updated to %d\n",ipos));
336                 }
337                 
338         }
339         return nWritten;        
340 }
341
342
343 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
344 {
345         yaffs_Object *obj;
346         struct inode *inode = f->f_dentry->d_inode;
347         unsigned long offset, curoffs;
348         struct list_head *i;    
349         yaffs_Object *l;
350         
351         char name[YAFFS_MAX_NAME_LENGTH +1];
352                 
353         obj =  yaffs_DentryToObject(f->f_dentry);
354         
355         offset = f->f_pos;
356         
357         T(("yaffs_readdir: starting at %d\n",(int)offset));
358         
359         if(offset == 0)
360         {
361         T((KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
362                 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
363                 {
364                         goto out;
365                 }
366                 offset++;
367                 f->f_pos++;
368         }
369         if(offset == 1)
370         {
371                 T((KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
372                 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
373                 {
374                         goto out;
375                 }
376                 offset++;
377                 f->f_pos++;
378         }
379         
380         curoffs = 1;
381         
382         //down(&obj->sem);
383
384         
385         list_for_each(i,&obj->variant.directoryVariant.children)
386         {
387                 curoffs++;
388                 if(curoffs >= offset)
389                 {               
390                         l = list_entry(i, yaffs_Object,siblings);
391                         
392                         yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1); 
393                         T((KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
394                         
395                         if(filldir(dirent,
396                                            name,
397                                            strlen(name),
398                                            offset,
399                                            yaffs_GetObjectInode(l),
400                                            yaffs_GetObjectType(l))
401                                            < 0)
402                         {
403                                 goto up_and_out;
404                         }
405                         
406                         offset++;
407                         f->f_pos++;        
408                 }
409         }
410
411   up_and_out:
412
413         //up(&obj->sem);
414         
415   out:
416         return 0;
417 }
418
419
420 /*
421  * File creation. Allocate an inode, and we're done..
422  */
423 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
424 {
425         struct inode *inode;
426         
427         yaffs_Object *obj = NULL;
428         yaffs_Object *parent = yaffs_InodeToObject(dir);
429         
430         int error = -ENOSPC;
431
432         if(parent)
433         {
434                 T((KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
435                                          parent->objectId,parent->variantType));
436         }
437         else
438         {
439                 T((KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
440                 return -EPERM;
441         }
442         
443         T(("yaffs_mknod: making oject for %s, mode %x\n",
444                                 dentry->d_name.name, mode));
445
446         switch (mode & S_IFMT) 
447         {
448                 default:
449                 
450                         break;
451                 case S_IFREG:   // file         
452                         T((KERN_DEBUG"yaffs_mknod: making file\n"));
453                         obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
454                         break;
455                 case S_IFDIR:   // directory
456                         T((KERN_DEBUG"yaffs_mknod: making directory\n"));
457                         obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
458                         break;
459                 case S_IFLNK:   // symlink
460                         T((KERN_DEBUG"yaffs_mknod: making file\n"));
461                         obj = NULL; // Todo
462                         break;
463         }
464         
465         if(obj)
466         {
467                 inode = yaffs_get_inode(dir->i_sb, mode, dev, obj);
468
469 // did not fix dir bug          if((mode & S_IFMT) == S_IFDIR)  atomic_inc(&inode->i_count);
470
471                 d_instantiate(dentry, inode);
472                 T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
473                 error = 0;
474         }
475         else
476         {
477                 T((KERN_DEBUG"yaffs_mknod failed making object\n"));
478                 error = -ENOMEM;
479         }
480         
481
482         return error;
483 }
484
485 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
486 {
487         int retVal;
488         T((KERN_DEBUG"yaffs_mkdir\n"));
489         retVal =  yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
490 #if 0
491  // attempt to fix dir bug - didn't work
492         if(!retVal)
493         {
494                 dget(dentry);
495         }
496 #endif
497         return retVal;
498 }
499
500 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
501 {
502         T((KERN_DEBUG"yaffs_create\n"));
503         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
504 }
505
506
507 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
508 {
509         
510         T((KERN_DEBUG"yaffs_unlink\n"));
511         
512         if(yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name) == YAFFS_OK)
513         {
514                 return 0;
515         }
516         else
517         {
518                 return -ENOTEMPTY;
519         }
520 }
521
522
523 /*
524  * Link a file..
525  */
526 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
527 {
528         struct inode *inode = old_dentry->d_inode;
529         
530         T((KERN_DEBUG"yaffs_link\n"));
531         
532         return -EPERM; //Todo
533         
534
535         if (S_ISDIR(inode->i_mode))
536                 return -EPERM;
537
538
539         return 0;
540 }
541
542
543 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
544 {
545         int error;
546         
547         T((KERN_DEBUG"yaffs_symlink\n"));
548
549         
550         return -ENOMEM; //Todo
551
552         error = yaffs_mknod(dir, dentry, S_IFLNK | S_IRWXUGO, 0);
553         return error;
554 }
555
556 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
557 {
558
559         T((KERN_DEBUG"yaffs_sync_object\n"));
560         yaffs_FlushFile(yaffs_DentryToObject(dentry));
561         return 0;
562 }
563
564 /*
565  * The VFS layer already does all the dentry stuff for rename.
566  */
567 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
568 {
569         
570         
571         if( yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
572                                yaffs_InodeToObject(new_dir),new_dentry->d_name.name) == YAFFS_OK)
573         {
574                 return 0;
575         }
576         else
577         {
578                 return -ENOTEMPTY;
579         }
580         
581
582 }
583
584 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
585 {
586         struct inode *inode = dentry->d_inode;
587         int error;
588         
589         T((KERN_DEBUG"yaffs_setattr\n"));
590         
591         if((error = inode_change_ok(inode,attr)) == 0)
592         {
593         
594                 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
595                 {
596                         error = 0;
597                 }
598                 else
599                 {
600                         error = -EPERM;
601                 }
602
603                 inode_setattr(inode,attr);
604         }
605         return error;
606 }
607
608 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
609 {
610         T((KERN_DEBUG"yaffs_statfs\n"));
611
612         buf->f_type = YAFFS_MAGIC;
613         buf->f_bsize = YAFFS_BYTES_PER_CHUNK;
614         buf->f_namelen = 255;
615         buf->f_blocks = yaffs_SuperToDevice(sb)->nBlocks * YAFFS_CHUNKS_PER_BLOCK;
616         buf->f_files = 0;
617         buf->f_ffree = 0;
618         buf->f_bavail = buf->f_bfree = yaffs_GetNumberOfFreeChunks(yaffs_SuperToDevice(sb));
619         return 0;
620 }
621
622 static void yaffs_read_inode (struct inode *inode)
623 {
624
625         yaffs_Object *obj ; 
626         
627         T((KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
628
629         obj  = yaffs_FindObjectByNumber(yaffs_SuperToDevice(inode->i_sb),inode->i_ino);
630         
631         yaffs_FillInodeFromObject(inode,obj);
632 }
633
634
635 static void yaffs_put_super(struct super_block *sb)
636 {
637         yaffs_Device *dev = yaffs_SuperToDevice(sb);
638         
639         if(dev->putSuperFunc)
640         {
641                  dev->putSuperFunc(sb);
642         }
643 }
644
645
646 #ifdef YAFFS_MTD_ENABLED
647
648 static void  yaffs_MTDPutSuper(struct super_block *sb)
649 {
650         
651         struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
652         
653         if(mtd->sync)
654         {
655                 mtd->sync(mtd);
656         }
657         
658         put_mtd_device(mtd);
659 }
660
661 #endif
662
663 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
664 {
665         struct inode * inode;
666         struct dentry * root;
667         yaffs_Device *dev;
668         
669
670         T(("yaffs_read_super: %s\n", useRam ? "RAM" : "MTD"));
671         sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
672         sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
673         sb->s_magic = YAFFS_MAGIC;
674         sb->s_op = &yaffs_super_ops;
675         
676         if(!sb)
677                 printk(KERN_INFO"yaffs: sb is NULL\n");
678         else if(!sb->s_dev)
679                 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
680         else if(! kdevname(sb->s_dev))
681                 printk(KERN_INFO"yaffs: kdevname is NULL\n");
682         else
683                 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
684
685         
686         
687         if(useRam)
688         {
689
690 #ifdef YAFFS_RAM_ENABLED
691                 // Set the yaffs_Device up for ram emulation
692
693                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
694                 if(!dev)
695                 {
696                         // Deep shit could not allocate device structure
697                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
698                         return NULL;
699                 }
700
701                 memset(dev,0,sizeof(yaffs_Device));
702                 dev->genericDevice = NULL; // Not used for RAM emulation.
703
704                 dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
705                 dev->startBlock = 1;  // Don't use block 0
706                 dev->endBlock = dev->nBlocks - 1;
707
708                 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
709                 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
710                 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
711                 dev->initialiseNAND = nandemul_InitialiseNAND;
712                 
713 #endif
714
715         }
716         else
717         {       
718 #ifdef YAFFS_MTD_ENABLED
719                 struct mtd_info *mtd;
720                 
721                 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
722                  MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
723                         
724                 // Hope it's a NAND mtd
725                 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
726                 if (!mtd) 
727                 {
728                         printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
729                         return NULL;
730                 }
731                 
732                 if(mtd->type != MTD_NANDFLASH)
733                 {
734                         printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
735                         return NULL;
736                 }
737
738                 printk(KERN_DEBUG" erase %x\n",mtd->erase);
739                 printk(KERN_DEBUG" read %x\n",mtd->read);
740                 printk(KERN_DEBUG" write %x\n",mtd->write);
741                 printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
742                 printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
743                 printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
744                 printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
745
746
747                 if(!mtd->erase ||
748                    !mtd->read  ||
749                    !mtd->write ||
750                    !mtd->read_oob ||
751                    !mtd->write_oob)
752                 {
753                         printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
754                         return NULL;
755                 }
756                 
757                 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
758                    mtd->oobsize != YAFFS_BYTES_PER_SPARE)
759                 {
760                         printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
761                         return NULL;
762                 }
763                    
764
765                 // OK, so if we got here, we have an MTD that's NAND and looks 
766                 // like it has the right capabilities
767                 // Set the yaffs_Device up for ram emulation
768
769                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
770                 if(!dev)
771                 {
772                         // Deep shit could not allocate device structure
773                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
774                         return NULL;
775                 }
776
777                 memset(dev,0,sizeof(yaffs_Device));
778                 dev->genericDevice = mtd; 
779
780                 // Set up the memory size parameters....
781                 
782 // NCB          dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
783                 dev->nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
784                 dev->startBlock = 1;  // Don't use block 0
785                 dev->endBlock = dev->nBlocks - 1;
786
787                 // ... and the functions.
788                 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
789                 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
790                 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
791                 dev->initialiseNAND = nandmtd_InitialiseNAND;
792                 
793                 dev->putSuperFunc = yaffs_MTDPutSuper;
794 #endif
795         }
796
797
798
799         yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
800         T(("yaffs_read_super: guts initialised\n"));
801
802         // Create root inode
803         inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
804         if (!inode)
805                 return NULL;
806
807         T(("yaffs_read_super: got root inode\n"));
808                 
809
810         root = d_alloc_root(inode);
811
812         T(("yaffs_read_super: d_alloc_root done\n"));
813
814         if (!root) {
815                 iput(inode);
816                 return NULL;
817         }
818         sb->s_root = root;
819
820         T(("yaffs_read_super: done\n"));
821         return sb;
822 }
823
824 #ifdef YAFFS_MTD_ENABLED
825 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
826 {
827         return yaffs_internal_read_super(0,sb,data,silent);
828 }
829
830 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
831 #endif
832
833 #ifdef YAFFS_RAM_ENABLED
834
835 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
836 {
837         return yaffs_internal_read_super(1,sb,data,silent);
838 }
839
840 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
841 #endif // YAFFS_RAM_ENABLED
842
843
844 static struct proc_dir_entry *my_proc_entry;
845
846
847 static int  yaffs_proc_read(
848         char *page,
849         char **start,
850         off_t offset,
851         int count,
852         int *eof,
853         void *data
854         )
855 {
856
857         static char my_buffer[1000];
858
859         if (offset > 0) return 0;
860
861         /* Fill the buffer and get its length */
862         sprintf( my_buffer, 
863                 "YAFFS built:"__DATE__ " "__TIME__"\n"
864                 
865         );
866
867         strcpy(page,my_buffer);
868         return strlen(my_buffer);
869 }
870
871 static int __init init_yaffs_fs(void)
872 {
873         int error = 0;
874         
875         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
876     /* Install the proc_fs entry */
877     my_proc_entry = create_proc_read_entry("yaffs",
878                                            S_IRUGO | S_IFREG,
879                                            &proc_root,
880                                            yaffs_proc_read,
881                                            NULL);
882     if(!my_proc_entry)
883     {
884        return -ENOMEM;
885     }
886
887 #ifdef YAFFS_RAM_ENABLED
888
889     error = register_filesystem(&yaffs_ram_fs_type);
890     if(error)
891     {
892         return error;
893     }
894 #endif //YAFFS_RAM_ENABLED
895
896 #ifdef YAFFS_MTD_ENABLED
897         error = register_filesystem(&yaffs_fs_type);
898         if(error)
899         {
900 #ifdef YAFFS_RAM_ENABLED
901                 unregister_filesystem(&yaffs_ram_fs_type);
902 #endif //YAFFS_RAM_ENABLED
903         }
904 #endif // YAFFS_MTD_ENABLED
905
906         return error;
907 }
908
909 static void __exit exit_yaffs_fs(void)
910 {
911         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
912
913     remove_proc_entry("yaffs",&proc_root);
914     
915 #ifdef YAFFS_RAM_ENABLED
916         unregister_filesystem(&yaffs_ram_fs_type);
917 #endif
918 #ifdef YAFFS_MTD_ENABLED
919         unregister_filesystem(&yaffs_fs_type);
920 #endif
921
922 }
923
924 module_init(init_yaffs_fs)
925 module_exit(exit_yaffs_fs)
926
927 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
928 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
929 MODULE_LICENSE("GPL");
930