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