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