2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2009 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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
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.
23 * This is the file system front-end to YAFFS that hooks it up to
27 * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
29 * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
31 * >> inode->u.generic_ip points to the associated yaffs_Object.
34 const char *yaffs_fs_c_version =
35 "$Id: yaffs_fs.c,v 1.102 2010-03-15 23:10:33 charles Exp $";
36 extern const char *yaffs_guts_c_version;
38 #include <linux/version.h>
40 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
41 #define YAFFS_COMPILE_BACKGROUND
44 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
45 #include <linux/config.h>
47 #include <linux/kernel.h>
48 #include <linux/module.h>
49 #include <linux/slab.h>
50 #include <linux/init.h>
52 #include <linux/proc_fs.h>
53 #include <linux/smp_lock.h>
54 #include <linux/pagemap.h>
55 #include <linux/mtd/mtd.h>
56 #include <linux/interrupt.h>
57 #include <linux/string.h>
58 #include <linux/ctype.h>
59 #include <linux/exportfs.h>
61 #ifdef YAFFS_COMPILE_BACKGROUND
62 #include <linux/kthread.h>
63 #include <linux/delay.h>
67 #include "asm/div64.h"
72 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
74 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
75 #include <linux/statfs.h>
76 #define UnlockPage(p) unlock_page(p)
77 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
79 /* FIXME: use sb->s_id instead ? */
80 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
84 #include <linux/locks.h>
85 #define BDEVNAME_SIZE 0
86 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
88 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
89 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
95 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
96 #define YPROC_ROOT (&proc_root)
98 #define YPROC_ROOT NULL
101 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
102 #define WRITE_SIZE_STR "writesize"
103 #define WRITE_SIZE(mtd) ((mtd)->writesize)
105 #define WRITE_SIZE_STR "oobblock"
106 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
109 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
110 #define YAFFS_USE_WRITE_BEGIN_END 1
112 #define YAFFS_USE_WRITE_BEGIN_END 0
115 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
116 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
118 uint64_t result = partition_size;
119 do_div(result, block_size);
120 return (uint32_t)result;
123 #define YCALCBLOCKS(s, b) ((s)/(b))
126 #include <linux/uaccess.h>
128 #include "yportenv.h"
129 #include "yaffs_trace.h"
130 #include "yaffs_guts.h"
132 #include "yaffs_linux.h"
134 #include <linux/mtd/mtd.h>
135 #include "yaffs_mtdif.h"
136 #include "yaffs_mtdif1.h"
137 #include "yaffs_mtdif2.h"
139 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
140 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
141 unsigned int yaffs_auto_checkpoint = 1;
142 unsigned int yaffs_gc_control = 1;
144 /* Module Parameters */
145 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
146 module_param(yaffs_traceMask, uint, 0644);
147 module_param(yaffs_wr_attempts, uint, 0644);
148 module_param(yaffs_auto_checkpoint, uint, 0644);
149 module_param(yaffs_gc_control, uint, 0644);
151 MODULE_PARM(yaffs_traceMask, "i");
152 MODULE_PARM(yaffs_wr_attempts, "i");
153 MODULE_PARM(yaffs_auto_checkpoint, "i");
154 MODULE_PARM(yaffs_gc_control, "i");
157 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
158 /* use iget and read_inode */
159 #define Y_IGET(sb, inum) iget((sb), (inum))
160 static void yaffs_read_inode(struct inode *inode);
163 /* Call local equivalent */
164 #define YAFFS_USE_OWN_IGET
165 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
167 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
170 /*#define T(x) printk x */
172 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
173 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
175 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
178 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
179 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
181 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
182 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
184 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
188 #define update_dir_time(dir) do {\
189 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
192 static void yaffs_put_super(struct super_block *sb);
194 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
196 static ssize_t yaffs_hold_space(struct file *f);
197 static void yaffs_release_space(struct file *f);
199 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
200 static int yaffs_file_flush(struct file *file, fl_owner_t id);
202 static int yaffs_file_flush(struct file *file);
205 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
208 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
210 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
211 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
212 struct nameidata *n);
213 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
214 struct nameidata *n);
216 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
217 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
219 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
220 struct dentry *dentry);
221 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
222 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
223 const char *symname);
224 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
226 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
227 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
230 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
233 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
234 struct inode *new_dir, struct dentry *new_dentry);
235 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
237 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
238 static int yaffs_sync_fs(struct super_block *sb, int wait);
239 static void yaffs_write_super(struct super_block *sb);
241 static int yaffs_sync_fs(struct super_block *sb);
242 static int yaffs_write_super(struct super_block *sb);
245 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
246 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
247 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
248 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
250 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
253 #ifdef YAFFS_HAS_PUT_INODE
254 static void yaffs_put_inode(struct inode *inode);
257 static void yaffs_delete_inode(struct inode *);
258 static void yaffs_clear_inode(struct inode *);
260 static int yaffs_readpage(struct file *file, struct page *page);
261 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
262 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
264 static int yaffs_writepage(struct page *page);
268 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
269 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
270 loff_t pos, unsigned len, unsigned flags,
271 struct page **pagep, void **fsdata);
272 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
273 loff_t pos, unsigned len, unsigned copied,
274 struct page *pg, void *fsdadata);
276 static int yaffs_prepare_write(struct file *f, struct page *pg,
277 unsigned offset, unsigned to);
278 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
283 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
285 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
286 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
288 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
290 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
292 static struct address_space_operations yaffs_file_address_operations = {
293 .readpage = yaffs_readpage,
294 .writepage = yaffs_writepage,
295 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
296 .write_begin = yaffs_write_begin,
297 .write_end = yaffs_write_end,
299 .prepare_write = yaffs_prepare_write,
300 .commit_write = yaffs_commit_write,
304 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
305 static const struct file_operations yaffs_file_operations = {
306 .read = do_sync_read,
307 .write = do_sync_write,
308 .aio_read = generic_file_aio_read,
309 .aio_write = generic_file_aio_write,
310 .mmap = generic_file_mmap,
311 .flush = yaffs_file_flush,
312 .fsync = yaffs_sync_object,
313 .splice_read = generic_file_splice_read,
314 .splice_write = generic_file_splice_write,
315 .llseek = generic_file_llseek,
318 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
320 static const struct file_operations yaffs_file_operations = {
321 .read = do_sync_read,
322 .write = do_sync_write,
323 .aio_read = generic_file_aio_read,
324 .aio_write = generic_file_aio_write,
325 .mmap = generic_file_mmap,
326 .flush = yaffs_file_flush,
327 .fsync = yaffs_sync_object,
328 .sendfile = generic_file_sendfile,
333 static const struct file_operations yaffs_file_operations = {
334 .read = generic_file_read,
335 .write = generic_file_write,
336 .mmap = generic_file_mmap,
337 .flush = yaffs_file_flush,
338 .fsync = yaffs_sync_object,
339 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
340 .sendfile = generic_file_sendfile,
345 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
346 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
348 void * kaddr = kmap_atomic(page, KM_USER0);
349 memset(kaddr + start, 0, end - start);
350 kunmap_atomic(kaddr, KM_USER0);
351 flush_dcache_page(page);
356 static const struct inode_operations yaffs_file_inode_operations = {
357 .setattr = yaffs_setattr,
360 static const struct inode_operations yaffs_symlink_inode_operations = {
361 .readlink = yaffs_readlink,
362 .follow_link = yaffs_follow_link,
363 .setattr = yaffs_setattr,
366 static const struct inode_operations yaffs_dir_inode_operations = {
367 .create = yaffs_create,
368 .lookup = yaffs_lookup,
370 .unlink = yaffs_unlink,
371 .symlink = yaffs_symlink,
372 .mkdir = yaffs_mkdir,
373 .rmdir = yaffs_unlink,
374 .mknod = yaffs_mknod,
375 .rename = yaffs_rename,
376 .setattr = yaffs_setattr,
379 static const struct file_operations yaffs_dir_operations = {
380 .read = generic_read_dir,
381 .readdir = yaffs_readdir,
382 .fsync = yaffs_sync_object,
383 .llseek = yaffs_dir_llseek,
386 static const struct super_operations yaffs_super_ops = {
387 .statfs = yaffs_statfs,
389 #ifndef YAFFS_USE_OWN_IGET
390 .read_inode = yaffs_read_inode,
392 #ifdef YAFFS_HAS_PUT_INODE
393 .put_inode = yaffs_put_inode,
395 .put_super = yaffs_put_super,
396 .delete_inode = yaffs_delete_inode,
397 .clear_inode = yaffs_clear_inode,
398 .sync_fs = yaffs_sync_fs,
399 .write_super = yaffs_write_super,
402 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
404 return yaffs_gc_control;
407 static void yaffs_GrossLock(yaffs_Device *dev)
409 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
410 down(&(yaffs_DeviceToContext(dev)->grossLock));
411 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
414 static void yaffs_GrossUnlock(yaffs_Device *dev)
416 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
417 up(&(yaffs_DeviceToContext(dev)->grossLock));
422 static struct inode *
423 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
425 return yaffs_iget(sb, ino);
428 static struct dentry *
429 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
431 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
434 static struct dentry *
435 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
437 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
440 struct dentry *yaffs2_get_parent(struct dentry *dentry)
443 struct super_block *sb = dentry->d_inode->i_sb;
444 struct dentry *parent = ERR_PTR(-ENOENT);
446 unsigned long parent_ino;
448 yaffs_Object *parent_obj;
450 d_obj = yaffs_InodeToObject(dentry->d_inode);
453 parent_obj = d_obj->parent;
455 parent_ino = yaffs_GetObjectInode(parent_obj);
456 inode = yaffs_iget(sb, parent_ino);
459 parent = ERR_CAST(inode);
461 parent = d_obtain_alias(inode);
462 if (!IS_ERR(parent)) {
463 parent = ERR_PTR(-ENOMEM);
473 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,9))
474 /* Just declare a zero structure as a NULL value implies
475 * using the default functions of expfs.
478 static struct export_operations yaffs_export_ops =
480 .fh_to_dentry = yaffs2_fh_to_dentry,
481 .fh_to_parent = yaffs2_fh_to_parent,
482 .get_parent = yaffs2_get_parent,
487 /*-----------------------------------------------------------------*/
488 /* Directory search context allows us to unlock access to yaffs during
489 * filldir without causing problems with the directory being modified.
490 * This is similar to the tried and tested mechanism used in yaffs direct.
492 * A search context iterates along a doubly linked list of siblings in the
493 * directory. If the iterating object is deleted then this would corrupt
494 * the list iteration, likely causing a crash. The search context avoids
495 * this by using the removeObjectCallback to move the search context to the
496 * next object before the object is deleted.
498 * Many readdirs (and thus seach conexts) may be alive simulateously so
499 * each yaffs_Device has a list of these.
501 * A seach context lives for the duration of a readdir.
503 * All these functions must be called while yaffs is locked.
506 struct yaffs_SearchContext {
508 yaffs_Object *dirObj;
509 yaffs_Object *nextReturn;
510 struct ylist_head others;
514 * yaffs_NewSearch() creates a new search context, initialises it and
515 * adds it to the device's search context list.
517 * Called at start of readdir.
519 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
521 yaffs_Device *dev = dir->myDev;
522 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
526 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
527 sc->nextReturn = NULL;
529 sc->nextReturn = ylist_entry(
530 dir->variant.directoryVariant.children.next,
531 yaffs_Object,siblings);
532 YINIT_LIST_HEAD(&sc->others);
533 ylist_add(&sc->others,&(yaffs_DeviceToContext(dev)->searchContexts));
539 * yaffs_EndSearch() disposes of a search context and cleans up.
541 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
544 ylist_del(&sc->others);
550 * yaffs_SearchAdvance() moves a search context to the next object.
551 * Called when the search iterates or when an object removal causes
552 * the search context to be moved to the next object.
554 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
559 if( sc->nextReturn == NULL ||
560 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
561 sc->nextReturn = NULL;
563 struct ylist_head *next = sc->nextReturn->siblings.next;
565 if( next == &sc->dirObj->variant.directoryVariant.children)
566 sc->nextReturn = NULL; /* end of list */
568 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
573 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
574 * We check open search contexts and advance any which are currently
575 * on the object being iterated.
577 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
580 struct ylist_head *i;
581 struct yaffs_SearchContext *sc;
582 struct ylist_head *search_contexts = &(yaffs_DeviceToContext(obj->myDev)->searchContexts);
585 /* Iterate through the directory search contexts.
586 * If any are currently on the object being removed, then advance
587 * the search context to the next object to prevent a hanging pointer.
589 ylist_for_each(i, search_contexts) {
591 sc = ylist_entry(i, struct yaffs_SearchContext,others);
592 if(sc->nextReturn == obj)
593 yaffs_SearchAdvance(sc);
600 /*-----------------------------------------------------------------*/
602 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
605 unsigned char *alias;
608 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
610 yaffs_GrossLock(dev);
612 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
614 yaffs_GrossUnlock(dev);
619 ret = vfs_readlink(dentry, buffer, buflen, alias);
624 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
625 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
627 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
630 unsigned char *alias;
632 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
634 yaffs_GrossLock(dev);
636 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
638 yaffs_GrossUnlock(dev);
645 ret = vfs_follow_link(nd, alias);
648 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
655 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
659 * Lookup is used to find objects in the fs
661 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
663 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
666 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
670 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
672 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
674 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
675 yaffs_GrossLock(dev);
678 ("yaffs_lookup for %d:%s\n",
679 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
681 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
682 dentry->d_name.name);
684 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
686 /* Can't hold gross lock when calling yaffs_get_inode() */
687 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
688 yaffs_GrossUnlock(dev);
692 ("yaffs_lookup found %d\n", obj->objectId));
694 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
698 ("yaffs_loookup dentry \n"));
699 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
700 * d_add even if NULL inode */
702 /*dget(dentry); // try to solve directory bug */
703 d_add(dentry, inode);
711 T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
715 /* added NCB for 2.5/6 compatability - forces add even if inode is
716 * NULL which creates dentry hash */
717 d_add(dentry, inode);
723 #ifdef YAFFS_HAS_PUT_INODE
725 /* For now put inode is just for debugging
726 * Put inode is called when the inode **structure** is put.
728 static void yaffs_put_inode(struct inode *inode)
731 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
732 atomic_read(&inode->i_count)));
737 /* clear is called to tell the fs to release any per-inode data it holds */
738 static void yaffs_clear_inode(struct inode *inode)
743 obj = yaffs_InodeToObject(inode);
746 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
747 atomic_read(&inode->i_count),
748 obj ? "object exists" : "null object"));
752 yaffs_GrossLock(dev);
754 /* Clear the association between the inode and
758 yaffs_InodeToObjectLV(inode) = NULL;
760 /* If the object freeing was deferred, then the real
762 * This should fix the inode inconsistency problem.
765 yaffs_HandleDeferedFree(obj);
767 yaffs_GrossUnlock(dev);
772 /* delete is called when the link count is zero and the inode
773 * is put (ie. nobody wants to know about it anymore, time to
775 * NB Must call clear_inode()
777 static void yaffs_delete_inode(struct inode *inode)
779 yaffs_Object *obj = yaffs_InodeToObject(inode);
783 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
784 atomic_read(&inode->i_count),
785 obj ? "object exists" : "null object"));
789 yaffs_GrossLock(dev);
790 yaffs_DeleteObject(obj);
791 yaffs_GrossUnlock(dev);
793 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
794 truncate_inode_pages(&inode->i_data, 0);
799 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
800 static int yaffs_file_flush(struct file *file, fl_owner_t id)
802 static int yaffs_file_flush(struct file *file)
805 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
807 yaffs_Device *dev = obj->myDev;
810 ("yaffs_file_flush object %d (%s)\n", obj->objectId,
811 obj->dirty ? "dirty" : "clean"));
813 yaffs_GrossLock(dev);
815 yaffs_FlushFile(obj, 1, 0);
817 yaffs_GrossUnlock(dev);
822 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
824 /* Lifted from jffs2 */
827 unsigned char *pg_buf;
832 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock at %08x, size %08x\n",
833 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
834 (unsigned)PAGE_CACHE_SIZE));
836 obj = yaffs_DentryToObject(f->f_dentry);
840 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
841 BUG_ON(!PageLocked(pg));
848 /* FIXME: Can kmap fail? */
850 yaffs_GrossLock(dev);
852 ret = yaffs_ReadDataFromFile(obj, pg_buf,
853 pg->index << PAGE_CACHE_SHIFT,
856 yaffs_GrossUnlock(dev);
862 ClearPageUptodate(pg);
869 flush_dcache_page(pg);
872 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock done\n"));
876 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
878 int ret = yaffs_readpage_nolock(f, pg);
883 static int yaffs_readpage(struct file *f, struct page *pg)
887 T(YAFFS_TRACE_OS, ("yaffs_readpage\n"));
888 ret=yaffs_readpage_unlock(f, pg);
889 T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
893 /* writepage inspired by/stolen from smbfs */
895 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
896 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
898 static int yaffs_writepage(struct page *page)
901 struct address_space *mapping = page->mapping;
903 unsigned long end_index;
912 inode = mapping->host;
915 i_size = i_size_read(inode);
917 end_index = i_size >> PAGE_CACHE_SHIFT;
919 if(page->index < end_index)
920 nBytes = PAGE_CACHE_SIZE;
922 nBytes = i_size & (PAGE_CACHE_SIZE -1);
924 if (page->index > end_index || !nBytes) {
926 ("yaffs_writepage at %08x, inode size = %08x!!!\n",
927 (unsigned)(page->index << PAGE_CACHE_SHIFT),
928 (unsigned)inode->i_size));
930 (" -> don't care!!\n"));
932 zero_user_segment(page,0,PAGE_CACHE_SIZE);
933 set_page_writeback(page);
935 end_page_writeback(page);
940 if(nBytes != PAGE_CACHE_SIZE)
941 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
947 obj = yaffs_InodeToObject(inode);
948 yaffs_GrossLock(obj->myDev);
951 ("yaffs_writepage at %08x, size %08x\n",
952 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
954 ("writepag0: obj = %05x, ino = %05x\n",
955 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
957 nWritten = yaffs_WriteDataToFile(obj, buffer,
958 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
961 ("writepag1: obj = %05x, ino = %05x\n",
962 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
964 yaffs_GrossUnlock(obj->myDev);
967 set_page_writeback(page);
969 end_page_writeback(page);
972 return (nWritten == nBytes) ? 0 : -ENOSPC;
976 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
977 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
978 loff_t pos, unsigned len, unsigned flags,
979 struct page **pagep, void **fsdata)
981 struct page *pg = NULL;
982 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
988 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
989 pg = grab_cache_page_write_begin(mapping, index, flags);
991 pg = __grab_cache_page(mapping, index);
999 T(YAFFS_TRACE_OS, ("start yaffs_write_begin index %d(%x) uptodate %d\n",(int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
1002 space_held = yaffs_hold_space(filp);
1009 /* Update page if required */
1011 if (!Page_Uptodate(pg))
1012 ret = yaffs_readpage_nolock(filp, pg);
1017 /* Happy path return */
1018 T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
1023 T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
1025 yaffs_release_space(filp);
1028 page_cache_release(pg);
1035 static int yaffs_prepare_write(struct file *f, struct page *pg,
1036 unsigned offset, unsigned to)
1038 T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
1040 if (!Page_Uptodate(pg))
1041 return yaffs_readpage_nolock(f, pg);
1046 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1047 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1048 loff_t pos, unsigned len, unsigned copied,
1049 struct page *pg, void *fsdadata)
1053 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1056 addr = kva + offset_into_page;
1059 ("yaffs_write_end addr %x pos %x nBytes %d\n",
1063 ret = yaffs_file_write(filp, addr, copied, &pos);
1065 if (ret != copied) {
1067 ("yaffs_write_end not same size ret %d copied %d\n",
1076 yaffs_release_space(filp);
1078 page_cache_release(pg);
1083 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1088 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1089 int nBytes = to - offset;
1092 unsigned spos = pos;
1096 addr = kva + offset;
1098 saddr = (unsigned) addr;
1101 ("yaffs_commit_write addr %x pos %x nBytes %d\n",
1102 saddr, spos, nBytes));
1104 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1106 if (nWritten != nBytes) {
1108 ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
1118 ("yaffs_commit_write returning %d\n",
1119 nWritten == nBytes ? 0 : nWritten));
1121 return nWritten == nBytes ? 0 : nWritten;
1126 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1131 /* Check mode against the variant type and attempt to repair if broken. */
1132 __u32 mode = obj->yst_mode;
1133 switch (obj->variantType) {
1134 case YAFFS_OBJECT_TYPE_FILE:
1135 if (!S_ISREG(mode)) {
1136 obj->yst_mode &= ~S_IFMT;
1137 obj->yst_mode |= S_IFREG;
1141 case YAFFS_OBJECT_TYPE_SYMLINK:
1142 if (!S_ISLNK(mode)) {
1143 obj->yst_mode &= ~S_IFMT;
1144 obj->yst_mode |= S_IFLNK;
1148 case YAFFS_OBJECT_TYPE_DIRECTORY:
1149 if (!S_ISDIR(mode)) {
1150 obj->yst_mode &= ~S_IFMT;
1151 obj->yst_mode |= S_IFDIR;
1155 case YAFFS_OBJECT_TYPE_UNKNOWN:
1156 case YAFFS_OBJECT_TYPE_HARDLINK:
1157 case YAFFS_OBJECT_TYPE_SPECIAL:
1163 inode->i_flags |= S_NOATIME;
1165 inode->i_ino = obj->objectId;
1166 inode->i_mode = obj->yst_mode;
1167 inode->i_uid = obj->yst_uid;
1168 inode->i_gid = obj->yst_gid;
1169 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1170 inode->i_blksize = inode->i_sb->s_blocksize;
1172 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1174 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1175 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1176 inode->i_atime.tv_nsec = 0;
1177 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1178 inode->i_mtime.tv_nsec = 0;
1179 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1180 inode->i_ctime.tv_nsec = 0;
1182 inode->i_rdev = obj->yst_rdev;
1183 inode->i_atime = obj->yst_atime;
1184 inode->i_mtime = obj->yst_mtime;
1185 inode->i_ctime = obj->yst_ctime;
1187 inode->i_size = yaffs_GetObjectFileLength(obj);
1188 inode->i_blocks = (inode->i_size + 511) >> 9;
1190 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1193 ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
1194 inode->i_mode, inode->i_uid, inode->i_gid,
1195 (int)inode->i_size, atomic_read(&inode->i_count)));
1197 switch (obj->yst_mode & S_IFMT) {
1198 default: /* fifo, device or socket */
1199 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1200 init_special_inode(inode, obj->yst_mode,
1201 old_decode_dev(obj->yst_rdev));
1203 init_special_inode(inode, obj->yst_mode,
1204 (dev_t) (obj->yst_rdev));
1207 case S_IFREG: /* file */
1208 inode->i_op = &yaffs_file_inode_operations;
1209 inode->i_fop = &yaffs_file_operations;
1210 inode->i_mapping->a_ops =
1211 &yaffs_file_address_operations;
1213 case S_IFDIR: /* directory */
1214 inode->i_op = &yaffs_dir_inode_operations;
1215 inode->i_fop = &yaffs_dir_operations;
1217 case S_IFLNK: /* symlink */
1218 inode->i_op = &yaffs_symlink_inode_operations;
1222 yaffs_InodeToObjectLV(inode) = obj;
1224 obj->myInode = inode;
1228 ("yaffs_FileInode invalid parameters\n"));
1233 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1236 struct inode *inode;
1240 ("yaffs_get_inode for NULL super_block!!\n"));
1247 ("yaffs_get_inode for NULL object!!\n"));
1253 ("yaffs_get_inode for object %d\n", obj->objectId));
1255 inode = Y_IGET(sb, obj->objectId);
1259 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1260 /* iget also increments the inode's i_count */
1261 /* NB You can't be holding grossLock or deadlock will happen! */
1266 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1271 struct inode *inode;
1274 obj = yaffs_DentryToObject(f->f_dentry);
1278 yaffs_GrossLock(dev);
1280 inode = f->f_dentry->d_inode;
1282 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1283 ipos = inode->i_size;
1289 ("yaffs_file_write: hey obj is null!\n"));
1292 ("yaffs_file_write about to write writing %u(%x) bytes"
1293 "to object %d at %d(%x)\n",
1294 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1296 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1299 ("yaffs_file_write: %d(%x) bytes written\n",
1300 (unsigned )n,(unsigned)n));
1305 if (ipos > inode->i_size) {
1306 inode->i_size = ipos;
1307 inode->i_blocks = (ipos + 511) >> 9;
1310 ("yaffs_file_write size updated to %d bytes, "
1312 ipos, (int)(inode->i_blocks)));
1316 yaffs_GrossUnlock(dev);
1317 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1320 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1321 /* For now we just assume few parallel writes and check against a small number. */
1322 /* Todo: need to do this with a counter to handle parallel reads better */
1324 static ssize_t yaffs_hold_space(struct file *f)
1332 obj = yaffs_DentryToObject(f->f_dentry);
1336 yaffs_GrossLock(dev);
1338 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1340 yaffs_GrossUnlock(dev);
1342 return (nFreeChunks > 20) ? 1 : 0;
1345 static void yaffs_release_space(struct file *f)
1351 obj = yaffs_DentryToObject(f->f_dentry);
1355 yaffs_GrossLock(dev);
1358 yaffs_GrossUnlock(dev);
1362 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1370 offset += i_size_read(file->f_path.dentry->d_inode);
1373 offset += file->f_pos;
1378 if (offset != file->f_pos)
1379 file->f_pos = offset;
1388 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1392 struct yaffs_SearchContext *sc;
1393 struct inode *inode = f->f_dentry->d_inode;
1394 unsigned long offset, curoffs;
1398 char name[YAFFS_MAX_NAME_LENGTH + 1];
1400 obj = yaffs_DentryToObject(f->f_dentry);
1403 yaffs_GrossLock(dev);
1405 yaffs_DeviceToContext(dev)->readdirProcess = current;
1409 sc = yaffs_NewSearch(obj);
1415 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
1419 ("yaffs_readdir: entry . ino %d \n",
1420 (int)inode->i_ino));
1421 yaffs_GrossUnlock(dev);
1422 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
1424 yaffs_GrossLock(dev);
1430 ("yaffs_readdir: entry .. ino %d \n",
1431 (int)f->f_dentry->d_parent->d_inode->i_ino));
1432 yaffs_GrossUnlock(dev);
1433 if (filldir(dirent, "..", 2, offset,
1434 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
1436 yaffs_GrossLock(dev);
1443 /* If the directory has changed since the open or last call to
1444 readdir, rewind to after the 2 canned entries. */
1445 #if 0 /* For exportfs patch */
1446 if (f->f_version != inode->i_version) {
1449 f->f_version = inode->i_version;
1453 while(sc->nextReturn){
1456 if (curoffs >= offset) {
1457 int this_inode = yaffs_GetObjectInode(l);
1458 int this_type = yaffs_GetObjectType(l);
1460 yaffs_GetObjectName(l, name,
1461 YAFFS_MAX_NAME_LENGTH + 1);
1463 ("yaffs_readdir: %s inode %d\n", name,
1464 yaffs_GetObjectInode(l)));
1466 yaffs_GrossUnlock(dev);
1476 yaffs_GrossLock(dev);
1481 yaffs_SearchAdvance(sc);
1485 yaffs_DeviceToContext(dev)->readdirProcess = NULL;
1487 yaffs_GrossUnlock(dev);
1489 yaffs_EndSearch(sc);
1497 * File creation. Allocate an inode, and we're done..
1500 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1503 #define YCRED(x) (x->cred)
1506 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1507 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1510 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1514 struct inode *inode;
1516 yaffs_Object *obj = NULL;
1519 yaffs_Object *parent = yaffs_InodeToObject(dir);
1521 int error = -ENOSPC;
1522 uid_t uid = YCRED(current)->fsuid;
1523 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1525 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1530 ("yaffs_mknod: parent object %d type %d\n",
1531 parent->objectId, parent->variantType));
1534 ("yaffs_mknod: could not get parent object\n"));
1538 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1540 dentry->d_name.name, mode, rdev));
1542 dev = parent->myDev;
1544 yaffs_GrossLock(dev);
1546 switch (mode & S_IFMT) {
1548 /* Special (socket, fifo, device...) */
1549 T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
1550 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1551 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1552 gid, old_encode_dev(rdev));
1554 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1558 case S_IFREG: /* file */
1559 T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
1560 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1563 case S_IFDIR: /* directory */
1565 ("yaffs_mknod: making directory\n"));
1566 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1569 case S_IFLNK: /* symlink */
1570 T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
1571 obj = NULL; /* Do we ever get here? */
1575 /* Can not call yaffs_get_inode() with gross lock held */
1576 yaffs_GrossUnlock(dev);
1579 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1580 d_instantiate(dentry, inode);
1581 update_dir_time(dir);
1583 ("yaffs_mknod created object %d count = %d\n",
1584 obj->objectId, atomic_read(&inode->i_count)));
1586 yaffs_FillInodeFromObject(dir,parent);
1589 ("yaffs_mknod failed making object\n"));
1596 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1599 T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
1600 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1604 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1605 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1606 struct nameidata *n)
1608 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1611 T(YAFFS_TRACE_OS, ("yaffs_create\n"));
1612 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1615 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1623 ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1624 dentry->d_name.name));
1625 obj = yaffs_InodeToObject(dir);
1628 yaffs_GrossLock(dev);
1630 retVal = yaffs_Unlink(obj, dentry->d_name.name);
1632 if (retVal == YAFFS_OK) {
1633 dentry->d_inode->i_nlink--;
1635 yaffs_GrossUnlock(dev);
1636 mark_inode_dirty(dentry->d_inode);
1637 update_dir_time(dir);
1640 yaffs_GrossUnlock(dev);
1647 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1648 struct dentry *dentry)
1650 struct inode *inode = old_dentry->d_inode;
1651 yaffs_Object *obj = NULL;
1652 yaffs_Object *link = NULL;
1655 T(YAFFS_TRACE_OS, ("yaffs_link\n"));
1657 obj = yaffs_InodeToObject(inode);
1660 yaffs_GrossLock(dev);
1662 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1663 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1667 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1668 d_instantiate(dentry, old_dentry->d_inode);
1669 atomic_inc(&old_dentry->d_inode->i_count);
1671 ("yaffs_link link count %d i_count %d\n",
1672 old_dentry->d_inode->i_nlink,
1673 atomic_read(&old_dentry->d_inode->i_count)));
1676 yaffs_GrossUnlock(dev);
1679 update_dir_time(dir);
1686 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1687 const char *symname)
1691 uid_t uid = YCRED(current)->fsuid;
1692 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1694 T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
1696 dev = yaffs_InodeToObject(dir)->myDev;
1697 yaffs_GrossLock(dev);
1698 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1699 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1700 yaffs_GrossUnlock(dev);
1703 struct inode *inode;
1705 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1706 d_instantiate(dentry, inode);
1707 update_dir_time(dir);
1708 T(YAFFS_TRACE_OS, ("symlink created OK\n"));
1711 T(YAFFS_TRACE_OS, ("symlink not created\n"));
1717 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1724 obj = yaffs_DentryToObject(dentry);
1728 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_object\n"));
1729 yaffs_GrossLock(dev);
1730 yaffs_FlushFile(obj, 1, datasync);
1731 yaffs_GrossUnlock(dev);
1736 * The VFS layer already does all the dentry stuff for rename.
1738 * NB: POSIX says you can rename an object over an old object of the same name
1740 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1741 struct inode *new_dir, struct dentry *new_dentry)
1744 int retVal = YAFFS_FAIL;
1745 yaffs_Object *target;
1747 T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
1748 dev = yaffs_InodeToObject(old_dir)->myDev;
1750 yaffs_GrossLock(dev);
1752 /* Check if the target is an existing directory that is not empty. */
1753 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1754 new_dentry->d_name.name);
1758 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1759 !ylist_empty(&target->variant.directoryVariant.children)) {
1761 T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
1763 retVal = YAFFS_FAIL;
1765 /* Now does unlinking internally using shadowing mechanism */
1766 T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
1768 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1769 old_dentry->d_name.name,
1770 yaffs_InodeToObject(new_dir),
1771 new_dentry->d_name.name);
1773 yaffs_GrossUnlock(dev);
1775 if (retVal == YAFFS_OK) {
1777 new_dentry->d_inode->i_nlink--;
1778 mark_inode_dirty(new_dentry->d_inode);
1781 update_dir_time(old_dir);
1782 if(old_dir != new_dir)
1783 update_dir_time(new_dir);
1790 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1792 struct inode *inode = dentry->d_inode;
1797 ("yaffs_setattr of object %d\n",
1798 yaffs_InodeToObject(inode)->objectId));
1800 /* Fail if a requested resize >= 2GB */
1801 if (attr->ia_valid & ATTR_SIZE &&
1802 (attr->ia_size >> 31))
1806 error = inode_change_ok(inode, attr);
1810 error = inode_setattr(inode, attr);
1811 T(YAFFS_TRACE_OS,("inode_setattr called\n"));
1812 if (attr->ia_valid & ATTR_SIZE)
1813 truncate_inode_pages(&inode->i_data,attr->ia_size);
1815 dev = yaffs_InodeToObject(inode)->myDev;
1816 if (attr->ia_valid & ATTR_SIZE){
1817 T(YAFFS_TRACE_OS,("resize to %d(%x)\n",(int)(attr->ia_size),(int)(attr->ia_size)));
1819 yaffs_GrossLock(dev);
1820 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1821 if(result == YAFFS_OK) {
1826 yaffs_GrossUnlock(dev);
1831 ("yaffs_setattr done returning %d\n",error));
1836 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1837 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1839 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1840 struct super_block *sb = dentry->d_sb;
1841 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1842 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1844 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1846 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1848 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1851 T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
1853 yaffs_GrossLock(dev);
1855 buf->f_type = YAFFS_MAGIC;
1856 buf->f_bsize = sb->s_blocksize;
1857 buf->f_namelen = 255;
1859 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1860 /* Do this if chunk size is not a power of 2 */
1862 uint64_t bytesInDev;
1865 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
1866 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
1868 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1869 buf->f_blocks = bytesInDev;
1871 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1872 ((uint64_t)(dev->nDataBytesPerChunk));
1874 do_div(bytesFree, sb->s_blocksize);
1876 buf->f_bfree = bytesFree;
1878 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1881 (dev->param.endBlock - dev->param.startBlock + 1) *
1882 dev->param.nChunksPerBlock /
1883 (sb->s_blocksize / dev->nDataBytesPerChunk);
1885 yaffs_GetNumberOfFreeChunks(dev) /
1886 (sb->s_blocksize / dev->nDataBytesPerChunk);
1889 (dev->param.endBlock - dev->param.startBlock + 1) *
1890 dev->param.nChunksPerBlock *
1891 (dev->nDataBytesPerChunk / sb->s_blocksize);
1894 yaffs_GetNumberOfFreeChunks(dev) *
1895 (dev->nDataBytesPerChunk / sb->s_blocksize);
1900 buf->f_bavail = buf->f_bfree;
1902 yaffs_GrossUnlock(dev);
1908 static void yaffs_FlushInodes(struct super_block *sb)
1913 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1914 obj = yaffs_InodeToObject(iptr);
1916 T(YAFFS_TRACE_OS, ("flushing obj %d\n",obj->objectId));
1917 yaffs_FlushFile(obj,1,0);
1923 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
1925 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1929 yaffs_FlushInodes(sb);
1930 yaffs_UpdateDirtyDirectories(dev);
1931 yaffs_FlushEntireDeviceCache(dev);
1933 yaffs_CheckpointSave(dev);
1936 static int yaffs_do_sync_fs(struct super_block *sb, int do_checkpoint)
1939 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1940 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1941 ("yaffs_do_sync_fs: %s %s\n",
1942 sb->s_dirt ? "dirty" : "clean",
1943 do_checkpoint ? "with checkpoint" : "no checkpoint"));
1946 yaffs_GrossLock(dev);
1947 yaffs_FlushSuperBlock(sb,do_checkpoint);
1948 yaffs_GrossUnlock(dev);
1956 * yaffs background thread functions .
1957 * yaffs_BackgroundThread() the thread function
1958 * yaffs_BackgroundStart() launches the background thread.
1959 * yaffs_BackgroundStop() cleans up the background thread.
1962 * The thread should only run after the yaffs is initialised
1963 * The thread should be stopped before yaffs is unmounted.
1964 * The thread should not do any writing while the fs is in read only.
1967 #ifdef YAFFS_COMPILE_BACKGROUND
1968 static int yaffs_BackgroundThread(void *data)
1970 yaffs_Device *dev = (yaffs_Device *)data;
1971 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
1973 T(YAFFS_TRACE_BACKGROUND,
1974 ("yaffs_background starting for dev %p\n",
1977 while(context->bgRunning){
1978 T(YAFFS_TRACE_BACKGROUND,
1979 ("yaffs_background\n"));
1981 if(kthread_should_stop())
1983 yaffs_GrossLock(dev);
1984 yaffs_UpdateDirtyDirectories(dev);
1985 yaffs_GrossUnlock(dev);
1991 static int yaffs_BackgroundStart(yaffs_Device *dev)
1995 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
1997 context->bgRunning = 1;
1999 context->bgThread = kthread_run(yaffs_BackgroundThread,(void *)dev,"yaffs_%x",(unsigned)dev);
2001 if(IS_ERR(context->bgThread)){
2002 retval = PTR_ERR(context->bgThread);
2003 context->bgThread = NULL;
2004 context->bgRunning = 0;
2009 static void yaffs_BackgroundStop(yaffs_Device *dev)
2011 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToContext(dev);
2013 ctxt->bgRunning = 0;
2015 if( ctxt->bgThread){
2016 kthread_stop(ctxt->bgThread);
2017 ctxt->bgThread = NULL;
2021 static int yaffs_BackgroundThread(void *data)
2026 static int yaffs_BackgroundStart(yaffs_Device *dev)
2031 static void yaffs_BackgroundStop(yaffs_Device *dev)
2037 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2038 static void yaffs_write_super(struct super_block *sb)
2040 static int yaffs_write_super(struct super_block *sb)
2044 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_write_super\n"));
2045 yaffs_do_sync_fs(sb, yaffs_auto_checkpoint >= 2);
2047 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2053 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2054 static int yaffs_sync_fs(struct super_block *sb, int wait)
2056 static int yaffs_sync_fs(struct super_block *sb)
2059 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_fs\n"));
2061 yaffs_do_sync_fs(sb,yaffs_auto_checkpoint >= 1);
2066 #ifdef YAFFS_USE_OWN_IGET
2068 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2070 struct inode *inode;
2072 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2075 ("yaffs_iget for %lu\n", ino));
2077 inode = iget_locked(sb, ino);
2079 return ERR_PTR(-ENOMEM);
2080 if (!(inode->i_state & I_NEW))
2083 /* NB This is called as a side effect of other functions, but
2084 * we had to release the lock to prevent deadlocks, so
2085 * need to lock again.
2088 yaffs_GrossLock(dev);
2090 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2092 yaffs_FillInodeFromObject(inode, obj);
2094 yaffs_GrossUnlock(dev);
2096 unlock_new_inode(inode);
2102 static void yaffs_read_inode(struct inode *inode)
2104 /* NB This is called as a side effect of other functions, but
2105 * we had to release the lock to prevent deadlocks, so
2106 * need to lock again.
2110 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2113 ("yaffs_read_inode for %d\n", (int)inode->i_ino));
2115 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
2116 yaffs_GrossLock(dev);
2118 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2120 yaffs_FillInodeFromObject(inode, obj);
2122 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
2123 yaffs_GrossUnlock(dev);
2128 static YLIST_HEAD(yaffs_context_list);
2129 struct semaphore yaffs_context_lock;
2131 #if 0 /* not used */
2132 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2134 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2136 if (*flags & MS_RDONLY) {
2137 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2140 ("yaffs_remount_fs: %s: RO\n", dev->name));
2142 yaffs_GrossLock(dev);
2144 yaffs_FlushSuperBlock(sb,1);
2149 yaffs_GrossUnlock(dev);
2152 ("yaffs_remount_fs: %s: RW\n", dev->name));
2159 static void yaffs_put_super(struct super_block *sb)
2161 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2163 T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
2165 yaffs_GrossLock(dev);
2167 yaffs_FlushSuperBlock(sb,1);
2169 if (yaffs_DeviceToContext(dev)->putSuperFunc)
2170 yaffs_DeviceToContext(dev)->putSuperFunc(sb);
2172 yaffs_BackgroundStop(dev);
2174 yaffs_Deinitialise(dev);
2176 yaffs_GrossUnlock(dev);
2178 down(&yaffs_context_lock);
2179 ylist_del_init(&(yaffs_DeviceToContext(dev)->contextList));
2180 up(&yaffs_context_lock);
2182 if (yaffs_DeviceToContext(dev)->spareBuffer) {
2183 YFREE(yaffs_DeviceToContext(dev)->spareBuffer);
2184 yaffs_DeviceToContext(dev)->spareBuffer = NULL;
2191 static void yaffs_MTDPutSuper(struct super_block *sb)
2193 struct mtd_info *mtd = yaffs_DeviceToContext(yaffs_SuperToDevice(sb))->mtd;
2198 put_mtd_device(mtd);
2202 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2204 struct super_block *sb = yaffs_DeviceToContext(dev)->superBlock;
2206 T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
2213 int skip_checkpoint_read;
2214 int skip_checkpoint_write;
2217 int tags_ecc_overridden;
2218 int lazy_loading_enabled;
2219 int lazy_loading_overridden;
2220 int empty_lost_and_found;
2221 int empty_lost_and_found_overridden;
2224 #define MAX_OPT_LEN 30
2225 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2227 char cur_opt[MAX_OPT_LEN + 1];
2231 /* Parse through the options which is a comma seperated list */
2233 while (options_str && *options_str && !error) {
2234 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2237 while(*options_str == ',')
2240 while (*options_str && *options_str != ',') {
2241 if (p < MAX_OPT_LEN) {
2242 cur_opt[p] = *options_str;
2248 if (!strcmp(cur_opt, "inband-tags"))
2249 options->inband_tags = 1;
2250 else if (!strcmp(cur_opt, "tags-ecc-off")){
2251 options->tags_ecc_on = 0;
2252 options->tags_ecc_overridden=1;
2253 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2254 options->tags_ecc_on = 1;
2255 options->tags_ecc_overridden = 1;
2256 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2257 options->lazy_loading_enabled = 0;
2258 options->lazy_loading_overridden=1;
2259 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2260 options->lazy_loading_enabled = 1;
2261 options->lazy_loading_overridden = 1;
2262 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2263 options->empty_lost_and_found = 0;
2264 options->empty_lost_and_found_overridden=1;
2265 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2266 options->empty_lost_and_found = 1;
2267 options->empty_lost_and_found_overridden=1;
2268 } else if (!strcmp(cur_opt, "no-cache"))
2269 options->no_cache = 1;
2270 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2271 options->skip_checkpoint_read = 1;
2272 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2273 options->skip_checkpoint_write = 1;
2274 else if (!strcmp(cur_opt, "no-checkpoint")) {
2275 options->skip_checkpoint_read = 1;
2276 options->skip_checkpoint_write = 1;
2278 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2287 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2288 struct super_block *sb,
2289 void *data, int silent)
2292 struct inode *inode = NULL;
2293 struct dentry *root;
2294 yaffs_Device *dev = 0;
2295 char devname_buf[BDEVNAME_SIZE + 1];
2296 struct mtd_info *mtd;
2298 char *data_str = (char *)data;
2299 struct yaffs_LinuxContext *context = NULL;
2300 yaffs_DeviceParam *param;
2302 yaffs_options options;
2304 sb->s_magic = YAFFS_MAGIC;
2305 sb->s_op = &yaffs_super_ops;
2306 sb->s_flags |= MS_NOATIME;
2308 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,9))
2309 sb->s_export_op = &yaffs_export_ops;
2313 printk(KERN_INFO "yaffs: sb is NULL\n");
2314 else if (!sb->s_dev)
2315 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2316 else if (!yaffs_devname(sb, devname_buf))
2317 printk(KERN_INFO "yaffs: devname is NULL\n");
2319 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2321 yaffs_devname(sb, devname_buf));
2326 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2328 memset(&options, 0, sizeof(options));
2330 if (yaffs_parse_options(&options, data_str)) {
2331 /* Option parsing failed */
2336 sb->s_blocksize = PAGE_CACHE_SIZE;
2337 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2338 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2340 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2342 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2344 ("yaffs: Write verification disabled. All guarantees "
2345 "null and void\n"));
2348 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2350 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2351 yaffs_devname(sb, devname_buf)));
2353 /* Check it's an mtd device..... */
2354 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2355 return NULL; /* This isn't an mtd device */
2357 /* Get the device */
2358 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2360 T(YAFFS_TRACE_ALWAYS,
2361 ("yaffs: MTD device #%u doesn't appear to exist\n",
2365 /* Check it's NAND */
2366 if (mtd->type != MTD_NANDFLASH) {
2367 T(YAFFS_TRACE_ALWAYS,
2368 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
2372 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
2373 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
2374 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
2375 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
2376 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
2377 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
2378 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
2379 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2380 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
2381 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
2382 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2383 T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
2385 T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
2388 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2390 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2391 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
2395 /* Added NCB 26/5/2006 for completeness */
2396 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2397 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
2403 if (yaffsVersion == 2) {
2404 /* Check for version 2 style functions */
2406 !mtd->block_isbad ||
2407 !mtd->block_markbad ||
2410 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2411 !mtd->read_oob || !mtd->write_oob) {
2414 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2416 T(YAFFS_TRACE_ALWAYS,
2417 ("yaffs: MTD device does not support required "
2422 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2423 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2424 !options.inband_tags) {
2425 T(YAFFS_TRACE_ALWAYS,
2426 ("yaffs: MTD device does not have the "
2427 "right page sizes\n"));
2431 /* Check for V1 style functions */
2435 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2436 !mtd->read_oob || !mtd->write_oob) {
2439 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2441 T(YAFFS_TRACE_ALWAYS,
2442 ("yaffs: MTD device does not support required "
2447 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2448 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2449 T(YAFFS_TRACE_ALWAYS,
2450 ("yaffs: MTD device does not support have the "
2451 "right page sizes\n"));
2456 /* OK, so if we got here, we have an MTD that's NAND and looks
2457 * like it has the right capabilities
2458 * Set the yaffs_Device up for mtd
2461 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2462 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2464 if(!dev || !context ){
2474 /* Deep shit could not allocate device structure */
2475 T(YAFFS_TRACE_ALWAYS,
2476 ("yaffs_read_super: Failed trying to allocate "
2477 "yaffs_Device. \n"));
2480 memset(dev, 0, sizeof(yaffs_Device));
2481 param = &(dev->param);
2483 memset(context,0,sizeof(struct yaffs_LinuxContext));
2484 dev->context = context;
2485 YINIT_LIST_HEAD(&(context->contextList));
2487 context->superBlock = sb;
2491 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2492 sb->s_fs_info = dev;
2494 sb->u.generic_sbp = dev;
2497 yaffs_DeviceToContext(dev)->mtd = mtd;
2498 param->name = mtd->name;
2500 /* Set up the memory size parameters.... */
2502 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2504 param->startBlock = 0;
2505 param->endBlock = nBlocks - 1;
2506 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2507 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2508 param->nReservedBlocks = 5;
2509 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2510 param->inbandTags = options.inband_tags;
2512 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2513 param->disableLazyLoad = 1;
2515 if(options.lazy_loading_overridden)
2516 param->disableLazyLoad = !options.lazy_loading_enabled;
2518 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2519 param->noTagsECC = 1;
2522 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2524 param->deferDirectoryUpdate = 1;
2527 if(options.tags_ecc_overridden)
2528 param->noTagsECC = !options.tags_ecc_on;
2530 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2531 param->emptyLostAndFound = 1;
2534 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2535 param->refreshPeriod = 0;
2537 param->refreshPeriod = 10000;
2540 if(options.empty_lost_and_found_overridden)
2541 param->emptyLostAndFound = options.empty_lost_and_found;
2543 /* ... and the functions. */
2544 if (yaffsVersion == 2) {
2545 param->writeChunkWithTagsToNAND =
2546 nandmtd2_WriteChunkWithTagsToNAND;
2547 param->readChunkWithTagsFromNAND =
2548 nandmtd2_ReadChunkWithTagsFromNAND;
2549 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2550 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2551 yaffs_DeviceToContext(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2552 param->isYaffs2 = 1;
2553 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2554 param->totalBytesPerChunk = mtd->writesize;
2555 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2557 param->totalBytesPerChunk = mtd->oobblock;
2558 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2560 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2562 param->startBlock = 0;
2563 param->endBlock = nBlocks - 1;
2565 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2566 /* use the MTD interface in yaffs_mtdif1.c */
2567 param->writeChunkWithTagsToNAND =
2568 nandmtd1_WriteChunkWithTagsToNAND;
2569 param->readChunkWithTagsFromNAND =
2570 nandmtd1_ReadChunkWithTagsFromNAND;
2571 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2572 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2574 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2575 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2577 param->isYaffs2 = 0;
2579 /* ... and common functions */
2580 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2581 param->initialiseNAND = nandmtd_InitialiseNAND;
2583 yaffs_DeviceToContext(dev)->putSuperFunc = yaffs_MTDPutSuper;
2585 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2586 param->gcControl = yaffs_gc_control_callback;
2588 yaffs_DeviceToContext(dev)->superBlock= sb;
2591 #ifndef CONFIG_YAFFS_DOES_ECC
2592 param->useNANDECC = 1;
2595 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2596 param->wideTnodesDisabled = 1;
2599 param->skipCheckpointRead = options.skip_checkpoint_read;
2600 param->skipCheckpointWrite = options.skip_checkpoint_write;
2602 /* we assume this is protected by lock_kernel() in mount/umount */
2603 down(&yaffs_context_lock);
2604 ylist_add_tail(&(yaffs_DeviceToContext(dev)->contextList), &yaffs_context_list);
2605 up(&yaffs_context_lock);
2607 /* Directory search handling...*/
2608 YINIT_LIST_HEAD(&(yaffs_DeviceToContext(dev)->searchContexts));
2609 param->removeObjectCallback = yaffs_RemoveObjectCallback;
2611 init_MUTEX(&(yaffs_DeviceToContext(dev)->grossLock));
2613 yaffs_GrossLock(dev);
2615 err = yaffs_GutsInitialise(dev);
2618 ("yaffs_read_super: guts initialised %s\n",
2619 (err == YAFFS_OK) ? "OK" : "FAILED"));
2622 yaffs_BackgroundStart(dev);
2624 if(!context->bgThread)
2625 param->deferDirectoryUpdate = 0;
2628 /* Release lock before yaffs_get_inode() */
2629 yaffs_GrossUnlock(dev);
2631 /* Create root inode */
2632 if (err == YAFFS_OK)
2633 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2639 inode->i_op = &yaffs_dir_inode_operations;
2640 inode->i_fop = &yaffs_dir_operations;
2642 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
2644 root = d_alloc_root(inode);
2646 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
2653 sb->s_dirt = !dev->isCheckpointed;
2654 T(YAFFS_TRACE_ALWAYS,
2655 ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
2657 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
2662 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2663 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2666 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2669 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2670 static int yaffs_read_super(struct file_system_type *fs,
2671 int flags, const char *dev_name,
2672 void *data, struct vfsmount *mnt)
2675 return get_sb_bdev(fs, flags, dev_name, data,
2676 yaffs_internal_read_super_mtd, mnt);
2679 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2680 int flags, const char *dev_name,
2684 return get_sb_bdev(fs, flags, dev_name, data,
2685 yaffs_internal_read_super_mtd);
2689 static struct file_system_type yaffs_fs_type = {
2690 .owner = THIS_MODULE,
2692 .get_sb = yaffs_read_super,
2693 .kill_sb = kill_block_super,
2694 .fs_flags = FS_REQUIRES_DEV,
2697 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2700 return yaffs_internal_read_super(1, sb, data, silent);
2703 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2708 #ifdef CONFIG_YAFFS_YAFFS2
2710 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2711 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2714 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2717 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2718 static int yaffs2_read_super(struct file_system_type *fs,
2719 int flags, const char *dev_name, void *data,
2720 struct vfsmount *mnt)
2722 return get_sb_bdev(fs, flags, dev_name, data,
2723 yaffs2_internal_read_super_mtd, mnt);
2726 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2727 int flags, const char *dev_name,
2731 return get_sb_bdev(fs, flags, dev_name, data,
2732 yaffs2_internal_read_super_mtd);
2736 static struct file_system_type yaffs2_fs_type = {
2737 .owner = THIS_MODULE,
2739 .get_sb = yaffs2_read_super,
2740 .kill_sb = kill_block_super,
2741 .fs_flags = FS_REQUIRES_DEV,
2744 static struct super_block *yaffs2_read_super(struct super_block *sb,
2745 void *data, int silent)
2747 return yaffs_internal_read_super(2, sb, data, silent);
2750 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2754 #endif /* CONFIG_YAFFS_YAFFS2 */
2756 static struct proc_dir_entry *my_proc_entry;
2757 static struct proc_dir_entry *debug_proc_entry;
2759 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
2761 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
2762 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
2763 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
2764 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
2765 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
2766 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
2767 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
2768 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
2769 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
2770 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
2771 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
2772 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
2774 buf += sprintf(buf, "\n");
2780 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
2782 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2783 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2784 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2785 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2786 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2787 buf += sprintf(buf, "\n");
2788 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2789 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2790 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2791 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2792 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2793 buf += sprintf(buf, "\n");
2794 buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
2795 buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
2796 buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
2797 buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
2798 buf += sprintf(buf, "garbageCollections. %u\n", dev->garbageCollections);
2799 buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGarbageCollections);
2800 buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
2801 buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
2802 buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
2803 buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
2804 buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
2805 buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
2806 buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
2807 buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
2808 buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
2809 buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
2811 sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
2816 static int yaffs_proc_read(char *page,
2818 off_t offset, int count, int *eof, void *data)
2820 struct ylist_head *item;
2825 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2826 * We use 'offset' (*ppos) to indicate where we are in devList.
2827 * This also assumes the user has posted a read buffer large
2828 * enough to hold the complete output; but that's life in /proc.
2833 /* Print header first */
2835 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2836 "\n%s\n%s\n", yaffs_fs_c_version,
2837 yaffs_guts_c_version);
2839 buf += sprintf(buf,"\n");
2843 down(&yaffs_context_lock);
2845 /* Locate and print the Nth entry. Order N-squared but N is small. */
2846 ylist_for_each(item, &yaffs_context_list) {
2847 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2848 yaffs_Device *dev = dc->dev;
2850 if (n < (step & ~1)) {
2855 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
2856 buf = yaffs_dump_dev_part0(buf, dev);
2858 buf = yaffs_dump_dev_part1(buf, dev);
2862 up(&yaffs_context_lock);
2865 return buf - page < count ? buf - page : count;
2868 static int yaffs_debug_proc_read(char *page,
2870 off_t offset, int count, int *eof, void *data)
2872 struct ylist_head *item;
2876 down(&yaffs_context_lock);
2878 /* Locate and print the Nth entry. Order N-squared but N is small. */
2879 ylist_for_each(item, &yaffs_context_list) {
2880 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2881 yaffs_Device *dev = dc->dev;
2887 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2888 nObjects = dev->nObjectsCreated -dev->nFreeObjects;
2889 nTnodes = dev->nTnodesCreated - dev->nFreeTnodes;
2892 buf += sprintf(buf,"%d, %d, %d, %d, %d\n",
2893 n, dev->nFreeChunks, erasedChunks,
2896 up(&yaffs_context_lock);
2899 return buf - page < count ? buf - page : count;
2903 * Set the verbosity of the warnings and error messages.
2905 * Note that the names can only be a..z or _ with the current code.
2910 unsigned mask_bitfield;
2912 {"allocate", YAFFS_TRACE_ALLOCATE},
2913 {"always", YAFFS_TRACE_ALWAYS},
2914 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2915 {"buffers", YAFFS_TRACE_BUFFERS},
2916 {"bug", YAFFS_TRACE_BUG},
2917 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2918 {"deletion", YAFFS_TRACE_DELETION},
2919 {"erase", YAFFS_TRACE_ERASE},
2920 {"error", YAFFS_TRACE_ERROR},
2921 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2922 {"gc", YAFFS_TRACE_GC},
2923 {"mtd", YAFFS_TRACE_MTD},
2924 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2925 {"os", YAFFS_TRACE_OS},
2926 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2927 {"scan", YAFFS_TRACE_SCAN},
2928 {"tracing", YAFFS_TRACE_TRACING},
2929 {"sync", YAFFS_TRACE_SYNC},
2931 {"background", YAFFS_TRACE_BACKGROUND},
2933 {"verify", YAFFS_TRACE_VERIFY},
2934 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2935 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2936 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2938 {"write", YAFFS_TRACE_WRITE},
2939 {"all", 0xffffffff},
2944 #define MAX_MASK_NAME_LENGTH 40
2945 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2946 unsigned long count, void *data)
2948 unsigned rg = 0, mask_bitfield;
2952 char substring[MAX_MASK_NAME_LENGTH + 1];
2958 rg = yaffs_traceMask;
2960 while (!done && (pos < count)) {
2962 while ((pos < count) && isspace(buf[pos]))
2979 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2981 if (end > buf + pos) {
2982 mask_name = "numeral";
2983 len = end - (buf + pos);
2987 for (x = buf + pos, i = 0;
2988 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2989 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2991 substring[i] = '\0';
2993 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2994 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
2995 mask_name = mask_flags[i].mask_name;
2996 mask_bitfield = mask_flags[i].mask_bitfield;
3003 if (mask_name != NULL) {
3007 rg &= ~mask_bitfield;
3010 rg |= mask_bitfield;
3016 rg |= mask_bitfield;
3022 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3024 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
3026 if (rg & YAFFS_TRACE_ALWAYS) {
3027 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3029 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3030 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3038 static int yaffs_proc_write(struct file *file, const char *buf,
3039 unsigned long count, void *data)
3041 return yaffs_proc_write_trace_options(file, buf, count, data);
3044 /* Stuff to handle installation of file systems */
3045 struct file_system_to_install {
3046 struct file_system_type *fst;
3050 static struct file_system_to_install fs_to_install[] = {
3051 {&yaffs_fs_type, 0},
3052 {&yaffs2_fs_type, 0},
3056 static int __init init_yaffs_fs(void)
3059 struct file_system_to_install *fsinst;
3061 T(YAFFS_TRACE_ALWAYS,
3062 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3064 init_MUTEX(&yaffs_context_lock);
3066 /* Install the proc_fs entries */
3067 my_proc_entry = create_proc_entry("yaffs",
3071 if (my_proc_entry) {
3072 my_proc_entry->write_proc = yaffs_proc_write;
3073 my_proc_entry->read_proc = yaffs_proc_read;
3074 my_proc_entry->data = NULL;
3078 debug_proc_entry = create_proc_entry("yaffs_debug",
3082 if (debug_proc_entry) {
3083 debug_proc_entry->write_proc = NULL;
3084 debug_proc_entry->read_proc = yaffs_debug_proc_read;
3085 debug_proc_entry->data = NULL;
3089 /* Now add the file system entries */
3091 fsinst = fs_to_install;
3093 while (fsinst->fst && !error) {
3094 error = register_filesystem(fsinst->fst);
3096 fsinst->installed = 1;
3100 /* Any errors? uninstall */
3102 fsinst = fs_to_install;
3104 while (fsinst->fst) {
3105 if (fsinst->installed) {
3106 unregister_filesystem(fsinst->fst);
3107 fsinst->installed = 0;
3116 static void __exit exit_yaffs_fs(void)
3119 struct file_system_to_install *fsinst;
3121 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3124 remove_proc_entry("yaffs", YPROC_ROOT);
3126 fsinst = fs_to_install;
3128 while (fsinst->fst) {
3129 if (fsinst->installed) {
3130 unregister_filesystem(fsinst->fst);
3131 fsinst->installed = 0;
3137 module_init(init_yaffs_fs)
3138 module_exit(exit_yaffs_fs)
3140 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3141 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
3142 MODULE_LICENSE("GPL");