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