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