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