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
46 #define YAFFS_USE_TRUNCATE_SETSIZE
48 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
49 #define YAFFS_HAS_EVICT_INODE
52 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
53 #define YAFFS_NEW_FOLLOW_LINK 1
55 #define YAFFS_NEW_FOLLOW_LINK 0
58 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
59 #include <linux/config.h>
62 #include <linux/kernel.h>
63 #include <linux/module.h>
64 #include <linux/slab.h>
65 #include <linux/init.h>
67 #include <linux/proc_fs.h>
68 #include <linux/smp_lock.h>
69 #include <linux/pagemap.h>
70 #include <linux/mtd/mtd.h>
71 #include <linux/interrupt.h>
72 #include <linux/string.h>
73 #include <linux/ctype.h>
75 #if (YAFFS_NEW_FOLLOW_LINK == 1)
76 #include <linux/namei.h>
79 #ifdef YAFFS_COMPILE_EXPORTFS
80 #include <linux/exportfs.h>
83 #ifdef YAFFS_COMPILE_BACKGROUND
84 #include <linux/kthread.h>
85 #include <linux/delay.h>
86 #include <linux/freezer.h>
89 #include <asm/div64.h>
91 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
93 #include <linux/statfs.h>
95 #define UnlockPage(p) unlock_page(p)
96 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
98 /* FIXME: use sb->s_id instead ? */
99 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
103 #include <linux/locks.h>
104 #define BDEVNAME_SIZE 0
105 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
107 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
108 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
114 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
115 #define YPROC_ROOT (&proc_root)
117 #define YPROC_ROOT NULL
120 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
121 #define WRITE_SIZE_STR "writesize"
122 #define WRITE_SIZE(mtd) ((mtd)->writesize)
124 #define WRITE_SIZE_STR "oobblock"
125 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
128 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
129 #define YAFFS_USE_WRITE_BEGIN_END 1
131 #define YAFFS_USE_WRITE_BEGIN_END 0
134 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
135 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
137 uint64_t result = partition_size;
138 do_div(result, block_size);
139 return (uint32_t)result;
142 #define YCALCBLOCKS(s, b) ((s)/(b))
145 #include <linux/uaccess.h>
146 #include <linux/mtd/mtd.h>
148 #include "yportenv.h"
149 #include "yaffs_trace.h"
150 #include "yaffs_guts.h"
152 #include "yaffs_linux.h"
154 #include "yaffs_mtdif.h"
155 #include "yaffs_mtdif1.h"
156 #include "yaffs_mtdif2.h"
158 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
159 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
160 unsigned int yaffs_auto_checkpoint = 1;
161 unsigned int yaffs_gc_control = 1;
162 unsigned int yaffs_bg_enable = 1;
164 /* Module Parameters */
165 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
166 module_param(yaffs_traceMask, uint, 0644);
167 module_param(yaffs_wr_attempts, uint, 0644);
168 module_param(yaffs_auto_checkpoint, uint, 0644);
169 module_param(yaffs_gc_control, uint, 0644);
170 module_param(yaffs_bg_enable, uint, 0644);
172 MODULE_PARM(yaffs_traceMask, "i");
173 MODULE_PARM(yaffs_wr_attempts, "i");
174 MODULE_PARM(yaffs_auto_checkpoint, "i");
175 MODULE_PARM(yaffs_gc_control, "i");
178 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
179 /* use iget and read_inode */
180 #define Y_IGET(sb, inum) iget((sb), (inum))
181 static void yaffs_read_inode(struct inode *inode);
184 /* Call local equivalent */
185 #define YAFFS_USE_OWN_IGET
186 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
188 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
191 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
192 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
194 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
197 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
198 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
200 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
201 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
203 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
207 #define update_dir_time(dir) do {\
208 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
211 static void yaffs_put_super(struct super_block *sb);
213 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
215 static ssize_t yaffs_hold_space(struct file *f);
216 static void yaffs_release_space(struct file *f);
218 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
219 static int yaffs_file_flush(struct file *file, fl_owner_t id);
221 static int yaffs_file_flush(struct file *file);
224 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
225 static int yaffs_sync_object(struct file *file, int datasync);
227 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
231 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
233 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
234 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
235 struct nameidata *n);
236 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
237 struct nameidata *n);
239 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
240 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
242 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
243 struct dentry *dentry);
244 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
245 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
246 const char *symname);
247 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
249 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
250 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
253 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
256 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
257 struct inode *new_dir, struct dentry *new_dentry);
258 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
260 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
261 static int yaffs_sync_fs(struct super_block *sb, int wait);
262 static void yaffs_write_super(struct super_block *sb);
264 static int yaffs_sync_fs(struct super_block *sb);
265 static int yaffs_write_super(struct super_block *sb);
268 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
269 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
270 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
271 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
273 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
276 #ifdef YAFFS_HAS_PUT_INODE
277 static void yaffs_put_inode(struct inode *inode);
280 #ifdef YAFFS_HAS_EVICT_INODE
281 static void yaffs_evict_inode(struct inode *);
283 static void yaffs_delete_inode(struct inode *);
284 static void yaffs_clear_inode(struct inode *);
287 static int yaffs_readpage(struct file *file, struct page *page);
288 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
289 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
291 static int yaffs_writepage(struct page *page);
294 #ifdef CONFIG_YAFFS_XATTR
295 int yaffs_setxattr(struct dentry *dentry, const char *name,
296 const void *value, size_t size, int flags);
297 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
299 int yaffs_removexattr(struct dentry *dentry, const char *name);
300 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
304 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
305 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
306 loff_t pos, unsigned len, unsigned flags,
307 struct page **pagep, void **fsdata);
308 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
309 loff_t pos, unsigned len, unsigned copied,
310 struct page *pg, void *fsdadata);
312 static int yaffs_prepare_write(struct file *f, struct page *pg,
313 unsigned offset, unsigned to);
314 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
319 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
321 #if (YAFFS_NEW_FOLLOW_LINK == 1)
322 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
323 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
325 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
328 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev);
330 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
332 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
335 static struct address_space_operations yaffs_file_address_operations = {
336 .readpage = yaffs_readpage,
337 .writepage = yaffs_writepage,
338 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
339 .write_begin = yaffs_write_begin,
340 .write_end = yaffs_write_end,
342 .prepare_write = yaffs_prepare_write,
343 .commit_write = yaffs_commit_write,
348 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
349 static const struct file_operations yaffs_file_operations = {
350 .read = do_sync_read,
351 .write = do_sync_write,
352 .aio_read = generic_file_aio_read,
353 .aio_write = generic_file_aio_write,
354 .mmap = generic_file_mmap,
355 .flush = yaffs_file_flush,
356 .fsync = yaffs_sync_object,
357 .splice_read = generic_file_splice_read,
358 .splice_write = generic_file_splice_write,
359 .llseek = generic_file_llseek,
362 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
364 static const struct file_operations yaffs_file_operations = {
365 .read = do_sync_read,
366 .write = do_sync_write,
367 .aio_read = generic_file_aio_read,
368 .aio_write = generic_file_aio_write,
369 .mmap = generic_file_mmap,
370 .flush = yaffs_file_flush,
371 .fsync = yaffs_sync_object,
372 .sendfile = generic_file_sendfile,
377 static const struct file_operations yaffs_file_operations = {
378 .read = generic_file_read,
379 .write = generic_file_write,
380 .mmap = generic_file_mmap,
381 .flush = yaffs_file_flush,
382 .fsync = yaffs_sync_object,
383 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
384 .sendfile = generic_file_sendfile,
389 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
390 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
392 void * kaddr = kmap_atomic(page, KM_USER0);
393 memset(kaddr + start, 0, end - start);
394 kunmap_atomic(kaddr, KM_USER0);
395 flush_dcache_page(page);
400 static const struct inode_operations yaffs_file_inode_operations = {
401 .setattr = yaffs_setattr,
402 #ifdef CONFIG_YAFFS_XATTR
403 .setxattr = yaffs_setxattr,
404 .getxattr = yaffs_getxattr,
405 .listxattr = yaffs_listxattr,
406 .removexattr = yaffs_removexattr,
410 static const struct inode_operations yaffs_symlink_inode_operations = {
411 .readlink = yaffs_readlink,
412 .follow_link = yaffs_follow_link,
413 #if (YAFFS_NEW_FOLLOW_LINK == 1)
414 .put_link = yaffs_put_link,
416 .setattr = yaffs_setattr,
417 #ifdef CONFIG_YAFFS_XATTR
418 .setxattr = yaffs_setxattr,
419 .getxattr = yaffs_getxattr,
420 .listxattr = yaffs_listxattr,
421 .removexattr = yaffs_removexattr,
425 static const struct inode_operations yaffs_dir_inode_operations = {
426 .create = yaffs_create,
427 .lookup = yaffs_lookup,
429 .unlink = yaffs_unlink,
430 .symlink = yaffs_symlink,
431 .mkdir = yaffs_mkdir,
432 .rmdir = yaffs_unlink,
433 .mknod = yaffs_mknod,
434 .rename = yaffs_rename,
435 .setattr = yaffs_setattr,
436 #ifdef CONFIG_YAFFS_XATTR
437 .setxattr = yaffs_setxattr,
438 .getxattr = yaffs_getxattr,
439 .listxattr = yaffs_listxattr,
440 .removexattr = yaffs_removexattr,
444 static const struct file_operations yaffs_dir_operations = {
445 .read = generic_read_dir,
446 .readdir = yaffs_readdir,
447 .fsync = yaffs_sync_object,
448 .llseek = yaffs_dir_llseek,
451 static const struct super_operations yaffs_super_ops = {
452 .statfs = yaffs_statfs,
454 #ifndef YAFFS_USE_OWN_IGET
455 .read_inode = yaffs_read_inode,
457 #ifdef YAFFS_HAS_PUT_INODE
458 .put_inode = yaffs_put_inode,
460 .put_super = yaffs_put_super,
461 #ifdef YAFFS_HAS_EVICT_INODE
462 .evict_inode = yaffs_evict_inode,
464 .delete_inode = yaffs_delete_inode,
465 .clear_inode = yaffs_clear_inode,
467 .sync_fs = yaffs_sync_fs,
468 .write_super = yaffs_write_super,
472 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
474 #ifdef YAFFS_USE_SETATTR_COPY
475 setattr_copy(inode,attr);
478 return inode_setattr(inode, attr);
483 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
485 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
486 truncate_setsize(inode,newsize);
489 return simple_setsize(inode, newsize);
494 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
496 return yaffs_gc_control;
499 static void yaffs_GrossLock(yaffs_Device *dev)
501 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
502 down(&(yaffs_DeviceToLC(dev)->grossLock));
503 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
506 static void yaffs_GrossUnlock(yaffs_Device *dev)
508 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
509 up(&(yaffs_DeviceToLC(dev)->grossLock));
512 #ifdef YAFFS_COMPILE_EXPORTFS
514 static struct inode *
515 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
517 return Y_IGET(sb, ino);
520 static struct dentry *
521 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
523 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
526 static struct dentry *
527 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
529 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
532 struct dentry *yaffs2_get_parent(struct dentry *dentry)
535 struct super_block *sb = dentry->d_inode->i_sb;
536 struct dentry *parent = ERR_PTR(-ENOENT);
538 unsigned long parent_ino;
540 yaffs_Object *parent_obj;
542 d_obj = yaffs_InodeToObject(dentry->d_inode);
545 parent_obj = d_obj->parent;
547 parent_ino = yaffs_GetObjectInode(parent_obj);
548 inode = Y_IGET(sb, parent_ino);
551 parent = ERR_CAST(inode);
553 parent = d_obtain_alias(inode);
554 if (!IS_ERR(parent)) {
555 parent = ERR_PTR(-ENOMEM);
565 /* Just declare a zero structure as a NULL value implies
566 * using the default functions of exportfs.
569 static struct export_operations yaffs_export_ops =
571 .fh_to_dentry = yaffs2_fh_to_dentry,
572 .fh_to_parent = yaffs2_fh_to_parent,
573 .get_parent = yaffs2_get_parent,
578 /*-----------------------------------------------------------------*/
579 /* Directory search context allows us to unlock access to yaffs during
580 * filldir without causing problems with the directory being modified.
581 * This is similar to the tried and tested mechanism used in yaffs direct.
583 * A search context iterates along a doubly linked list of siblings in the
584 * directory. If the iterating object is deleted then this would corrupt
585 * the list iteration, likely causing a crash. The search context avoids
586 * this by using the removeObjectCallback to move the search context to the
587 * next object before the object is deleted.
589 * Many readdirs (and thus seach conexts) may be alive simulateously so
590 * each yaffs_Device has a list of these.
592 * A seach context lives for the duration of a readdir.
594 * All these functions must be called while yaffs is locked.
597 struct yaffs_SearchContext {
599 yaffs_Object *dirObj;
600 yaffs_Object *nextReturn;
601 struct ylist_head others;
605 * yaffs_NewSearch() creates a new search context, initialises it and
606 * adds it to the device's search context list.
608 * Called at start of readdir.
610 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
612 yaffs_Device *dev = dir->myDev;
613 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
617 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
618 sc->nextReturn = NULL;
620 sc->nextReturn = ylist_entry(
621 dir->variant.directoryVariant.children.next,
622 yaffs_Object,siblings);
623 YINIT_LIST_HEAD(&sc->others);
624 ylist_add(&sc->others,&(yaffs_DeviceToLC(dev)->searchContexts));
630 * yaffs_EndSearch() disposes of a search context and cleans up.
632 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
635 ylist_del(&sc->others);
641 * yaffs_SearchAdvance() moves a search context to the next object.
642 * Called when the search iterates or when an object removal causes
643 * the search context to be moved to the next object.
645 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
650 if( sc->nextReturn == NULL ||
651 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
652 sc->nextReturn = NULL;
654 struct ylist_head *next = sc->nextReturn->siblings.next;
656 if( next == &sc->dirObj->variant.directoryVariant.children)
657 sc->nextReturn = NULL; /* end of list */
659 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
664 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
665 * We check open search contexts and advance any which are currently
666 * on the object being iterated.
668 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
671 struct ylist_head *i;
672 struct yaffs_SearchContext *sc;
673 struct ylist_head *search_contexts = &(yaffs_DeviceToLC(obj->myDev)->searchContexts);
676 /* Iterate through the directory search contexts.
677 * If any are currently on the object being removed, then advance
678 * the search context to the next object to prevent a hanging pointer.
680 ylist_for_each(i, search_contexts) {
682 sc = ylist_entry(i, struct yaffs_SearchContext,others);
683 if(sc->nextReturn == obj)
684 yaffs_SearchAdvance(sc);
691 /*-----------------------------------------------------------------*/
693 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
696 unsigned char *alias;
699 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
701 yaffs_GrossLock(dev);
703 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
705 yaffs_GrossUnlock(dev);
710 ret = vfs_readlink(dentry, buffer, buflen, alias);
715 #if (YAFFS_NEW_FOLLOW_LINK == 1)
716 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
718 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
721 unsigned char *alias;
723 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
725 yaffs_GrossLock(dev);
727 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
728 yaffs_GrossUnlock(dev);
735 #if (YAFFS_NEW_FOLLOW_LINK == 1)
736 nd_set_link(nd, alias);
741 ret = vfs_follow_link(nd, alias);
748 #if (YAFFS_NEW_FOLLOW_LINK == 1)
749 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) {
754 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
758 * Lookup is used to find objects in the fs
760 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
762 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
765 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
769 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
771 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
773 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
774 yaffs_GrossLock(dev);
777 (TSTR("yaffs_lookup for %d:%s\n"),
778 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
780 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
781 dentry->d_name.name);
783 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
785 /* Can't hold gross lock when calling yaffs_get_inode() */
786 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
787 yaffs_GrossUnlock(dev);
791 (TSTR("yaffs_lookup found %d\n"), obj->objectId));
793 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
797 (TSTR("yaffs_loookup dentry \n")));
798 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
799 * d_add even if NULL inode */
801 /*dget(dentry); // try to solve directory bug */
802 d_add(dentry, inode);
810 T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
814 /* added NCB for 2.5/6 compatability - forces add even if inode is
815 * NULL which creates dentry hash */
816 d_add(dentry, inode);
822 #ifdef YAFFS_HAS_PUT_INODE
824 /* For now put inode is just for debugging
825 * Put inode is called when the inode **structure** is put.
827 static void yaffs_put_inode(struct inode *inode)
830 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
831 atomic_read(&inode->i_count)));
837 static void yaffs_UnstitchObject(struct inode *inode, yaffs_Object *obj)
839 /* Clear the association between the inode and
843 yaffs_InodeToObjectLV(inode) = NULL;
845 /* If the object freeing was deferred, then the real
847 * This should fix the inode inconsistency problem.
849 yaffs_HandleDeferedFree(obj);
852 #ifdef YAFFS_HAS_EVICT_INODE
853 /* yaffs_evict_inode combines into one operation what was previously done in
854 * yaffs_clear_inode() and yaffs_delete_inode()
857 static void yaffs_evict_inode( struct inode *inode)
863 obj = yaffs_InodeToObject(inode);
866 (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
867 atomic_read(&inode->i_count),
868 obj ? "object exists" : "null object"));
870 if (!inode->i_nlink && !is_bad_inode(inode))
872 truncate_inode_pages(&inode->i_data,0);
873 end_writeback(inode);
877 yaffs_GrossLock(dev);
878 yaffs_DeleteObject(obj);
879 yaffs_GrossUnlock(dev);
883 yaffs_GrossLock(dev);
884 yaffs_UnstitchObject(inode,obj);
885 yaffs_GrossUnlock(dev);
892 /* clear is called to tell the fs to release any per-inode data it holds.
893 * The object might still exist on disk and is just being thrown out of the cache
894 * or else the object has actually been deleted and we're being called via
896 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
899 static void yaffs_clear_inode(struct inode *inode)
904 obj = yaffs_InodeToObject(inode);
907 (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
908 atomic_read(&inode->i_count),
909 obj ? "object exists" : "null object"));
913 yaffs_GrossLock(dev);
914 yaffs_UnstitchObject(inode,obj);
915 yaffs_GrossUnlock(dev);
920 /* delete is called when the link count is zero and the inode
921 * is put (ie. nobody wants to know about it anymore, time to
923 * NB Must call clear_inode()
925 static void yaffs_delete_inode(struct inode *inode)
927 yaffs_Object *obj = yaffs_InodeToObject(inode);
931 (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
932 atomic_read(&inode->i_count),
933 obj ? "object exists" : "null object"));
937 yaffs_GrossLock(dev);
938 yaffs_DeleteObject(obj);
939 yaffs_GrossUnlock(dev);
941 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
942 truncate_inode_pages(&inode->i_data, 0);
949 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
950 static int yaffs_file_flush(struct file *file, fl_owner_t id)
952 static int yaffs_file_flush(struct file *file)
955 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
957 yaffs_Device *dev = obj->myDev;
960 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->objectId,
961 obj->dirty ? "dirty" : "clean"));
963 yaffs_GrossLock(dev);
965 yaffs_FlushFile(obj, 1, 0);
967 yaffs_GrossUnlock(dev);
972 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
974 /* Lifted from jffs2 */
977 unsigned char *pg_buf;
983 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
984 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
985 (unsigned)PAGE_CACHE_SIZE));
987 obj = yaffs_DentryToObject(f->f_dentry);
991 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
992 BUG_ON(!PageLocked(pg));
999 /* FIXME: Can kmap fail? */
1001 yaffs_GrossLock(dev);
1003 ret = yaffs_ReadDataFromFile(obj, pg_buf,
1004 pg->index << PAGE_CACHE_SHIFT,
1007 yaffs_GrossUnlock(dev);
1013 ClearPageUptodate(pg);
1016 SetPageUptodate(pg);
1020 flush_dcache_page(pg);
1023 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
1027 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1029 int ret = yaffs_readpage_nolock(f, pg);
1034 static int yaffs_readpage(struct file *f, struct page *pg)
1038 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
1039 ret=yaffs_readpage_unlock(f, pg);
1040 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
1044 /* writepage inspired by/stolen from smbfs */
1046 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1047 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1049 static int yaffs_writepage(struct page *page)
1053 struct address_space *mapping = page->mapping;
1054 struct inode *inode;
1055 unsigned long end_index;
1064 inode = mapping->host;
1067 i_size = i_size_read(inode);
1069 end_index = i_size >> PAGE_CACHE_SHIFT;
1071 if(page->index < end_index)
1072 nBytes = PAGE_CACHE_SIZE;
1074 nBytes = i_size & (PAGE_CACHE_SIZE -1);
1076 if (page->index > end_index || !nBytes) {
1078 (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
1079 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1080 (unsigned)inode->i_size));
1082 (TSTR(" -> don't care!!\n")));
1084 zero_user_segment(page,0,PAGE_CACHE_SIZE);
1085 set_page_writeback(page);
1087 end_page_writeback(page);
1092 if(nBytes != PAGE_CACHE_SIZE)
1093 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
1097 buffer = kmap(page);
1099 obj = yaffs_InodeToObject(inode);
1101 yaffs_GrossLock(dev);
1104 (TSTR("yaffs_writepage at %08x, size %08x\n"),
1105 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
1107 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
1108 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1110 nWritten = yaffs_WriteDataToFile(obj, buffer,
1111 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
1113 yaffs_MarkSuperBlockDirty(dev);
1116 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
1117 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1119 yaffs_GrossUnlock(dev);
1122 set_page_writeback(page);
1124 end_page_writeback(page);
1127 return (nWritten == nBytes) ? 0 : -ENOSPC;
1131 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1132 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1133 loff_t pos, unsigned len, unsigned flags,
1134 struct page **pagep, void **fsdata)
1136 struct page *pg = NULL;
1137 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1143 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1144 pg = grab_cache_page_write_begin(mapping, index, flags);
1146 pg = __grab_cache_page(mapping, index);
1155 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
1156 (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
1159 space_held = yaffs_hold_space(filp);
1166 /* Update page if required */
1168 if (!Page_Uptodate(pg))
1169 ret = yaffs_readpage_nolock(filp, pg);
1174 /* Happy path return */
1175 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
1181 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
1183 yaffs_release_space(filp);
1186 page_cache_release(pg);
1193 static int yaffs_prepare_write(struct file *f, struct page *pg,
1194 unsigned offset, unsigned to)
1196 T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
1198 if (!Page_Uptodate(pg))
1199 return yaffs_readpage_nolock(f, pg);
1204 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1205 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1206 loff_t pos, unsigned len, unsigned copied,
1207 struct page *pg, void *fsdadata)
1211 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1214 addr = kva + offset_into_page;
1217 ("yaffs_write_end addr %p pos %x nBytes %d\n",
1218 addr,(unsigned)pos, copied));
1220 ret = yaffs_file_write(filp, addr, copied, &pos);
1222 if (ret != copied) {
1224 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
1233 yaffs_release_space(filp);
1235 page_cache_release(pg);
1240 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1245 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1246 int nBytes = to - offset;
1249 unsigned spos = pos;
1253 addr = kva + offset;
1255 saddr = (unsigned) addr;
1258 (TSTR("yaffs_commit_write addr %x pos %x nBytes %d\n"),
1259 saddr, spos, nBytes));
1261 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1263 if (nWritten != nBytes) {
1265 (TSTR("yaffs_commit_write not same size nWritten %d nBytes %d\n"),
1275 (TSTR("yaffs_commit_write returning %d\n"),
1276 nWritten == nBytes ? 0 : nWritten));
1278 return nWritten == nBytes ? 0 : nWritten;
1283 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1288 /* Check mode against the variant type and attempt to repair if broken. */
1289 __u32 mode = obj->yst_mode;
1290 switch (obj->variantType) {
1291 case YAFFS_OBJECT_TYPE_FILE:
1292 if (!S_ISREG(mode)) {
1293 obj->yst_mode &= ~S_IFMT;
1294 obj->yst_mode |= S_IFREG;
1298 case YAFFS_OBJECT_TYPE_SYMLINK:
1299 if (!S_ISLNK(mode)) {
1300 obj->yst_mode &= ~S_IFMT;
1301 obj->yst_mode |= S_IFLNK;
1305 case YAFFS_OBJECT_TYPE_DIRECTORY:
1306 if (!S_ISDIR(mode)) {
1307 obj->yst_mode &= ~S_IFMT;
1308 obj->yst_mode |= S_IFDIR;
1312 case YAFFS_OBJECT_TYPE_UNKNOWN:
1313 case YAFFS_OBJECT_TYPE_HARDLINK:
1314 case YAFFS_OBJECT_TYPE_SPECIAL:
1320 inode->i_flags |= S_NOATIME;
1322 inode->i_ino = obj->objectId;
1323 inode->i_mode = obj->yst_mode;
1324 inode->i_uid = obj->yst_uid;
1325 inode->i_gid = obj->yst_gid;
1326 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1327 inode->i_blksize = inode->i_sb->s_blocksize;
1329 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1331 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1332 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1333 inode->i_atime.tv_nsec = 0;
1334 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1335 inode->i_mtime.tv_nsec = 0;
1336 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1337 inode->i_ctime.tv_nsec = 0;
1339 inode->i_rdev = obj->yst_rdev;
1340 inode->i_atime = obj->yst_atime;
1341 inode->i_mtime = obj->yst_mtime;
1342 inode->i_ctime = obj->yst_ctime;
1344 inode->i_size = yaffs_GetObjectFileLength(obj);
1345 inode->i_blocks = (inode->i_size + 511) >> 9;
1347 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1350 (TSTR("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n"),
1351 inode->i_mode, inode->i_uid, inode->i_gid,
1352 (int)inode->i_size, atomic_read(&inode->i_count)));
1354 switch (obj->yst_mode & S_IFMT) {
1355 default: /* fifo, device or socket */
1356 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1357 init_special_inode(inode, obj->yst_mode,
1358 old_decode_dev(obj->yst_rdev));
1360 init_special_inode(inode, obj->yst_mode,
1361 (dev_t) (obj->yst_rdev));
1364 case S_IFREG: /* file */
1365 inode->i_op = &yaffs_file_inode_operations;
1366 inode->i_fop = &yaffs_file_operations;
1367 inode->i_mapping->a_ops =
1368 &yaffs_file_address_operations;
1370 case S_IFDIR: /* directory */
1371 inode->i_op = &yaffs_dir_inode_operations;
1372 inode->i_fop = &yaffs_dir_operations;
1374 case S_IFLNK: /* symlink */
1375 inode->i_op = &yaffs_symlink_inode_operations;
1379 yaffs_InodeToObjectLV(inode) = obj;
1381 obj->myInode = inode;
1385 (TSTR("yaffs_FileInode invalid parameters\n")));
1390 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1393 struct inode *inode;
1397 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1404 (TSTR("yaffs_get_inode for NULL object!!\n")));
1410 (TSTR("yaffs_get_inode for object %d\n"), obj->objectId));
1412 inode = Y_IGET(sb, obj->objectId);
1416 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1417 /* iget also increments the inode's i_count */
1418 /* NB You can't be holding grossLock or deadlock will happen! */
1423 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1428 struct inode *inode;
1431 obj = yaffs_DentryToObject(f->f_dentry);
1435 yaffs_GrossLock(dev);
1437 inode = f->f_dentry->d_inode;
1439 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1440 ipos = inode->i_size;
1446 (TSTR("yaffs_file_write: hey obj is null!\n")));
1449 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1450 "to object %d at %d(%x)\n"),
1451 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1453 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1455 yaffs_MarkSuperBlockDirty(dev);
1458 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1459 (unsigned )n,(unsigned)n));
1464 if (ipos > inode->i_size) {
1465 inode->i_size = ipos;
1466 inode->i_blocks = (ipos + 511) >> 9;
1469 (TSTR("yaffs_file_write size updated to %d bytes, "
1471 ipos, (int)(inode->i_blocks)));
1475 yaffs_GrossUnlock(dev);
1476 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1479 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1480 /* For now we just assume few parallel writes and check against a small number. */
1481 /* Todo: need to do this with a counter to handle parallel reads better */
1483 static ssize_t yaffs_hold_space(struct file *f)
1491 obj = yaffs_DentryToObject(f->f_dentry);
1495 yaffs_GrossLock(dev);
1497 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1499 yaffs_GrossUnlock(dev);
1501 return (nFreeChunks > 20) ? 1 : 0;
1504 static void yaffs_release_space(struct file *f)
1510 obj = yaffs_DentryToObject(f->f_dentry);
1514 yaffs_GrossLock(dev);
1517 yaffs_GrossUnlock(dev);
1521 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1529 offset += i_size_read(file->f_path.dentry->d_inode);
1532 offset += file->f_pos;
1537 if (offset != file->f_pos)
1538 file->f_pos = offset;
1547 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1551 struct yaffs_SearchContext *sc;
1552 struct inode *inode = f->f_dentry->d_inode;
1553 unsigned long offset, curoffs;
1557 char name[YAFFS_MAX_NAME_LENGTH + 1];
1559 obj = yaffs_DentryToObject(f->f_dentry);
1562 yaffs_GrossLock(dev);
1564 yaffs_DeviceToLC(dev)->readdirProcess = current;
1568 sc = yaffs_NewSearch(obj);
1574 T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1578 (TSTR("yaffs_readdir: entry . ino %d \n"),
1579 (int)inode->i_ino));
1580 yaffs_GrossUnlock(dev);
1581 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0){
1582 yaffs_GrossLock(dev);
1585 yaffs_GrossLock(dev);
1591 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1592 (int)f->f_dentry->d_parent->d_inode->i_ino));
1593 yaffs_GrossUnlock(dev);
1594 if (filldir(dirent, "..", 2, offset,
1595 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
1596 yaffs_GrossLock(dev);
1599 yaffs_GrossLock(dev);
1606 /* If the directory has changed since the open or last call to
1607 readdir, rewind to after the 2 canned entries. */
1608 if (f->f_version != inode->i_version) {
1611 f->f_version = inode->i_version;
1614 while(sc->nextReturn){
1617 if (curoffs >= offset) {
1618 int this_inode = yaffs_GetObjectInode(l);
1619 int this_type = yaffs_GetObjectType(l);
1621 yaffs_GetObjectName(l, name,
1622 YAFFS_MAX_NAME_LENGTH + 1);
1624 (TSTR("yaffs_readdir: %s inode %d\n"),
1625 name, yaffs_GetObjectInode(l)));
1627 yaffs_GrossUnlock(dev);
1635 yaffs_GrossLock(dev);
1639 yaffs_GrossLock(dev);
1644 yaffs_SearchAdvance(sc);
1648 yaffs_EndSearch(sc);
1649 yaffs_DeviceToLC(dev)->readdirProcess = NULL;
1650 yaffs_GrossUnlock(dev);
1658 * File creation. Allocate an inode, and we're done..
1661 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1664 #define YCRED(x) (x->cred)
1667 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1668 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1671 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1675 struct inode *inode;
1677 yaffs_Object *obj = NULL;
1680 yaffs_Object *parent = yaffs_InodeToObject(dir);
1682 int error = -ENOSPC;
1683 uid_t uid = YCRED(current)->fsuid;
1684 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1686 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1691 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1692 parent->objectId, parent->variantType));
1695 (TSTR("yaffs_mknod: could not get parent object\n")));
1699 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1700 "mode %x dev %x\n"),
1701 dentry->d_name.name, mode, rdev));
1703 dev = parent->myDev;
1705 yaffs_GrossLock(dev);
1707 switch (mode & S_IFMT) {
1709 /* Special (socket, fifo, device...) */
1710 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1711 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1712 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1713 gid, old_encode_dev(rdev));
1715 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1719 case S_IFREG: /* file */
1720 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1721 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1724 case S_IFDIR: /* directory */
1726 (TSTR("yaffs_mknod: making directory\n")));
1727 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1730 case S_IFLNK: /* symlink */
1731 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1732 obj = NULL; /* Do we ever get here? */
1736 /* Can not call yaffs_get_inode() with gross lock held */
1737 yaffs_GrossUnlock(dev);
1740 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1741 d_instantiate(dentry, inode);
1742 update_dir_time(dir);
1744 (TSTR("yaffs_mknod created object %d count = %d\n"),
1745 obj->objectId, atomic_read(&inode->i_count)));
1747 yaffs_FillInodeFromObject(dir,parent);
1750 (TSTR("yaffs_mknod failed making object\n")));
1757 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1760 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1761 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1765 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1766 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1767 struct nameidata *n)
1769 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1772 T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1773 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1776 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1784 (TSTR("yaffs_unlink %d:%s\n"),
1786 dentry->d_name.name));
1787 obj = yaffs_InodeToObject(dir);
1790 yaffs_GrossLock(dev);
1792 retVal = yaffs_Unlink(obj, dentry->d_name.name);
1794 if (retVal == YAFFS_OK) {
1795 dentry->d_inode->i_nlink--;
1797 yaffs_GrossUnlock(dev);
1798 mark_inode_dirty(dentry->d_inode);
1799 update_dir_time(dir);
1802 yaffs_GrossUnlock(dev);
1809 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1810 struct dentry *dentry)
1812 struct inode *inode = old_dentry->d_inode;
1813 yaffs_Object *obj = NULL;
1814 yaffs_Object *link = NULL;
1817 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1819 obj = yaffs_InodeToObject(inode);
1822 yaffs_GrossLock(dev);
1824 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1825 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1829 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1830 d_instantiate(dentry, old_dentry->d_inode);
1831 atomic_inc(&old_dentry->d_inode->i_count);
1833 (TSTR("yaffs_link link count %d i_count %d\n"),
1834 old_dentry->d_inode->i_nlink,
1835 atomic_read(&old_dentry->d_inode->i_count)));
1838 yaffs_GrossUnlock(dev);
1841 update_dir_time(dir);
1848 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1849 const char *symname)
1853 uid_t uid = YCRED(current)->fsuid;
1854 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1856 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1858 dev = yaffs_InodeToObject(dir)->myDev;
1859 yaffs_GrossLock(dev);
1860 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1861 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1862 yaffs_GrossUnlock(dev);
1865 struct inode *inode;
1867 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1868 d_instantiate(dentry, inode);
1869 update_dir_time(dir);
1870 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1873 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1879 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1880 static int yaffs_sync_object(struct file *file, int datasync)
1882 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1889 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1890 struct dentry *dentry = file->f_path.dentry;
1893 obj = yaffs_DentryToObject(dentry);
1897 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1898 (TSTR("yaffs_sync_object\n")));
1899 yaffs_GrossLock(dev);
1900 yaffs_FlushFile(obj, 1, datasync);
1901 yaffs_GrossUnlock(dev);
1906 * The VFS layer already does all the dentry stuff for rename.
1908 * NB: POSIX says you can rename an object over an old object of the same name
1910 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1911 struct inode *new_dir, struct dentry *new_dentry)
1914 int retVal = YAFFS_FAIL;
1915 yaffs_Object *target;
1917 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1918 dev = yaffs_InodeToObject(old_dir)->myDev;
1920 yaffs_GrossLock(dev);
1922 /* Check if the target is an existing directory that is not empty. */
1923 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1924 new_dentry->d_name.name);
1928 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1929 !ylist_empty(&target->variant.directoryVariant.children)) {
1931 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1933 retVal = YAFFS_FAIL;
1935 /* Now does unlinking internally using shadowing mechanism */
1936 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_RenameObject\n")));
1938 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1939 old_dentry->d_name.name,
1940 yaffs_InodeToObject(new_dir),
1941 new_dentry->d_name.name);
1943 yaffs_GrossUnlock(dev);
1945 if (retVal == YAFFS_OK) {
1947 new_dentry->d_inode->i_nlink--;
1948 mark_inode_dirty(new_dentry->d_inode);
1951 update_dir_time(old_dir);
1952 if(old_dir != new_dir)
1953 update_dir_time(new_dir);
1960 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1962 struct inode *inode = dentry->d_inode;
1967 (TSTR("yaffs_setattr of object %d\n"),
1968 yaffs_InodeToObject(inode)->objectId));
1970 /* Fail if a requested resize >= 2GB */
1971 if (attr->ia_valid & ATTR_SIZE &&
1972 (attr->ia_size >> 31))
1976 error = inode_change_ok(inode, attr);
1980 error = yaffs_vfs_setattr(inode, attr);
1981 T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
1982 if (attr->ia_valid & ATTR_SIZE){
1983 yaffs_vfs_setsize(inode,attr->ia_size);
1984 inode->i_blocks = (inode->i_size + 511) >> 9;
1987 dev = yaffs_InodeToObject(inode)->myDev;
1988 if (attr->ia_valid & ATTR_SIZE){
1989 T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
1990 (int)(attr->ia_size),(int)(attr->ia_size)));
1992 yaffs_GrossLock(dev);
1993 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1994 if(result == YAFFS_OK) {
1999 yaffs_GrossUnlock(dev);
2004 (TSTR("yaffs_setattr done returning %d\n"),error));
2009 #ifdef CONFIG_YAFFS_XATTR
2010 int yaffs_setxattr(struct dentry *dentry, const char *name,
2011 const void *value, size_t size, int flags)
2013 struct inode *inode = dentry->d_inode;
2016 yaffs_Object *obj = yaffs_InodeToObject(inode);
2019 (TSTR("yaffs_setxattr of object %d\n"),
2026 yaffs_GrossLock(dev);
2027 result = yaffs_SetXAttribute(obj, name, value, size, flags);
2028 if(result == YAFFS_OK)
2032 yaffs_GrossUnlock(dev);
2036 (TSTR("yaffs_setxattr done returning %d\n"),error));
2042 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
2045 struct inode *inode = dentry->d_inode;
2048 yaffs_Object *obj = yaffs_InodeToObject(inode);
2051 (TSTR("yaffs_getxattr \"%s\" from object %d\n"),
2052 name, obj->objectId));
2056 yaffs_GrossLock(dev);
2057 error = yaffs_GetXAttribute(obj, name, buff, size);
2058 yaffs_GrossUnlock(dev);
2062 (TSTR("yaffs_getxattr done returning %d\n"),error));
2067 int yaffs_removexattr(struct dentry *dentry, const char *name)
2069 struct inode *inode = dentry->d_inode;
2072 yaffs_Object *obj = yaffs_InodeToObject(inode);
2075 (TSTR("yaffs_removexattr of object %d\n"),
2082 yaffs_GrossLock(dev);
2083 result = yaffs_RemoveXAttribute(obj, name);
2084 if(result == YAFFS_OK)
2088 yaffs_GrossUnlock(dev);
2092 (TSTR("yaffs_removexattr done returning %d\n"),error));
2097 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
2099 struct inode *inode = dentry->d_inode;
2102 yaffs_Object *obj = yaffs_InodeToObject(inode);
2105 (TSTR("yaffs_listxattr of object %d\n"),
2111 yaffs_GrossLock(dev);
2112 error = yaffs_ListXAttributes(obj, buff, size);
2113 yaffs_GrossUnlock(dev);
2117 (TSTR("yaffs_listxattr done returning %d\n"),error));
2125 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2126 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2128 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2129 struct super_block *sb = dentry->d_sb;
2130 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2131 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2133 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2135 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2137 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2140 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
2142 yaffs_GrossLock(dev);
2144 buf->f_type = YAFFS_MAGIC;
2145 buf->f_bsize = sb->s_blocksize;
2146 buf->f_namelen = 255;
2148 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
2149 /* Do this if chunk size is not a power of 2 */
2151 uint64_t bytesInDev;
2154 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
2155 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
2157 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
2158 buf->f_blocks = bytesInDev;
2160 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
2161 ((uint64_t)(dev->nDataBytesPerChunk));
2163 do_div(bytesFree, sb->s_blocksize);
2165 buf->f_bfree = bytesFree;
2167 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2170 (dev->param.endBlock - dev->param.startBlock + 1) *
2171 dev->param.nChunksPerBlock /
2172 (sb->s_blocksize / dev->nDataBytesPerChunk);
2174 yaffs_GetNumberOfFreeChunks(dev) /
2175 (sb->s_blocksize / dev->nDataBytesPerChunk);
2178 (dev->param.endBlock - dev->param.startBlock + 1) *
2179 dev->param.nChunksPerBlock *
2180 (dev->nDataBytesPerChunk / sb->s_blocksize);
2183 yaffs_GetNumberOfFreeChunks(dev) *
2184 (dev->nDataBytesPerChunk / sb->s_blocksize);
2189 buf->f_bavail = buf->f_bfree;
2191 yaffs_GrossUnlock(dev);
2197 static void yaffs_FlushInodes(struct super_block *sb)
2202 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
2203 obj = yaffs_InodeToObject(iptr);
2205 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
2207 yaffs_FlushFile(obj,1,0);
2213 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
2215 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2219 yaffs_FlushInodes(sb);
2220 yaffs_UpdateDirtyDirectories(dev);
2221 yaffs_FlushEntireDeviceCache(dev);
2223 yaffs_CheckpointSave(dev);
2227 static unsigned yaffs_bg_gc_urgency(yaffs_Device *dev)
2229 unsigned erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2230 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2231 unsigned scatteredFree = 0; /* Free chunks not in an erased block */
2233 if(erasedChunks < dev->nFreeChunks)
2234 scatteredFree = (dev->nFreeChunks - erasedChunks);
2236 if(!context->bgRunning)
2238 else if(scatteredFree < (dev->param.nChunksPerBlock * 2))
2240 else if(erasedChunks > dev->nFreeChunks/2)
2242 else if(erasedChunks > dev->nFreeChunks/4)
2248 static int yaffs_do_sync_fs(struct super_block *sb,
2249 int request_checkpoint)
2252 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2253 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2254 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2257 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2258 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
2260 sb->s_dirt ? "dirty" : "clean",
2261 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2262 oneshot_checkpoint ? " one-shot" : "" ));
2264 yaffs_GrossLock(dev);
2265 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2266 oneshot_checkpoint) &&
2267 !dev->isCheckpointed;
2269 if (sb->s_dirt || do_checkpoint) {
2270 yaffs_FlushSuperBlock(sb, !dev->isCheckpointed && do_checkpoint);
2272 if(oneshot_checkpoint)
2273 yaffs_auto_checkpoint &= ~4;
2275 yaffs_GrossUnlock(dev);
2281 * yaffs background thread functions .
2282 * yaffs_BackgroundThread() the thread function
2283 * yaffs_BackgroundStart() launches the background thread.
2284 * yaffs_BackgroundStop() cleans up the background thread.
2287 * The thread should only run after the yaffs is initialised
2288 * The thread should be stopped before yaffs is unmounted.
2289 * The thread should not do any writing while the fs is in read only.
2292 #ifdef YAFFS_COMPILE_BACKGROUND
2294 void yaffs_background_waker(unsigned long data)
2296 wake_up_process((struct task_struct *)data);
2299 static int yaffs_BackgroundThread(void *data)
2301 yaffs_Device *dev = (yaffs_Device *)data;
2302 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2303 unsigned long now = jiffies;
2304 unsigned long next_dir_update = now;
2305 unsigned long next_gc = now;
2306 unsigned long expires;
2307 unsigned int urgency;
2310 struct timer_list timer;
2312 T(YAFFS_TRACE_BACKGROUND,
2313 (TSTR("yaffs_background starting for dev %p\n"),
2318 while(context->bgRunning){
2319 T(YAFFS_TRACE_BACKGROUND,
2320 (TSTR("yaffs_background\n")));
2322 if(kthread_should_stop())
2328 yaffs_GrossLock(dev);
2332 if(time_after(now, next_dir_update) && yaffs_bg_enable){
2333 yaffs_UpdateDirtyDirectories(dev);
2334 next_dir_update = now + HZ;
2337 if(time_after(now,next_gc) && yaffs_bg_enable){
2338 if(!dev->isCheckpointed){
2339 urgency = yaffs_bg_gc_urgency(dev);
2340 gcResult = yaffs_BackgroundGarbageCollect(dev, urgency);
2342 next_gc = now + HZ/20+1;
2343 else if(urgency > 0)
2344 next_gc = now + HZ/10+1;
2346 next_gc = now + HZ * 2;
2348 * gc not running so set to next_dir_update
2349 * to cut down on wake ups
2351 next_gc = next_dir_update;
2353 yaffs_GrossUnlock(dev);
2355 expires = next_dir_update;
2356 if (time_before(next_gc,expires))
2358 if(time_before(expires,now))
2361 init_timer_on_stack(&timer);
2362 timer.expires = expires+1;
2363 timer.data = (unsigned long) current;
2364 timer.function = yaffs_background_waker;
2366 set_current_state(TASK_INTERRUPTIBLE);
2369 del_timer_sync(&timer);
2378 static int yaffs_BackgroundStart(yaffs_Device *dev)
2381 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2386 context->bgRunning = 1;
2388 context->bgThread = kthread_run(yaffs_BackgroundThread,
2389 (void *)dev,"yaffs-bg-%d",context->mount_id);
2391 if(IS_ERR(context->bgThread)){
2392 retval = PTR_ERR(context->bgThread);
2393 context->bgThread = NULL;
2394 context->bgRunning = 0;
2399 static void yaffs_BackgroundStop(yaffs_Device *dev)
2401 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToLC(dev);
2403 ctxt->bgRunning = 0;
2405 if( ctxt->bgThread){
2406 kthread_stop(ctxt->bgThread);
2407 ctxt->bgThread = NULL;
2411 static int yaffs_BackgroundThread(void *data)
2416 static int yaffs_BackgroundStart(yaffs_Device *dev)
2421 static void yaffs_BackgroundStop(yaffs_Device *dev)
2427 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2428 static void yaffs_write_super(struct super_block *sb)
2430 static int yaffs_write_super(struct super_block *sb)
2433 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2435 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2436 (TSTR("yaffs_write_super%s\n"),
2437 request_checkpoint ? " checkpt" : ""));
2439 yaffs_do_sync_fs(sb, request_checkpoint);
2441 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2447 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2448 static int yaffs_sync_fs(struct super_block *sb, int wait)
2450 static int yaffs_sync_fs(struct super_block *sb)
2453 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2455 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2456 (TSTR("yaffs_sync_fs%s\n"),
2457 request_checkpoint ? " checkpt" : ""));
2459 yaffs_do_sync_fs(sb, request_checkpoint);
2464 #ifdef YAFFS_USE_OWN_IGET
2466 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2468 struct inode *inode;
2470 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2473 (TSTR("yaffs_iget for %lu\n"), ino));
2475 inode = iget_locked(sb, ino);
2477 return ERR_PTR(-ENOMEM);
2478 if (!(inode->i_state & I_NEW))
2481 /* NB This is called as a side effect of other functions, but
2482 * we had to release the lock to prevent deadlocks, so
2483 * need to lock again.
2486 yaffs_GrossLock(dev);
2488 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2490 yaffs_FillInodeFromObject(inode, obj);
2492 yaffs_GrossUnlock(dev);
2494 unlock_new_inode(inode);
2500 static void yaffs_read_inode(struct inode *inode)
2502 /* NB This is called as a side effect of other functions, but
2503 * we had to release the lock to prevent deadlocks, so
2504 * need to lock again.
2508 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2511 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2513 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2514 yaffs_GrossLock(dev);
2516 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2518 yaffs_FillInodeFromObject(inode, obj);
2520 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2521 yaffs_GrossUnlock(dev);
2526 static YLIST_HEAD(yaffs_context_list);
2527 struct semaphore yaffs_context_lock;
2529 static void yaffs_put_super(struct super_block *sb)
2531 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2533 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2535 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2536 (TSTR("Shutting down yaffs background thread\n")));
2537 yaffs_BackgroundStop(dev);
2538 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2539 (TSTR("yaffs background thread shut down\n")));
2541 yaffs_GrossLock(dev);
2543 yaffs_FlushSuperBlock(sb,1);
2545 if (yaffs_DeviceToLC(dev)->putSuperFunc)
2546 yaffs_DeviceToLC(dev)->putSuperFunc(sb);
2549 yaffs_Deinitialise(dev);
2551 yaffs_GrossUnlock(dev);
2553 down(&yaffs_context_lock);
2554 ylist_del_init(&(yaffs_DeviceToLC(dev)->contextList));
2555 up(&yaffs_context_lock);
2557 if (yaffs_DeviceToLC(dev)->spareBuffer) {
2558 YFREE(yaffs_DeviceToLC(dev)->spareBuffer);
2559 yaffs_DeviceToLC(dev)->spareBuffer = NULL;
2566 static void yaffs_MTDPutSuper(struct super_block *sb)
2568 struct mtd_info *mtd = yaffs_DeviceToMtd(yaffs_SuperToDevice(sb));
2573 put_mtd_device(mtd);
2577 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2579 struct super_block *sb = yaffs_DeviceToLC(dev)->superBlock;
2581 T(YAFFS_TRACE_OS, (TSTR("yaffs_MarkSuperBlockDirty() sb = %p\n"), sb));
2588 int skip_checkpoint_read;
2589 int skip_checkpoint_write;
2592 int tags_ecc_overridden;
2593 int lazy_loading_enabled;
2594 int lazy_loading_overridden;
2595 int empty_lost_and_found;
2596 int empty_lost_and_found_overridden;
2599 #define MAX_OPT_LEN 30
2600 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2602 char cur_opt[MAX_OPT_LEN + 1];
2606 /* Parse through the options which is a comma seperated list */
2608 while (options_str && *options_str && !error) {
2609 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2612 while(*options_str == ',')
2615 while (*options_str && *options_str != ',') {
2616 if (p < MAX_OPT_LEN) {
2617 cur_opt[p] = *options_str;
2623 if (!strcmp(cur_opt, "inband-tags"))
2624 options->inband_tags = 1;
2625 else if (!strcmp(cur_opt, "tags-ecc-off")){
2626 options->tags_ecc_on = 0;
2627 options->tags_ecc_overridden=1;
2628 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2629 options->tags_ecc_on = 1;
2630 options->tags_ecc_overridden = 1;
2631 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2632 options->lazy_loading_enabled = 0;
2633 options->lazy_loading_overridden=1;
2634 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2635 options->lazy_loading_enabled = 1;
2636 options->lazy_loading_overridden = 1;
2637 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2638 options->empty_lost_and_found = 0;
2639 options->empty_lost_and_found_overridden=1;
2640 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2641 options->empty_lost_and_found = 1;
2642 options->empty_lost_and_found_overridden=1;
2643 } else if (!strcmp(cur_opt, "no-cache"))
2644 options->no_cache = 1;
2645 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2646 options->skip_checkpoint_read = 1;
2647 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2648 options->skip_checkpoint_write = 1;
2649 else if (!strcmp(cur_opt, "no-checkpoint")) {
2650 options->skip_checkpoint_read = 1;
2651 options->skip_checkpoint_write = 1;
2653 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2662 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2663 struct super_block *sb,
2664 void *data, int silent)
2667 struct inode *inode = NULL;
2668 struct dentry *root;
2669 yaffs_Device *dev = 0;
2670 char devname_buf[BDEVNAME_SIZE + 1];
2671 struct mtd_info *mtd;
2673 char *data_str = (char *)data;
2674 struct yaffs_LinuxContext *context = NULL;
2675 yaffs_DeviceParam *param;
2679 yaffs_options options;
2683 struct yaffs_LinuxContext *context_iterator;
2684 struct ylist_head *l;
2686 sb->s_magic = YAFFS_MAGIC;
2687 sb->s_op = &yaffs_super_ops;
2688 sb->s_flags |= MS_NOATIME;
2690 readOnly =((sb->s_flags & MS_RDONLY) != 0);
2693 #ifdef YAFFS_COMPILE_EXPORTFS
2694 sb->s_export_op = &yaffs_export_ops;
2698 printk(KERN_INFO "yaffs: sb is NULL\n");
2699 else if (!sb->s_dev)
2700 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2701 else if (!yaffs_devname(sb, devname_buf))
2702 printk(KERN_INFO "yaffs: devname is NULL\n");
2704 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2706 yaffs_devname(sb, devname_buf),
2707 readOnly ? "ro" : "rw");
2712 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2714 memset(&options, 0, sizeof(options));
2716 if (yaffs_parse_options(&options, data_str)) {
2717 /* Option parsing failed */
2722 sb->s_blocksize = PAGE_CACHE_SIZE;
2723 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2726 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffsVersion));
2728 (TSTR("yaffs_read_super: block size %d\n"),
2729 (int)(sb->s_blocksize)));
2731 T(YAFFS_TRACE_ALWAYS,
2732 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2733 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2734 yaffs_devname(sb, devname_buf)));
2736 /* Check it's an mtd device..... */
2737 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2738 return NULL; /* This isn't an mtd device */
2740 /* Get the device */
2741 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2743 T(YAFFS_TRACE_ALWAYS,
2744 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2748 /* Check it's NAND */
2749 if (mtd->type != MTD_NANDFLASH) {
2750 T(YAFFS_TRACE_ALWAYS,
2751 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2756 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2757 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2758 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2759 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2760 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2761 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2762 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2763 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2764 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2765 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2766 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2767 T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2769 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2772 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2774 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2775 T(YAFFS_TRACE_ALWAYS,
2776 (TSTR("yaffs: auto selecting yaffs2\n")));
2780 /* Added NCB 26/5/2006 for completeness */
2781 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2782 T(YAFFS_TRACE_ALWAYS,
2783 (TSTR("yaffs: auto selecting yaffs1\n")));
2789 if (yaffsVersion == 2) {
2790 /* Check for version 2 style functions */
2792 !mtd->block_isbad ||
2793 !mtd->block_markbad ||
2796 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2797 !mtd->read_oob || !mtd->write_oob) {
2800 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2802 T(YAFFS_TRACE_ALWAYS,
2803 (TSTR("yaffs: MTD device does not support required "
2808 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2809 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2810 !options.inband_tags) {
2811 T(YAFFS_TRACE_ALWAYS,
2812 (TSTR("yaffs: MTD device does not have the "
2813 "right page sizes\n")));
2817 /* Check for V1 style functions */
2821 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2822 !mtd->read_oob || !mtd->write_oob) {
2825 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2827 T(YAFFS_TRACE_ALWAYS,
2828 (TSTR("yaffs: MTD device does not support required "
2833 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2834 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2835 T(YAFFS_TRACE_ALWAYS,
2836 (TSTR("yaffs: MTD device does not support have the "
2837 "right page sizes\n")));
2842 /* OK, so if we got here, we have an MTD that's NAND and looks
2843 * like it has the right capabilities
2844 * Set the yaffs_Device up for mtd
2847 if (!readOnly && !(mtd->flags & MTD_WRITEABLE)){
2849 printk(KERN_INFO "yaffs: mtd is read only, setting superblock read only");
2850 sb->s_flags |= MS_RDONLY;
2853 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2854 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2856 if(!dev || !context ){
2866 /* Deep shit could not allocate device structure */
2867 T(YAFFS_TRACE_ALWAYS,
2868 (TSTR("yaffs_read_super: Failed trying to allocate "
2869 "yaffs_Device. \n")));
2872 memset(dev, 0, sizeof(yaffs_Device));
2873 param = &(dev->param);
2875 memset(context,0,sizeof(struct yaffs_LinuxContext));
2876 dev->osContext = context;
2877 YINIT_LIST_HEAD(&(context->contextList));
2879 context->superBlock = sb;
2881 dev->readOnly = readOnly;
2883 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2884 sb->s_fs_info = dev;
2886 sb->u.generic_sbp = dev;
2889 dev->driverContext = mtd;
2890 param->name = mtd->name;
2892 /* Set up the memory size parameters.... */
2894 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2896 param->startBlock = 0;
2897 param->endBlock = nBlocks - 1;
2898 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2899 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2900 param->nReservedBlocks = 5;
2901 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2902 param->inbandTags = options.inband_tags;
2904 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2905 param->disableLazyLoad = 1;
2907 #ifdef CONFIG_YAFFS_XATTR
2908 param->enableXattr = 1;
2910 if(options.lazy_loading_overridden)
2911 param->disableLazyLoad = !options.lazy_loading_enabled;
2913 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2914 param->noTagsECC = 1;
2917 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2919 param->deferDirectoryUpdate = 1;
2922 if(options.tags_ecc_overridden)
2923 param->noTagsECC = !options.tags_ecc_on;
2925 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2926 param->emptyLostAndFound = 1;
2929 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2930 param->refreshPeriod = 0;
2932 param->refreshPeriod = 500;
2935 if(options.empty_lost_and_found_overridden)
2936 param->emptyLostAndFound = options.empty_lost_and_found;
2938 /* ... and the functions. */
2939 if (yaffsVersion == 2) {
2940 param->writeChunkWithTagsToNAND =
2941 nandmtd2_WriteChunkWithTagsToNAND;
2942 param->readChunkWithTagsFromNAND =
2943 nandmtd2_ReadChunkWithTagsFromNAND;
2944 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2945 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2946 yaffs_DeviceToLC(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2947 param->isYaffs2 = 1;
2948 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2949 param->totalBytesPerChunk = mtd->writesize;
2950 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2952 param->totalBytesPerChunk = mtd->oobblock;
2953 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2955 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2957 param->startBlock = 0;
2958 param->endBlock = nBlocks - 1;
2960 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2961 /* use the MTD interface in yaffs_mtdif1.c */
2962 param->writeChunkWithTagsToNAND =
2963 nandmtd1_WriteChunkWithTagsToNAND;
2964 param->readChunkWithTagsFromNAND =
2965 nandmtd1_ReadChunkWithTagsFromNAND;
2966 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2967 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2969 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2970 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2972 param->isYaffs2 = 0;
2974 /* ... and common functions */
2975 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2976 param->initialiseNAND = nandmtd_InitialiseNAND;
2978 yaffs_DeviceToLC(dev)->putSuperFunc = yaffs_MTDPutSuper;
2980 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2981 param->gcControl = yaffs_gc_control_callback;
2983 yaffs_DeviceToLC(dev)->superBlock= sb;
2986 #ifndef CONFIG_YAFFS_DOES_ECC
2987 param->useNANDECC = 1;
2990 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2991 param->wideTnodesDisabled = 1;
2994 param->skipCheckpointRead = options.skip_checkpoint_read;
2995 param->skipCheckpointWrite = options.skip_checkpoint_write;
2997 down(&yaffs_context_lock);
2998 /* Get a mount id */
3000 for(mount_id=0; ! found; mount_id++){
3002 ylist_for_each(l,&yaffs_context_list){
3003 context_iterator = ylist_entry(l,struct yaffs_LinuxContext,contextList);
3004 if(context_iterator->mount_id == mount_id)
3008 context->mount_id = mount_id;
3010 ylist_add_tail(&(yaffs_DeviceToLC(dev)->contextList), &yaffs_context_list);
3011 up(&yaffs_context_lock);
3013 /* Directory search handling...*/
3014 YINIT_LIST_HEAD(&(yaffs_DeviceToLC(dev)->searchContexts));
3015 param->removeObjectCallback = yaffs_RemoveObjectCallback;
3017 init_MUTEX(&(yaffs_DeviceToLC(dev)->grossLock));
3019 yaffs_GrossLock(dev);
3021 err = yaffs_GutsInitialise(dev);
3024 (TSTR("yaffs_read_super: guts initialised %s\n"),
3025 (err == YAFFS_OK) ? "OK" : "FAILED"));
3028 yaffs_BackgroundStart(dev);
3030 if(!context->bgThread)
3031 param->deferDirectoryUpdate = 0;
3034 /* Release lock before yaffs_get_inode() */
3035 yaffs_GrossUnlock(dev);
3037 /* Create root inode */
3038 if (err == YAFFS_OK)
3039 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3045 inode->i_op = &yaffs_dir_inode_operations;
3046 inode->i_fop = &yaffs_dir_operations;
3048 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
3050 root = d_alloc_root(inode);
3052 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
3059 sb->s_dirt = !dev->isCheckpointed;
3060 T(YAFFS_TRACE_ALWAYS,
3061 (TSTR("yaffs_read_super: isCheckpointed %d\n"),
3062 dev->isCheckpointed));
3064 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
3069 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3070 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3073 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3076 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3077 static int yaffs_read_super(struct file_system_type *fs,
3078 int flags, const char *dev_name,
3079 void *data, struct vfsmount *mnt)
3082 return get_sb_bdev(fs, flags, dev_name, data,
3083 yaffs_internal_read_super_mtd, mnt);
3086 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3087 int flags, const char *dev_name,
3091 return get_sb_bdev(fs, flags, dev_name, data,
3092 yaffs_internal_read_super_mtd);
3096 static struct file_system_type yaffs_fs_type = {
3097 .owner = THIS_MODULE,
3099 .get_sb = yaffs_read_super,
3100 .kill_sb = kill_block_super,
3101 .fs_flags = FS_REQUIRES_DEV,
3104 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3107 return yaffs_internal_read_super(1, sb, data, silent);
3110 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3115 #ifdef CONFIG_YAFFS_YAFFS2
3117 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3118 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3121 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3124 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3125 static int yaffs2_read_super(struct file_system_type *fs,
3126 int flags, const char *dev_name, void *data,
3127 struct vfsmount *mnt)
3129 return get_sb_bdev(fs, flags, dev_name, data,
3130 yaffs2_internal_read_super_mtd, mnt);
3133 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3134 int flags, const char *dev_name,
3138 return get_sb_bdev(fs, flags, dev_name, data,
3139 yaffs2_internal_read_super_mtd);
3143 static struct file_system_type yaffs2_fs_type = {
3144 .owner = THIS_MODULE,
3146 .get_sb = yaffs2_read_super,
3147 .kill_sb = kill_block_super,
3148 .fs_flags = FS_REQUIRES_DEV,
3151 static struct super_block *yaffs2_read_super(struct super_block *sb,
3152 void *data, int silent)
3154 return yaffs_internal_read_super(2, sb, data, silent);
3157 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3161 #endif /* CONFIG_YAFFS_YAFFS2 */
3163 static struct proc_dir_entry *my_proc_entry;
3164 static struct proc_dir_entry *debug_proc_entry;
3166 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
3168 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
3169 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
3170 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
3171 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
3172 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
3173 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
3174 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
3175 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
3176 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
3177 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
3178 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
3179 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
3181 buf += sprintf(buf, "\n");
3187 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
3189 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3190 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3191 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3192 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3193 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3194 buf += sprintf(buf, "\n");
3195 buf += sprintf(buf, "nTnodes............ %d\n", dev->nTnodes);
3196 buf += sprintf(buf, "nObjects........... %d\n", dev->nObjects);
3197 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3198 buf += sprintf(buf, "\n");
3199 buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
3200 buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
3201 buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
3202 buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
3203 buf += sprintf(buf, "allGCs............. %u\n", dev->allGCs);
3204 buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGCs);
3205 buf += sprintf(buf, "oldestDirtyGCs..... %u\n", dev->oldestDirtyGCs);
3206 buf += sprintf(buf, "nGCBlocks.......... %u\n", dev->nGCBlocks);
3207 buf += sprintf(buf, "backgroundGCs...... %u\n", dev->backgroundGCs);
3208 buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
3209 buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
3210 buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
3211 buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
3212 buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
3213 buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
3214 buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
3215 buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
3216 buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
3217 buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
3219 sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
3224 static int yaffs_proc_read(char *page,
3226 off_t offset, int count, int *eof, void *data)
3228 struct ylist_head *item;
3233 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3234 * We use 'offset' (*ppos) to indicate where we are in devList.
3235 * This also assumes the user has posted a read buffer large
3236 * enough to hold the complete output; but that's life in /proc.
3241 /* Print header first */
3243 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__"\n");
3245 buf += sprintf(buf,"\n");
3249 down(&yaffs_context_lock);
3251 /* Locate and print the Nth entry. Order N-squared but N is small. */
3252 ylist_for_each(item, &yaffs_context_list) {
3253 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3254 yaffs_Device *dev = dc->dev;
3256 if (n < (step & ~1)) {
3261 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
3262 buf = yaffs_dump_dev_part0(buf, dev);
3264 buf = yaffs_dump_dev_part1(buf, dev);
3268 up(&yaffs_context_lock);
3271 return buf - page < count ? buf - page : count;
3274 static int yaffs_stats_proc_read(char *page,
3276 off_t offset, int count, int *eof, void *data)
3278 struct ylist_head *item;
3282 down(&yaffs_context_lock);
3284 /* Locate and print the Nth entry. Order N-squared but N is small. */
3285 ylist_for_each(item, &yaffs_context_list) {
3286 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3287 yaffs_Device *dev = dc->dev;
3291 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3293 buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
3294 n, dev->nFreeChunks, erasedChunks,
3295 dev->backgroundGCs, dev->oldestDirtyGCs,
3296 dev->nObjects, dev->nTnodes);
3298 up(&yaffs_context_lock);
3301 return buf - page < count ? buf - page : count;
3305 * Set the verbosity of the warnings and error messages.
3307 * Note that the names can only be a..z or _ with the current code.
3312 unsigned mask_bitfield;
3314 {"allocate", YAFFS_TRACE_ALLOCATE},
3315 {"always", YAFFS_TRACE_ALWAYS},
3316 {"background", YAFFS_TRACE_BACKGROUND},
3317 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3318 {"buffers", YAFFS_TRACE_BUFFERS},
3319 {"bug", YAFFS_TRACE_BUG},
3320 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3321 {"deletion", YAFFS_TRACE_DELETION},
3322 {"erase", YAFFS_TRACE_ERASE},
3323 {"error", YAFFS_TRACE_ERROR},
3324 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3325 {"gc", YAFFS_TRACE_GC},
3326 {"lock", YAFFS_TRACE_LOCK},
3327 {"mtd", YAFFS_TRACE_MTD},
3328 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3329 {"os", YAFFS_TRACE_OS},
3330 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3331 {"scan", YAFFS_TRACE_SCAN},
3332 {"tracing", YAFFS_TRACE_TRACING},
3333 {"sync", YAFFS_TRACE_SYNC},
3334 {"write", YAFFS_TRACE_WRITE},
3336 {"verify", YAFFS_TRACE_VERIFY},
3337 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3338 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3339 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3341 {"all", 0xffffffff},
3346 #define MAX_MASK_NAME_LENGTH 40
3347 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3348 unsigned long count, void *data)
3350 unsigned rg = 0, mask_bitfield;
3354 char substring[MAX_MASK_NAME_LENGTH + 1];
3360 rg = yaffs_traceMask;
3362 while (!done && (pos < count)) {
3364 while ((pos < count) && isspace(buf[pos]))
3381 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3383 if (end > buf + pos) {
3384 mask_name = "numeral";
3385 len = end - (buf + pos);
3389 for (x = buf + pos, i = 0;
3390 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3391 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3393 substring[i] = '\0';
3395 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3396 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3397 mask_name = mask_flags[i].mask_name;
3398 mask_bitfield = mask_flags[i].mask_bitfield;
3405 if (mask_name != NULL) {
3409 rg &= ~mask_bitfield;
3412 rg |= mask_bitfield;
3418 rg |= mask_bitfield;
3424 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3426 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
3428 if (rg & YAFFS_TRACE_ALWAYS) {
3429 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3431 flag = ((rg & mask_flags[i].mask_bitfield) ==
3432 mask_flags[i].mask_bitfield) ? '+' : '-';
3433 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3441 static int yaffs_proc_write(struct file *file, const char *buf,
3442 unsigned long count, void *data)
3444 return yaffs_proc_write_trace_options(file, buf, count, data);
3447 /* Stuff to handle installation of file systems */
3448 struct file_system_to_install {
3449 struct file_system_type *fst;
3453 static struct file_system_to_install fs_to_install[] = {
3454 {&yaffs_fs_type, 0},
3455 {&yaffs2_fs_type, 0},
3459 static int __init init_yaffs_fs(void)
3462 struct file_system_to_install *fsinst;
3464 T(YAFFS_TRACE_ALWAYS,
3465 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3467 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3468 T(YAFFS_TRACE_ALWAYS,
3469 (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3475 init_MUTEX(&yaffs_context_lock);
3477 /* Install the proc_fs entries */
3478 my_proc_entry = create_proc_entry("yaffs",
3482 if (my_proc_entry) {
3483 my_proc_entry->write_proc = yaffs_proc_write;
3484 my_proc_entry->read_proc = yaffs_proc_read;
3485 my_proc_entry->data = NULL;
3489 debug_proc_entry = create_proc_entry("yaffs_stats",
3493 if (debug_proc_entry) {
3494 debug_proc_entry->write_proc = NULL;
3495 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3496 debug_proc_entry->data = NULL;
3500 /* Now add the file system entries */
3502 fsinst = fs_to_install;
3504 while (fsinst->fst && !error) {
3505 error = register_filesystem(fsinst->fst);
3507 fsinst->installed = 1;
3511 /* Any errors? uninstall */
3513 fsinst = fs_to_install;
3515 while (fsinst->fst) {
3516 if (fsinst->installed) {
3517 unregister_filesystem(fsinst->fst);
3518 fsinst->installed = 0;
3527 static void __exit exit_yaffs_fs(void)
3530 struct file_system_to_install *fsinst;
3532 T(YAFFS_TRACE_ALWAYS,
3533 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3535 remove_proc_entry("yaffs", YPROC_ROOT);
3536 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3538 fsinst = fs_to_install;
3540 while (fsinst->fst) {
3541 if (fsinst->installed) {
3542 unregister_filesystem(fsinst->fst);
3543 fsinst->installed = 0;
3549 module_init(init_yaffs_fs)
3550 module_exit(exit_yaffs_fs)
3552 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3553 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3554 MODULE_LICENSE("GPL");