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