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