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