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