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