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