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