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