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