Apply Luc's patches
[yaffs2.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  * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with this superblock
19  * >> 2.6: sb->s_fs_info  points to the yaffs_Device associated with this superblock
20  * >> inode->u.generic_ip points to the associated yaffs_Object.
21  *
22  *
23  * Acknowledgements:
24  * * Luc van OostenRyck for numerous patches.
25  * * Nick Bane for numerous patches.
26  * * Nick Bane for 2.5/2.6 integration.
27  * * Andras Toth for mknod rdev issue.
28  * * Michael Fischer for finding the problem with inode inconsistency.
29  * * Some code bodily lifted from JFFS2.
30  */
31
32
33 const char *yaffs_fs_c_version = "$Id: yaffs_fs.c,v 1.9 2005-07-19 20:41:59 charles Exp $";
34 extern const char *yaffs_guts_c_version;
35
36
37
38 #include <linux/config.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/version.h>
42 #include <linux/slab.h>
43 #include <linux/init.h>
44 #include <linux/list.h>
45 #include <linux/fs.h>
46 #include <linux/proc_fs.h>
47 #include <linux/pagemap.h>
48 #include <linux/mtd/mtd.h>
49 #include <linux/interrupt.h>
50 #include <linux/string.h>
51
52
53 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
54
55 #include <linux/statfs.h>       /* Added NCB 15-8-2003 */
56 #include <asm/statfs.h>
57 #define UnlockPage(p) unlock_page(p)
58 #define Page_Uptodate(page)     test_bit(PG_uptodate, &(page)->flags)
59 //#define kdevname(x) cdevname(to_kdev_t(x))
60 #define kdevname(x) "(unavailable)"     // temporary fix
61
62 #else
63
64 #include <linux/locks.h>
65
66 #endif
67
68 #include <asm/uaccess.h>
69
70 #include "yportenv.h"
71 #include "yaffs_guts.h"
72
73
74
75
76 unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS | YAFFS_TRACE_BAD_BLOCKS;
77 //unsigned yaffs_traceMask = 0xFFFFFFFF;
78
79
80 #ifdef CONFIG_YAFFS_RAM_ENABLED
81 #include "yaffs_nandemul.h" 
82 // 2 MB of RAM for emulation
83 #define YAFFS_RAM_EMULATION_SIZE  0x200000
84 #endif //CONFIG_YAFFS_RAM_ENABLED
85
86 #if CONFIG_YAFFS2_RAM_ENABLED
87 #include "yaffs_nandemul2k.h"
88 #endif
89
90 #ifdef CONFIG_YAFFS_MTD_ENABLED
91 #include <linux/mtd/mtd.h>
92 #include "yaffs_mtdif.h"
93 #include "yaffs_mtdif2.h"
94 #endif //CONFIG_YAFFS_MTD_ENABLED
95
96 //#define T(x) printk x
97
98
99
100 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
101 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
102 //NCB #define yaffs_SuperToDevice(sb)   ((yaffs_Device *)sb->u.generic_sbp)
103
104 //#if defined(CONFIG_KERNEL_2_5)
105 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
106 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
107 #else
108 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
109 #endif
110
111
112 static void yaffs_put_super(struct super_block *sb);
113
114 #if 0
115 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos);
116 #endif
117
118 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos);
119
120 static int yaffs_file_flush(struct file* file);
121
122 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync);
123
124 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
125
126 //#if defined(CONFIG_KERNEL_2_5)        /* Added NCB 185-8-2003 */
127 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
128 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *n);
129 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *n);
130 #else
131 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
132 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry);
133 #endif
134 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry);
135 static int yaffs_unlink(struct inode * dir, struct dentry *dentry);
136 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname);
137 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode);
138
139 //#if defined(CONFIG_KERNEL_2_5)
140 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
141 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev);
142 #else
143 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev);
144 #endif
145 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry);
146 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
147
148 //#if defined(CONFIG_KERNEL_2_5)        /* Added NCB 185-8-2003 */
149 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
150 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
151 #else
152 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
153 #endif
154 static void yaffs_read_inode (struct inode *inode);
155
156 //#if defined(CONFIG_KERNEL_2_5)
157 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
158 static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data);
159 #else
160 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);
161 #endif
162
163 static void yaffs_put_inode (struct inode *inode);
164 static void yaffs_delete_inode(struct inode *);
165 static void yaffs_clear_inode(struct inode *);
166
167 static int yaffs_readpage(struct file *file, struct page * page);
168 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
169 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
170 #else
171 static int yaffs_writepage(struct page *page);
172 #endif
173 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
174 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
175
176 static int yaffs_readlink(struct dentry *dentry, char __user *buffer, int buflen);
177 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
178
179
180
181
182 static struct address_space_operations yaffs_file_address_operations = {
183         .readpage:              yaffs_readpage,
184         .writepage:             yaffs_writepage,
185         .prepare_write: yaffs_prepare_write,
186         .commit_write:  yaffs_commit_write
187 };
188
189
190 static struct file_operations yaffs_file_operations = {
191
192         .read:          generic_file_read,
193         .write:         generic_file_write,
194
195         .mmap:          generic_file_mmap,
196         .flush:         yaffs_file_flush,
197         .fsync:         yaffs_sync_object,
198 };
199
200
201 static struct inode_operations yaffs_file_inode_operations = {
202         .setattr:       yaffs_setattr,
203 };
204
205
206 struct inode_operations yaffs_symlink_inode_operations =
207 {       
208         .readlink:      yaffs_readlink,
209         .follow_link:   yaffs_follow_link,
210         .setattr:       yaffs_setattr
211 };
212
213 static struct inode_operations yaffs_dir_inode_operations = {
214         .create:                yaffs_create,
215         .lookup:                yaffs_lookup,
216         .link:          yaffs_link,
217         .unlink:                yaffs_unlink,   
218         .symlink:       yaffs_symlink,
219         .mkdir:         yaffs_mkdir,
220         .rmdir:         yaffs_unlink,
221         .mknod:         yaffs_mknod,
222         .rename:                yaffs_rename,
223         .setattr:       yaffs_setattr,
224 };
225
226 static struct file_operations yaffs_dir_operations = {
227         .read:          generic_read_dir,
228         .readdir:       yaffs_readdir,
229         .fsync:         yaffs_sync_object,
230 };
231
232
233 static struct super_operations yaffs_super_ops = {
234         .statfs:                        yaffs_statfs,
235         .read_inode:            yaffs_read_inode,
236         .put_inode:             yaffs_put_inode,
237         .put_super:             yaffs_put_super,
238         .delete_inode:          yaffs_delete_inode,
239         .clear_inode:           yaffs_clear_inode,
240 };
241
242
243
244 static void yaffs_GrossLock(yaffs_Device *dev)
245 {
246         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs locking\n"));
247
248         down(&dev->grossLock);
249 }
250
251 static void yaffs_GrossUnlock(yaffs_Device *dev)
252 {
253         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs unlocking\n"));
254         up(&dev->grossLock);
255
256 }
257
258 static int yaffs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
259 {
260         unsigned char *alias;
261         int ret;
262
263         yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
264
265
266         yaffs_GrossLock(dev);
267         
268         alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
269         
270         yaffs_GrossUnlock(dev);
271         
272         if(!alias)
273                 return -ENOMEM;
274
275         ret = vfs_readlink(dentry, buffer, buflen, alias);
276         kfree(alias);
277         return ret;
278 }
279
280 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
281 {
282         unsigned char *alias;
283         int ret;
284         yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
285
286
287         yaffs_GrossLock(dev);
288
289         alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
290         
291         yaffs_GrossUnlock(dev);
292         
293         if(!alias)
294                 return -ENOMEM;
295
296         ret = vfs_follow_link(nd,alias);
297         kfree(alias);
298         return ret;
299 }
300
301
302 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj);
303
304 /*
305  * Lookup is used to find objects in the fs
306  */
307 //#if defined(CONFIG_KERNEL_2_5)        /* Added NCB 185-8-2003 */
308 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
309
310 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *n)
311 #else
312 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
313 #endif
314 {
315         yaffs_Object *obj;
316         struct inode *inode = NULL; // NCB 2.5/2.6 needs NULL here
317         
318         yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
319
320
321         yaffs_GrossLock(dev);
322
323         
324         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_lookup for %d:%s\n",yaffs_InodeToObject(dir)->objectId,dentry->d_name.name));
325         
326         obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),dentry->d_name.name);
327         
328         obj = yaffs_GetEquivalentObject(obj); // in case it was a hardlink
329         
330
331         
332         if(obj)
333         {
334                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
335                 
336                 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode,0,obj);
337                 
338                 if(inode)
339                 {
340                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_loookup dentry \n"));
341 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to d_add even if NULL inode */ 
342 #if 0
343                         //dget(dentry); // try to solve directory bug
344                         d_add(dentry,inode);
345                         
346                         yaffs_GrossUnlock(dev);
347
348                         // return dentry;
349                         return NULL;
350 #endif
351                 }
352
353         }
354         else
355         {
356                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_lookup not found\n"));
357                 
358         }
359         yaffs_GrossUnlock(dev);
360
361 /* added NCB for 2.5/6 compatability - forces add even if inode is NULL which creates dentry hash*/     
362         d_add(dentry,inode);
363         
364         return NULL;
365         //      return (ERR_PTR(-EIO));
366         
367 }
368
369 // For now put inode is just for debugging
370 // Put inode is called when the inode **structure** is put.
371 static void yaffs_put_inode(struct inode *inode)
372 {
373         T(YAFFS_TRACE_OS,("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count)));
374         
375 }
376
377 // clear is called to tell the fs to release any per-inode data it holds
378 static void yaffs_clear_inode(struct inode *inode)
379 {
380         yaffs_Object *obj;
381         yaffs_Device *dev;
382         
383         obj = yaffs_InodeToObject(inode);
384         
385         T(YAFFS_TRACE_OS,("yaffs_clear_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count),
386                 obj ? "object exists" : "null object"));        
387
388         if(obj)
389         {
390                 dev = obj->myDev;
391                 yaffs_GrossLock(dev);
392                 
393                 // Clear the association between the inode ant the yaffs_Object.
394                 obj->myInode = NULL;
395                 inode->u.generic_ip = NULL;
396                 
397                 // If the object freeing was deferred, then the real free happens now.
398                 // This should fix the inode inconsistency problem.
399                 
400                 yaffs_HandleDeferedFree(obj);
401                 
402                 yaffs_GrossUnlock(dev);
403         }
404         
405         
406 }
407
408 // delete is called when the link count is zero and the inode
409 // is put (ie. nobody wants to know about it anymore, time to
410 // delete the file).
411 // NB Must call clear_inode()
412 static void yaffs_delete_inode(struct inode *inode)
413 {
414         yaffs_Object *obj = yaffs_InodeToObject(inode);
415         yaffs_Device *dev;
416
417         T(YAFFS_TRACE_OS,("yaffs_delete_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count),
418                 obj ? "object exists" : "null object"));
419         
420         if(obj)
421         {
422                 dev = obj->myDev;
423                 yaffs_GrossLock(dev);
424                 yaffs_DeleteFile(obj);
425                 yaffs_GrossUnlock(dev);
426         }
427         clear_inode(inode);
428 }
429
430
431 static int yaffs_file_flush(struct file* file)
432 {
433         yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
434         
435         yaffs_Device *dev = obj->myDev;
436         
437         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_flush object %d (%s)\n",obj->objectId,
438                                 obj->dirty ? "dirty" : "clean"));
439
440         yaffs_GrossLock(dev);
441         
442     yaffs_FlushFile(obj,1);
443
444         yaffs_GrossUnlock(dev);
445
446     return 0;
447 }
448
449
450
451 static int yaffs_readpage_nolock(struct file *f, struct page * pg)
452 {
453         // Lifted from jffs2
454         
455         yaffs_Object *obj;
456         unsigned char *pg_buf;
457         int ret;
458
459         yaffs_Device *dev;
460
461         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readpage at %08x, size %08x\n",
462                       (unsigned)(pg->index << PAGE_CACHE_SHIFT), (unsigned)PAGE_CACHE_SIZE));
463
464         obj  = yaffs_DentryToObject(f->f_dentry);
465
466         dev = obj->myDev;
467         
468         
469 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
470         BUG_ON(!PageLocked(pg));
471 #else
472         if (!PageLocked(pg))
473                 PAGE_BUG(pg);
474 #endif
475
476         pg_buf = kmap(pg);
477         /* FIXME: Can kmap fail? */
478
479         yaffs_GrossLock(dev);
480         
481         ret = yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
482
483         yaffs_GrossUnlock(dev);
484         
485         if(ret >= 0) ret = 0;
486
487         if (ret) {
488                 ClearPageUptodate(pg);
489                 SetPageError(pg);
490         } else {
491                 SetPageUptodate(pg);
492                 ClearPageError(pg);
493         }
494
495         flush_dcache_page(pg);
496         kunmap(pg);
497
498
499         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readpage done\n"));
500         return ret;
501 }
502
503 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
504 {
505         int ret = yaffs_readpage_nolock(f,pg);
506         UnlockPage(pg);
507         return ret;
508 }
509
510 static int yaffs_readpage(struct file *f, struct page * pg)
511 {
512         return yaffs_readpage_unlock(f,pg);
513 }
514
515 // writepage inspired by/stolen from smbfs
516 //
517
518 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
519 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
520 #else
521 static int yaffs_writepage(struct page *page)
522 #endif
523 {
524         struct address_space *mapping = page->mapping;
525         struct inode *inode;
526         unsigned long end_index;
527         char *buffer;
528         yaffs_Object *obj;
529         int nWritten = 0;
530         unsigned nBytes;
531
532         if (!mapping)
533                 BUG();
534         inode = mapping->host;
535         if (!inode)
536                 BUG();
537
538         end_index = inode->i_size >> PAGE_CACHE_SHIFT;
539
540         /* easy case */
541         if (page->index < end_index)
542         {
543                 nBytes = PAGE_CACHE_SIZE;
544         }
545         else
546         {
547                 nBytes = inode->i_size & (PAGE_CACHE_SIZE-1);
548         }
549         //  What's happening here?
550         ///* OK, are we completely out? */
551         //if (page->index >= end_index+1 || !offset)
552         //      return -EIO;
553
554         get_page(page);
555
556
557         buffer = kmap(page);
558
559         obj = yaffs_InodeToObject(inode);
560         yaffs_GrossLock(obj->myDev);
561
562
563         nWritten = yaffs_WriteDataToFile(obj,buffer,page->index << PAGE_CACHE_SHIFT,nBytes,0);
564
565         yaffs_GrossUnlock(obj->myDev);
566         
567         kunmap(page);
568         SetPageUptodate(page);
569         UnlockPage(page);
570         put_page(page);
571
572         return (nWritten == nBytes) ? 0  : -ENOSPC;
573 }
574
575
576
577 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
578 {
579
580         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_prepair_write\n"));
581         if(!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
582                 return  yaffs_readpage_nolock(f,pg);    
583
584         return 0;
585         
586 }
587
588 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
589 {
590
591         void *addr = page_address(pg) + offset;
592         loff_t pos = (((loff_t)pg->index) << PAGE_CACHE_SHIFT) + offset;
593         int nBytes = to - offset;
594         int nWritten;
595         
596         unsigned spos = pos;
597         unsigned saddr = (unsigned)addr;
598
599         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write addr %x pos %x nBytes %d\n",saddr,spos,nBytes));
600         
601         nWritten = yaffs_file_write(f,addr, nBytes, &pos);
602         
603         if(nWritten != nBytes)
604         {
605                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write not same size nWritten %d  nBytes %d\n",nWritten,nBytes));
606                 SetPageError(pg);
607                 ClearPageUptodate(pg);
608         }
609         else
610         {
611                 SetPageUptodate(pg);
612         }
613
614         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write returning %d\n",nWritten));
615         
616         return nWritten;
617
618 }
619
620
621
622 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
623 {
624         if (inode && obj) 
625         {
626                 inode->i_ino = obj->objectId;
627                 inode->i_mode = obj->yst_mode;
628                 inode->i_uid = obj->yst_uid;
629                 inode->i_gid = obj->yst_gid;
630                 inode->i_blksize = inode->i_sb->s_blocksize;
631 //#if defined(CONFIG_KERNEL_2_5)
632 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
633
634                 inode->i_rdev = old_decode_dev(obj->yst_rdev);
635                 inode->i_atime.tv_sec = (time_t)(obj->yst_atime);
636                 inode->i_atime.tv_nsec = 0;
637                 inode->i_mtime.tv_sec = (time_t)obj->yst_mtime;
638                 inode->i_mtime.tv_nsec =0;
639                 inode->i_ctime.tv_sec = (time_t)obj->yst_ctime;
640                 inode->i_ctime.tv_nsec = 0;
641 #else
642                 inode->i_rdev = obj->yst_rdev;
643                 inode->i_atime = obj->yst_atime;
644                 inode->i_mtime = obj->yst_mtime;
645                 inode->i_ctime = obj->yst_ctime;
646 #endif
647                 inode->i_size = yaffs_GetObjectFileLength(obj);
648                 inode->i_blocks = (inode->i_size + 511) >> 9;
649
650                 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
651                 
652                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
653                                 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
654                 
655                 switch (obj->yst_mode & S_IFMT) 
656                 {
657                         default: // fifo, device or socket
658 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
659                                init_special_inode(inode, obj->yst_mode,old_decode_dev(obj->yst_rdev));
660 #else
661                                  init_special_inode(inode, obj->yst_mode,(dev_t)(obj->yst_rdev));
662 #endif                          
663                         break;
664                         case S_IFREG:   // file         
665                                 inode->i_op = &yaffs_file_inode_operations;
666                                 inode->i_fop = &yaffs_file_operations;
667                                 inode->i_mapping->a_ops = &yaffs_file_address_operations;
668                                 break;
669                         case S_IFDIR:   // directory
670                                 inode->i_op = &yaffs_dir_inode_operations;
671                                 inode->i_fop = &yaffs_dir_operations;
672                                 break;
673                         case S_IFLNK:   // symlink
674                                 inode->i_op = &yaffs_symlink_inode_operations;
675                                 break;
676                 }
677                 
678                 
679                 inode->u.generic_ip = obj;
680                 obj->myInode = inode;
681                 
682         }
683         else
684         {
685                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
686         }
687
688 }
689
690 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
691 {
692         struct inode * inode;
693         
694         if(!sb)
695         {
696           T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_get_inode for NULL super_block!!\n"));
697           return NULL;
698           
699         }
700         
701         if(!obj)
702         {
703           T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_get_inode for NULL object!!\n"));
704           return NULL;
705           
706         }
707         
708         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
709
710         inode = iget(sb,obj->objectId);
711
712         // NB Side effect: iget calls back to yaffs_read_inode().
713         // iget also increments the inode's i_count
714         
715         return inode;
716 }
717
718 #if 0
719
720 // No longer used because we use generic rw */
721 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
722 {
723         yaffs_Object *obj;
724         int nRead,ipos;
725         struct inode *inode;
726         yaffs_Device *dev;
727         
728         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_read\n"));
729
730         obj  = yaffs_DentryToObject(f->f_dentry);
731         
732         dev = obj->myDev;
733         
734         yaffs_GrossLock(dev);
735         
736         inode = f->f_dentry->d_inode;
737         
738         if (*pos < inode->i_size) 
739         {
740                         if (*pos + n > inode->i_size)
741                         {
742                                 n = inode->i_size - *pos;
743                         }
744         }
745         else
746         {
747                 n = 0;
748         }
749         
750         nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
751         if(nRead > 0)
752         {
753                 f->f_pos += nRead;
754         }
755         
756         yaffs_GrossUnlock(dev);
757         
758         ipos = *pos;
759         
760         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
761         return nRead;
762         
763 }
764
765 #endif
766
767 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
768 {
769         yaffs_Object *obj;
770         int nWritten,ipos;
771         struct inode *inode;
772         yaffs_Device *dev;
773         
774         
775         obj  = yaffs_DentryToObject(f->f_dentry);
776         
777         dev = obj->myDev;
778         
779         yaffs_GrossLock(dev);
780
781         inode = f->f_dentry->d_inode;
782
783         if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
784         {
785                 ipos = inode->i_size;
786         }
787         else
788         {
789                 ipos = *pos;
790         }
791         
792         
793         if(!obj)
794         {
795                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write: hey obj is null!\n"));
796         }
797         else
798         {
799                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write about to write writing %d bytes to object %d at %d\n",n,obj->objectId,ipos));
800         }
801
802         nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n,0);
803
804         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
805         if(nWritten > 0)
806         {
807                 ipos += nWritten;
808                 *pos = ipos;
809                 if(ipos > inode->i_size)
810                 {
811                         inode->i_size = ipos;
812                         inode->i_blocks = (ipos + 511)>>9;
813                         
814                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,(int)(inode->i_blocks)));
815                 }
816                 
817         }
818         yaffs_GrossUnlock(dev);
819         
820         return nWritten != n ? -ENOSPC : nWritten;
821 }
822
823
824
825 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
826 {
827         yaffs_Object *obj;
828         yaffs_Device *dev;
829         struct inode *inode = f->f_dentry->d_inode;
830         unsigned long offset, curoffs;
831         struct list_head *i;    
832         yaffs_Object *l;
833         
834         char name[YAFFS_MAX_NAME_LENGTH +1];
835                 
836         obj =  yaffs_DentryToObject(f->f_dentry);
837         dev = obj->myDev;
838         
839         yaffs_GrossLock(dev);
840         
841         offset = f->f_pos;
842         
843         T(YAFFS_TRACE_OS,("yaffs_readdir: starting at %d\n",(int)offset));
844         
845         if(offset == 0)
846         {
847         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
848                 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
849                 {
850                         goto out;
851                 }
852                 offset++;
853                 f->f_pos++;
854         }
855         if(offset == 1)
856         {
857                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
858                 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
859                 {
860                         goto out;
861                 }
862                 offset++;
863                 f->f_pos++;
864         }
865         
866         curoffs = 1;
867         
868         list_for_each(i,&obj->variant.directoryVariant.children)
869         {
870                 curoffs++;
871                 if(curoffs >= offset)
872                 {               
873                         l = list_entry(i, yaffs_Object,siblings);
874                         
875                         yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1); 
876                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
877                         
878                         if(filldir(dirent,
879                                            name,
880                                            strlen(name),
881                                            offset,
882                                            yaffs_GetObjectInode(l),
883                                            yaffs_GetObjectType(l))
884                                            < 0)
885                         {
886                                 goto up_and_out;
887                         }
888                         
889                         offset++;
890                         f->f_pos++;        
891                 }
892         }
893
894   up_and_out:
895   out:
896   
897     yaffs_GrossUnlock(dev);
898     
899         return 0;
900 }
901
902
903 /*
904  * File creation. Allocate an inode, and we're done..
905  */
906 //#if defined(CONFIG_KERNEL_2_5)
907 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
908 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
909 #else
910 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
911 #endif
912 {
913         struct inode *inode;
914         
915         yaffs_Object *obj = NULL;
916         yaffs_Device *dev;
917         
918         yaffs_Object *parent = yaffs_InodeToObject(dir);
919         
920         int error = -ENOSPC;
921
922         if(parent)
923         {
924                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
925                                          parent->objectId,parent->variantType));
926         }
927         else
928         {
929                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
930                 return -EPERM;
931         }
932         
933         T(YAFFS_TRACE_OS,("yaffs_mknod: making oject for %s, mode %x dev %x\n",
934                                         dentry->d_name.name, mode,rdev));
935
936         dev = parent->myDev;
937         
938         yaffs_GrossLock(dev);
939
940         switch (mode & S_IFMT) 
941         {
942                 default:
943                         // Special (socket, fifo, device...)
944                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making special\n"));
945 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
946                         obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,old_encode_dev(rdev));
947 #else
948                         obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,rdev);
949 #endif                  
950                 break;
951                 case S_IFREG:   // file         
952                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
953                         obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
954                         break;
955                 case S_IFDIR:   // directory
956                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making directory\n"));
957                         obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
958                         break;
959                 case S_IFLNK:   // symlink
960                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
961                         obj = NULL; // Do we ever get here?
962                         break;
963         }
964         
965         if(obj)
966         {
967                 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
968                 d_instantiate(dentry, inode);
969                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
970                 error = 0;
971         }
972         else
973         {
974                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod failed making object\n"));
975                 error = -ENOMEM;
976         }
977
978         yaffs_GrossUnlock(dev);
979
980         return error;
981 }
982
983 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
984 {
985         int retVal;
986         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mkdir\n"));
987         retVal =  yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
988 #if 0
989  // attempt to fix dir bug - didn't work
990         if(!retVal)
991         {
992                 dget(dentry);
993         }
994 #endif
995         return retVal;
996 }
997
998 //#if defined(CONFIG_KERNEL_2_5)        /* Added NCB 185-8-2003 */
999 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1000 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *n)
1001 #else
1002 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1003 #endif
1004 {
1005         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_create\n"));
1006         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1007 }
1008
1009
1010 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
1011 {
1012         int retVal;
1013         
1014         yaffs_Device *dev;
1015         
1016         
1017         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_unlink %d:%s\n",(int)(dir->i_ino),dentry->d_name.name));
1018         
1019         dev = yaffs_InodeToObject(dir)->myDev;
1020         
1021         yaffs_GrossLock(dev);
1022         
1023         
1024         retVal = yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name);
1025         
1026         
1027         yaffs_GrossUnlock(dev);
1028         
1029         if( retVal == YAFFS_OK)
1030         {
1031                 dentry->d_inode->i_nlink--;
1032                 mark_inode_dirty(dentry->d_inode);
1033                 return 0;
1034         }
1035         else
1036         {
1037                 return -ENOTEMPTY;
1038         }
1039 }
1040
1041
1042 /*
1043  * Create a link...
1044  */
1045 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
1046 {
1047         struct inode *inode = old_dentry->d_inode;
1048         yaffs_Object *obj = NULL;
1049         yaffs_Object *link=NULL;
1050         yaffs_Device *dev;
1051         
1052         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link\n"));
1053         
1054         obj = yaffs_InodeToObject(inode);
1055         dev = obj->myDev;
1056         
1057         yaffs_GrossLock(dev);
1058
1059         if (!S_ISDIR(inode->i_mode))    // Don't link directories
1060         {
1061                 link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
1062         }
1063         
1064
1065         if(link)
1066         {
1067                 old_dentry->d_inode->i_nlink =  yaffs_GetObjectLinkCount(obj);
1068                 d_instantiate(dentry, old_dentry->d_inode);
1069                 atomic_inc(&old_dentry->d_inode->i_count);
1070                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link link count %d i_count %d\n",    
1071                         old_dentry->d_inode->i_nlink,atomic_read(&old_dentry->d_inode->i_count)));
1072         
1073         }
1074         
1075         yaffs_GrossUnlock(dev);
1076         
1077
1078         if(link)
1079         {
1080         
1081                 return 0;
1082         }
1083         
1084         
1085         return -EPERM; 
1086 }
1087
1088
1089 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
1090 {
1091         yaffs_Object *obj;
1092         yaffs_Device *dev;
1093         
1094         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_symlink\n"));
1095         
1096         dev = yaffs_InodeToObject(dir)->myDev;
1097         yaffs_GrossLock(dev);
1098         obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name, 
1099                                                          S_IFLNK | S_IRWXUGO, current->uid, current->gid,
1100                                                          symname);
1101         yaffs_GrossUnlock(dev);
1102
1103         if(obj)
1104         {
1105
1106                 struct inode* inode;
1107         
1108                 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1109                 d_instantiate(dentry, inode);
1110                 T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink created OK\n"));
1111                 return 0;
1112         }
1113         else
1114         {
1115                 T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink not created\n"));
1116
1117         }
1118         
1119         return -ENOMEM;
1120 }
1121
1122 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
1123 {
1124
1125         yaffs_Object *obj;
1126         yaffs_Device *dev;
1127         
1128         obj = yaffs_DentryToObject(dentry);
1129
1130         dev = obj->myDev;
1131         
1132         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_sync_object\n"));
1133         yaffs_GrossLock(dev);
1134         yaffs_FlushFile(obj,1);
1135         yaffs_GrossUnlock(dev);
1136         return 0;
1137 }
1138
1139 /*
1140  * The VFS layer already does all the dentry stuff for rename.
1141  *
1142  * NB: POSIX says you can rename an object over an old object of the same name
1143  */
1144 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
1145 {
1146         yaffs_Device *dev;
1147         int retVal = YAFFS_FAIL;
1148         int removed = 0;
1149         yaffs_Object *target;
1150         
1151         dev = yaffs_InodeToObject(old_dir)->myDev;
1152
1153         yaffs_GrossLock(dev);
1154         
1155         // Check if the target is an existing directory that is not empty.
1156         target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1157         
1158         if(target &&
1159            target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1160            !list_empty(&target->variant.directoryVariant.children))
1161         {
1162                 retVal = YAFFS_FAIL;
1163         }
1164         else
1165         {
1166            
1167                 // Unlink the target if it exists
1168                 removed = yaffs_Unlink(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1169
1170         
1171                 retVal =  yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
1172                                                                         yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1173                                                                         
1174         }
1175         yaffs_GrossUnlock(dev);
1176         
1177         if(retVal == YAFFS_OK)
1178         {
1179                 if(removed == YAFFS_OK)
1180                 {
1181                         new_dentry->d_inode->i_nlink--;
1182                         mark_inode_dirty(new_dentry->d_inode);
1183                 }
1184                 
1185                 return 0;
1186         }
1187         else
1188         {
1189                 return -ENOTEMPTY;
1190         }
1191         
1192
1193 }
1194
1195 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1196 {
1197         struct inode *inode = dentry->d_inode;
1198         int error;
1199         yaffs_Device *dev;
1200         
1201         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_setattr of object %d\n",yaffs_InodeToObject(inode)->objectId));
1202         
1203         if((error = inode_change_ok(inode,attr)) == 0)
1204         {
1205         
1206                 dev = yaffs_InodeToObject(inode)->myDev;
1207                 yaffs_GrossLock(dev);
1208                 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
1209                 {
1210                         error = 0;
1211                 }
1212                 else
1213                 {
1214                         error = -EPERM;
1215                 }
1216                 yaffs_GrossUnlock(dev);
1217                 if (!error)
1218                         error = inode_setattr(inode,attr);
1219         }
1220         return error;
1221 }
1222
1223 //#if defined(CONFIG_KERNEL_2_5)        /* Added NCB 185-8-2003 */
1224 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1225 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1226 #else
1227 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1228 #endif
1229 {
1230
1231         
1232         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1233         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_statfs\n"));
1234
1235         yaffs_GrossLock(dev);
1236         
1237         
1238         buf->f_type = YAFFS_MAGIC;
1239         buf->f_bsize = sb->s_blocksize;
1240         buf->f_namelen = 255;
1241         if(sb->s_blocksize > dev->nBytesPerChunk)
1242         {
1243                 
1244                 buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * dev->nChunksPerBlock/
1245                                                 (sb->s_blocksize/dev->nBytesPerChunk);
1246                 buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev)/
1247                                                 (sb->s_blocksize/dev->nBytesPerChunk);
1248         }
1249         else
1250         {
1251                 
1252                 buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * dev->nChunksPerBlock *
1253                                                 (dev->nBytesPerChunk/sb->s_blocksize);
1254                 buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev) *
1255                                                 (dev->nBytesPerChunk/sb->s_blocksize);
1256         }
1257         buf->f_files = 0;
1258         buf->f_ffree = 0;
1259         buf->f_bavail =  buf->f_bfree;
1260         
1261         yaffs_GrossUnlock(dev);
1262         return 0;
1263 }
1264
1265 static void yaffs_read_inode (struct inode *inode)
1266 {
1267         // NB This is called as a side effect of other functions and
1268         // thus gross locking should always be in place already.
1269         
1270         yaffs_Object *obj ; 
1271         yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1272         
1273         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
1274
1275         obj  = yaffs_FindObjectByNumber(dev,inode->i_ino);
1276         
1277         yaffs_FillInodeFromObject(inode,obj);
1278
1279 }
1280
1281
1282
1283 // Todo
1284 // Currently we only can report a single partition. Need to use lists here
1285 static yaffs_Device *yaffs_dev;
1286 static yaffs_Device *yaffsram_dev;
1287
1288
1289
1290 static void yaffs_put_super(struct super_block *sb)
1291 {
1292         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1293         
1294         yaffs_GrossLock(dev);
1295         if(dev->putSuperFunc)
1296         {
1297                  dev->putSuperFunc(sb);
1298         }
1299         yaffs_Deinitialise(dev);
1300         yaffs_GrossUnlock(dev);
1301
1302         if(dev == yaffs_dev) yaffs_dev = NULL;
1303         if(dev == yaffsram_dev) yaffsram_dev = NULL;
1304                 
1305         kfree(dev);
1306 }
1307
1308
1309 #ifdef CONFIG_YAFFS_MTD_ENABLED
1310
1311 static void  yaffs_MTDPutSuper(struct super_block *sb)
1312 {
1313         
1314         struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1315         
1316         if(mtd->sync)
1317         {
1318                 mtd->sync(mtd);
1319         }
1320         
1321         put_mtd_device(mtd);
1322 }
1323
1324 #endif
1325
1326
1327 static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam, struct super_block * sb, void * data, int silent)
1328 {
1329         int nBlocks;
1330         struct inode * inode = NULL;
1331         struct dentry * root;
1332         yaffs_Device *dev;
1333         int err;
1334         
1335         sb->s_magic = YAFFS_MAGIC;
1336         sb->s_op = &yaffs_super_ops;
1337         
1338         if(!sb)
1339                 printk(KERN_INFO"yaffs: sb is NULL\n");
1340         else if(!sb->s_dev)
1341                 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
1342         else if(! kdevname(sb->s_dev))
1343                 printk(KERN_INFO"yaffs: kdevname is NULL\n");
1344         else
1345                 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
1346
1347         
1348
1349 #ifdef CONFIG_YAFFS_USE_CHUNK_SIZE
1350         sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
1351         sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
1352 #else
1353         sb->s_blocksize = PAGE_CACHE_SIZE;
1354         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1355 #endif
1356         T(YAFFS_TRACE_OS,("yaffs_read_super: Using yaffs%d\n",yaffsVersion));
1357         T(YAFFS_TRACE_OS,("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",(int)(sb->s_blocksize)));
1358
1359 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1360         T(YAFFS_TRACE_OS,("yaffs: Write verification disabled. All guarantees null and void\n"));
1361 #endif
1362
1363
1364         
1365         if(useRam)
1366         {
1367
1368 #ifdef CONFIG_YAFFS_RAM_ENABLED
1369                 // Set the yaffs_Device up for ram emulation
1370
1371                 
1372 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1373                 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1374 #else
1375                 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1376 #endif
1377
1378                 if(!dev)
1379                 {
1380                         // Deep shit could not allocate device structure
1381                         T(YAFFS_TRACE_OS,("yaffs_read_super: Failed trying to allocate yaffs_Device.\n"));
1382                         return NULL;
1383                 }
1384
1385                 memset(dev,0,sizeof(yaffs_Device));
1386                 dev->genericDevice = NULL; // Not used for RAM emulation.
1387
1388                 nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1389                 dev->startBlock = 0;  
1390                 dev->endBlock = nBlocks - 1;
1391                 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1392                 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1393                 dev->nReservedBlocks = 5;
1394                 
1395                 if(yaffsVersion == 2)
1396                 {
1397                         dev->writeChunkWithTagsToNAND  = nandemul2k_WriteChunkWithTagsToNAND;
1398                         dev->readChunkWithTagsFromNAND = nandemul2k_ReadChunkWithTagsFromNAND;
1399                         dev->markNANDBlockBad          = nandemul2k_MarkNANDBlockBad;
1400                         dev->queryNANDBlock            = nandemul2k_QueryNANDBlock;
1401                         dev->eraseBlockInNAND          = nandemul2k_EraseBlockInNAND;
1402                         dev->initialiseNAND            = nandemul2k_InitialiseNAND;
1403                         dev->isYaffs2 = 1;
1404                         dev->nChunksPerBlock = nandemul2k_GetChunksPerBlock();
1405                         dev->nBytesPerChunk =  nandemul2k_GetBytesPerChunk();;
1406                         nBlocks = nandemul2k_GetNumberOfBlocks();
1407                         dev->startBlock = 0;
1408                         dev->endBlock = nBlocks - 1;
1409                 }
1410                 else
1411                 {
1412                         dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
1413                         dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
1414                         dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
1415                         dev->initialiseNAND = nandemul_InitialiseNAND;
1416                         dev->isYaffs2 = 0;
1417                 }
1418                 
1419                 yaffsram_dev = dev;
1420                 
1421 #endif
1422
1423         }
1424         else
1425         {       
1426 #ifdef CONFIG_YAFFS_MTD_ENABLED
1427                 struct mtd_info *mtd;
1428                 
1429                 T(YAFFS_TRACE_ALWAYS,("yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
1430                  MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev)));
1431                         
1432                 // Check it's an mtd device.....
1433                 if(MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
1434                 {
1435                         return NULL; // This isn't an mtd device
1436                 } 
1437                 
1438                 // Get the device
1439                 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1440                 if (!mtd) 
1441                 {
1442                         T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev)));
1443                         return NULL;
1444                 }
1445                 
1446                 // Check it's NAND
1447                 if(mtd->type != MTD_NANDFLASH)
1448                 {
1449                         T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1450                         return NULL;
1451                 }
1452
1453                 T(YAFFS_TRACE_OS,(" erase %p\n",mtd->erase));
1454                 T(YAFFS_TRACE_OS,(" read %p\n",mtd->read));
1455                 T(YAFFS_TRACE_OS,(" write %p\n",mtd->write));
1456                 T(YAFFS_TRACE_OS,(" readoob %p\n",mtd->read_oob));
1457                 T(YAFFS_TRACE_OS,(" writeoob %p\n",mtd->write_oob));
1458                 T(YAFFS_TRACE_OS,(" block_isbad %p\n",mtd->block_isbad));
1459                 T(YAFFS_TRACE_OS,(" block_markbad %p\n",mtd->block_markbad));
1460                 T(YAFFS_TRACE_OS,(" oobblock %d\n",mtd->oobblock));
1461                 T(YAFFS_TRACE_OS,(" oobsize %d\n",mtd->oobsize));
1462                 T(YAFFS_TRACE_OS,(" erasesize %d\n",mtd->erasesize));
1463                 T(YAFFS_TRACE_OS,(" size %d\n",mtd->size));
1464
1465                 if(yaffsVersion == 2)
1466                 {
1467                         // Check for version 2 style functions
1468                         if(!mtd->erase ||
1469                            !mtd->block_isbad ||
1470                            !mtd->block_markbad ||
1471                            !mtd->read  ||
1472                            !mtd->write ||
1473 #ifndef CONFIG_YAFFS_USE_OLD_MTD
1474                            !mtd->write_ecc ||
1475                            !mtd->read_ecc ||
1476 #endif
1477                            !mtd->read_oob ||
1478                            !mtd->write_oob )
1479                         {
1480                                 T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device does not support required functions\n"));;
1481                                 return NULL;
1482                         }
1483                 
1484                         if(mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1485                            mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE)
1486                         {
1487                                 T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device does not support have the right page sizes\n"));
1488                                 return NULL;
1489                         }               }
1490                 else
1491                 {
1492                         // Check for V1 style functions
1493                         if(!mtd->erase ||
1494                            !mtd->read  ||
1495                            !mtd->write ||
1496 #ifndef CONFIG_YAFFS_USE_OLD_MTD
1497                            !mtd->write_ecc ||
1498                            !mtd->read_ecc ||
1499 #endif
1500                            !mtd->read_oob ||
1501                            !mtd->write_oob )
1502                         {
1503                                 T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device does not support required functions\n"));;
1504                                 return NULL;
1505                         }
1506                 
1507                         if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
1508                            mtd->oobsize != YAFFS_BYTES_PER_SPARE)
1509                         {
1510                                 T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device does not support have the right page sizes\n"));
1511                                 return NULL;
1512                         }
1513                 }
1514                    
1515
1516                         // OK, so if we got here, we have an MTD that's NAND and looks 
1517                         // like it has the right capabilities
1518                         // Set the yaffs_Device up for mtd
1519
1520 //#if defined(CONFIG_KERNEL_2_5)
1521 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1522                 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1523 #else
1524                 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1525 #endif
1526                 if(!dev)
1527                 {
1528                         // Deep shit could not allocate device structure
1529                         T(YAFFS_TRACE_ALWAYS,("yaffs_read_super: Failed trying to allocate yaffs_Device. \n"));
1530                         return NULL;
1531                 }
1532
1533                 memset(dev,0,sizeof(yaffs_Device));
1534                 dev->genericDevice = mtd; 
1535
1536                 // Set up the memory size parameters....
1537                 
1538                 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1539                 dev->startBlock = 0;
1540                 dev->endBlock = nBlocks - 1;
1541                 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1542                 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1543                 dev->nReservedBlocks = 5;
1544                 dev->nShortOpCaches = 10; // Enable short op caching
1545                 
1546
1547                 // ... and the functions.
1548                 if(yaffsVersion == 2)
1549                 {
1550                         dev->writeChunkWithTagsToNAND = nandmtd2_WriteChunkWithTagsToNAND;
1551                         dev->readChunkWithTagsFromNAND = nandmtd2_ReadChunkWithTagsFromNAND;
1552                         dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1553                         dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1554                         dev->spareBuffer = YMALLOC(mtd->oobsize);
1555                         dev->isYaffs2 = 1;
1556                         dev->nBytesPerChunk = mtd->oobblock;
1557                         dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1558                         nBlocks = mtd->size / mtd->erasesize;
1559                         dev->startBlock = 0;
1560                         dev->endBlock = nBlocks - 1;
1561                 }
1562                 else
1563                 {
1564                         dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1565                         dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1566                         dev->isYaffs2 = 0;
1567                 }
1568                 // ... and common functions
1569                 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1570                 dev->initialiseNAND = nandmtd_InitialiseNAND;
1571                 
1572                 dev->putSuperFunc = yaffs_MTDPutSuper;
1573                 
1574 #ifdef CONFIG_YAFFS_USE_NANDECC
1575                 dev->useNANDECC = 1;
1576 #endif
1577
1578                 yaffs_dev = dev;
1579                 
1580 #endif
1581         }
1582
1583         init_MUTEX(&dev->grossLock);
1584         
1585         
1586         yaffs_GrossLock(dev);
1587         
1588         err = yaffs_GutsInitialise(dev);
1589
1590         T(YAFFS_TRACE_OS,("yaffs_read_super: guts initialised %s\n", (err == YAFFS_OK) ? "OK" : "FAILED"));
1591
1592         // Create root inode
1593         if(err == YAFFS_OK)
1594           inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(dev));
1595
1596         yaffs_GrossUnlock(dev);
1597
1598         if (!inode)
1599                 return NULL;
1600                 
1601 // added NCB
1602         inode->i_op = & yaffs_dir_inode_operations;
1603         inode->i_fop = & yaffs_dir_operations;
1604
1605         T(YAFFS_TRACE_OS,("yaffs_read_super: got root inode\n"));
1606                 
1607
1608         root = d_alloc_root(inode);
1609
1610         T(YAFFS_TRACE_OS,("yaffs_read_super: d_alloc_root done\n"));
1611
1612         if (!root) {
1613                 iput(inode);
1614                 return NULL;
1615         }
1616         sb->s_root = root;
1617
1618         T(YAFFS_TRACE_OS,("yaffs_read_super: done\n"));
1619         return sb;
1620 }
1621
1622
1623 static int yaffs_internal_read_super_ram(struct super_block * sb, void * data, int silent)
1624 {
1625          return yaffs_internal_read_super(1,1,sb,data,silent) ? 0 : -1;
1626 }
1627 static int yaffs_internal_read_super_mtd(struct super_block * sb, void * data, int silent)
1628 {
1629          return yaffs_internal_read_super(1,0,sb,data,silent) ? 0 : -1;
1630 }
1631
1632 static int yaffs2_internal_read_super_ram(struct super_block * sb, void * data, int silent)
1633 {
1634          return yaffs_internal_read_super(2,1,sb,data,silent) ? 0 : -1;
1635 }
1636 static int yaffs2_internal_read_super_mtd(struct super_block * sb, void * data, int silent)
1637 {
1638          return yaffs_internal_read_super(2,0,sb,data,silent) ? 0 : -1;
1639 }
1640
1641
1642
1643 #ifdef CONFIG_YAFFS_MTD_ENABLED
1644 //#if defined(CONFIG_KERNEL_2_5)
1645 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1646 static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1647 {
1648
1649     return get_sb_bdev(fs, flags, dev_name, data, yaffs_internal_read_super_mtd);
1650 }
1651
1652 /* changes NCB 2.5.70 */
1653 //static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1654 static struct file_system_type yaffs_fs_type = {
1655         .owner          = THIS_MODULE,
1656         .name           = "yaffs",
1657         .get_sb         = yaffs_read_super,
1658         .kill_sb        = kill_block_super,
1659 //      .kill_sb        = kill_litter_super,
1660         .fs_flags       = FS_REQUIRES_DEV,
1661 };
1662 #else
1663 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
1664 {
1665         return yaffs_internal_read_super(1,0,sb,data,silent);
1666 }
1667
1668 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1669 #endif
1670
1671 #endif // CONFIG_YAFFS_MTD_ENABLED
1672
1673 #ifdef CONFIG_YAFFS2_MTD_ENABLED
1674
1675 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1676 static struct super_block *yaffs2_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1677 {
1678
1679     return get_sb_bdev(fs, flags, dev_name, data, yaffs2_internal_read_super_mtd);
1680 }
1681
1682 /* changes NCB 2.5.70 */
1683 //static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1684 static struct file_system_type yaffs2_fs_type = {
1685         .owner          = THIS_MODULE,
1686         .name           = "yaffs2",
1687         .get_sb         = yaffs2_read_super,
1688         .kill_sb        = kill_block_super,
1689 //      .kill_sb        = kill_litter_super,
1690         .fs_flags       = FS_REQUIRES_DEV,
1691 };
1692 #else
1693 static struct super_block *yaffs2_read_super(struct super_block * sb, void * data, int silent)
1694 {
1695         return yaffs_internal_read_super(2,0,sb,data,silent);
1696 }
1697
1698 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super, FS_REQUIRES_DEV);
1699 #endif
1700
1701 #endif // CONFIG_YAFFS2_MTD_ENABLED
1702
1703
1704 #ifdef CONFIG_YAFFS_RAM_ENABLED
1705
1706 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1707 static struct super_block *yaffs_ram_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1708 {
1709
1710     return get_sb_nodev(fs, flags, data, yaffs_internal_read_super_ram);
1711 }
1712
1713
1714 static struct file_system_type yaffs_ram_fs_type = {
1715         .owner          = THIS_MODULE,
1716         .name           = "yaffsram",
1717         .get_sb         = yaffs_ram_read_super,
1718         .kill_sb        = kill_litter_super,
1719         .fs_flags       = 0 ,
1720 };
1721 #else
1722 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
1723 {
1724         return yaffs_internal_read_super(1,1,sb,data,silent);
1725 }
1726
1727 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
1728 #endif
1729
1730 #endif // CONFIG_YAFFS_RAM_ENABLED
1731
1732 #ifdef CONFIG_YAFFS2_RAM_ENABLED
1733
1734 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1735 static struct super_block *yaffs2_ram_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1736 {
1737
1738     return get_sb_nodev(fs, flags, data, yaffs2_internal_read_super_ram);
1739 }
1740
1741
1742 static struct file_system_type yaffs2_ram_fs_type = {
1743         .owner          = THIS_MODULE,
1744         .name           = "yaffs2ram",
1745         .get_sb         = yaffs2_ram_read_super,
1746         .kill_sb        = kill_litter_super,
1747         .fs_flags       = 0 ,
1748 };
1749 #else
1750 static struct super_block *yaffs2_ram_read_super(struct super_block * sb, void * data, int silent)
1751 {
1752         return yaffs_internal_read_super(2,1,sb,data,silent);
1753 }
1754
1755 static DECLARE_FSTYPE(yaffs2_ram_fs_type, "yaffs2ram", yaffs2_ram_read_super, FS_SINGLE);
1756 #endif
1757
1758 #endif // CONFIG_YAFFS2_RAM_ENABLED
1759
1760
1761
1762 static struct proc_dir_entry *my_proc_entry;
1763 static struct proc_dir_entry *my_proc_ram_write_entry;
1764
1765 static char * yaffs_dump_dev(char *buf,yaffs_Device *dev,char *name)
1766 {
1767         buf +=sprintf(buf,"\nDevice %s\n",name);
1768         buf +=sprintf(buf,"startBlock......... %d\n",dev->startBlock);
1769         buf +=sprintf(buf,"endBlock........... %d\n",dev->endBlock);
1770         buf +=sprintf(buf,"chunkGroupBits..... %d\n",dev->chunkGroupBits);
1771         buf +=sprintf(buf,"chunkGroupSize..... %d\n",dev->chunkGroupSize);
1772         buf +=sprintf(buf,"nErasedBlocks...... %d\n",dev->nErasedBlocks);
1773         buf +=sprintf(buf,"nTnodesCreated..... %d\n",dev->nTnodesCreated);
1774         buf +=sprintf(buf,"nFreeTnodes........ %d\n",dev->nFreeTnodes);
1775         buf +=sprintf(buf,"nObjectsCreated.... %d\n",dev->nObjectsCreated);
1776         buf +=sprintf(buf,"nFreeObjects....... %d\n",dev->nFreeObjects);
1777         buf +=sprintf(buf,"nFreeChunks........ %d\n",dev->nFreeChunks);
1778         buf +=sprintf(buf,"nPageWrites........ %d\n",dev->nPageWrites);
1779         buf +=sprintf(buf,"nPageReads......... %d\n",dev->nPageReads);
1780         buf +=sprintf(buf,"nBlockErasures..... %d\n",dev->nBlockErasures);
1781         buf +=sprintf(buf,"nGCCopies.......... %d\n",dev->nGCCopies);
1782         buf +=sprintf(buf,"garbageCollections. %d\n",dev->garbageCollections);
1783         buf +=sprintf(buf,"passiveGCs......... %d\n",dev->passiveGarbageCollections);
1784         buf +=sprintf(buf,"nRetriedWrites..... %d\n",dev->nRetriedWrites);
1785         buf +=sprintf(buf,"nRetireBlocks...... %d\n",dev->nRetiredBlocks);
1786         buf +=sprintf(buf,"eccFixed........... %d\n",dev->eccFixed);
1787         buf +=sprintf(buf,"eccUnfixed......... %d\n",dev->eccUnfixed);
1788         buf +=sprintf(buf,"tagsEccFixed....... %d\n",dev->tagsEccFixed);
1789         buf +=sprintf(buf,"tagsEccUnfixed..... %d\n",dev->tagsEccUnfixed);
1790         buf +=sprintf(buf,"cacheHits.......... %d\n",dev->cacheHits);
1791         buf +=sprintf(buf,"nDeletedFiles...... %d\n",dev->nDeletedFiles);
1792         buf +=sprintf(buf,"nUnlinkedFiles..... %d\n",dev->nUnlinkedFiles);
1793         buf +=sprintf(buf,"nBackgroudDeletions %d\n",dev->nBackgroundDeletions);
1794         buf +=sprintf(buf,"useNANDECC......... %d\n",dev->useNANDECC);
1795         buf +=sprintf(buf,"isYaffs2........... %d\n",dev->isYaffs2);
1796
1797         
1798         return buf;     
1799 }
1800
1801 static int  yaffs_proc_read(
1802         char *page,
1803         char **start,
1804         off_t offset,
1805         int count,
1806         int *eof,
1807         void *data
1808         )
1809 {
1810
1811         char my_buffer[3000];
1812         char *buf;
1813         buf = my_buffer;
1814
1815         if (offset > 0) return 0;
1816
1817         /* Fill the buffer and get its length */
1818         buf +=sprintf(buf,"YAFFS built:"__DATE__ " "__TIME__"\n%s\n%s\n", yaffs_fs_c_version,yaffs_guts_c_version);
1819         
1820         if(yaffs_dev) buf = yaffs_dump_dev(buf,yaffs_dev,"yaffs");
1821         if(yaffsram_dev) buf = yaffs_dump_dev(buf,yaffsram_dev,"yaffsram");
1822         
1823
1824         strcpy(page,my_buffer);
1825         return strlen(my_buffer);
1826 }
1827
1828
1829 static int  yaffs_proc_ram_write(
1830         char *page,
1831         char **start,
1832         off_t offset,
1833         int count,
1834         int *eof,
1835         void *data
1836         )
1837 {
1838
1839         printk(KERN_DEBUG "yaffs write size %d\n",count);
1840         return count;
1841 }
1842
1843
1844
1845 // Stuff to handle installation of file systems
1846 struct file_system_to_install
1847 {
1848    struct file_system_type *fst;
1849    int installed;
1850 };
1851
1852 static struct file_system_to_install fs_to_install[] =
1853 {
1854 #ifdef CONFIG_YAFFS_RAM_ENABLED
1855      { &yaffs_ram_fs_type, 0},
1856 #endif
1857 #ifdef CONFIG_YAFFS2_RAM_ENABLED
1858      { &yaffs2_ram_fs_type,0},
1859 #endif
1860 #ifdef CONFIG_YAFFS_MTD_ENABLED
1861      { &yaffs_fs_type,0},
1862 #endif
1863 #ifdef CONFIG_YAFFS2_MTD_ENABLED
1864      { &yaffs2_fs_type,0},
1865 #endif
1866      { NULL,0}
1867 };
1868
1869 static int __init init_yaffs_fs(void)
1870 {
1871         int error = 0;
1872         struct file_system_to_install *fsinst;  
1873         
1874         yaffs_dev = yaffsram_dev = NULL;
1875         
1876    T(YAFFS_TRACE_ALWAYS,("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
1877
1878
1879
1880     /* Install the proc_fs entry */
1881     my_proc_entry = create_proc_read_entry("yaffs",
1882                                            S_IRUGO | S_IFREG,
1883                                            &proc_root,
1884                                            yaffs_proc_read,
1885                                            NULL);
1886     if(!my_proc_entry)
1887     {
1888        return -ENOMEM;
1889     }
1890     
1891     
1892
1893     // Now add the file system entries
1894     
1895     fsinst = fs_to_install;
1896     
1897     while(fsinst->fst && !error)
1898     {
1899       error = register_filesystem(fsinst->fst);
1900       if(!error)
1901       {
1902           fsinst->installed = 1;
1903       }
1904       fsinst++;
1905     }
1906    
1907     // Any errors? uninstall 
1908     if(error)
1909     {
1910             fsinst = fs_to_install;
1911             
1912             while(fsinst->fst)
1913             {
1914               if(fsinst->installed)
1915               {
1916                 unregister_filesystem(fsinst->fst);
1917                 fsinst->installed = 0;
1918               }
1919               fsinst++;
1920             }
1921     }
1922     
1923     return error;
1924 }
1925
1926 static void __exit exit_yaffs_fs(void)
1927 {
1928
1929    struct file_system_to_install *fsinst;
1930    
1931    T(YAFFS_TRACE_ALWAYS,("yaffs " __DATE__ " " __TIME__ " removing. \n"));
1932
1933    remove_proc_entry("yaffs",&proc_root);
1934     
1935     fsinst = fs_to_install;
1936     
1937     while(fsinst->fst)
1938     {
1939       if(fsinst->installed)
1940       {
1941         unregister_filesystem(fsinst->fst);
1942         fsinst->installed = 0;
1943       }
1944       fsinst++;
1945     }
1946
1947 }
1948
1949 module_init(init_yaffs_fs)
1950 module_exit(exit_yaffs_fs)
1951
1952 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
1953 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002,2003,2004");
1954 MODULE_LICENSE("GPL");
1955
1956