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