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