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