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