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