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