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