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