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