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