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