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