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