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