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