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