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