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