Add a symlink option to the patching script
[yaffs2.git] / yaffs_fs.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2007 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  * Acknowledgements:
9  * Luc van OostenRyck for numerous patches.
10  * Nick Bane for numerous patches.
11  * Nick Bane for 2.5/2.6 integration.
12  * Andras Toth for mknod rdev issue.
13  * Michael Fischer for finding the problem with inode inconsistency.
14  * Some code bodily lifted from JFFS
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License version 2 as
18  * published by the Free Software Foundation.
19  */
20
21 /*
22  *
23  * This is the file system front-end to YAFFS that hooks it up to
24  * the VFS.
25  *
26  * Special notes: 
27  * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
28  *         this superblock
29  * >> 2.6: sb->s_fs_info  points to the yaffs_Device associated with this
30  *         superblock
31  * >> inode->u.generic_ip points to the associated yaffs_Object.
32  */
33
34 const char *yaffs_fs_c_version =
35     "$Id: yaffs_fs.c,v 1.61 2007-07-18 19:40:38 charles Exp $";
36 extern const char *yaffs_guts_c_version;
37
38 #include <linux/version.h>
39 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
40 #include <linux/config.h>
41 #endif
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/init.h>
46 #include <linux/list.h>
47 #include <linux/fs.h>
48 #include <linux/proc_fs.h>
49 #include <linux/smp_lock.h>
50 #include <linux/pagemap.h>
51 #include <linux/mtd/mtd.h>
52 #include <linux/interrupt.h>
53 #include <linux/string.h>
54 #include <linux/ctype.h>
55
56 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
57
58 #include <linux/statfs.h>       /* Added NCB 15-8-2003 */
59 #include <asm/statfs.h>
60 #define UnlockPage(p) unlock_page(p)
61 #define Page_Uptodate(page)     test_bit(PG_uptodate, &(page)->flags)
62
63 /* FIXME: use sb->s_id instead ? */
64 #define yaffs_devname(sb, buf)  bdevname(sb->s_bdev, buf)
65
66 #else
67
68 #include <linux/locks.h>
69 #define BDEVNAME_SIZE           0
70 #define yaffs_devname(sb, buf)  kdevname(sb->s_dev)
71
72 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
73 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
74 #define __user
75 #endif
76
77 #endif
78
79 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
80 #define WRITE_SIZE_STR "writesize"
81 #define WRITE_SIZE(mtd) (mtd)->writesize
82 #else
83 #define WRITE_SIZE_STR "oobblock"
84 #define WRITE_SIZE(mtd) (mtd)->oobblock
85 #endif
86
87 #include <asm/uaccess.h>
88
89 #include "yportenv.h"
90 #include "yaffs_guts.h"
91
92 #include <linux/mtd/mtd.h>
93 #include "yaffs_mtdif.h"
94 #include "yaffs_mtdif1.h"
95 #include "yaffs_mtdif2.h"
96
97 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
98 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
99
100 /* Module Parameters */
101 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
102 module_param(yaffs_traceMask,uint,0644);
103 module_param(yaffs_wr_attempts,uint,0644);
104 #else
105 MODULE_PARM(yaffs_traceMask,"i");
106 MODULE_PARM(yaffs_wr_attempts,"i");
107 #endif
108
109 /*#define T(x) printk x */
110
111 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
112 #define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
113 #else
114 #define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
115 #endif
116
117 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
118 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
119
120 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
121 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
122 #else
123 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
124 #endif
125
126 static void yaffs_put_super(struct super_block *sb);
127
128 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
129                                 loff_t * pos);
130
131 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
132 static int yaffs_file_flush(struct file *file, fl_owner_t id);
133 #else
134 static int yaffs_file_flush(struct file *file);
135 #endif
136
137 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
138                              int datasync);
139
140 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
141
142 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
143 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
144                         struct nameidata *n);
145 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
146                                    struct nameidata *n);
147 #else
148 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
149 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
150 #endif
151 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
152                       struct dentry *dentry);
153 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
154 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
155                          const char *symname);
156 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
157
158 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
159 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
160                        dev_t dev);
161 #else
162 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
163                        int dev);
164 #endif
165 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
166                         struct inode *new_dir, struct dentry *new_dentry);
167 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
168
169 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
170 static int yaffs_sync_fs(struct super_block *sb, int wait);
171 static void yaffs_write_super(struct super_block *sb);
172 #else
173 static int yaffs_sync_fs(struct super_block *sb);
174 static int yaffs_write_super(struct super_block *sb);
175 #endif
176
177 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
178 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
179 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
180 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
181 #else
182 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
183 #endif
184 static void yaffs_read_inode(struct inode *inode);
185
186 static void yaffs_put_inode(struct inode *inode);
187 static void yaffs_delete_inode(struct inode *);
188 static void yaffs_clear_inode(struct inode *);
189
190 static int yaffs_readpage(struct file *file, struct page *page);
191 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
192 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
193 #else
194 static int yaffs_writepage(struct page *page);
195 #endif
196 static int yaffs_prepare_write(struct file *f, struct page *pg,
197                                unsigned offset, unsigned to);
198 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
199                               unsigned to);
200
201 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
202                           int buflen);
203 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
204 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
205 #else
206 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
207 #endif
208
209 static struct address_space_operations yaffs_file_address_operations = {
210         .readpage = yaffs_readpage,
211         .writepage = yaffs_writepage,
212         .prepare_write = yaffs_prepare_write,
213         .commit_write = yaffs_commit_write,
214 };
215
216 static struct file_operations yaffs_file_operations = {
217 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
218         .read = do_sync_read,
219         .write = do_sync_write,
220         .aio_read = generic_file_aio_read,
221         .aio_write = generic_file_aio_write,
222 #else
223         .read = generic_file_read,
224         .write = generic_file_write,
225 #endif
226         .mmap = generic_file_mmap,
227         .flush = yaffs_file_flush,
228         .fsync = yaffs_sync_object,
229 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
230         .sendfile = generic_file_sendfile,
231 #endif
232
233 };
234
235 static struct inode_operations yaffs_file_inode_operations = {
236         .setattr = yaffs_setattr,
237 };
238
239 static struct inode_operations yaffs_symlink_inode_operations = {
240         .readlink = yaffs_readlink,
241         .follow_link = yaffs_follow_link,
242         .setattr = yaffs_setattr,
243 };
244
245 static struct inode_operations yaffs_dir_inode_operations = {
246         .create = yaffs_create,
247         .lookup = yaffs_lookup,
248         .link = yaffs_link,
249         .unlink = yaffs_unlink,
250         .symlink = yaffs_symlink,
251         .mkdir = yaffs_mkdir,
252         .rmdir = yaffs_unlink,
253         .mknod = yaffs_mknod,
254         .rename = yaffs_rename,
255         .setattr = yaffs_setattr,
256 };
257
258 static struct file_operations yaffs_dir_operations = {
259         .read = generic_read_dir,
260         .readdir = yaffs_readdir,
261         .fsync = yaffs_sync_object,
262 };
263
264 static struct super_operations yaffs_super_ops = {
265         .statfs = yaffs_statfs,
266         .read_inode = yaffs_read_inode,
267         .put_inode = yaffs_put_inode,
268         .put_super = yaffs_put_super,
269         .delete_inode = yaffs_delete_inode,
270         .clear_inode = yaffs_clear_inode,
271         .sync_fs = yaffs_sync_fs,
272         .write_super = yaffs_write_super,
273 };
274
275 static void yaffs_GrossLock(yaffs_Device * dev)
276 {
277         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
278
279         down(&dev->grossLock);
280 }
281
282 static void yaffs_GrossUnlock(yaffs_Device * dev)
283 {
284         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
285         up(&dev->grossLock);
286
287 }
288
289 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
290                           int buflen)
291 {
292         unsigned char *alias;
293         int ret;
294
295         yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
296
297         yaffs_GrossLock(dev);
298
299         alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
300
301         yaffs_GrossUnlock(dev);
302
303         if (!alias)
304                 return -ENOMEM;
305
306         ret = vfs_readlink(dentry, buffer, buflen, alias);
307         kfree(alias);
308         return ret;
309 }
310
311 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
312 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
313 #else
314 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
315 #endif
316 {
317         unsigned char *alias;
318         int ret;
319         yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
320
321         yaffs_GrossLock(dev);
322
323         alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
324
325         yaffs_GrossUnlock(dev);
326
327         if (!alias)
328         {
329                 ret = -ENOMEM;
330                 goto out;
331         }
332
333         ret = vfs_follow_link(nd, alias);
334         kfree(alias);
335 out:
336 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
337         return ERR_PTR (ret);
338 #else
339         return ret;
340 #endif
341 }
342
343 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
344                               yaffs_Object * obj);
345
346 /*
347  * Lookup is used to find objects in the fs
348  */
349 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
350
351 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
352                                    struct nameidata *n)
353 #else
354 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
355 #endif
356 {
357         yaffs_Object *obj;
358         struct inode *inode = NULL;     /* NCB 2.5/2.6 needs NULL here */
359
360         yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
361
362         yaffs_GrossLock(dev);
363
364         T(YAFFS_TRACE_OS,
365           (KERN_DEBUG "yaffs_lookup for %d:%s\n",
366            yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
367
368         obj =
369             yaffs_FindObjectByName(yaffs_InodeToObject(dir),
370                                    dentry->d_name.name);
371
372         obj = yaffs_GetEquivalentObject(obj);   /* in case it was a hardlink */
373         
374         /* Can't hold gross lock when calling yaffs_get_inode() */
375         yaffs_GrossUnlock(dev);
376
377         if (obj) {
378                 T(YAFFS_TRACE_OS,
379                   (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
380
381                 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
382
383                 if (inode) {
384                         T(YAFFS_TRACE_OS,
385                           (KERN_DEBUG "yaffs_loookup dentry \n"));
386 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
387  * d_add even if NULL inode */
388 #if 0
389                         /*dget(dentry); // try to solve directory bug */
390                         d_add(dentry, inode);
391
392                         /* return dentry; */
393                         return NULL;
394 #endif
395                 }
396
397         } else {
398                 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
399
400         }
401
402 /* added NCB for 2.5/6 compatability - forces add even if inode is
403  * NULL which creates dentry hash */
404         d_add(dentry, inode);
405
406         return NULL;
407         /*      return (ERR_PTR(-EIO)); */
408
409 }
410
411 /* For now put inode is just for debugging
412  * Put inode is called when the inode **structure** is put.
413  */
414 static void yaffs_put_inode(struct inode *inode)
415 {
416         T(YAFFS_TRACE_OS,
417           ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
418            atomic_read(&inode->i_count)));
419
420 }
421
422 /* clear is called to tell the fs to release any per-inode data it holds */
423 static void yaffs_clear_inode(struct inode *inode)
424 {
425         yaffs_Object *obj;
426         yaffs_Device *dev;
427
428         obj = yaffs_InodeToObject(inode);
429
430         T(YAFFS_TRACE_OS,
431           ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
432            atomic_read(&inode->i_count),
433            obj ? "object exists" : "null object"));
434
435         if (obj) {
436                 dev = obj->myDev;
437                 yaffs_GrossLock(dev);
438
439                 /* Clear the association between the inode and
440                  * the yaffs_Object.
441                  */
442                 obj->myInode = NULL;
443                 yaffs_InodeToObjectLV(inode) = NULL;
444
445                 /* If the object freeing was deferred, then the real
446                  * free happens now.
447                  * This should fix the inode inconsistency problem.
448                  */
449
450                 yaffs_HandleDeferedFree(obj);
451
452                 yaffs_GrossUnlock(dev);
453         }
454
455 }
456
457 /* delete is called when the link count is zero and the inode
458  * is put (ie. nobody wants to know about it anymore, time to
459  * delete the file).
460  * NB Must call clear_inode()
461  */
462 static void yaffs_delete_inode(struct inode *inode)
463 {
464         yaffs_Object *obj = yaffs_InodeToObject(inode);
465         yaffs_Device *dev;
466
467         T(YAFFS_TRACE_OS,
468           ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
469            atomic_read(&inode->i_count),
470            obj ? "object exists" : "null object"));
471
472         if (obj) {
473                 dev = obj->myDev;
474                 yaffs_GrossLock(dev);
475                 yaffs_DeleteFile(obj);
476                 yaffs_GrossUnlock(dev);
477         }
478 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
479         truncate_inode_pages (&inode->i_data, 0);
480 #endif
481         clear_inode(inode);
482 }
483
484 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
485 static int yaffs_file_flush(struct file *file, fl_owner_t id)
486 #else
487 static int yaffs_file_flush(struct file *file)
488 #endif
489 {
490         yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
491
492         yaffs_Device *dev = obj->myDev;
493
494         T(YAFFS_TRACE_OS,
495           (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
496            obj->dirty ? "dirty" : "clean"));
497
498         yaffs_GrossLock(dev);
499
500         yaffs_FlushFile(obj, 1);
501
502         yaffs_GrossUnlock(dev);
503
504         return 0;
505 }
506
507 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
508 {
509         /* Lifted from jffs2 */
510
511         yaffs_Object *obj;
512         unsigned char *pg_buf;
513         int ret;
514
515         yaffs_Device *dev;
516
517         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
518                            (unsigned)(pg->index << PAGE_CACHE_SHIFT),
519                            (unsigned)PAGE_CACHE_SIZE));
520
521         obj = yaffs_DentryToObject(f->f_dentry);
522
523         dev = obj->myDev;
524
525 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
526         BUG_ON(!PageLocked(pg));
527 #else
528         if (!PageLocked(pg))
529                 PAGE_BUG(pg);
530 #endif
531
532         pg_buf = kmap(pg);
533         /* FIXME: Can kmap fail? */
534
535         yaffs_GrossLock(dev);
536
537         ret =
538             yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
539                                    PAGE_CACHE_SIZE);
540
541         yaffs_GrossUnlock(dev);
542
543         if (ret >= 0)
544                 ret = 0;
545
546         if (ret) {
547                 ClearPageUptodate(pg);
548                 SetPageError(pg);
549         } else {
550                 SetPageUptodate(pg);
551                 ClearPageError(pg);
552         }
553
554         flush_dcache_page(pg);
555         kunmap(pg);
556
557         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
558         return ret;
559 }
560
561 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
562 {
563         int ret = yaffs_readpage_nolock(f, pg);
564         UnlockPage(pg);
565         return ret;
566 }
567
568 static int yaffs_readpage(struct file *f, struct page *pg)
569 {
570         return yaffs_readpage_unlock(f, pg);
571 }
572
573 /* writepage inspired by/stolen from smbfs */
574
575 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
576 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
577 #else
578 static int yaffs_writepage(struct page *page)
579 #endif
580 {
581         struct address_space *mapping = page->mapping;
582         loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
583         struct inode *inode;
584         unsigned long end_index;
585         char *buffer;
586         yaffs_Object *obj;
587         int nWritten = 0;
588         unsigned nBytes;
589
590         if (!mapping)
591                 BUG();
592         inode = mapping->host;
593         if (!inode)
594                 BUG();
595
596         if (offset > inode->i_size) {
597                 T(YAFFS_TRACE_OS,
598                   (KERN_DEBUG
599                    "yaffs_writepage at %08x, inode size = %08x!!!\n",
600                    (unsigned)(page->index << PAGE_CACHE_SHIFT),
601                    (unsigned)inode->i_size));
602                 T(YAFFS_TRACE_OS,
603                   (KERN_DEBUG "                -> don't care!!\n"));
604                 unlock_page(page);
605                 return 0;
606         }
607
608         end_index = inode->i_size >> PAGE_CACHE_SHIFT;
609
610         /* easy case */
611         if (page->index < end_index) {
612                 nBytes = PAGE_CACHE_SIZE;
613         } else {
614                 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
615         }
616
617         get_page(page);
618
619         buffer = kmap(page);
620
621         obj = yaffs_InodeToObject(inode);
622         yaffs_GrossLock(obj->myDev);
623
624         T(YAFFS_TRACE_OS,
625           (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
626            (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
627         T(YAFFS_TRACE_OS,
628           (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
629            (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
630
631         nWritten =
632             yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
633                                   nBytes, 0);
634
635         T(YAFFS_TRACE_OS,
636           (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
637            (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
638
639         yaffs_GrossUnlock(obj->myDev);
640
641         kunmap(page);
642         SetPageUptodate(page);
643         UnlockPage(page);
644         put_page(page);
645
646         return (nWritten == nBytes) ? 0 : -ENOSPC;
647 }
648
649 static int yaffs_prepare_write(struct file *f, struct page *pg,
650                                unsigned offset, unsigned to)
651 {
652
653         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
654         if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
655                 return yaffs_readpage_nolock(f, pg);
656
657         return 0;
658
659 }
660
661 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
662                               unsigned to)
663 {
664
665         void *addr = page_address(pg) + offset;
666         loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
667         int nBytes = to - offset;
668         int nWritten;
669
670         unsigned spos = pos;
671         unsigned saddr = (unsigned)addr;
672
673         T(YAFFS_TRACE_OS,
674           (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
675            spos, nBytes));
676
677         nWritten = yaffs_file_write(f, addr, nBytes, &pos);
678
679         if (nWritten != nBytes) {
680                 T(YAFFS_TRACE_OS,
681                   (KERN_DEBUG
682                    "yaffs_commit_write not same size nWritten %d  nBytes %d\n",
683                    nWritten, nBytes));
684                 SetPageError(pg);
685                 ClearPageUptodate(pg);
686         } else {
687                 SetPageUptodate(pg);
688         }
689
690         T(YAFFS_TRACE_OS,
691           (KERN_DEBUG "yaffs_commit_write returning %d\n",
692            nWritten == nBytes ? 0 : nWritten));
693
694         return nWritten == nBytes ? 0 : nWritten;
695
696 }
697
698 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
699 {
700         if (inode && obj) {
701
702
703                 /* Check mode against the variant type and attempt to repair if broken. */
704                 __u32 mode = obj->yst_mode;
705                 switch( obj->variantType ){
706                 case YAFFS_OBJECT_TYPE_FILE :
707                         if( ! S_ISREG(mode) ){
708                                 obj->yst_mode &= ~S_IFMT;
709                                 obj->yst_mode |= S_IFREG;
710                         }
711  
712                         break;
713                 case YAFFS_OBJECT_TYPE_SYMLINK :
714                         if( ! S_ISLNK(mode) ){
715                                 obj->yst_mode &= ~S_IFMT;
716                                 obj->yst_mode |= S_IFLNK;
717                         }
718  
719                         break;
720                 case YAFFS_OBJECT_TYPE_DIRECTORY :
721                         if( ! S_ISDIR(mode) ){
722                                 obj->yst_mode &= ~S_IFMT;
723                                 obj->yst_mode |= S_IFDIR;
724                         }
725  
726                         break;
727                 case YAFFS_OBJECT_TYPE_UNKNOWN :
728                 case YAFFS_OBJECT_TYPE_HARDLINK :
729                 case YAFFS_OBJECT_TYPE_SPECIAL :
730                 default:
731                         /* TODO? */
732                         break;
733                 }
734
735                 inode->i_ino = obj->objectId;
736                 inode->i_mode = obj->yst_mode;
737                 inode->i_uid = obj->yst_uid;
738                 inode->i_gid = obj->yst_gid;
739 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
740                 inode->i_blksize = inode->i_sb->s_blocksize;
741 #endif
742 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
743
744                 inode->i_rdev = old_decode_dev(obj->yst_rdev);
745                 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
746                 inode->i_atime.tv_nsec = 0;
747                 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
748                 inode->i_mtime.tv_nsec = 0;
749                 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
750                 inode->i_ctime.tv_nsec = 0;
751 #else
752                 inode->i_rdev = obj->yst_rdev;
753                 inode->i_atime = obj->yst_atime;
754                 inode->i_mtime = obj->yst_mtime;
755                 inode->i_ctime = obj->yst_ctime;
756 #endif
757                 inode->i_size = yaffs_GetObjectFileLength(obj);
758                 inode->i_blocks = (inode->i_size + 511) >> 9;
759
760                 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
761
762                 T(YAFFS_TRACE_OS,
763                   (KERN_DEBUG
764                    "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
765                    inode->i_mode, inode->i_uid, inode->i_gid,
766                    (int)inode->i_size, atomic_read(&inode->i_count)));
767
768                 switch (obj->yst_mode & S_IFMT) {
769                 default:        /* fifo, device or socket */
770 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
771                         init_special_inode(inode, obj->yst_mode,
772                                            old_decode_dev(obj->yst_rdev));
773 #else
774                         init_special_inode(inode, obj->yst_mode,
775                                            (dev_t) (obj->yst_rdev));
776 #endif
777                         break;
778                 case S_IFREG:   /* file */
779                         inode->i_op = &yaffs_file_inode_operations;
780                         inode->i_fop = &yaffs_file_operations;
781                         inode->i_mapping->a_ops =
782                             &yaffs_file_address_operations;
783                         break;
784                 case S_IFDIR:   /* directory */
785                         inode->i_op = &yaffs_dir_inode_operations;
786                         inode->i_fop = &yaffs_dir_operations;
787                         break;
788                 case S_IFLNK:   /* symlink */
789                         inode->i_op = &yaffs_symlink_inode_operations;
790                         break;
791                 }
792
793                 yaffs_InodeToObjectLV(inode) = obj;
794
795                 obj->myInode = inode;
796
797         } else {
798                 T(YAFFS_TRACE_OS,
799                   (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
800         }
801
802 }
803
804 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
805                               yaffs_Object * obj)
806 {
807         struct inode *inode;
808
809         if (!sb) {
810                 T(YAFFS_TRACE_OS,
811                   (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
812                 return NULL;
813
814         }
815
816         if (!obj) {
817                 T(YAFFS_TRACE_OS,
818                   (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
819                 return NULL;
820
821         }
822
823         T(YAFFS_TRACE_OS,
824           (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
825
826         inode = iget(sb, obj->objectId);
827
828         /* NB Side effect: iget calls back to yaffs_read_inode(). */
829         /* iget also increments the inode's i_count */
830         /* NB You can't be holding grossLock or deadlock will happen! */
831
832         return inode;
833 }
834
835 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
836                                 loff_t * pos)
837 {
838         yaffs_Object *obj;
839         int nWritten, ipos;
840         struct inode *inode;
841         yaffs_Device *dev;
842
843         obj = yaffs_DentryToObject(f->f_dentry);
844
845         dev = obj->myDev;
846
847         yaffs_GrossLock(dev);
848
849         inode = f->f_dentry->d_inode;
850
851         if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
852                 ipos = inode->i_size;
853         } else {
854                 ipos = *pos;
855         }
856
857         if (!obj) {
858                 T(YAFFS_TRACE_OS,
859                   (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
860         } else {
861                 T(YAFFS_TRACE_OS,
862                   (KERN_DEBUG
863                    "yaffs_file_write about to write writing %d bytes"
864                    "to object %d at %d\n",
865                    n, obj->objectId, ipos));
866         }
867
868         nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
869
870         T(YAFFS_TRACE_OS,
871           (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
872            n, nWritten, ipos));
873         if (nWritten > 0) {
874                 ipos += nWritten;
875                 *pos = ipos;
876                 if (ipos > inode->i_size) {
877                         inode->i_size = ipos;
878                         inode->i_blocks = (ipos + 511) >> 9;
879
880                         T(YAFFS_TRACE_OS,
881                           (KERN_DEBUG
882                            "yaffs_file_write size updated to %d bytes, "
883                            "%d blocks\n",
884                            ipos, (int)(inode->i_blocks)));
885                 }
886
887         }
888         yaffs_GrossUnlock(dev);
889         return nWritten == 0 ? -ENOSPC : nWritten;
890 }
891
892 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
893 {
894         yaffs_Object *obj;
895         yaffs_Device *dev;
896         struct inode *inode = f->f_dentry->d_inode;
897         unsigned long offset, curoffs;
898         struct list_head *i;
899         yaffs_Object *l;
900
901         char name[YAFFS_MAX_NAME_LENGTH + 1];
902
903         obj = yaffs_DentryToObject(f->f_dentry);
904         dev = obj->myDev;
905
906         yaffs_GrossLock(dev);
907
908         offset = f->f_pos;
909
910         T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
911
912         if (offset == 0) {
913                 T(YAFFS_TRACE_OS,
914                   (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
915                    (int)inode->i_ino));
916                 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
917                     < 0) {
918                         goto out;
919                 }
920                 offset++;
921                 f->f_pos++;
922         }
923         if (offset == 1) {
924                 T(YAFFS_TRACE_OS,
925                   (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
926                    (int)f->f_dentry->d_parent->d_inode->i_ino));
927                 if (filldir
928                     (dirent, "..", 2, offset,
929                      f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
930                         goto out;
931                 }
932                 offset++;
933                 f->f_pos++;
934         }
935
936         curoffs = 1;
937
938         /* If the directory has changed since the open or last call to
939            readdir, rewind to after the 2 canned entries. */
940
941         if (f->f_version != inode->i_version) {
942                 offset = 2;
943                 f->f_pos = offset;
944                 f->f_version = inode->i_version;
945         }
946
947         list_for_each(i, &obj->variant.directoryVariant.children) {
948                 curoffs++;
949                 if (curoffs >= offset) {
950                         l = list_entry(i, yaffs_Object, siblings);
951
952                         yaffs_GetObjectName(l, name,
953                                             YAFFS_MAX_NAME_LENGTH + 1);
954                         T(YAFFS_TRACE_OS,
955                           (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
956                            yaffs_GetObjectInode(l)));
957
958                         if (filldir(dirent,
959                                     name,
960                                     strlen(name),
961                                     offset,
962                                     yaffs_GetObjectInode(l),
963                                     yaffs_GetObjectType(l))
964                             < 0) {
965                                 goto up_and_out;
966                         }
967
968                         offset++;
969                         f->f_pos++;
970                 }
971         }
972
973       up_and_out:
974       out:
975
976         yaffs_GrossUnlock(dev);
977
978         return 0;
979 }
980
981 /*
982  * File creation. Allocate an inode, and we're done..
983  */
984 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
985 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
986                        dev_t rdev)
987 #else
988 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
989                        int rdev)
990 #endif
991 {
992         struct inode *inode;
993
994         yaffs_Object *obj = NULL;
995         yaffs_Device *dev;
996
997         yaffs_Object *parent = yaffs_InodeToObject(dir);
998
999         int error = -ENOSPC;
1000         uid_t uid = current->fsuid;
1001         gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1002         
1003         if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1004                 mode |= S_ISGID;
1005
1006         if (parent) {
1007                 T(YAFFS_TRACE_OS,
1008                   (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
1009                    parent->objectId, parent->variantType));
1010         } else {
1011                 T(YAFFS_TRACE_OS,
1012                   (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
1013                 return -EPERM;
1014         }
1015
1016         T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1017                            "mode %x dev %x\n",
1018                            dentry->d_name.name, mode, rdev));
1019
1020         dev = parent->myDev;
1021
1022         yaffs_GrossLock(dev);
1023
1024         switch (mode & S_IFMT) {
1025         default:
1026                 /* Special (socket, fifo, device...) */
1027                 T(YAFFS_TRACE_OS, (KERN_DEBUG
1028                                    "yaffs_mknod: making special\n"));
1029 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1030                 obj =
1031                     yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1032                                        gid, old_encode_dev(rdev));
1033 #else
1034                 obj =
1035                     yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1036                                        gid, rdev);
1037 #endif
1038                 break;
1039         case S_IFREG:           /* file          */
1040                 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1041                 obj =
1042                     yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1043                                     gid);
1044                 break;
1045         case S_IFDIR:           /* directory */
1046                 T(YAFFS_TRACE_OS,
1047                   (KERN_DEBUG "yaffs_mknod: making directory\n"));
1048                 obj =
1049                     yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1050                                          uid, gid);
1051                 break;
1052         case S_IFLNK:           /* symlink */
1053                 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1054                 obj = NULL;     /* Do we ever get here? */
1055                 break;
1056         }
1057         
1058         /* Can not call yaffs_get_inode() with gross lock held */
1059         yaffs_GrossUnlock(dev);
1060
1061         if (obj) {
1062                 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1063                 d_instantiate(dentry, inode);
1064                 T(YAFFS_TRACE_OS,
1065                   (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
1066                    obj->objectId, atomic_read(&inode->i_count)));
1067                 error = 0;
1068         } else {
1069                 T(YAFFS_TRACE_OS,
1070                   (KERN_DEBUG "yaffs_mknod failed making object\n"));
1071                 error = -ENOMEM;
1072         }
1073
1074         return error;
1075 }
1076
1077 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1078 {
1079         int retVal;
1080         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
1081         retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1082 #if 0
1083         /* attempt to fix dir bug - didn't work */
1084         if (!retVal) {
1085                 dget(dentry);
1086         }
1087 #endif
1088         return retVal;
1089 }
1090
1091 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1092 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1093                         struct nameidata *n)
1094 #else
1095 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1096 #endif
1097 {
1098         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1099         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1100 }
1101
1102 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1103 {
1104         int retVal;
1105
1106         yaffs_Device *dev;
1107
1108         T(YAFFS_TRACE_OS,
1109           (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1110            dentry->d_name.name));
1111
1112         dev = yaffs_InodeToObject(dir)->myDev;
1113
1114         yaffs_GrossLock(dev);
1115
1116         retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1117
1118         if (retVal == YAFFS_OK) {
1119                 dentry->d_inode->i_nlink--;
1120                 dir->i_version++;
1121                 yaffs_GrossUnlock(dev);
1122                 mark_inode_dirty(dentry->d_inode);
1123                 return 0;
1124         }
1125         yaffs_GrossUnlock(dev);
1126         return -ENOTEMPTY;
1127 }
1128
1129 /*
1130  * Create a link...
1131  */
1132 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1133                       struct dentry *dentry)
1134 {
1135         struct inode *inode = old_dentry->d_inode;
1136         yaffs_Object *obj = NULL;
1137         yaffs_Object *link = NULL;
1138         yaffs_Device *dev;
1139
1140         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1141
1142         obj = yaffs_InodeToObject(inode);
1143         dev = obj->myDev;
1144
1145         yaffs_GrossLock(dev);
1146
1147         if (!S_ISDIR(inode->i_mode))    /* Don't link directories */
1148         {
1149                 link =
1150                     yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1151                                obj);
1152         }
1153
1154         if (link) {
1155                 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1156                 d_instantiate(dentry, old_dentry->d_inode);
1157                 atomic_inc(&old_dentry->d_inode->i_count);
1158                 T(YAFFS_TRACE_OS,
1159                   (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1160                    old_dentry->d_inode->i_nlink,
1161                    atomic_read(&old_dentry->d_inode->i_count)));
1162
1163         }
1164
1165         yaffs_GrossUnlock(dev);
1166
1167         if (link) {
1168
1169                 return 0;
1170         }
1171
1172         return -EPERM;
1173 }
1174
1175 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1176                          const char *symname)
1177 {
1178         yaffs_Object *obj;
1179         yaffs_Device *dev;
1180         uid_t uid = current->fsuid;
1181         gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1182
1183         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1184
1185         dev = yaffs_InodeToObject(dir)->myDev;
1186         yaffs_GrossLock(dev);
1187         obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1188                                  S_IFLNK | S_IRWXUGO, uid, gid, symname);
1189         yaffs_GrossUnlock(dev);
1190
1191         if (obj) {
1192
1193                 struct inode *inode;
1194
1195                 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1196                 d_instantiate(dentry, inode);
1197                 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1198                 return 0;
1199         } else {
1200                 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1201
1202         }
1203
1204         return -ENOMEM;
1205 }
1206
1207 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1208                              int datasync)
1209 {
1210
1211         yaffs_Object *obj;
1212         yaffs_Device *dev;
1213
1214         obj = yaffs_DentryToObject(dentry);
1215
1216         dev = obj->myDev;
1217
1218         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1219         yaffs_GrossLock(dev);
1220         yaffs_FlushFile(obj, 1);
1221         yaffs_GrossUnlock(dev);
1222         return 0;
1223 }
1224
1225 /*
1226  * The VFS layer already does all the dentry stuff for rename.
1227  *
1228  * NB: POSIX says you can rename an object over an old object of the same name
1229  */
1230 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1231                         struct inode *new_dir, struct dentry *new_dentry)
1232 {
1233         yaffs_Device *dev;
1234         int retVal = YAFFS_FAIL;
1235         yaffs_Object *target;
1236
1237         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1238         dev = yaffs_InodeToObject(old_dir)->myDev;
1239
1240         yaffs_GrossLock(dev);
1241
1242         /* Check if the target is an existing directory that is not empty. */
1243         target =
1244             yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1245                                    new_dentry->d_name.name);
1246         
1247         
1248
1249         if (target &&
1250             target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1251             !list_empty(&target->variant.directoryVariant.children)) {
1252             
1253                 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1254
1255                 retVal = YAFFS_FAIL;
1256         } else {
1257
1258                 /* Now does unlinking internally using shadowing mechanism */
1259                 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1260                 
1261                 retVal =
1262                     yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1263                                        old_dentry->d_name.name,
1264                                        yaffs_InodeToObject(new_dir),
1265                                        new_dentry->d_name.name);
1266
1267         }
1268         yaffs_GrossUnlock(dev);
1269
1270         if (retVal == YAFFS_OK) {
1271                 if(target) {
1272                         new_dentry->d_inode->i_nlink--;
1273                         mark_inode_dirty(new_dentry->d_inode);
1274                 }
1275
1276                 return 0;
1277         } else {
1278                 return -ENOTEMPTY;
1279         }
1280
1281 }
1282
1283 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1284 {
1285         struct inode *inode = dentry->d_inode;
1286         int error;
1287         yaffs_Device *dev;
1288
1289         T(YAFFS_TRACE_OS,
1290           (KERN_DEBUG "yaffs_setattr of object %d\n",
1291            yaffs_InodeToObject(inode)->objectId));
1292
1293         if ((error = inode_change_ok(inode, attr)) == 0) {
1294
1295                 dev = yaffs_InodeToObject(inode)->myDev;
1296                 yaffs_GrossLock(dev);
1297                 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1298                     YAFFS_OK) {
1299                         error = 0;
1300                 } else {
1301                         error = -EPERM;
1302                 }
1303                 yaffs_GrossUnlock(dev);
1304                 if (!error)
1305                         error = inode_setattr(inode, attr);
1306         }
1307         return error;
1308 }
1309
1310 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1311 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1312 {
1313         yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1314         struct super_block *sb = dentry->d_sb;
1315 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1316 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1317 {
1318         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1319 #else
1320 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1321 {
1322         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1323 #endif
1324
1325         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1326
1327         yaffs_GrossLock(dev);
1328
1329         buf->f_type = YAFFS_MAGIC;
1330         buf->f_bsize = sb->s_blocksize;
1331         buf->f_namelen = 255;
1332         if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1333
1334                 buf->f_blocks =
1335                     (dev->endBlock - dev->startBlock +
1336                      1) * dev->nChunksPerBlock / (sb->s_blocksize /
1337                                                   dev->nDataBytesPerChunk);
1338                 buf->f_bfree =
1339                     yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
1340                                                         dev->nDataBytesPerChunk);
1341         } else {
1342
1343                 buf->f_blocks =
1344                     (dev->endBlock - dev->startBlock +
1345                      1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
1346                                                   sb->s_blocksize);
1347                 buf->f_bfree =
1348                     yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
1349                                                         sb->s_blocksize);
1350         }
1351         buf->f_files = 0;
1352         buf->f_ffree = 0;
1353         buf->f_bavail = buf->f_bfree;
1354
1355         yaffs_GrossUnlock(dev);
1356         return 0;
1357 }
1358
1359
1360 /**
1361 static int yaffs_do_sync_fs(struct super_block *sb)
1362 {
1363
1364         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1365         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
1366
1367         if(sb->s_dirt) {
1368                 yaffs_GrossLock(dev);
1369
1370                 if(dev)
1371                         yaffs_CheckpointSave(dev);
1372                 
1373                 yaffs_GrossUnlock(dev);
1374
1375                 sb->s_dirt = 0;
1376         }
1377         return 0;
1378 }
1379 **/
1380
1381 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1382 static void yaffs_write_super(struct super_block *sb)
1383 #else
1384 static int yaffs_write_super(struct super_block *sb)
1385 #endif
1386 {
1387
1388         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
1389 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
1390         return 0; /* yaffs_do_sync_fs(sb);*/
1391 #endif
1392 }
1393
1394
1395 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1396 static int yaffs_sync_fs(struct super_block *sb, int wait)
1397 #else
1398 static int yaffs_sync_fs(struct super_block *sb)
1399 #endif
1400 {
1401
1402         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
1403         
1404         return 0; /* yaffs_do_sync_fs(sb);*/
1405         
1406 }
1407
1408
1409 static void yaffs_read_inode(struct inode *inode)
1410 {
1411         /* NB This is called as a side effect of other functions, but
1412          * we had to release the lock to prevent deadlocks, so 
1413          * need to lock again.
1414          */
1415
1416         yaffs_Object *obj;
1417         yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1418
1419         T(YAFFS_TRACE_OS,
1420           (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1421
1422         yaffs_GrossLock(dev);
1423         
1424         obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1425
1426         yaffs_FillInodeFromObject(inode, obj);
1427
1428         yaffs_GrossUnlock(dev);
1429 }
1430
1431 static LIST_HEAD(yaffs_dev_list);
1432
1433 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1434 {
1435         yaffs_Device    *dev = yaffs_SuperToDevice(sb);
1436
1437         if( *flags & MS_RDONLY ) {
1438                 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1439             
1440                 T(YAFFS_TRACE_OS,
1441                         (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
1442
1443                 yaffs_GrossLock(dev);
1444          
1445                 yaffs_FlushEntireDeviceCache(dev);
1446         
1447                 yaffs_CheckpointSave(dev);
1448  
1449                 if (mtd->sync)
1450                         mtd->sync(mtd);
1451
1452                 yaffs_GrossUnlock(dev);
1453         }
1454         else {
1455                 T(YAFFS_TRACE_OS, 
1456                         (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
1457         }
1458  
1459         return 0;
1460 }
1461
1462 static void yaffs_put_super(struct super_block *sb)
1463 {
1464         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1465
1466         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
1467
1468         yaffs_GrossLock(dev);
1469         
1470         yaffs_FlushEntireDeviceCache(dev);
1471
1472         yaffs_CheckpointSave(dev);
1473
1474         if (dev->putSuperFunc) {
1475                 dev->putSuperFunc(sb);
1476         }
1477
1478         yaffs_Deinitialise(dev);
1479         
1480         yaffs_GrossUnlock(dev);
1481
1482         /* we assume this is protected by lock_kernel() in mount/umount */
1483         list_del(&dev->devList);
1484         
1485         if(dev->spareBuffer){
1486                 YFREE(dev->spareBuffer);
1487                 dev->spareBuffer = NULL;
1488         }
1489
1490         kfree(dev);
1491 }
1492
1493
1494 static void yaffs_MTDPutSuper(struct super_block *sb)
1495 {
1496
1497         struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1498
1499         if (mtd->sync) {
1500                 mtd->sync(mtd);
1501         }
1502
1503         put_mtd_device(mtd);
1504 }
1505
1506
1507 static void yaffs_MarkSuperBlockDirty(void *vsb)
1508 {
1509         struct super_block *sb = (struct super_block *)vsb;
1510         
1511         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
1512 //      if(sb)
1513 //              sb->s_dirt = 1;
1514 }
1515
1516 typedef struct {
1517         int inband_tags;
1518         int skip_checkpoint_read;
1519         int skip_checkpoint_write;
1520         int no_cache;
1521 } yaffs_options;
1522
1523 #define MAX_OPT_LEN 20
1524 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
1525 {
1526         char cur_opt[MAX_OPT_LEN+1];
1527         int p;
1528         int error = 0;
1529         
1530         /* Parse through the options which is a comma seperated list */
1531         
1532         while(options_str && *options_str && !error){
1533                 memset(cur_opt,0,MAX_OPT_LEN+1);
1534                 p = 0;
1535                 
1536                 while(*options_str && *options_str != ','){
1537                         if(p < MAX_OPT_LEN){
1538                                 cur_opt[p] = *options_str;
1539                                 p++;
1540                         }
1541                         options_str++;
1542                 }
1543                 
1544                 if(!strcmp(cur_opt,"inband-tags"))
1545                         options->inband_tags = 1;
1546                 else if(!strcmp(cur_opt,"no-cache"))
1547                         options->no_cache = 1;
1548                 else if(!strcmp(cur_opt,"no-checkpoint-read"))
1549                         options->skip_checkpoint_read = 1;
1550                 else if(!strcmp(cur_opt,"no-checkpoint-write"))
1551                         options->skip_checkpoint_write = 1;
1552                 else if(!strcmp(cur_opt,"no-checkpoint")){
1553                         options->skip_checkpoint_read = 1;
1554                         options->skip_checkpoint_write = 1;
1555                 } else {
1556                         printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
1557                         error = 1;
1558                 }
1559                 
1560         }
1561
1562         return error;
1563 }
1564
1565 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1566                                                      struct super_block *sb,
1567                                                      void *data, int silent)
1568 {
1569         int nBlocks;
1570         struct inode *inode = NULL;
1571         struct dentry *root;
1572         yaffs_Device *dev = 0;
1573         char devname_buf[BDEVNAME_SIZE + 1];
1574         struct mtd_info *mtd;
1575         int err;
1576         char *data_str = (char *)data;
1577         
1578         yaffs_options options;
1579
1580         sb->s_magic = YAFFS_MAGIC;
1581         sb->s_op = &yaffs_super_ops;
1582
1583         if (!sb)
1584                 printk(KERN_INFO "yaffs: sb is NULL\n");
1585         else if (!sb->s_dev)
1586                 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1587         else if (!yaffs_devname(sb, devname_buf))
1588                 printk(KERN_INFO "yaffs: devname is NULL\n");
1589         else
1590                 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1591                        sb->s_dev,
1592                        yaffs_devname(sb, devname_buf));
1593                     
1594         if(!data_str)
1595                 data_str = "";
1596    
1597         printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
1598         
1599         memset(&options,0,sizeof(options));
1600         
1601         if(yaffs_parse_options(&options,data_str)){
1602                 /* Option parsing failed */
1603                 return NULL;
1604         }
1605
1606
1607         sb->s_blocksize = PAGE_CACHE_SIZE;
1608         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1609         T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1610         T(YAFFS_TRACE_OS,
1611           ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1612
1613 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1614         T(YAFFS_TRACE_OS,
1615           ("yaffs: Write verification disabled. All guarantees "
1616            "null and void\n"));
1617 #endif
1618
1619         T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1620                                "\"%s\"\n",
1621                                MAJOR(sb->s_dev), MINOR(sb->s_dev),
1622                                yaffs_devname(sb, devname_buf)));
1623
1624         /* Check it's an mtd device..... */
1625         if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1626                 return NULL;    /* This isn't an mtd device */
1627         }
1628         /* Get the device */
1629         mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1630         if (!mtd) {
1631                 T(YAFFS_TRACE_ALWAYS,
1632                   ("yaffs: MTD device #%u doesn't appear to exist\n",
1633                    MINOR(sb->s_dev)));
1634                 return NULL;
1635         }
1636         /* Check it's NAND */
1637         if (mtd->type != MTD_NANDFLASH) {
1638                 T(YAFFS_TRACE_ALWAYS,
1639                   ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1640                 return NULL;
1641         }
1642
1643         T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1644         T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1645         T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1646         T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1647         T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1648         T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1649         T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1650         T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
1651         T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1652         T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1653         T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1654         
1655 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1656
1657         if (yaffsVersion == 1 && 
1658 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1659             mtd->writesize >= 2048) {
1660 #else
1661             mtd->oobblock >= 2048) {
1662 #endif
1663             T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1664             yaffsVersion = 2;
1665         }       
1666         
1667         /* Added NCB 26/5/2006 for completeness */
1668         if (yaffsVersion == 2 && 
1669 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1670             mtd->writesize == 512) {
1671 #else
1672             mtd->oobblock == 512) {
1673 #endif
1674             T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
1675             yaffsVersion = 1;
1676         }       
1677
1678 #endif
1679
1680         if (yaffsVersion == 2) {
1681                 /* Check for version 2 style functions */
1682                 if (!mtd->erase ||
1683                     !mtd->block_isbad ||
1684                     !mtd->block_markbad ||
1685                     !mtd->read ||
1686                     !mtd->write ||
1687 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1688                     !mtd->read_oob || !mtd->write_oob) {
1689 #else
1690                     !mtd->write_ecc ||
1691                     !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1692 #endif
1693                         T(YAFFS_TRACE_ALWAYS,
1694                           ("yaffs: MTD device does not support required "
1695                            "functions\n"));;
1696                         return NULL;
1697                 }
1698
1699 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1700                 if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1701 #else
1702                 if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1703 #endif
1704                     mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
1705                         T(YAFFS_TRACE_ALWAYS,
1706                           ("yaffs: MTD device does not have the "
1707                            "right page sizes\n"));
1708                         return NULL;
1709                 }
1710         } else {
1711                 /* Check for V1 style functions */
1712                 if (!mtd->erase ||
1713                     !mtd->read ||
1714                     !mtd->write ||
1715 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1716                     !mtd->read_oob || !mtd->write_oob) {
1717 #else
1718                     !mtd->write_ecc ||
1719                     !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1720 #endif
1721                         T(YAFFS_TRACE_ALWAYS,
1722                           ("yaffs: MTD device does not support required "
1723                            "functions\n"));;
1724                         return NULL;
1725                 }
1726
1727                 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
1728                     mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1729                         T(YAFFS_TRACE_ALWAYS,
1730                           ("yaffs: MTD device does not support have the "
1731                            "right page sizes\n"));
1732                         return NULL;
1733                 }
1734         }
1735
1736         /* OK, so if we got here, we have an MTD that's NAND and looks
1737          * like it has the right capabilities
1738          * Set the yaffs_Device up for mtd
1739          */
1740
1741 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1742         sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1743 #else
1744         sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1745 #endif
1746         if (!dev) {
1747                 /* Deep shit could not allocate device structure */
1748                 T(YAFFS_TRACE_ALWAYS,
1749                   ("yaffs_read_super: Failed trying to allocate "
1750                    "yaffs_Device. \n"));
1751                 return NULL;
1752         }
1753
1754         memset(dev, 0, sizeof(yaffs_Device));
1755         dev->genericDevice = mtd;
1756         dev->name = mtd->name;
1757
1758         /* Set up the memory size parameters.... */
1759
1760         nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1761         dev->startBlock = 0;
1762         dev->endBlock = nBlocks - 1;
1763         dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1764         dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1765         dev->nReservedBlocks = 5;
1766         dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
1767
1768         /* ... and the functions. */
1769         if (yaffsVersion == 2) {
1770                 dev->writeChunkWithTagsToNAND =
1771                     nandmtd2_WriteChunkWithTagsToNAND;
1772                 dev->readChunkWithTagsFromNAND =
1773                     nandmtd2_ReadChunkWithTagsFromNAND;
1774                 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1775                 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1776                 dev->spareBuffer = YMALLOC(mtd->oobsize);
1777                 dev->isYaffs2 = 1;
1778 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1779                 dev->nDataBytesPerChunk = mtd->writesize;
1780                 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
1781 #else
1782                 dev->nDataBytesPerChunk = mtd->oobblock;
1783                 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1784 #endif
1785                 nBlocks = mtd->size / mtd->erasesize;
1786
1787                 dev->nCheckpointReservedBlocks = CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS;
1788                 dev->startBlock = 0;
1789                 dev->endBlock = nBlocks - 1;
1790         } else {
1791 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1792                 /* use the MTD interface in yaffs_mtdif1.c */
1793                 dev->writeChunkWithTagsToNAND =
1794                         nandmtd1_WriteChunkWithTagsToNAND;
1795                 dev->readChunkWithTagsFromNAND =
1796                         nandmtd1_ReadChunkWithTagsFromNAND;
1797                 dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
1798                 dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
1799 #else
1800                 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1801                 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1802 #endif
1803                 dev->isYaffs2 = 0;
1804         }
1805         /* ... and common functions */
1806         dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1807         dev->initialiseNAND = nandmtd_InitialiseNAND;
1808
1809         dev->putSuperFunc = yaffs_MTDPutSuper;
1810         
1811         dev->superBlock = (void *)sb;
1812         dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
1813         
1814
1815 #ifndef CONFIG_YAFFS_DOES_ECC
1816         dev->useNANDECC = 1;
1817 #endif
1818
1819 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1820         dev->wideTnodesDisabled = 1;
1821 #endif
1822
1823         dev->skipCheckpointRead = options.skip_checkpoint_read;
1824         dev->skipCheckpointWrite = options.skip_checkpoint_write;
1825         
1826         /* we assume this is protected by lock_kernel() in mount/umount */
1827         list_add_tail(&dev->devList, &yaffs_dev_list);
1828
1829         init_MUTEX(&dev->grossLock);
1830
1831         yaffs_GrossLock(dev);
1832
1833         err = yaffs_GutsInitialise(dev);
1834
1835         T(YAFFS_TRACE_OS,
1836           ("yaffs_read_super: guts initialised %s\n",
1837            (err == YAFFS_OK) ? "OK" : "FAILED"));
1838         
1839         /* Release lock before yaffs_get_inode() */
1840         yaffs_GrossUnlock(dev);
1841
1842         /* Create root inode */
1843         if (err == YAFFS_OK)
1844                 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1845                                         yaffs_Root(dev));
1846
1847         if (!inode)
1848                 return NULL;
1849
1850         inode->i_op = &yaffs_dir_inode_operations;
1851         inode->i_fop = &yaffs_dir_operations;
1852
1853         T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1854
1855         root = d_alloc_root(inode);
1856
1857         T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1858
1859         if (!root) {
1860                 iput(inode);
1861                 return NULL;
1862         }
1863         sb->s_root = root;
1864
1865         T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1866         return sb;
1867 }
1868
1869
1870 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1871 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
1872                                          int silent)
1873 {
1874         return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
1875 }
1876
1877 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1878 static int yaffs_read_super(struct file_system_type *fs,
1879                             int flags, const char *dev_name,
1880                             void *data, struct vfsmount *mnt)
1881 {
1882
1883         return get_sb_bdev(fs, flags, dev_name, data,
1884                            yaffs_internal_read_super_mtd, mnt);
1885 }
1886 #else
1887 static struct super_block *yaffs_read_super(struct file_system_type *fs,
1888                                             int flags, const char *dev_name,
1889                                             void *data)
1890 {
1891
1892         return get_sb_bdev(fs, flags, dev_name, data,
1893                            yaffs_internal_read_super_mtd);
1894 }
1895 #endif
1896
1897 static struct file_system_type yaffs_fs_type = {
1898         .owner = THIS_MODULE,
1899         .name = "yaffs",
1900         .get_sb = yaffs_read_super,
1901         .kill_sb = kill_block_super,
1902         .fs_flags = FS_REQUIRES_DEV,
1903 };
1904 #else
1905 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
1906                                             int silent)
1907 {
1908         return yaffs_internal_read_super(1, sb, data, silent);
1909 }
1910
1911 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
1912                       FS_REQUIRES_DEV);
1913 #endif
1914
1915
1916 #ifdef CONFIG_YAFFS_YAFFS2
1917
1918 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1919 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
1920                                           int silent)
1921 {
1922         return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
1923 }
1924
1925 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1926 static int yaffs2_read_super(struct file_system_type *fs,
1927                         int flags, const char *dev_name, void *data,
1928                         struct vfsmount *mnt)
1929 {
1930         return get_sb_bdev(fs, flags, dev_name, data,
1931                         yaffs2_internal_read_super_mtd, mnt);
1932 }
1933 #else
1934 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
1935                                              int flags, const char *dev_name,
1936                                              void *data)
1937 {
1938
1939         return get_sb_bdev(fs, flags, dev_name, data,
1940                            yaffs2_internal_read_super_mtd);
1941 }
1942 #endif
1943
1944 static struct file_system_type yaffs2_fs_type = {
1945         .owner = THIS_MODULE,
1946         .name = "yaffs2",
1947         .get_sb = yaffs2_read_super,
1948         .kill_sb = kill_block_super,
1949         .fs_flags = FS_REQUIRES_DEV,
1950 };
1951 #else
1952 static struct super_block *yaffs2_read_super(struct super_block *sb,
1953                                              void *data, int silent)
1954 {
1955         return yaffs_internal_read_super(2, sb, data, silent);
1956 }
1957
1958 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
1959                       FS_REQUIRES_DEV);
1960 #endif
1961
1962 #endif                          /* CONFIG_YAFFS_YAFFS2 */
1963
1964 static struct proc_dir_entry *my_proc_entry;
1965
1966 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
1967 {
1968         buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
1969         buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
1970         buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
1971         buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
1972         buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
1973         buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
1974         buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
1975         buf += sprintf(buf, "nCheckptResBlocks.. %d\n", dev->nCheckpointReservedBlocks);
1976         buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
1977         buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
1978         buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
1979         buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
1980         buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
1981         buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
1982         buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
1983         buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
1984         buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
1985         buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
1986         buf +=
1987             sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
1988         buf +=
1989             sprintf(buf, "passiveGCs......... %d\n",
1990                     dev->passiveGarbageCollections);
1991         buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
1992         buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
1993         buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
1994         buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
1995         buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
1996         buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
1997         buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
1998         buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
1999         buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2000         buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2001         buf +=
2002             sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2003         buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
2004         buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
2005
2006         return buf;
2007 }
2008
2009 static int yaffs_proc_read(char *page,
2010                            char **start,
2011                            off_t offset, int count, int *eof, void *data)
2012 {
2013         struct list_head *item;
2014         char *buf = page;
2015         int step = offset;
2016         int n = 0;
2017
2018         /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2019          * We use 'offset' (*ppos) to indicate where we are in devList.
2020          * This also assumes the user has posted a read buffer large
2021          * enough to hold the complete output; but that's life in /proc.
2022          */
2023
2024         *(int *)start = 1;
2025
2026         /* Print header first */
2027         if (step == 0) {
2028                 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2029                                "\n%s\n%s\n", yaffs_fs_c_version,
2030                                yaffs_guts_c_version);
2031         }
2032
2033         /* hold lock_kernel while traversing yaffs_dev_list */
2034         lock_kernel();
2035
2036         /* Locate and print the Nth entry.  Order N-squared but N is small. */
2037         list_for_each(item, &yaffs_dev_list) {
2038                 yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
2039                 if (n < step) {
2040                         n++;
2041                         continue;
2042                 }
2043                 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
2044                 buf = yaffs_dump_dev(buf, dev);
2045                 break;
2046         }
2047         unlock_kernel();
2048
2049         return buf - page < count ? buf - page : count;
2050 }
2051
2052 /**
2053  * Set the verbosity of the warnings and error messages.
2054  *
2055  * Note that the names can only be a..z or _ with the current code.
2056  */
2057
2058 static struct {
2059         char *mask_name;
2060         unsigned mask_bitfield;
2061 } mask_flags[] = {
2062         {"allocate", YAFFS_TRACE_ALLOCATE},
2063         {"always", YAFFS_TRACE_ALWAYS},
2064         {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2065         {"buffers", YAFFS_TRACE_BUFFERS},
2066         {"bug", YAFFS_TRACE_BUG},
2067         {"checkpt", YAFFS_TRACE_CHECKPOINT},
2068         {"deletion", YAFFS_TRACE_DELETION},
2069         {"erase", YAFFS_TRACE_ERASE},
2070         {"error", YAFFS_TRACE_ERROR},
2071         {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2072         {"gc", YAFFS_TRACE_GC},
2073         {"mtd", YAFFS_TRACE_MTD},
2074         {"nandaccess", YAFFS_TRACE_NANDACCESS},
2075         {"os", YAFFS_TRACE_OS},
2076         {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2077         {"scan", YAFFS_TRACE_SCAN},
2078         {"tracing", YAFFS_TRACE_TRACING},
2079
2080         {"verify", YAFFS_TRACE_VERIFY},
2081         {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2082         {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2083         {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2084
2085         {"write", YAFFS_TRACE_WRITE},
2086         {"all", 0xffffffff},
2087         {"none", 0},
2088         {NULL, 0},
2089 };
2090
2091 #define MAX_MASK_NAME_LENGTH 40
2092 static int yaffs_proc_write(struct file *file, const char *buf,
2093                                          unsigned long count, void *data)
2094 {
2095         unsigned rg = 0, mask_bitfield;
2096         char *end;
2097         char *mask_name;
2098         char *x; 
2099         char substring[MAX_MASK_NAME_LENGTH+1];
2100         int i;
2101         int done = 0;
2102         int add, len = 0;
2103         int pos = 0;
2104
2105         rg = yaffs_traceMask;
2106
2107         while (!done && (pos < count)) {
2108                 done = 1;
2109                 while ((pos < count) && isspace(buf[pos])) {
2110                         pos++;
2111                 }
2112
2113                 switch (buf[pos]) {
2114                 case '+':
2115                 case '-':
2116                 case '=':
2117                         add = buf[pos];
2118                         pos++;
2119                         break;
2120
2121                 default:
2122                         add = ' ';
2123                         break;
2124                 }
2125                 mask_name = NULL;
2126                 
2127                 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2128                 if (end > buf + pos) {
2129                         mask_name = "numeral";
2130                         len = end - (buf + pos);
2131                         done = 0;
2132                 } else {
2133                         for(x = buf + pos, i = 0; 
2134                             (*x == '_' || (*x >='a' && *x <= 'z')) &&
2135                             i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2136                             substring[i] = *x;
2137                         substring[i] = '\0';
2138                         
2139                         for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2140                                 //len = strlen(mask_flags[i].mask_name);
2141                                 //if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
2142                                 if(strcmp(substring,mask_flags[i].mask_name) == 0){
2143                                         mask_name = mask_flags[i].mask_name;
2144                                         mask_bitfield = mask_flags[i].mask_bitfield;
2145                                         done = 0;
2146                                         break;
2147                                 }
2148                         }
2149                 }
2150
2151                 if (mask_name != NULL) {
2152                         // pos += len;
2153                         done = 0;
2154                         switch(add) {
2155                         case '-':
2156                                 rg &= ~mask_bitfield;
2157                                 break;
2158                         case '+':
2159                                 rg |= mask_bitfield;
2160                                 break;
2161                         case '=':
2162                                 rg = mask_bitfield;
2163                                 break;
2164                         default:
2165                                 rg |= mask_bitfield;
2166                                 break;
2167                         }
2168                 }
2169         }
2170
2171         yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2172         
2173         printk("new trace = 0x%08X\n",yaffs_traceMask);
2174         
2175         if (rg & YAFFS_TRACE_ALWAYS) {
2176                 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2177                         char flag;
2178                         flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2179                         printk("%c%s\n", flag, mask_flags[i].mask_name);
2180                 }
2181         }
2182
2183         return count;
2184 }
2185
2186 /* Stuff to handle installation of file systems */
2187 struct file_system_to_install {
2188         struct file_system_type *fst;
2189         int installed;
2190 };
2191
2192 static struct file_system_to_install fs_to_install[] = {
2193 //#ifdef CONFIG_YAFFS_YAFFS1
2194         {&yaffs_fs_type, 0},
2195 //#endif
2196 //#ifdef CONFIG_YAFFS_YAFFS2
2197         {&yaffs2_fs_type, 0},
2198 //#endif
2199         {NULL, 0}
2200 };
2201
2202 static int __init init_yaffs_fs(void)
2203 {
2204         int error = 0;
2205         struct file_system_to_install *fsinst;
2206
2207         T(YAFFS_TRACE_ALWAYS,
2208           ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2209
2210         /* Install the proc_fs entry */
2211         my_proc_entry = create_proc_entry("yaffs",
2212                                                S_IRUGO | S_IFREG,
2213                                                &proc_root);
2214
2215         if (my_proc_entry) {
2216                 my_proc_entry->write_proc = yaffs_proc_write;
2217                 my_proc_entry->read_proc = yaffs_proc_read;
2218                 my_proc_entry->data = NULL;
2219         } else {
2220                 return -ENOMEM;
2221         }
2222
2223         /* Now add the file system entries */
2224
2225         fsinst = fs_to_install;
2226
2227         while (fsinst->fst && !error) {
2228                 error = register_filesystem(fsinst->fst);
2229                 if (!error) {
2230                         fsinst->installed = 1;
2231                 }
2232                 fsinst++;
2233         }
2234
2235         /* Any errors? uninstall  */
2236         if (error) {
2237                 fsinst = fs_to_install;
2238
2239                 while (fsinst->fst) {
2240                         if (fsinst->installed) {
2241                                 unregister_filesystem(fsinst->fst);
2242                                 fsinst->installed = 0;
2243                         }
2244                         fsinst++;
2245                 }
2246         }
2247
2248         return error;
2249 }
2250
2251 static void __exit exit_yaffs_fs(void)
2252 {
2253
2254         struct file_system_to_install *fsinst;
2255
2256         T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2257                                " removing. \n"));
2258
2259         remove_proc_entry("yaffs", &proc_root);
2260
2261         fsinst = fs_to_install;
2262
2263         while (fsinst->fst) {
2264                 if (fsinst->installed) {
2265                         unregister_filesystem(fsinst->fst);
2266                         fsinst->installed = 0;
2267                 }
2268                 fsinst++;
2269         }
2270
2271 }
2272
2273 module_init(init_yaffs_fs)
2274 module_exit(exit_yaffs_fs)
2275
2276 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2277 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2278 MODULE_LICENSE("GPL");