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