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