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