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