*** 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 patches marked NCB.
25  * * Some code bodily lifted from JFFS2.
26  */
27
28
29 #include <linux/config.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/version.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/list.h>
36 #include <linux/fs.h>
37 #include <linux/proc_fs.h>
38 #include <linux/pagemap.h>
39 #include <linux/mtd/mtd.h>
40 #include <linux/interrupt.h>
41 #include <linux/string.h>
42 #include <linux/locks.h>
43
44 #include <asm/uaccess.h>
45
46 #include "yaffs_guts.h"
47
48 #ifdef CONFIG_YAFFS_RAM_ENABLED
49 #include "yaffs_nandemul.h" 
50 // 2 MB of RAM for emulation
51 #define YAFFS_RAM_EMULATION_SIZE  0x200000
52 #endif //CONFIG_YAFFS_RAM_ENABLED
53
54 #ifdef CONFIG_YAFFS_MTD_ENABLED
55 #include <linux/mtd/mtd.h>
56 #include "yaffs_mtdif.h"
57 #endif //CONFIG_YAFFS_MTD_ENABLED
58
59 #define T(x) printk x
60
61
62
63 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
64 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
65 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
66
67
68
69 static void yaffs_put_super(struct super_block *sb);
70
71 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos);
72 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos);
73 static int yaffs_file_flush(struct file* file);
74
75 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync);
76
77 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
78
79 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
80 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry);
81 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry);
82 static int yaffs_unlink(struct inode * dir, struct dentry *dentry);
83 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname);
84 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode);
85 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev);
86 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry);
87 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
88
89 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
90 static void yaffs_read_inode (struct inode *inode);
91 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);
92 static void yaffs_put_inode (struct inode *inode);
93 static int yaffs_readpage(struct file *file, struct page * page);
94
95 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
96 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
97
98 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen);
99 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
100
101
102
103
104
105 static struct address_space_operations yaffs_file_address_operations = {
106         readpage:               yaffs_readpage,
107         prepare_write:  yaffs_prepare_write,
108         commit_write:   yaffs_commit_write
109 };
110
111
112 static struct file_operations yaffs_file_operations = {
113 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
114         read:           generic_file_read,
115         write:          generic_file_write,
116 #else
117         read:           yaffs_file_read,
118         write:          yaffs_file_write,
119 #endif
120         mmap:           generic_file_mmap,
121         flush:          yaffs_file_flush,
122         fsync:          yaffs_sync_object,
123 };
124
125
126 static struct inode_operations yaffs_file_inode_operations = {
127         setattr:        yaffs_setattr,
128 };
129
130
131 struct inode_operations yaffs_symlink_inode_operations =
132 {       
133         readlink:       yaffs_readlink,
134         follow_link:    yaffs_follow_link,
135         setattr:        yaffs_setattr
136 };
137
138 static struct inode_operations yaffs_dir_inode_operations = {
139         create:         yaffs_create,
140         lookup:         yaffs_lookup,
141         link:           yaffs_link,
142         unlink:         yaffs_unlink,   
143         symlink:        yaffs_symlink,
144         mkdir:          yaffs_mkdir,
145         rmdir:          yaffs_unlink,
146         mknod:          yaffs_mknod,
147         rename:         yaffs_rename,
148         setattr:        yaffs_setattr,
149 };
150
151 static struct file_operations yaffs_dir_operations = {
152         read:           generic_read_dir,
153         readdir:        yaffs_readdir,
154         fsync:          yaffs_sync_object,
155 };
156
157
158 static struct super_operations yaffs_super_ops = {
159         statfs:                 yaffs_statfs,
160         read_inode:             yaffs_read_inode,
161         put_inode:              yaffs_put_inode,
162         put_super:              yaffs_put_super,
163 //      read_inode:
164 //      remount_fs:
165 //      clear_inode:
166 };
167
168
169
170
171
172 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen)
173 {
174         unsigned char *alias;
175         int ret;
176
177         alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
178         
179         if(!alias)
180                 return -ENOMEM;
181
182         ret = vfs_readlink(dentry, buffer, buflen, alias);
183         kfree(alias);
184         return ret;
185 }
186
187 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
188 {
189         unsigned char *alias;
190         int ret;
191         alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
192         
193         if(!alias)
194                 return -ENOMEM;
195
196         ret = vfs_follow_link(nd,alias);
197         kfree(alias);
198         return ret;
199 }
200
201
202 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj);
203
204 /*
205  * Lookup is used to find objects in the fs
206  */
207 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
208 {
209         yaffs_Object *obj;
210         struct inode *inode;
211         
212         
213         T((KERN_DEBUG"yaffs_lookup for %d:%s\n",yaffs_InodeToObject(dir)->objectId,dentry->d_name.name));
214         
215         obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),dentry->d_name.name);
216         
217         obj = yaffs_GetEquivalentObject(obj); // in case it was a hardlink
218         
219         if(obj)
220         {
221                 T((KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
222                 
223                 inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj);
224                 
225                 if(inode)
226                 {
227                         T((KERN_DEBUG"yaffs_loookup looks good\n"));
228                         dget(dentry); // try to solve directory bug
229                         d_add(dentry,inode);
230                         return dentry;
231                 }
232
233         }
234         else
235         {
236                 T((KERN_DEBUG"yaffs_lookup not found\n"));
237                 
238         }
239         return NULL;
240         
241 }
242
243 // For now put inode is just for debugging
244 static void yaffs_put_inode(struct inode *inode)
245 {
246         T(("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count)));
247         
248         // yaffs_FlushFile(yaffs_InodeToObject(inode));
249         
250 }
251
252
253 static int yaffs_file_flush(struct file* file)
254 {
255         yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
256         
257         T((KERN_DEBUG"yaffs_file_flush object %d (%s)\n",obj->objectId,
258                                 obj->dirty ? "dirty" : "clean"));
259
260     yaffs_FlushFile(obj);
261
262     return 0;
263 }
264
265
266
267 static int yaffs_readpage_nolock(struct file *f, struct page * pg)
268 {
269         // Lifted from jffs2
270         
271         yaffs_Object *obj;
272         unsigned char *pg_buf;  
273         int ret;
274         
275         T((KERN_DEBUG"yaffs_readpage at %08x, size %08x\n", 
276                       pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE));
277
278         obj  = yaffs_DentryToObject(f->f_dentry);
279
280         //down(obj->sem);
281         
282         if (!PageLocked(pg))
283                 PAGE_BUG(pg);
284
285         pg_buf = kmap(pg);
286         /* FIXME: Can kmap fail? */
287
288         ret = yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
289
290         if(ret >= 0) ret = 0;
291
292         if (ret) {
293                 ClearPageUptodate(pg);
294                 SetPageError(pg);
295         } else {
296                 SetPageUptodate(pg);
297                 ClearPageError(pg);
298         }
299
300         flush_dcache_page(pg);
301         kunmap(pg);
302
303
304
305         //up(&obj->sem);
306
307         T((KERN_DEBUG"yaffs_readpage done\n"));
308         return ret;
309 }
310
311 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
312 {
313         int ret = yaffs_readpage_nolock(f,pg);
314         UnlockPage(pg);
315         return ret;
316 }
317
318 static int yaffs_readpage(struct file *f, struct page * pg)
319 {
320         return yaffs_readpage_unlock(f,pg);
321 }
322
323
324 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
325 {
326
327         T((KERN_DEBUG"yaffs_prepair_write\n"));
328         if(!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
329                 return  yaffs_readpage_nolock(f,pg);    
330
331         return 0;
332         
333 }
334
335 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
336 {
337
338         void *addr = page_address(pg) + offset;
339         loff_t pos = (((loff_t)pg->index) << PAGE_CACHE_SHIFT) + offset;
340         int nBytes = to - offset;
341         int nWritten;
342         
343         unsigned spos = pos;
344         unsigned saddr = addr;
345
346         T((KERN_DEBUG"yaffs_commit_write addr %x pos %x nBytes %d\n",saddr,spos,nBytes));
347         
348         nWritten = yaffs_file_write(f,addr, nBytes, &pos);
349         
350         if(nWritten != nBytes)
351         {
352                 T((KERN_DEBUG"yaffs_commit_write not same size nWritten %d  nBytes %d\n",nWritten,nBytes));
353                 SetPageError(pg);
354                 ClearPageUptodate(pg);
355         }
356         else
357         {
358                 SetPageUptodate(pg);
359         }
360
361         T((KERN_DEBUG"yaffs_commit_write returning %d\n",nWritten));
362         
363         return nWritten;
364
365 }
366
367
368
369 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
370 {
371         if (inode && obj) 
372         {
373                 inode->i_ino = obj->objectId;
374                 inode->i_mode = obj->st_mode;
375                 inode->i_uid = obj->st_uid;
376                 inode->i_gid = obj->st_gid;
377                 inode->i_blksize = inode->i_sb->s_blocksize;
378                 inode->i_blocks = 0;
379                 inode->i_rdev = obj->st_rdev;;
380                 inode->i_atime = obj->st_atime;
381                 inode->i_mtime = obj->st_mtime;
382                 inode->i_ctime = obj->st_ctime;
383                 inode->i_size = yaffs_GetObjectFileLength(obj);
384                 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
385                 
386                 T((KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
387                                 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
388                 
389                 switch (obj->st_mode & S_IFMT) 
390                 {
391                         default: // fifo, device or socket
392                                 init_special_inode(inode, obj->st_mode, obj->st_rdev);
393                                 break;
394                         case S_IFREG:   // file         
395                                 inode->i_op = &yaffs_file_inode_operations;
396                                 inode->i_fop = &yaffs_file_operations;
397                                 inode->i_mapping->a_ops = &yaffs_file_address_operations;
398                                 break;
399                         case S_IFDIR:   // directory
400                                 inode->i_op = &yaffs_dir_inode_operations;
401                                 inode->i_fop = &yaffs_dir_operations;
402                                 break;
403                         case S_IFLNK:   // symlink
404                                 inode->i_op = &yaffs_symlink_inode_operations;
405                                 break;
406                 }
407                 
408                 
409                 inode->u.generic_ip = obj;
410                 
411         }
412         else
413         {
414                 T((KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
415         }
416
417 }
418
419 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
420 {
421         struct inode * inode;
422         
423         T((KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
424
425         inode = iget(sb,obj->objectId);
426
427         // No need to call this since iget calls it via the read_inode callback
428         // yaffs_FillInodeFromObject(inode,obj);
429
430         return inode;
431 }
432
433 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
434 {
435         yaffs_Object *obj;
436         int nRead,ipos;
437         struct inode *inode;
438         
439         T((KERN_DEBUG"yaffs_file_read\n"));
440
441         obj  = yaffs_DentryToObject(f->f_dentry);
442         inode = f->f_dentry->d_inode;
443         
444         if (*pos < inode->i_size) 
445         {
446                         if (*pos + n > inode->i_size)
447                         {
448                                 n = inode->i_size - *pos;
449                         }
450         }
451         else
452         {
453                 n = 0;
454         }
455         
456         nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
457         if(nRead > 0)
458         {
459                 f->f_pos += nRead;
460         }
461         ipos = *pos;
462         T((KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
463         return nRead;
464         
465 }
466
467
468 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
469 {
470         yaffs_Object *obj;
471         int nWritten,ipos;
472         struct inode *inode;
473         
474         obj  = yaffs_DentryToObject(f->f_dentry);
475         inode = f->f_dentry->d_inode;
476
477         if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
478         {
479                 ipos = inode->i_size;
480         }
481         else
482         {
483                 ipos = *pos;
484         }
485         
486         
487         if(!obj)
488         {
489                 T((KERN_DEBUG"yaffs_file_write: hey obj is null!\n"));
490         }
491         else
492         {
493                 T((KERN_DEBUG"yaffs_file_write about to write writing %d bytes to object %d at %d\n",n,obj->objectId,ipos));
494         }
495
496         nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n);
497
498         T((KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
499         if(nWritten > 0)
500         {
501                 ipos += nWritten;
502                 *pos = ipos;
503                 if(ipos > inode->i_size)
504                 {
505                         inode->i_size = ipos;
506                         inode->i_blocks = (ipos + inode->i_blksize - 1)/ inode->i_blksize;
507                         
508                         T((KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,inode->i_blocks));
509                 }
510                 
511         }
512         return nWritten != n ? -ENOSPC : nWritten;      
513 }
514
515
516 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
517 {
518         yaffs_Object *obj;
519         struct inode *inode = f->f_dentry->d_inode;
520         unsigned long offset, curoffs;
521         struct list_head *i;    
522         yaffs_Object *l;
523         
524         char name[YAFFS_MAX_NAME_LENGTH +1];
525                 
526         obj =  yaffs_DentryToObject(f->f_dentry);
527         
528         offset = f->f_pos;
529         
530         T(("yaffs_readdir: starting at %d\n",(int)offset));
531         
532         if(offset == 0)
533         {
534         T((KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
535                 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
536                 {
537                         goto out;
538                 }
539                 offset++;
540                 f->f_pos++;
541         }
542         if(offset == 1)
543         {
544                 T((KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
545                 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
546                 {
547                         goto out;
548                 }
549                 offset++;
550                 f->f_pos++;
551         }
552         
553         curoffs = 1;
554         
555         //down(&obj->sem);
556
557         
558         list_for_each(i,&obj->variant.directoryVariant.children)
559         {
560                 curoffs++;
561                 if(curoffs >= offset)
562                 {               
563                         l = list_entry(i, yaffs_Object,siblings);
564                         
565                         yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1); 
566                         T((KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
567                         
568                         if(filldir(dirent,
569                                            name,
570                                            strlen(name),
571                                            offset,
572                                            yaffs_GetObjectInode(l),
573                                            yaffs_GetObjectType(l))
574                                            < 0)
575                         {
576                                 goto up_and_out;
577                         }
578                         
579                         offset++;
580                         f->f_pos++;        
581                 }
582         }
583
584   up_and_out:
585
586         //up(&obj->sem);
587         
588   out:
589         return 0;
590 }
591
592
593 /*
594  * File creation. Allocate an inode, and we're done..
595  */
596 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
597 {
598         struct inode *inode;
599         
600         yaffs_Object *obj = NULL;
601         yaffs_Object *parent = yaffs_InodeToObject(dir);
602         
603         int error = -ENOSPC;
604
605         if(parent)
606         {
607                 T((KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
608                                          parent->objectId,parent->variantType));
609         }
610         else
611         {
612                 T((KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
613                 return -EPERM;
614         }
615         
616         T(("yaffs_mknod: making oject for %s, mode %x dev %x\n",
617                                 dentry->d_name.name, mode,dev));
618
619         switch (mode & S_IFMT) 
620         {
621                 default:
622                         // Special (socket, fifo, device...)
623                         T((KERN_DEBUG"yaffs_mknod: making special\n"));
624                         obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,dev);
625                         break;
626                 case S_IFREG:   // file         
627                         T((KERN_DEBUG"yaffs_mknod: making file\n"));
628                         obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
629                         break;
630                 case S_IFDIR:   // directory
631                         T((KERN_DEBUG"yaffs_mknod: making directory\n"));
632                         obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
633                         break;
634                 case S_IFLNK:   // symlink
635                         T((KERN_DEBUG"yaffs_mknod: making file\n"));
636                         obj = NULL; // Do we ever get here?
637                         break;
638         }
639         
640         if(obj)
641         {
642                 inode = yaffs_get_inode(dir->i_sb, mode, dev, obj);
643                 d_instantiate(dentry, inode);
644                 T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
645                 error = 0;
646         }
647         else
648         {
649                 T((KERN_DEBUG"yaffs_mknod failed making object\n"));
650                 error = -ENOMEM;
651         }
652         
653
654         return error;
655 }
656
657 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
658 {
659         int retVal;
660         T((KERN_DEBUG"yaffs_mkdir\n"));
661         retVal =  yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
662 #if 0
663  // attempt to fix dir bug - didn't work
664         if(!retVal)
665         {
666                 dget(dentry);
667         }
668 #endif
669         return retVal;
670 }
671
672 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
673 {
674         T((KERN_DEBUG"yaffs_create\n"));
675         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
676 }
677
678
679 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
680 {
681         
682         T((KERN_DEBUG"yaffs_unlink %d:%s\n",dir->i_ino,dentry->d_name.name));
683         
684         if(yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name) == YAFFS_OK)
685         {
686                 return 0;
687         }
688         else
689         {
690                 return -ENOTEMPTY;
691         }
692 }
693
694
695 /*
696  * Create a link...
697  */
698 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
699 {
700         struct inode *inode = old_dentry->d_inode;
701         yaffs_Object *obj = NULL;
702         yaffs_Object *link=NULL;
703         
704         T((KERN_DEBUG"yaffs_link\n"));
705         
706         obj = yaffs_InodeToObject(inode);
707         
708         link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
709         
710         if(link)
711         {
712                 return 0;
713         }
714         
715         
716         return -EPERM; 
717 }
718
719
720 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
721 {
722         yaffs_Object *obj;
723         
724         T((KERN_DEBUG"yaffs_symlink\n"));
725         
726         obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name, 
727                                                          S_IFLNK | S_IRWXUGO, current->uid, current->gid,
728                                                          symname);
729
730         if(obj)
731         {
732                 T((KERN_DEBUG"symlink created OK\n"));
733                 return 0;
734         }
735         else
736         {
737                 T((KERN_DEBUG"symlink not created\n"));
738
739         }
740         
741         return -ENOMEM;
742 }
743
744 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
745 {
746
747         T((KERN_DEBUG"yaffs_sync_object\n"));
748         yaffs_FlushFile(yaffs_DentryToObject(dentry));
749         return 0;
750 }
751
752 /*
753  * The VFS layer already does all the dentry stuff for rename.
754  */
755 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
756 {
757         
758         
759         if( yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
760                                yaffs_InodeToObject(new_dir),new_dentry->d_name.name) == YAFFS_OK)
761         {
762                 return 0;
763         }
764         else
765         {
766                 return -ENOTEMPTY;
767         }
768         
769
770 }
771
772 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
773 {
774         struct inode *inode = dentry->d_inode;
775         int error;
776         
777         T((KERN_DEBUG"yaffs_setattr of object %d\n",yaffs_InodeToObject(inode)->objectId));
778         
779         if((error = inode_change_ok(inode,attr)) == 0)
780         {
781         
782                 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
783                 {
784                         error = 0;
785                 }
786                 else
787                 {
788                         error = -EPERM;
789                 }
790
791                 inode_setattr(inode,attr);
792         }
793         return error;
794 }
795
796 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
797 {
798         T((KERN_DEBUG"yaffs_statfs\n"));
799
800         buf->f_type = YAFFS_MAGIC;
801         buf->f_bsize = sb->s_blocksize;
802         buf->f_namelen = 255;
803         buf->f_blocks = yaffs_SuperToDevice(sb)->nBlocks * YAFFS_CHUNKS_PER_BLOCK/
804                                                 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
805         buf->f_files = 0;
806         buf->f_ffree = 0;
807         buf->f_bfree = yaffs_GetNumberOfFreeChunks(yaffs_SuperToDevice(sb))/
808                                                 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
809         buf->f_bavail =  buf->f_bfree;
810         return 0;
811 }
812
813 static void yaffs_read_inode (struct inode *inode)
814 {
815
816         yaffs_Object *obj ; 
817         
818         T((KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
819
820         obj  = yaffs_FindObjectByNumber(yaffs_SuperToDevice(inode->i_sb),inode->i_ino);
821         
822         yaffs_FillInodeFromObject(inode,obj);
823 }
824
825
826 static void yaffs_put_super(struct super_block *sb)
827 {
828         yaffs_Device *dev = yaffs_SuperToDevice(sb);
829         
830         if(dev->putSuperFunc)
831         {
832                  dev->putSuperFunc(sb);
833         }
834         yaffs_Deinitialise(dev);
835         kfree(dev);
836 }
837
838
839 #ifdef CONFIG_YAFFS_MTD_ENABLED
840
841 static void  yaffs_MTDPutSuper(struct super_block *sb)
842 {
843         
844         struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
845         
846         if(mtd->sync)
847         {
848                 mtd->sync(mtd);
849         }
850         
851         put_mtd_device(mtd);
852 }
853
854 #endif
855
856 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
857 {
858         struct inode * inode;
859         struct dentry * root;
860         yaffs_Device *dev;
861         
862         sb->s_magic = YAFFS_MAGIC;
863         sb->s_op = &yaffs_super_ops;
864         
865         if(!sb)
866                 printk(KERN_INFO"yaffs: sb is NULL\n");
867         else if(!sb->s_dev)
868                 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
869         else if(! kdevname(sb->s_dev))
870                 printk(KERN_INFO"yaffs: kdevname is NULL\n");
871         else
872                 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
873
874         
875
876 #ifdef CONFIG_YAFFS_USE_CHUNK_SIZE
877         sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
878         sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
879 #else
880         sb->s_blocksize = PAGE_CACHE_SIZE;
881         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
882 #endif
883         T(("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",sb->s_blocksize));
884
885 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
886         T(("yaffs: Write verification disabled. All guarantees null and void\n");
887 #endif
888
889
890         
891         if(useRam)
892         {
893
894 #ifdef CONFIG_YAFFS_RAM_ENABLED
895                 // Set the yaffs_Device up for ram emulation
896
897                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
898                 if(!dev)
899                 {
900                         // Deep shit could not allocate device structure
901                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
902                         return NULL;
903                 }
904
905                 memset(dev,0,sizeof(yaffs_Device));
906                 dev->genericDevice = NULL; // Not used for RAM emulation.
907
908                 dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
909                 dev->startBlock = 1;  // Don't use block 0
910                 dev->endBlock = dev->nBlocks - 1;
911
912                 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
913                 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
914                 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
915                 dev->initialiseNAND = nandemul_InitialiseNAND;
916                 
917 #endif
918
919         }
920         else
921         {       
922 #ifdef CONFIG_YAFFS_MTD_ENABLED
923                 struct mtd_info *mtd;
924                 
925                 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
926                  MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
927                         
928                 // Hope it's a NAND mtd
929                 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
930                 if (!mtd) 
931                 {
932                         printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
933                         return NULL;
934                 }
935                 
936                 if(mtd->type != MTD_NANDFLASH)
937                 {
938                         printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
939                         return NULL;
940                 }
941
942                 printk(KERN_DEBUG" erase %x\n",mtd->erase);
943                 printk(KERN_DEBUG" read %x\n",mtd->read);
944                 printk(KERN_DEBUG" write %x\n",mtd->write);
945                 printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
946                 printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
947                 printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
948                 printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
949
950
951                 if(!mtd->erase ||
952                    !mtd->read  ||
953                    !mtd->write ||
954                    !mtd->read_oob ||
955                    !mtd->write_oob)
956                 {
957                         printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
958                         return NULL;
959                 }
960                 
961                 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
962                    mtd->oobsize != YAFFS_BYTES_PER_SPARE)
963                 {
964                         printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
965                         return NULL;
966                 }
967                    
968
969                 // OK, so if we got here, we have an MTD that's NAND and looks 
970                 // like it has the right capabilities
971                 // Set the yaffs_Device up for ram emulation
972
973                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
974                 if(!dev)
975                 {
976                         // Deep shit could not allocate device structure
977                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
978                         return NULL;
979                 }
980
981                 memset(dev,0,sizeof(yaffs_Device));
982                 dev->genericDevice = mtd; 
983
984                 // Set up the memory size parameters....
985                 
986 // NCB          dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
987                 dev->nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
988                 dev->startBlock = 1;  // Don't use block 0
989                 dev->endBlock = dev->nBlocks - 1;
990
991                 // ... and the functions.
992                 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
993                 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
994                 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
995                 dev->initialiseNAND = nandmtd_InitialiseNAND;
996                 
997                 dev->putSuperFunc = yaffs_MTDPutSuper;
998 #endif
999         }
1000
1001
1002
1003         yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
1004         T(("yaffs_read_super: guts initialised\n"));
1005
1006         // Create root inode
1007         inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
1008         if (!inode)
1009                 return NULL;
1010
1011         T(("yaffs_read_super: got root inode\n"));
1012                 
1013
1014         root = d_alloc_root(inode);
1015
1016         T(("yaffs_read_super: d_alloc_root done\n"));
1017
1018         if (!root) {
1019                 iput(inode);
1020                 return NULL;
1021         }
1022         sb->s_root = root;
1023
1024         T(("yaffs_read_super: done\n"));
1025         return sb;
1026 }
1027
1028 #ifdef CONFIG_YAFFS_MTD_ENABLED
1029 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
1030 {
1031         return yaffs_internal_read_super(0,sb,data,silent);
1032 }
1033
1034 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1035 #endif
1036
1037 #ifdef CONFIG_YAFFS_RAM_ENABLED
1038
1039 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
1040 {
1041         return yaffs_internal_read_super(1,sb,data,silent);
1042 }
1043
1044 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
1045 #endif // CONFIG_YAFFS_RAM_ENABLED
1046
1047
1048 static struct proc_dir_entry *my_proc_entry;
1049
1050
1051 static int  yaffs_proc_read(
1052         char *page,
1053         char **start,
1054         off_t offset,
1055         int count,
1056         int *eof,
1057         void *data
1058         )
1059 {
1060
1061         static char my_buffer[1000];
1062
1063         if (offset > 0) return 0;
1064
1065         /* Fill the buffer and get its length */
1066         sprintf( my_buffer, 
1067                 "YAFFS built:"__DATE__ " "__TIME__"\n"
1068                 
1069         );
1070
1071         strcpy(page,my_buffer);
1072         return strlen(my_buffer);
1073 }
1074
1075 static int __init init_yaffs_fs(void)
1076 {
1077         int error = 0;
1078         
1079         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
1080 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
1081         printk(KERN_DEBUG "yaffs is using generic read/write (caching)\n");
1082 #else
1083         printk(KERN_DEBUG "yaffs is using direct read/write (uncached)\n");
1084 #endif
1085
1086
1087     /* Install the proc_fs entry */
1088     my_proc_entry = create_proc_read_entry("yaffs",
1089                                            S_IRUGO | S_IFREG,
1090                                            &proc_root,
1091                                            yaffs_proc_read,
1092                                            NULL);
1093     if(!my_proc_entry)
1094     {
1095        return -ENOMEM;
1096     }
1097
1098 #ifdef CONFIG_YAFFS_RAM_ENABLED
1099
1100     error = register_filesystem(&yaffs_ram_fs_type);
1101     if(error)
1102     {
1103         return error;
1104     }
1105 #endif //CONFIG_YAFFS_RAM_ENABLED
1106
1107 #ifdef CONFIG_YAFFS_MTD_ENABLED
1108         error = register_filesystem(&yaffs_fs_type);
1109         if(error)
1110         {
1111 #ifdef CONFIG_YAFFS_RAM_ENABLED
1112                 unregister_filesystem(&yaffs_ram_fs_type);
1113 #endif //CONFIG_YAFFS_RAM_ENABLED
1114         }
1115 #endif // CONFIG_YAFFS_MTD_ENABLED
1116
1117         return error;
1118 }
1119
1120 static void __exit exit_yaffs_fs(void)
1121 {
1122         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
1123
1124     remove_proc_entry("yaffs",&proc_root);
1125     
1126 #ifdef CONFIG_YAFFS_RAM_ENABLED
1127         unregister_filesystem(&yaffs_ram_fs_type);
1128 #endif
1129 #ifdef CONFIG_YAFFS_MTD_ENABLED
1130         unregister_filesystem(&yaffs_fs_type);
1131 #endif
1132
1133 }
1134
1135 module_init(init_yaffs_fs)
1136 module_exit(exit_yaffs_fs)
1137
1138 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
1139 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
1140 MODULE_LICENSE("GPL");
1141