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