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