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,35))
45 #define YAFFS_USE_SETATTR_COPY
47 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
48 #define YAFFS_HAS_EVICT_INODE
51 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
52 #define YAFFS_NEW_FOLLOW_LINK 1
54 #define YAFFS_NEW_FOLLOW_LINK 0
57 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
58 #include <linux/config.h>
61 #include <linux/kernel.h>
62 #include <linux/module.h>
63 #include <linux/slab.h>
64 #include <linux/init.h>
66 #include <linux/proc_fs.h>
67 #include <linux/smp_lock.h>
68 #include <linux/pagemap.h>
69 #include <linux/mtd/mtd.h>
70 #include <linux/interrupt.h>
71 #include <linux/string.h>
72 #include <linux/ctype.h>
74 #if (YAFFS_NEW_FOLLOW_LINK == 1)
75 #include <linux/namei.h>
78 #ifdef YAFFS_COMPILE_EXPORTFS
79 #include <linux/exportfs.h>
82 #ifdef YAFFS_COMPILE_BACKGROUND
83 #include <linux/kthread.h>
84 #include <linux/delay.h>
85 #include <linux/freezer.h>
88 #include <asm/div64.h>
90 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
92 #include <linux/statfs.h>
94 #define UnlockPage(p) unlock_page(p)
95 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
97 /* FIXME: use sb->s_id instead ? */
98 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
102 #include <linux/locks.h>
103 #define BDEVNAME_SIZE 0
104 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
106 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
107 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
113 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
114 #define YPROC_ROOT (&proc_root)
116 #define YPROC_ROOT NULL
119 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
120 #define Y_INIT_TIMER(a) init_timer(a)
122 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
125 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
126 #define WRITE_SIZE_STR "writesize"
127 #define WRITE_SIZE(mtd) ((mtd)->writesize)
129 #define WRITE_SIZE_STR "oobblock"
130 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
133 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
134 #define YAFFS_USE_WRITE_BEGIN_END 1
136 #define YAFFS_USE_WRITE_BEGIN_END 0
139 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
140 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
142 uint64_t result = partition_size;
143 do_div(result, block_size);
144 return (uint32_t)result;
147 #define YCALCBLOCKS(s, b) ((s)/(b))
150 #include <linux/uaccess.h>
151 #include <linux/mtd/mtd.h>
153 #include "yportenv.h"
154 #include "yaffs_trace.h"
155 #include "yaffs_guts.h"
157 #include "yaffs_linux.h"
159 #include "yaffs_mtdif.h"
160 #include "yaffs_mtdif1.h"
161 #include "yaffs_mtdif2.h"
163 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
164 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
165 unsigned int yaffs_auto_checkpoint = 1;
166 unsigned int yaffs_gc_control = 1;
168 /* Module Parameters */
169 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
170 module_param(yaffs_traceMask, uint, 0644);
171 module_param(yaffs_wr_attempts, uint, 0644);
172 module_param(yaffs_auto_checkpoint, uint, 0644);
173 module_param(yaffs_gc_control, uint, 0644);
175 MODULE_PARM(yaffs_traceMask, "i");
176 MODULE_PARM(yaffs_wr_attempts, "i");
177 MODULE_PARM(yaffs_auto_checkpoint, "i");
178 MODULE_PARM(yaffs_gc_control, "i");
181 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
182 /* use iget and read_inode */
183 #define Y_IGET(sb, inum) iget((sb), (inum))
184 static void yaffs_read_inode(struct inode *inode);
187 /* Call local equivalent */
188 #define YAFFS_USE_OWN_IGET
189 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
191 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
194 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
195 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
197 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
200 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
201 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
203 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
204 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
206 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
210 #define update_dir_time(dir) do {\
211 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
214 static void yaffs_put_super(struct super_block *sb);
216 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
218 static ssize_t yaffs_hold_space(struct file *f);
219 static void yaffs_release_space(struct file *f);
221 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
222 static int yaffs_file_flush(struct file *file, fl_owner_t id);
224 static int yaffs_file_flush(struct file *file);
227 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
228 static int yaffs_sync_object(struct file *file, int datasync);
230 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
234 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
236 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
237 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
238 struct nameidata *n);
239 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
240 struct nameidata *n);
242 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
243 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
245 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
246 struct dentry *dentry);
247 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
248 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
249 const char *symname);
250 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
252 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
253 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
256 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
259 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
260 struct inode *new_dir, struct dentry *new_dentry);
261 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
263 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
264 static int yaffs_sync_fs(struct super_block *sb, int wait);
265 static void yaffs_write_super(struct super_block *sb);
267 static int yaffs_sync_fs(struct super_block *sb);
268 static int yaffs_write_super(struct super_block *sb);
271 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
272 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
273 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
274 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
276 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
279 #ifdef YAFFS_HAS_PUT_INODE
280 static void yaffs_put_inode(struct inode *inode);
283 #ifdef YAFFS_HAS_EVICT_INODE
284 static void yaffs_evict_inode(struct inode *);
286 static void yaffs_delete_inode(struct inode *);
287 static void yaffs_clear_inode(struct inode *);
290 static int yaffs_readpage(struct file *file, struct page *page);
291 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
292 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
294 static int yaffs_writepage(struct page *page);
297 #ifdef CONFIG_YAFFS_XATTR
298 int yaffs_setxattr(struct dentry *dentry, const char *name,
299 const void *value, size_t size, int flags);
300 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
302 int yaffs_removexattr(struct dentry *dentry, const char *name);
303 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
307 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
308 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
309 loff_t pos, unsigned len, unsigned flags,
310 struct page **pagep, void **fsdata);
311 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
312 loff_t pos, unsigned len, unsigned copied,
313 struct page *pg, void *fsdadata);
315 static int yaffs_prepare_write(struct file *f, struct page *pg,
316 unsigned offset, unsigned to);
317 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
322 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
324 #if (YAFFS_NEW_FOLLOW_LINK == 1)
325 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
326 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
328 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
331 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev);
333 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
335 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
338 static struct address_space_operations yaffs_file_address_operations = {
339 .readpage = yaffs_readpage,
340 .writepage = yaffs_writepage,
341 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
342 .write_begin = yaffs_write_begin,
343 .write_end = yaffs_write_end,
345 .prepare_write = yaffs_prepare_write,
346 .commit_write = yaffs_commit_write,
351 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
352 static const struct file_operations yaffs_file_operations = {
353 .read = do_sync_read,
354 .write = do_sync_write,
355 .aio_read = generic_file_aio_read,
356 .aio_write = generic_file_aio_write,
357 .mmap = generic_file_mmap,
358 .flush = yaffs_file_flush,
359 .fsync = yaffs_sync_object,
360 .splice_read = generic_file_splice_read,
361 .splice_write = generic_file_splice_write,
362 .llseek = generic_file_llseek,
365 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
367 static const struct file_operations yaffs_file_operations = {
368 .read = do_sync_read,
369 .write = do_sync_write,
370 .aio_read = generic_file_aio_read,
371 .aio_write = generic_file_aio_write,
372 .mmap = generic_file_mmap,
373 .flush = yaffs_file_flush,
374 .fsync = yaffs_sync_object,
375 .sendfile = generic_file_sendfile,
380 static const struct file_operations yaffs_file_operations = {
381 .read = generic_file_read,
382 .write = generic_file_write,
383 .mmap = generic_file_mmap,
384 .flush = yaffs_file_flush,
385 .fsync = yaffs_sync_object,
386 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
387 .sendfile = generic_file_sendfile,
392 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
393 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
395 void * kaddr = kmap_atomic(page, KM_USER0);
396 memset(kaddr + start, 0, end - start);
397 kunmap_atomic(kaddr, KM_USER0);
398 flush_dcache_page(page);
403 static const struct inode_operations yaffs_file_inode_operations = {
404 .setattr = yaffs_setattr,
405 #ifdef CONFIG_YAFFS_XATTR
406 .setxattr = yaffs_setxattr,
407 .getxattr = yaffs_getxattr,
408 .listxattr = yaffs_listxattr,
409 .removexattr = yaffs_removexattr,
413 static const struct inode_operations yaffs_symlink_inode_operations = {
414 .readlink = yaffs_readlink,
415 .follow_link = yaffs_follow_link,
416 #if (YAFFS_NEW_FOLLOW_LINK == 1)
417 .put_link = yaffs_put_link,
419 .setattr = yaffs_setattr,
420 #ifdef CONFIG_YAFFS_XATTR
421 .setxattr = yaffs_setxattr,
422 .getxattr = yaffs_getxattr,
423 .listxattr = yaffs_listxattr,
424 .removexattr = yaffs_removexattr,
428 static const struct inode_operations yaffs_dir_inode_operations = {
429 .create = yaffs_create,
430 .lookup = yaffs_lookup,
432 .unlink = yaffs_unlink,
433 .symlink = yaffs_symlink,
434 .mkdir = yaffs_mkdir,
435 .rmdir = yaffs_unlink,
436 .mknod = yaffs_mknod,
437 .rename = yaffs_rename,
438 .setattr = yaffs_setattr,
439 #ifdef CONFIG_YAFFS_XATTR
440 .setxattr = yaffs_setxattr,
441 .getxattr = yaffs_getxattr,
442 .listxattr = yaffs_listxattr,
443 .removexattr = yaffs_removexattr,
447 static const struct file_operations yaffs_dir_operations = {
448 .read = generic_read_dir,
449 .readdir = yaffs_readdir,
450 .fsync = yaffs_sync_object,
451 .llseek = yaffs_dir_llseek,
454 static const struct super_operations yaffs_super_ops = {
455 .statfs = yaffs_statfs,
457 #ifndef YAFFS_USE_OWN_IGET
458 .read_inode = yaffs_read_inode,
460 #ifdef YAFFS_HAS_PUT_INODE
461 .put_inode = yaffs_put_inode,
463 .put_super = yaffs_put_super,
464 #ifdef YAFFS_HAS_EVICT_INODE
465 .evict_inode = yaffs_evict_inode,
467 .delete_inode = yaffs_delete_inode,
468 .clear_inode = yaffs_clear_inode,
470 .sync_fs = yaffs_sync_fs,
471 .write_super = yaffs_write_super,
475 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
477 #ifdef YAFFS_USE_SETATTR_COPY
478 setattr_copy(inode,attr);
481 return inode_setattr(inode, attr);
486 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
488 return yaffs_gc_control;
491 static void yaffs_GrossLock(yaffs_Device *dev)
493 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
494 down(&(yaffs_DeviceToLC(dev)->grossLock));
495 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
498 static void yaffs_GrossUnlock(yaffs_Device *dev)
500 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
501 up(&(yaffs_DeviceToLC(dev)->grossLock));
504 #ifdef YAFFS_COMPILE_EXPORTFS
506 static struct inode *
507 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
509 return Y_IGET(sb, ino);
512 static struct dentry *
513 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
515 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
518 static struct dentry *
519 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
521 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
524 struct dentry *yaffs2_get_parent(struct dentry *dentry)
527 struct super_block *sb = dentry->d_inode->i_sb;
528 struct dentry *parent = ERR_PTR(-ENOENT);
530 unsigned long parent_ino;
532 yaffs_Object *parent_obj;
534 d_obj = yaffs_InodeToObject(dentry->d_inode);
537 parent_obj = d_obj->parent;
539 parent_ino = yaffs_GetObjectInode(parent_obj);
540 inode = Y_IGET(sb, parent_ino);
543 parent = ERR_CAST(inode);
545 parent = d_obtain_alias(inode);
546 if (!IS_ERR(parent)) {
547 parent = ERR_PTR(-ENOMEM);
557 /* Just declare a zero structure as a NULL value implies
558 * using the default functions of exportfs.
561 static struct export_operations yaffs_export_ops =
563 .fh_to_dentry = yaffs2_fh_to_dentry,
564 .fh_to_parent = yaffs2_fh_to_parent,
565 .get_parent = yaffs2_get_parent,
570 /*-----------------------------------------------------------------*/
571 /* Directory search context allows us to unlock access to yaffs during
572 * filldir without causing problems with the directory being modified.
573 * This is similar to the tried and tested mechanism used in yaffs direct.
575 * A search context iterates along a doubly linked list of siblings in the
576 * directory. If the iterating object is deleted then this would corrupt
577 * the list iteration, likely causing a crash. The search context avoids
578 * this by using the removeObjectCallback to move the search context to the
579 * next object before the object is deleted.
581 * Many readdirs (and thus seach conexts) may be alive simulateously so
582 * each yaffs_Device has a list of these.
584 * A seach context lives for the duration of a readdir.
586 * All these functions must be called while yaffs is locked.
589 struct yaffs_SearchContext {
591 yaffs_Object *dirObj;
592 yaffs_Object *nextReturn;
593 struct ylist_head others;
597 * yaffs_NewSearch() creates a new search context, initialises it and
598 * adds it to the device's search context list.
600 * Called at start of readdir.
602 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
604 yaffs_Device *dev = dir->myDev;
605 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
609 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
610 sc->nextReturn = NULL;
612 sc->nextReturn = ylist_entry(
613 dir->variant.directoryVariant.children.next,
614 yaffs_Object,siblings);
615 YINIT_LIST_HEAD(&sc->others);
616 ylist_add(&sc->others,&(yaffs_DeviceToLC(dev)->searchContexts));
622 * yaffs_EndSearch() disposes of a search context and cleans up.
624 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
627 ylist_del(&sc->others);
633 * yaffs_SearchAdvance() moves a search context to the next object.
634 * Called when the search iterates or when an object removal causes
635 * the search context to be moved to the next object.
637 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
642 if( sc->nextReturn == NULL ||
643 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
644 sc->nextReturn = NULL;
646 struct ylist_head *next = sc->nextReturn->siblings.next;
648 if( next == &sc->dirObj->variant.directoryVariant.children)
649 sc->nextReturn = NULL; /* end of list */
651 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
656 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
657 * We check open search contexts and advance any which are currently
658 * on the object being iterated.
660 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
663 struct ylist_head *i;
664 struct yaffs_SearchContext *sc;
665 struct ylist_head *search_contexts = &(yaffs_DeviceToLC(obj->myDev)->searchContexts);
668 /* Iterate through the directory search contexts.
669 * If any are currently on the object being removed, then advance
670 * the search context to the next object to prevent a hanging pointer.
672 ylist_for_each(i, search_contexts) {
674 sc = ylist_entry(i, struct yaffs_SearchContext,others);
675 if(sc->nextReturn == obj)
676 yaffs_SearchAdvance(sc);
683 /*-----------------------------------------------------------------*/
685 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
688 unsigned char *alias;
691 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
693 yaffs_GrossLock(dev);
695 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
697 yaffs_GrossUnlock(dev);
702 ret = vfs_readlink(dentry, buffer, buflen, alias);
707 #if (YAFFS_NEW_FOLLOW_LINK == 1)
708 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
710 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
713 unsigned char *alias;
715 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
717 yaffs_GrossLock(dev);
719 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
720 yaffs_GrossUnlock(dev);
727 #if (YAFFS_NEW_FOLLOW_LINK == 1)
728 nd_set_link(nd, alias);
733 ret = vfs_follow_link(nd, alias);
740 #if (YAFFS_NEW_FOLLOW_LINK == 1)
741 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) {
746 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
750 * Lookup is used to find objects in the fs
752 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
754 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
757 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
761 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
763 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
765 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
766 yaffs_GrossLock(dev);
769 (TSTR("yaffs_lookup for %d:%s\n"),
770 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
772 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
773 dentry->d_name.name);
775 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
777 /* Can't hold gross lock when calling yaffs_get_inode() */
778 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
779 yaffs_GrossUnlock(dev);
783 (TSTR("yaffs_lookup found %d\n"), obj->objectId));
785 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
789 (TSTR("yaffs_loookup dentry \n")));
790 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
791 * d_add even if NULL inode */
793 /*dget(dentry); // try to solve directory bug */
794 d_add(dentry, inode);
802 T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
806 /* added NCB for 2.5/6 compatability - forces add even if inode is
807 * NULL which creates dentry hash */
808 d_add(dentry, inode);
814 #ifdef YAFFS_HAS_PUT_INODE
816 /* For now put inode is just for debugging
817 * Put inode is called when the inode **structure** is put.
819 static void yaffs_put_inode(struct inode *inode)
822 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
823 atomic_read(&inode->i_count)));
829 static void yaffs_UnstitchObject(struct inode *inode, yaffs_Object *obj)
831 /* Clear the association between the inode and
835 yaffs_InodeToObjectLV(inode) = NULL;
837 /* If the object freeing was deferred, then the real
839 * This should fix the inode inconsistency problem.
841 yaffs_HandleDeferedFree(obj);
844 #ifdef YAFFS_HAS_EVICT_INODE
845 /* yaffs_evict_inode combines into one operation what was previously done in
846 * yaffs_clear_inode() and yaffs_delete_inode()
849 static void yaffs_evict_inode( struct inode *inode)
855 obj = yaffs_InodeToObject(inode);
858 (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
859 atomic_read(&inode->i_count),
860 obj ? "object exists" : "null object"));
862 if (!inode->i_nlink && !is_bad_inode(inode))
864 truncate_inode_pages(&inode->i_data,0);
868 yaffs_GrossLock(dev);
869 yaffs_DeleteObject(obj);
870 yaffs_GrossUnlock(dev);
872 end_writeback(inode);
875 yaffs_GrossLock(dev);
876 yaffs_UnstitchObject(inode,obj);
877 yaffs_GrossUnlock(dev);
884 /* clear is called to tell the fs to release any per-inode data it holds.
885 * The object might still exist on disk and is just being thrown out of the cache
886 * or else the object has actually been deleted and we're being called via
888 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
891 static void yaffs_clear_inode(struct inode *inode)
896 obj = yaffs_InodeToObject(inode);
899 (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
900 atomic_read(&inode->i_count),
901 obj ? "object exists" : "null object"));
905 yaffs_GrossLock(dev);
906 yaffs_UnstitchObject(inode,obj);
907 yaffs_GrossUnlock(dev);
912 /* delete is called when the link count is zero and the inode
913 * is put (ie. nobody wants to know about it anymore, time to
915 * NB Must call clear_inode()
917 static void yaffs_delete_inode(struct inode *inode)
919 yaffs_Object *obj = yaffs_InodeToObject(inode);
923 (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
924 atomic_read(&inode->i_count),
925 obj ? "object exists" : "null object"));
929 yaffs_GrossLock(dev);
930 yaffs_DeleteObject(obj);
931 yaffs_GrossUnlock(dev);
933 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
934 truncate_inode_pages(&inode->i_data, 0);
941 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
942 static int yaffs_file_flush(struct file *file, fl_owner_t id)
944 static int yaffs_file_flush(struct file *file)
947 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
949 yaffs_Device *dev = obj->myDev;
952 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->objectId,
953 obj->dirty ? "dirty" : "clean"));
955 yaffs_GrossLock(dev);
957 yaffs_FlushFile(obj, 1, 0);
959 yaffs_GrossUnlock(dev);
964 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
966 /* Lifted from jffs2 */
969 unsigned char *pg_buf;
975 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
976 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
977 (unsigned)PAGE_CACHE_SIZE));
979 obj = yaffs_DentryToObject(f->f_dentry);
983 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
984 BUG_ON(!PageLocked(pg));
991 /* FIXME: Can kmap fail? */
993 yaffs_GrossLock(dev);
995 ret = yaffs_ReadDataFromFile(obj, pg_buf,
996 pg->index << PAGE_CACHE_SHIFT,
999 yaffs_GrossUnlock(dev);
1005 ClearPageUptodate(pg);
1008 SetPageUptodate(pg);
1012 flush_dcache_page(pg);
1015 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
1019 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1021 int ret = yaffs_readpage_nolock(f, pg);
1026 static int yaffs_readpage(struct file *f, struct page *pg)
1030 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
1031 ret=yaffs_readpage_unlock(f, pg);
1032 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
1036 /* writepage inspired by/stolen from smbfs */
1038 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1039 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1041 static int yaffs_writepage(struct page *page)
1045 struct address_space *mapping = page->mapping;
1046 struct inode *inode;
1047 unsigned long end_index;
1056 inode = mapping->host;
1059 i_size = i_size_read(inode);
1061 end_index = i_size >> PAGE_CACHE_SHIFT;
1063 if(page->index < end_index)
1064 nBytes = PAGE_CACHE_SIZE;
1066 nBytes = i_size & (PAGE_CACHE_SIZE -1);
1068 if (page->index > end_index || !nBytes) {
1070 (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
1071 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1072 (unsigned)inode->i_size));
1074 (TSTR(" -> don't care!!\n")));
1076 zero_user_segment(page,0,PAGE_CACHE_SIZE);
1077 set_page_writeback(page);
1079 end_page_writeback(page);
1084 if(nBytes != PAGE_CACHE_SIZE)
1085 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
1089 buffer = kmap(page);
1091 obj = yaffs_InodeToObject(inode);
1093 yaffs_GrossLock(dev);
1096 (TSTR("yaffs_writepage at %08x, size %08x\n"),
1097 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
1099 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
1100 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1102 nWritten = yaffs_WriteDataToFile(obj, buffer,
1103 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
1105 yaffs_MarkSuperBlockDirty(dev);
1108 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
1109 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1111 yaffs_GrossUnlock(dev);
1114 set_page_writeback(page);
1116 end_page_writeback(page);
1119 return (nWritten == nBytes) ? 0 : -ENOSPC;
1123 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1124 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1125 loff_t pos, unsigned len, unsigned flags,
1126 struct page **pagep, void **fsdata)
1128 struct page *pg = NULL;
1129 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1135 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1136 pg = grab_cache_page_write_begin(mapping, index, flags);
1138 pg = __grab_cache_page(mapping, index);
1147 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
1148 (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
1151 space_held = yaffs_hold_space(filp);
1158 /* Update page if required */
1160 if (!Page_Uptodate(pg))
1161 ret = yaffs_readpage_nolock(filp, pg);
1166 /* Happy path return */
1167 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
1173 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
1175 yaffs_release_space(filp);
1178 page_cache_release(pg);
1185 static int yaffs_prepare_write(struct file *f, struct page *pg,
1186 unsigned offset, unsigned to)
1188 T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
1190 if (!Page_Uptodate(pg))
1191 return yaffs_readpage_nolock(f, pg);
1196 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1197 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1198 loff_t pos, unsigned len, unsigned copied,
1199 struct page *pg, void *fsdadata)
1203 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1206 addr = kva + offset_into_page;
1209 ("yaffs_write_end addr %p pos %x nBytes %d\n",
1210 addr,(unsigned)pos, copied));
1212 ret = yaffs_file_write(filp, addr, copied, &pos);
1214 if (ret != copied) {
1216 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
1225 yaffs_release_space(filp);
1227 page_cache_release(pg);
1232 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1237 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1238 int nBytes = to - offset;
1241 unsigned spos = pos;
1245 addr = kva + offset;
1247 saddr = (unsigned) addr;
1250 (TSTR("yaffs_commit_write addr %x pos %x nBytes %d\n"),
1251 saddr, spos, nBytes));
1253 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1255 if (nWritten != nBytes) {
1257 (TSTR("yaffs_commit_write not same size nWritten %d nBytes %d\n"),
1267 (TSTR("yaffs_commit_write returning %d\n"),
1268 nWritten == nBytes ? 0 : nWritten));
1270 return nWritten == nBytes ? 0 : nWritten;
1275 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1280 /* Check mode against the variant type and attempt to repair if broken. */
1281 __u32 mode = obj->yst_mode;
1282 switch (obj->variantType) {
1283 case YAFFS_OBJECT_TYPE_FILE:
1284 if (!S_ISREG(mode)) {
1285 obj->yst_mode &= ~S_IFMT;
1286 obj->yst_mode |= S_IFREG;
1290 case YAFFS_OBJECT_TYPE_SYMLINK:
1291 if (!S_ISLNK(mode)) {
1292 obj->yst_mode &= ~S_IFMT;
1293 obj->yst_mode |= S_IFLNK;
1297 case YAFFS_OBJECT_TYPE_DIRECTORY:
1298 if (!S_ISDIR(mode)) {
1299 obj->yst_mode &= ~S_IFMT;
1300 obj->yst_mode |= S_IFDIR;
1304 case YAFFS_OBJECT_TYPE_UNKNOWN:
1305 case YAFFS_OBJECT_TYPE_HARDLINK:
1306 case YAFFS_OBJECT_TYPE_SPECIAL:
1312 inode->i_flags |= S_NOATIME;
1314 inode->i_ino = obj->objectId;
1315 inode->i_mode = obj->yst_mode;
1316 inode->i_uid = obj->yst_uid;
1317 inode->i_gid = obj->yst_gid;
1318 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1319 inode->i_blksize = inode->i_sb->s_blocksize;
1321 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1323 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1324 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1325 inode->i_atime.tv_nsec = 0;
1326 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1327 inode->i_mtime.tv_nsec = 0;
1328 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1329 inode->i_ctime.tv_nsec = 0;
1331 inode->i_rdev = obj->yst_rdev;
1332 inode->i_atime = obj->yst_atime;
1333 inode->i_mtime = obj->yst_mtime;
1334 inode->i_ctime = obj->yst_ctime;
1336 inode->i_size = yaffs_GetObjectFileLength(obj);
1337 inode->i_blocks = (inode->i_size + 511) >> 9;
1339 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1342 (TSTR("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n"),
1343 inode->i_mode, inode->i_uid, inode->i_gid,
1344 (int)inode->i_size, atomic_read(&inode->i_count)));
1346 switch (obj->yst_mode & S_IFMT) {
1347 default: /* fifo, device or socket */
1348 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1349 init_special_inode(inode, obj->yst_mode,
1350 old_decode_dev(obj->yst_rdev));
1352 init_special_inode(inode, obj->yst_mode,
1353 (dev_t) (obj->yst_rdev));
1356 case S_IFREG: /* file */
1357 inode->i_op = &yaffs_file_inode_operations;
1358 inode->i_fop = &yaffs_file_operations;
1359 inode->i_mapping->a_ops =
1360 &yaffs_file_address_operations;
1362 case S_IFDIR: /* directory */
1363 inode->i_op = &yaffs_dir_inode_operations;
1364 inode->i_fop = &yaffs_dir_operations;
1366 case S_IFLNK: /* symlink */
1367 inode->i_op = &yaffs_symlink_inode_operations;
1371 yaffs_InodeToObjectLV(inode) = obj;
1373 obj->myInode = inode;
1377 (TSTR("yaffs_FileInode invalid parameters\n")));
1382 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1385 struct inode *inode;
1389 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1396 (TSTR("yaffs_get_inode for NULL object!!\n")));
1402 (TSTR("yaffs_get_inode for object %d\n"), obj->objectId));
1404 inode = Y_IGET(sb, obj->objectId);
1408 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1409 /* iget also increments the inode's i_count */
1410 /* NB You can't be holding grossLock or deadlock will happen! */
1415 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1420 struct inode *inode;
1423 obj = yaffs_DentryToObject(f->f_dentry);
1427 yaffs_GrossLock(dev);
1429 inode = f->f_dentry->d_inode;
1431 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1432 ipos = inode->i_size;
1438 (TSTR("yaffs_file_write: hey obj is null!\n")));
1441 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1442 "to object %d at %d(%x)\n"),
1443 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1445 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1447 yaffs_MarkSuperBlockDirty(dev);
1450 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1451 (unsigned )n,(unsigned)n));
1456 if (ipos > inode->i_size) {
1457 inode->i_size = ipos;
1458 inode->i_blocks = (ipos + 511) >> 9;
1461 (TSTR("yaffs_file_write size updated to %d bytes, "
1463 ipos, (int)(inode->i_blocks)));
1467 yaffs_GrossUnlock(dev);
1468 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1471 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1472 /* For now we just assume few parallel writes and check against a small number. */
1473 /* Todo: need to do this with a counter to handle parallel reads better */
1475 static ssize_t yaffs_hold_space(struct file *f)
1483 obj = yaffs_DentryToObject(f->f_dentry);
1487 yaffs_GrossLock(dev);
1489 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1491 yaffs_GrossUnlock(dev);
1493 return (nFreeChunks > 20) ? 1 : 0;
1496 static void yaffs_release_space(struct file *f)
1502 obj = yaffs_DentryToObject(f->f_dentry);
1506 yaffs_GrossLock(dev);
1509 yaffs_GrossUnlock(dev);
1513 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1521 offset += i_size_read(file->f_path.dentry->d_inode);
1524 offset += file->f_pos;
1529 if (offset != file->f_pos)
1530 file->f_pos = offset;
1539 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1543 struct yaffs_SearchContext *sc;
1544 struct inode *inode = f->f_dentry->d_inode;
1545 unsigned long offset, curoffs;
1549 char name[YAFFS_MAX_NAME_LENGTH + 1];
1551 obj = yaffs_DentryToObject(f->f_dentry);
1554 yaffs_GrossLock(dev);
1556 yaffs_DeviceToLC(dev)->readdirProcess = current;
1560 sc = yaffs_NewSearch(obj);
1566 T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1570 (TSTR("yaffs_readdir: entry . ino %d \n"),
1571 (int)inode->i_ino));
1572 yaffs_GrossUnlock(dev);
1573 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0){
1574 yaffs_GrossLock(dev);
1577 yaffs_GrossLock(dev);
1583 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1584 (int)f->f_dentry->d_parent->d_inode->i_ino));
1585 yaffs_GrossUnlock(dev);
1586 if (filldir(dirent, "..", 2, offset,
1587 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
1588 yaffs_GrossLock(dev);
1591 yaffs_GrossLock(dev);
1598 /* If the directory has changed since the open or last call to
1599 readdir, rewind to after the 2 canned entries. */
1600 if (f->f_version != inode->i_version) {
1603 f->f_version = inode->i_version;
1606 while(sc->nextReturn){
1609 if (curoffs >= offset) {
1610 int this_inode = yaffs_GetObjectInode(l);
1611 int this_type = yaffs_GetObjectType(l);
1613 yaffs_GetObjectName(l, name,
1614 YAFFS_MAX_NAME_LENGTH + 1);
1616 (TSTR("yaffs_readdir: %s inode %d\n"),
1617 name, yaffs_GetObjectInode(l)));
1619 yaffs_GrossUnlock(dev);
1627 yaffs_GrossLock(dev);
1631 yaffs_GrossLock(dev);
1636 yaffs_SearchAdvance(sc);
1640 yaffs_EndSearch(sc);
1641 yaffs_DeviceToLC(dev)->readdirProcess = NULL;
1642 yaffs_GrossUnlock(dev);
1650 * File creation. Allocate an inode, and we're done..
1653 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1656 #define YCRED(x) (x->cred)
1659 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1660 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1663 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1667 struct inode *inode;
1669 yaffs_Object *obj = NULL;
1672 yaffs_Object *parent = yaffs_InodeToObject(dir);
1674 int error = -ENOSPC;
1675 uid_t uid = YCRED(current)->fsuid;
1676 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1678 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1683 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1684 parent->objectId, parent->variantType));
1687 (TSTR("yaffs_mknod: could not get parent object\n")));
1691 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1692 "mode %x dev %x\n"),
1693 dentry->d_name.name, mode, rdev));
1695 dev = parent->myDev;
1697 yaffs_GrossLock(dev);
1699 switch (mode & S_IFMT) {
1701 /* Special (socket, fifo, device...) */
1702 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1703 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1704 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1705 gid, old_encode_dev(rdev));
1707 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1711 case S_IFREG: /* file */
1712 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1713 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1716 case S_IFDIR: /* directory */
1718 (TSTR("yaffs_mknod: making directory\n")));
1719 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1722 case S_IFLNK: /* symlink */
1723 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1724 obj = NULL; /* Do we ever get here? */
1728 /* Can not call yaffs_get_inode() with gross lock held */
1729 yaffs_GrossUnlock(dev);
1732 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1733 d_instantiate(dentry, inode);
1734 update_dir_time(dir);
1736 (TSTR("yaffs_mknod created object %d count = %d\n"),
1737 obj->objectId, atomic_read(&inode->i_count)));
1739 yaffs_FillInodeFromObject(dir,parent);
1742 (TSTR("yaffs_mknod failed making object\n")));
1749 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1752 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1753 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1757 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1758 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1759 struct nameidata *n)
1761 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1764 T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1765 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1768 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1776 (TSTR("yaffs_unlink %d:%s\n"),
1778 dentry->d_name.name));
1779 obj = yaffs_InodeToObject(dir);
1782 yaffs_GrossLock(dev);
1784 retVal = yaffs_Unlink(obj, dentry->d_name.name);
1786 if (retVal == YAFFS_OK) {
1787 dentry->d_inode->i_nlink--;
1789 yaffs_GrossUnlock(dev);
1790 mark_inode_dirty(dentry->d_inode);
1791 update_dir_time(dir);
1794 yaffs_GrossUnlock(dev);
1801 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1802 struct dentry *dentry)
1804 struct inode *inode = old_dentry->d_inode;
1805 yaffs_Object *obj = NULL;
1806 yaffs_Object *link = NULL;
1809 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1811 obj = yaffs_InodeToObject(inode);
1814 yaffs_GrossLock(dev);
1816 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1817 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1821 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1822 d_instantiate(dentry, old_dentry->d_inode);
1823 atomic_inc(&old_dentry->d_inode->i_count);
1825 (TSTR("yaffs_link link count %d i_count %d\n"),
1826 old_dentry->d_inode->i_nlink,
1827 atomic_read(&old_dentry->d_inode->i_count)));
1830 yaffs_GrossUnlock(dev);
1833 update_dir_time(dir);
1840 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1841 const char *symname)
1845 uid_t uid = YCRED(current)->fsuid;
1846 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1848 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1850 dev = yaffs_InodeToObject(dir)->myDev;
1851 yaffs_GrossLock(dev);
1852 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1853 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1854 yaffs_GrossUnlock(dev);
1857 struct inode *inode;
1859 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1860 d_instantiate(dentry, inode);
1861 update_dir_time(dir);
1862 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1865 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1871 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1872 static int yaffs_sync_object(struct file *file, int datasync)
1874 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1881 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1882 struct dentry *dentry = file->f_path.dentry;
1885 obj = yaffs_DentryToObject(dentry);
1889 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1890 (TSTR("yaffs_sync_object\n")));
1891 yaffs_GrossLock(dev);
1892 yaffs_FlushFile(obj, 1, datasync);
1893 yaffs_GrossUnlock(dev);
1898 * The VFS layer already does all the dentry stuff for rename.
1900 * NB: POSIX says you can rename an object over an old object of the same name
1902 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1903 struct inode *new_dir, struct dentry *new_dentry)
1906 int retVal = YAFFS_FAIL;
1907 yaffs_Object *target;
1909 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1910 dev = yaffs_InodeToObject(old_dir)->myDev;
1912 yaffs_GrossLock(dev);
1914 /* Check if the target is an existing directory that is not empty. */
1915 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1916 new_dentry->d_name.name);
1920 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1921 !ylist_empty(&target->variant.directoryVariant.children)) {
1923 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1925 retVal = YAFFS_FAIL;
1927 /* Now does unlinking internally using shadowing mechanism */
1928 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_RenameObject\n")));
1930 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1931 old_dentry->d_name.name,
1932 yaffs_InodeToObject(new_dir),
1933 new_dentry->d_name.name);
1935 yaffs_GrossUnlock(dev);
1937 if (retVal == YAFFS_OK) {
1939 new_dentry->d_inode->i_nlink--;
1940 mark_inode_dirty(new_dentry->d_inode);
1943 update_dir_time(old_dir);
1944 if(old_dir != new_dir)
1945 update_dir_time(new_dir);
1952 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1954 struct inode *inode = dentry->d_inode;
1959 (TSTR("yaffs_setattr of object %d\n"),
1960 yaffs_InodeToObject(inode)->objectId));
1962 /* Fail if a requested resize >= 2GB */
1963 if (attr->ia_valid & ATTR_SIZE &&
1964 (attr->ia_size >> 31))
1968 error = inode_change_ok(inode, attr);
1972 error = yaffs_vfs_setattr(inode, attr);
1973 T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
1974 if (attr->ia_valid & ATTR_SIZE)
1975 truncate_inode_pages(&inode->i_data,attr->ia_size);
1977 dev = yaffs_InodeToObject(inode)->myDev;
1978 if (attr->ia_valid & ATTR_SIZE){
1979 T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
1980 (int)(attr->ia_size),(int)(attr->ia_size)));
1982 yaffs_GrossLock(dev);
1983 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1984 if(result == YAFFS_OK) {
1989 yaffs_GrossUnlock(dev);
1994 (TSTR("yaffs_setattr done returning %d\n"),error));
1999 #ifdef CONFIG_YAFFS_XATTR
2000 int yaffs_setxattr(struct dentry *dentry, const char *name,
2001 const void *value, size_t size, int flags)
2003 struct inode *inode = dentry->d_inode;
2006 yaffs_Object *obj = yaffs_InodeToObject(inode);
2009 (TSTR("yaffs_setxattr of object %d\n"),
2016 yaffs_GrossLock(dev);
2017 result = yaffs_SetXAttribute(obj, name, value, size, flags);
2018 if(result == YAFFS_OK)
2022 yaffs_GrossUnlock(dev);
2026 (TSTR("yaffs_setxattr done returning %d\n"),error));
2032 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
2035 struct inode *inode = dentry->d_inode;
2038 yaffs_Object *obj = yaffs_InodeToObject(inode);
2041 (TSTR("yaffs_getxattr of object %d\n"),
2047 yaffs_GrossLock(dev);
2048 error = yaffs_GetXAttribute(obj, name, buff, size);
2049 yaffs_GrossUnlock(dev);
2053 (TSTR("yaffs_getxattr done returning %d\n"),error));
2058 int yaffs_removexattr(struct dentry *dentry, const char *name)
2060 struct inode *inode = dentry->d_inode;
2063 yaffs_Object *obj = yaffs_InodeToObject(inode);
2066 (TSTR("yaffs_removexattr of object %d\n"),
2073 yaffs_GrossLock(dev);
2074 result = yaffs_RemoveXAttribute(obj, name);
2075 if(result == YAFFS_OK)
2079 yaffs_GrossUnlock(dev);
2083 (TSTR("yaffs_removexattr done returning %d\n"),error));
2088 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
2090 struct inode *inode = dentry->d_inode;
2093 yaffs_Object *obj = yaffs_InodeToObject(inode);
2096 (TSTR("yaffs_listxattr of object %d\n"),
2102 yaffs_GrossLock(dev);
2103 error = yaffs_ListXAttributes(obj, buff, size);
2104 yaffs_GrossUnlock(dev);
2108 (TSTR("yaffs_listxattr done returning %d\n"),error));
2116 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2117 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2119 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2120 struct super_block *sb = dentry->d_sb;
2121 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2122 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2124 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2126 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2128 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2131 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
2133 yaffs_GrossLock(dev);
2135 buf->f_type = YAFFS_MAGIC;
2136 buf->f_bsize = sb->s_blocksize;
2137 buf->f_namelen = 255;
2139 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
2140 /* Do this if chunk size is not a power of 2 */
2142 uint64_t bytesInDev;
2145 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
2146 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
2148 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
2149 buf->f_blocks = bytesInDev;
2151 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
2152 ((uint64_t)(dev->nDataBytesPerChunk));
2154 do_div(bytesFree, sb->s_blocksize);
2156 buf->f_bfree = bytesFree;
2158 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2161 (dev->param.endBlock - dev->param.startBlock + 1) *
2162 dev->param.nChunksPerBlock /
2163 (sb->s_blocksize / dev->nDataBytesPerChunk);
2165 yaffs_GetNumberOfFreeChunks(dev) /
2166 (sb->s_blocksize / dev->nDataBytesPerChunk);
2169 (dev->param.endBlock - dev->param.startBlock + 1) *
2170 dev->param.nChunksPerBlock *
2171 (dev->nDataBytesPerChunk / sb->s_blocksize);
2174 yaffs_GetNumberOfFreeChunks(dev) *
2175 (dev->nDataBytesPerChunk / sb->s_blocksize);
2180 buf->f_bavail = buf->f_bfree;
2182 yaffs_GrossUnlock(dev);
2188 static void yaffs_FlushInodes(struct super_block *sb)
2193 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
2194 obj = yaffs_InodeToObject(iptr);
2196 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
2198 yaffs_FlushFile(obj,1,0);
2204 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
2206 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2210 yaffs_FlushInodes(sb);
2211 yaffs_UpdateDirtyDirectories(dev);
2212 yaffs_FlushEntireDeviceCache(dev);
2214 yaffs_CheckpointSave(dev);
2218 static unsigned yaffs_bg_gc_urgency(yaffs_Device *dev)
2220 unsigned erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2221 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2222 unsigned scatteredFree = 0; /* Free chunks not in an erased block */
2224 if(erasedChunks < dev->nFreeChunks)
2225 scatteredFree = (dev->nFreeChunks - erasedChunks);
2227 if(!context->bgRunning)
2229 else if(scatteredFree < (dev->param.nChunksPerBlock * 2))
2231 else if(erasedChunks > dev->nFreeChunks/2)
2233 else if(erasedChunks > dev->nFreeChunks/4)
2239 static int yaffs_do_sync_fs(struct super_block *sb,
2240 int request_checkpoint)
2243 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2244 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2245 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2248 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2249 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
2251 sb->s_dirt ? "dirty" : "clean",
2252 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2253 oneshot_checkpoint ? " one-shot" : "" ));
2255 yaffs_GrossLock(dev);
2256 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2257 oneshot_checkpoint) &&
2258 !dev->isCheckpointed;
2260 if (sb->s_dirt || do_checkpoint) {
2261 yaffs_FlushSuperBlock(sb, !dev->isCheckpointed && do_checkpoint);
2263 if(oneshot_checkpoint)
2264 yaffs_auto_checkpoint &= ~4;
2266 yaffs_GrossUnlock(dev);
2272 * yaffs background thread functions .
2273 * yaffs_BackgroundThread() the thread function
2274 * yaffs_BackgroundStart() launches the background thread.
2275 * yaffs_BackgroundStop() cleans up the background thread.
2278 * The thread should only run after the yaffs is initialised
2279 * The thread should be stopped before yaffs is unmounted.
2280 * The thread should not do any writing while the fs is in read only.
2283 #ifdef YAFFS_COMPILE_BACKGROUND
2285 void yaffs_background_waker(unsigned long data)
2287 wake_up_process((struct task_struct *)data);
2290 static int yaffs_BackgroundThread(void *data)
2292 yaffs_Device *dev = (yaffs_Device *)data;
2293 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2294 unsigned long now = jiffies;
2295 unsigned long next_dir_update = now;
2296 unsigned long next_gc = now;
2297 unsigned long expires;
2298 unsigned int urgency;
2301 struct timer_list timer;
2303 T(YAFFS_TRACE_BACKGROUND,
2304 (TSTR("yaffs_background starting for dev %p\n"),
2309 while(context->bgRunning){
2310 T(YAFFS_TRACE_BACKGROUND,
2311 (TSTR("yaffs_background\n")));
2313 if(kthread_should_stop())
2319 yaffs_GrossLock(dev);
2323 if(time_after(now, next_dir_update)){
2324 yaffs_UpdateDirtyDirectories(dev);
2325 next_dir_update = now + HZ;
2328 if(time_after(now,next_gc)){
2329 if(!dev->isCheckpointed){
2330 urgency = yaffs_bg_gc_urgency(dev);
2331 gcResult = yaffs_BackgroundGarbageCollect(dev, urgency);
2333 next_gc = now + HZ/20+1;
2334 else if(urgency > 0)
2335 next_gc = now + HZ/10+1;
2337 next_gc = now + HZ * 2;
2339 * gc not running so set to next_dir_update
2340 * to cut down on wake ups
2342 next_gc = next_dir_update;
2344 yaffs_GrossUnlock(dev);
2346 expires = next_dir_update;
2347 if (time_before(next_gc,expires))
2349 if(time_before(expires,now))
2352 Y_INIT_TIMER(&timer);
2353 timer.expires = expires+1;
2354 timer.data = (unsigned long) current;
2355 timer.function = yaffs_background_waker;
2357 set_current_state(TASK_INTERRUPTIBLE);
2360 del_timer_sync(&timer);
2369 static int yaffs_BackgroundStart(yaffs_Device *dev)
2372 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2377 context->bgRunning = 1;
2379 context->bgThread = kthread_run(yaffs_BackgroundThread,
2380 (void *)dev,"yaffs-bg-%d",context->mount_id);
2382 if(IS_ERR(context->bgThread)){
2383 retval = PTR_ERR(context->bgThread);
2384 context->bgThread = NULL;
2385 context->bgRunning = 0;
2390 static void yaffs_BackgroundStop(yaffs_Device *dev)
2392 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToLC(dev);
2394 ctxt->bgRunning = 0;
2396 if( ctxt->bgThread){
2397 kthread_stop(ctxt->bgThread);
2398 ctxt->bgThread = NULL;
2402 static int yaffs_BackgroundThread(void *data)
2407 static int yaffs_BackgroundStart(yaffs_Device *dev)
2412 static void yaffs_BackgroundStop(yaffs_Device *dev)
2418 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2419 static void yaffs_write_super(struct super_block *sb)
2421 static int yaffs_write_super(struct super_block *sb)
2424 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2426 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2427 (TSTR("yaffs_write_super%s\n"),
2428 request_checkpoint ? " checkpt" : ""));
2430 yaffs_do_sync_fs(sb, request_checkpoint);
2432 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2438 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2439 static int yaffs_sync_fs(struct super_block *sb, int wait)
2441 static int yaffs_sync_fs(struct super_block *sb)
2444 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2446 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2447 (TSTR("yaffs_sync_fs%s\n"),
2448 request_checkpoint ? " checkpt" : ""));
2450 yaffs_do_sync_fs(sb, request_checkpoint);
2455 #ifdef YAFFS_USE_OWN_IGET
2457 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2459 struct inode *inode;
2461 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2464 (TSTR("yaffs_iget for %lu\n"), ino));
2466 inode = iget_locked(sb, ino);
2468 return ERR_PTR(-ENOMEM);
2469 if (!(inode->i_state & I_NEW))
2472 /* NB This is called as a side effect of other functions, but
2473 * we had to release the lock to prevent deadlocks, so
2474 * need to lock again.
2477 yaffs_GrossLock(dev);
2479 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2481 yaffs_FillInodeFromObject(inode, obj);
2483 yaffs_GrossUnlock(dev);
2485 unlock_new_inode(inode);
2491 static void yaffs_read_inode(struct inode *inode)
2493 /* NB This is called as a side effect of other functions, but
2494 * we had to release the lock to prevent deadlocks, so
2495 * need to lock again.
2499 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2502 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2504 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2505 yaffs_GrossLock(dev);
2507 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2509 yaffs_FillInodeFromObject(inode, obj);
2511 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2512 yaffs_GrossUnlock(dev);
2517 static YLIST_HEAD(yaffs_context_list);
2518 struct semaphore yaffs_context_lock;
2520 static void yaffs_put_super(struct super_block *sb)
2522 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2524 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2526 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2527 (TSTR("Shutting down yaffs background thread\n")));
2528 yaffs_BackgroundStop(dev);
2529 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2530 (TSTR("yaffs background thread shut down\n")));
2532 yaffs_GrossLock(dev);
2534 yaffs_FlushSuperBlock(sb,1);
2536 if (yaffs_DeviceToLC(dev)->putSuperFunc)
2537 yaffs_DeviceToLC(dev)->putSuperFunc(sb);
2540 yaffs_Deinitialise(dev);
2542 yaffs_GrossUnlock(dev);
2544 down(&yaffs_context_lock);
2545 ylist_del_init(&(yaffs_DeviceToLC(dev)->contextList));
2546 up(&yaffs_context_lock);
2548 if (yaffs_DeviceToLC(dev)->spareBuffer) {
2549 YFREE(yaffs_DeviceToLC(dev)->spareBuffer);
2550 yaffs_DeviceToLC(dev)->spareBuffer = NULL;
2557 static void yaffs_MTDPutSuper(struct super_block *sb)
2559 struct mtd_info *mtd = yaffs_DeviceToMtd(yaffs_SuperToDevice(sb));
2564 put_mtd_device(mtd);
2568 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2570 struct super_block *sb = yaffs_DeviceToLC(dev)->superBlock;
2572 T(YAFFS_TRACE_OS, (TSTR("yaffs_MarkSuperBlockDirty() sb = %p\n"), sb));
2579 int skip_checkpoint_read;
2580 int skip_checkpoint_write;
2583 int tags_ecc_overridden;
2584 int lazy_loading_enabled;
2585 int lazy_loading_overridden;
2586 int empty_lost_and_found;
2587 int empty_lost_and_found_overridden;
2590 #define MAX_OPT_LEN 30
2591 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2593 char cur_opt[MAX_OPT_LEN + 1];
2597 /* Parse through the options which is a comma seperated list */
2599 while (options_str && *options_str && !error) {
2600 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2603 while(*options_str == ',')
2606 while (*options_str && *options_str != ',') {
2607 if (p < MAX_OPT_LEN) {
2608 cur_opt[p] = *options_str;
2614 if (!strcmp(cur_opt, "inband-tags"))
2615 options->inband_tags = 1;
2616 else if (!strcmp(cur_opt, "tags-ecc-off")){
2617 options->tags_ecc_on = 0;
2618 options->tags_ecc_overridden=1;
2619 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2620 options->tags_ecc_on = 1;
2621 options->tags_ecc_overridden = 1;
2622 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2623 options->lazy_loading_enabled = 0;
2624 options->lazy_loading_overridden=1;
2625 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2626 options->lazy_loading_enabled = 1;
2627 options->lazy_loading_overridden = 1;
2628 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2629 options->empty_lost_and_found = 0;
2630 options->empty_lost_and_found_overridden=1;
2631 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2632 options->empty_lost_and_found = 1;
2633 options->empty_lost_and_found_overridden=1;
2634 } else if (!strcmp(cur_opt, "no-cache"))
2635 options->no_cache = 1;
2636 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2637 options->skip_checkpoint_read = 1;
2638 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2639 options->skip_checkpoint_write = 1;
2640 else if (!strcmp(cur_opt, "no-checkpoint")) {
2641 options->skip_checkpoint_read = 1;
2642 options->skip_checkpoint_write = 1;
2644 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2653 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2654 struct super_block *sb,
2655 void *data, int silent)
2658 struct inode *inode = NULL;
2659 struct dentry *root;
2660 yaffs_Device *dev = 0;
2661 char devname_buf[BDEVNAME_SIZE + 1];
2662 struct mtd_info *mtd;
2664 char *data_str = (char *)data;
2665 struct yaffs_LinuxContext *context = NULL;
2666 yaffs_DeviceParam *param;
2670 yaffs_options options;
2674 struct yaffs_LinuxContext *context_iterator;
2675 struct ylist_head *l;
2677 sb->s_magic = YAFFS_MAGIC;
2678 sb->s_op = &yaffs_super_ops;
2679 sb->s_flags |= MS_NOATIME;
2681 readOnly =((sb->s_flags & MS_RDONLY) != 0);
2684 #ifdef YAFFS_COMPILE_EXPORTFS
2685 sb->s_export_op = &yaffs_export_ops;
2689 printk(KERN_INFO "yaffs: sb is NULL\n");
2690 else if (!sb->s_dev)
2691 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2692 else if (!yaffs_devname(sb, devname_buf))
2693 printk(KERN_INFO "yaffs: devname is NULL\n");
2695 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2697 yaffs_devname(sb, devname_buf),
2698 readOnly ? "ro" : "rw");
2703 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2705 memset(&options, 0, sizeof(options));
2707 if (yaffs_parse_options(&options, data_str)) {
2708 /* Option parsing failed */
2713 sb->s_blocksize = PAGE_CACHE_SIZE;
2714 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2717 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffsVersion));
2719 (TSTR("yaffs_read_super: block size %d\n"),
2720 (int)(sb->s_blocksize)));
2722 T(YAFFS_TRACE_ALWAYS,
2723 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2724 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2725 yaffs_devname(sb, devname_buf)));
2727 /* Check it's an mtd device..... */
2728 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2729 return NULL; /* This isn't an mtd device */
2731 /* Get the device */
2732 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2734 T(YAFFS_TRACE_ALWAYS,
2735 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2739 /* Check it's NAND */
2740 if (mtd->type != MTD_NANDFLASH) {
2741 T(YAFFS_TRACE_ALWAYS,
2742 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2747 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2748 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2749 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2750 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2751 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2752 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2753 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2754 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2755 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2756 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2757 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2758 T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2760 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2763 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2765 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2766 T(YAFFS_TRACE_ALWAYS,
2767 (TSTR("yaffs: auto selecting yaffs2\n")));
2771 /* Added NCB 26/5/2006 for completeness */
2772 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2773 T(YAFFS_TRACE_ALWAYS,
2774 (TSTR("yaffs: auto selecting yaffs1\n")));
2780 if (yaffsVersion == 2) {
2781 /* Check for version 2 style functions */
2783 !mtd->block_isbad ||
2784 !mtd->block_markbad ||
2787 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2788 !mtd->read_oob || !mtd->write_oob) {
2791 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2793 T(YAFFS_TRACE_ALWAYS,
2794 (TSTR("yaffs: MTD device does not support required "
2799 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2800 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2801 !options.inband_tags) {
2802 T(YAFFS_TRACE_ALWAYS,
2803 (TSTR("yaffs: MTD device does not have the "
2804 "right page sizes\n")));
2808 /* Check for V1 style functions */
2812 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2813 !mtd->read_oob || !mtd->write_oob) {
2816 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2818 T(YAFFS_TRACE_ALWAYS,
2819 (TSTR("yaffs: MTD device does not support required "
2824 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2825 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2826 T(YAFFS_TRACE_ALWAYS,
2827 (TSTR("yaffs: MTD device does not support have the "
2828 "right page sizes\n")));
2833 /* OK, so if we got here, we have an MTD that's NAND and looks
2834 * like it has the right capabilities
2835 * Set the yaffs_Device up for mtd
2838 if (!readOnly && !(mtd->flags & MTD_WRITEABLE)){
2840 printk(KERN_INFO "yaffs: mtd is read only, setting superblock read only");
2841 sb->s_flags |= MS_RDONLY;
2844 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2845 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2847 if(!dev || !context ){
2857 /* Deep shit could not allocate device structure */
2858 T(YAFFS_TRACE_ALWAYS,
2859 (TSTR("yaffs_read_super: Failed trying to allocate "
2860 "yaffs_Device. \n")));
2863 memset(dev, 0, sizeof(yaffs_Device));
2864 param = &(dev->param);
2866 memset(context,0,sizeof(struct yaffs_LinuxContext));
2867 dev->osContext = context;
2868 YINIT_LIST_HEAD(&(context->contextList));
2870 context->superBlock = sb;
2872 dev->readOnly = readOnly;
2874 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2875 sb->s_fs_info = dev;
2877 sb->u.generic_sbp = dev;
2880 dev->driverContext = mtd;
2881 param->name = mtd->name;
2883 /* Set up the memory size parameters.... */
2885 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2887 param->startBlock = 0;
2888 param->endBlock = nBlocks - 1;
2889 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2890 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2891 param->nReservedBlocks = 5;
2892 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2893 param->inbandTags = options.inband_tags;
2895 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2896 param->disableLazyLoad = 1;
2898 #ifdef CONFIG_YAFFS_XATTR
2899 param->enableXattr = 1;
2901 if(options.lazy_loading_overridden)
2902 param->disableLazyLoad = !options.lazy_loading_enabled;
2904 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2905 param->noTagsECC = 1;
2908 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2910 param->deferDirectoryUpdate = 1;
2913 if(options.tags_ecc_overridden)
2914 param->noTagsECC = !options.tags_ecc_on;
2916 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2917 param->emptyLostAndFound = 1;
2920 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2921 param->refreshPeriod = 0;
2923 param->refreshPeriod = 500;
2926 if(options.empty_lost_and_found_overridden)
2927 param->emptyLostAndFound = options.empty_lost_and_found;
2929 /* ... and the functions. */
2930 if (yaffsVersion == 2) {
2931 param->writeChunkWithTagsToNAND =
2932 nandmtd2_WriteChunkWithTagsToNAND;
2933 param->readChunkWithTagsFromNAND =
2934 nandmtd2_ReadChunkWithTagsFromNAND;
2935 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2936 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2937 yaffs_DeviceToLC(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2938 param->isYaffs2 = 1;
2939 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2940 param->totalBytesPerChunk = mtd->writesize;
2941 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2943 param->totalBytesPerChunk = mtd->oobblock;
2944 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2946 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2948 param->startBlock = 0;
2949 param->endBlock = nBlocks - 1;
2951 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2952 /* use the MTD interface in yaffs_mtdif1.c */
2953 param->writeChunkWithTagsToNAND =
2954 nandmtd1_WriteChunkWithTagsToNAND;
2955 param->readChunkWithTagsFromNAND =
2956 nandmtd1_ReadChunkWithTagsFromNAND;
2957 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2958 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2960 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2961 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2963 param->isYaffs2 = 0;
2965 /* ... and common functions */
2966 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2967 param->initialiseNAND = nandmtd_InitialiseNAND;
2969 yaffs_DeviceToLC(dev)->putSuperFunc = yaffs_MTDPutSuper;
2971 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2972 param->gcControl = yaffs_gc_control_callback;
2974 yaffs_DeviceToLC(dev)->superBlock= sb;
2977 #ifndef CONFIG_YAFFS_DOES_ECC
2978 param->useNANDECC = 1;
2981 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2982 param->wideTnodesDisabled = 1;
2985 param->skipCheckpointRead = options.skip_checkpoint_read;
2986 param->skipCheckpointWrite = options.skip_checkpoint_write;
2988 down(&yaffs_context_lock);
2989 /* Get a mount id */
2991 for(mount_id=0; ! found; mount_id++){
2993 ylist_for_each(l,&yaffs_context_list){
2994 context_iterator = ylist_entry(l,struct yaffs_LinuxContext,contextList);
2995 if(context_iterator->mount_id == mount_id)
2999 context->mount_id = mount_id;
3001 ylist_add_tail(&(yaffs_DeviceToLC(dev)->contextList), &yaffs_context_list);
3002 up(&yaffs_context_lock);
3004 /* Directory search handling...*/
3005 YINIT_LIST_HEAD(&(yaffs_DeviceToLC(dev)->searchContexts));
3006 param->removeObjectCallback = yaffs_RemoveObjectCallback;
3008 init_MUTEX(&(yaffs_DeviceToLC(dev)->grossLock));
3010 yaffs_GrossLock(dev);
3012 err = yaffs_GutsInitialise(dev);
3015 (TSTR("yaffs_read_super: guts initialised %s\n"),
3016 (err == YAFFS_OK) ? "OK" : "FAILED"));
3019 yaffs_BackgroundStart(dev);
3021 if(!context->bgThread)
3022 param->deferDirectoryUpdate = 0;
3025 /* Release lock before yaffs_get_inode() */
3026 yaffs_GrossUnlock(dev);
3028 /* Create root inode */
3029 if (err == YAFFS_OK)
3030 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3036 inode->i_op = &yaffs_dir_inode_operations;
3037 inode->i_fop = &yaffs_dir_operations;
3039 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
3041 root = d_alloc_root(inode);
3043 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
3050 sb->s_dirt = !dev->isCheckpointed;
3051 T(YAFFS_TRACE_ALWAYS,
3052 (TSTR("yaffs_read_super: isCheckpointed %d\n"),
3053 dev->isCheckpointed));
3055 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
3060 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3061 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3064 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3067 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3068 static int yaffs_read_super(struct file_system_type *fs,
3069 int flags, const char *dev_name,
3070 void *data, struct vfsmount *mnt)
3073 return get_sb_bdev(fs, flags, dev_name, data,
3074 yaffs_internal_read_super_mtd, mnt);
3077 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3078 int flags, const char *dev_name,
3082 return get_sb_bdev(fs, flags, dev_name, data,
3083 yaffs_internal_read_super_mtd);
3087 static struct file_system_type yaffs_fs_type = {
3088 .owner = THIS_MODULE,
3090 .get_sb = yaffs_read_super,
3091 .kill_sb = kill_block_super,
3092 .fs_flags = FS_REQUIRES_DEV,
3095 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3098 return yaffs_internal_read_super(1, sb, data, silent);
3101 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3106 #ifdef CONFIG_YAFFS_YAFFS2
3108 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3109 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3112 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3115 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3116 static int yaffs2_read_super(struct file_system_type *fs,
3117 int flags, const char *dev_name, void *data,
3118 struct vfsmount *mnt)
3120 return get_sb_bdev(fs, flags, dev_name, data,
3121 yaffs2_internal_read_super_mtd, mnt);
3124 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3125 int flags, const char *dev_name,
3129 return get_sb_bdev(fs, flags, dev_name, data,
3130 yaffs2_internal_read_super_mtd);
3134 static struct file_system_type yaffs2_fs_type = {
3135 .owner = THIS_MODULE,
3137 .get_sb = yaffs2_read_super,
3138 .kill_sb = kill_block_super,
3139 .fs_flags = FS_REQUIRES_DEV,
3142 static struct super_block *yaffs2_read_super(struct super_block *sb,
3143 void *data, int silent)
3145 return yaffs_internal_read_super(2, sb, data, silent);
3148 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3152 #endif /* CONFIG_YAFFS_YAFFS2 */
3154 static struct proc_dir_entry *my_proc_entry;
3155 static struct proc_dir_entry *debug_proc_entry;
3157 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
3159 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
3160 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
3161 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
3162 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
3163 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
3164 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
3165 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
3166 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
3167 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
3168 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
3169 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
3170 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
3172 buf += sprintf(buf, "\n");
3178 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
3180 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3181 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3182 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3183 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3184 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3185 buf += sprintf(buf, "\n");
3186 buf += sprintf(buf, "nTnodes............ %d\n", dev->nTnodes);
3187 buf += sprintf(buf, "nObjects........... %d\n", dev->nObjects);
3188 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3189 buf += sprintf(buf, "\n");
3190 buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
3191 buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
3192 buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
3193 buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
3194 buf += sprintf(buf, "allGCs............. %u\n", dev->allGCs);
3195 buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGCs);
3196 buf += sprintf(buf, "oldestDirtyGCs..... %u\n", dev->oldestDirtyGCs);
3197 buf += sprintf(buf, "backgroundGCs...... %u\n", dev->backgroundGCs);
3198 buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
3199 buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
3200 buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
3201 buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
3202 buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
3203 buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
3204 buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
3205 buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
3206 buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
3207 buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
3209 sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
3214 static int yaffs_proc_read(char *page,
3216 off_t offset, int count, int *eof, void *data)
3218 struct ylist_head *item;
3223 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3224 * We use 'offset' (*ppos) to indicate where we are in devList.
3225 * This also assumes the user has posted a read buffer large
3226 * enough to hold the complete output; but that's life in /proc.
3231 /* Print header first */
3233 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__"\n");
3235 buf += sprintf(buf,"\n");
3239 down(&yaffs_context_lock);
3241 /* Locate and print the Nth entry. Order N-squared but N is small. */
3242 ylist_for_each(item, &yaffs_context_list) {
3243 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3244 yaffs_Device *dev = dc->dev;
3246 if (n < (step & ~1)) {
3251 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
3252 buf = yaffs_dump_dev_part0(buf, dev);
3254 buf = yaffs_dump_dev_part1(buf, dev);
3258 up(&yaffs_context_lock);
3261 return buf - page < count ? buf - page : count;
3264 static int yaffs_stats_proc_read(char *page,
3266 off_t offset, int count, int *eof, void *data)
3268 struct ylist_head *item;
3272 down(&yaffs_context_lock);
3274 /* Locate and print the Nth entry. Order N-squared but N is small. */
3275 ylist_for_each(item, &yaffs_context_list) {
3276 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3277 yaffs_Device *dev = dc->dev;
3281 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3283 buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
3284 n, dev->nFreeChunks, erasedChunks,
3285 dev->backgroundGCs, dev->oldestDirtyGCs,
3286 dev->nObjects, dev->nTnodes);
3288 up(&yaffs_context_lock);
3291 return buf - page < count ? buf - page : count;
3295 * Set the verbosity of the warnings and error messages.
3297 * Note that the names can only be a..z or _ with the current code.
3302 unsigned mask_bitfield;
3304 {"allocate", YAFFS_TRACE_ALLOCATE},
3305 {"always", YAFFS_TRACE_ALWAYS},
3306 {"background", YAFFS_TRACE_BACKGROUND},
3307 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3308 {"buffers", YAFFS_TRACE_BUFFERS},
3309 {"bug", YAFFS_TRACE_BUG},
3310 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3311 {"deletion", YAFFS_TRACE_DELETION},
3312 {"erase", YAFFS_TRACE_ERASE},
3313 {"error", YAFFS_TRACE_ERROR},
3314 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3315 {"gc", YAFFS_TRACE_GC},
3316 {"lock", YAFFS_TRACE_LOCK},
3317 {"mtd", YAFFS_TRACE_MTD},
3318 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3319 {"os", YAFFS_TRACE_OS},
3320 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3321 {"scan", YAFFS_TRACE_SCAN},
3322 {"tracing", YAFFS_TRACE_TRACING},
3323 {"sync", YAFFS_TRACE_SYNC},
3324 {"write", YAFFS_TRACE_WRITE},
3326 {"verify", YAFFS_TRACE_VERIFY},
3327 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3328 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3329 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3331 {"all", 0xffffffff},
3336 #define MAX_MASK_NAME_LENGTH 40
3337 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3338 unsigned long count, void *data)
3340 unsigned rg = 0, mask_bitfield;
3344 char substring[MAX_MASK_NAME_LENGTH + 1];
3350 rg = yaffs_traceMask;
3352 while (!done && (pos < count)) {
3354 while ((pos < count) && isspace(buf[pos]))
3371 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3373 if (end > buf + pos) {
3374 mask_name = "numeral";
3375 len = end - (buf + pos);
3379 for (x = buf + pos, i = 0;
3380 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3381 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3383 substring[i] = '\0';
3385 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3386 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3387 mask_name = mask_flags[i].mask_name;
3388 mask_bitfield = mask_flags[i].mask_bitfield;
3395 if (mask_name != NULL) {
3399 rg &= ~mask_bitfield;
3402 rg |= mask_bitfield;
3408 rg |= mask_bitfield;
3414 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3416 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
3418 if (rg & YAFFS_TRACE_ALWAYS) {
3419 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3421 flag = ((rg & mask_flags[i].mask_bitfield) ==
3422 mask_flags[i].mask_bitfield) ? '+' : '-';
3423 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3431 static int yaffs_proc_write(struct file *file, const char *buf,
3432 unsigned long count, void *data)
3434 return yaffs_proc_write_trace_options(file, buf, count, data);
3437 /* Stuff to handle installation of file systems */
3438 struct file_system_to_install {
3439 struct file_system_type *fst;
3443 static struct file_system_to_install fs_to_install[] = {
3444 {&yaffs_fs_type, 0},
3445 {&yaffs2_fs_type, 0},
3449 static int __init init_yaffs_fs(void)
3452 struct file_system_to_install *fsinst;
3454 T(YAFFS_TRACE_ALWAYS,
3455 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3457 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3458 T(YAFFS_TRACE_ALWAYS,
3459 (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3465 init_MUTEX(&yaffs_context_lock);
3467 /* Install the proc_fs entries */
3468 my_proc_entry = create_proc_entry("yaffs",
3472 if (my_proc_entry) {
3473 my_proc_entry->write_proc = yaffs_proc_write;
3474 my_proc_entry->read_proc = yaffs_proc_read;
3475 my_proc_entry->data = NULL;
3479 debug_proc_entry = create_proc_entry("yaffs_stats",
3483 if (debug_proc_entry) {
3484 debug_proc_entry->write_proc = NULL;
3485 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3486 debug_proc_entry->data = NULL;
3490 /* Now add the file system entries */
3492 fsinst = fs_to_install;
3494 while (fsinst->fst && !error) {
3495 error = register_filesystem(fsinst->fst);
3497 fsinst->installed = 1;
3501 /* Any errors? uninstall */
3503 fsinst = fs_to_install;
3505 while (fsinst->fst) {
3506 if (fsinst->installed) {
3507 unregister_filesystem(fsinst->fst);
3508 fsinst->installed = 0;
3517 static void __exit exit_yaffs_fs(void)
3520 struct file_system_to_install *fsinst;
3522 T(YAFFS_TRACE_ALWAYS,
3523 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3525 remove_proc_entry("yaffs", YPROC_ROOT);
3526 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3528 fsinst = fs_to_install;
3530 while (fsinst->fst) {
3531 if (fsinst->installed) {
3532 unregister_filesystem(fsinst->fst);
3533 fsinst->installed = 0;
3539 module_init(init_yaffs_fs)
3540 module_exit(exit_yaffs_fs)
3542 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3543 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3544 MODULE_LICENSE("GPL");