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
38 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6, 23))
39 #define YAFFS_COMPILE_FREEZER
43 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28))
44 #define YAFFS_COMPILE_EXPORTFS
47 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
48 #define YAFFS_USE_SETATTR_COPY
49 #define YAFFS_USE_TRUNCATE_SETSIZE
51 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
52 #define YAFFS_HAS_EVICT_INODE
55 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
56 #define YAFFS_NEW_FOLLOW_LINK 1
58 #define YAFFS_NEW_FOLLOW_LINK 0
61 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
62 #include <linux/config.h>
65 #include <linux/kernel.h>
66 #include <linux/module.h>
67 #include <linux/slab.h>
68 #include <linux/init.h>
70 #include <linux/proc_fs.h>
71 #include <linux/smp_lock.h>
72 #include <linux/pagemap.h>
73 #include <linux/mtd/mtd.h>
74 #include <linux/interrupt.h>
75 #include <linux/string.h>
76 #include <linux/ctype.h>
78 #if (YAFFS_NEW_FOLLOW_LINK == 1)
79 #include <linux/namei.h>
82 #ifdef YAFFS_COMPILE_EXPORTFS
83 #include <linux/exportfs.h>
86 #ifdef YAFFS_COMPILE_BACKGROUND
87 #include <linux/kthread.h>
88 #include <linux/delay.h>
90 #ifdef YAFFS_COMPILE_FREEZER
91 #include <linux/freezer.h>
94 #include <asm/div64.h>
96 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
98 #include <linux/statfs.h>
100 #define UnlockPage(p) unlock_page(p)
101 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
103 /* FIXME: use sb->s_id instead ? */
104 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
108 #include <linux/locks.h>
109 #define BDEVNAME_SIZE 0
110 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
112 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
113 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
119 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
120 #define YPROC_ROOT (&proc_root)
122 #define YPROC_ROOT NULL
125 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
126 #define Y_INIT_TIMER(a) init_timer(a)
128 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
131 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
132 #define WRITE_SIZE_STR "writesize"
133 #define WRITE_SIZE(mtd) ((mtd)->writesize)
135 #define WRITE_SIZE_STR "oobblock"
136 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
139 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
140 #define YAFFS_USE_WRITE_BEGIN_END 1
142 #define YAFFS_USE_WRITE_BEGIN_END 0
145 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
146 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
148 uint64_t result = partition_size;
149 do_div(result, block_size);
150 return (uint32_t)result;
153 #define YCALCBLOCKS(s, b) ((s)/(b))
156 #include <linux/uaccess.h>
157 #include <linux/mtd/mtd.h>
159 #include "yportenv.h"
160 #include "yaffs_trace.h"
161 #include "yaffs_guts.h"
163 #include "yaffs_linux.h"
165 #include "yaffs_mtdif.h"
166 #include "yaffs_mtdif1.h"
167 #include "yaffs_mtdif2.h"
169 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
170 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
171 unsigned int yaffs_auto_checkpoint = 1;
172 unsigned int yaffs_gc_control = 1;
173 unsigned int yaffs_bg_enable = 1;
175 /* Module Parameters */
176 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
177 module_param(yaffs_traceMask, uint, 0644);
178 module_param(yaffs_wr_attempts, uint, 0644);
179 module_param(yaffs_auto_checkpoint, uint, 0644);
180 module_param(yaffs_gc_control, uint, 0644);
181 module_param(yaffs_bg_enable, uint, 0644);
183 MODULE_PARM(yaffs_traceMask, "i");
184 MODULE_PARM(yaffs_wr_attempts, "i");
185 MODULE_PARM(yaffs_auto_checkpoint, "i");
186 MODULE_PARM(yaffs_gc_control, "i");
189 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
190 /* use iget and read_inode */
191 #define Y_IGET(sb, inum) iget((sb), (inum))
192 static void yaffs_read_inode(struct inode *inode);
195 /* Call local equivalent */
196 #define YAFFS_USE_OWN_IGET
197 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
199 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
202 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
203 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
205 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
208 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
209 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
211 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
212 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
214 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
218 #define update_dir_time(dir) do {\
219 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
222 static void yaffs_put_super(struct super_block *sb);
224 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
226 static ssize_t yaffs_hold_space(struct file *f);
227 static void yaffs_release_space(struct file *f);
229 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
230 static int yaffs_file_flush(struct file *file, fl_owner_t id);
232 static int yaffs_file_flush(struct file *file);
235 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
236 static int yaffs_sync_object(struct file *file, int datasync);
238 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
242 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
244 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
245 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
246 struct nameidata *n);
247 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
248 struct nameidata *n);
250 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
251 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
253 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
254 struct dentry *dentry);
255 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
256 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
257 const char *symname);
258 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
260 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
261 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
264 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
267 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
268 struct inode *new_dir, struct dentry *new_dentry);
269 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
271 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
272 static int yaffs_sync_fs(struct super_block *sb, int wait);
273 static void yaffs_write_super(struct super_block *sb);
275 static int yaffs_sync_fs(struct super_block *sb);
276 static int yaffs_write_super(struct super_block *sb);
279 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
280 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
281 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
282 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
284 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
287 #ifdef YAFFS_HAS_PUT_INODE
288 static void yaffs_put_inode(struct inode *inode);
291 #ifdef YAFFS_HAS_EVICT_INODE
292 static void yaffs_evict_inode(struct inode *);
294 static void yaffs_delete_inode(struct inode *);
295 static void yaffs_clear_inode(struct inode *);
298 static int yaffs_readpage(struct file *file, struct page *page);
299 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
300 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
302 static int yaffs_writepage(struct page *page);
305 #ifdef CONFIG_YAFFS_XATTR
306 int yaffs_setxattr(struct dentry *dentry, const char *name,
307 const void *value, size_t size, int flags);
308 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
310 int yaffs_removexattr(struct dentry *dentry, const char *name);
311 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
315 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
316 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
317 loff_t pos, unsigned len, unsigned flags,
318 struct page **pagep, void **fsdata);
319 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
320 loff_t pos, unsigned len, unsigned copied,
321 struct page *pg, void *fsdadata);
323 static int yaffs_prepare_write(struct file *f, struct page *pg,
324 unsigned offset, unsigned to);
325 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
330 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
332 #if (YAFFS_NEW_FOLLOW_LINK == 1)
333 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
334 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
336 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
339 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev);
341 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
343 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
346 static struct address_space_operations yaffs_file_address_operations = {
347 .readpage = yaffs_readpage,
348 .writepage = yaffs_writepage,
349 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
350 .write_begin = yaffs_write_begin,
351 .write_end = yaffs_write_end,
353 .prepare_write = yaffs_prepare_write,
354 .commit_write = yaffs_commit_write,
359 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
360 static const struct file_operations yaffs_file_operations = {
361 .read = do_sync_read,
362 .write = do_sync_write,
363 .aio_read = generic_file_aio_read,
364 .aio_write = generic_file_aio_write,
365 .mmap = generic_file_mmap,
366 .flush = yaffs_file_flush,
367 .fsync = yaffs_sync_object,
368 .splice_read = generic_file_splice_read,
369 .splice_write = generic_file_splice_write,
370 .llseek = generic_file_llseek,
373 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
375 static const struct file_operations yaffs_file_operations = {
376 .read = do_sync_read,
377 .write = do_sync_write,
378 .aio_read = generic_file_aio_read,
379 .aio_write = generic_file_aio_write,
380 .mmap = generic_file_mmap,
381 .flush = yaffs_file_flush,
382 .fsync = yaffs_sync_object,
383 .sendfile = generic_file_sendfile,
388 static const struct file_operations yaffs_file_operations = {
389 .read = generic_file_read,
390 .write = generic_file_write,
391 .mmap = generic_file_mmap,
392 .flush = yaffs_file_flush,
393 .fsync = yaffs_sync_object,
394 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
395 .sendfile = generic_file_sendfile,
400 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
401 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
403 void * kaddr = kmap_atomic(page, KM_USER0);
404 memset(kaddr + start, 0, end - start);
405 kunmap_atomic(kaddr, KM_USER0);
406 flush_dcache_page(page);
411 static const struct inode_operations yaffs_file_inode_operations = {
412 .setattr = yaffs_setattr,
413 #ifdef CONFIG_YAFFS_XATTR
414 .setxattr = yaffs_setxattr,
415 .getxattr = yaffs_getxattr,
416 .listxattr = yaffs_listxattr,
417 .removexattr = yaffs_removexattr,
421 static const struct inode_operations yaffs_symlink_inode_operations = {
422 .readlink = yaffs_readlink,
423 .follow_link = yaffs_follow_link,
424 #if (YAFFS_NEW_FOLLOW_LINK == 1)
425 .put_link = yaffs_put_link,
427 .setattr = yaffs_setattr,
428 #ifdef CONFIG_YAFFS_XATTR
429 .setxattr = yaffs_setxattr,
430 .getxattr = yaffs_getxattr,
431 .listxattr = yaffs_listxattr,
432 .removexattr = yaffs_removexattr,
436 static const struct inode_operations yaffs_dir_inode_operations = {
437 .create = yaffs_create,
438 .lookup = yaffs_lookup,
440 .unlink = yaffs_unlink,
441 .symlink = yaffs_symlink,
442 .mkdir = yaffs_mkdir,
443 .rmdir = yaffs_unlink,
444 .mknod = yaffs_mknod,
445 .rename = yaffs_rename,
446 .setattr = yaffs_setattr,
447 #ifdef CONFIG_YAFFS_XATTR
448 .setxattr = yaffs_setxattr,
449 .getxattr = yaffs_getxattr,
450 .listxattr = yaffs_listxattr,
451 .removexattr = yaffs_removexattr,
455 static const struct file_operations yaffs_dir_operations = {
456 .read = generic_read_dir,
457 .readdir = yaffs_readdir,
458 .fsync = yaffs_sync_object,
459 .llseek = yaffs_dir_llseek,
462 static const struct super_operations yaffs_super_ops = {
463 .statfs = yaffs_statfs,
465 #ifndef YAFFS_USE_OWN_IGET
466 .read_inode = yaffs_read_inode,
468 #ifdef YAFFS_HAS_PUT_INODE
469 .put_inode = yaffs_put_inode,
471 .put_super = yaffs_put_super,
472 #ifdef YAFFS_HAS_EVICT_INODE
473 .evict_inode = yaffs_evict_inode,
475 .delete_inode = yaffs_delete_inode,
476 .clear_inode = yaffs_clear_inode,
478 .sync_fs = yaffs_sync_fs,
479 .write_super = yaffs_write_super,
483 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
485 #ifdef YAFFS_USE_SETATTR_COPY
486 setattr_copy(inode,attr);
489 return inode_setattr(inode, attr);
494 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
496 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
497 truncate_setsize(inode,newsize);
500 truncate_inode_pages(&inode->i_data,newsize);
506 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
508 return yaffs_gc_control;
511 static void yaffs_GrossLock(yaffs_Device *dev)
513 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
514 down(&(yaffs_DeviceToLC(dev)->grossLock));
515 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
518 static void yaffs_GrossUnlock(yaffs_Device *dev)
520 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
521 up(&(yaffs_DeviceToLC(dev)->grossLock));
524 #ifdef YAFFS_COMPILE_EXPORTFS
526 static struct inode *
527 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
529 return Y_IGET(sb, ino);
532 static struct dentry *
533 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
535 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
538 static struct dentry *
539 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
541 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
544 struct dentry *yaffs2_get_parent(struct dentry *dentry)
547 struct super_block *sb = dentry->d_inode->i_sb;
548 struct dentry *parent = ERR_PTR(-ENOENT);
550 unsigned long parent_ino;
552 yaffs_Object *parent_obj;
554 d_obj = yaffs_InodeToObject(dentry->d_inode);
557 parent_obj = d_obj->parent;
559 parent_ino = yaffs_GetObjectInode(parent_obj);
560 inode = Y_IGET(sb, parent_ino);
563 parent = ERR_CAST(inode);
565 parent = d_obtain_alias(inode);
566 if (!IS_ERR(parent)) {
567 parent = ERR_PTR(-ENOMEM);
577 /* Just declare a zero structure as a NULL value implies
578 * using the default functions of exportfs.
581 static struct export_operations yaffs_export_ops =
583 .fh_to_dentry = yaffs2_fh_to_dentry,
584 .fh_to_parent = yaffs2_fh_to_parent,
585 .get_parent = yaffs2_get_parent,
590 /*-----------------------------------------------------------------*/
591 /* Directory search context allows us to unlock access to yaffs during
592 * filldir without causing problems with the directory being modified.
593 * This is similar to the tried and tested mechanism used in yaffs direct.
595 * A search context iterates along a doubly linked list of siblings in the
596 * directory. If the iterating object is deleted then this would corrupt
597 * the list iteration, likely causing a crash. The search context avoids
598 * this by using the removeObjectCallback to move the search context to the
599 * next object before the object is deleted.
601 * Many readdirs (and thus seach conexts) may be alive simulateously so
602 * each yaffs_Device has a list of these.
604 * A seach context lives for the duration of a readdir.
606 * All these functions must be called while yaffs is locked.
609 struct yaffs_SearchContext {
611 yaffs_Object *dirObj;
612 yaffs_Object *nextReturn;
613 struct ylist_head others;
617 * yaffs_NewSearch() creates a new search context, initialises it and
618 * adds it to the device's search context list.
620 * Called at start of readdir.
622 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
624 yaffs_Device *dev = dir->myDev;
625 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
629 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
630 sc->nextReturn = NULL;
632 sc->nextReturn = ylist_entry(
633 dir->variant.directoryVariant.children.next,
634 yaffs_Object,siblings);
635 YINIT_LIST_HEAD(&sc->others);
636 ylist_add(&sc->others,&(yaffs_DeviceToLC(dev)->searchContexts));
642 * yaffs_EndSearch() disposes of a search context and cleans up.
644 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
647 ylist_del(&sc->others);
653 * yaffs_SearchAdvance() moves a search context to the next object.
654 * Called when the search iterates or when an object removal causes
655 * the search context to be moved to the next object.
657 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
662 if( sc->nextReturn == NULL ||
663 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
664 sc->nextReturn = NULL;
666 struct ylist_head *next = sc->nextReturn->siblings.next;
668 if( next == &sc->dirObj->variant.directoryVariant.children)
669 sc->nextReturn = NULL; /* end of list */
671 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
676 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
677 * We check open search contexts and advance any which are currently
678 * on the object being iterated.
680 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
683 struct ylist_head *i;
684 struct yaffs_SearchContext *sc;
685 struct ylist_head *search_contexts = &(yaffs_DeviceToLC(obj->myDev)->searchContexts);
688 /* Iterate through the directory search contexts.
689 * If any are currently on the object being removed, then advance
690 * the search context to the next object to prevent a hanging pointer.
692 ylist_for_each(i, search_contexts) {
694 sc = ylist_entry(i, struct yaffs_SearchContext,others);
695 if(sc->nextReturn == obj)
696 yaffs_SearchAdvance(sc);
703 /*-----------------------------------------------------------------*/
705 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
708 unsigned char *alias;
711 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
713 yaffs_GrossLock(dev);
715 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
717 yaffs_GrossUnlock(dev);
722 ret = vfs_readlink(dentry, buffer, buflen, alias);
727 #if (YAFFS_NEW_FOLLOW_LINK == 1)
728 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
730 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
733 unsigned char *alias;
735 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
737 yaffs_GrossLock(dev);
739 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
740 yaffs_GrossUnlock(dev);
747 #if (YAFFS_NEW_FOLLOW_LINK == 1)
748 nd_set_link(nd, alias);
753 ret = vfs_follow_link(nd, alias);
760 #if (YAFFS_NEW_FOLLOW_LINK == 1)
761 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) {
766 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
770 * Lookup is used to find objects in the fs
772 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
774 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
777 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
781 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
783 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
785 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
786 yaffs_GrossLock(dev);
789 (TSTR("yaffs_lookup for %d:%s\n"),
790 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
792 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
793 dentry->d_name.name);
795 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
797 /* Can't hold gross lock when calling yaffs_get_inode() */
798 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
799 yaffs_GrossUnlock(dev);
803 (TSTR("yaffs_lookup found %d\n"), obj->objectId));
805 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
809 (TSTR("yaffs_loookup dentry \n")));
810 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
811 * d_add even if NULL inode */
813 /*dget(dentry); // try to solve directory bug */
814 d_add(dentry, inode);
822 T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
826 /* added NCB for 2.5/6 compatability - forces add even if inode is
827 * NULL which creates dentry hash */
828 d_add(dentry, inode);
834 #ifdef YAFFS_HAS_PUT_INODE
836 /* For now put inode is just for debugging
837 * Put inode is called when the inode **structure** is put.
839 static void yaffs_put_inode(struct inode *inode)
842 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
843 atomic_read(&inode->i_count)));
849 static void yaffs_UnstitchObject(struct inode *inode, yaffs_Object *obj)
851 /* Clear the association between the inode and
855 yaffs_InodeToObjectLV(inode) = NULL;
857 /* If the object freeing was deferred, then the real
859 * This should fix the inode inconsistency problem.
861 yaffs_HandleDeferedFree(obj);
864 #ifdef YAFFS_HAS_EVICT_INODE
865 /* yaffs_evict_inode combines into one operation what was previously done in
866 * yaffs_clear_inode() and yaffs_delete_inode()
869 static void yaffs_evict_inode( struct inode *inode)
875 obj = yaffs_InodeToObject(inode);
878 (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
879 atomic_read(&inode->i_count),
880 obj ? "object exists" : "null object"));
882 if (!inode->i_nlink && !is_bad_inode(inode))
884 truncate_inode_pages(&inode->i_data,0);
885 end_writeback(inode);
889 yaffs_GrossLock(dev);
890 yaffs_DeleteObject(obj);
891 yaffs_GrossUnlock(dev);
895 yaffs_GrossLock(dev);
896 yaffs_UnstitchObject(inode,obj);
897 yaffs_GrossUnlock(dev);
904 /* clear is called to tell the fs to release any per-inode data it holds.
905 * The object might still exist on disk and is just being thrown out of the cache
906 * or else the object has actually been deleted and we're being called via
908 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
911 static void yaffs_clear_inode(struct inode *inode)
916 obj = yaffs_InodeToObject(inode);
919 (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
920 atomic_read(&inode->i_count),
921 obj ? "object exists" : "null object"));
925 yaffs_GrossLock(dev);
926 yaffs_UnstitchObject(inode,obj);
927 yaffs_GrossUnlock(dev);
932 /* delete is called when the link count is zero and the inode
933 * is put (ie. nobody wants to know about it anymore, time to
935 * NB Must call clear_inode()
937 static void yaffs_delete_inode(struct inode *inode)
939 yaffs_Object *obj = yaffs_InodeToObject(inode);
943 (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
944 atomic_read(&inode->i_count),
945 obj ? "object exists" : "null object"));
949 yaffs_GrossLock(dev);
950 yaffs_DeleteObject(obj);
951 yaffs_GrossUnlock(dev);
953 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
954 truncate_inode_pages(&inode->i_data, 0);
961 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
962 static int yaffs_file_flush(struct file *file, fl_owner_t id)
964 static int yaffs_file_flush(struct file *file)
967 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
969 yaffs_Device *dev = obj->myDev;
972 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->objectId,
973 obj->dirty ? "dirty" : "clean"));
975 yaffs_GrossLock(dev);
977 yaffs_FlushFile(obj, 1, 0);
979 yaffs_GrossUnlock(dev);
984 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
986 /* Lifted from jffs2 */
989 unsigned char *pg_buf;
995 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
996 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
997 (unsigned)PAGE_CACHE_SIZE));
999 obj = yaffs_DentryToObject(f->f_dentry);
1003 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1004 BUG_ON(!PageLocked(pg));
1006 if (!PageLocked(pg))
1011 /* FIXME: Can kmap fail? */
1013 yaffs_GrossLock(dev);
1015 ret = yaffs_ReadDataFromFile(obj, pg_buf,
1016 pg->index << PAGE_CACHE_SHIFT,
1019 yaffs_GrossUnlock(dev);
1025 ClearPageUptodate(pg);
1028 SetPageUptodate(pg);
1032 flush_dcache_page(pg);
1035 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
1039 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1041 int ret = yaffs_readpage_nolock(f, pg);
1046 static int yaffs_readpage(struct file *f, struct page *pg)
1050 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
1051 ret=yaffs_readpage_unlock(f, pg);
1052 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
1056 /* writepage inspired by/stolen from smbfs */
1058 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1059 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1061 static int yaffs_writepage(struct page *page)
1065 struct address_space *mapping = page->mapping;
1066 struct inode *inode;
1067 unsigned long end_index;
1076 inode = mapping->host;
1079 i_size = i_size_read(inode);
1081 end_index = i_size >> PAGE_CACHE_SHIFT;
1083 if(page->index < end_index)
1084 nBytes = PAGE_CACHE_SIZE;
1086 nBytes = i_size & (PAGE_CACHE_SIZE -1);
1088 if (page->index > end_index || !nBytes) {
1090 (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
1091 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1092 (unsigned)inode->i_size));
1094 (TSTR(" -> don't care!!\n")));
1096 zero_user_segment(page,0,PAGE_CACHE_SIZE);
1097 set_page_writeback(page);
1099 end_page_writeback(page);
1104 if(nBytes != PAGE_CACHE_SIZE)
1105 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
1109 buffer = kmap(page);
1111 obj = yaffs_InodeToObject(inode);
1113 yaffs_GrossLock(dev);
1116 (TSTR("yaffs_writepage at %08x, size %08x\n"),
1117 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
1119 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
1120 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1122 nWritten = yaffs_WriteDataToFile(obj, buffer,
1123 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
1125 yaffs_MarkSuperBlockDirty(dev);
1128 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
1129 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1131 yaffs_GrossUnlock(dev);
1134 set_page_writeback(page);
1136 end_page_writeback(page);
1139 return (nWritten == nBytes) ? 0 : -ENOSPC;
1143 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1144 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1145 loff_t pos, unsigned len, unsigned flags,
1146 struct page **pagep, void **fsdata)
1148 struct page *pg = NULL;
1149 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1155 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1156 pg = grab_cache_page_write_begin(mapping, index, flags);
1158 pg = __grab_cache_page(mapping, index);
1167 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
1168 (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
1171 space_held = yaffs_hold_space(filp);
1178 /* Update page if required */
1180 if (!Page_Uptodate(pg))
1181 ret = yaffs_readpage_nolock(filp, pg);
1186 /* Happy path return */
1187 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
1193 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
1195 yaffs_release_space(filp);
1198 page_cache_release(pg);
1205 static int yaffs_prepare_write(struct file *f, struct page *pg,
1206 unsigned offset, unsigned to)
1208 T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
1210 if (!Page_Uptodate(pg))
1211 return yaffs_readpage_nolock(f, pg);
1216 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1217 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1218 loff_t pos, unsigned len, unsigned copied,
1219 struct page *pg, void *fsdadata)
1223 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1226 addr = kva + offset_into_page;
1229 ("yaffs_write_end addr %p pos %x nBytes %d\n",
1230 addr,(unsigned)pos, copied));
1232 ret = yaffs_file_write(filp, addr, copied, &pos);
1234 if (ret != copied) {
1236 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
1245 yaffs_release_space(filp);
1247 page_cache_release(pg);
1252 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1257 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1258 int nBytes = to - offset;
1261 unsigned spos = pos;
1265 addr = kva + offset;
1267 saddr = (unsigned) addr;
1270 (TSTR("yaffs_commit_write addr %x pos %x nBytes %d\n"),
1271 saddr, spos, nBytes));
1273 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1275 if (nWritten != nBytes) {
1277 (TSTR("yaffs_commit_write not same size nWritten %d nBytes %d\n"),
1287 (TSTR("yaffs_commit_write returning %d\n"),
1288 nWritten == nBytes ? 0 : nWritten));
1290 return nWritten == nBytes ? 0 : nWritten;
1295 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1300 /* Check mode against the variant type and attempt to repair if broken. */
1301 __u32 mode = obj->yst_mode;
1302 switch (obj->variantType) {
1303 case YAFFS_OBJECT_TYPE_FILE:
1304 if (!S_ISREG(mode)) {
1305 obj->yst_mode &= ~S_IFMT;
1306 obj->yst_mode |= S_IFREG;
1310 case YAFFS_OBJECT_TYPE_SYMLINK:
1311 if (!S_ISLNK(mode)) {
1312 obj->yst_mode &= ~S_IFMT;
1313 obj->yst_mode |= S_IFLNK;
1317 case YAFFS_OBJECT_TYPE_DIRECTORY:
1318 if (!S_ISDIR(mode)) {
1319 obj->yst_mode &= ~S_IFMT;
1320 obj->yst_mode |= S_IFDIR;
1324 case YAFFS_OBJECT_TYPE_UNKNOWN:
1325 case YAFFS_OBJECT_TYPE_HARDLINK:
1326 case YAFFS_OBJECT_TYPE_SPECIAL:
1332 inode->i_flags |= S_NOATIME;
1334 inode->i_ino = obj->objectId;
1335 inode->i_mode = obj->yst_mode;
1336 inode->i_uid = obj->yst_uid;
1337 inode->i_gid = obj->yst_gid;
1338 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1339 inode->i_blksize = inode->i_sb->s_blocksize;
1341 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1343 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1344 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1345 inode->i_atime.tv_nsec = 0;
1346 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1347 inode->i_mtime.tv_nsec = 0;
1348 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1349 inode->i_ctime.tv_nsec = 0;
1351 inode->i_rdev = obj->yst_rdev;
1352 inode->i_atime = obj->yst_atime;
1353 inode->i_mtime = obj->yst_mtime;
1354 inode->i_ctime = obj->yst_ctime;
1356 inode->i_size = yaffs_GetObjectFileLength(obj);
1357 inode->i_blocks = (inode->i_size + 511) >> 9;
1359 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1362 (TSTR("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n"),
1363 inode->i_mode, inode->i_uid, inode->i_gid,
1364 (int)inode->i_size, atomic_read(&inode->i_count)));
1366 switch (obj->yst_mode & S_IFMT) {
1367 default: /* fifo, device or socket */
1368 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1369 init_special_inode(inode, obj->yst_mode,
1370 old_decode_dev(obj->yst_rdev));
1372 init_special_inode(inode, obj->yst_mode,
1373 (dev_t) (obj->yst_rdev));
1376 case S_IFREG: /* file */
1377 inode->i_op = &yaffs_file_inode_operations;
1378 inode->i_fop = &yaffs_file_operations;
1379 inode->i_mapping->a_ops =
1380 &yaffs_file_address_operations;
1382 case S_IFDIR: /* directory */
1383 inode->i_op = &yaffs_dir_inode_operations;
1384 inode->i_fop = &yaffs_dir_operations;
1386 case S_IFLNK: /* symlink */
1387 inode->i_op = &yaffs_symlink_inode_operations;
1391 yaffs_InodeToObjectLV(inode) = obj;
1393 obj->myInode = inode;
1397 (TSTR("yaffs_FileInode invalid parameters\n")));
1402 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1405 struct inode *inode;
1409 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1416 (TSTR("yaffs_get_inode for NULL object!!\n")));
1422 (TSTR("yaffs_get_inode for object %d\n"), obj->objectId));
1424 inode = Y_IGET(sb, obj->objectId);
1428 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1429 /* iget also increments the inode's i_count */
1430 /* NB You can't be holding grossLock or deadlock will happen! */
1435 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1440 struct inode *inode;
1443 obj = yaffs_DentryToObject(f->f_dentry);
1447 yaffs_GrossLock(dev);
1449 inode = f->f_dentry->d_inode;
1451 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1452 ipos = inode->i_size;
1458 (TSTR("yaffs_file_write: hey obj is null!\n")));
1461 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1462 "to object %d at %d(%x)\n"),
1463 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1465 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1467 yaffs_MarkSuperBlockDirty(dev);
1470 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1471 (unsigned )n,(unsigned)n));
1476 if (ipos > inode->i_size) {
1477 inode->i_size = ipos;
1478 inode->i_blocks = (ipos + 511) >> 9;
1481 (TSTR("yaffs_file_write size updated to %d bytes, "
1483 ipos, (int)(inode->i_blocks)));
1487 yaffs_GrossUnlock(dev);
1488 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1491 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1492 /* For now we just assume few parallel writes and check against a small number. */
1493 /* Todo: need to do this with a counter to handle parallel reads better */
1495 static ssize_t yaffs_hold_space(struct file *f)
1503 obj = yaffs_DentryToObject(f->f_dentry);
1507 yaffs_GrossLock(dev);
1509 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1511 yaffs_GrossUnlock(dev);
1513 return (nFreeChunks > 20) ? 1 : 0;
1516 static void yaffs_release_space(struct file *f)
1522 obj = yaffs_DentryToObject(f->f_dentry);
1526 yaffs_GrossLock(dev);
1529 yaffs_GrossUnlock(dev);
1533 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1541 offset += i_size_read(file->f_path.dentry->d_inode);
1544 offset += file->f_pos;
1549 if (offset != file->f_pos)
1550 file->f_pos = offset;
1559 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1563 struct yaffs_SearchContext *sc;
1564 struct inode *inode = f->f_dentry->d_inode;
1565 unsigned long offset, curoffs;
1569 char name[YAFFS_MAX_NAME_LENGTH + 1];
1571 obj = yaffs_DentryToObject(f->f_dentry);
1574 yaffs_GrossLock(dev);
1576 yaffs_DeviceToLC(dev)->readdirProcess = current;
1580 sc = yaffs_NewSearch(obj);
1586 T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1590 (TSTR("yaffs_readdir: entry . ino %d \n"),
1591 (int)inode->i_ino));
1592 yaffs_GrossUnlock(dev);
1593 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0){
1594 yaffs_GrossLock(dev);
1597 yaffs_GrossLock(dev);
1603 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1604 (int)f->f_dentry->d_parent->d_inode->i_ino));
1605 yaffs_GrossUnlock(dev);
1606 if (filldir(dirent, "..", 2, offset,
1607 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
1608 yaffs_GrossLock(dev);
1611 yaffs_GrossLock(dev);
1618 /* If the directory has changed since the open or last call to
1619 readdir, rewind to after the 2 canned entries. */
1620 if (f->f_version != inode->i_version) {
1623 f->f_version = inode->i_version;
1626 while(sc->nextReturn){
1629 if (curoffs >= offset) {
1630 int this_inode = yaffs_GetObjectInode(l);
1631 int this_type = yaffs_GetObjectType(l);
1633 yaffs_GetObjectName(l, name,
1634 YAFFS_MAX_NAME_LENGTH + 1);
1636 (TSTR("yaffs_readdir: %s inode %d\n"),
1637 name, yaffs_GetObjectInode(l)));
1639 yaffs_GrossUnlock(dev);
1647 yaffs_GrossLock(dev);
1651 yaffs_GrossLock(dev);
1656 yaffs_SearchAdvance(sc);
1660 yaffs_EndSearch(sc);
1661 yaffs_DeviceToLC(dev)->readdirProcess = NULL;
1662 yaffs_GrossUnlock(dev);
1670 * File creation. Allocate an inode, and we're done..
1673 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1676 #define YCRED(x) (x->cred)
1679 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1680 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1683 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1687 struct inode *inode;
1689 yaffs_Object *obj = NULL;
1692 yaffs_Object *parent = yaffs_InodeToObject(dir);
1694 int error = -ENOSPC;
1695 uid_t uid = YCRED(current)->fsuid;
1696 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1698 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1703 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1704 parent->objectId, parent->variantType));
1707 (TSTR("yaffs_mknod: could not get parent object\n")));
1711 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1712 "mode %x dev %x\n"),
1713 dentry->d_name.name, mode, rdev));
1715 dev = parent->myDev;
1717 yaffs_GrossLock(dev);
1719 switch (mode & S_IFMT) {
1721 /* Special (socket, fifo, device...) */
1722 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1723 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1724 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1725 gid, old_encode_dev(rdev));
1727 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1731 case S_IFREG: /* file */
1732 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1733 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1736 case S_IFDIR: /* directory */
1738 (TSTR("yaffs_mknod: making directory\n")));
1739 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1742 case S_IFLNK: /* symlink */
1743 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1744 obj = NULL; /* Do we ever get here? */
1748 /* Can not call yaffs_get_inode() with gross lock held */
1749 yaffs_GrossUnlock(dev);
1752 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1753 d_instantiate(dentry, inode);
1754 update_dir_time(dir);
1756 (TSTR("yaffs_mknod created object %d count = %d\n"),
1757 obj->objectId, atomic_read(&inode->i_count)));
1759 yaffs_FillInodeFromObject(dir,parent);
1762 (TSTR("yaffs_mknod failed making object\n")));
1769 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1772 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1773 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1777 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1778 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1779 struct nameidata *n)
1781 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1784 T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1785 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1788 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1796 (TSTR("yaffs_unlink %d:%s\n"),
1798 dentry->d_name.name));
1799 obj = yaffs_InodeToObject(dir);
1802 yaffs_GrossLock(dev);
1804 retVal = yaffs_Unlink(obj, dentry->d_name.name);
1806 if (retVal == YAFFS_OK) {
1807 dentry->d_inode->i_nlink--;
1809 yaffs_GrossUnlock(dev);
1810 mark_inode_dirty(dentry->d_inode);
1811 update_dir_time(dir);
1814 yaffs_GrossUnlock(dev);
1821 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1822 struct dentry *dentry)
1824 struct inode *inode = old_dentry->d_inode;
1825 yaffs_Object *obj = NULL;
1826 yaffs_Object *link = NULL;
1829 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1831 obj = yaffs_InodeToObject(inode);
1834 yaffs_GrossLock(dev);
1836 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1837 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1841 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1842 d_instantiate(dentry, old_dentry->d_inode);
1843 atomic_inc(&old_dentry->d_inode->i_count);
1845 (TSTR("yaffs_link link count %d i_count %d\n"),
1846 old_dentry->d_inode->i_nlink,
1847 atomic_read(&old_dentry->d_inode->i_count)));
1850 yaffs_GrossUnlock(dev);
1853 update_dir_time(dir);
1860 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1861 const char *symname)
1865 uid_t uid = YCRED(current)->fsuid;
1866 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1868 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1870 dev = yaffs_InodeToObject(dir)->myDev;
1871 yaffs_GrossLock(dev);
1872 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1873 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1874 yaffs_GrossUnlock(dev);
1877 struct inode *inode;
1879 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1880 d_instantiate(dentry, inode);
1881 update_dir_time(dir);
1882 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1885 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1891 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1892 static int yaffs_sync_object(struct file *file, int datasync)
1894 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1901 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1902 struct dentry *dentry = file->f_path.dentry;
1905 obj = yaffs_DentryToObject(dentry);
1909 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1910 (TSTR("yaffs_sync_object\n")));
1911 yaffs_GrossLock(dev);
1912 yaffs_FlushFile(obj, 1, datasync);
1913 yaffs_GrossUnlock(dev);
1918 * The VFS layer already does all the dentry stuff for rename.
1920 * NB: POSIX says you can rename an object over an old object of the same name
1922 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1923 struct inode *new_dir, struct dentry *new_dentry)
1926 int retVal = YAFFS_FAIL;
1927 yaffs_Object *target;
1929 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1930 dev = yaffs_InodeToObject(old_dir)->myDev;
1932 yaffs_GrossLock(dev);
1934 /* Check if the target is an existing directory that is not empty. */
1935 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1936 new_dentry->d_name.name);
1940 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1941 !ylist_empty(&target->variant.directoryVariant.children)) {
1943 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1945 retVal = YAFFS_FAIL;
1947 /* Now does unlinking internally using shadowing mechanism */
1948 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_RenameObject\n")));
1950 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1951 old_dentry->d_name.name,
1952 yaffs_InodeToObject(new_dir),
1953 new_dentry->d_name.name);
1955 yaffs_GrossUnlock(dev);
1957 if (retVal == YAFFS_OK) {
1959 new_dentry->d_inode->i_nlink--;
1960 mark_inode_dirty(new_dentry->d_inode);
1963 update_dir_time(old_dir);
1964 if(old_dir != new_dir)
1965 update_dir_time(new_dir);
1972 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1974 struct inode *inode = dentry->d_inode;
1979 (TSTR("yaffs_setattr of object %d\n"),
1980 yaffs_InodeToObject(inode)->objectId));
1982 /* Fail if a requested resize >= 2GB */
1983 if (attr->ia_valid & ATTR_SIZE &&
1984 (attr->ia_size >> 31))
1988 error = inode_change_ok(inode, attr);
1992 error = yaffs_vfs_setattr(inode, attr);
1993 T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
1994 if (attr->ia_valid & ATTR_SIZE){
1995 yaffs_vfs_setsize(inode,attr->ia_size);
1996 inode->i_blocks = (inode->i_size + 511) >> 9;
1999 dev = yaffs_InodeToObject(inode)->myDev;
2000 if (attr->ia_valid & ATTR_SIZE){
2001 T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
2002 (int)(attr->ia_size),(int)(attr->ia_size)));
2004 yaffs_GrossLock(dev);
2005 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
2006 if(result == YAFFS_OK) {
2011 yaffs_GrossUnlock(dev);
2016 (TSTR("yaffs_setattr done returning %d\n"),error));
2021 #ifdef CONFIG_YAFFS_XATTR
2022 int yaffs_setxattr(struct dentry *dentry, const char *name,
2023 const void *value, size_t size, int flags)
2025 struct inode *inode = dentry->d_inode;
2028 yaffs_Object *obj = yaffs_InodeToObject(inode);
2031 (TSTR("yaffs_setxattr of object %d\n"),
2038 yaffs_GrossLock(dev);
2039 result = yaffs_SetXAttribute(obj, name, value, size, flags);
2040 if(result == YAFFS_OK)
2044 yaffs_GrossUnlock(dev);
2048 (TSTR("yaffs_setxattr done returning %d\n"),error));
2054 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
2057 struct inode *inode = dentry->d_inode;
2060 yaffs_Object *obj = yaffs_InodeToObject(inode);
2063 (TSTR("yaffs_getxattr \"%s\" from object %d\n"),
2064 name, obj->objectId));
2068 yaffs_GrossLock(dev);
2069 error = yaffs_GetXAttribute(obj, name, buff, size);
2070 yaffs_GrossUnlock(dev);
2074 (TSTR("yaffs_getxattr done returning %d\n"),error));
2079 int yaffs_removexattr(struct dentry *dentry, const char *name)
2081 struct inode *inode = dentry->d_inode;
2084 yaffs_Object *obj = yaffs_InodeToObject(inode);
2087 (TSTR("yaffs_removexattr of object %d\n"),
2094 yaffs_GrossLock(dev);
2095 result = yaffs_RemoveXAttribute(obj, name);
2096 if(result == YAFFS_OK)
2100 yaffs_GrossUnlock(dev);
2104 (TSTR("yaffs_removexattr done returning %d\n"),error));
2109 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
2111 struct inode *inode = dentry->d_inode;
2114 yaffs_Object *obj = yaffs_InodeToObject(inode);
2117 (TSTR("yaffs_listxattr of object %d\n"),
2123 yaffs_GrossLock(dev);
2124 error = yaffs_ListXAttributes(obj, buff, size);
2125 yaffs_GrossUnlock(dev);
2129 (TSTR("yaffs_listxattr done returning %d\n"),error));
2137 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2138 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2140 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2141 struct super_block *sb = dentry->d_sb;
2142 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2143 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2145 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2147 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2149 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2152 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
2154 yaffs_GrossLock(dev);
2156 buf->f_type = YAFFS_MAGIC;
2157 buf->f_bsize = sb->s_blocksize;
2158 buf->f_namelen = 255;
2160 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
2161 /* Do this if chunk size is not a power of 2 */
2163 uint64_t bytesInDev;
2166 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
2167 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
2169 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
2170 buf->f_blocks = bytesInDev;
2172 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
2173 ((uint64_t)(dev->nDataBytesPerChunk));
2175 do_div(bytesFree, sb->s_blocksize);
2177 buf->f_bfree = bytesFree;
2179 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2182 (dev->param.endBlock - dev->param.startBlock + 1) *
2183 dev->param.nChunksPerBlock /
2184 (sb->s_blocksize / dev->nDataBytesPerChunk);
2186 yaffs_GetNumberOfFreeChunks(dev) /
2187 (sb->s_blocksize / dev->nDataBytesPerChunk);
2190 (dev->param.endBlock - dev->param.startBlock + 1) *
2191 dev->param.nChunksPerBlock *
2192 (dev->nDataBytesPerChunk / sb->s_blocksize);
2195 yaffs_GetNumberOfFreeChunks(dev) *
2196 (dev->nDataBytesPerChunk / sb->s_blocksize);
2201 buf->f_bavail = buf->f_bfree;
2203 yaffs_GrossUnlock(dev);
2209 static void yaffs_FlushInodes(struct super_block *sb)
2214 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
2215 obj = yaffs_InodeToObject(iptr);
2217 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
2219 yaffs_FlushFile(obj,1,0);
2225 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
2227 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2231 yaffs_FlushInodes(sb);
2232 yaffs_UpdateDirtyDirectories(dev);
2233 yaffs_FlushEntireDeviceCache(dev);
2235 yaffs_CheckpointSave(dev);
2239 static unsigned yaffs_bg_gc_urgency(yaffs_Device *dev)
2241 unsigned erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2242 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2243 unsigned scatteredFree = 0; /* Free chunks not in an erased block */
2245 if(erasedChunks < dev->nFreeChunks)
2246 scatteredFree = (dev->nFreeChunks - erasedChunks);
2248 if(!context->bgRunning)
2250 else if(scatteredFree < (dev->param.nChunksPerBlock * 2))
2252 else if(erasedChunks > dev->nFreeChunks/2)
2254 else if(erasedChunks > dev->nFreeChunks/4)
2260 static int yaffs_do_sync_fs(struct super_block *sb,
2261 int request_checkpoint)
2264 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2265 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2266 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2269 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2270 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
2272 sb->s_dirt ? "dirty" : "clean",
2273 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2274 oneshot_checkpoint ? " one-shot" : "" ));
2276 yaffs_GrossLock(dev);
2277 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2278 oneshot_checkpoint) &&
2279 !dev->isCheckpointed;
2281 if (sb->s_dirt || do_checkpoint) {
2282 yaffs_FlushSuperBlock(sb, !dev->isCheckpointed && do_checkpoint);
2284 if(oneshot_checkpoint)
2285 yaffs_auto_checkpoint &= ~4;
2287 yaffs_GrossUnlock(dev);
2293 * yaffs background thread functions .
2294 * yaffs_BackgroundThread() the thread function
2295 * yaffs_BackgroundStart() launches the background thread.
2296 * yaffs_BackgroundStop() cleans up the background thread.
2299 * The thread should only run after the yaffs is initialised
2300 * The thread should be stopped before yaffs is unmounted.
2301 * The thread should not do any writing while the fs is in read only.
2304 #ifdef YAFFS_COMPILE_BACKGROUND
2306 void yaffs_background_waker(unsigned long data)
2308 wake_up_process((struct task_struct *)data);
2311 static int yaffs_BackgroundThread(void *data)
2313 yaffs_Device *dev = (yaffs_Device *)data;
2314 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2315 unsigned long now = jiffies;
2316 unsigned long next_dir_update = now;
2317 unsigned long next_gc = now;
2318 unsigned long expires;
2319 unsigned int urgency;
2322 struct timer_list timer;
2324 T(YAFFS_TRACE_BACKGROUND,
2325 (TSTR("yaffs_background starting for dev %p\n"),
2328 #ifdef YAFFS_COMPILE_FREEZER
2331 while(context->bgRunning){
2332 T(YAFFS_TRACE_BACKGROUND,
2333 (TSTR("yaffs_background\n")));
2335 if(kthread_should_stop())
2338 #ifdef YAFFS_COMPILE_FREEZER
2342 yaffs_GrossLock(dev);
2346 if(time_after(now, next_dir_update) && yaffs_bg_enable){
2347 yaffs_UpdateDirtyDirectories(dev);
2348 next_dir_update = now + HZ;
2351 if(time_after(now,next_gc) && yaffs_bg_enable){
2352 if(!dev->isCheckpointed){
2353 urgency = yaffs_bg_gc_urgency(dev);
2354 gcResult = yaffs_BackgroundGarbageCollect(dev, urgency);
2356 next_gc = now + HZ/20+1;
2357 else if(urgency > 0)
2358 next_gc = now + HZ/10+1;
2360 next_gc = now + HZ * 2;
2362 * gc not running so set to next_dir_update
2363 * to cut down on wake ups
2365 next_gc = next_dir_update;
2367 yaffs_GrossUnlock(dev);
2369 expires = next_dir_update;
2370 if (time_before(next_gc,expires))
2372 if(time_before(expires,now))
2375 Y_INIT_TIMER(&timer);
2376 timer.expires = expires+1;
2377 timer.data = (unsigned long) current;
2378 timer.function = yaffs_background_waker;
2380 set_current_state(TASK_INTERRUPTIBLE);
2383 del_timer_sync(&timer);
2392 static int yaffs_BackgroundStart(yaffs_Device *dev)
2395 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2400 context->bgRunning = 1;
2402 context->bgThread = kthread_run(yaffs_BackgroundThread,
2403 (void *)dev,"yaffs-bg-%d",context->mount_id);
2405 if(IS_ERR(context->bgThread)){
2406 retval = PTR_ERR(context->bgThread);
2407 context->bgThread = NULL;
2408 context->bgRunning = 0;
2413 static void yaffs_BackgroundStop(yaffs_Device *dev)
2415 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToLC(dev);
2417 ctxt->bgRunning = 0;
2419 if( ctxt->bgThread){
2420 kthread_stop(ctxt->bgThread);
2421 ctxt->bgThread = NULL;
2425 static int yaffs_BackgroundThread(void *data)
2430 static int yaffs_BackgroundStart(yaffs_Device *dev)
2435 static void yaffs_BackgroundStop(yaffs_Device *dev)
2441 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2442 static void yaffs_write_super(struct super_block *sb)
2444 static int yaffs_write_super(struct super_block *sb)
2447 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2449 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2450 (TSTR("yaffs_write_super%s\n"),
2451 request_checkpoint ? " checkpt" : ""));
2453 yaffs_do_sync_fs(sb, request_checkpoint);
2455 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2461 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2462 static int yaffs_sync_fs(struct super_block *sb, int wait)
2464 static int yaffs_sync_fs(struct super_block *sb)
2467 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2469 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2470 (TSTR("yaffs_sync_fs%s\n"),
2471 request_checkpoint ? " checkpt" : ""));
2473 yaffs_do_sync_fs(sb, request_checkpoint);
2478 #ifdef YAFFS_USE_OWN_IGET
2480 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2482 struct inode *inode;
2484 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2487 (TSTR("yaffs_iget for %lu\n"), ino));
2489 inode = iget_locked(sb, ino);
2491 return ERR_PTR(-ENOMEM);
2492 if (!(inode->i_state & I_NEW))
2495 /* NB This is called as a side effect of other functions, but
2496 * we had to release the lock to prevent deadlocks, so
2497 * need to lock again.
2500 yaffs_GrossLock(dev);
2502 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2504 yaffs_FillInodeFromObject(inode, obj);
2506 yaffs_GrossUnlock(dev);
2508 unlock_new_inode(inode);
2514 static void yaffs_read_inode(struct inode *inode)
2516 /* NB This is called as a side effect of other functions, but
2517 * we had to release the lock to prevent deadlocks, so
2518 * need to lock again.
2522 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2525 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2527 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2528 yaffs_GrossLock(dev);
2530 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2532 yaffs_FillInodeFromObject(inode, obj);
2534 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2535 yaffs_GrossUnlock(dev);
2540 static YLIST_HEAD(yaffs_context_list);
2541 struct semaphore yaffs_context_lock;
2543 static void yaffs_put_super(struct super_block *sb)
2545 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2547 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2549 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2550 (TSTR("Shutting down yaffs background thread\n")));
2551 yaffs_BackgroundStop(dev);
2552 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2553 (TSTR("yaffs background thread shut down\n")));
2555 yaffs_GrossLock(dev);
2557 yaffs_FlushSuperBlock(sb,1);
2559 if (yaffs_DeviceToLC(dev)->putSuperFunc)
2560 yaffs_DeviceToLC(dev)->putSuperFunc(sb);
2563 yaffs_Deinitialise(dev);
2565 yaffs_GrossUnlock(dev);
2567 down(&yaffs_context_lock);
2568 ylist_del_init(&(yaffs_DeviceToLC(dev)->contextList));
2569 up(&yaffs_context_lock);
2571 if (yaffs_DeviceToLC(dev)->spareBuffer) {
2572 YFREE(yaffs_DeviceToLC(dev)->spareBuffer);
2573 yaffs_DeviceToLC(dev)->spareBuffer = NULL;
2580 static void yaffs_MTDPutSuper(struct super_block *sb)
2582 struct mtd_info *mtd = yaffs_DeviceToMtd(yaffs_SuperToDevice(sb));
2587 put_mtd_device(mtd);
2591 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2593 struct super_block *sb = yaffs_DeviceToLC(dev)->superBlock;
2595 T(YAFFS_TRACE_OS, (TSTR("yaffs_MarkSuperBlockDirty() sb = %p\n"), sb));
2602 int skip_checkpoint_read;
2603 int skip_checkpoint_write;
2606 int tags_ecc_overridden;
2607 int lazy_loading_enabled;
2608 int lazy_loading_overridden;
2609 int empty_lost_and_found;
2610 int empty_lost_and_found_overridden;
2613 #define MAX_OPT_LEN 30
2614 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2616 char cur_opt[MAX_OPT_LEN + 1];
2620 /* Parse through the options which is a comma seperated list */
2622 while (options_str && *options_str && !error) {
2623 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2626 while(*options_str == ',')
2629 while (*options_str && *options_str != ',') {
2630 if (p < MAX_OPT_LEN) {
2631 cur_opt[p] = *options_str;
2637 if (!strcmp(cur_opt, "inband-tags"))
2638 options->inband_tags = 1;
2639 else if (!strcmp(cur_opt, "tags-ecc-off")){
2640 options->tags_ecc_on = 0;
2641 options->tags_ecc_overridden=1;
2642 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2643 options->tags_ecc_on = 1;
2644 options->tags_ecc_overridden = 1;
2645 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2646 options->lazy_loading_enabled = 0;
2647 options->lazy_loading_overridden=1;
2648 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2649 options->lazy_loading_enabled = 1;
2650 options->lazy_loading_overridden = 1;
2651 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2652 options->empty_lost_and_found = 0;
2653 options->empty_lost_and_found_overridden=1;
2654 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2655 options->empty_lost_and_found = 1;
2656 options->empty_lost_and_found_overridden=1;
2657 } else if (!strcmp(cur_opt, "no-cache"))
2658 options->no_cache = 1;
2659 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2660 options->skip_checkpoint_read = 1;
2661 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2662 options->skip_checkpoint_write = 1;
2663 else if (!strcmp(cur_opt, "no-checkpoint")) {
2664 options->skip_checkpoint_read = 1;
2665 options->skip_checkpoint_write = 1;
2667 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2676 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2677 struct super_block *sb,
2678 void *data, int silent)
2681 struct inode *inode = NULL;
2682 struct dentry *root;
2683 yaffs_Device *dev = 0;
2684 char devname_buf[BDEVNAME_SIZE + 1];
2685 struct mtd_info *mtd;
2687 char *data_str = (char *)data;
2688 struct yaffs_LinuxContext *context = NULL;
2689 yaffs_DeviceParam *param;
2693 yaffs_options options;
2697 struct yaffs_LinuxContext *context_iterator;
2698 struct ylist_head *l;
2700 sb->s_magic = YAFFS_MAGIC;
2701 sb->s_op = &yaffs_super_ops;
2702 sb->s_flags |= MS_NOATIME;
2704 readOnly =((sb->s_flags & MS_RDONLY) != 0);
2707 #ifdef YAFFS_COMPILE_EXPORTFS
2708 sb->s_export_op = &yaffs_export_ops;
2712 printk(KERN_INFO "yaffs: sb is NULL\n");
2713 else if (!sb->s_dev)
2714 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2715 else if (!yaffs_devname(sb, devname_buf))
2716 printk(KERN_INFO "yaffs: devname is NULL\n");
2718 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2720 yaffs_devname(sb, devname_buf),
2721 readOnly ? "ro" : "rw");
2726 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2728 memset(&options, 0, sizeof(options));
2730 if (yaffs_parse_options(&options, data_str)) {
2731 /* Option parsing failed */
2736 sb->s_blocksize = PAGE_CACHE_SIZE;
2737 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2740 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffsVersion));
2742 (TSTR("yaffs_read_super: block size %d\n"),
2743 (int)(sb->s_blocksize)));
2745 T(YAFFS_TRACE_ALWAYS,
2746 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2747 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2748 yaffs_devname(sb, devname_buf)));
2750 /* Check it's an mtd device..... */
2751 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2752 return NULL; /* This isn't an mtd device */
2754 /* Get the device */
2755 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2757 T(YAFFS_TRACE_ALWAYS,
2758 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2762 /* Check it's NAND */
2763 if (mtd->type != MTD_NANDFLASH) {
2764 T(YAFFS_TRACE_ALWAYS,
2765 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2770 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2771 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2772 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2773 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2774 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2775 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2776 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2777 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2778 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2779 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2780 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2781 T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2783 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2786 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2788 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2789 T(YAFFS_TRACE_ALWAYS,
2790 (TSTR("yaffs: auto selecting yaffs2\n")));
2794 /* Added NCB 26/5/2006 for completeness */
2795 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2796 T(YAFFS_TRACE_ALWAYS,
2797 (TSTR("yaffs: auto selecting yaffs1\n")));
2803 if (yaffsVersion == 2) {
2804 /* Check for version 2 style functions */
2806 !mtd->block_isbad ||
2807 !mtd->block_markbad ||
2810 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2811 !mtd->read_oob || !mtd->write_oob) {
2814 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2816 T(YAFFS_TRACE_ALWAYS,
2817 (TSTR("yaffs: MTD device does not support required "
2822 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2823 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2824 !options.inband_tags) {
2825 T(YAFFS_TRACE_ALWAYS,
2826 (TSTR("yaffs: MTD device does not have the "
2827 "right page sizes\n")));
2831 /* Check for V1 style functions */
2835 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2836 !mtd->read_oob || !mtd->write_oob) {
2839 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2841 T(YAFFS_TRACE_ALWAYS,
2842 (TSTR("yaffs: MTD device does not support required "
2847 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2848 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2849 T(YAFFS_TRACE_ALWAYS,
2850 (TSTR("yaffs: MTD device does not support have the "
2851 "right page sizes\n")));
2856 /* OK, so if we got here, we have an MTD that's NAND and looks
2857 * like it has the right capabilities
2858 * Set the yaffs_Device up for mtd
2861 if (!readOnly && !(mtd->flags & MTD_WRITEABLE)){
2863 printk(KERN_INFO "yaffs: mtd is read only, setting superblock read only");
2864 sb->s_flags |= MS_RDONLY;
2867 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2868 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2870 if(!dev || !context ){
2880 /* Deep shit could not allocate device structure */
2881 T(YAFFS_TRACE_ALWAYS,
2882 (TSTR("yaffs_read_super: Failed trying to allocate "
2883 "yaffs_Device. \n")));
2886 memset(dev, 0, sizeof(yaffs_Device));
2887 param = &(dev->param);
2889 memset(context,0,sizeof(struct yaffs_LinuxContext));
2890 dev->osContext = context;
2891 YINIT_LIST_HEAD(&(context->contextList));
2893 context->superBlock = sb;
2895 dev->readOnly = readOnly;
2897 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2898 sb->s_fs_info = dev;
2900 sb->u.generic_sbp = dev;
2903 dev->driverContext = mtd;
2904 param->name = mtd->name;
2906 /* Set up the memory size parameters.... */
2908 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2910 param->startBlock = 0;
2911 param->endBlock = nBlocks - 1;
2912 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2913 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2914 param->nReservedBlocks = 5;
2915 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2916 param->inbandTags = options.inband_tags;
2918 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2919 param->disableLazyLoad = 1;
2921 #ifdef CONFIG_YAFFS_XATTR
2922 param->enableXattr = 1;
2924 if(options.lazy_loading_overridden)
2925 param->disableLazyLoad = !options.lazy_loading_enabled;
2927 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2928 param->noTagsECC = 1;
2931 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2933 param->deferDirectoryUpdate = 1;
2936 if(options.tags_ecc_overridden)
2937 param->noTagsECC = !options.tags_ecc_on;
2939 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2940 param->emptyLostAndFound = 1;
2943 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2944 param->refreshPeriod = 0;
2946 param->refreshPeriod = 500;
2949 if(options.empty_lost_and_found_overridden)
2950 param->emptyLostAndFound = options.empty_lost_and_found;
2952 /* ... and the functions. */
2953 if (yaffsVersion == 2) {
2954 param->writeChunkWithTagsToNAND =
2955 nandmtd2_WriteChunkWithTagsToNAND;
2956 param->readChunkWithTagsFromNAND =
2957 nandmtd2_ReadChunkWithTagsFromNAND;
2958 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2959 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2960 yaffs_DeviceToLC(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2961 param->isYaffs2 = 1;
2962 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2963 param->totalBytesPerChunk = mtd->writesize;
2964 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2966 param->totalBytesPerChunk = mtd->oobblock;
2967 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2969 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2971 param->startBlock = 0;
2972 param->endBlock = nBlocks - 1;
2974 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2975 /* use the MTD interface in yaffs_mtdif1.c */
2976 param->writeChunkWithTagsToNAND =
2977 nandmtd1_WriteChunkWithTagsToNAND;
2978 param->readChunkWithTagsFromNAND =
2979 nandmtd1_ReadChunkWithTagsFromNAND;
2980 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2981 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2983 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2984 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2986 param->isYaffs2 = 0;
2988 /* ... and common functions */
2989 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2990 param->initialiseNAND = nandmtd_InitialiseNAND;
2992 yaffs_DeviceToLC(dev)->putSuperFunc = yaffs_MTDPutSuper;
2994 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2995 param->gcControl = yaffs_gc_control_callback;
2997 yaffs_DeviceToLC(dev)->superBlock= sb;
3000 #ifndef CONFIG_YAFFS_DOES_ECC
3001 param->useNANDECC = 1;
3004 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3005 param->wideTnodesDisabled = 1;
3008 param->skipCheckpointRead = options.skip_checkpoint_read;
3009 param->skipCheckpointWrite = options.skip_checkpoint_write;
3011 down(&yaffs_context_lock);
3012 /* Get a mount id */
3014 for(mount_id=0; ! found; mount_id++){
3016 ylist_for_each(l,&yaffs_context_list){
3017 context_iterator = ylist_entry(l,struct yaffs_LinuxContext,contextList);
3018 if(context_iterator->mount_id == mount_id)
3022 context->mount_id = mount_id;
3024 ylist_add_tail(&(yaffs_DeviceToLC(dev)->contextList), &yaffs_context_list);
3025 up(&yaffs_context_lock);
3027 /* Directory search handling...*/
3028 YINIT_LIST_HEAD(&(yaffs_DeviceToLC(dev)->searchContexts));
3029 param->removeObjectCallback = yaffs_RemoveObjectCallback;
3031 init_MUTEX(&(yaffs_DeviceToLC(dev)->grossLock));
3033 yaffs_GrossLock(dev);
3035 err = yaffs_GutsInitialise(dev);
3038 (TSTR("yaffs_read_super: guts initialised %s\n"),
3039 (err == YAFFS_OK) ? "OK" : "FAILED"));
3042 yaffs_BackgroundStart(dev);
3044 if(!context->bgThread)
3045 param->deferDirectoryUpdate = 0;
3048 /* Release lock before yaffs_get_inode() */
3049 yaffs_GrossUnlock(dev);
3051 /* Create root inode */
3052 if (err == YAFFS_OK)
3053 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3059 inode->i_op = &yaffs_dir_inode_operations;
3060 inode->i_fop = &yaffs_dir_operations;
3062 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
3064 root = d_alloc_root(inode);
3066 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
3073 sb->s_dirt = !dev->isCheckpointed;
3074 T(YAFFS_TRACE_ALWAYS,
3075 (TSTR("yaffs_read_super: isCheckpointed %d\n"),
3076 dev->isCheckpointed));
3078 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
3083 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3084 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3087 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3090 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3091 static int yaffs_read_super(struct file_system_type *fs,
3092 int flags, const char *dev_name,
3093 void *data, struct vfsmount *mnt)
3096 return get_sb_bdev(fs, flags, dev_name, data,
3097 yaffs_internal_read_super_mtd, mnt);
3100 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3101 int flags, const char *dev_name,
3105 return get_sb_bdev(fs, flags, dev_name, data,
3106 yaffs_internal_read_super_mtd);
3110 static struct file_system_type yaffs_fs_type = {
3111 .owner = THIS_MODULE,
3113 .get_sb = yaffs_read_super,
3114 .kill_sb = kill_block_super,
3115 .fs_flags = FS_REQUIRES_DEV,
3118 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3121 return yaffs_internal_read_super(1, sb, data, silent);
3124 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3129 #ifdef CONFIG_YAFFS_YAFFS2
3131 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3132 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3135 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3138 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3139 static int yaffs2_read_super(struct file_system_type *fs,
3140 int flags, const char *dev_name, void *data,
3141 struct vfsmount *mnt)
3143 return get_sb_bdev(fs, flags, dev_name, data,
3144 yaffs2_internal_read_super_mtd, mnt);
3147 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3148 int flags, const char *dev_name,
3152 return get_sb_bdev(fs, flags, dev_name, data,
3153 yaffs2_internal_read_super_mtd);
3157 static struct file_system_type yaffs2_fs_type = {
3158 .owner = THIS_MODULE,
3160 .get_sb = yaffs2_read_super,
3161 .kill_sb = kill_block_super,
3162 .fs_flags = FS_REQUIRES_DEV,
3165 static struct super_block *yaffs2_read_super(struct super_block *sb,
3166 void *data, int silent)
3168 return yaffs_internal_read_super(2, sb, data, silent);
3171 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3175 #endif /* CONFIG_YAFFS_YAFFS2 */
3177 static struct proc_dir_entry *my_proc_entry;
3178 static struct proc_dir_entry *debug_proc_entry;
3180 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
3182 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
3183 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
3184 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
3185 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
3186 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
3187 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
3188 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
3189 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
3190 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
3191 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
3192 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
3193 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
3195 buf += sprintf(buf, "\n");
3201 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
3203 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3204 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3205 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3206 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3207 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3208 buf += sprintf(buf, "\n");
3209 buf += sprintf(buf, "nTnodes............ %d\n", dev->nTnodes);
3210 buf += sprintf(buf, "nObjects........... %d\n", dev->nObjects);
3211 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3212 buf += sprintf(buf, "\n");
3213 buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
3214 buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
3215 buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
3216 buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
3217 buf += sprintf(buf, "allGCs............. %u\n", dev->allGCs);
3218 buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGCs);
3219 buf += sprintf(buf, "oldestDirtyGCs..... %u\n", dev->oldestDirtyGCs);
3220 buf += sprintf(buf, "nGCBlocks.......... %u\n", dev->nGCBlocks);
3221 buf += sprintf(buf, "backgroundGCs...... %u\n", dev->backgroundGCs);
3222 buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
3223 buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
3224 buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
3225 buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
3226 buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
3227 buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
3228 buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
3229 buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
3230 buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
3231 buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
3233 sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
3238 static int yaffs_proc_read(char *page,
3240 off_t offset, int count, int *eof, void *data)
3242 struct ylist_head *item;
3247 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3248 * We use 'offset' (*ppos) to indicate where we are in devList.
3249 * This also assumes the user has posted a read buffer large
3250 * enough to hold the complete output; but that's life in /proc.
3255 /* Print header first */
3257 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__"\n");
3259 buf += sprintf(buf,"\n");
3263 down(&yaffs_context_lock);
3265 /* Locate and print the Nth entry. Order N-squared but N is small. */
3266 ylist_for_each(item, &yaffs_context_list) {
3267 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3268 yaffs_Device *dev = dc->dev;
3270 if (n < (step & ~1)) {
3275 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
3276 buf = yaffs_dump_dev_part0(buf, dev);
3278 buf = yaffs_dump_dev_part1(buf, dev);
3282 up(&yaffs_context_lock);
3285 return buf - page < count ? buf - page : count;
3288 static int yaffs_stats_proc_read(char *page,
3290 off_t offset, int count, int *eof, void *data)
3292 struct ylist_head *item;
3296 down(&yaffs_context_lock);
3298 /* Locate and print the Nth entry. Order N-squared but N is small. */
3299 ylist_for_each(item, &yaffs_context_list) {
3300 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3301 yaffs_Device *dev = dc->dev;
3305 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3307 buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
3308 n, dev->nFreeChunks, erasedChunks,
3309 dev->backgroundGCs, dev->oldestDirtyGCs,
3310 dev->nObjects, dev->nTnodes);
3312 up(&yaffs_context_lock);
3315 return buf - page < count ? buf - page : count;
3319 * Set the verbosity of the warnings and error messages.
3321 * Note that the names can only be a..z or _ with the current code.
3326 unsigned mask_bitfield;
3328 {"allocate", YAFFS_TRACE_ALLOCATE},
3329 {"always", YAFFS_TRACE_ALWAYS},
3330 {"background", YAFFS_TRACE_BACKGROUND},
3331 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3332 {"buffers", YAFFS_TRACE_BUFFERS},
3333 {"bug", YAFFS_TRACE_BUG},
3334 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3335 {"deletion", YAFFS_TRACE_DELETION},
3336 {"erase", YAFFS_TRACE_ERASE},
3337 {"error", YAFFS_TRACE_ERROR},
3338 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3339 {"gc", YAFFS_TRACE_GC},
3340 {"lock", YAFFS_TRACE_LOCK},
3341 {"mtd", YAFFS_TRACE_MTD},
3342 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3343 {"os", YAFFS_TRACE_OS},
3344 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3345 {"scan", YAFFS_TRACE_SCAN},
3346 {"tracing", YAFFS_TRACE_TRACING},
3347 {"sync", YAFFS_TRACE_SYNC},
3348 {"write", YAFFS_TRACE_WRITE},
3350 {"verify", YAFFS_TRACE_VERIFY},
3351 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3352 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3353 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3355 {"all", 0xffffffff},
3360 #define MAX_MASK_NAME_LENGTH 40
3361 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3362 unsigned long count, void *data)
3364 unsigned rg = 0, mask_bitfield;
3368 char substring[MAX_MASK_NAME_LENGTH + 1];
3374 rg = yaffs_traceMask;
3376 while (!done && (pos < count)) {
3378 while ((pos < count) && isspace(buf[pos]))
3395 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3397 if (end > buf + pos) {
3398 mask_name = "numeral";
3399 len = end - (buf + pos);
3403 for (x = buf + pos, i = 0;
3404 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3405 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3407 substring[i] = '\0';
3409 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3410 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3411 mask_name = mask_flags[i].mask_name;
3412 mask_bitfield = mask_flags[i].mask_bitfield;
3419 if (mask_name != NULL) {
3423 rg &= ~mask_bitfield;
3426 rg |= mask_bitfield;
3432 rg |= mask_bitfield;
3438 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3440 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
3442 if (rg & YAFFS_TRACE_ALWAYS) {
3443 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3445 flag = ((rg & mask_flags[i].mask_bitfield) ==
3446 mask_flags[i].mask_bitfield) ? '+' : '-';
3447 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3455 static int yaffs_proc_write(struct file *file, const char *buf,
3456 unsigned long count, void *data)
3458 return yaffs_proc_write_trace_options(file, buf, count, data);
3461 /* Stuff to handle installation of file systems */
3462 struct file_system_to_install {
3463 struct file_system_type *fst;
3467 static struct file_system_to_install fs_to_install[] = {
3468 {&yaffs_fs_type, 0},
3469 {&yaffs2_fs_type, 0},
3473 static int __init init_yaffs_fs(void)
3476 struct file_system_to_install *fsinst;
3478 T(YAFFS_TRACE_ALWAYS,
3479 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3481 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3482 T(YAFFS_TRACE_ALWAYS,
3483 (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3489 init_MUTEX(&yaffs_context_lock);
3491 /* Install the proc_fs entries */
3492 my_proc_entry = create_proc_entry("yaffs",
3496 if (my_proc_entry) {
3497 my_proc_entry->write_proc = yaffs_proc_write;
3498 my_proc_entry->read_proc = yaffs_proc_read;
3499 my_proc_entry->data = NULL;
3503 debug_proc_entry = create_proc_entry("yaffs_stats",
3507 if (debug_proc_entry) {
3508 debug_proc_entry->write_proc = NULL;
3509 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3510 debug_proc_entry->data = NULL;
3514 /* Now add the file system entries */
3516 fsinst = fs_to_install;
3518 while (fsinst->fst && !error) {
3519 error = register_filesystem(fsinst->fst);
3521 fsinst->installed = 1;
3525 /* Any errors? uninstall */
3527 fsinst = fs_to_install;
3529 while (fsinst->fst) {
3530 if (fsinst->installed) {
3531 unregister_filesystem(fsinst->fst);
3532 fsinst->installed = 0;
3541 static void __exit exit_yaffs_fs(void)
3544 struct file_system_to_install *fsinst;
3546 T(YAFFS_TRACE_ALWAYS,
3547 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3549 remove_proc_entry("yaffs", YPROC_ROOT);
3550 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3552 fsinst = fs_to_install;
3554 while (fsinst->fst) {
3555 if (fsinst->installed) {
3556 unregister_filesystem(fsinst->fst);
3557 fsinst->installed = 0;
3563 module_init(init_yaffs_fs)
3564 module_exit(exit_yaffs_fs)
3566 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3567 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3568 MODULE_LICENSE("GPL");