During scanning, only adjust shrinkSize if if is a shrink header
[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.26 2005-08-01 21:02:22 luc 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         struct inode *inode;
492         unsigned long end_index;
493         char *buffer;
494         yaffs_Object *obj;
495         int nWritten = 0;
496         unsigned nBytes;
497
498         if (!mapping)
499                 BUG();
500         inode = mapping->host;
501         if (!inode)
502                 BUG();
503
504         end_index = inode->i_size >> PAGE_CACHE_SHIFT;
505
506         /* easy case */
507         if (page->index < end_index)
508         {
509                 nBytes = PAGE_CACHE_SIZE;
510         }
511         else
512         {
513                 nBytes = inode->i_size & (PAGE_CACHE_SIZE-1);
514         }
515         //  What's happening here?
516         ///* OK, are we completely out? */
517         //if (page->index >= end_index+1 || !offset)
518         //      return -EIO;
519
520         get_page(page);
521
522
523         buffer = kmap(page);
524
525         obj = yaffs_InodeToObject(inode);
526         yaffs_GrossLock(obj->myDev);
527
528
529         nWritten = yaffs_WriteDataToFile(obj,buffer,page->index << PAGE_CACHE_SHIFT,nBytes,0);
530
531         yaffs_GrossUnlock(obj->myDev);
532         
533         kunmap(page);
534         SetPageUptodate(page);
535         UnlockPage(page);
536         put_page(page);
537
538         return (nWritten == nBytes) ? 0  : -ENOSPC;
539 }
540
541
542
543 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
544 {
545
546         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_prepair_write\n"));
547         if(!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
548                 return  yaffs_readpage_nolock(f,pg);    
549
550         return 0;
551         
552 }
553
554 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
555 {
556
557         void *addr = page_address(pg) + offset;
558         loff_t pos = (((loff_t)pg->index) << PAGE_CACHE_SHIFT) + offset;
559         int nBytes = to - offset;
560         int nWritten;
561         
562         unsigned spos = pos;
563         unsigned saddr = (unsigned)addr;
564
565         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write addr %x pos %x nBytes %d\n",saddr,spos,nBytes));
566         
567         nWritten = yaffs_file_write(f,addr, nBytes, &pos);
568         
569         if(nWritten != nBytes)
570         {
571                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write not same size nWritten %d  nBytes %d\n",nWritten,nBytes));
572                 SetPageError(pg);
573                 ClearPageUptodate(pg);
574         }
575         else
576         {
577                 SetPageUptodate(pg);
578         }
579
580         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write returning %d\n",nWritten));
581         
582         return nWritten;
583
584 }
585
586
587
588 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
589 {
590         if (inode && obj) 
591         {
592                 inode->i_ino = obj->objectId;
593                 inode->i_mode = obj->yst_mode;
594                 inode->i_uid = obj->yst_uid;
595                 inode->i_gid = obj->yst_gid;
596                 inode->i_blksize = inode->i_sb->s_blocksize;
597 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
598
599                 inode->i_rdev = old_decode_dev(obj->yst_rdev);
600                 inode->i_atime.tv_sec = (time_t)(obj->yst_atime);
601                 inode->i_atime.tv_nsec = 0;
602                 inode->i_mtime.tv_sec = (time_t)obj->yst_mtime;
603                 inode->i_mtime.tv_nsec =0;
604                 inode->i_ctime.tv_sec = (time_t)obj->yst_ctime;
605                 inode->i_ctime.tv_nsec = 0;
606 #else
607                 inode->i_rdev = obj->yst_rdev;
608                 inode->i_atime = obj->yst_atime;
609                 inode->i_mtime = obj->yst_mtime;
610                 inode->i_ctime = obj->yst_ctime;
611 #endif
612                 inode->i_size = yaffs_GetObjectFileLength(obj);
613                 inode->i_blocks = (inode->i_size + 511) >> 9;
614
615                 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
616                 
617                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
618                                 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
619                 
620                 switch (obj->yst_mode & S_IFMT) 
621                 {
622                         default: // fifo, device or socket
623 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
624                                init_special_inode(inode, obj->yst_mode,old_decode_dev(obj->yst_rdev));
625 #else
626                                  init_special_inode(inode, obj->yst_mode,(dev_t)(obj->yst_rdev));
627 #endif                          
628                         break;
629                         case S_IFREG:   // file         
630                                 inode->i_op = &yaffs_file_inode_operations;
631                                 inode->i_fop = &yaffs_file_operations;
632                                 inode->i_mapping->a_ops = &yaffs_file_address_operations;
633                                 break;
634                         case S_IFDIR:   // directory
635                                 inode->i_op = &yaffs_dir_inode_operations;
636                                 inode->i_fop = &yaffs_dir_operations;
637                                 break;
638                         case S_IFLNK:   // symlink
639                                 inode->i_op = &yaffs_symlink_inode_operations;
640                                 break;
641                 }
642                 
643                 
644                 inode->u.generic_ip = obj;
645                 obj->myInode = inode;
646                 
647         }
648         else
649         {
650                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
651         }
652
653 }
654
655 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
656 {
657         struct inode * inode;
658         
659         if(!sb)
660         {
661           T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_get_inode for NULL super_block!!\n"));
662           return NULL;
663           
664         }
665         
666         if(!obj)
667         {
668           T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_get_inode for NULL object!!\n"));
669           return NULL;
670           
671         }
672         
673         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
674
675         inode = iget(sb,obj->objectId);
676
677         // NB Side effect: iget calls back to yaffs_read_inode().
678         // iget also increments the inode's i_count
679         
680         return inode;
681 }
682
683 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
684 {
685         yaffs_Object *obj;
686         int nWritten,ipos;
687         struct inode *inode;
688         yaffs_Device *dev;
689         
690         
691         obj  = yaffs_DentryToObject(f->f_dentry);
692         
693         dev = obj->myDev;
694         
695         yaffs_GrossLock(dev);
696
697         inode = f->f_dentry->d_inode;
698
699         if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
700         {
701                 ipos = inode->i_size;
702         }
703         else
704         {
705                 ipos = *pos;
706         }
707         
708         
709         if(!obj)
710         {
711                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write: hey obj is null!\n"));
712         }
713         else
714         {
715                 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));
716         }
717
718         nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n,0);
719
720         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
721         if(nWritten > 0)
722         {
723                 ipos += nWritten;
724                 *pos = ipos;
725                 if(ipos > inode->i_size)
726                 {
727                         inode->i_size = ipos;
728                         inode->i_blocks = (ipos + 511)>>9;
729                         
730                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,(int)(inode->i_blocks)));
731                 }
732                 
733         }
734         yaffs_GrossUnlock(dev);
735         
736         return nWritten != n ? -ENOSPC : nWritten;
737 }
738
739
740
741 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
742 {
743         yaffs_Object *obj;
744         yaffs_Device *dev;
745         struct inode *inode = f->f_dentry->d_inode;
746         unsigned long offset, curoffs;
747         struct list_head *i;    
748         yaffs_Object *l;
749         
750         char name[YAFFS_MAX_NAME_LENGTH +1];
751                 
752         obj =  yaffs_DentryToObject(f->f_dentry);
753         dev = obj->myDev;
754         
755         yaffs_GrossLock(dev);
756         
757         offset = f->f_pos;
758         
759         T(YAFFS_TRACE_OS,("yaffs_readdir: starting at %d\n",(int)offset));
760         
761         if(offset == 0)
762         {
763         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
764                 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
765                 {
766                         goto out;
767                 }
768                 offset++;
769                 f->f_pos++;
770         }
771         if(offset == 1)
772         {
773                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
774                 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
775                 {
776                         goto out;
777                 }
778                 offset++;
779                 f->f_pos++;
780         }
781         
782         curoffs = 1;
783         
784         list_for_each(i,&obj->variant.directoryVariant.children)
785         {
786                 curoffs++;
787                 if(curoffs >= offset)
788                 {               
789                         l = list_entry(i, yaffs_Object,siblings);
790                         
791                         yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1); 
792                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
793                         
794                         if(filldir(dirent,
795                                            name,
796                                            strlen(name),
797                                            offset,
798                                            yaffs_GetObjectInode(l),
799                                            yaffs_GetObjectType(l))
800                                            < 0)
801                         {
802                                 goto up_and_out;
803                         }
804                         
805                         offset++;
806                         f->f_pos++;        
807                 }
808         }
809
810   up_and_out:
811   out:
812   
813     yaffs_GrossUnlock(dev);
814     
815         return 0;
816 }
817
818
819 /*
820  * File creation. Allocate an inode, and we're done..
821  */
822 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
823 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
824 #else
825 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
826 #endif
827 {
828         struct inode *inode;
829         
830         yaffs_Object *obj = NULL;
831         yaffs_Device *dev;
832         
833         yaffs_Object *parent = yaffs_InodeToObject(dir);
834         
835         int error = -ENOSPC;
836         uid_t uid = current->fsuid;
837         gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
838
839         if(parent)
840         {
841                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
842                                          parent->objectId,parent->variantType));
843         }
844         else
845         {
846                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
847                 return -EPERM;
848         }
849         
850         T(YAFFS_TRACE_OS,("yaffs_mknod: making oject for %s, mode %x dev %x\n",
851                                         dentry->d_name.name, mode,rdev));
852
853         dev = parent->myDev;
854         
855         yaffs_GrossLock(dev);
856
857         switch (mode & S_IFMT) 
858         {
859                 default:
860                         // Special (socket, fifo, device...)
861                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making special\n"));
862 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
863                         obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,uid, gid,old_encode_dev(rdev));
864 #else
865                         obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,uid, gid,rdev);
866 #endif                  
867                 break;
868                 case S_IFREG:   // file         
869                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
870                         obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,uid, gid);
871                         break;
872                 case S_IFDIR:   // directory
873                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making directory\n"));
874                         obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,uid, gid);
875                         break;
876                 case S_IFLNK:   // symlink
877                         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
878                         obj = NULL; // Do we ever get here?
879                         break;
880         }
881         
882         if(obj)
883         {
884                 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
885                 d_instantiate(dentry, inode);
886                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
887                 error = 0;
888         }
889         else
890         {
891                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod failed making object\n"));
892                 error = -ENOMEM;
893         }
894
895         yaffs_GrossUnlock(dev);
896
897         return error;
898 }
899
900 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
901 {
902         int retVal;
903         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mkdir\n"));
904         retVal =  yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
905 #if 0
906  // attempt to fix dir bug - didn't work
907         if(!retVal)
908         {
909                 dget(dentry);
910         }
911 #endif
912         return retVal;
913 }
914
915 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
916 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *n)
917 #else
918 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
919 #endif
920 {
921         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_create\n"));
922         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
923 }
924
925
926 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
927 {
928         int retVal;
929         
930         yaffs_Device *dev;
931         
932         
933         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_unlink %d:%s\n",(int)(dir->i_ino),dentry->d_name.name));
934         
935         dev = yaffs_InodeToObject(dir)->myDev;
936         
937         yaffs_GrossLock(dev);
938         
939         
940         retVal = yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name);
941         
942         
943         yaffs_GrossUnlock(dev);
944         
945         if( retVal == YAFFS_OK)
946         {
947                 dentry->d_inode->i_nlink--;
948                 mark_inode_dirty(dentry->d_inode);
949                 return 0;
950         }
951         else
952         {
953                 return -ENOTEMPTY;
954         }
955 }
956
957
958 /*
959  * Create a link...
960  */
961 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
962 {
963         struct inode *inode = old_dentry->d_inode;
964         yaffs_Object *obj = NULL;
965         yaffs_Object *link=NULL;
966         yaffs_Device *dev;
967         
968         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link\n"));
969         
970         obj = yaffs_InodeToObject(inode);
971         dev = obj->myDev;
972         
973         yaffs_GrossLock(dev);
974
975         if (!S_ISDIR(inode->i_mode))    // Don't link directories
976         {
977                 link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
978         }
979         
980
981         if(link)
982         {
983                 old_dentry->d_inode->i_nlink =  yaffs_GetObjectLinkCount(obj);
984                 d_instantiate(dentry, old_dentry->d_inode);
985                 atomic_inc(&old_dentry->d_inode->i_count);
986                 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link link count %d i_count %d\n",    
987                         old_dentry->d_inode->i_nlink,atomic_read(&old_dentry->d_inode->i_count)));
988         
989         }
990         
991         yaffs_GrossUnlock(dev);
992         
993
994         if(link)
995         {
996         
997                 return 0;
998         }
999         
1000         
1001         return -EPERM; 
1002 }
1003
1004
1005 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
1006 {
1007         yaffs_Object *obj;
1008         yaffs_Device *dev;
1009         uid_t uid = current->fsuid;
1010         gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1011         
1012         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_symlink\n"));
1013         
1014         dev = yaffs_InodeToObject(dir)->myDev;
1015         yaffs_GrossLock(dev);
1016         obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name, 
1017                                                          S_IFLNK | S_IRWXUGO, uid, gid,
1018                                                          symname);
1019         yaffs_GrossUnlock(dev);
1020
1021         if(obj)
1022         {
1023
1024                 struct inode* inode;
1025         
1026                 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1027                 d_instantiate(dentry, inode);
1028                 T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink created OK\n"));
1029                 return 0;
1030         }
1031         else
1032         {
1033                 T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink not created\n"));
1034
1035         }
1036         
1037         return -ENOMEM;
1038 }
1039
1040 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
1041 {
1042
1043         yaffs_Object *obj;
1044         yaffs_Device *dev;
1045         
1046         obj = yaffs_DentryToObject(dentry);
1047
1048         dev = obj->myDev;
1049         
1050         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_sync_object\n"));
1051         yaffs_GrossLock(dev);
1052         yaffs_FlushFile(obj,1);
1053         yaffs_GrossUnlock(dev);
1054         return 0;
1055 }
1056
1057 /*
1058  * The VFS layer already does all the dentry stuff for rename.
1059  *
1060  * NB: POSIX says you can rename an object over an old object of the same name
1061  */
1062 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
1063 {
1064         yaffs_Device *dev;
1065         int retVal = YAFFS_FAIL;
1066         int removed = 0;
1067         yaffs_Object *target;
1068         
1069         dev = yaffs_InodeToObject(old_dir)->myDev;
1070
1071         yaffs_GrossLock(dev);
1072         
1073         // Check if the target is an existing directory that is not empty.
1074         target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1075         
1076         if(target &&
1077            target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1078            !list_empty(&target->variant.directoryVariant.children))
1079         {
1080                 retVal = YAFFS_FAIL;
1081         }
1082         else
1083         {
1084            
1085                 // Unlink the target if it exists
1086                 removed = yaffs_Unlink(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1087
1088         
1089                 retVal =  yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
1090                                                                         yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1091                                                                         
1092         }
1093         yaffs_GrossUnlock(dev);
1094         
1095         if(retVal == YAFFS_OK)
1096         {
1097                 if(removed == YAFFS_OK)
1098                 {
1099                         new_dentry->d_inode->i_nlink--;
1100                         mark_inode_dirty(new_dentry->d_inode);
1101                 }
1102                 
1103                 return 0;
1104         }
1105         else
1106         {
1107                 return -ENOTEMPTY;
1108         }
1109         
1110
1111 }
1112
1113 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1114 {
1115         struct inode *inode = dentry->d_inode;
1116         int error;
1117         yaffs_Device *dev;
1118         
1119         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_setattr of object %d\n",yaffs_InodeToObject(inode)->objectId));
1120         
1121         if((error = inode_change_ok(inode,attr)) == 0)
1122         {
1123         
1124                 dev = yaffs_InodeToObject(inode)->myDev;
1125                 yaffs_GrossLock(dev);
1126                 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
1127                 {
1128                         error = 0;
1129                 }
1130                 else
1131                 {
1132                         error = -EPERM;
1133                 }
1134                 yaffs_GrossUnlock(dev);
1135                 if (!error)
1136                         error = inode_setattr(inode,attr);
1137         }
1138         return error;
1139 }
1140
1141 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1142 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1143 #else
1144 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1145 #endif
1146 {
1147
1148         
1149         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1150         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_statfs\n"));
1151
1152         yaffs_GrossLock(dev);
1153         
1154         
1155         buf->f_type = YAFFS_MAGIC;
1156         buf->f_bsize = sb->s_blocksize;
1157         buf->f_namelen = 255;
1158         if(sb->s_blocksize > dev->nBytesPerChunk)
1159         {
1160                 
1161                 buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * dev->nChunksPerBlock/
1162                                                 (sb->s_blocksize/dev->nBytesPerChunk);
1163                 buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev)/
1164                                                 (sb->s_blocksize/dev->nBytesPerChunk);
1165         }
1166         else
1167         {
1168                 
1169                 buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * dev->nChunksPerBlock *
1170                                                 (dev->nBytesPerChunk/sb->s_blocksize);
1171                 buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev) *
1172                                                 (dev->nBytesPerChunk/sb->s_blocksize);
1173         }
1174         buf->f_files = 0;
1175         buf->f_ffree = 0;
1176         buf->f_bavail =  buf->f_bfree;
1177         
1178         yaffs_GrossUnlock(dev);
1179         return 0;
1180 }
1181
1182 static void yaffs_read_inode (struct inode *inode)
1183 {
1184         // NB This is called as a side effect of other functions and
1185         // thus gross locking should always be in place already.
1186         
1187         yaffs_Object *obj ; 
1188         yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1189         
1190         T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
1191
1192         obj  = yaffs_FindObjectByNumber(dev,inode->i_ino);
1193         
1194         yaffs_FillInodeFromObject(inode,obj);
1195
1196 }
1197
1198 static LIST_HEAD(yaffs_dev_list);
1199
1200 static void yaffs_put_super(struct super_block *sb)
1201 {
1202         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1203         
1204         yaffs_GrossLock(dev);
1205         if(dev->putSuperFunc)
1206         {
1207                  dev->putSuperFunc(sb);
1208         }
1209         yaffs_Deinitialise(dev);
1210         yaffs_GrossUnlock(dev);
1211
1212         /* we assume this is protected by lock_kernel() in mount/umount */
1213         list_del(&dev->devList);
1214
1215         kfree(dev);
1216 }
1217
1218
1219 #ifdef CONFIG_YAFFS_YAFFS1
1220
1221 static void  yaffs_MTDPutSuper(struct super_block *sb)
1222 {
1223         
1224         struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1225         
1226         if(mtd->sync)
1227         {
1228                 mtd->sync(mtd);
1229         }
1230         
1231         put_mtd_device(mtd);
1232 }
1233
1234 #endif
1235
1236
1237 static struct super_block *yaffs_internal_read_super(int yaffsVersion, struct super_block * sb, void * data, int silent)
1238 {
1239         int nBlocks;
1240         struct inode * inode = NULL;
1241         struct dentry * root;
1242         yaffs_Device *dev = 0;
1243         char devname_buf[BDEVNAME_SIZE+1];
1244         struct mtd_info *mtd;
1245         int err;
1246         
1247         sb->s_magic = YAFFS_MAGIC;
1248         sb->s_op = &yaffs_super_ops;
1249         
1250         if(!sb)
1251                 printk(KERN_INFO"yaffs: sb is NULL\n");
1252         else if(!sb->s_dev)
1253                 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
1254         else if(!yaffs_devname(sb, devname_buf))
1255                 printk(KERN_INFO"yaffs: devname is NULL\n");
1256         else
1257                 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, yaffs_devname(sb, devname_buf));
1258
1259         
1260
1261         sb->s_blocksize = PAGE_CACHE_SIZE;
1262         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1263         T(YAFFS_TRACE_OS,("yaffs_read_super: Using yaffs%d\n",yaffsVersion));
1264         T(YAFFS_TRACE_OS,("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1265
1266 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1267         T(YAFFS_TRACE_OS,("yaffs: Write verification disabled. All guarantees null and void\n"));
1268 #endif
1269
1270
1271         T(YAFFS_TRACE_ALWAYS,("yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
1272          MAJOR(sb->s_dev),MINOR(sb->s_dev), yaffs_devname(sb, devname_buf)));
1273                 
1274         // Check it's an mtd device.....
1275         if(MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
1276         {
1277                 return NULL; // This isn't an mtd device
1278         } 
1279         
1280         // Get the device
1281         mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1282         if (!mtd) 
1283         {
1284                 T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev)));
1285                 return NULL;
1286         }
1287         
1288         // Check it's NAND
1289         if(mtd->type != MTD_NANDFLASH)
1290         {
1291                 T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1292                 return NULL;
1293         }
1294
1295         T(YAFFS_TRACE_OS,(" erase %p\n",mtd->erase));
1296         T(YAFFS_TRACE_OS,(" read %p\n",mtd->read));
1297         T(YAFFS_TRACE_OS,(" write %p\n",mtd->write));
1298         T(YAFFS_TRACE_OS,(" readoob %p\n",mtd->read_oob));
1299         T(YAFFS_TRACE_OS,(" writeoob %p\n",mtd->write_oob));
1300         T(YAFFS_TRACE_OS,(" block_isbad %p\n",mtd->block_isbad));
1301         T(YAFFS_TRACE_OS,(" block_markbad %p\n",mtd->block_markbad));
1302         T(YAFFS_TRACE_OS,(" oobblock %d\n",mtd->oobblock));
1303         T(YAFFS_TRACE_OS,(" oobsize %d\n",mtd->oobsize));
1304         T(YAFFS_TRACE_OS,(" erasesize %d\n",mtd->erasesize));
1305         T(YAFFS_TRACE_OS,(" size %d\n",mtd->size));
1306
1307         if(yaffsVersion == 2)
1308         {
1309                 // Check for version 2 style functions
1310                 if(!mtd->erase ||
1311                    !mtd->block_isbad ||
1312                    !mtd->block_markbad ||
1313                    !mtd->read  ||
1314                    !mtd->write ||
1315                    !mtd->write_ecc ||
1316                    !mtd->read_ecc ||
1317                    !mtd->read_oob ||
1318                    !mtd->write_oob )
1319                 {
1320                         T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device does not support required functions\n"));;
1321                         return NULL;
1322                 }
1323         
1324                 if(mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1325                    mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE)
1326                 {
1327                         T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device does not support have the right page sizes\n"));
1328                         return NULL;
1329                 }               }
1330         else
1331         {
1332                 // Check for V1 style functions
1333                 if(!mtd->erase ||
1334                    !mtd->read  ||
1335                    !mtd->write ||
1336                    !mtd->write_ecc ||
1337                    !mtd->read_ecc ||
1338                    !mtd->read_oob ||
1339                    !mtd->write_oob )
1340                 {
1341                         T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device does not support required functions\n"));;
1342                         return NULL;
1343                 }
1344         
1345                 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
1346                    mtd->oobsize != YAFFS_BYTES_PER_SPARE)
1347                 {
1348                         T(YAFFS_TRACE_ALWAYS,("yaffs: MTD device does not support have the right page sizes\n"));
1349                         return NULL;
1350                 }
1351         }
1352            
1353
1354                 // OK, so if we got here, we have an MTD that's NAND and looks 
1355                 // like it has the right capabilities
1356                 // Set the yaffs_Device up for mtd
1357
1358 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1359         sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1360 #else
1361         sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1362 #endif
1363         if(!dev)
1364         {
1365                 // Deep shit could not allocate device structure
1366                 T(YAFFS_TRACE_ALWAYS,("yaffs_read_super: Failed trying to allocate yaffs_Device. \n"));
1367                 return NULL;
1368         }
1369
1370         memset(dev,0,sizeof(yaffs_Device));
1371         dev->genericDevice = mtd; 
1372         dev->name = mtd->name;
1373
1374         // Set up the memory size parameters....
1375         
1376         nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1377         dev->startBlock = 0;
1378         dev->endBlock = nBlocks - 1;
1379         dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1380         dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1381         dev->nReservedBlocks = 5;
1382         dev->nShortOpCaches = 10; // Enable short op caching
1383         
1384
1385         // ... and the functions.
1386         if(yaffsVersion == 2)
1387         {
1388                 dev->writeChunkWithTagsToNAND = nandmtd2_WriteChunkWithTagsToNAND;
1389                 dev->readChunkWithTagsFromNAND = nandmtd2_ReadChunkWithTagsFromNAND;
1390                 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1391                 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1392                 dev->spareBuffer = YMALLOC(mtd->oobsize);
1393                 dev->isYaffs2 = 1;
1394                 dev->nBytesPerChunk = mtd->oobblock;
1395                 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1396                 nBlocks = mtd->size / mtd->erasesize;
1397                 dev->startBlock = 0;
1398                 dev->endBlock = nBlocks - 1;
1399         }
1400         else
1401         {
1402                 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1403                 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1404                 dev->isYaffs2 = 0;
1405         }
1406         // ... and common functions
1407         dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1408         dev->initialiseNAND = nandmtd_InitialiseNAND;
1409         
1410         dev->putSuperFunc = yaffs_MTDPutSuper;
1411         
1412 #ifndef CONFIG_YAFFS_DOES_ECC
1413         dev->useNANDECC = 1;
1414 #endif
1415
1416         /* we assume this is protected by lock_kernel() in mount/umount */
1417         list_add_tail(&dev->devList, &yaffs_dev_list);
1418
1419         init_MUTEX(&dev->grossLock);
1420         
1421         
1422         yaffs_GrossLock(dev);
1423         
1424         err = yaffs_GutsInitialise(dev);
1425
1426         T(YAFFS_TRACE_OS,("yaffs_read_super: guts initialised %s\n", (err == YAFFS_OK) ? "OK" : "FAILED"));
1427
1428         // Create root inode
1429         if(err == YAFFS_OK)
1430           inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(dev));
1431
1432         yaffs_GrossUnlock(dev);
1433
1434         if (!inode)
1435                 return NULL;
1436                 
1437 // added NCB
1438         inode->i_op = & yaffs_dir_inode_operations;
1439         inode->i_fop = & yaffs_dir_operations;
1440
1441         T(YAFFS_TRACE_OS,("yaffs_read_super: got root inode\n"));
1442                 
1443
1444         root = d_alloc_root(inode);
1445
1446         T(YAFFS_TRACE_OS,("yaffs_read_super: d_alloc_root done\n"));
1447
1448         if (!root) {
1449                 iput(inode);
1450                 return NULL;
1451         }
1452         sb->s_root = root;
1453
1454         T(YAFFS_TRACE_OS,("yaffs_read_super: done\n"));
1455         return sb;
1456 }
1457
1458
1459
1460 #ifdef CONFIG_YAFFS_YAFFS1
1461
1462 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1463 static int yaffs_internal_read_super_mtd(struct super_block * sb, void * data, int silent)
1464 {
1465          return yaffs_internal_read_super(1,sb,data,silent) ? 0 : -1;
1466 }
1467
1468 static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1469 {
1470
1471     return get_sb_bdev(fs, flags, dev_name, data, yaffs_internal_read_super_mtd);
1472 }
1473
1474 static struct file_system_type yaffs_fs_type = {
1475         .owner          = THIS_MODULE,
1476         .name           = "yaffs",
1477         .get_sb         = yaffs_read_super,
1478         .kill_sb        = kill_block_super,
1479         .fs_flags       = FS_REQUIRES_DEV,
1480 };
1481 #else
1482 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
1483 {
1484         return yaffs_internal_read_super(1,sb,data,silent);
1485 }
1486
1487 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1488 #endif
1489
1490 #endif // CONFIG_YAFFS_YAFFS1
1491
1492 #ifdef CONFIG_YAFFS_YAFFS2
1493
1494 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1495 static int yaffs2_internal_read_super_mtd(struct super_block * sb, void * data, int silent)
1496 {
1497          return yaffs_internal_read_super(2,sb,data,silent) ? 0 : -1;
1498 }
1499
1500 static struct super_block *yaffs2_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1501 {
1502
1503     return get_sb_bdev(fs, flags, dev_name, data, yaffs2_internal_read_super_mtd);
1504 }
1505
1506 static struct file_system_type yaffs2_fs_type = {
1507         .owner          = THIS_MODULE,
1508         .name           = "yaffs2",
1509         .get_sb         = yaffs2_read_super,
1510         .kill_sb        = kill_block_super,
1511         .fs_flags       = FS_REQUIRES_DEV,
1512 };
1513 #else
1514 static struct super_block *yaffs2_read_super(struct super_block * sb, void * data, int silent)
1515 {
1516         return yaffs_internal_read_super(2,sb,data,silent);
1517 }
1518
1519 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super, FS_REQUIRES_DEV);
1520 #endif
1521
1522 #endif // CONFIG_YAFFS_YAFFS2
1523
1524
1525
1526
1527 static struct proc_dir_entry *my_proc_entry;
1528
1529 static char * yaffs_dump_dev(char *buf,yaffs_Device *dev)
1530 {
1531         buf +=sprintf(buf,"startBlock......... %d\n",dev->startBlock);
1532         buf +=sprintf(buf,"endBlock........... %d\n",dev->endBlock);
1533         buf +=sprintf(buf,"chunkGroupBits..... %d\n",dev->chunkGroupBits);
1534         buf +=sprintf(buf,"chunkGroupSize..... %d\n",dev->chunkGroupSize);
1535         buf +=sprintf(buf,"nErasedBlocks...... %d\n",dev->nErasedBlocks);
1536         buf +=sprintf(buf,"nTnodesCreated..... %d\n",dev->nTnodesCreated);
1537         buf +=sprintf(buf,"nFreeTnodes........ %d\n",dev->nFreeTnodes);
1538         buf +=sprintf(buf,"nObjectsCreated.... %d\n",dev->nObjectsCreated);
1539         buf +=sprintf(buf,"nFreeObjects....... %d\n",dev->nFreeObjects);
1540         buf +=sprintf(buf,"nFreeChunks........ %d\n",dev->nFreeChunks);
1541         buf +=sprintf(buf,"nPageWrites........ %d\n",dev->nPageWrites);
1542         buf +=sprintf(buf,"nPageReads......... %d\n",dev->nPageReads);
1543         buf +=sprintf(buf,"nBlockErasures..... %d\n",dev->nBlockErasures);
1544         buf +=sprintf(buf,"nGCCopies.......... %d\n",dev->nGCCopies);
1545         buf +=sprintf(buf,"garbageCollections. %d\n",dev->garbageCollections);
1546         buf +=sprintf(buf,"passiveGCs......... %d\n",dev->passiveGarbageCollections);
1547         buf +=sprintf(buf,"nRetriedWrites..... %d\n",dev->nRetriedWrites);
1548         buf +=sprintf(buf,"nRetireBlocks...... %d\n",dev->nRetiredBlocks);
1549         buf +=sprintf(buf,"eccFixed........... %d\n",dev->eccFixed);
1550         buf +=sprintf(buf,"eccUnfixed......... %d\n",dev->eccUnfixed);
1551         buf +=sprintf(buf,"tagsEccFixed....... %d\n",dev->tagsEccFixed);
1552         buf +=sprintf(buf,"tagsEccUnfixed..... %d\n",dev->tagsEccUnfixed);
1553         buf +=sprintf(buf,"cacheHits.......... %d\n",dev->cacheHits);
1554         buf +=sprintf(buf,"nDeletedFiles...... %d\n",dev->nDeletedFiles);
1555         buf +=sprintf(buf,"nUnlinkedFiles..... %d\n",dev->nUnlinkedFiles);
1556         buf +=sprintf(buf,"nBackgroudDeletions %d\n",dev->nBackgroundDeletions);
1557         buf +=sprintf(buf,"useNANDECC......... %d\n",dev->useNANDECC);
1558         buf +=sprintf(buf,"isYaffs2........... %d\n",dev->isYaffs2);
1559
1560         return buf;     
1561 }
1562
1563 static int  yaffs_proc_read(
1564         char *page,
1565         char **start,
1566         off_t offset,
1567         int count,
1568         int *eof,
1569         void *data
1570         )
1571 {
1572         struct list_head *item;
1573         char *buf = page;
1574         int step = offset;
1575         int n = 0;
1576
1577         /* Get proc_file_read() to step 'offset' by one on each sucessive call.
1578          * We use 'offset' (*ppos) to indicate where we are in devList.
1579          * This also assumes the user has posted a read buffer large
1580          * enough to hold the complete output; but that's life in /proc.
1581          */
1582
1583         *(int *)start = 1;
1584
1585         /* Print header first */
1586         if (step == 0) {
1587                 buf += sprintf(buf, "YAFFS built:" __DATE__ " "__TIME__
1588                 "\n%s\n%s\n", yaffs_fs_c_version, yaffs_guts_c_version);
1589         }
1590
1591         /* hold lock_kernel while traversing yaffs_dev_list */
1592         lock_kernel();
1593
1594         /* Locate and print the Nth entry.  Order N-squared but N is small. */
1595         list_for_each(item, &yaffs_dev_list) {
1596                 yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
1597                 if (n < step) {
1598                         n++;
1599                         continue;
1600                 }
1601                 buf += sprintf(buf,"\nDevice %d \"%s\"\n", n, dev->name);
1602                 buf = yaffs_dump_dev(buf, dev);
1603                 break;
1604         }
1605         unlock_kernel();
1606
1607         return buf-page < count ? buf-page : count;
1608 }
1609
1610 // Stuff to handle installation of file systems
1611 struct file_system_to_install
1612 {
1613    struct file_system_type *fst;
1614    int installed;
1615 };
1616
1617 static struct file_system_to_install fs_to_install[] =
1618 {
1619 #ifdef CONFIG_YAFFS_YAFFS1
1620      { &yaffs_fs_type,0},
1621 #endif
1622 #ifdef CONFIG_YAFFS_YAFFS2
1623      { &yaffs2_fs_type,0},
1624 #endif
1625      { NULL,0}
1626 };
1627
1628 static int __init init_yaffs_fs(void)
1629 {
1630         int error = 0;
1631         struct file_system_to_install *fsinst;  
1632         
1633    T(YAFFS_TRACE_ALWAYS,("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
1634
1635
1636
1637     /* Install the proc_fs entry */
1638     my_proc_entry = create_proc_read_entry("yaffs",
1639                                            S_IRUGO | S_IFREG,
1640                                            &proc_root,
1641                                            yaffs_proc_read,
1642                                            NULL);
1643     if(!my_proc_entry)
1644     {
1645        return -ENOMEM;
1646     }
1647     
1648     
1649
1650     // Now add the file system entries
1651     
1652     fsinst = fs_to_install;
1653     
1654     while(fsinst->fst && !error)
1655     {
1656       error = register_filesystem(fsinst->fst);
1657       if(!error)
1658       {
1659           fsinst->installed = 1;
1660       }
1661       fsinst++;
1662     }
1663    
1664     // Any errors? uninstall 
1665     if(error)
1666     {
1667             fsinst = fs_to_install;
1668             
1669             while(fsinst->fst)
1670             {
1671               if(fsinst->installed)
1672               {
1673                 unregister_filesystem(fsinst->fst);
1674                 fsinst->installed = 0;
1675               }
1676               fsinst++;
1677             }
1678     }
1679     
1680     return error;
1681 }
1682
1683 static void __exit exit_yaffs_fs(void)
1684 {
1685
1686    struct file_system_to_install *fsinst;
1687    
1688    T(YAFFS_TRACE_ALWAYS,("yaffs " __DATE__ " " __TIME__ " removing. \n"));
1689
1690    remove_proc_entry("yaffs",&proc_root);
1691     
1692     fsinst = fs_to_install;
1693     
1694     while(fsinst->fst)
1695     {
1696       if(fsinst->installed)
1697       {
1698         unregister_filesystem(fsinst->fst);
1699         fsinst->installed = 0;
1700       }
1701       fsinst++;
1702     }
1703
1704 }
1705
1706 module_init(init_yaffs_fs)
1707 module_exit(exit_yaffs_fs)
1708
1709 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
1710 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002,2003,2004");
1711 MODULE_LICENSE("GPL");
1712
1713