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