*** 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.30 2003-09-20 01:13:48 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 #if defined(CONFIG_KERNEL_2_5)
527                 inode->i_rdev = to_kdev_t(obj->st_rdev);
528                 inode->i_atime.tv_sec = (time_t)(obj->st_atime);
529                 inode->i_atime.tv_nsec = 0;
530                 inode->i_mtime.tv_sec = (time_t)obj->st_mtime;
531                 inode->i_mtime.tv_nsec =0;
532                 inode->i_ctime.tv_sec = (time_t)obj->st_ctime;
533                 inode->i_ctime.tv_nsec = 0;
534 #else
535                 inode->i_rdev = obj->st_rdev;
536                 inode->i_atime = obj->st_atime;
537                 inode->i_mtime = obj->st_mtime;
538                 inode->i_ctime = obj->st_ctime;
539 #endif
540                 inode->i_size = yaffs_GetObjectFileLength(obj);
541                 inode->i_blocks = (inode->i_size + 511) >> 9;
542
543                 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
544                 
545                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
546                                 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
547                 
548                 switch (obj->st_mode & S_IFMT) 
549                 {
550                         default: // fifo, device or socket
551                                 init_special_inode(inode, obj->st_mode,(dev_t)(obj->st_rdev));
552                                 break;
553                         case S_IFREG:   // file         
554                                 inode->i_op = &yaffs_file_inode_operations;
555                                 inode->i_fop = &yaffs_file_operations;
556                                 inode->i_mapping->a_ops = &yaffs_file_address_operations;
557                                 break;
558                         case S_IFDIR:   // directory
559                                 inode->i_op = &yaffs_dir_inode_operations;
560                                 inode->i_fop = &yaffs_dir_operations;
561                                 break;
562                         case S_IFLNK:   // symlink
563                                 inode->i_op = &yaffs_symlink_inode_operations;
564                                 break;
565                 }
566                 
567                 
568                 inode->u.generic_ip = obj;
569                 obj->myInode = inode;
570                 
571         }
572         else
573         {
574                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
575         }
576
577 }
578
579 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
580 {
581         struct inode * inode;
582         
583         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
584
585         inode = iget(sb,obj->objectId);
586
587         // NB Side effect: iget calls back to yaffs_read_inode().
588         // iget also increments the inode's i_count
589         
590         return inode;
591 }
592
593 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
594 {
595         yaffs_Object *obj;
596         int nRead,ipos;
597         struct inode *inode;
598         yaffs_Device *dev;
599         
600         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_read\n"));
601
602         obj  = yaffs_DentryToObject(f->f_dentry);
603         
604         dev = obj->myDev;
605         
606         yaffs_GrossLock(dev);
607         
608         inode = f->f_dentry->d_inode;
609         
610         if (*pos < inode->i_size) 
611         {
612                         if (*pos + n > inode->i_size)
613                         {
614                                 n = inode->i_size - *pos;
615                         }
616         }
617         else
618         {
619                 n = 0;
620         }
621         
622         nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
623         if(nRead > 0)
624         {
625                 f->f_pos += nRead;
626         }
627         
628         yaffs_GrossUnlock(dev);
629         
630         ipos = *pos;
631         
632         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
633         return nRead;
634         
635 }
636
637
638 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
639 {
640         yaffs_Object *obj;
641         int nWritten,ipos;
642         struct inode *inode;
643         yaffs_Device *dev;
644         
645         
646         obj  = yaffs_DentryToObject(f->f_dentry);
647         
648         dev = obj->myDev;
649         
650         yaffs_GrossLock(dev);
651         
652         inode = f->f_dentry->d_inode;
653
654         if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
655         {
656                 ipos = inode->i_size;
657         }
658         else
659         {
660                 ipos = *pos;
661         }
662         
663         
664         if(!obj)
665         {
666                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write: hey obj is null!\n"));
667         }
668         else
669         {
670                 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));
671         }
672
673         nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n);
674
675         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
676         if(nWritten > 0)
677         {
678                 ipos += nWritten;
679                 *pos = ipos;
680                 if(ipos > inode->i_size)
681                 {
682                         inode->i_size = ipos;
683                         inode->i_blocks = (ipos + 511)>>9;
684                         
685                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,(int)(inode->i_blocks)));
686                 }
687                 
688         }
689         yaffs_GrossUnlock(dev);
690         
691         return nWritten != n ? -ENOSPC : nWritten;      
692 }
693
694
695 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
696 {
697         yaffs_Object *obj;
698         yaffs_Device *dev;
699         struct inode *inode = f->f_dentry->d_inode;
700         unsigned long offset, curoffs;
701         struct list_head *i;    
702         yaffs_Object *l;
703         
704         char name[YAFFS_MAX_NAME_LENGTH +1];
705                 
706         obj =  yaffs_DentryToObject(f->f_dentry);
707         dev = obj->myDev;
708         
709         yaffs_GrossLock(dev);
710         
711         offset = f->f_pos;
712         
713         T(YAFFS_TRACE_OS,("yaffs_readdir: starting at %d\n",(int)offset));
714         
715         if(offset == 0)
716         {
717         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
718                 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
719                 {
720                         goto out;
721                 }
722                 offset++;
723                 f->f_pos++;
724         }
725         if(offset == 1)
726         {
727                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
728                 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
729                 {
730                         goto out;
731                 }
732                 offset++;
733                 f->f_pos++;
734         }
735         
736         curoffs = 1;
737         
738         list_for_each(i,&obj->variant.directoryVariant.children)
739         {
740                 curoffs++;
741                 if(curoffs >= offset)
742                 {               
743                         l = list_entry(i, yaffs_Object,siblings);
744                         
745                         yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1); 
746                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
747                         
748                         if(filldir(dirent,
749                                            name,
750                                            strlen(name),
751                                            offset,
752                                            yaffs_GetObjectInode(l),
753                                            yaffs_GetObjectType(l))
754                                            < 0)
755                         {
756                                 goto up_and_out;
757                         }
758                         
759                         offset++;
760                         f->f_pos++;        
761                 }
762         }
763
764   up_and_out:
765   out:
766   
767     yaffs_GrossUnlock(dev);
768     
769         return 0;
770 }
771
772
773 /*
774  * File creation. Allocate an inode, and we're done..
775  */
776 #if defined(CONFIG_KERNEL_2_5)
777 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
778 #else
779 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
780 #endif
781 {
782         struct inode *inode;
783         
784         yaffs_Object *obj = NULL;
785         yaffs_Device *dev;
786         
787         yaffs_Object *parent = yaffs_InodeToObject(dir);
788         
789         int error = -ENOSPC;
790
791         if(parent)
792         {
793                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
794                                          parent->objectId,parent->variantType));
795         }
796         else
797         {
798                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
799                 return -EPERM;
800         }
801         
802         T(YAFFS_TRACE_OS,("yaffs_mknod: making oject for %s, mode %x dev %x\n",
803                                         dentry->d_name.name, mode,rdev));
804
805         dev = parent->myDev;
806         
807         yaffs_GrossLock(dev);
808
809         switch (mode & S_IFMT) 
810         {
811                 default:
812                         // Special (socket, fifo, device...)
813                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making special\n"));
814                         obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,rdev);
815                         break;
816                 case S_IFREG:   // file         
817                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
818                         obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
819                         break;
820                 case S_IFDIR:   // directory
821                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making directory\n"));
822                         obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
823                         break;
824                 case S_IFLNK:   // symlink
825                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
826                         obj = NULL; // Do we ever get here?
827                         break;
828         }
829         
830         if(obj)
831         {
832                 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
833                 d_instantiate(dentry, inode);
834                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
835                 error = 0;
836         }
837         else
838         {
839                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod failed making object\n"));
840                 error = -ENOMEM;
841         }
842
843         yaffs_GrossUnlock(dev);
844
845         return error;
846 }
847
848 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
849 {
850         int retVal;
851         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mkdir\n"));
852         retVal =  yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
853 #if 0
854  // attempt to fix dir bug - didn't work
855         if(!retVal)
856         {
857                 dget(dentry);
858         }
859 #endif
860         return retVal;
861 }
862
863 #if defined(CONFIG_KERNEL_2_5)  /* Added NCB 185-8-2003 */
864 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *n)
865 #else
866 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
867 #endif
868 {
869         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_create\n"));
870         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
871 }
872
873
874 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
875 {
876         int retVal;
877         
878         yaffs_Device *dev;
879         
880         
881         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_unlink %d:%s\n",(int)(dir->i_ino),dentry->d_name.name));
882         
883         dev = yaffs_InodeToObject(dir)->myDev;
884         
885         yaffs_GrossLock(dev);
886         
887         
888         retVal = yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name);
889         
890         
891         yaffs_GrossUnlock(dev);
892         
893         if( retVal == YAFFS_OK)
894         {
895                 dentry->d_inode->i_nlink--;
896                 mark_inode_dirty(dentry->d_inode);
897                 return 0;
898         }
899         else
900         {
901                 return -ENOTEMPTY;
902         }
903 }
904
905
906 /*
907  * Create a link...
908  */
909 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
910 {
911         struct inode *inode = old_dentry->d_inode;
912         yaffs_Object *obj = NULL;
913         yaffs_Object *link=NULL;
914         yaffs_Device *dev;
915         
916         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link\n"));
917         
918         obj = yaffs_InodeToObject(inode);
919         dev = obj->myDev;
920         
921         yaffs_GrossLock(dev);
922
923         if (!S_ISDIR(inode->i_mode))    // Don't link directories
924         {
925                 link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
926         }
927         
928
929         if(link)
930         {
931                 old_dentry->d_inode->i_nlink =  yaffs_GetObjectLinkCount(obj);
932                 d_instantiate(dentry, old_dentry->d_inode);
933                 atomic_inc(&old_dentry->d_inode->i_count);
934                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link link count %d i_count %d\n",    
935                         old_dentry->d_inode->i_nlink,atomic_read(&old_dentry->d_inode->i_count)));
936         
937         }
938         
939         yaffs_GrossUnlock(dev);
940         
941
942         if(link)
943         {
944         
945                 return 0;
946         }
947         
948         
949         return -EPERM; 
950 }
951
952
953 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
954 {
955         yaffs_Object *obj;
956         yaffs_Device *dev;
957         
958         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_symlink\n"));
959         
960         dev = yaffs_InodeToObject(dir)->myDev;
961         yaffs_GrossLock(dev);
962         obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name, 
963                                                          S_IFLNK | S_IRWXUGO, current->uid, current->gid,
964                                                          symname);
965         yaffs_GrossUnlock(dev);
966
967         if(obj)
968         {
969
970                 struct inode* inode;
971         
972                 inode = yaffs_get_inode(dir->i_sb, obj->st_mode, 0, obj);
973                 d_instantiate(dentry, inode);
974                 T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink created OK\n"));
975                 return 0;
976         }
977         else
978         {
979                 T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink not created\n"));
980
981         }
982         
983         return -ENOMEM;
984 }
985
986 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
987 {
988
989         yaffs_Object *obj;
990         yaffs_Device *dev;
991         
992         obj = yaffs_DentryToObject(dentry);
993
994         dev = obj->myDev;
995         
996         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_sync_object\n"));
997         yaffs_GrossLock(dev);
998         yaffs_FlushFile(obj,1);
999         yaffs_GrossUnlock(dev);
1000         return 0;
1001 }
1002
1003 /*
1004  * The VFS layer already does all the dentry stuff for rename.
1005  *
1006  * NB: POSIX says you can rename an object over an old object of the same name
1007  */
1008 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
1009 {
1010         yaffs_Device *dev;
1011         int retVal = YAFFS_FAIL;
1012         int removed = 0;
1013         yaffs_Object *target;
1014         
1015         dev = yaffs_InodeToObject(old_dir)->myDev;
1016
1017         yaffs_GrossLock(dev);
1018         
1019         // Check if the target is an existing directory that is not empty.
1020         target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1021         
1022         if(target &&
1023            target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1024            !list_empty(&target->variant.directoryVariant.children))
1025         {
1026                 retVal = YAFFS_FAIL;
1027         }
1028         else
1029         {
1030            
1031                 // Unlink the target if it exists
1032                 removed = yaffs_Unlink(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1033
1034         
1035                 retVal =  yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
1036                                                                         yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1037                                                                         
1038         }
1039         yaffs_GrossUnlock(dev);
1040         
1041         if(retVal == YAFFS_OK)
1042         {
1043                 if(removed == YAFFS_OK)
1044                 {
1045                         new_dentry->d_inode->i_nlink--;
1046                         mark_inode_dirty(new_dentry->d_inode);
1047                 }
1048                 
1049                 return 0;
1050         }
1051         else
1052         {
1053                 return -ENOTEMPTY;
1054         }
1055         
1056
1057 }
1058
1059 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1060 {
1061         struct inode *inode = dentry->d_inode;
1062         int error;
1063         yaffs_Device *dev;
1064         
1065         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_setattr of object %d\n",yaffs_InodeToObject(inode)->objectId));
1066         
1067         if((error = inode_change_ok(inode,attr)) == 0)
1068         {
1069         
1070                 dev = yaffs_InodeToObject(inode)->myDev;
1071                 yaffs_GrossLock(dev);
1072                 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
1073                 {
1074                         error = 0;
1075                 }
1076                 else
1077                 {
1078                         error = -EPERM;
1079                 }
1080                 yaffs_GrossUnlock(dev);
1081                 inode_setattr(inode,attr);
1082         }
1083         return error;
1084 }
1085
1086 #if defined(CONFIG_KERNEL_2_5)  /* Added NCB 185-8-2003 */
1087 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1088 #else
1089 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1090 #endif
1091 {
1092         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1093         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_statfs\n"));
1094
1095         yaffs_GrossLock(dev);
1096         
1097         buf->f_type = YAFFS_MAGIC;
1098         buf->f_bsize = sb->s_blocksize;
1099         buf->f_namelen = 255;
1100         buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * YAFFS_CHUNKS_PER_BLOCK/
1101                                                 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
1102         buf->f_files = 0;
1103         buf->f_ffree = 0;
1104         buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev)/
1105                                                 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
1106         buf->f_bavail =  buf->f_bfree;
1107         
1108         yaffs_GrossUnlock(dev);
1109         return 0;
1110 }
1111
1112 static void yaffs_read_inode (struct inode *inode)
1113 {
1114         // NB This is called as a side effect of other functions and
1115         // thus gross locking should always be in place already.
1116         
1117         yaffs_Object *obj ; 
1118         yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1119         
1120         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
1121
1122         obj  = yaffs_FindObjectByNumber(dev,inode->i_ino);
1123         
1124         yaffs_FillInodeFromObject(inode,obj);
1125
1126 }
1127
1128
1129
1130 static yaffs_Device *yaffs_dev;
1131 static yaffs_Device *yaffsram_dev;
1132
1133
1134
1135 static void yaffs_put_super(struct super_block *sb)
1136 {
1137         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1138         
1139         yaffs_GrossLock(dev);
1140         if(dev->putSuperFunc)
1141         {
1142                  dev->putSuperFunc(sb);
1143         }
1144         yaffs_Deinitialise(dev);
1145         yaffs_GrossUnlock(dev);
1146
1147         if(dev == yaffs_dev) yaffs_dev = NULL;
1148         if(dev == yaffsram_dev) yaffsram_dev = NULL;
1149                 
1150         kfree(dev);
1151 }
1152
1153
1154 #ifdef CONFIG_YAFFS_MTD_ENABLED
1155
1156 static void  yaffs_MTDPutSuper(struct super_block *sb)
1157 {
1158         
1159         struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1160         
1161         if(mtd->sync)
1162         {
1163                 mtd->sync(mtd);
1164         }
1165         
1166         put_mtd_device(mtd);
1167 }
1168
1169 #endif
1170
1171
1172 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
1173 {
1174         int nBlocks;
1175         struct inode * inode;
1176         struct dentry * root;
1177         yaffs_Device *dev;
1178         
1179         sb->s_magic = YAFFS_MAGIC;
1180         sb->s_op = &yaffs_super_ops;
1181         
1182         if(!sb)
1183                 printk(KERN_INFO"yaffs: sb is NULL\n");
1184         else if(!sb->s_dev)
1185                 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
1186         else if(! kdevname(sb->s_dev))
1187                 printk(KERN_INFO"yaffs: kdevname is NULL\n");
1188         else
1189                 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
1190
1191         
1192
1193 #ifdef CONFIG_YAFFS_USE_CHUNK_SIZE
1194         sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
1195         sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
1196 #else
1197         sb->s_blocksize = PAGE_CACHE_SIZE;
1198         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1199 #endif
1200         T(YAFFS_TRACE_OS,("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",(int)(sb->s_blocksize)));
1201
1202 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1203         T(YAFFS_TRACE_OS,("yaffs: Write verification disabled. All guarantees null and void\n"));
1204 #endif
1205
1206
1207         
1208         if(useRam)
1209         {
1210
1211 #ifdef CONFIG_YAFFS_RAM_ENABLED
1212                 // Set the yaffs_Device up for ram emulation
1213
1214                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1215                 if(!dev)
1216                 {
1217                         // Deep shit could not allocate device structure
1218                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1219                         return NULL;
1220                 }
1221
1222                 memset(dev,0,sizeof(yaffs_Device));
1223                 dev->genericDevice = NULL; // Not used for RAM emulation.
1224
1225                 nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1226                 dev->startBlock = 1;  // Don't use block 0
1227                 dev->endBlock = nBlocks - 1;
1228                 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1229                 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1230                 dev->nReservedBlocks = 5;
1231
1232                 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
1233                 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
1234                 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
1235                 dev->initialiseNAND = nandemul_InitialiseNAND;
1236
1237                 yaffsram_dev = dev;
1238                 
1239 #endif
1240
1241         }
1242         else
1243         {       
1244 #ifdef CONFIG_YAFFS_MTD_ENABLED
1245                 struct mtd_info *mtd;
1246                 
1247                 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
1248                  MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
1249                         
1250                 // Check it's an mtd device.....
1251                 if(MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
1252                 {
1253                         return NULL; // This isn't an mtd device
1254                 } 
1255                 
1256                 // Get the device
1257                 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1258                 if (!mtd) 
1259                 {
1260                         printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
1261                         return NULL;
1262                 }
1263                 
1264                 // Check it's NAND
1265                 if(mtd->type != MTD_NANDFLASH)
1266                 {
1267                         printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
1268                         return NULL;
1269                 }
1270
1271                 //printk(KERN_DEBUG" erase %x\n",mtd->erase);
1272                 //printk(KERN_DEBUG" read %x\n",mtd->read);
1273                 //printk(KERN_DEBUG" write %x\n",mtd->write);
1274                 //printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
1275                 //printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
1276                 //printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
1277                 //printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
1278
1279
1280                 if(!mtd->erase ||
1281                    !mtd->read  ||
1282                    !mtd->write ||
1283 #ifndef CONFIG_YAFFS_USE_OLD_MTD
1284                    !mtd->write_ecc ||
1285                    !mtd->read_ecc ||
1286 #endif
1287                    !mtd->read_oob ||
1288                    !mtd->write_oob )
1289                 {
1290                         printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
1291                         return NULL;
1292                 }
1293                 
1294                 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
1295                    mtd->oobsize != YAFFS_BYTES_PER_SPARE)
1296                 {
1297                         printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
1298                         return NULL;
1299                 }
1300                    
1301
1302                 // OK, so if we got here, we have an MTD that's NAND and looks 
1303                 // like it has the right capabilities
1304                 // Set the yaffs_Device up for ram emulation
1305
1306 #if defined(CONFIG_KERNEL_2_5)
1307                 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1308 #else
1309                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1310 #endif
1311                 if(!dev)
1312                 {
1313                         // Deep shit could not allocate device structure
1314                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1315                         return NULL;
1316                 }
1317
1318                 memset(dev,0,sizeof(yaffs_Device));
1319                 dev->genericDevice = mtd; 
1320
1321                 // Set up the memory size parameters....
1322                 
1323                 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1324                 dev->startBlock = 1;  // Don't use block 0
1325                 dev->endBlock = nBlocks - 1;
1326                 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1327                 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1328                 dev->nReservedBlocks = 5;
1329                 dev->nShortOpCaches = 10; // Enable short op caching
1330                 
1331
1332                 // ... and the functions.
1333                 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1334                 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1335                 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1336                 dev->initialiseNAND = nandmtd_InitialiseNAND;
1337                                 
1338                 dev->putSuperFunc = yaffs_MTDPutSuper;
1339                 
1340 #ifdef CONFIG_YAFFS_USE_NANDECC
1341                 dev->useNANDECC = 1;
1342 #endif
1343
1344                 yaffs_dev = dev;
1345                 
1346 #endif
1347         }
1348
1349         init_MUTEX(&dev->grossLock);
1350         
1351         
1352         yaffs_GrossLock(dev);
1353         yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
1354
1355         T(YAFFS_TRACE_OS,("yaffs_read_super: guts initialised\n"));
1356
1357         // Create root inode
1358         inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
1359
1360         yaffs_GrossUnlock(dev);
1361
1362         if (!inode)
1363                 return NULL;
1364                 
1365 // added NCB
1366         inode->i_op = & yaffs_dir_inode_operations;
1367         inode->i_fop = & yaffs_dir_operations;
1368
1369         T(YAFFS_TRACE_OS,("yaffs_read_super: got root inode\n"));
1370                 
1371
1372         root = d_alloc_root(inode);
1373
1374         T(YAFFS_TRACE_OS,("yaffs_read_super: d_alloc_root done\n"));
1375
1376         if (!root) {
1377                 iput(inode);
1378                 return NULL;
1379         }
1380         sb->s_root = root;
1381
1382         T(YAFFS_TRACE_OS,("yaffs_read_super: done\n"));
1383         return sb;
1384 }
1385
1386 static int yaffs_internal_read_super_ram(struct super_block * sb, void * data, int silent)
1387 {
1388          return yaffs_internal_read_super(1,sb,data,silent) ? 0 : -1;
1389 }
1390 static int yaffs_internal_read_super_mtd(struct super_block * sb, void * data, int silent)
1391 {
1392          return yaffs_internal_read_super(0,sb,data,silent) ? 0 : -1;
1393 }
1394
1395
1396 #ifdef CONFIG_YAFFS_MTD_ENABLED
1397 #if defined(CONFIG_KERNEL_2_5)
1398 static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1399 {
1400
1401     return get_sb_bdev(fs, flags, dev_name, data, yaffs_internal_read_super_mtd);
1402 }
1403
1404 /* changes NCB 2.5.70 */
1405 //static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1406 static struct file_system_type yaffs_fs_type = {
1407         .owner          = THIS_MODULE,
1408         .name           = "yaffs",
1409         .get_sb         = yaffs_read_super,
1410 //      .kill_sb        = kill_block_super,
1411         .kill_sb        = kill_litter_super,
1412         .fs_flags       = FS_REQUIRES_DEV,
1413 };
1414 #else
1415 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
1416 {
1417         return yaffs_internal_read_super(0,sb,data,silent);
1418 }
1419
1420 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1421 #endif
1422 #endif
1423
1424 #ifdef CONFIG_YAFFS_RAM_ENABLED
1425
1426 #if defined(CONFIG_KERNEL_2_5)
1427 static struct super_block *yaffs_ram_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1428 {
1429
1430     return get_sb_bdev(fs, flags, dev_name, data, yaffs_internal_read_super_ram);
1431 }
1432
1433 /* changes NCB 2.5.70 */
1434 //static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1435 static struct file_system_type yaffs_fs_type = {
1436         .owner          = THIS_MODULE,
1437         .name           = "yaffsram",
1438         .get_sb         = yaffs_ram_read_super,
1439 //      .kill_sb        = kill_block_super,
1440         .kill_sb        = kill_litter_super,
1441         .fs_flags       = FS_SINGLE,
1442 };
1443 #else
1444 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
1445 {
1446         return yaffs_internal_read_super(1,sb,data,silent);
1447 }
1448
1449 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
1450 #endif
1451 #endif // CONFIG_YAFFS_RAM_ENABLED
1452
1453
1454 static struct proc_dir_entry *my_proc_entry;
1455 static struct proc_dir_entry *my_proc_ram_write_entry;
1456
1457 static char * yaffs_dump_dev(char *buf,yaffs_Device *dev,char *name)
1458 {
1459         buf +=sprintf(buf,"\nDevice %s\n",name);
1460         buf +=sprintf(buf,"startBlock......... %d\n",dev->startBlock);
1461         buf +=sprintf(buf,"endBlock........... %d\n",dev->endBlock);
1462         buf +=sprintf(buf,"chunkGroupBits..... %d\n",dev->chunkGroupBits);
1463         buf +=sprintf(buf,"chunkGroupSize..... %d\n",dev->chunkGroupSize);
1464         buf +=sprintf(buf,"nErasedBlocks...... %d\n",dev->nErasedBlocks);
1465         buf +=sprintf(buf,"nTnodesCreated..... %d\n",dev->nTnodesCreated);
1466         buf +=sprintf(buf,"nFreeTnodes........ %d\n",dev->nFreeTnodes);
1467         buf +=sprintf(buf,"nObjectsCreated.... %d\n",dev->nObjectsCreated);
1468         buf +=sprintf(buf,"nFreeObjects....... %d\n",dev->nFreeObjects);
1469         buf +=sprintf(buf,"nFreeChunks........ %d\n",dev->nFreeChunks);
1470         buf +=sprintf(buf,"nPageWrites........ %d\n",dev->nPageWrites);
1471         buf +=sprintf(buf,"nPageReads......... %d\n",dev->nPageReads);
1472         buf +=sprintf(buf,"nBlockErasures..... %d\n",dev->nBlockErasures);
1473         buf +=sprintf(buf,"nGCCopies.......... %d\n",dev->nGCCopies);
1474         buf +=sprintf(buf,"garbageCollections. %d\n",dev->garbageCollections);
1475         buf +=sprintf(buf,"passiveGCs......... %d\n",dev->passiveGarbageCollections);
1476         buf +=sprintf(buf,"nRetriedWrites..... %d\n",dev->nRetriedWrites);
1477         buf +=sprintf(buf,"nRetireBlocks...... %d\n",dev->nRetiredBlocks);
1478         buf +=sprintf(buf,"eccFixed........... %d\n",dev->eccFixed);
1479         buf +=sprintf(buf,"eccUnfixed......... %d\n",dev->eccUnfixed);
1480         buf +=sprintf(buf,"tagsEccFixed....... %d\n",dev->tagsEccFixed);
1481         buf +=sprintf(buf,"tagsEccUnfixed..... %d\n",dev->tagsEccUnfixed);
1482         buf +=sprintf(buf,"cacheHits.......... %d\n",dev->cacheHits);
1483         buf +=sprintf(buf,"nDeletedFiles...... %d\n",dev->nDeletedFiles);
1484         buf +=sprintf(buf,"nUnlinkedFiles..... %d\n",dev->nUnlinkedFiles);
1485         buf +=sprintf(buf,"nBackgroudDeletions %d\n",dev->nBackgroundDeletions);
1486         buf +=sprintf(buf,"useNANDECC......... %d\n",dev->useNANDECC);
1487
1488         
1489         return buf;     
1490 }
1491
1492 static int  yaffs_proc_read(
1493         char *page,
1494         char **start,
1495         off_t offset,
1496         int count,
1497         int *eof,
1498         void *data
1499         )
1500 {
1501
1502         char my_buffer[3000];
1503         char *buf;
1504         buf = my_buffer;
1505
1506         if (offset > 0) return 0;
1507
1508         /* Fill the buffer and get its length */
1509         buf +=sprintf(buf,"YAFFS built:"__DATE__ " "__TIME__"\n%s\n%s\n", yaffs_fs_c_version,yaffs_guts_c_version);
1510         
1511         if(yaffs_dev) buf = yaffs_dump_dev(buf,yaffs_dev,"yaffs");
1512         if(yaffsram_dev) buf = yaffs_dump_dev(buf,yaffsram_dev,"yaffsram");
1513         
1514
1515         strcpy(page,my_buffer);
1516         return strlen(my_buffer);
1517 }
1518
1519
1520 static int  yaffs_proc_ram_write(
1521         char *page,
1522         char **start,
1523         off_t offset,
1524         int count,
1525         int *eof,
1526         void *data
1527         )
1528 {
1529
1530         printk(KERN_DEBUG "yaffs write size %d\n",count);
1531         return count;
1532 }
1533
1534 static int __init init_yaffs_fs(void)
1535 {
1536         int error = 0;
1537         
1538         yaffs_dev = yaffsram_dev = NULL;
1539         
1540         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
1541 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
1542         printk(KERN_DEBUG "yaffs is using generic read/write (caching)\n");
1543 #else
1544         printk(KERN_DEBUG "yaffs is using direct read/write (uncached)\n");
1545 #endif
1546
1547
1548     /* Install the proc_fs entry */
1549     my_proc_entry = create_proc_read_entry("yaffs",
1550                                            S_IRUGO | S_IFREG,
1551                                            &proc_root,
1552                                            yaffs_proc_read,
1553                                            NULL);
1554     if(!my_proc_entry)
1555     {
1556        return -ENOMEM;
1557     }
1558
1559 #ifdef CONFIG_YAFFS_RAM_ENABLED
1560 #if 0
1561     my_proc_ram_write_entry = create_proc_entry("yaffs_ram",
1562                                            S_IRUGO | S_IFREG,
1563                                            &proc_root);
1564  
1565     if(!my_proc_ram_write_entry)
1566     {
1567        return -ENOMEM;
1568     }
1569     else
1570     {
1571         my_proc_ram_write_entry->write_proc = yaffs_proc_ram_write;
1572     }
1573 #endif
1574
1575     error = register_filesystem(&yaffs_ram_fs_type);
1576     if(error)
1577     {
1578         return error;
1579     }
1580 #endif //CONFIG_YAFFS_RAM_ENABLED
1581
1582 #ifdef CONFIG_YAFFS_MTD_ENABLED
1583         error = register_filesystem(&yaffs_fs_type);
1584         if(error)
1585         {
1586 #ifdef CONFIG_YAFFS_RAM_ENABLED
1587                 unregister_filesystem(&yaffs_ram_fs_type);
1588 #endif //CONFIG_YAFFS_RAM_ENABLED
1589         }
1590 #endif // CONFIG_YAFFS_MTD_ENABLED
1591
1592         return error;
1593 }
1594
1595 static void __exit exit_yaffs_fs(void)
1596 {
1597         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
1598
1599     remove_proc_entry("yaffs",&proc_root);
1600     
1601 #ifdef CONFIG_YAFFS_RAM_ENABLED
1602         unregister_filesystem(&yaffs_ram_fs_type);
1603 #endif
1604 #ifdef CONFIG_YAFFS_MTD_ENABLED
1605         unregister_filesystem(&yaffs_fs_type);
1606 #endif
1607
1608 }
1609
1610 module_init(init_yaffs_fs)
1611 module_exit(exit_yaffs_fs)
1612
1613 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
1614 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
1615 MODULE_LICENSE("GPL");
1616