yaffs Finshed direct/timothy_tests/linux_test.
[yaffs2.git] / yaffs_vfs_single.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  * Acknowledgements:
9  * Luc van OostenRyck for numerous patches.
10  * Nick Bane for numerous patches.
11  * Nick Bane for 2.5/2.6 integration.
12  * Andras Toth for mknod rdev issue.
13  * Michael Fischer for finding the problem with inode inconsistency.
14  * Some code bodily lifted from JFFS
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License version 2 as
18  * published by the Free Software Foundation.
19  */
20
21 /*
22  *
23  * This is the file system front-end to YAFFS that hooks it up to
24  * the VFS.
25  *
26  * Special notes:
27  * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
28  *         this superblock
29  * >> 2.6: sb->s_fs_info  points to the struct yaffs_dev associated with this
30  *         superblock
31  * >> inode->u.generic_ip points to the associated struct yaffs_obj.
32  */
33
34 /*
35  * NB There are two variants of Linux VFS glue code. This variant supports
36  * a single version and should not include any multi-version code.
37  */
38 #include <linux/version.h>
39
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/slab.h>
43 #include <linux/init.h>
44 #include <linux/fs.h>
45 #include <linux/proc_fs.h>
46 #include <linux/smp_lock.h>
47 #include <linux/pagemap.h>
48 #include <linux/mtd/mtd.h>
49 #include <linux/interrupt.h>
50 #include <linux/string.h>
51 #include <linux/ctype.h>
52 #include <linux/namei.h>
53 #include <linux/exportfs.h>
54 #include <linux/kthread.h>
55 #include <linux/delay.h>
56 #include <linux/freezer.h>
57
58 #include <asm/div64.h>
59
60 #include <linux/statfs.h>
61
62 #define yaffs_devname(sb, buf)  bdevname(sb->s_bdev, buf)
63
64 #define YPROC_ROOT  NULL
65
66 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
67
68 #define WRITE_SIZE_STR "writesize"
69 #define WRITE_SIZE(mtd) ((mtd)->writesize)
70
71 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
72 {
73         uint64_t result = partition_size;
74         do_div(result, block_size);
75         return (uint32_t) result;
76 }
77
78 #include <linux/uaccess.h>
79 #include <linux/mtd/mtd.h>
80
81 #include "yportenv.h"
82 #include "yaffs_trace.h"
83 #include "yaffs_guts.h"
84 #include "yaffs_attribs.h"
85
86 #include "yaffs_linux.h"
87
88 #include "yaffs_mtdif.h"
89 #include "yaffs_mtdif1.h"
90 #include "yaffs_mtdif2.h"
91
92 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
93 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
94 unsigned int yaffs_auto_checkpoint = 1;
95 unsigned int yaffs_gc_control = 1;
96 unsigned int yaffs_bg_enable = 1;
97
98 /* Module Parameters */
99 module_param(yaffs_trace_mask, uint, 0644);
100 module_param(yaffs_wr_attempts, uint, 0644);
101 module_param(yaffs_auto_checkpoint, uint, 0644);
102 module_param(yaffs_gc_control, uint, 0644);
103 module_param(yaffs_bg_enable, uint, 0644);
104
105
106 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
107 #define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
108 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
109 #define yaffs_super_to_dev(sb)  ((struct yaffs_dev *)sb->s_fs_info)
110
111 #define update_dir_time(dir) do {\
112                         (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
113                 } while(0)
114
115
116 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
117 {
118         return yaffs_gc_control;
119 }
120
121 static void yaffs_gross_lock(struct yaffs_dev *dev)
122 {
123         yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
124         mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
125         yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
126 }
127
128 static void yaffs_gross_unlock(struct yaffs_dev *dev)
129 {
130         yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
131         mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
132 }
133
134 static void yaffs_fill_inode_from_obj(struct inode *inode,
135                                       struct yaffs_obj *obj);
136
137 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
138 {
139         struct inode *inode;
140         struct yaffs_obj *obj;
141         struct yaffs_dev *dev = yaffs_super_to_dev(sb);
142
143         yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
144
145         inode = iget_locked(sb, ino);
146         if (!inode)
147                 return ERR_PTR(-ENOMEM);
148         if (!(inode->i_state & I_NEW))
149                 return inode;
150
151         /* NB This is called as a side effect of other functions, but
152          * we had to release the lock to prevent deadlocks, so
153          * need to lock again.
154          */
155
156         yaffs_gross_lock(dev);
157
158         obj = yaffs_find_by_number(dev, inode->i_ino);
159
160         yaffs_fill_inode_from_obj(inode, obj);
161
162         yaffs_gross_unlock(dev);
163
164         unlock_new_inode(inode);
165         return inode;
166 }
167
168 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
169                               struct yaffs_obj *obj)
170 {
171         struct inode *inode;
172
173         if (!sb) {
174                 yaffs_trace(YAFFS_TRACE_OS,
175                         "yaffs_get_inode for NULL super_block!!");
176                 return NULL;
177
178         }
179
180         if (!obj) {
181                 yaffs_trace(YAFFS_TRACE_OS,
182                         "yaffs_get_inode for NULL object!!");
183                 return NULL;
184
185         }
186
187         yaffs_trace(YAFFS_TRACE_OS,
188                 "yaffs_get_inode for object %d",
189                 obj->obj_id);
190
191         inode = yaffs_iget(sb, obj->obj_id);
192         if (IS_ERR(inode))
193                 return NULL;
194
195         /* NB Side effect: iget calls back to yaffs_read_inode(). */
196         /* iget also increments the inode's i_count */
197         /* NB You can't be holding gross_lock or deadlock will happen! */
198
199         return inode;
200 }
201
202 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
203                        dev_t rdev)
204 {
205         struct inode *inode;
206
207         struct yaffs_obj *obj = NULL;
208         struct yaffs_dev *dev;
209
210         struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
211
212         int error = -ENOSPC;
213         uid_t uid = current->cred->fsuid;
214         gid_t gid =
215             (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
216
217         if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
218                 mode |= S_ISGID;
219
220         if (parent) {
221                 yaffs_trace(YAFFS_TRACE_OS,
222                         "yaffs_mknod: parent object %d type %d",
223                         parent->obj_id, parent->variant_type);
224         } else {
225                 yaffs_trace(YAFFS_TRACE_OS,
226                         "yaffs_mknod: could not get parent object");
227                 return -EPERM;
228         }
229
230         yaffs_trace(YAFFS_TRACE_OS,
231                 "yaffs_mknod: making oject for %s, mode %x dev %x",
232                 dentry->d_name.name, mode, rdev);
233
234         dev = parent->my_dev;
235
236         yaffs_gross_lock(dev);
237
238         switch (mode & S_IFMT) {
239         default:
240                 /* Special (socket, fifo, device...) */
241                 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
242                 obj =
243                     yaffs_create_special(parent, dentry->d_name.name, mode, uid,
244                                          gid, old_encode_dev(rdev));
245                 break;
246         case S_IFREG:           /* file          */
247                 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
248                 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
249                                         gid);
250                 break;
251         case S_IFDIR:           /* directory */
252                 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
253                 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
254                                        uid, gid);
255                 break;
256         case S_IFLNK:           /* symlink */
257                 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
258                 obj = NULL;     /* Do we ever get here? */
259                 break;
260         }
261
262         /* Can not call yaffs_get_inode() with gross lock held */
263         yaffs_gross_unlock(dev);
264
265         if (obj) {
266                 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
267                 d_instantiate(dentry, inode);
268                 update_dir_time(dir);
269                 yaffs_trace(YAFFS_TRACE_OS,
270                         "yaffs_mknod created object %d count = %d",
271                         obj->obj_id, atomic_read(&inode->i_count));
272                 error = 0;
273                 yaffs_fill_inode_from_obj(dir, parent);
274         } else {
275                 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
276                 error = -ENOMEM;
277         }
278
279         return error;
280 }
281
282 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
283 {
284         return yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
285 }
286
287 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
288                         struct nameidata *n)
289 {
290         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
291 }
292
293 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
294                       struct dentry *dentry)
295 {
296         struct inode *inode = old_dentry->d_inode;
297         struct yaffs_obj *obj = NULL;
298         struct yaffs_obj *link = NULL;
299         struct yaffs_dev *dev;
300
301         yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
302
303         obj = yaffs_inode_to_obj(inode);
304         dev = obj->my_dev;
305
306         yaffs_gross_lock(dev);
307
308         if (!S_ISDIR(inode->i_mode))    /* Don't link directories */
309                 link =
310                     yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
311                                    obj);
312
313         if (link) {
314                 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
315                 d_instantiate(dentry, old_dentry->d_inode);
316                 atomic_inc(&old_dentry->d_inode->i_count);
317                 yaffs_trace(YAFFS_TRACE_OS,
318                         "yaffs_link link count %d i_count %d",
319                         old_dentry->d_inode->i_nlink,
320                         atomic_read(&old_dentry->d_inode->i_count));
321         }
322
323         yaffs_gross_unlock(dev);
324
325         if (link) {
326                 update_dir_time(dir);
327                 return 0;
328         }
329
330         return -EPERM;
331 }
332
333 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
334                          const char *symname)
335 {
336         struct yaffs_obj *obj;
337         struct yaffs_dev *dev;
338         uid_t uid = current->cred->fsuid;
339         gid_t gid =
340             (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
341
342         yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
343
344         dev = yaffs_inode_to_obj(dir)->my_dev;
345         yaffs_gross_lock(dev);
346         obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
347                                    S_IFLNK | S_IRWXUGO, uid, gid, symname);
348         yaffs_gross_unlock(dev);
349
350         if (obj) {
351                 struct inode *inode;
352
353                 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
354                 d_instantiate(dentry, inode);
355                 update_dir_time(dir);
356                 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
357                 return 0;
358         } else {
359                 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
360         }
361
362         return -ENOMEM;
363 }
364
365 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
366                                    struct nameidata *n)
367 {
368         struct yaffs_obj *obj;
369         struct inode *inode = NULL;
370
371         struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
372
373         if (current != yaffs_dev_to_lc(dev)->readdir_process)
374                 yaffs_gross_lock(dev);
375
376         yaffs_trace(YAFFS_TRACE_OS,
377                 "yaffs_lookup for %d:%s",
378                 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
379
380         obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
381
382         obj = yaffs_get_equivalent_obj(obj);    /* in case it was a hardlink */
383
384         /* Can't hold gross lock when calling yaffs_get_inode() */
385         if (current != yaffs_dev_to_lc(dev)->readdir_process)
386                 yaffs_gross_unlock(dev);
387
388         if (obj) {
389                 yaffs_trace(YAFFS_TRACE_OS,
390                         "yaffs_lookup found %d", obj->obj_id);
391
392                 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
393
394                 if (inode) {
395                         yaffs_trace(YAFFS_TRACE_OS, "yaffs_loookup dentry");
396                         d_add(dentry, inode);
397                         /* return dentry; */
398                         return NULL;
399                 }
400
401         } else {
402                 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
403
404         }
405
406         d_add(dentry, inode);
407
408         return NULL;
409 }
410
411 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
412 {
413         int ret_val;
414
415         struct yaffs_dev *dev;
416         struct yaffs_obj *obj;
417
418         yaffs_trace(YAFFS_TRACE_OS,
419                 "yaffs_unlink %d:%s",
420                 (int)(dir->i_ino), dentry->d_name.name);
421         obj = yaffs_inode_to_obj(dir);
422         dev = obj->my_dev;
423
424         yaffs_gross_lock(dev);
425
426         ret_val = yaffs_unlinker(obj, dentry->d_name.name);
427
428         if (ret_val == YAFFS_OK) {
429                 dentry->d_inode->i_nlink--;
430                 dir->i_version++;
431                 yaffs_gross_unlock(dev);
432                 mark_inode_dirty(dentry->d_inode);
433                 update_dir_time(dir);
434                 return 0;
435         }
436         yaffs_gross_unlock(dev);
437         return -ENOTEMPTY;
438 }
439
440 static int yaffs_sync_object(struct file *file, int datasync)
441 {
442
443         struct yaffs_obj *obj;
444         struct yaffs_dev *dev;
445         struct dentry *dentry = file->f_path.dentry;
446
447         obj = yaffs_dentry_to_obj(dentry);
448
449         dev = obj->my_dev;
450
451         yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, "yaffs_sync_object");
452         yaffs_gross_lock(dev);
453         yaffs_flush_file(obj, 1, datasync);
454         yaffs_gross_unlock(dev);
455         return 0;
456 }
457 /*
458  * The VFS layer already does all the dentry stuff for rename.
459  *
460  * NB: POSIX says you can rename an object over an old object of the same name
461  */
462 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
463                         struct inode *new_dir, struct dentry *new_dentry)
464 {
465         struct yaffs_dev *dev;
466         int ret_val = YAFFS_FAIL;
467         struct yaffs_obj *target;
468
469         yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
470         dev = yaffs_inode_to_obj(old_dir)->my_dev;
471
472         yaffs_gross_lock(dev);
473
474         /* Check if the target is an existing directory that is not empty. */
475         target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
476                                     new_dentry->d_name.name);
477
478         if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
479             !list_empty(&target->variant.dir_variant.children)) {
480
481                 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
482
483                 ret_val = YAFFS_FAIL;
484         } else {
485                 /* Now does unlinking internally using shadowing mechanism */
486                 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
487
488                 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
489                                            old_dentry->d_name.name,
490                                            yaffs_inode_to_obj(new_dir),
491                                            new_dentry->d_name.name);
492         }
493         yaffs_gross_unlock(dev);
494
495         if (ret_val == YAFFS_OK) {
496                 if (target) {
497                         new_dentry->d_inode->i_nlink--;
498                         mark_inode_dirty(new_dentry->d_inode);
499                 }
500
501                 update_dir_time(old_dir);
502                 if (old_dir != new_dir)
503                         update_dir_time(new_dir);
504                 return 0;
505         } else {
506                 return -ENOTEMPTY;
507         }
508 }
509
510 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
511 {
512         struct inode *inode = dentry->d_inode;
513         int error = 0;
514         struct yaffs_dev *dev;
515
516         yaffs_trace(YAFFS_TRACE_OS,
517                 "yaffs_setattr of object %d",
518                 yaffs_inode_to_obj(inode)->obj_id);
519
520         /* Fail if a requested resize >= 2GB */
521         if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
522                 error = -EINVAL;
523
524         if (error == 0)
525                 error = inode_change_ok(inode, attr);
526         if (error == 0) {
527                 int result;
528                 if (!error) {
529                         setattr_copy(inode, attr);
530                         yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
531                         if (attr->ia_valid & ATTR_SIZE) {
532                                 truncate_setsize(inode, attr->ia_size);
533                                 inode->i_blocks = (inode->i_size + 511) >> 9;
534                         }
535                 }
536                 dev = yaffs_inode_to_obj(inode)->my_dev;
537                 if (attr->ia_valid & ATTR_SIZE) {
538                         yaffs_trace(YAFFS_TRACE_OS, "resize to %d(%x)",
539                                            (int)(attr->ia_size),
540                                            (int)(attr->ia_size));
541                 }
542                 yaffs_gross_lock(dev);
543                 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
544                 if (result == YAFFS_OK) {
545                         error = 0;
546                 } else {
547                         error = -EPERM;
548                 }
549                 yaffs_gross_unlock(dev);
550
551         }
552
553         yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
554
555         return error;
556 }
557
558 #ifdef CONFIG_YAFFS_XATTR
559 static int yaffs_setxattr(struct dentry *dentry, const char *name,
560                    const void *value, size_t size, int flags)
561 {
562         struct inode *inode = dentry->d_inode;
563         int error = 0;
564         struct yaffs_dev *dev;
565         struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
566
567         yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
568
569         if (error == 0) {
570                 int result;
571                 dev = obj->my_dev;
572                 yaffs_gross_lock(dev);
573                 result = yaffs_set_xattrib(obj, name, value, size, flags);
574                 if (result == YAFFS_OK)
575                         error = 0;
576                 else if (result < 0)
577                         error = result;
578                 yaffs_gross_unlock(dev);
579
580         }
581         yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
582
583         return error;
584 }
585
586 static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, void *buff,
587                        size_t size)
588 {
589         struct inode *inode = dentry->d_inode;
590         int error = 0;
591         struct yaffs_dev *dev;
592         struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
593
594         yaffs_trace(YAFFS_TRACE_OS,
595                 "yaffs_getxattr \"%s\" from object %d",
596                 name, obj->obj_id);
597
598         if (error == 0) {
599                 dev = obj->my_dev;
600                 yaffs_gross_lock(dev);
601                 error = yaffs_get_xattrib(obj, name, buff, size);
602                 yaffs_gross_unlock(dev);
603
604         }
605         yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
606
607         return error;
608 }
609
610 static int yaffs_removexattr(struct dentry *dentry, const char *name)
611 {
612         struct inode *inode = dentry->d_inode;
613         int error = 0;
614         struct yaffs_dev *dev;
615         struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
616
617         yaffs_trace(YAFFS_TRACE_OS,
618                 "yaffs_removexattr of object %d", obj->obj_id);
619
620         if (error == 0) {
621                 int result;
622                 dev = obj->my_dev;
623                 yaffs_gross_lock(dev);
624                 result = yaffs_remove_xattrib(obj, name);
625                 if (result == YAFFS_OK)
626                         error = 0;
627                 else if (result < 0)
628                         error = result;
629                 yaffs_gross_unlock(dev);
630
631         }
632         yaffs_trace(YAFFS_TRACE_OS,
633                 "yaffs_removexattr done returning %d", error);
634
635         return error;
636 }
637
638 static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
639 {
640         struct inode *inode = dentry->d_inode;
641         int error = 0;
642         struct yaffs_dev *dev;
643         struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
644
645         yaffs_trace(YAFFS_TRACE_OS,
646                 "yaffs_listxattr of object %d", obj->obj_id);
647
648         if (error == 0) {
649                 dev = obj->my_dev;
650                 yaffs_gross_lock(dev);
651                 error = yaffs_list_xattrib(obj, buff, size);
652                 yaffs_gross_unlock(dev);
653
654         }
655         yaffs_trace(YAFFS_TRACE_OS,
656                 "yaffs_listxattr done returning %d", error);
657
658         return error;
659 }
660
661 #endif
662
663 static const struct inode_operations yaffs_dir_inode_operations = {
664         .create = yaffs_create,
665         .lookup = yaffs_lookup,
666         .link = yaffs_link,
667         .unlink = yaffs_unlink,
668         .symlink = yaffs_symlink,
669         .mkdir = yaffs_mkdir,
670         .rmdir = yaffs_unlink,
671         .mknod = yaffs_mknod,
672         .rename = yaffs_rename,
673         .setattr = yaffs_setattr,
674 #ifdef CONFIG_YAFFS_XATTR
675         .setxattr = yaffs_setxattr,
676         .getxattr = yaffs_getxattr,
677         .listxattr = yaffs_listxattr,
678         .removexattr = yaffs_removexattr,
679 #endif
680 };
681 /*-----------------------------------------------------------------*/
682 /* Directory search context allows us to unlock access to yaffs during
683  * filldir without causing problems with the directory being modified.
684  * This is similar to the tried and tested mechanism used in yaffs direct.
685  *
686  * A search context iterates along a doubly linked list of siblings in the
687  * directory. If the iterating object is deleted then this would corrupt
688  * the list iteration, likely causing a crash. The search context avoids
689  * this by using the remove_obj_fn to move the search context to the
690  * next object before the object is deleted.
691  *
692  * Many readdirs (and thus seach conexts) may be alive simulateously so
693  * each struct yaffs_dev has a list of these.
694  *
695  * A seach context lives for the duration of a readdir.
696  *
697  * All these functions must be called while yaffs is locked.
698  */
699
700 struct yaffs_search_context {
701         struct yaffs_dev *dev;
702         struct yaffs_obj *dir_obj;
703         struct yaffs_obj *next_return;
704         struct list_head others;
705 };
706
707 /*
708  * yaffs_new_search() creates a new search context, initialises it and
709  * adds it to the device's search context list.
710  *
711  * Called at start of readdir.
712  */
713 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
714 {
715         struct yaffs_dev *dev = dir->my_dev;
716         struct yaffs_search_context *sc =
717             kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
718         if (sc) {
719                 sc->dir_obj = dir;
720                 sc->dev = dev;
721                 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
722                         sc->next_return = NULL;
723                 else
724                         sc->next_return =
725                             list_entry(dir->variant.dir_variant.children.next,
726                                        struct yaffs_obj, siblings);
727                 INIT_LIST_HEAD(&sc->others);
728                 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
729         }
730         return sc;
731 }
732
733 /*
734  * yaffs_search_end() disposes of a search context and cleans up.
735  */
736 static void yaffs_search_end(struct yaffs_search_context *sc)
737 {
738         if (sc) {
739                 list_del(&sc->others);
740                 kfree(sc);
741         }
742 }
743
744 /*
745  * yaffs_search_advance() moves a search context to the next object.
746  * Called when the search iterates or when an object removal causes
747  * the search context to be moved to the next object.
748  */
749 static void yaffs_search_advance(struct yaffs_search_context *sc)
750 {
751         if (!sc)
752                 return;
753
754         if (sc->next_return == NULL ||
755             list_empty(&sc->dir_obj->variant.dir_variant.children))
756                 sc->next_return = NULL;
757         else {
758                 struct list_head *next = sc->next_return->siblings.next;
759
760                 if (next == &sc->dir_obj->variant.dir_variant.children)
761                         sc->next_return = NULL; /* end of list */
762                 else
763                         sc->next_return =
764                             list_entry(next, struct yaffs_obj, siblings);
765         }
766 }
767
768 /*
769  * yaffs_remove_obj_callback() is called when an object is unlinked.
770  * We check open search contexts and advance any which are currently
771  * on the object being iterated.
772  */
773 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
774 {
775
776         struct list_head *i;
777         struct yaffs_search_context *sc;
778         struct list_head *search_contexts =
779             &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
780
781         /* Iterate through the directory search contexts.
782          * If any are currently on the object being removed, then advance
783          * the search context to the next object to prevent a hanging pointer.
784          */
785         list_for_each(i, search_contexts) {
786                 sc = list_entry(i, struct yaffs_search_context, others);
787                 if (sc->next_return == obj)
788                         yaffs_search_advance(sc);
789         }
790
791 }
792
793 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
794 {
795         struct yaffs_obj *obj;
796         struct yaffs_dev *dev;
797         struct yaffs_search_context *sc;
798         struct inode *inode = f->f_dentry->d_inode;
799         unsigned long offset, curoffs;
800         struct yaffs_obj *l;
801         int ret_val = 0;
802
803         char name[YAFFS_MAX_NAME_LENGTH + 1];
804
805         obj = yaffs_dentry_to_obj(f->f_dentry);
806         dev = obj->my_dev;
807
808         yaffs_gross_lock(dev);
809
810         yaffs_dev_to_lc(dev)->readdir_process = current;
811
812         offset = f->f_pos;
813
814         sc = yaffs_new_search(obj);
815         if (!sc) {
816                 ret_val = -ENOMEM;
817                 goto out;
818         }
819
820         yaffs_trace(YAFFS_TRACE_OS,
821                 "yaffs_readdir: starting at %d", (int)offset);
822
823         if (offset == 0) {
824                 yaffs_trace(YAFFS_TRACE_OS,
825                         "yaffs_readdir: entry . ino %d",
826                         (int)inode->i_ino);
827                 yaffs_gross_unlock(dev);
828                 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
829                         yaffs_gross_lock(dev);
830                         goto out;
831                 }
832                 yaffs_gross_lock(dev);
833                 offset++;
834                 f->f_pos++;
835         }
836         if (offset == 1) {
837                 yaffs_trace(YAFFS_TRACE_OS,
838                         "yaffs_readdir: entry .. ino %d",
839                         (int)f->f_dentry->d_parent->d_inode->i_ino);
840                 yaffs_gross_unlock(dev);
841                 if (filldir(dirent, "..", 2, offset,
842                             f->f_dentry->d_parent->d_inode->i_ino,
843                             DT_DIR) < 0) {
844                         yaffs_gross_lock(dev);
845                         goto out;
846                 }
847                 yaffs_gross_lock(dev);
848                 offset++;
849                 f->f_pos++;
850         }
851
852         curoffs = 1;
853
854         /* If the directory has changed since the open or last call to
855            readdir, rewind to after the 2 canned entries. */
856         if (f->f_version != inode->i_version) {
857                 offset = 2;
858                 f->f_pos = offset;
859                 f->f_version = inode->i_version;
860         }
861
862         while (sc->next_return) {
863                 curoffs++;
864                 l = sc->next_return;
865                 if (curoffs >= offset) {
866                         int this_inode = yaffs_get_obj_inode(l);
867                         int this_type = yaffs_get_obj_type(l);
868
869                         yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
870                         yaffs_trace(YAFFS_TRACE_OS,
871                                 "yaffs_readdir: %s inode %d",
872                                 name, yaffs_get_obj_inode(l));
873
874                         yaffs_gross_unlock(dev);
875
876                         if (filldir(dirent,
877                                     name,
878                                     strlen(name),
879                                     offset, this_inode, this_type) < 0) {
880                                 yaffs_gross_lock(dev);
881                                 goto out;
882                         }
883
884                         yaffs_gross_lock(dev);
885
886                         offset++;
887                         f->f_pos++;
888                 }
889                 yaffs_search_advance(sc);
890         }
891
892 out:
893         yaffs_search_end(sc);
894         yaffs_dev_to_lc(dev)->readdir_process = NULL;
895         yaffs_gross_unlock(dev);
896
897         return ret_val;
898 }
899
900 static const struct file_operations yaffs_dir_operations = {
901         .read = generic_read_dir,
902         .readdir = yaffs_readdir,
903         .fsync = yaffs_sync_object,
904         .llseek = generic_file_llseek,
905 };
906
907
908
909 static int yaffs_file_flush(struct file *file, fl_owner_t id)
910 {
911         struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
912
913         struct yaffs_dev *dev = obj->my_dev;
914
915         yaffs_trace(YAFFS_TRACE_OS,
916                 "yaffs_file_flush object %d (%s)",
917                 obj->obj_id, obj->dirty ? "dirty" : "clean");
918
919         yaffs_gross_lock(dev);
920
921         yaffs_flush_file(obj, 1, 0);
922
923         yaffs_gross_unlock(dev);
924
925         return 0;
926 }
927
928 static const struct file_operations yaffs_file_operations = {
929         .read = do_sync_read,
930         .write = do_sync_write,
931         .aio_read = generic_file_aio_read,
932         .aio_write = generic_file_aio_write,
933         .mmap = generic_file_mmap,
934         .flush = yaffs_file_flush,
935         .fsync = yaffs_sync_object,
936         .splice_read = generic_file_splice_read,
937         .splice_write = generic_file_splice_write,
938         .llseek = generic_file_llseek,
939 };
940
941
942 /* ExportFS support */
943 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
944                                           uint32_t generation)
945 {
946         return yaffs_iget(sb, ino);
947 }
948
949 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
950                                           struct fid *fid, int fh_len,
951                                           int fh_type)
952 {
953         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
954                                     yaffs2_nfs_get_inode);
955 }
956
957 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
958                                           struct fid *fid, int fh_len,
959                                           int fh_type)
960 {
961         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
962                                     yaffs2_nfs_get_inode);
963 }
964
965 struct dentry *yaffs2_get_parent(struct dentry *dentry)
966 {
967
968         struct super_block *sb = dentry->d_inode->i_sb;
969         struct dentry *parent = ERR_PTR(-ENOENT);
970         struct inode *inode;
971         unsigned long parent_ino;
972         struct yaffs_obj *d_obj;
973         struct yaffs_obj *parent_obj;
974
975         d_obj = yaffs_inode_to_obj(dentry->d_inode);
976
977         if (d_obj) {
978                 parent_obj = d_obj->parent;
979                 if (parent_obj) {
980                         parent_ino = yaffs_get_obj_inode(parent_obj);
981                         inode = yaffs_iget(sb, parent_ino);
982
983                         if (IS_ERR(inode)) {
984                                 parent = ERR_CAST(inode);
985                         } else {
986                                 parent = d_obtain_alias(inode);
987                                 if (!IS_ERR(parent)) {
988                                         parent = ERR_PTR(-ENOMEM);
989                                         iput(inode);
990                                 }
991                         }
992                 }
993         }
994
995         return parent;
996 }
997
998 /* Just declare a zero structure as a NULL value implies
999  * using the default functions of exportfs.
1000  */
1001
1002 static struct export_operations yaffs_export_ops = {
1003         .fh_to_dentry = yaffs2_fh_to_dentry,
1004         .fh_to_parent = yaffs2_fh_to_parent,
1005         .get_parent = yaffs2_get_parent,
1006 };
1007
1008
1009 /*-----------------------------------------------------------------*/
1010
1011 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1012                           int buflen)
1013 {
1014         unsigned char *alias;
1015         int ret;
1016
1017         struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1018
1019         yaffs_gross_lock(dev);
1020
1021         alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1022
1023         yaffs_gross_unlock(dev);
1024
1025         if (!alias)
1026                 return -ENOMEM;
1027
1028         ret = vfs_readlink(dentry, buffer, buflen, alias);
1029         kfree(alias);
1030         return ret;
1031 }
1032
1033 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1034 {
1035         unsigned char *alias;
1036         void *ret;
1037         struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1038
1039         yaffs_gross_lock(dev);
1040
1041         alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1042         yaffs_gross_unlock(dev);
1043
1044         if (!alias) {
1045                 ret = ERR_PTR(-ENOMEM);
1046                 goto out;
1047         }
1048
1049         nd_set_link(nd, alias);
1050         ret = (void *)alias;
1051 out:
1052         return ret;
1053 }
1054
1055 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
1056 {
1057         kfree(alias);
1058 }
1059
1060
1061 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
1062 {
1063         /* Clear the association between the inode and
1064          * the struct yaffs_obj.
1065          */
1066         obj->my_inode = NULL;
1067         yaffs_inode_to_obj_lv(inode) = NULL;
1068
1069         /* If the object freeing was deferred, then the real
1070          * free happens now.
1071          * This should fix the inode inconsistency problem.
1072          */
1073         yaffs_handle_defered_free(obj);
1074 }
1075
1076 /* yaffs_evict_inode combines into one operation what was previously done in
1077  * yaffs_clear_inode() and yaffs_delete_inode()
1078  *
1079  */
1080 static void yaffs_evict_inode(struct inode *inode)
1081 {
1082         struct yaffs_obj *obj;
1083         struct yaffs_dev *dev;
1084         int deleteme = 0;
1085
1086         obj = yaffs_inode_to_obj(inode);
1087
1088         yaffs_trace(YAFFS_TRACE_OS,
1089                 "yaffs_evict_inode: ino %d, count %d %s",
1090                 (int)inode->i_ino,
1091                 atomic_read(&inode->i_count),
1092                 obj ? "object exists" : "null object");
1093
1094         if (!inode->i_nlink && !is_bad_inode(inode))
1095                 deleteme = 1;
1096         truncate_inode_pages(&inode->i_data, 0);
1097         end_writeback(inode);
1098
1099         if (deleteme && obj) {
1100                 dev = obj->my_dev;
1101                 yaffs_gross_lock(dev);
1102                 yaffs_del_obj(obj);
1103                 yaffs_gross_unlock(dev);
1104         }
1105         if (obj) {
1106                 dev = obj->my_dev;
1107                 yaffs_gross_lock(dev);
1108                 yaffs_unstitch_obj(inode, obj);
1109                 yaffs_gross_unlock(dev);
1110         }
1111
1112 }
1113
1114 static void yaffs_touch_super(struct yaffs_dev *dev)
1115 {
1116         struct super_block *sb = yaffs_dev_to_lc(dev)->super;
1117
1118         yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb);
1119         if (sb)
1120                 sb->s_dirt = 1;
1121 }
1122
1123 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1124 {
1125         /* Lifted from jffs2 */
1126
1127         struct yaffs_obj *obj;
1128         unsigned char *pg_buf;
1129         int ret;
1130
1131         struct yaffs_dev *dev;
1132
1133         yaffs_trace(YAFFS_TRACE_OS,
1134                 "yaffs_readpage_nolock at %08x, size %08x",
1135                 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1136                 (unsigned)PAGE_CACHE_SIZE);
1137
1138         obj = yaffs_dentry_to_obj(f->f_dentry);
1139
1140         dev = obj->my_dev;
1141
1142         BUG_ON(!PageLocked(pg));
1143
1144         pg_buf = kmap(pg);
1145         /* FIXME: Can kmap fail? */
1146
1147         yaffs_gross_lock(dev);
1148
1149         ret = yaffs_file_rd(obj, pg_buf,
1150                             pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
1151
1152         yaffs_gross_unlock(dev);
1153
1154         if (ret >= 0)
1155                 ret = 0;
1156
1157         if (ret) {
1158                 ClearPageUptodate(pg);
1159                 SetPageError(pg);
1160         } else {
1161                 SetPageUptodate(pg);
1162                 ClearPageError(pg);
1163         }
1164
1165         flush_dcache_page(pg);
1166         kunmap(pg);
1167
1168         yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
1169         return ret;
1170 }
1171
1172 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1173 {
1174         int ret = yaffs_readpage_nolock(f, pg);
1175         unlock_page(pg);
1176         return ret;
1177 }
1178
1179 static int yaffs_readpage(struct file *f, struct page *pg)
1180 {
1181         int ret;
1182
1183         yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
1184         ret = yaffs_readpage_unlock(f, pg);
1185         yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
1186         return ret;
1187 }
1188
1189 /* writepage inspired by/stolen from smbfs */
1190
1191 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1192 {
1193         struct yaffs_dev *dev;
1194         struct address_space *mapping = page->mapping;
1195         struct inode *inode;
1196         unsigned long end_index;
1197         char *buffer;
1198         struct yaffs_obj *obj;
1199         int n_written = 0;
1200         unsigned n_bytes;
1201         loff_t i_size;
1202
1203         if (!mapping)
1204                 BUG();
1205         inode = mapping->host;
1206         if (!inode)
1207                 BUG();
1208         i_size = i_size_read(inode);
1209
1210         end_index = i_size >> PAGE_CACHE_SHIFT;
1211
1212         if (page->index < end_index)
1213                 n_bytes = PAGE_CACHE_SIZE;
1214         else {
1215                 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
1216
1217                 if (page->index > end_index || !n_bytes) {
1218                         yaffs_trace(YAFFS_TRACE_OS,
1219                                 "yaffs_writepage at %08x, inode size = %08x!!!",
1220                                 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1221                                 (unsigned)inode->i_size);
1222                         yaffs_trace(YAFFS_TRACE_OS,
1223                           "                -> don't care!!");
1224
1225                         zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1226                         set_page_writeback(page);
1227                         unlock_page(page);
1228                         end_page_writeback(page);
1229                         return 0;
1230                 }
1231         }
1232
1233         if (n_bytes != PAGE_CACHE_SIZE)
1234                 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
1235
1236         get_page(page);
1237
1238         buffer = kmap(page);
1239
1240         obj = yaffs_inode_to_obj(inode);
1241         dev = obj->my_dev;
1242         yaffs_gross_lock(dev);
1243
1244         yaffs_trace(YAFFS_TRACE_OS,
1245                 "yaffs_writepage at %08x, size %08x",
1246                 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes);
1247         yaffs_trace(YAFFS_TRACE_OS,
1248                 "writepag0: obj = %05x, ino = %05x",
1249                 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1250
1251         n_written = yaffs_wr_file(obj, buffer,
1252                                   page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
1253
1254         yaffs_touch_super(dev);
1255
1256         yaffs_trace(YAFFS_TRACE_OS,
1257                 "writepag1: obj = %05x, ino = %05x",
1258                 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1259
1260         yaffs_gross_unlock(dev);
1261
1262         kunmap(page);
1263         set_page_writeback(page);
1264         unlock_page(page);
1265         end_page_writeback(page);
1266         put_page(page);
1267
1268         return (n_written == n_bytes) ? 0 : -ENOSPC;
1269 }
1270
1271 /* Space holding and freeing is done to ensure we have space available for 
1272  * write_begin/end.
1273  * For now we just assume few parallel writes and check against a small
1274  * number.
1275  * Todo: need to do this with a counter to handle parallel reads better.
1276  */
1277
1278 static ssize_t yaffs_hold_space(struct file *f)
1279 {
1280         struct yaffs_obj *obj;
1281         struct yaffs_dev *dev;
1282
1283         int n_free_chunks;
1284
1285         obj = yaffs_dentry_to_obj(f->f_dentry);
1286
1287         dev = obj->my_dev;
1288
1289         yaffs_gross_lock(dev);
1290
1291         n_free_chunks = yaffs_get_n_free_chunks(dev);
1292
1293         yaffs_gross_unlock(dev);
1294
1295         return (n_free_chunks > 20) ? 1 : 0;
1296 }
1297
1298 static void yaffs_release_space(struct file *f)
1299 {
1300         struct yaffs_obj *obj;
1301         struct yaffs_dev *dev;
1302
1303         obj = yaffs_dentry_to_obj(f->f_dentry);
1304
1305         dev = obj->my_dev;
1306
1307         yaffs_gross_lock(dev);
1308
1309         yaffs_gross_unlock(dev);
1310 }
1311
1312 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1313                              loff_t pos, unsigned len, unsigned flags,
1314                              struct page **pagep, void **fsdata)
1315 {
1316         struct page *pg = NULL;
1317         pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1318
1319         int ret = 0;
1320         int space_held = 0;
1321
1322         /* Get a page */
1323         pg = grab_cache_page_write_begin(mapping, index, flags);
1324
1325         *pagep = pg;
1326         if (!pg) {
1327                 ret = -ENOMEM;
1328                 goto out;
1329         }
1330         yaffs_trace(YAFFS_TRACE_OS,
1331                 "start yaffs_write_begin index %d(%x) uptodate %d",
1332                 (int)index, (int)index, PageUptodate(pg) ? 1 : 0);
1333
1334         /* Get fs space */
1335         space_held = yaffs_hold_space(filp);
1336
1337         if (!space_held) {
1338                 ret = -ENOSPC;
1339                 goto out;
1340         }
1341
1342         /* Update page if required */
1343
1344         if (!PageUptodate(pg))
1345                 ret = yaffs_readpage_nolock(filp, pg);
1346
1347         if (ret)
1348                 goto out;
1349
1350         /* Happy path return */
1351         yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
1352
1353         return 0;
1354
1355 out:
1356         yaffs_trace(YAFFS_TRACE_OS,
1357                 "end yaffs_write_begin fail returning %d", ret);
1358         if (space_held)
1359                 yaffs_release_space(filp);
1360         if (pg) {
1361                 unlock_page(pg);
1362                 page_cache_release(pg);
1363         }
1364         return ret;
1365 }
1366
1367 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1368                                 loff_t * pos)
1369 {
1370         struct yaffs_obj *obj;
1371         int n_written, ipos;
1372         struct inode *inode;
1373         struct yaffs_dev *dev;
1374
1375         obj = yaffs_dentry_to_obj(f->f_dentry);
1376
1377         if (!obj) {
1378                 /* This should not happen */
1379                 yaffs_trace(YAFFS_TRACE_OS,
1380                         "yaffs_file_write: hey obj is null!");
1381                 return -ENINVAL;
1382         }
1383
1384         dev = obj->my_dev;
1385
1386         yaffs_gross_lock(dev);
1387
1388         inode = f->f_dentry->d_inode;
1389
1390         if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1391                 ipos = inode->i_size;
1392         else
1393                 ipos = *pos;
1394
1395         yaffs_trace(YAFFS_TRACE_OS,
1396                 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %d(%x)",
1397                 (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos);
1398
1399         n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1400
1401         yaffs_touch_super(dev);
1402
1403         yaffs_trace(YAFFS_TRACE_OS,
1404                 "yaffs_file_write: %d(%x) bytes written",
1405                 (unsigned)n, (unsigned)n);
1406
1407         if (n_written > 0) {
1408                 ipos += n_written;
1409                 *pos = ipos;
1410                 if (ipos > inode->i_size) {
1411                         inode->i_size = ipos;
1412                         inode->i_blocks = (ipos + 511) >> 9;
1413
1414                         yaffs_trace(YAFFS_TRACE_OS,
1415                                 "yaffs_file_write size updated to %d bytes, %d blocks",
1416                                 ipos, (int)(inode->i_blocks));
1417                 }
1418
1419         }
1420         yaffs_gross_unlock(dev);
1421         return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1422 }
1423
1424 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1425                            loff_t pos, unsigned len, unsigned copied,
1426                            struct page *pg, void *fsdadata)
1427 {
1428         int ret = 0;
1429         void *addr, *kva;
1430         uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1431
1432         kva = kmap(pg);
1433         addr = kva + offset_into_page;
1434
1435         yaffs_trace(YAFFS_TRACE_OS,
1436                 "yaffs_write_end addr %p pos %x n_bytes %d",
1437                 addr, (unsigned)pos, copied);
1438
1439         ret = yaffs_file_write(filp, addr, copied, &pos);
1440
1441         if (ret != copied) {
1442                 yaffs_trace(YAFFS_TRACE_OS,
1443                         "yaffs_write_end not same size ret %d  copied %d",
1444                         ret, copied);
1445                 SetPageError(pg);
1446         }
1447
1448         kunmap(pg);
1449
1450         yaffs_release_space(filp);
1451         unlock_page(pg);
1452         page_cache_release(pg);
1453         return ret;
1454 }
1455
1456 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1457 {
1458         struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1459         struct super_block *sb = dentry->d_sb;
1460
1461         yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
1462
1463         yaffs_gross_lock(dev);
1464
1465         buf->f_type = YAFFS_MAGIC;
1466         buf->f_bsize = sb->s_blocksize;
1467         buf->f_namelen = 255;
1468
1469         if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
1470                 /* Do this if chunk size is not a power of 2 */
1471
1472                 uint64_t bytes_in_dev;
1473                 uint64_t bytes_free;
1474
1475                 bytes_in_dev =
1476                     ((uint64_t)
1477                      ((dev->param.end_block - dev->param.start_block +
1478                        1))) * ((uint64_t) (dev->param.chunks_per_block *
1479                                            dev->data_bytes_per_chunk));
1480
1481                 do_div(bytes_in_dev, sb->s_blocksize);  /* bytes_in_dev becomes the number of blocks */
1482                 buf->f_blocks = bytes_in_dev;
1483
1484                 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
1485                     ((uint64_t) (dev->data_bytes_per_chunk));
1486
1487                 do_div(bytes_free, sb->s_blocksize);
1488
1489                 buf->f_bfree = bytes_free;
1490
1491         } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
1492
1493                 buf->f_blocks =
1494                     (dev->param.end_block - dev->param.start_block + 1) *
1495                     dev->param.chunks_per_block /
1496                     (sb->s_blocksize / dev->data_bytes_per_chunk);
1497                 buf->f_bfree =
1498                     yaffs_get_n_free_chunks(dev) /
1499                     (sb->s_blocksize / dev->data_bytes_per_chunk);
1500         } else {
1501                 buf->f_blocks =
1502                     (dev->param.end_block - dev->param.start_block + 1) *
1503                     dev->param.chunks_per_block *
1504                     (dev->data_bytes_per_chunk / sb->s_blocksize);
1505
1506                 buf->f_bfree =
1507                     yaffs_get_n_free_chunks(dev) *
1508                     (dev->data_bytes_per_chunk / sb->s_blocksize);
1509         }
1510
1511         buf->f_files = 0;
1512         buf->f_ffree = 0;
1513         buf->f_bavail = buf->f_bfree;
1514
1515         yaffs_gross_unlock(dev);
1516         return 0;
1517 }
1518
1519 static void yaffs_flush_inodes(struct super_block *sb)
1520 {
1521         struct inode *iptr;
1522         struct yaffs_obj *obj;
1523
1524         list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
1525                 obj = yaffs_inode_to_obj(iptr);
1526                 if (obj) {
1527                         yaffs_trace(YAFFS_TRACE_OS,
1528                                 "flushing obj %d", obj->obj_id);
1529                         yaffs_flush_file(obj, 1, 0);
1530                 }
1531         }
1532 }
1533
1534 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
1535 {
1536         struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1537         if (!dev)
1538                 return;
1539
1540         yaffs_flush_inodes(sb);
1541         yaffs_update_dirty_dirs(dev);
1542         yaffs_flush_whole_cache(dev);
1543         if (do_checkpoint)
1544                 yaffs_checkpoint_save(dev);
1545 }
1546
1547 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
1548 {
1549         unsigned erased_chunks =
1550             dev->n_erased_blocks * dev->param.chunks_per_block;
1551         struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1552         unsigned scattered = 0; /* Free chunks not in an erased block */
1553
1554         if (erased_chunks < dev->n_free_chunks)
1555                 scattered = (dev->n_free_chunks - erased_chunks);
1556
1557         if (!context->bg_running)
1558                 return 0;
1559         else if (scattered < (dev->param.chunks_per_block * 2))
1560                 return 0;
1561         else if (erased_chunks > dev->n_free_chunks / 2)
1562                 return 0;
1563         else if (erased_chunks > dev->n_free_chunks / 4)
1564                 return 1;
1565         else
1566                 return 2;
1567 }
1568
1569 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
1570 {
1571
1572         struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1573         unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
1574         unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
1575         int do_checkpoint;
1576
1577         yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1578                 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
1579                 gc_urgent,
1580                 sb->s_dirt ? "dirty" : "clean",
1581                 request_checkpoint ? "checkpoint requested" : "no checkpoint",
1582                 oneshot_checkpoint ? " one-shot" : "");
1583
1584         yaffs_gross_lock(dev);
1585         do_checkpoint = ((request_checkpoint && !gc_urgent) ||
1586                          oneshot_checkpoint) && !dev->is_checkpointed;
1587
1588         if (sb->s_dirt || do_checkpoint) {
1589                 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
1590                 sb->s_dirt = 0;
1591                 if (oneshot_checkpoint)
1592                         yaffs_auto_checkpoint &= ~4;
1593         }
1594         yaffs_gross_unlock(dev);
1595
1596         return 0;
1597 }
1598
1599 /*
1600  * yaffs background thread functions .
1601  * yaffs_bg_thread_fn() the thread function
1602  * yaffs_bg_start() launches the background thread.
1603  * yaffs_bg_stop() cleans up the background thread.
1604  *
1605  * NB: 
1606  * The thread should only run after the yaffs is initialised
1607  * The thread should be stopped before yaffs is unmounted.
1608  * The thread should not do any writing while the fs is in read only.
1609  */
1610
1611 void yaffs_background_waker(unsigned long data)
1612 {
1613         wake_up_process((struct task_struct *)data);
1614 }
1615
1616 static int yaffs_bg_thread_fn(void *data)
1617 {
1618         struct yaffs_dev *dev = (struct yaffs_dev *)data;
1619         struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1620         unsigned long now = jiffies;
1621         unsigned long next_dir_update = now;
1622         unsigned long next_gc = now;
1623         unsigned long expires;
1624         unsigned int urgency;
1625
1626         int gc_result;
1627         struct timer_list timer;
1628
1629         yaffs_trace(YAFFS_TRACE_BACKGROUND,
1630                 "yaffs_background starting for dev %p", (void *)dev);
1631
1632         set_freezable();
1633         while (context->bg_running) {
1634                 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
1635
1636                 if (kthread_should_stop())
1637                         break;
1638
1639                 if (try_to_freeze())
1640                         continue;
1641
1642                 yaffs_gross_lock(dev);
1643
1644                 now = jiffies;
1645
1646                 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
1647                         yaffs_update_dirty_dirs(dev);
1648                         next_dir_update = now + HZ;
1649                 }
1650
1651                 if (time_after(now, next_gc) && yaffs_bg_enable) {
1652                         if (!dev->is_checkpointed) {
1653                                 urgency = yaffs_bg_gc_urgency(dev);
1654                                 gc_result = yaffs_bg_gc(dev, urgency);
1655                                 if (urgency > 1)
1656                                         next_gc = now + HZ / 20 + 1;
1657                                 else if (urgency > 0)
1658                                         next_gc = now + HZ / 10 + 1;
1659                                 else
1660                                         next_gc = now + HZ * 2;
1661                         } else  {
1662                                 /*
1663                                  * gc not running so set to next_dir_update
1664                                  * to cut down on wake ups
1665                                  */
1666                                 next_gc = next_dir_update;
1667                         }
1668                 }
1669                 yaffs_gross_unlock(dev);
1670                 expires = next_dir_update;
1671                 if (time_before(next_gc, expires))
1672                         expires = next_gc;
1673                 if (time_before(expires, now))
1674                         expires = now + HZ;
1675
1676                 Y_INIT_TIMER(&timer);
1677                 timer.expires = expires + 1;
1678                 timer.data = (unsigned long)current;
1679                 timer.function = yaffs_background_waker;
1680
1681                 set_current_state(TASK_INTERRUPTIBLE);
1682                 add_timer(&timer);
1683                 schedule();
1684                 del_timer_sync(&timer);
1685         }
1686
1687         return 0;
1688 }
1689
1690 static int yaffs_bg_start(struct yaffs_dev *dev)
1691 {
1692         int retval = 0;
1693         struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1694
1695         if (dev->read_only)
1696                 return -1;
1697
1698         context->bg_running = 1;
1699
1700         context->bg_thread = kthread_run(yaffs_bg_thread_fn,
1701                                          (void *)dev, "yaffs-bg-%d",
1702                                          context->mount_id);
1703
1704         if (IS_ERR(context->bg_thread)) {
1705                 retval = PTR_ERR(context->bg_thread);
1706                 context->bg_thread = NULL;
1707                 context->bg_running = 0;
1708         }
1709         return retval;
1710 }
1711
1712 static void yaffs_bg_stop(struct yaffs_dev *dev)
1713 {
1714         struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
1715
1716         ctxt->bg_running = 0;
1717
1718         if (ctxt->bg_thread) {
1719                 kthread_stop(ctxt->bg_thread);
1720                 ctxt->bg_thread = NULL;
1721         }
1722 }
1723
1724 static void yaffs_write_super(struct super_block *sb)
1725 {
1726         unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
1727
1728         yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1729                 "yaffs_write_super%s",
1730                 request_checkpoint ? " checkpt" : "");
1731
1732         yaffs_do_sync_fs(sb, request_checkpoint);
1733
1734 }
1735
1736 static int yaffs_sync_fs(struct super_block *sb, int wait)
1737 {
1738         unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
1739
1740         yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1741                 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
1742
1743         yaffs_do_sync_fs(sb, request_checkpoint);
1744
1745         return 0;
1746 }
1747
1748
1749 static LIST_HEAD(yaffs_context_list);
1750 struct mutex yaffs_context_lock;
1751
1752
1753
1754 struct yaffs_options {
1755         int inband_tags;
1756         int skip_checkpoint_read;
1757         int skip_checkpoint_write;
1758         int no_cache;
1759         int tags_ecc_on;
1760         int tags_ecc_overridden;
1761         int lazy_loading_enabled;
1762         int lazy_loading_overridden;
1763         int empty_lost_and_found;
1764         int empty_lost_and_found_overridden;
1765 };
1766
1767 #define MAX_OPT_LEN 30
1768 static int yaffs_parse_options(struct yaffs_options *options,
1769                                const char *options_str)
1770 {
1771         char cur_opt[MAX_OPT_LEN + 1];
1772         int p;
1773         int error = 0;
1774
1775         /* Parse through the options which is a comma seperated list */
1776
1777         while (options_str && *options_str && !error) {
1778                 memset(cur_opt, 0, MAX_OPT_LEN + 1);
1779                 p = 0;
1780
1781                 while (*options_str == ',')
1782                         options_str++;
1783
1784                 while (*options_str && *options_str != ',') {
1785                         if (p < MAX_OPT_LEN) {
1786                                 cur_opt[p] = *options_str;
1787                                 p++;
1788                         }
1789                         options_str++;
1790                 }
1791
1792                 if (!strcmp(cur_opt, "inband-tags")) {
1793                         options->inband_tags = 1;
1794                 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
1795                         options->tags_ecc_on = 0;
1796                         options->tags_ecc_overridden = 1;
1797                 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
1798                         options->tags_ecc_on = 1;
1799                         options->tags_ecc_overridden = 1;
1800                 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
1801                         options->lazy_loading_enabled = 0;
1802                         options->lazy_loading_overridden = 1;
1803                 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
1804                         options->lazy_loading_enabled = 1;
1805                         options->lazy_loading_overridden = 1;
1806                 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
1807                         options->empty_lost_and_found = 0;
1808                         options->empty_lost_and_found_overridden = 1;
1809                 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
1810                         options->empty_lost_and_found = 1;
1811                         options->empty_lost_and_found_overridden = 1;
1812                 } else if (!strcmp(cur_opt, "no-cache")) {
1813                         options->no_cache = 1;
1814                 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
1815                         options->skip_checkpoint_read = 1;
1816                 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
1817                         options->skip_checkpoint_write = 1;
1818                 } else if (!strcmp(cur_opt, "no-checkpoint")) {
1819                         options->skip_checkpoint_read = 1;
1820                         options->skip_checkpoint_write = 1;
1821                 } else {
1822                         printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
1823                                cur_opt);
1824                         error = 1;
1825                 }
1826         }
1827
1828         return error;
1829 }
1830
1831 static struct address_space_operations yaffs_file_address_operations = {
1832         .readpage = yaffs_readpage,
1833         .writepage = yaffs_writepage,
1834         .write_begin = yaffs_write_begin,
1835         .write_end = yaffs_write_end,
1836 };
1837
1838
1839
1840 static const struct inode_operations yaffs_file_inode_operations = {
1841         .setattr = yaffs_setattr,
1842 #ifdef CONFIG_YAFFS_XATTR
1843         .setxattr = yaffs_setxattr,
1844         .getxattr = yaffs_getxattr,
1845         .listxattr = yaffs_listxattr,
1846         .removexattr = yaffs_removexattr,
1847 #endif
1848 };
1849
1850 static const struct inode_operations yaffs_symlink_inode_operations = {
1851         .readlink = yaffs_readlink,
1852         .follow_link = yaffs_follow_link,
1853         .put_link = yaffs_put_link,
1854         .setattr = yaffs_setattr,
1855 #ifdef CONFIG_YAFFS_XATTR
1856         .setxattr = yaffs_setxattr,
1857         .getxattr = yaffs_getxattr,
1858         .listxattr = yaffs_listxattr,
1859         .removexattr = yaffs_removexattr,
1860 #endif
1861 };
1862
1863 static void yaffs_fill_inode_from_obj(struct inode *inode,
1864                                       struct yaffs_obj *obj)
1865 {
1866         if (inode && obj) {
1867
1868                 /* Check mode against the variant type and attempt to repair if broken. */
1869                 u32 mode = obj->yst_mode;
1870                 switch (obj->variant_type) {
1871                 case YAFFS_OBJECT_TYPE_FILE:
1872                         if (!S_ISREG(mode)) {
1873                                 obj->yst_mode &= ~S_IFMT;
1874                                 obj->yst_mode |= S_IFREG;
1875                         }
1876
1877                         break;
1878                 case YAFFS_OBJECT_TYPE_SYMLINK:
1879                         if (!S_ISLNK(mode)) {
1880                                 obj->yst_mode &= ~S_IFMT;
1881                                 obj->yst_mode |= S_IFLNK;
1882                         }
1883
1884                         break;
1885                 case YAFFS_OBJECT_TYPE_DIRECTORY:
1886                         if (!S_ISDIR(mode)) {
1887                                 obj->yst_mode &= ~S_IFMT;
1888                                 obj->yst_mode |= S_IFDIR;
1889                         }
1890
1891                         break;
1892                 case YAFFS_OBJECT_TYPE_UNKNOWN:
1893                 case YAFFS_OBJECT_TYPE_HARDLINK:
1894                 case YAFFS_OBJECT_TYPE_SPECIAL:
1895                 default:
1896                         /* TODO? */
1897                         break;
1898                 }
1899
1900                 inode->i_flags |= S_NOATIME;
1901
1902                 inode->i_ino = obj->obj_id;
1903                 inode->i_mode = obj->yst_mode;
1904                 inode->i_uid = obj->yst_uid;
1905                 inode->i_gid = obj->yst_gid;
1906
1907                 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1908
1909                 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1910                 inode->i_atime.tv_nsec = 0;
1911                 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1912                 inode->i_mtime.tv_nsec = 0;
1913                 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1914                 inode->i_ctime.tv_nsec = 0;
1915                 inode->i_size = yaffs_get_obj_length(obj);
1916                 inode->i_blocks = (inode->i_size + 511) >> 9;
1917
1918                 inode->i_nlink = yaffs_get_obj_link_count(obj);
1919
1920                 yaffs_trace(YAFFS_TRACE_OS,
1921                         "yaffs_fill_inode mode %x uid %d gid %d size %d count %d",
1922                         inode->i_mode, inode->i_uid, inode->i_gid,
1923                         (int)inode->i_size, atomic_read(&inode->i_count));
1924
1925                 switch (obj->yst_mode & S_IFMT) {
1926                 default:        /* fifo, device or socket */
1927                         init_special_inode(inode, obj->yst_mode,
1928                                            old_decode_dev(obj->yst_rdev));
1929                         break;
1930                 case S_IFREG:   /* file */
1931                         inode->i_op = &yaffs_file_inode_operations;
1932                         inode->i_fop = &yaffs_file_operations;
1933                         inode->i_mapping->a_ops =
1934                             &yaffs_file_address_operations;
1935                         break;
1936                 case S_IFDIR:   /* directory */
1937                         inode->i_op = &yaffs_dir_inode_operations;
1938                         inode->i_fop = &yaffs_dir_operations;
1939                         break;
1940                 case S_IFLNK:   /* symlink */
1941                         inode->i_op = &yaffs_symlink_inode_operations;
1942                         break;
1943                 }
1944
1945                 yaffs_inode_to_obj_lv(inode) = obj;
1946
1947                 obj->my_inode = inode;
1948
1949         } else {
1950                 yaffs_trace(YAFFS_TRACE_OS,
1951                         "yaffs_fill_inode invalid parameters");
1952         }
1953 }
1954
1955 static void yaffs_put_super(struct super_block *sb)
1956 {
1957         struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1958
1959         yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
1960
1961         yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1962                 "Shutting down yaffs background thread");
1963         yaffs_bg_stop(dev);
1964         yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1965                 "yaffs background thread shut down");
1966
1967         yaffs_gross_lock(dev);
1968
1969         yaffs_flush_super(sb, 1);
1970
1971         if (yaffs_dev_to_lc(dev)->put_super_fn)
1972                 yaffs_dev_to_lc(dev)->put_super_fn(sb);
1973
1974         yaffs_deinitialise(dev);
1975
1976         yaffs_gross_unlock(dev);
1977         mutex_lock(&yaffs_context_lock);
1978         list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
1979         mutex_unlock(&yaffs_context_lock);
1980
1981         if (yaffs_dev_to_lc(dev)->spare_buffer) {
1982                 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
1983                 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
1984         }
1985
1986         kfree(dev);
1987 }
1988
1989 static void yaffs_mtd_put_super(struct super_block *sb)
1990 {
1991         struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
1992
1993         if (mtd->sync)
1994                 mtd->sync(mtd);
1995
1996         put_mtd_device(mtd);
1997 }
1998
1999 static const struct super_operations yaffs_super_ops = {
2000         .statfs = yaffs_statfs,
2001         .put_super = yaffs_put_super,
2002         .evict_inode = yaffs_evict_inode,
2003         .sync_fs = yaffs_sync_fs,
2004         .write_super = yaffs_write_super,
2005 };
2006
2007 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2008                                                      struct super_block *sb,
2009                                                      void *data, int silent)
2010 {
2011         int n_blocks;
2012         struct inode *inode = NULL;
2013         struct dentry *root;
2014         struct yaffs_dev *dev = 0;
2015         char devname_buf[BDEVNAME_SIZE + 1];
2016         struct mtd_info *mtd;
2017         int err;
2018         char *data_str = (char *)data;
2019         struct yaffs_linux_context *context = NULL;
2020         struct yaffs_param *param;
2021
2022         int read_only = 0;
2023
2024         struct yaffs_options options;
2025
2026         unsigned mount_id;
2027         int found;
2028         struct yaffs_linux_context *context_iterator;
2029         struct list_head *l;
2030
2031         if (!sb) {
2032                 printk(KERN_INFO "yaffs: sb is NULL\n");
2033                 return NULL;
2034         }
2035
2036         sb->s_magic = YAFFS_MAGIC;
2037         sb->s_op = &yaffs_super_ops;
2038         sb->s_flags |= MS_NOATIME;
2039
2040         read_only = ((sb->s_flags & MS_RDONLY) != 0);
2041
2042         sb->s_export_op = &yaffs_export_ops;
2043
2044         if (!sb->s_dev)
2045                 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2046         else if (!yaffs_devname(sb, devname_buf))
2047                 printk(KERN_INFO "yaffs: devname is NULL\n");
2048         else
2049                 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2050                        sb->s_dev,
2051                        yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2052
2053         if (!data_str)
2054                 data_str = "";
2055
2056         printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2057
2058         memset(&options, 0, sizeof(options));
2059
2060         if (yaffs_parse_options(&options, data_str)) {
2061                 /* Option parsing failed */
2062                 return NULL;
2063         }
2064
2065         sb->s_blocksize = PAGE_CACHE_SIZE;
2066         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2067
2068         yaffs_trace(YAFFS_TRACE_OS,
2069                 "yaffs_read_super: Using yaffs%d", yaffs_version);
2070         yaffs_trace(YAFFS_TRACE_OS,
2071                 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2072
2073         yaffs_trace(YAFFS_TRACE_ALWAYS,
2074                 "Attempting MTD mount of %u.%u,\"%s\"",
2075                 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2076                 yaffs_devname(sb, devname_buf));
2077
2078         /* Check it's an mtd device..... */
2079         if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2080                 return NULL;    /* This isn't an mtd device */
2081
2082         /* Get the device */
2083         mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2084         if (!mtd) {
2085                 yaffs_trace(YAFFS_TRACE_ALWAYS,
2086                         "MTD device #%u doesn't appear to exist",
2087                         MINOR(sb->s_dev));
2088                 return NULL;
2089         }
2090         /* Check it's NAND */
2091         if (mtd->type != MTD_NANDFLASH) {
2092                 yaffs_trace(YAFFS_TRACE_ALWAYS,
2093                         "MTD device is not NAND it's type %d",
2094                         mtd->type);
2095                 return NULL;
2096         }
2097
2098         yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase);
2099         yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read);
2100         yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write);
2101         yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob);
2102         yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob);
2103         yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad);
2104         yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad);
2105         yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
2106         yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
2107         yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
2108         yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
2109
2110 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2111
2112         if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2113                 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2114                 yaffs_version = 2;
2115         }
2116
2117         /* Added NCB 26/5/2006 for completeness */
2118         if (yaffs_version == 2 && !options.inband_tags
2119             && WRITE_SIZE(mtd) == 512) {
2120                 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2121                 yaffs_version = 1;
2122         }
2123 #endif
2124
2125         if (yaffs_version == 2) {
2126                 /* Check for version 2 style functions */
2127                 if (!mtd->erase ||
2128                     !mtd->block_isbad ||
2129                     !mtd->block_markbad ||
2130                     !mtd->read ||
2131                     !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2132                         yaffs_trace(YAFFS_TRACE_ALWAYS,
2133                                 "MTD device does not support required functions");
2134                         return NULL;
2135                 }
2136
2137                 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2138                      mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2139                     !options.inband_tags) {
2140                         yaffs_trace(YAFFS_TRACE_ALWAYS,
2141                                 "MTD device does not have the right page sizes");
2142                         return NULL;
2143                 }
2144         } else {
2145                 /* Check for V1 style functions */
2146                 if (!mtd->erase ||
2147                     !mtd->read ||
2148                     !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2149                         yaffs_trace(YAFFS_TRACE_ALWAYS,
2150                                 "MTD device does not support required functions");
2151                         return NULL;
2152                 }
2153
2154                 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2155                     mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2156                         yaffs_trace(YAFFS_TRACE_ALWAYS,
2157                                 "MTD device does not support have the right page sizes");
2158                         return NULL;
2159                 }
2160         }
2161
2162         /* OK, so if we got here, we have an MTD that's NAND and looks
2163          * like it has the right capabilities
2164          * Set the struct yaffs_dev up for mtd
2165          */
2166
2167         if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2168                 read_only = 1;
2169                 printk(KERN_INFO
2170                        "yaffs: mtd is read only, setting superblock read only");
2171                 sb->s_flags |= MS_RDONLY;
2172         }
2173
2174         dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2175         context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2176
2177         if (!dev || !context) {
2178                 if (dev)
2179                         kfree(dev);
2180                 if (context)
2181                         kfree(context);
2182                 dev = NULL;
2183                 context = NULL;
2184         }
2185
2186         if (!dev) {
2187                 /* Deep shit could not allocate device structure */
2188                 yaffs_trace(YAFFS_TRACE_ALWAYS,
2189                         "yaffs_read_super failed trying to allocate yaffs_dev");
2190                 return NULL;
2191         }
2192         memset(dev, 0, sizeof(struct yaffs_dev));
2193         param = &(dev->param);
2194
2195         memset(context, 0, sizeof(struct yaffs_linux_context));
2196         dev->os_context = context;
2197         INIT_LIST_HEAD(&(context->context_list));
2198         context->dev = dev;
2199         context->super = sb;
2200
2201         dev->read_only = read_only;
2202
2203         sb->s_fs_info = dev;
2204
2205         dev->driver_context = mtd;
2206         param->name = mtd->name;
2207
2208         /* Set up the memory size parameters.... */
2209
2210         n_blocks =
2211             YCALCBLOCKS(mtd->size,
2212                         (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2213
2214         param->start_block = 0;
2215         param->end_block = n_blocks - 1;
2216         param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2217         param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2218         param->n_reserved_blocks = 5;
2219         param->n_caches = (options.no_cache) ? 0 : 10;
2220         param->inband_tags = options.inband_tags;
2221
2222 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2223         param->disable_lazy_load = 1;
2224 #endif
2225 #ifdef CONFIG_YAFFS_XATTR
2226         param->enable_xattr = 1;
2227 #endif
2228         if (options.lazy_loading_overridden)
2229                 param->disable_lazy_load = !options.lazy_loading_enabled;
2230
2231 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2232         param->no_tags_ecc = 1;
2233 #endif
2234
2235 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2236 #else
2237         param->defered_dir_update = 1;
2238 #endif
2239
2240         if (options.tags_ecc_overridden)
2241                 param->no_tags_ecc = !options.tags_ecc_on;
2242
2243 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2244         param->empty_lost_n_found = 1;
2245 #endif
2246
2247 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2248         param->refresh_period = 0;
2249 #else
2250         param->refresh_period = 500;
2251 #endif
2252
2253 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2254         param->always_check_erased = 1;
2255 #endif
2256
2257         if (options.empty_lost_and_found_overridden)
2258                 param->empty_lost_n_found = options.empty_lost_and_found;
2259
2260         /* ... and the functions. */
2261         if (yaffs_version == 2) {
2262                 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2263                 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2264                 param->bad_block_fn = nandmtd2_mark_block_bad;
2265                 param->query_block_fn = nandmtd2_query_block;
2266                 yaffs_dev_to_lc(dev)->spare_buffer = 
2267                                 kmalloc(mtd->oobsize, GFP_NOFS);
2268                 param->is_yaffs2 = 1;
2269                 param->total_bytes_per_chunk = mtd->writesize;
2270                 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2271                 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2272
2273                 param->start_block = 0;
2274                 param->end_block = n_blocks - 1;
2275         } else {
2276                 /* use the MTD interface in yaffs_mtdif1.c */
2277                 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2278                 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2279                 param->bad_block_fn = nandmtd1_mark_block_bad;
2280                 param->query_block_fn = nandmtd1_query_block;
2281                 param->is_yaffs2 = 0;
2282         }
2283         /* ... and common functions */
2284         param->erase_fn = nandmtd_erase_block;
2285         param->initialise_flash_fn = nandmtd_initialise;
2286
2287         yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2288
2289         param->sb_dirty_fn = yaffs_touch_super;
2290         param->gc_control = yaffs_gc_control_callback;
2291
2292         yaffs_dev_to_lc(dev)->super = sb;
2293
2294 #ifndef CONFIG_YAFFS_DOES_ECC
2295         param->use_nand_ecc = 1;
2296 #endif
2297
2298         param->skip_checkpt_rd = options.skip_checkpoint_read;
2299         param->skip_checkpt_wr = options.skip_checkpoint_write;
2300
2301         mutex_lock(&yaffs_context_lock);
2302         /* Get a mount id */
2303         found = 0;
2304         for (mount_id = 0; !found; mount_id++) {
2305                 found = 1;
2306                 list_for_each(l, &yaffs_context_list) {
2307                         context_iterator =
2308                             list_entry(l, struct yaffs_linux_context,
2309                                        context_list);
2310                         if (context_iterator->mount_id == mount_id)
2311                                 found = 0;
2312                 }
2313         }
2314         context->mount_id = mount_id;
2315
2316         list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2317                       &yaffs_context_list);
2318         mutex_unlock(&yaffs_context_lock);
2319
2320         /* Directory search handling... */
2321         INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2322         param->remove_obj_fn = yaffs_remove_obj_callback;
2323
2324         mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2325
2326         yaffs_gross_lock(dev);
2327
2328         err = yaffs_guts_initialise(dev);
2329
2330         yaffs_trace(YAFFS_TRACE_OS,
2331                 "yaffs_read_super: guts initialised %s",
2332                 (err == YAFFS_OK) ? "OK" : "FAILED");
2333
2334         if (err == YAFFS_OK)
2335                 yaffs_bg_start(dev);
2336
2337         if (!context->bg_thread)
2338                 param->defered_dir_update = 0;
2339
2340         /* Release lock before yaffs_get_inode() */
2341         yaffs_gross_unlock(dev);
2342
2343         /* Create root inode */
2344         if (err == YAFFS_OK)
2345                 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2346
2347         if (!inode)
2348                 return NULL;
2349
2350         inode->i_op = &yaffs_dir_inode_operations;
2351         inode->i_fop = &yaffs_dir_operations;
2352
2353         yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2354
2355         root = d_alloc_root(inode);
2356
2357         yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
2358
2359         if (!root) {
2360                 iput(inode);
2361                 return NULL;
2362         }
2363         sb->s_root = root;
2364         sb->s_dirt = !dev->is_checkpointed;
2365         yaffs_trace(YAFFS_TRACE_ALWAYS,
2366                 "yaffs_read_super: is_checkpointed %d",
2367                 dev->is_checkpointed);
2368
2369         yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
2370         return sb;
2371 }
2372
2373 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2374                                          int silent)
2375 {
2376         return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2377 }
2378
2379 static int yaffs_read_super(struct file_system_type *fs,
2380                             int flags, const char *dev_name,
2381                             void *data, struct vfsmount *mnt)
2382 {
2383
2384         return get_sb_bdev(fs, flags, dev_name, data,
2385                            yaffs_internal_read_super_mtd, mnt);
2386 }
2387
2388 static struct file_system_type yaffs_fs_type = {
2389         .owner = THIS_MODULE,
2390         .name = "yaffs",
2391         .get_sb = yaffs_read_super,
2392         .kill_sb = kill_block_super,
2393         .fs_flags = FS_REQUIRES_DEV,
2394 };
2395
2396 #ifdef CONFIG_YAFFS_YAFFS2
2397
2398 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2399                                           int silent)
2400 {
2401         return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2402 }
2403
2404 static int yaffs2_read_super(struct file_system_type *fs,
2405                              int flags, const char *dev_name, void *data,
2406                              struct vfsmount *mnt)
2407 {
2408         return get_sb_bdev(fs, flags, dev_name, data,
2409                            yaffs2_internal_read_super_mtd, mnt);
2410 }
2411
2412 static struct file_system_type yaffs2_fs_type = {
2413         .owner = THIS_MODULE,
2414         .name = "yaffs2",
2415         .get_sb = yaffs2_read_super,
2416         .kill_sb = kill_block_super,
2417         .fs_flags = FS_REQUIRES_DEV,
2418 };
2419 #endif /* CONFIG_YAFFS_YAFFS2 */
2420
2421 static struct proc_dir_entry *my_proc_entry;
2422
2423 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
2424 {
2425         struct yaffs_param *param = &dev->param;
2426         buf += sprintf(buf, "start_block........... %d\n", param->start_block);
2427         buf += sprintf(buf, "end_block............. %d\n", param->end_block);
2428         buf += sprintf(buf, "total_bytes_per_chunk. %d\n",
2429                         param->total_bytes_per_chunk);
2430         buf += sprintf(buf, "use_nand_ecc.......... %d\n",
2431                         param->use_nand_ecc);
2432         buf += sprintf(buf, "no_tags_ecc........... %d\n", param->no_tags_ecc);
2433         buf += sprintf(buf, "is_yaffs2............. %d\n", param->is_yaffs2);
2434         buf += sprintf(buf, "inband_tags........... %d\n", param->inband_tags);
2435         buf += sprintf(buf, "empty_lost_n_found.... %d\n",
2436                         param->empty_lost_n_found);
2437         buf += sprintf(buf, "disable_lazy_load..... %d\n",
2438                         param->disable_lazy_load);
2439         buf += sprintf(buf, "refresh_period........ %d\n",
2440                         param->refresh_period);
2441         buf += sprintf(buf, "n_caches.............. %d\n", param->n_caches);
2442         buf += sprintf(buf, "n_reserved_blocks..... %d\n",
2443                         param->n_reserved_blocks);
2444         buf += sprintf(buf, "always_check_erased... %d\n",
2445                         param->always_check_erased);
2446
2447         return buf;
2448 }
2449
2450 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
2451 {
2452         buf +=
2453             sprintf(buf, "data_bytes_per_chunk.. %d\n",
2454                     dev->data_bytes_per_chunk);
2455         buf += sprintf(buf, "chunk_grp_bits........ %d\n", dev->chunk_grp_bits);
2456         buf += sprintf(buf, "chunk_grp_size........ %d\n", dev->chunk_grp_size);
2457         buf +=
2458             sprintf(buf, "n_erased_blocks....... %d\n", dev->n_erased_blocks);
2459         buf +=
2460             sprintf(buf, "blocks_in_checkpt..... %d\n", dev->blocks_in_checkpt);
2461         buf += sprintf(buf, "\n");
2462         buf += sprintf(buf, "n_tnodes.............. %d\n", dev->n_tnodes);
2463         buf += sprintf(buf, "n_obj................. %d\n", dev->n_obj);
2464         buf += sprintf(buf, "n_free_chunks......... %d\n", dev->n_free_chunks);
2465         buf += sprintf(buf, "\n");
2466         buf += sprintf(buf, "n_page_writes......... %u\n", dev->n_page_writes);
2467         buf += sprintf(buf, "n_page_reads.......... %u\n", dev->n_page_reads);
2468         buf += sprintf(buf, "n_erasures............ %u\n", dev->n_erasures);
2469         buf += sprintf(buf, "n_gc_copies........... %u\n", dev->n_gc_copies);
2470         buf += sprintf(buf, "all_gcs............... %u\n", dev->all_gcs);
2471         buf +=
2472             sprintf(buf, "passive_gc_count...... %u\n", dev->passive_gc_count);
2473         buf +=
2474             sprintf(buf, "oldest_dirty_gc_count. %u\n",
2475                     dev->oldest_dirty_gc_count);
2476         buf += sprintf(buf, "n_gc_blocks........... %u\n", dev->n_gc_blocks);
2477         buf += sprintf(buf, "bg_gcs................ %u\n", dev->bg_gcs);
2478         buf +=
2479             sprintf(buf, "n_retired_writes...... %u\n", dev->n_retired_writes);
2480         buf +=
2481             sprintf(buf, "n_retired_blocks...... %u\n", dev->n_retired_blocks);
2482         buf += sprintf(buf, "n_ecc_fixed........... %u\n", dev->n_ecc_fixed);
2483         buf += sprintf(buf, "n_ecc_unfixed......... %u\n", dev->n_ecc_unfixed);
2484         buf +=
2485             sprintf(buf, "n_tags_ecc_fixed...... %u\n", dev->n_tags_ecc_fixed);
2486         buf +=
2487             sprintf(buf, "n_tags_ecc_unfixed.... %u\n",
2488                     dev->n_tags_ecc_unfixed);
2489         buf += sprintf(buf, "cache_hits............ %u\n", dev->cache_hits);
2490         buf +=
2491             sprintf(buf, "n_deleted_files....... %u\n", dev->n_deleted_files);
2492         buf +=
2493             sprintf(buf, "n_unlinked_files...... %u\n", dev->n_unlinked_files);
2494         buf += sprintf(buf, "refresh_count......... %u\n", dev->refresh_count);
2495         buf += sprintf(buf, "n_bg_deletions........ %u\n", dev->n_bg_deletions);
2496
2497         return buf;
2498 }
2499
2500 static int yaffs_proc_read(char *page,
2501                            char **start,
2502                            off_t offset, int count, int *eof, void *data)
2503 {
2504         struct list_head *item;
2505         char *buf = page;
2506         int step = offset;
2507         int n = 0;
2508
2509         /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2510          * We use 'offset' (*ppos) to indicate where we are in dev_list.
2511          * This also assumes the user has posted a read buffer large
2512          * enough to hold the complete output; but that's life in /proc.
2513          */
2514
2515         *(int *)start = 1;
2516
2517         /* Print header first */
2518         if (step == 0)
2519                 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__ "\n");
2520         else if (step == 1)
2521                 buf += sprintf(buf, "\n");
2522         else {
2523                 step -= 2;
2524
2525                 mutex_lock(&yaffs_context_lock);
2526
2527                 /* Locate and print the Nth entry.  Order N-squared but N is small. */
2528                 list_for_each(item, &yaffs_context_list) {
2529                         struct yaffs_linux_context *dc =
2530                             list_entry(item, struct yaffs_linux_context,
2531                                        context_list);
2532                         struct yaffs_dev *dev = dc->dev;
2533
2534                         if (n < (step & ~1)) {
2535                                 n += 2;
2536                                 continue;
2537                         }
2538                         if ((step & 1) == 0) {
2539                                 buf +=
2540                                     sprintf(buf, "\nDevice %d \"%s\"\n", n,
2541                                             dev->param.name);
2542                                 buf = yaffs_dump_dev_part0(buf, dev);
2543                         } else {
2544                                 buf = yaffs_dump_dev_part1(buf, dev);
2545                         }
2546
2547                         break;
2548                 }
2549                 mutex_unlock(&yaffs_context_lock);
2550         }
2551
2552         return buf - page < count ? buf - page : count;
2553 }
2554
2555
2556 /**
2557  * Set the verbosity of the warnings and error messages.
2558  *
2559  * Note that the names can only be a..z or _ with the current code.
2560  */
2561
2562 static struct {
2563         char *mask_name;
2564         unsigned mask_bitfield;
2565 } mask_flags[] = {
2566         {"allocate", YAFFS_TRACE_ALLOCATE}, 
2567         {"always", YAFFS_TRACE_ALWAYS},
2568         {"background", YAFFS_TRACE_BACKGROUND},
2569         {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2570         {"buffers", YAFFS_TRACE_BUFFERS},
2571         {"bug", YAFFS_TRACE_BUG},
2572         {"checkpt", YAFFS_TRACE_CHECKPOINT},
2573         {"deletion", YAFFS_TRACE_DELETION},
2574         {"erase", YAFFS_TRACE_ERASE},
2575         {"error", YAFFS_TRACE_ERROR},
2576         {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2577         {"gc", YAFFS_TRACE_GC},
2578         {"lock", YAFFS_TRACE_LOCK},
2579         {"mtd", YAFFS_TRACE_MTD},
2580         {"nandaccess", YAFFS_TRACE_NANDACCESS},
2581         {"os", YAFFS_TRACE_OS},
2582         {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2583         {"scan", YAFFS_TRACE_SCAN},
2584         {"mount", YAFFS_TRACE_MOUNT},
2585         {"tracing", YAFFS_TRACE_TRACING},
2586         {"sync", YAFFS_TRACE_SYNC},
2587         {"write", YAFFS_TRACE_WRITE},
2588         {"verify", YAFFS_TRACE_VERIFY},
2589         {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2590         {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2591         {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2592         {"all", 0xffffffff},
2593         {"none", 0},
2594         {NULL, 0},
2595 };
2596
2597 #define MAX_MASK_NAME_LENGTH 40
2598 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2599                                           unsigned long count, void *data)
2600 {
2601         unsigned rg = 0, mask_bitfield;
2602         char *end;
2603         char *mask_name;
2604         const char *x;
2605         char substring[MAX_MASK_NAME_LENGTH + 1];
2606         int i;
2607         int done = 0;
2608         int add, len = 0;
2609         int pos = 0;
2610
2611         rg = yaffs_trace_mask;
2612
2613         while (!done && (pos < count)) {
2614                 done = 1;
2615                 while ((pos < count) && isspace(buf[pos]))
2616                         pos++;
2617
2618                 switch (buf[pos]) {
2619                 case '+':
2620                 case '-':
2621                 case '=':
2622                         add = buf[pos];
2623                         pos++;
2624                         break;
2625
2626                 default:
2627                         add = ' ';
2628                         break;
2629                 }
2630                 mask_name = NULL;
2631
2632                 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2633
2634                 if (end > buf + pos) {
2635                         mask_name = "numeral";
2636                         len = end - (buf + pos);
2637                         pos += len;
2638                         done = 0;
2639                 } else {
2640                         for (x = buf + pos, i = 0;
2641                              (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2642                              i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2643                                 substring[i] = *x;
2644                         substring[i] = '\0';
2645
2646                         for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2647                                 if (strcmp(substring, mask_flags[i].mask_name)
2648                                     == 0) {
2649                                         mask_name = mask_flags[i].mask_name;
2650                                         mask_bitfield =
2651                                             mask_flags[i].mask_bitfield;
2652                                         done = 0;
2653                                         break;
2654                                 }
2655                         }
2656                 }
2657
2658                 if (mask_name != NULL) {
2659                         done = 0;
2660                         switch (add) {
2661                         case '-':
2662                                 rg &= ~mask_bitfield;
2663                                 break;
2664                         case '+':
2665                                 rg |= mask_bitfield;
2666                                 break;
2667                         case '=':
2668                                 rg = mask_bitfield;
2669                                 break;
2670                         default:
2671                                 rg |= mask_bitfield;
2672                                 break;
2673                         }
2674                 }
2675         }
2676
2677         yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
2678
2679         printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
2680
2681         if (rg & YAFFS_TRACE_ALWAYS) {
2682                 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2683                         char flag;
2684                         flag = ((rg & mask_flags[i].mask_bitfield) ==
2685                                 mask_flags[i].mask_bitfield) ? '+' : '-';
2686                         printk(KERN_DEBUG "%c%s\n", flag,
2687                                mask_flags[i].mask_name);
2688                 }
2689         }
2690
2691         return count;
2692 }
2693
2694 static int yaffs_proc_write(struct file *file, const char *buf,
2695                             unsigned long count, void *data)
2696 {
2697         return yaffs_proc_write_trace_options(file, buf, count, data);
2698 }
2699
2700 /* Stuff to handle installation of file systems */
2701 struct file_system_to_install {
2702         struct file_system_type *fst;
2703         int installed;
2704 };
2705
2706 static struct file_system_to_install fs_to_install[] = {
2707         {&yaffs_fs_type, 0},
2708         {&yaffs2_fs_type, 0},
2709         {NULL, 0}
2710 };
2711
2712 static int __init init_yaffs_fs(void)
2713 {
2714         int error = 0;
2715         struct file_system_to_install *fsinst;
2716
2717         yaffs_trace(YAFFS_TRACE_ALWAYS,
2718                 "yaffs built " __DATE__ " " __TIME__ " Installing.");
2719
2720 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2721         yaffs_trace(YAFFS_TRACE_ALWAYS,
2722                 "\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n");
2723 #endif
2724
2725         mutex_init(&yaffs_context_lock);
2726
2727         /* Install the proc_fs entries */
2728         my_proc_entry = create_proc_entry("yaffs",
2729                                           S_IRUGO | S_IFREG, YPROC_ROOT);
2730
2731         if (my_proc_entry) {
2732                 my_proc_entry->write_proc = yaffs_proc_write;
2733                 my_proc_entry->read_proc = yaffs_proc_read;
2734                 my_proc_entry->data = NULL;
2735         } else {
2736                 return -ENOMEM;
2737         }
2738
2739
2740         /* Now add the file system entries */
2741
2742         fsinst = fs_to_install;
2743
2744         while (fsinst->fst && !error) {
2745                 error = register_filesystem(fsinst->fst);
2746                 if (!error)
2747                         fsinst->installed = 1;
2748                 fsinst++;
2749         }
2750
2751         /* Any errors? uninstall  */
2752         if (error) {
2753                 fsinst = fs_to_install;
2754
2755                 while (fsinst->fst) {
2756                         if (fsinst->installed) {
2757                                 unregister_filesystem(fsinst->fst);
2758                                 fsinst->installed = 0;
2759                         }
2760                         fsinst++;
2761                 }
2762         }
2763
2764         return error;
2765 }
2766
2767 static void __exit exit_yaffs_fs(void)
2768 {
2769
2770         struct file_system_to_install *fsinst;
2771
2772         yaffs_trace(YAFFS_TRACE_ALWAYS,
2773                 "yaffs built " __DATE__ " " __TIME__ " removing.");
2774
2775         remove_proc_entry("yaffs", YPROC_ROOT);
2776
2777         fsinst = fs_to_install;
2778
2779         while (fsinst->fst) {
2780                 if (fsinst->installed) {
2781                         unregister_filesystem(fsinst->fst);
2782                         fsinst->installed = 0;
2783                 }
2784                 fsinst++;
2785         }
2786 }
2787
2788 module_init(init_yaffs_fs)
2789     module_exit(exit_yaffs_fs)
2790
2791     MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2792 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
2793 MODULE_LICENSE("GPL");