*** 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         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 = obj->st_rdev;;
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: // fifo, device or socket
328                                 init_special_inode(inode, obj->st_mode, obj->st_rdev);
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 dev %x\n",
530                                 dentry->d_name.name, mode,dev));
531
532         switch (mode & S_IFMT) 
533         {
534                 default:
535                         // Special (socket, fifo, device...)
536                         T((KERN_DEBUG"yaffs_mknod: making special\n"));
537                         obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,dev);
538                         break;
539                 case S_IFREG:   // file         
540                         T((KERN_DEBUG"yaffs_mknod: making file\n"));
541                         obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
542                         break;
543                 case S_IFDIR:   // directory
544                         T((KERN_DEBUG"yaffs_mknod: making directory\n"));
545                         obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
546                         break;
547                 case S_IFLNK:   // symlink
548                         T((KERN_DEBUG"yaffs_mknod: making file\n"));
549                         obj = NULL; // Do we ever get here?
550                         break;
551         }
552         
553         if(obj)
554         {
555                 inode = yaffs_get_inode(dir->i_sb, mode, dev, obj);
556                 d_instantiate(dentry, inode);
557                 T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
558                 error = 0;
559         }
560         else
561         {
562                 T((KERN_DEBUG"yaffs_mknod failed making object\n"));
563                 error = -ENOMEM;
564         }
565         
566
567         return error;
568 }
569
570 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
571 {
572         int retVal;
573         T((KERN_DEBUG"yaffs_mkdir\n"));
574         retVal =  yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
575 #if 0
576  // attempt to fix dir bug - didn't work
577         if(!retVal)
578         {
579                 dget(dentry);
580         }
581 #endif
582         return retVal;
583 }
584
585 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
586 {
587         T((KERN_DEBUG"yaffs_create\n"));
588         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
589 }
590
591
592 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
593 {
594         
595         T((KERN_DEBUG"yaffs_unlink\n"));
596         
597         if(yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name) == YAFFS_OK)
598         {
599                 return 0;
600         }
601         else
602         {
603                 return -ENOTEMPTY;
604         }
605 }
606
607
608 /*
609  * Create a link...
610  */
611 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
612 {
613         struct inode *inode = old_dentry->d_inode;
614         yaffs_Object *obj = NULL;
615         yaffs_Object *link=NULL;
616         
617         T((KERN_DEBUG"yaffs_link\n"));
618         
619         obj = yaffs_InodeToObject(inode);
620         
621         link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
622         
623         if(link)
624         {
625                 return 0;
626         }
627         
628         
629         return -EPERM; 
630 }
631
632
633 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
634 {
635         yaffs_Object *obj;
636         
637         T((KERN_DEBUG"yaffs_symlink\n"));
638         
639         obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name, 
640                                                          S_IFLNK | S_IRWXUGO, current->uid, current->gid,
641                                                          symname);
642
643         if(obj)
644         {
645                 T((KERN_DEBUG"symlink created OK\n"));
646                 return 0;
647         }
648         else
649         {
650                 T((KERN_DEBUG"symlink not created\n"));
651
652         }
653         
654         return -ENOMEM;
655 }
656
657 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
658 {
659
660         T((KERN_DEBUG"yaffs_sync_object\n"));
661         yaffs_FlushFile(yaffs_DentryToObject(dentry));
662         return 0;
663 }
664
665 /*
666  * The VFS layer already does all the dentry stuff for rename.
667  */
668 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
669 {
670         
671         
672         if( yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
673                                yaffs_InodeToObject(new_dir),new_dentry->d_name.name) == YAFFS_OK)
674         {
675                 return 0;
676         }
677         else
678         {
679                 return -ENOTEMPTY;
680         }
681         
682
683 }
684
685 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
686 {
687         struct inode *inode = dentry->d_inode;
688         int error;
689         
690         T((KERN_DEBUG"yaffs_setattr\n"));
691         
692         if((error = inode_change_ok(inode,attr)) == 0)
693         {
694         
695                 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
696                 {
697                         error = 0;
698                 }
699                 else
700                 {
701                         error = -EPERM;
702                 }
703
704                 inode_setattr(inode,attr);
705         }
706         return error;
707 }
708
709 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
710 {
711         T((KERN_DEBUG"yaffs_statfs\n"));
712
713         buf->f_type = YAFFS_MAGIC;
714         buf->f_bsize = YAFFS_BYTES_PER_CHUNK;
715         buf->f_namelen = 255;
716         buf->f_blocks = yaffs_SuperToDevice(sb)->nBlocks * YAFFS_CHUNKS_PER_BLOCK;
717         buf->f_files = 0;
718         buf->f_ffree = 0;
719         buf->f_bavail = buf->f_bfree = yaffs_GetNumberOfFreeChunks(yaffs_SuperToDevice(sb));
720         return 0;
721 }
722
723 static void yaffs_read_inode (struct inode *inode)
724 {
725
726         yaffs_Object *obj ; 
727         
728         T((KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
729
730         obj  = yaffs_FindObjectByNumber(yaffs_SuperToDevice(inode->i_sb),inode->i_ino);
731         
732         yaffs_FillInodeFromObject(inode,obj);
733 }
734
735
736 static void yaffs_put_super(struct super_block *sb)
737 {
738         yaffs_Device *dev = yaffs_SuperToDevice(sb);
739         
740         if(dev->putSuperFunc)
741         {
742                  dev->putSuperFunc(sb);
743         }
744 }
745
746
747 #ifdef YAFFS_MTD_ENABLED
748
749 static void  yaffs_MTDPutSuper(struct super_block *sb)
750 {
751         
752         struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
753         
754         if(mtd->sync)
755         {
756                 mtd->sync(mtd);
757         }
758         
759         put_mtd_device(mtd);
760 }
761
762 #endif
763
764 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
765 {
766         struct inode * inode;
767         struct dentry * root;
768         yaffs_Device *dev;
769         
770
771         T(("yaffs_read_super: %s\n", useRam ? "RAM" : "MTD"));
772         sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
773         sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
774         sb->s_magic = YAFFS_MAGIC;
775         sb->s_op = &yaffs_super_ops;
776         
777         if(!sb)
778                 printk(KERN_INFO"yaffs: sb is NULL\n");
779         else if(!sb->s_dev)
780                 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
781         else if(! kdevname(sb->s_dev))
782                 printk(KERN_INFO"yaffs: kdevname is NULL\n");
783         else
784                 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
785
786         
787         
788         if(useRam)
789         {
790
791 #ifdef YAFFS_RAM_ENABLED
792                 // Set the yaffs_Device up for ram emulation
793
794                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
795                 if(!dev)
796                 {
797                         // Deep shit could not allocate device structure
798                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
799                         return NULL;
800                 }
801
802                 memset(dev,0,sizeof(yaffs_Device));
803                 dev->genericDevice = NULL; // Not used for RAM emulation.
804
805                 dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
806                 dev->startBlock = 1;  // Don't use block 0
807                 dev->endBlock = dev->nBlocks - 1;
808
809                 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
810                 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
811                 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
812                 dev->initialiseNAND = nandemul_InitialiseNAND;
813                 
814 #endif
815
816         }
817         else
818         {       
819 #ifdef YAFFS_MTD_ENABLED
820                 struct mtd_info *mtd;
821                 
822                 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
823                  MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
824                         
825                 // Hope it's a NAND mtd
826                 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
827                 if (!mtd) 
828                 {
829                         printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
830                         return NULL;
831                 }
832                 
833                 if(mtd->type != MTD_NANDFLASH)
834                 {
835                         printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
836                         return NULL;
837                 }
838
839                 printk(KERN_DEBUG" erase %x\n",mtd->erase);
840                 printk(KERN_DEBUG" read %x\n",mtd->read);
841                 printk(KERN_DEBUG" write %x\n",mtd->write);
842                 printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
843                 printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
844                 printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
845                 printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
846
847
848                 if(!mtd->erase ||
849                    !mtd->read  ||
850                    !mtd->write ||
851                    !mtd->read_oob ||
852                    !mtd->write_oob)
853                 {
854                         printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
855                         return NULL;
856                 }
857                 
858                 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
859                    mtd->oobsize != YAFFS_BYTES_PER_SPARE)
860                 {
861                         printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
862                         return NULL;
863                 }
864                    
865
866                 // OK, so if we got here, we have an MTD that's NAND and looks 
867                 // like it has the right capabilities
868                 // Set the yaffs_Device up for ram emulation
869
870                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
871                 if(!dev)
872                 {
873                         // Deep shit could not allocate device structure
874                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
875                         return NULL;
876                 }
877
878                 memset(dev,0,sizeof(yaffs_Device));
879                 dev->genericDevice = mtd; 
880
881                 // Set up the memory size parameters....
882                 
883 // NCB          dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
884                 dev->nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
885                 dev->startBlock = 1;  // Don't use block 0
886                 dev->endBlock = dev->nBlocks - 1;
887
888                 // ... and the functions.
889                 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
890                 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
891                 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
892                 dev->initialiseNAND = nandmtd_InitialiseNAND;
893                 
894                 dev->putSuperFunc = yaffs_MTDPutSuper;
895 #endif
896         }
897
898
899
900         yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
901         T(("yaffs_read_super: guts initialised\n"));
902
903         // Create root inode
904         inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
905         if (!inode)
906                 return NULL;
907
908         T(("yaffs_read_super: got root inode\n"));
909                 
910
911         root = d_alloc_root(inode);
912
913         T(("yaffs_read_super: d_alloc_root done\n"));
914
915         if (!root) {
916                 iput(inode);
917                 return NULL;
918         }
919         sb->s_root = root;
920
921         T(("yaffs_read_super: done\n"));
922         return sb;
923 }
924
925 #ifdef YAFFS_MTD_ENABLED
926 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
927 {
928         return yaffs_internal_read_super(0,sb,data,silent);
929 }
930
931 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
932 #endif
933
934 #ifdef YAFFS_RAM_ENABLED
935
936 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
937 {
938         return yaffs_internal_read_super(1,sb,data,silent);
939 }
940
941 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
942 #endif // YAFFS_RAM_ENABLED
943
944
945 static struct proc_dir_entry *my_proc_entry;
946
947
948 static int  yaffs_proc_read(
949         char *page,
950         char **start,
951         off_t offset,
952         int count,
953         int *eof,
954         void *data
955         )
956 {
957
958         static char my_buffer[1000];
959
960         if (offset > 0) return 0;
961
962         /* Fill the buffer and get its length */
963         sprintf( my_buffer, 
964                 "YAFFS built:"__DATE__ " "__TIME__"\n"
965                 
966         );
967
968         strcpy(page,my_buffer);
969         return strlen(my_buffer);
970 }
971
972 static int __init init_yaffs_fs(void)
973 {
974         int error = 0;
975         
976         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
977     /* Install the proc_fs entry */
978     my_proc_entry = create_proc_read_entry("yaffs",
979                                            S_IRUGO | S_IFREG,
980                                            &proc_root,
981                                            yaffs_proc_read,
982                                            NULL);
983     if(!my_proc_entry)
984     {
985        return -ENOMEM;
986     }
987
988 #ifdef YAFFS_RAM_ENABLED
989
990     error = register_filesystem(&yaffs_ram_fs_type);
991     if(error)
992     {
993         return error;
994     }
995 #endif //YAFFS_RAM_ENABLED
996
997 #ifdef YAFFS_MTD_ENABLED
998         error = register_filesystem(&yaffs_fs_type);
999         if(error)
1000         {
1001 #ifdef YAFFS_RAM_ENABLED
1002                 unregister_filesystem(&yaffs_ram_fs_type);
1003 #endif //YAFFS_RAM_ENABLED
1004         }
1005 #endif // YAFFS_MTD_ENABLED
1006
1007         return error;
1008 }
1009
1010 static void __exit exit_yaffs_fs(void)
1011 {
1012         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
1013
1014     remove_proc_entry("yaffs",&proc_root);
1015     
1016 #ifdef YAFFS_RAM_ENABLED
1017         unregister_filesystem(&yaffs_ram_fs_type);
1018 #endif
1019 #ifdef YAFFS_MTD_ENABLED
1020         unregister_filesystem(&yaffs_fs_type);
1021 #endif
1022
1023 }
1024
1025 module_init(init_yaffs_fs)
1026 module_exit(exit_yaffs_fs)
1027
1028 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
1029 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
1030 MODULE_LICENSE("GPL");
1031