2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 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 #include <linux/version.h>
36 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
37 #define YAFFS_COMPILE_BACKGROUND
40 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28))
41 #define YAFFS_COMPILE_EXPORTFS
44 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
45 #define YAFFS_NEW_FOLLOW_LINK 1
47 #define YAFFS_NEW_FOLLOW_LINK 0
50 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
51 #include <linux/config.h>
54 #include <linux/kernel.h>
55 #include <linux/module.h>
56 #include <linux/slab.h>
57 #include <linux/init.h>
59 #include <linux/proc_fs.h>
60 #include <linux/smp_lock.h>
61 #include <linux/pagemap.h>
62 #include <linux/mtd/mtd.h>
63 #include <linux/interrupt.h>
64 #include <linux/string.h>
65 #include <linux/ctype.h>
67 #if (YAFFS_NEW_FOLLOW_LINK == 1)
68 #include <linux/namei.h>
71 #ifdef YAFFS_COMPILE_EXPORTFS
72 #include <linux/exportfs.h>
75 #ifdef YAFFS_COMPILE_BACKGROUND
76 #include <linux/kthread.h>
77 #include <linux/delay.h>
78 #include <linux/freezer.h>
81 #include <asm/div64.h>
83 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
85 #include <linux/statfs.h>
87 #define UnlockPage(p) unlock_page(p)
88 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
90 /* FIXME: use sb->s_id instead ? */
91 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
95 #include <linux/locks.h>
96 #define BDEVNAME_SIZE 0
97 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
99 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
100 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
106 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
107 #define YPROC_ROOT (&proc_root)
109 #define YPROC_ROOT NULL
112 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
113 #define WRITE_SIZE_STR "writesize"
114 #define WRITE_SIZE(mtd) ((mtd)->writesize)
116 #define WRITE_SIZE_STR "oobblock"
117 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
120 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
121 #define YAFFS_USE_WRITE_BEGIN_END 1
123 #define YAFFS_USE_WRITE_BEGIN_END 0
126 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
127 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
129 uint64_t result = partition_size;
130 do_div(result, block_size);
131 return (uint32_t)result;
134 #define YCALCBLOCKS(s, b) ((s)/(b))
137 #include <linux/uaccess.h>
138 #include <linux/mtd/mtd.h>
140 #include "yportenv.h"
141 #include "yaffs_trace.h"
142 #include "yaffs_guts.h"
144 #include "yaffs_linux.h"
146 #include "yaffs_mtdif.h"
147 #include "yaffs_mtdif1.h"
148 #include "yaffs_mtdif2.h"
150 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
151 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
152 unsigned int yaffs_auto_checkpoint = 1;
153 unsigned int yaffs_gc_control = 1;
155 /* Module Parameters */
156 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
157 module_param(yaffs_traceMask, uint, 0644);
158 module_param(yaffs_wr_attempts, uint, 0644);
159 module_param(yaffs_auto_checkpoint, uint, 0644);
160 module_param(yaffs_gc_control, uint, 0644);
162 MODULE_PARM(yaffs_traceMask, "i");
163 MODULE_PARM(yaffs_wr_attempts, "i");
164 MODULE_PARM(yaffs_auto_checkpoint, "i");
165 MODULE_PARM(yaffs_gc_control, "i");
168 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
169 /* use iget and read_inode */
170 #define Y_IGET(sb, inum) iget((sb), (inum))
171 static void yaffs_read_inode(struct inode *inode);
174 /* Call local equivalent */
175 #define YAFFS_USE_OWN_IGET
176 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
178 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
181 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
182 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
184 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
187 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
188 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
190 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
191 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
193 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
197 #define update_dir_time(dir) do {\
198 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
201 static void yaffs_put_super(struct super_block *sb);
203 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
205 static ssize_t yaffs_hold_space(struct file *f);
206 static void yaffs_release_space(struct file *f);
208 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
209 static int yaffs_file_flush(struct file *file, fl_owner_t id);
211 static int yaffs_file_flush(struct file *file);
214 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
215 static int yaffs_sync_object(struct file *file, int datasync);
217 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
221 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
223 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
224 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
225 struct nameidata *n);
226 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
227 struct nameidata *n);
229 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
230 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
232 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
233 struct dentry *dentry);
234 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
235 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
236 const char *symname);
237 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
239 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
240 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
243 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
246 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
247 struct inode *new_dir, struct dentry *new_dentry);
248 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
250 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
251 static int yaffs_sync_fs(struct super_block *sb, int wait);
252 static void yaffs_write_super(struct super_block *sb);
254 static int yaffs_sync_fs(struct super_block *sb);
255 static int yaffs_write_super(struct super_block *sb);
258 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
259 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
260 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
261 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
263 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
266 #ifdef YAFFS_HAS_PUT_INODE
267 static void yaffs_put_inode(struct inode *inode);
270 static void yaffs_delete_inode(struct inode *);
271 static void yaffs_clear_inode(struct inode *);
273 static int yaffs_readpage(struct file *file, struct page *page);
274 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
275 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
277 static int yaffs_writepage(struct page *page);
280 #ifdef CONFIG_YAFFS_XATTR
281 int yaffs_setxattr(struct dentry *dentry, const char *name,
282 const void *value, size_t size, int flags);
283 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
285 int yaffs_removexattr(struct dentry *dentry, const char *name);
286 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
290 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
291 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
292 loff_t pos, unsigned len, unsigned flags,
293 struct page **pagep, void **fsdata);
294 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
295 loff_t pos, unsigned len, unsigned copied,
296 struct page *pg, void *fsdadata);
298 static int yaffs_prepare_write(struct file *f, struct page *pg,
299 unsigned offset, unsigned to);
300 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
305 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
307 #if (YAFFS_NEW_FOLLOW_LINK == 1)
308 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
309 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
311 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
314 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev);
316 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
318 static struct address_space_operations yaffs_file_address_operations = {
319 .readpage = yaffs_readpage,
320 .writepage = yaffs_writepage,
321 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
322 .write_begin = yaffs_write_begin,
323 .write_end = yaffs_write_end,
325 .prepare_write = yaffs_prepare_write,
326 .commit_write = yaffs_commit_write,
330 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
331 static const struct file_operations yaffs_file_operations = {
332 .read = do_sync_read,
333 .write = do_sync_write,
334 .aio_read = generic_file_aio_read,
335 .aio_write = generic_file_aio_write,
336 .mmap = generic_file_mmap,
337 .flush = yaffs_file_flush,
338 .fsync = yaffs_sync_object,
339 .splice_read = generic_file_splice_read,
340 .splice_write = generic_file_splice_write,
341 .llseek = generic_file_llseek,
344 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
346 static const struct file_operations yaffs_file_operations = {
347 .read = do_sync_read,
348 .write = do_sync_write,
349 .aio_read = generic_file_aio_read,
350 .aio_write = generic_file_aio_write,
351 .mmap = generic_file_mmap,
352 .flush = yaffs_file_flush,
353 .fsync = yaffs_sync_object,
354 .sendfile = generic_file_sendfile,
359 static const struct file_operations yaffs_file_operations = {
360 .read = generic_file_read,
361 .write = generic_file_write,
362 .mmap = generic_file_mmap,
363 .flush = yaffs_file_flush,
364 .fsync = yaffs_sync_object,
365 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
366 .sendfile = generic_file_sendfile,
371 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
372 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
374 void * kaddr = kmap_atomic(page, KM_USER0);
375 memset(kaddr + start, 0, end - start);
376 kunmap_atomic(kaddr, KM_USER0);
377 flush_dcache_page(page);
382 static const struct inode_operations yaffs_file_inode_operations = {
383 .setattr = yaffs_setattr,
384 #ifdef CONFIG_YAFFS_XATTR
385 .setxattr = yaffs_setxattr,
386 .getxattr = yaffs_getxattr,
387 .listxattr = yaffs_listxattr,
388 .removexattr = yaffs_removexattr,
392 static const struct inode_operations yaffs_symlink_inode_operations = {
393 .readlink = yaffs_readlink,
394 .follow_link = yaffs_follow_link,
395 #if (YAFFS_NEW_FOLLOW_LINK == 1)
396 .put_link = yaffs_put_link,
398 .setattr = yaffs_setattr,
399 #ifdef CONFIG_YAFFS_XATTR
400 .setxattr = yaffs_setxattr,
401 .getxattr = yaffs_getxattr,
402 .listxattr = yaffs_listxattr,
403 .removexattr = yaffs_removexattr,
407 static const struct inode_operations yaffs_dir_inode_operations = {
408 .create = yaffs_create,
409 .lookup = yaffs_lookup,
411 .unlink = yaffs_unlink,
412 .symlink = yaffs_symlink,
413 .mkdir = yaffs_mkdir,
414 .rmdir = yaffs_unlink,
415 .mknod = yaffs_mknod,
416 .rename = yaffs_rename,
417 .setattr = yaffs_setattr,
418 #ifdef CONFIG_YAFFS_XATTR
419 .setxattr = yaffs_setxattr,
420 .getxattr = yaffs_getxattr,
421 .listxattr = yaffs_listxattr,
422 .removexattr = yaffs_removexattr,
426 static const struct file_operations yaffs_dir_operations = {
427 .read = generic_read_dir,
428 .readdir = yaffs_readdir,
429 .fsync = yaffs_sync_object,
430 .llseek = yaffs_dir_llseek,
433 static const struct super_operations yaffs_super_ops = {
434 .statfs = yaffs_statfs,
436 #ifndef YAFFS_USE_OWN_IGET
437 .read_inode = yaffs_read_inode,
439 #ifdef YAFFS_HAS_PUT_INODE
440 .put_inode = yaffs_put_inode,
442 .put_super = yaffs_put_super,
443 .delete_inode = yaffs_delete_inode,
444 .clear_inode = yaffs_clear_inode,
445 .sync_fs = yaffs_sync_fs,
446 .write_super = yaffs_write_super,
449 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
451 return yaffs_gc_control;
454 static void yaffs_GrossLock(yaffs_Device *dev)
456 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
457 down(&(yaffs_DeviceToLC(dev)->grossLock));
458 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
461 static void yaffs_GrossUnlock(yaffs_Device *dev)
463 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
464 up(&(yaffs_DeviceToLC(dev)->grossLock));
467 #ifdef YAFFS_COMPILE_EXPORTFS
469 static struct inode *
470 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
472 return Y_IGET(sb, ino);
475 static struct dentry *
476 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
478 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
481 static struct dentry *
482 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
484 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
487 struct dentry *yaffs2_get_parent(struct dentry *dentry)
490 struct super_block *sb = dentry->d_inode->i_sb;
491 struct dentry *parent = ERR_PTR(-ENOENT);
493 unsigned long parent_ino;
495 yaffs_Object *parent_obj;
497 d_obj = yaffs_InodeToObject(dentry->d_inode);
500 parent_obj = d_obj->parent;
502 parent_ino = yaffs_GetObjectInode(parent_obj);
503 inode = Y_IGET(sb, parent_ino);
506 parent = ERR_CAST(inode);
508 parent = d_obtain_alias(inode);
509 if (!IS_ERR(parent)) {
510 parent = ERR_PTR(-ENOMEM);
520 /* Just declare a zero structure as a NULL value implies
521 * using the default functions of exportfs.
524 static struct export_operations yaffs_export_ops =
526 .fh_to_dentry = yaffs2_fh_to_dentry,
527 .fh_to_parent = yaffs2_fh_to_parent,
528 .get_parent = yaffs2_get_parent,
533 /*-----------------------------------------------------------------*/
534 /* Directory search context allows us to unlock access to yaffs during
535 * filldir without causing problems with the directory being modified.
536 * This is similar to the tried and tested mechanism used in yaffs direct.
538 * A search context iterates along a doubly linked list of siblings in the
539 * directory. If the iterating object is deleted then this would corrupt
540 * the list iteration, likely causing a crash. The search context avoids
541 * this by using the removeObjectCallback to move the search context to the
542 * next object before the object is deleted.
544 * Many readdirs (and thus seach conexts) may be alive simulateously so
545 * each yaffs_Device has a list of these.
547 * A seach context lives for the duration of a readdir.
549 * All these functions must be called while yaffs is locked.
552 struct yaffs_SearchContext {
554 yaffs_Object *dirObj;
555 yaffs_Object *nextReturn;
556 struct ylist_head others;
560 * yaffs_NewSearch() creates a new search context, initialises it and
561 * adds it to the device's search context list.
563 * Called at start of readdir.
565 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
567 yaffs_Device *dev = dir->myDev;
568 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
572 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
573 sc->nextReturn = NULL;
575 sc->nextReturn = ylist_entry(
576 dir->variant.directoryVariant.children.next,
577 yaffs_Object,siblings);
578 YINIT_LIST_HEAD(&sc->others);
579 ylist_add(&sc->others,&(yaffs_DeviceToLC(dev)->searchContexts));
585 * yaffs_EndSearch() disposes of a search context and cleans up.
587 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
590 ylist_del(&sc->others);
596 * yaffs_SearchAdvance() moves a search context to the next object.
597 * Called when the search iterates or when an object removal causes
598 * the search context to be moved to the next object.
600 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
605 if( sc->nextReturn == NULL ||
606 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
607 sc->nextReturn = NULL;
609 struct ylist_head *next = sc->nextReturn->siblings.next;
611 if( next == &sc->dirObj->variant.directoryVariant.children)
612 sc->nextReturn = NULL; /* end of list */
614 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
619 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
620 * We check open search contexts and advance any which are currently
621 * on the object being iterated.
623 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
626 struct ylist_head *i;
627 struct yaffs_SearchContext *sc;
628 struct ylist_head *search_contexts = &(yaffs_DeviceToLC(obj->myDev)->searchContexts);
631 /* Iterate through the directory search contexts.
632 * If any are currently on the object being removed, then advance
633 * the search context to the next object to prevent a hanging pointer.
635 ylist_for_each(i, search_contexts) {
637 sc = ylist_entry(i, struct yaffs_SearchContext,others);
638 if(sc->nextReturn == obj)
639 yaffs_SearchAdvance(sc);
646 /*-----------------------------------------------------------------*/
648 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
651 unsigned char *alias;
654 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
656 yaffs_GrossLock(dev);
658 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
660 yaffs_GrossUnlock(dev);
665 ret = vfs_readlink(dentry, buffer, buflen, alias);
670 #if (YAFFS_NEW_FOLLOW_LINK == 1)
671 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
673 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
676 unsigned char *alias;
678 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
680 yaffs_GrossLock(dev);
682 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
683 yaffs_GrossUnlock(dev);
690 #if (YAFFS_NEW_FOLLOW_LINK == 1)
691 nd_set_link(nd, alias);
696 ret = vfs_follow_link(nd, alias);
703 #if (YAFFS_NEW_FOLLOW_LINK == 1)
704 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) {
709 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
713 * Lookup is used to find objects in the fs
715 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
717 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
720 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
724 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
726 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
728 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
729 yaffs_GrossLock(dev);
732 (TSTR("yaffs_lookup for %d:%s\n"),
733 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
735 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
736 dentry->d_name.name);
738 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
740 /* Can't hold gross lock when calling yaffs_get_inode() */
741 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
742 yaffs_GrossUnlock(dev);
746 (TSTR("yaffs_lookup found %d\n"), obj->objectId));
748 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
752 (TSTR("yaffs_loookup dentry \n")));
753 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
754 * d_add even if NULL inode */
756 /*dget(dentry); // try to solve directory bug */
757 d_add(dentry, inode);
765 T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
769 /* added NCB for 2.5/6 compatability - forces add even if inode is
770 * NULL which creates dentry hash */
771 d_add(dentry, inode);
777 #ifdef YAFFS_HAS_PUT_INODE
779 /* For now put inode is just for debugging
780 * Put inode is called when the inode **structure** is put.
782 static void yaffs_put_inode(struct inode *inode)
785 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
786 atomic_read(&inode->i_count)));
791 /* clear is called to tell the fs to release any per-inode data it holds */
792 static void yaffs_clear_inode(struct inode *inode)
797 obj = yaffs_InodeToObject(inode);
800 (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
801 atomic_read(&inode->i_count),
802 obj ? "object exists" : "null object"));
806 yaffs_GrossLock(dev);
808 /* Clear the association between the inode and
812 yaffs_InodeToObjectLV(inode) = NULL;
814 /* If the object freeing was deferred, then the real
816 * This should fix the inode inconsistency problem.
819 yaffs_HandleDeferedFree(obj);
821 yaffs_GrossUnlock(dev);
826 /* delete is called when the link count is zero and the inode
827 * is put (ie. nobody wants to know about it anymore, time to
829 * NB Must call clear_inode()
831 static void yaffs_delete_inode(struct inode *inode)
833 yaffs_Object *obj = yaffs_InodeToObject(inode);
837 (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
838 atomic_read(&inode->i_count),
839 obj ? "object exists" : "null object"));
843 yaffs_GrossLock(dev);
844 yaffs_DeleteObject(obj);
845 yaffs_GrossUnlock(dev);
847 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
848 truncate_inode_pages(&inode->i_data, 0);
853 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
854 static int yaffs_file_flush(struct file *file, fl_owner_t id)
856 static int yaffs_file_flush(struct file *file)
859 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
861 yaffs_Device *dev = obj->myDev;
864 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->objectId,
865 obj->dirty ? "dirty" : "clean"));
867 yaffs_GrossLock(dev);
869 yaffs_FlushFile(obj, 1, 0);
871 yaffs_GrossUnlock(dev);
876 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
878 /* Lifted from jffs2 */
881 unsigned char *pg_buf;
887 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
888 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
889 (unsigned)PAGE_CACHE_SIZE));
891 obj = yaffs_DentryToObject(f->f_dentry);
895 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
896 BUG_ON(!PageLocked(pg));
903 /* FIXME: Can kmap fail? */
905 yaffs_GrossLock(dev);
907 ret = yaffs_ReadDataFromFile(obj, pg_buf,
908 pg->index << PAGE_CACHE_SHIFT,
911 yaffs_GrossUnlock(dev);
917 ClearPageUptodate(pg);
924 flush_dcache_page(pg);
927 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
931 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
933 int ret = yaffs_readpage_nolock(f, pg);
938 static int yaffs_readpage(struct file *f, struct page *pg)
942 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
943 ret=yaffs_readpage_unlock(f, pg);
944 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
948 /* writepage inspired by/stolen from smbfs */
950 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
951 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
953 static int yaffs_writepage(struct page *page)
957 struct address_space *mapping = page->mapping;
959 unsigned long end_index;
968 inode = mapping->host;
971 i_size = i_size_read(inode);
973 end_index = i_size >> PAGE_CACHE_SHIFT;
975 if(page->index < end_index)
976 nBytes = PAGE_CACHE_SIZE;
978 nBytes = i_size & (PAGE_CACHE_SIZE -1);
980 if (page->index > end_index || !nBytes) {
982 (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
983 (unsigned)(page->index << PAGE_CACHE_SHIFT),
984 (unsigned)inode->i_size));
986 (TSTR(" -> don't care!!\n")));
988 zero_user_segment(page,0,PAGE_CACHE_SIZE);
989 set_page_writeback(page);
991 end_page_writeback(page);
996 if(nBytes != PAGE_CACHE_SIZE)
997 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
1001 buffer = kmap(page);
1003 obj = yaffs_InodeToObject(inode);
1005 yaffs_GrossLock(dev);
1008 (TSTR("yaffs_writepage at %08x, size %08x\n"),
1009 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
1011 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
1012 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1014 nWritten = yaffs_WriteDataToFile(obj, buffer,
1015 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
1017 yaffs_MarkSuperBlockDirty(dev);
1020 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
1021 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1023 yaffs_GrossUnlock(dev);
1026 set_page_writeback(page);
1028 end_page_writeback(page);
1031 return (nWritten == nBytes) ? 0 : -ENOSPC;
1035 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1036 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1037 loff_t pos, unsigned len, unsigned flags,
1038 struct page **pagep, void **fsdata)
1040 struct page *pg = NULL;
1041 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1047 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1048 pg = grab_cache_page_write_begin(mapping, index, flags);
1050 pg = __grab_cache_page(mapping, index);
1059 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
1060 (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
1063 space_held = yaffs_hold_space(filp);
1070 /* Update page if required */
1072 if (!Page_Uptodate(pg))
1073 ret = yaffs_readpage_nolock(filp, pg);
1078 /* Happy path return */
1079 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
1085 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
1087 yaffs_release_space(filp);
1090 page_cache_release(pg);
1097 static int yaffs_prepare_write(struct file *f, struct page *pg,
1098 unsigned offset, unsigned to)
1100 T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
1102 if (!Page_Uptodate(pg))
1103 return yaffs_readpage_nolock(f, pg);
1108 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1109 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1110 loff_t pos, unsigned len, unsigned copied,
1111 struct page *pg, void *fsdadata)
1115 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1118 addr = kva + offset_into_page;
1121 ("yaffs_write_end addr %p pos %x nBytes %d\n",
1122 addr,(unsigned)pos, copied));
1124 ret = yaffs_file_write(filp, addr, copied, &pos);
1126 if (ret != copied) {
1128 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
1137 yaffs_release_space(filp);
1139 page_cache_release(pg);
1144 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1149 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1150 int nBytes = to - offset;
1153 unsigned spos = pos;
1157 addr = kva + offset;
1159 saddr = (unsigned) addr;
1162 (TSTR("yaffs_commit_write addr %x pos %x nBytes %d\n"),
1163 saddr, spos, nBytes));
1165 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1167 if (nWritten != nBytes) {
1169 (TSTR("yaffs_commit_write not same size nWritten %d nBytes %d\n"),
1179 (TSTR("yaffs_commit_write returning %d\n"),
1180 nWritten == nBytes ? 0 : nWritten));
1182 return nWritten == nBytes ? 0 : nWritten;
1187 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1192 /* Check mode against the variant type and attempt to repair if broken. */
1193 __u32 mode = obj->yst_mode;
1194 switch (obj->variantType) {
1195 case YAFFS_OBJECT_TYPE_FILE:
1196 if (!S_ISREG(mode)) {
1197 obj->yst_mode &= ~S_IFMT;
1198 obj->yst_mode |= S_IFREG;
1202 case YAFFS_OBJECT_TYPE_SYMLINK:
1203 if (!S_ISLNK(mode)) {
1204 obj->yst_mode &= ~S_IFMT;
1205 obj->yst_mode |= S_IFLNK;
1209 case YAFFS_OBJECT_TYPE_DIRECTORY:
1210 if (!S_ISDIR(mode)) {
1211 obj->yst_mode &= ~S_IFMT;
1212 obj->yst_mode |= S_IFDIR;
1216 case YAFFS_OBJECT_TYPE_UNKNOWN:
1217 case YAFFS_OBJECT_TYPE_HARDLINK:
1218 case YAFFS_OBJECT_TYPE_SPECIAL:
1224 inode->i_flags |= S_NOATIME;
1226 inode->i_ino = obj->objectId;
1227 inode->i_mode = obj->yst_mode;
1228 inode->i_uid = obj->yst_uid;
1229 inode->i_gid = obj->yst_gid;
1230 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1231 inode->i_blksize = inode->i_sb->s_blocksize;
1233 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1235 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1236 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1237 inode->i_atime.tv_nsec = 0;
1238 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1239 inode->i_mtime.tv_nsec = 0;
1240 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1241 inode->i_ctime.tv_nsec = 0;
1243 inode->i_rdev = obj->yst_rdev;
1244 inode->i_atime = obj->yst_atime;
1245 inode->i_mtime = obj->yst_mtime;
1246 inode->i_ctime = obj->yst_ctime;
1248 inode->i_size = yaffs_GetObjectFileLength(obj);
1249 inode->i_blocks = (inode->i_size + 511) >> 9;
1251 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1254 (TSTR("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n"),
1255 inode->i_mode, inode->i_uid, inode->i_gid,
1256 (int)inode->i_size, atomic_read(&inode->i_count)));
1258 switch (obj->yst_mode & S_IFMT) {
1259 default: /* fifo, device or socket */
1260 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1261 init_special_inode(inode, obj->yst_mode,
1262 old_decode_dev(obj->yst_rdev));
1264 init_special_inode(inode, obj->yst_mode,
1265 (dev_t) (obj->yst_rdev));
1268 case S_IFREG: /* file */
1269 inode->i_op = &yaffs_file_inode_operations;
1270 inode->i_fop = &yaffs_file_operations;
1271 inode->i_mapping->a_ops =
1272 &yaffs_file_address_operations;
1274 case S_IFDIR: /* directory */
1275 inode->i_op = &yaffs_dir_inode_operations;
1276 inode->i_fop = &yaffs_dir_operations;
1278 case S_IFLNK: /* symlink */
1279 inode->i_op = &yaffs_symlink_inode_operations;
1283 yaffs_InodeToObjectLV(inode) = obj;
1285 obj->myInode = inode;
1289 (TSTR("yaffs_FileInode invalid parameters\n")));
1294 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1297 struct inode *inode;
1301 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1308 (TSTR("yaffs_get_inode for NULL object!!\n")));
1314 (TSTR("yaffs_get_inode for object %d\n"), obj->objectId));
1316 inode = Y_IGET(sb, obj->objectId);
1320 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1321 /* iget also increments the inode's i_count */
1322 /* NB You can't be holding grossLock or deadlock will happen! */
1327 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1332 struct inode *inode;
1335 obj = yaffs_DentryToObject(f->f_dentry);
1339 yaffs_GrossLock(dev);
1341 inode = f->f_dentry->d_inode;
1343 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1344 ipos = inode->i_size;
1350 (TSTR("yaffs_file_write: hey obj is null!\n")));
1353 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1354 "to object %d at %d(%x)\n"),
1355 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1357 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1359 yaffs_MarkSuperBlockDirty(dev);
1362 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1363 (unsigned )n,(unsigned)n));
1368 if (ipos > inode->i_size) {
1369 inode->i_size = ipos;
1370 inode->i_blocks = (ipos + 511) >> 9;
1373 (TSTR("yaffs_file_write size updated to %d bytes, "
1375 ipos, (int)(inode->i_blocks)));
1379 yaffs_GrossUnlock(dev);
1380 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1383 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1384 /* For now we just assume few parallel writes and check against a small number. */
1385 /* Todo: need to do this with a counter to handle parallel reads better */
1387 static ssize_t yaffs_hold_space(struct file *f)
1395 obj = yaffs_DentryToObject(f->f_dentry);
1399 yaffs_GrossLock(dev);
1401 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1403 yaffs_GrossUnlock(dev);
1405 return (nFreeChunks > 20) ? 1 : 0;
1408 static void yaffs_release_space(struct file *f)
1414 obj = yaffs_DentryToObject(f->f_dentry);
1418 yaffs_GrossLock(dev);
1421 yaffs_GrossUnlock(dev);
1425 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1433 offset += i_size_read(file->f_path.dentry->d_inode);
1436 offset += file->f_pos;
1441 if (offset != file->f_pos)
1442 file->f_pos = offset;
1451 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1455 struct yaffs_SearchContext *sc;
1456 struct inode *inode = f->f_dentry->d_inode;
1457 unsigned long offset, curoffs;
1461 char name[YAFFS_MAX_NAME_LENGTH + 1];
1463 obj = yaffs_DentryToObject(f->f_dentry);
1466 yaffs_GrossLock(dev);
1468 yaffs_DeviceToLC(dev)->readdirProcess = current;
1472 sc = yaffs_NewSearch(obj);
1478 T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1482 (TSTR("yaffs_readdir: entry . ino %d \n"),
1483 (int)inode->i_ino));
1484 yaffs_GrossUnlock(dev);
1485 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0){
1486 yaffs_GrossLock(dev);
1489 yaffs_GrossLock(dev);
1495 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1496 (int)f->f_dentry->d_parent->d_inode->i_ino));
1497 yaffs_GrossUnlock(dev);
1498 if (filldir(dirent, "..", 2, offset,
1499 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
1500 yaffs_GrossLock(dev);
1503 yaffs_GrossLock(dev);
1510 /* If the directory has changed since the open or last call to
1511 readdir, rewind to after the 2 canned entries. */
1512 if (f->f_version != inode->i_version) {
1515 f->f_version = inode->i_version;
1518 while(sc->nextReturn){
1521 if (curoffs >= offset) {
1522 int this_inode = yaffs_GetObjectInode(l);
1523 int this_type = yaffs_GetObjectType(l);
1525 yaffs_GetObjectName(l, name,
1526 YAFFS_MAX_NAME_LENGTH + 1);
1528 (TSTR("yaffs_readdir: %s inode %d\n"),
1529 name, yaffs_GetObjectInode(l)));
1531 yaffs_GrossUnlock(dev);
1539 yaffs_GrossLock(dev);
1543 yaffs_GrossLock(dev);
1548 yaffs_SearchAdvance(sc);
1552 yaffs_EndSearch(sc);
1553 yaffs_DeviceToLC(dev)->readdirProcess = NULL;
1554 yaffs_GrossUnlock(dev);
1562 * File creation. Allocate an inode, and we're done..
1565 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1568 #define YCRED(x) (x->cred)
1571 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1572 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1575 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1579 struct inode *inode;
1581 yaffs_Object *obj = NULL;
1584 yaffs_Object *parent = yaffs_InodeToObject(dir);
1586 int error = -ENOSPC;
1587 uid_t uid = YCRED(current)->fsuid;
1588 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1590 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1595 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1596 parent->objectId, parent->variantType));
1599 (TSTR("yaffs_mknod: could not get parent object\n")));
1603 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1604 "mode %x dev %x\n"),
1605 dentry->d_name.name, mode, rdev));
1607 dev = parent->myDev;
1609 yaffs_GrossLock(dev);
1611 switch (mode & S_IFMT) {
1613 /* Special (socket, fifo, device...) */
1614 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1615 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1616 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1617 gid, old_encode_dev(rdev));
1619 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1623 case S_IFREG: /* file */
1624 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1625 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1628 case S_IFDIR: /* directory */
1630 (TSTR("yaffs_mknod: making directory\n")));
1631 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1634 case S_IFLNK: /* symlink */
1635 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1636 obj = NULL; /* Do we ever get here? */
1640 /* Can not call yaffs_get_inode() with gross lock held */
1641 yaffs_GrossUnlock(dev);
1644 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1645 d_instantiate(dentry, inode);
1646 update_dir_time(dir);
1648 (TSTR("yaffs_mknod created object %d count = %d\n"),
1649 obj->objectId, atomic_read(&inode->i_count)));
1651 yaffs_FillInodeFromObject(dir,parent);
1654 (TSTR("yaffs_mknod failed making object\n")));
1661 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1664 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1665 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1669 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1670 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1671 struct nameidata *n)
1673 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1676 T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1677 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1680 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1688 (TSTR("yaffs_unlink %d:%s\n"),
1690 dentry->d_name.name));
1691 obj = yaffs_InodeToObject(dir);
1694 yaffs_GrossLock(dev);
1696 retVal = yaffs_Unlink(obj, dentry->d_name.name);
1698 if (retVal == YAFFS_OK) {
1699 dentry->d_inode->i_nlink--;
1701 yaffs_GrossUnlock(dev);
1702 mark_inode_dirty(dentry->d_inode);
1703 update_dir_time(dir);
1706 yaffs_GrossUnlock(dev);
1713 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1714 struct dentry *dentry)
1716 struct inode *inode = old_dentry->d_inode;
1717 yaffs_Object *obj = NULL;
1718 yaffs_Object *link = NULL;
1721 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1723 obj = yaffs_InodeToObject(inode);
1726 yaffs_GrossLock(dev);
1728 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1729 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1733 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1734 d_instantiate(dentry, old_dentry->d_inode);
1735 atomic_inc(&old_dentry->d_inode->i_count);
1737 (TSTR("yaffs_link link count %d i_count %d\n"),
1738 old_dentry->d_inode->i_nlink,
1739 atomic_read(&old_dentry->d_inode->i_count)));
1742 yaffs_GrossUnlock(dev);
1745 update_dir_time(dir);
1752 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1753 const char *symname)
1757 uid_t uid = YCRED(current)->fsuid;
1758 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1760 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1762 dev = yaffs_InodeToObject(dir)->myDev;
1763 yaffs_GrossLock(dev);
1764 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1765 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1766 yaffs_GrossUnlock(dev);
1769 struct inode *inode;
1771 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1772 d_instantiate(dentry, inode);
1773 update_dir_time(dir);
1774 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1777 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1783 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1784 static int yaffs_sync_object(struct file *file, int datasync)
1786 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1793 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1794 struct dentry *dentry = file->f_path.dentry;
1797 obj = yaffs_DentryToObject(dentry);
1801 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1802 (TSTR("yaffs_sync_object\n")));
1803 yaffs_GrossLock(dev);
1804 yaffs_FlushFile(obj, 1, datasync);
1805 yaffs_GrossUnlock(dev);
1810 * The VFS layer already does all the dentry stuff for rename.
1812 * NB: POSIX says you can rename an object over an old object of the same name
1814 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1815 struct inode *new_dir, struct dentry *new_dentry)
1818 int retVal = YAFFS_FAIL;
1819 yaffs_Object *target;
1821 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1822 dev = yaffs_InodeToObject(old_dir)->myDev;
1824 yaffs_GrossLock(dev);
1826 /* Check if the target is an existing directory that is not empty. */
1827 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1828 new_dentry->d_name.name);
1832 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1833 !ylist_empty(&target->variant.directoryVariant.children)) {
1835 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1837 retVal = YAFFS_FAIL;
1839 /* Now does unlinking internally using shadowing mechanism */
1840 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_RenameObject\n")));
1842 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1843 old_dentry->d_name.name,
1844 yaffs_InodeToObject(new_dir),
1845 new_dentry->d_name.name);
1847 yaffs_GrossUnlock(dev);
1849 if (retVal == YAFFS_OK) {
1851 new_dentry->d_inode->i_nlink--;
1852 mark_inode_dirty(new_dentry->d_inode);
1855 update_dir_time(old_dir);
1856 if(old_dir != new_dir)
1857 update_dir_time(new_dir);
1864 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1866 struct inode *inode = dentry->d_inode;
1871 (TSTR("yaffs_setattr of object %d\n"),
1872 yaffs_InodeToObject(inode)->objectId));
1874 /* Fail if a requested resize >= 2GB */
1875 if (attr->ia_valid & ATTR_SIZE &&
1876 (attr->ia_size >> 31))
1880 error = inode_change_ok(inode, attr);
1884 error = inode_setattr(inode, attr);
1885 T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
1886 if (attr->ia_valid & ATTR_SIZE)
1887 truncate_inode_pages(&inode->i_data,attr->ia_size);
1889 dev = yaffs_InodeToObject(inode)->myDev;
1890 if (attr->ia_valid & ATTR_SIZE){
1891 T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
1892 (int)(attr->ia_size),(int)(attr->ia_size)));
1894 yaffs_GrossLock(dev);
1895 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1896 if(result == YAFFS_OK) {
1901 yaffs_GrossUnlock(dev);
1906 (TSTR("yaffs_setattr done returning %d\n"),error));
1911 #ifdef CONFIG_YAFFS_XATTR
1912 int yaffs_setxattr(struct dentry *dentry, const char *name,
1913 const void *value, size_t size, int flags)
1915 struct inode *inode = dentry->d_inode;
1918 yaffs_Object *obj = yaffs_InodeToObject(inode);
1921 (TSTR("yaffs_setxattr of object %d\n"),
1928 yaffs_GrossLock(dev);
1929 result = yaffs_SetXAttribute(obj, name, value, size, flags);
1930 if(result == YAFFS_OK)
1934 yaffs_GrossUnlock(dev);
1938 (TSTR("yaffs_setxattr done returning %d\n"),error));
1944 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
1947 struct inode *inode = dentry->d_inode;
1950 yaffs_Object *obj = yaffs_InodeToObject(inode);
1953 (TSTR("yaffs_getxattr of object %d\n"),
1959 yaffs_GrossLock(dev);
1960 error = yaffs_GetXAttribute(obj, name, buff, size);
1961 yaffs_GrossUnlock(dev);
1965 (TSTR("yaffs_getxattr done returning %d\n"),error));
1970 int yaffs_removexattr(struct dentry *dentry, const char *name)
1972 struct inode *inode = dentry->d_inode;
1975 yaffs_Object *obj = yaffs_InodeToObject(inode);
1978 (TSTR("yaffs_removexattr of object %d\n"),
1985 yaffs_GrossLock(dev);
1986 result = yaffs_RemoveXAttribute(obj, name);
1987 if(result == YAFFS_OK)
1991 yaffs_GrossUnlock(dev);
1995 (TSTR("yaffs_removexattr done returning %d\n"),error));
2000 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
2002 struct inode *inode = dentry->d_inode;
2005 yaffs_Object *obj = yaffs_InodeToObject(inode);
2008 (TSTR("yaffs_listxattr of object %d\n"),
2014 yaffs_GrossLock(dev);
2015 error = yaffs_ListXAttributes(obj, buff, size);
2016 yaffs_GrossUnlock(dev);
2020 (TSTR("yaffs_listxattr done returning %d\n"),error));
2028 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2029 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2031 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2032 struct super_block *sb = dentry->d_sb;
2033 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2034 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2036 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2038 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2040 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2043 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
2045 yaffs_GrossLock(dev);
2047 buf->f_type = YAFFS_MAGIC;
2048 buf->f_bsize = sb->s_blocksize;
2049 buf->f_namelen = 255;
2051 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
2052 /* Do this if chunk size is not a power of 2 */
2054 uint64_t bytesInDev;
2057 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
2058 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
2060 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
2061 buf->f_blocks = bytesInDev;
2063 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
2064 ((uint64_t)(dev->nDataBytesPerChunk));
2066 do_div(bytesFree, sb->s_blocksize);
2068 buf->f_bfree = bytesFree;
2070 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2073 (dev->param.endBlock - dev->param.startBlock + 1) *
2074 dev->param.nChunksPerBlock /
2075 (sb->s_blocksize / dev->nDataBytesPerChunk);
2077 yaffs_GetNumberOfFreeChunks(dev) /
2078 (sb->s_blocksize / dev->nDataBytesPerChunk);
2081 (dev->param.endBlock - dev->param.startBlock + 1) *
2082 dev->param.nChunksPerBlock *
2083 (dev->nDataBytesPerChunk / sb->s_blocksize);
2086 yaffs_GetNumberOfFreeChunks(dev) *
2087 (dev->nDataBytesPerChunk / sb->s_blocksize);
2092 buf->f_bavail = buf->f_bfree;
2094 yaffs_GrossUnlock(dev);
2100 static void yaffs_FlushInodes(struct super_block *sb)
2105 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
2106 obj = yaffs_InodeToObject(iptr);
2108 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
2110 yaffs_FlushFile(obj,1,0);
2116 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
2118 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2122 yaffs_FlushInodes(sb);
2123 yaffs_UpdateDirtyDirectories(dev);
2124 yaffs_FlushEntireDeviceCache(dev);
2126 yaffs_CheckpointSave(dev);
2130 static unsigned yaffs_bg_gc_urgency(yaffs_Device *dev)
2132 unsigned erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2133 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2134 unsigned scatteredFree = 0; /* Free chunks not in an erased block */
2136 if(erasedChunks < dev->nFreeChunks)
2137 scatteredFree = (dev->nFreeChunks - erasedChunks);
2139 if(!context->bgRunning)
2141 else if(scatteredFree < (dev->param.nChunksPerBlock * 2))
2143 else if(erasedChunks > dev->nFreeChunks/2)
2145 else if(erasedChunks > dev->nFreeChunks/4)
2151 static int yaffs_do_sync_fs(struct super_block *sb,
2152 int request_checkpoint)
2155 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2156 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2157 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2160 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2161 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
2163 sb->s_dirt ? "dirty" : "clean",
2164 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2165 oneshot_checkpoint ? " one-shot" : "" ));
2167 yaffs_GrossLock(dev);
2168 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2169 oneshot_checkpoint) &&
2170 !dev->isCheckpointed;
2172 if (sb->s_dirt || do_checkpoint) {
2173 yaffs_FlushSuperBlock(sb, !dev->isCheckpointed && do_checkpoint);
2175 if(oneshot_checkpoint)
2176 yaffs_auto_checkpoint &= ~4;
2178 yaffs_GrossUnlock(dev);
2184 * yaffs background thread functions .
2185 * yaffs_BackgroundThread() the thread function
2186 * yaffs_BackgroundStart() launches the background thread.
2187 * yaffs_BackgroundStop() cleans up the background thread.
2190 * The thread should only run after the yaffs is initialised
2191 * The thread should be stopped before yaffs is unmounted.
2192 * The thread should not do any writing while the fs is in read only.
2195 #ifdef YAFFS_COMPILE_BACKGROUND
2197 void yaffs_background_waker(unsigned long data)
2199 wake_up_process((struct task_struct *)data);
2202 static int yaffs_BackgroundThread(void *data)
2204 yaffs_Device *dev = (yaffs_Device *)data;
2205 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2206 unsigned long now = jiffies;
2207 unsigned long next_dir_update = now;
2208 unsigned long next_gc = now;
2209 unsigned long expires;
2210 unsigned int urgency;
2213 struct timer_list timer;
2215 T(YAFFS_TRACE_BACKGROUND,
2216 (TSTR("yaffs_background starting for dev %p\n"),
2221 while(context->bgRunning){
2222 T(YAFFS_TRACE_BACKGROUND,
2223 (TSTR("yaffs_background\n")));
2225 if(kthread_should_stop())
2231 yaffs_GrossLock(dev);
2235 if(time_after(now, next_dir_update)){
2236 yaffs_UpdateDirtyDirectories(dev);
2237 next_dir_update = now + HZ;
2240 if(time_after(now,next_gc)){
2241 if(!dev->isCheckpointed){
2242 urgency = yaffs_bg_gc_urgency(dev);
2243 gcResult = yaffs_BackgroundGarbageCollect(dev, urgency);
2245 next_gc = now + HZ/20+1;
2246 else if(urgency > 0)
2247 next_gc = now + HZ/10+1;
2249 next_gc = now + HZ * 2;
2251 * gc not running so set to next_dir_update
2252 * to cut down on wake ups
2254 next_gc = next_dir_update;
2256 yaffs_GrossUnlock(dev);
2258 expires = next_dir_update;
2259 if (time_before(next_gc,expires))
2261 if(time_before(expires,now))
2264 init_timer_on_stack(&timer);
2265 timer.expires = expires+1;
2266 timer.data = (unsigned long) current;
2267 timer.function = yaffs_background_waker;
2269 set_current_state(TASK_INTERRUPTIBLE);
2272 del_timer_sync(&timer);
2281 static int yaffs_BackgroundStart(yaffs_Device *dev)
2284 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2289 context->bgRunning = 1;
2291 context->bgThread = kthread_run(yaffs_BackgroundThread,
2292 (void *)dev,"yaffs-bg-%d",context->mount_id);
2294 if(IS_ERR(context->bgThread)){
2295 retval = PTR_ERR(context->bgThread);
2296 context->bgThread = NULL;
2297 context->bgRunning = 0;
2302 static void yaffs_BackgroundStop(yaffs_Device *dev)
2304 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToLC(dev);
2306 ctxt->bgRunning = 0;
2308 if( ctxt->bgThread){
2309 kthread_stop(ctxt->bgThread);
2310 ctxt->bgThread = NULL;
2314 static int yaffs_BackgroundThread(void *data)
2319 static int yaffs_BackgroundStart(yaffs_Device *dev)
2324 static void yaffs_BackgroundStop(yaffs_Device *dev)
2330 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2331 static void yaffs_write_super(struct super_block *sb)
2333 static int yaffs_write_super(struct super_block *sb)
2336 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2338 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2339 (TSTR("yaffs_write_super%s\n"),
2340 request_checkpoint ? " checkpt" : ""));
2342 yaffs_do_sync_fs(sb, request_checkpoint);
2344 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2350 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2351 static int yaffs_sync_fs(struct super_block *sb, int wait)
2353 static int yaffs_sync_fs(struct super_block *sb)
2356 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2358 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2359 (TSTR("yaffs_sync_fs%s\n"),
2360 request_checkpoint ? " checkpt" : ""));
2362 yaffs_do_sync_fs(sb, request_checkpoint);
2367 #ifdef YAFFS_USE_OWN_IGET
2369 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2371 struct inode *inode;
2373 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2376 (TSTR("yaffs_iget for %lu\n"), ino));
2378 inode = iget_locked(sb, ino);
2380 return ERR_PTR(-ENOMEM);
2381 if (!(inode->i_state & I_NEW))
2384 /* NB This is called as a side effect of other functions, but
2385 * we had to release the lock to prevent deadlocks, so
2386 * need to lock again.
2389 yaffs_GrossLock(dev);
2391 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2393 yaffs_FillInodeFromObject(inode, obj);
2395 yaffs_GrossUnlock(dev);
2397 unlock_new_inode(inode);
2403 static void yaffs_read_inode(struct inode *inode)
2405 /* NB This is called as a side effect of other functions, but
2406 * we had to release the lock to prevent deadlocks, so
2407 * need to lock again.
2411 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2414 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2416 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2417 yaffs_GrossLock(dev);
2419 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2421 yaffs_FillInodeFromObject(inode, obj);
2423 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2424 yaffs_GrossUnlock(dev);
2429 static YLIST_HEAD(yaffs_context_list);
2430 struct semaphore yaffs_context_lock;
2432 static void yaffs_put_super(struct super_block *sb)
2434 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2436 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2438 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2439 (TSTR("Shutting down yaffs background thread\n")));
2440 yaffs_BackgroundStop(dev);
2441 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2442 (TSTR("yaffs background thread shut down\n")));
2444 yaffs_GrossLock(dev);
2446 yaffs_FlushSuperBlock(sb,1);
2448 if (yaffs_DeviceToLC(dev)->putSuperFunc)
2449 yaffs_DeviceToLC(dev)->putSuperFunc(sb);
2452 yaffs_Deinitialise(dev);
2454 yaffs_GrossUnlock(dev);
2456 down(&yaffs_context_lock);
2457 ylist_del_init(&(yaffs_DeviceToLC(dev)->contextList));
2458 up(&yaffs_context_lock);
2460 if (yaffs_DeviceToLC(dev)->spareBuffer) {
2461 YFREE(yaffs_DeviceToLC(dev)->spareBuffer);
2462 yaffs_DeviceToLC(dev)->spareBuffer = NULL;
2469 static void yaffs_MTDPutSuper(struct super_block *sb)
2471 struct mtd_info *mtd = yaffs_DeviceToMtd(yaffs_SuperToDevice(sb));
2476 put_mtd_device(mtd);
2480 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2482 struct super_block *sb = yaffs_DeviceToLC(dev)->superBlock;
2484 T(YAFFS_TRACE_OS, (TSTR("yaffs_MarkSuperBlockDirty() sb = %p\n"), sb));
2491 int skip_checkpoint_read;
2492 int skip_checkpoint_write;
2495 int tags_ecc_overridden;
2496 int lazy_loading_enabled;
2497 int lazy_loading_overridden;
2498 int empty_lost_and_found;
2499 int empty_lost_and_found_overridden;
2502 #define MAX_OPT_LEN 30
2503 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2505 char cur_opt[MAX_OPT_LEN + 1];
2509 /* Parse through the options which is a comma seperated list */
2511 while (options_str && *options_str && !error) {
2512 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2515 while(*options_str == ',')
2518 while (*options_str && *options_str != ',') {
2519 if (p < MAX_OPT_LEN) {
2520 cur_opt[p] = *options_str;
2526 if (!strcmp(cur_opt, "inband-tags"))
2527 options->inband_tags = 1;
2528 else if (!strcmp(cur_opt, "tags-ecc-off")){
2529 options->tags_ecc_on = 0;
2530 options->tags_ecc_overridden=1;
2531 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2532 options->tags_ecc_on = 1;
2533 options->tags_ecc_overridden = 1;
2534 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2535 options->lazy_loading_enabled = 0;
2536 options->lazy_loading_overridden=1;
2537 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2538 options->lazy_loading_enabled = 1;
2539 options->lazy_loading_overridden = 1;
2540 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2541 options->empty_lost_and_found = 0;
2542 options->empty_lost_and_found_overridden=1;
2543 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2544 options->empty_lost_and_found = 1;
2545 options->empty_lost_and_found_overridden=1;
2546 } else if (!strcmp(cur_opt, "no-cache"))
2547 options->no_cache = 1;
2548 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2549 options->skip_checkpoint_read = 1;
2550 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2551 options->skip_checkpoint_write = 1;
2552 else if (!strcmp(cur_opt, "no-checkpoint")) {
2553 options->skip_checkpoint_read = 1;
2554 options->skip_checkpoint_write = 1;
2556 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2565 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2566 struct super_block *sb,
2567 void *data, int silent)
2570 struct inode *inode = NULL;
2571 struct dentry *root;
2572 yaffs_Device *dev = 0;
2573 char devname_buf[BDEVNAME_SIZE + 1];
2574 struct mtd_info *mtd;
2576 char *data_str = (char *)data;
2577 struct yaffs_LinuxContext *context = NULL;
2578 yaffs_DeviceParam *param;
2582 yaffs_options options;
2586 struct yaffs_LinuxContext *context_iterator;
2587 struct ylist_head *l;
2589 sb->s_magic = YAFFS_MAGIC;
2590 sb->s_op = &yaffs_super_ops;
2591 sb->s_flags |= MS_NOATIME;
2593 readOnly =((sb->s_flags & MS_RDONLY) != 0);
2596 #ifdef YAFFS_COMPILE_EXPORTFS
2597 sb->s_export_op = &yaffs_export_ops;
2601 printk(KERN_INFO "yaffs: sb is NULL\n");
2602 else if (!sb->s_dev)
2603 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2604 else if (!yaffs_devname(sb, devname_buf))
2605 printk(KERN_INFO "yaffs: devname is NULL\n");
2607 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2609 yaffs_devname(sb, devname_buf),
2610 readOnly ? "ro" : "rw");
2615 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2617 memset(&options, 0, sizeof(options));
2619 if (yaffs_parse_options(&options, data_str)) {
2620 /* Option parsing failed */
2625 sb->s_blocksize = PAGE_CACHE_SIZE;
2626 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2629 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffsVersion));
2631 (TSTR("yaffs_read_super: block size %d\n"),
2632 (int)(sb->s_blocksize)));
2634 T(YAFFS_TRACE_ALWAYS,
2635 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2636 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2637 yaffs_devname(sb, devname_buf)));
2639 /* Check it's an mtd device..... */
2640 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2641 return NULL; /* This isn't an mtd device */
2643 /* Get the device */
2644 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2646 T(YAFFS_TRACE_ALWAYS,
2647 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2651 /* Check it's NAND */
2652 if (mtd->type != MTD_NANDFLASH) {
2653 T(YAFFS_TRACE_ALWAYS,
2654 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2659 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2660 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2661 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2662 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2663 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2664 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2665 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2666 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2667 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2668 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2669 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2670 T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2672 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2675 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2677 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2678 T(YAFFS_TRACE_ALWAYS,
2679 (TSTR("yaffs: auto selecting yaffs2\n")));
2683 /* Added NCB 26/5/2006 for completeness */
2684 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2685 T(YAFFS_TRACE_ALWAYS,
2686 (TSTR("yaffs: auto selecting yaffs1\n")));
2692 if (yaffsVersion == 2) {
2693 /* Check for version 2 style functions */
2695 !mtd->block_isbad ||
2696 !mtd->block_markbad ||
2699 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2700 !mtd->read_oob || !mtd->write_oob) {
2703 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2705 T(YAFFS_TRACE_ALWAYS,
2706 (TSTR("yaffs: MTD device does not support required "
2711 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2712 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2713 !options.inband_tags) {
2714 T(YAFFS_TRACE_ALWAYS,
2715 (TSTR("yaffs: MTD device does not have the "
2716 "right page sizes\n")));
2720 /* Check for V1 style functions */
2724 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2725 !mtd->read_oob || !mtd->write_oob) {
2728 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2730 T(YAFFS_TRACE_ALWAYS,
2731 (TSTR("yaffs: MTD device does not support required "
2736 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2737 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2738 T(YAFFS_TRACE_ALWAYS,
2739 (TSTR("yaffs: MTD device does not support have the "
2740 "right page sizes\n")));
2745 /* OK, so if we got here, we have an MTD that's NAND and looks
2746 * like it has the right capabilities
2747 * Set the yaffs_Device up for mtd
2750 if (!readOnly && !(mtd->flags & MTD_WRITEABLE)){
2752 printk(KERN_INFO "yaffs: mtd is read only, setting superblock read only");
2753 sb->s_flags |= MS_RDONLY;
2756 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2757 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2759 if(!dev || !context ){
2769 /* Deep shit could not allocate device structure */
2770 T(YAFFS_TRACE_ALWAYS,
2771 (TSTR("yaffs_read_super: Failed trying to allocate "
2772 "yaffs_Device. \n")));
2775 memset(dev, 0, sizeof(yaffs_Device));
2776 param = &(dev->param);
2778 memset(context,0,sizeof(struct yaffs_LinuxContext));
2779 dev->osContext = context;
2780 YINIT_LIST_HEAD(&(context->contextList));
2782 context->superBlock = sb;
2784 dev->readOnly = readOnly;
2786 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2787 sb->s_fs_info = dev;
2789 sb->u.generic_sbp = dev;
2792 dev->driverContext = mtd;
2793 param->name = mtd->name;
2795 /* Set up the memory size parameters.... */
2797 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2799 param->startBlock = 0;
2800 param->endBlock = nBlocks - 1;
2801 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2802 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2803 param->nReservedBlocks = 5;
2804 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2805 param->inbandTags = options.inband_tags;
2807 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2808 param->disableLazyLoad = 1;
2810 #ifdef CONFIG_YAFFS_XATTR
2811 param->enableXattr = 1;
2813 if(options.lazy_loading_overridden)
2814 param->disableLazyLoad = !options.lazy_loading_enabled;
2816 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2817 param->noTagsECC = 1;
2820 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2822 param->deferDirectoryUpdate = 1;
2825 if(options.tags_ecc_overridden)
2826 param->noTagsECC = !options.tags_ecc_on;
2828 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2829 param->emptyLostAndFound = 1;
2832 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2833 param->refreshPeriod = 0;
2835 param->refreshPeriod = 500;
2838 if(options.empty_lost_and_found_overridden)
2839 param->emptyLostAndFound = options.empty_lost_and_found;
2841 /* ... and the functions. */
2842 if (yaffsVersion == 2) {
2843 param->writeChunkWithTagsToNAND =
2844 nandmtd2_WriteChunkWithTagsToNAND;
2845 param->readChunkWithTagsFromNAND =
2846 nandmtd2_ReadChunkWithTagsFromNAND;
2847 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2848 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2849 yaffs_DeviceToLC(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2850 param->isYaffs2 = 1;
2851 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2852 param->totalBytesPerChunk = mtd->writesize;
2853 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2855 param->totalBytesPerChunk = mtd->oobblock;
2856 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2858 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2860 param->startBlock = 0;
2861 param->endBlock = nBlocks - 1;
2863 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2864 /* use the MTD interface in yaffs_mtdif1.c */
2865 param->writeChunkWithTagsToNAND =
2866 nandmtd1_WriteChunkWithTagsToNAND;
2867 param->readChunkWithTagsFromNAND =
2868 nandmtd1_ReadChunkWithTagsFromNAND;
2869 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2870 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2872 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2873 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2875 param->isYaffs2 = 0;
2877 /* ... and common functions */
2878 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2879 param->initialiseNAND = nandmtd_InitialiseNAND;
2881 yaffs_DeviceToLC(dev)->putSuperFunc = yaffs_MTDPutSuper;
2883 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2884 param->gcControl = yaffs_gc_control_callback;
2886 yaffs_DeviceToLC(dev)->superBlock= sb;
2889 #ifndef CONFIG_YAFFS_DOES_ECC
2890 param->useNANDECC = 1;
2893 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2894 param->wideTnodesDisabled = 1;
2897 param->skipCheckpointRead = options.skip_checkpoint_read;
2898 param->skipCheckpointWrite = options.skip_checkpoint_write;
2900 down(&yaffs_context_lock);
2901 /* Get a mount id */
2903 for(mount_id=0; ! found; mount_id++){
2905 ylist_for_each(l,&yaffs_context_list){
2906 context_iterator = ylist_entry(l,struct yaffs_LinuxContext,contextList);
2907 if(context_iterator->mount_id == mount_id)
2911 context->mount_id = mount_id;
2913 ylist_add_tail(&(yaffs_DeviceToLC(dev)->contextList), &yaffs_context_list);
2914 up(&yaffs_context_lock);
2916 /* Directory search handling...*/
2917 YINIT_LIST_HEAD(&(yaffs_DeviceToLC(dev)->searchContexts));
2918 param->removeObjectCallback = yaffs_RemoveObjectCallback;
2920 init_MUTEX(&(yaffs_DeviceToLC(dev)->grossLock));
2922 yaffs_GrossLock(dev);
2924 err = yaffs_GutsInitialise(dev);
2927 (TSTR("yaffs_read_super: guts initialised %s\n"),
2928 (err == YAFFS_OK) ? "OK" : "FAILED"));
2931 yaffs_BackgroundStart(dev);
2933 if(!context->bgThread)
2934 param->deferDirectoryUpdate = 0;
2937 /* Release lock before yaffs_get_inode() */
2938 yaffs_GrossUnlock(dev);
2940 /* Create root inode */
2941 if (err == YAFFS_OK)
2942 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2948 inode->i_op = &yaffs_dir_inode_operations;
2949 inode->i_fop = &yaffs_dir_operations;
2951 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
2953 root = d_alloc_root(inode);
2955 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
2962 sb->s_dirt = !dev->isCheckpointed;
2963 T(YAFFS_TRACE_ALWAYS,
2964 (TSTR("yaffs_read_super: isCheckpointed %d\n"),
2965 dev->isCheckpointed));
2967 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
2972 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2973 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2976 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2979 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2980 static int yaffs_read_super(struct file_system_type *fs,
2981 int flags, const char *dev_name,
2982 void *data, struct vfsmount *mnt)
2985 return get_sb_bdev(fs, flags, dev_name, data,
2986 yaffs_internal_read_super_mtd, mnt);
2989 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2990 int flags, const char *dev_name,
2994 return get_sb_bdev(fs, flags, dev_name, data,
2995 yaffs_internal_read_super_mtd);
2999 static struct file_system_type yaffs_fs_type = {
3000 .owner = THIS_MODULE,
3002 .get_sb = yaffs_read_super,
3003 .kill_sb = kill_block_super,
3004 .fs_flags = FS_REQUIRES_DEV,
3007 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3010 return yaffs_internal_read_super(1, sb, data, silent);
3013 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3018 #ifdef CONFIG_YAFFS_YAFFS2
3020 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3021 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3024 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3027 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3028 static int yaffs2_read_super(struct file_system_type *fs,
3029 int flags, const char *dev_name, void *data,
3030 struct vfsmount *mnt)
3032 return get_sb_bdev(fs, flags, dev_name, data,
3033 yaffs2_internal_read_super_mtd, mnt);
3036 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3037 int flags, const char *dev_name,
3041 return get_sb_bdev(fs, flags, dev_name, data,
3042 yaffs2_internal_read_super_mtd);
3046 static struct file_system_type yaffs2_fs_type = {
3047 .owner = THIS_MODULE,
3049 .get_sb = yaffs2_read_super,
3050 .kill_sb = kill_block_super,
3051 .fs_flags = FS_REQUIRES_DEV,
3054 static struct super_block *yaffs2_read_super(struct super_block *sb,
3055 void *data, int silent)
3057 return yaffs_internal_read_super(2, sb, data, silent);
3060 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3064 #endif /* CONFIG_YAFFS_YAFFS2 */
3066 static struct proc_dir_entry *my_proc_entry;
3067 static struct proc_dir_entry *debug_proc_entry;
3069 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
3071 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
3072 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
3073 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
3074 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
3075 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
3076 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
3077 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
3078 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
3079 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
3080 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
3081 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
3082 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
3084 buf += sprintf(buf, "\n");
3090 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
3092 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3093 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3094 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3095 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3096 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3097 buf += sprintf(buf, "\n");
3098 buf += sprintf(buf, "nTnodes............ %d\n", dev->nTnodes);
3099 buf += sprintf(buf, "nObjects........... %d\n", dev->nObjects);
3100 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3101 buf += sprintf(buf, "\n");
3102 buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
3103 buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
3104 buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
3105 buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
3106 buf += sprintf(buf, "allGCs............. %u\n", dev->allGCs);
3107 buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGCs);
3108 buf += sprintf(buf, "oldestDirtyGCs..... %u\n", dev->oldestDirtyGCs);
3109 buf += sprintf(buf, "backgroundGCs...... %u\n", dev->backgroundGCs);
3110 buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
3111 buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
3112 buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
3113 buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
3114 buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
3115 buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
3116 buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
3117 buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
3118 buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
3119 buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
3121 sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
3126 static int yaffs_proc_read(char *page,
3128 off_t offset, int count, int *eof, void *data)
3130 struct ylist_head *item;
3135 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3136 * We use 'offset' (*ppos) to indicate where we are in devList.
3137 * This also assumes the user has posted a read buffer large
3138 * enough to hold the complete output; but that's life in /proc.
3143 /* Print header first */
3145 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__"\n");
3147 buf += sprintf(buf,"\n");
3151 down(&yaffs_context_lock);
3153 /* Locate and print the Nth entry. Order N-squared but N is small. */
3154 ylist_for_each(item, &yaffs_context_list) {
3155 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3156 yaffs_Device *dev = dc->dev;
3158 if (n < (step & ~1)) {
3163 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
3164 buf = yaffs_dump_dev_part0(buf, dev);
3166 buf = yaffs_dump_dev_part1(buf, dev);
3170 up(&yaffs_context_lock);
3173 return buf - page < count ? buf - page : count;
3176 static int yaffs_stats_proc_read(char *page,
3178 off_t offset, int count, int *eof, void *data)
3180 struct ylist_head *item;
3184 down(&yaffs_context_lock);
3186 /* Locate and print the Nth entry. Order N-squared but N is small. */
3187 ylist_for_each(item, &yaffs_context_list) {
3188 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3189 yaffs_Device *dev = dc->dev;
3193 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3195 buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
3196 n, dev->nFreeChunks, erasedChunks,
3197 dev->backgroundGCs, dev->oldestDirtyGCs,
3198 dev->nObjects, dev->nTnodes);
3200 up(&yaffs_context_lock);
3203 return buf - page < count ? buf - page : count;
3207 * Set the verbosity of the warnings and error messages.
3209 * Note that the names can only be a..z or _ with the current code.
3214 unsigned mask_bitfield;
3216 {"allocate", YAFFS_TRACE_ALLOCATE},
3217 {"always", YAFFS_TRACE_ALWAYS},
3218 {"background", YAFFS_TRACE_BACKGROUND},
3219 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3220 {"buffers", YAFFS_TRACE_BUFFERS},
3221 {"bug", YAFFS_TRACE_BUG},
3222 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3223 {"deletion", YAFFS_TRACE_DELETION},
3224 {"erase", YAFFS_TRACE_ERASE},
3225 {"error", YAFFS_TRACE_ERROR},
3226 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3227 {"gc", YAFFS_TRACE_GC},
3228 {"lock", YAFFS_TRACE_LOCK},
3229 {"mtd", YAFFS_TRACE_MTD},
3230 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3231 {"os", YAFFS_TRACE_OS},
3232 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3233 {"scan", YAFFS_TRACE_SCAN},
3234 {"tracing", YAFFS_TRACE_TRACING},
3235 {"sync", YAFFS_TRACE_SYNC},
3236 {"write", YAFFS_TRACE_WRITE},
3238 {"verify", YAFFS_TRACE_VERIFY},
3239 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3240 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3241 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3243 {"all", 0xffffffff},
3248 #define MAX_MASK_NAME_LENGTH 40
3249 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3250 unsigned long count, void *data)
3252 unsigned rg = 0, mask_bitfield;
3256 char substring[MAX_MASK_NAME_LENGTH + 1];
3262 rg = yaffs_traceMask;
3264 while (!done && (pos < count)) {
3266 while ((pos < count) && isspace(buf[pos]))
3283 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3285 if (end > buf + pos) {
3286 mask_name = "numeral";
3287 len = end - (buf + pos);
3291 for (x = buf + pos, i = 0;
3292 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3293 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3295 substring[i] = '\0';
3297 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3298 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3299 mask_name = mask_flags[i].mask_name;
3300 mask_bitfield = mask_flags[i].mask_bitfield;
3307 if (mask_name != NULL) {
3311 rg &= ~mask_bitfield;
3314 rg |= mask_bitfield;
3320 rg |= mask_bitfield;
3326 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3328 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
3330 if (rg & YAFFS_TRACE_ALWAYS) {
3331 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3333 flag = ((rg & mask_flags[i].mask_bitfield) ==
3334 mask_flags[i].mask_bitfield) ? '+' : '-';
3335 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3343 static int yaffs_proc_write(struct file *file, const char *buf,
3344 unsigned long count, void *data)
3346 return yaffs_proc_write_trace_options(file, buf, count, data);
3349 /* Stuff to handle installation of file systems */
3350 struct file_system_to_install {
3351 struct file_system_type *fst;
3355 static struct file_system_to_install fs_to_install[] = {
3356 {&yaffs_fs_type, 0},
3357 {&yaffs2_fs_type, 0},
3361 static int __init init_yaffs_fs(void)
3364 struct file_system_to_install *fsinst;
3366 T(YAFFS_TRACE_ALWAYS,
3367 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3369 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3370 T(YAFFS_TRACE_ALWAYS,
3371 (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3377 init_MUTEX(&yaffs_context_lock);
3379 /* Install the proc_fs entries */
3380 my_proc_entry = create_proc_entry("yaffs",
3384 if (my_proc_entry) {
3385 my_proc_entry->write_proc = yaffs_proc_write;
3386 my_proc_entry->read_proc = yaffs_proc_read;
3387 my_proc_entry->data = NULL;
3391 debug_proc_entry = create_proc_entry("yaffs_stats",
3395 if (debug_proc_entry) {
3396 debug_proc_entry->write_proc = NULL;
3397 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3398 debug_proc_entry->data = NULL;
3402 /* Now add the file system entries */
3404 fsinst = fs_to_install;
3406 while (fsinst->fst && !error) {
3407 error = register_filesystem(fsinst->fst);
3409 fsinst->installed = 1;
3413 /* Any errors? uninstall */
3415 fsinst = fs_to_install;
3417 while (fsinst->fst) {
3418 if (fsinst->installed) {
3419 unregister_filesystem(fsinst->fst);
3420 fsinst->installed = 0;
3429 static void __exit exit_yaffs_fs(void)
3432 struct file_system_to_install *fsinst;
3434 T(YAFFS_TRACE_ALWAYS,
3435 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3437 remove_proc_entry("yaffs", YPROC_ROOT);
3438 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3440 fsinst = fs_to_install;
3442 while (fsinst->fst) {
3443 if (fsinst->installed) {
3444 unregister_filesystem(fsinst->fst);
3445 fsinst->installed = 0;
3451 module_init(init_yaffs_fs)
3452 module_exit(exit_yaffs_fs)
3454 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3455 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3456 MODULE_LICENSE("GPL");