2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * Luc van OostenRyck for numerous patches.
10 * Nick Bane for numerous patches.
11 * Nick Bane for 2.5/2.6 integration.
12 * Andras Toth for mknod rdev issue.
13 * Michael Fischer for finding the problem with inode inconsistency.
14 * Some code bodily lifted from JFFS
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
23 * This is the file system front-end to YAFFS that hooks it up to
27 * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
29 * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
31 * >> inode->u.generic_ip points to the associated yaffs_Object.
34 #include <linux/version.h>
36 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
37 #define YAFFS_COMPILE_BACKGROUND
40 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28))
41 #define YAFFS_COMPILE_EXPORTFS
44 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
45 #define YAFFS_USE_SETATTR_COPY
47 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
48 #define YAFFS_HAS_EVICT_INODE
51 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
52 #define YAFFS_NEW_FOLLOW_LINK 1
54 #define YAFFS_NEW_FOLLOW_LINK 0
57 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
58 #include <linux/config.h>
61 #include <linux/kernel.h>
62 #include <linux/module.h>
63 #include <linux/slab.h>
64 #include <linux/init.h>
66 #include <linux/proc_fs.h>
67 #include <linux/smp_lock.h>
68 #include <linux/pagemap.h>
69 #include <linux/mtd/mtd.h>
70 #include <linux/interrupt.h>
71 #include <linux/string.h>
72 #include <linux/ctype.h>
74 #if (YAFFS_NEW_FOLLOW_LINK == 1)
75 #include <linux/namei.h>
78 #ifdef YAFFS_COMPILE_EXPORTFS
79 #include <linux/exportfs.h>
82 #ifdef YAFFS_COMPILE_BACKGROUND
83 #include <linux/kthread.h>
84 #include <linux/delay.h>
85 #include <linux/freezer.h>
88 #include <asm/div64.h>
90 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
92 #include <linux/statfs.h>
94 #define UnlockPage(p) unlock_page(p)
95 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
97 /* FIXME: use sb->s_id instead ? */
98 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
102 #include <linux/locks.h>
103 #define BDEVNAME_SIZE 0
104 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
106 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
107 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
113 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
114 #define YPROC_ROOT (&proc_root)
116 #define YPROC_ROOT NULL
119 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
120 #define WRITE_SIZE_STR "writesize"
121 #define WRITE_SIZE(mtd) ((mtd)->writesize)
123 #define WRITE_SIZE_STR "oobblock"
124 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
127 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
128 #define YAFFS_USE_WRITE_BEGIN_END 1
130 #define YAFFS_USE_WRITE_BEGIN_END 0
133 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
134 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
136 uint64_t result = partition_size;
137 do_div(result, block_size);
138 return (uint32_t)result;
141 #define YCALCBLOCKS(s, b) ((s)/(b))
144 #include <linux/uaccess.h>
145 #include <linux/mtd/mtd.h>
147 #include "yportenv.h"
148 #include "yaffs_trace.h"
149 #include "yaffs_guts.h"
151 #include "yaffs_linux.h"
153 #include "yaffs_mtdif.h"
154 #include "yaffs_mtdif1.h"
155 #include "yaffs_mtdif2.h"
157 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
158 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
159 unsigned int yaffs_auto_checkpoint = 1;
160 unsigned int yaffs_gc_control = 1;
162 /* Module Parameters */
163 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
164 module_param(yaffs_traceMask, uint, 0644);
165 module_param(yaffs_wr_attempts, uint, 0644);
166 module_param(yaffs_auto_checkpoint, uint, 0644);
167 module_param(yaffs_gc_control, uint, 0644);
169 MODULE_PARM(yaffs_traceMask, "i");
170 MODULE_PARM(yaffs_wr_attempts, "i");
171 MODULE_PARM(yaffs_auto_checkpoint, "i");
172 MODULE_PARM(yaffs_gc_control, "i");
175 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
176 /* use iget and read_inode */
177 #define Y_IGET(sb, inum) iget((sb), (inum))
178 static void yaffs_read_inode(struct inode *inode);
181 /* Call local equivalent */
182 #define YAFFS_USE_OWN_IGET
183 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
185 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
188 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
189 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
191 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
194 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
195 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
197 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
198 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
200 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
204 #define update_dir_time(dir) do {\
205 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
208 static void yaffs_put_super(struct super_block *sb);
210 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
212 static ssize_t yaffs_hold_space(struct file *f);
213 static void yaffs_release_space(struct file *f);
215 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
216 static int yaffs_file_flush(struct file *file, fl_owner_t id);
218 static int yaffs_file_flush(struct file *file);
221 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
222 static int yaffs_sync_object(struct file *file, int datasync);
224 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
228 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
230 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
231 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
232 struct nameidata *n);
233 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
234 struct nameidata *n);
236 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
237 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
239 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
240 struct dentry *dentry);
241 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
242 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
243 const char *symname);
244 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
246 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
247 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
250 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
253 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
254 struct inode *new_dir, struct dentry *new_dentry);
255 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
257 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
258 static int yaffs_sync_fs(struct super_block *sb, int wait);
259 static void yaffs_write_super(struct super_block *sb);
261 static int yaffs_sync_fs(struct super_block *sb);
262 static int yaffs_write_super(struct super_block *sb);
265 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
266 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
267 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
268 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
270 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
273 #ifdef YAFFS_HAS_PUT_INODE
274 static void yaffs_put_inode(struct inode *inode);
277 #ifdef YAFFS_HAS_EVICT_INODE
278 static void yaffs_evict_inode(struct inode *);
280 static void yaffs_delete_inode(struct inode *);
281 static void yaffs_clear_inode(struct inode *);
284 static int yaffs_readpage(struct file *file, struct page *page);
285 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
286 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
288 static int yaffs_writepage(struct page *page);
291 #ifdef CONFIG_YAFFS_XATTR
292 int yaffs_setxattr(struct dentry *dentry, const char *name,
293 const void *value, size_t size, int flags);
294 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
296 int yaffs_removexattr(struct dentry *dentry, const char *name);
297 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
301 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
302 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
303 loff_t pos, unsigned len, unsigned flags,
304 struct page **pagep, void **fsdata);
305 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
306 loff_t pos, unsigned len, unsigned copied,
307 struct page *pg, void *fsdadata);
309 static int yaffs_prepare_write(struct file *f, struct page *pg,
310 unsigned offset, unsigned to);
311 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
316 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
318 #if (YAFFS_NEW_FOLLOW_LINK == 1)
319 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
320 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
322 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
325 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev);
327 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
329 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
332 static struct address_space_operations yaffs_file_address_operations = {
333 .readpage = yaffs_readpage,
334 .writepage = yaffs_writepage,
335 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
336 .write_begin = yaffs_write_begin,
337 .write_end = yaffs_write_end,
339 .prepare_write = yaffs_prepare_write,
340 .commit_write = yaffs_commit_write,
345 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
346 static const struct file_operations yaffs_file_operations = {
347 .read = do_sync_read,
348 .write = do_sync_write,
349 .aio_read = generic_file_aio_read,
350 .aio_write = generic_file_aio_write,
351 .mmap = generic_file_mmap,
352 .flush = yaffs_file_flush,
353 .fsync = yaffs_sync_object,
354 .splice_read = generic_file_splice_read,
355 .splice_write = generic_file_splice_write,
356 .llseek = generic_file_llseek,
359 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
361 static const struct file_operations yaffs_file_operations = {
362 .read = do_sync_read,
363 .write = do_sync_write,
364 .aio_read = generic_file_aio_read,
365 .aio_write = generic_file_aio_write,
366 .mmap = generic_file_mmap,
367 .flush = yaffs_file_flush,
368 .fsync = yaffs_sync_object,
369 .sendfile = generic_file_sendfile,
374 static const struct file_operations yaffs_file_operations = {
375 .read = generic_file_read,
376 .write = generic_file_write,
377 .mmap = generic_file_mmap,
378 .flush = yaffs_file_flush,
379 .fsync = yaffs_sync_object,
380 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
381 .sendfile = generic_file_sendfile,
386 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
387 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
389 void * kaddr = kmap_atomic(page, KM_USER0);
390 memset(kaddr + start, 0, end - start);
391 kunmap_atomic(kaddr, KM_USER0);
392 flush_dcache_page(page);
397 static const struct inode_operations yaffs_file_inode_operations = {
398 .setattr = yaffs_setattr,
399 #ifdef CONFIG_YAFFS_XATTR
400 .setxattr = yaffs_setxattr,
401 .getxattr = yaffs_getxattr,
402 .listxattr = yaffs_listxattr,
403 .removexattr = yaffs_removexattr,
407 static const struct inode_operations yaffs_symlink_inode_operations = {
408 .readlink = yaffs_readlink,
409 .follow_link = yaffs_follow_link,
410 #if (YAFFS_NEW_FOLLOW_LINK == 1)
411 .put_link = yaffs_put_link,
413 .setattr = yaffs_setattr,
414 #ifdef CONFIG_YAFFS_XATTR
415 .setxattr = yaffs_setxattr,
416 .getxattr = yaffs_getxattr,
417 .listxattr = yaffs_listxattr,
418 .removexattr = yaffs_removexattr,
422 static const struct inode_operations yaffs_dir_inode_operations = {
423 .create = yaffs_create,
424 .lookup = yaffs_lookup,
426 .unlink = yaffs_unlink,
427 .symlink = yaffs_symlink,
428 .mkdir = yaffs_mkdir,
429 .rmdir = yaffs_unlink,
430 .mknod = yaffs_mknod,
431 .rename = yaffs_rename,
432 .setattr = yaffs_setattr,
433 #ifdef CONFIG_YAFFS_XATTR
434 .setxattr = yaffs_setxattr,
435 .getxattr = yaffs_getxattr,
436 .listxattr = yaffs_listxattr,
437 .removexattr = yaffs_removexattr,
441 static const struct file_operations yaffs_dir_operations = {
442 .read = generic_read_dir,
443 .readdir = yaffs_readdir,
444 .fsync = yaffs_sync_object,
445 .llseek = yaffs_dir_llseek,
448 static const struct super_operations yaffs_super_ops = {
449 .statfs = yaffs_statfs,
451 #ifndef YAFFS_USE_OWN_IGET
452 .read_inode = yaffs_read_inode,
454 #ifdef YAFFS_HAS_PUT_INODE
455 .put_inode = yaffs_put_inode,
457 .put_super = yaffs_put_super,
458 #ifdef YAFFS_HAS_EVICT_INODE
459 .evict_inode = yaffs_evict_inode,
461 .delete_inode = yaffs_delete_inode,
462 .clear_inode = yaffs_clear_inode,
464 .sync_fs = yaffs_sync_fs,
465 .write_super = yaffs_write_super,
469 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
471 #ifdef YAFFS_USE_SETATTR_COPY
472 setattr_copy(inode,attr);
475 return inode_setattr(inode, attr);
480 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
482 return yaffs_gc_control;
485 static void yaffs_GrossLock(yaffs_Device *dev)
487 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
488 down(&(yaffs_DeviceToLC(dev)->grossLock));
489 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
492 static void yaffs_GrossUnlock(yaffs_Device *dev)
494 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
495 up(&(yaffs_DeviceToLC(dev)->grossLock));
498 #ifdef YAFFS_COMPILE_EXPORTFS
500 static struct inode *
501 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
503 return Y_IGET(sb, ino);
506 static struct dentry *
507 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
509 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
512 static struct dentry *
513 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
515 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
518 struct dentry *yaffs2_get_parent(struct dentry *dentry)
521 struct super_block *sb = dentry->d_inode->i_sb;
522 struct dentry *parent = ERR_PTR(-ENOENT);
524 unsigned long parent_ino;
526 yaffs_Object *parent_obj;
528 d_obj = yaffs_InodeToObject(dentry->d_inode);
531 parent_obj = d_obj->parent;
533 parent_ino = yaffs_GetObjectInode(parent_obj);
534 inode = Y_IGET(sb, parent_ino);
537 parent = ERR_CAST(inode);
539 parent = d_obtain_alias(inode);
540 if (!IS_ERR(parent)) {
541 parent = ERR_PTR(-ENOMEM);
551 /* Just declare a zero structure as a NULL value implies
552 * using the default functions of exportfs.
555 static struct export_operations yaffs_export_ops =
557 .fh_to_dentry = yaffs2_fh_to_dentry,
558 .fh_to_parent = yaffs2_fh_to_parent,
559 .get_parent = yaffs2_get_parent,
564 /*-----------------------------------------------------------------*/
565 /* Directory search context allows us to unlock access to yaffs during
566 * filldir without causing problems with the directory being modified.
567 * This is similar to the tried and tested mechanism used in yaffs direct.
569 * A search context iterates along a doubly linked list of siblings in the
570 * directory. If the iterating object is deleted then this would corrupt
571 * the list iteration, likely causing a crash. The search context avoids
572 * this by using the removeObjectCallback to move the search context to the
573 * next object before the object is deleted.
575 * Many readdirs (and thus seach conexts) may be alive simulateously so
576 * each yaffs_Device has a list of these.
578 * A seach context lives for the duration of a readdir.
580 * All these functions must be called while yaffs is locked.
583 struct yaffs_SearchContext {
585 yaffs_Object *dirObj;
586 yaffs_Object *nextReturn;
587 struct ylist_head others;
591 * yaffs_NewSearch() creates a new search context, initialises it and
592 * adds it to the device's search context list.
594 * Called at start of readdir.
596 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
598 yaffs_Device *dev = dir->myDev;
599 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
603 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
604 sc->nextReturn = NULL;
606 sc->nextReturn = ylist_entry(
607 dir->variant.directoryVariant.children.next,
608 yaffs_Object,siblings);
609 YINIT_LIST_HEAD(&sc->others);
610 ylist_add(&sc->others,&(yaffs_DeviceToLC(dev)->searchContexts));
616 * yaffs_EndSearch() disposes of a search context and cleans up.
618 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
621 ylist_del(&sc->others);
627 * yaffs_SearchAdvance() moves a search context to the next object.
628 * Called when the search iterates or when an object removal causes
629 * the search context to be moved to the next object.
631 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
636 if( sc->nextReturn == NULL ||
637 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
638 sc->nextReturn = NULL;
640 struct ylist_head *next = sc->nextReturn->siblings.next;
642 if( next == &sc->dirObj->variant.directoryVariant.children)
643 sc->nextReturn = NULL; /* end of list */
645 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
650 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
651 * We check open search contexts and advance any which are currently
652 * on the object being iterated.
654 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
657 struct ylist_head *i;
658 struct yaffs_SearchContext *sc;
659 struct ylist_head *search_contexts = &(yaffs_DeviceToLC(obj->myDev)->searchContexts);
662 /* Iterate through the directory search contexts.
663 * If any are currently on the object being removed, then advance
664 * the search context to the next object to prevent a hanging pointer.
666 ylist_for_each(i, search_contexts) {
668 sc = ylist_entry(i, struct yaffs_SearchContext,others);
669 if(sc->nextReturn == obj)
670 yaffs_SearchAdvance(sc);
677 /*-----------------------------------------------------------------*/
679 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
682 unsigned char *alias;
685 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
687 yaffs_GrossLock(dev);
689 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
691 yaffs_GrossUnlock(dev);
696 ret = vfs_readlink(dentry, buffer, buflen, alias);
701 #if (YAFFS_NEW_FOLLOW_LINK == 1)
702 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
704 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
707 unsigned char *alias;
709 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
711 yaffs_GrossLock(dev);
713 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
714 yaffs_GrossUnlock(dev);
721 #if (YAFFS_NEW_FOLLOW_LINK == 1)
722 nd_set_link(nd, alias);
727 ret = vfs_follow_link(nd, alias);
734 #if (YAFFS_NEW_FOLLOW_LINK == 1)
735 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) {
740 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
744 * Lookup is used to find objects in the fs
746 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
748 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
751 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
755 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
757 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
759 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
760 yaffs_GrossLock(dev);
763 (TSTR("yaffs_lookup for %d:%s\n"),
764 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
766 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
767 dentry->d_name.name);
769 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
771 /* Can't hold gross lock when calling yaffs_get_inode() */
772 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
773 yaffs_GrossUnlock(dev);
777 (TSTR("yaffs_lookup found %d\n"), obj->objectId));
779 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
783 (TSTR("yaffs_loookup dentry \n")));
784 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
785 * d_add even if NULL inode */
787 /*dget(dentry); // try to solve directory bug */
788 d_add(dentry, inode);
796 T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
800 /* added NCB for 2.5/6 compatability - forces add even if inode is
801 * NULL which creates dentry hash */
802 d_add(dentry, inode);
808 #ifdef YAFFS_HAS_PUT_INODE
810 /* For now put inode is just for debugging
811 * Put inode is called when the inode **structure** is put.
813 static void yaffs_put_inode(struct inode *inode)
816 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
817 atomic_read(&inode->i_count)));
823 static void yaffs_UnstitchObject(struct inode *inode, yaffs_Object *obj)
825 /* Clear the association between the inode and
829 yaffs_InodeToObjectLV(inode) = NULL;
831 /* If the object freeing was deferred, then the real
833 * This should fix the inode inconsistency problem.
835 yaffs_HandleDeferedFree(obj);
838 #ifdef YAFFS_HAS_EVICT_INODE
839 /* yaffs_evict_inode combines into one operation what was previously done in
840 * yaffs_clear_inode() and yaffs_delete_inode()
843 static void yaffs_evict_inode( struct inode *inode)
849 obj = yaffs_InodeToObject(inode);
852 (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
853 atomic_read(&inode->i_count),
854 obj ? "object exists" : "null object"));
856 if (!inode->i_nlink && !is_bad_inode(inode))
858 truncate_inode_pages(&inode->i_data,0);
862 yaffs_GrossLock(dev);
863 yaffs_DeleteObject(obj);
864 yaffs_GrossUnlock(dev);
866 end_writeback(inode);
869 yaffs_GrossLock(dev);
870 yaffs_UnstitchObject(inode,obj);
871 yaffs_GrossUnlock(dev);
878 /* clear is called to tell the fs to release any per-inode data it holds.
879 * The object might still exist on disk and is just being thrown out of the cache
880 * or else the object has actually been deleted and we're being called via
882 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
885 static void yaffs_clear_inode(struct inode *inode)
890 obj = yaffs_InodeToObject(inode);
893 (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
894 atomic_read(&inode->i_count),
895 obj ? "object exists" : "null object"));
899 yaffs_GrossLock(dev);
900 yaffs_UnstitchObject(inode,obj);
901 yaffs_GrossUnlock(dev);
906 /* delete is called when the link count is zero and the inode
907 * is put (ie. nobody wants to know about it anymore, time to
909 * NB Must call clear_inode()
911 static void yaffs_delete_inode(struct inode *inode)
913 yaffs_Object *obj = yaffs_InodeToObject(inode);
917 (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
918 atomic_read(&inode->i_count),
919 obj ? "object exists" : "null object"));
923 yaffs_GrossLock(dev);
924 yaffs_DeleteObject(obj);
925 yaffs_GrossUnlock(dev);
927 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
928 truncate_inode_pages(&inode->i_data, 0);
935 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
936 static int yaffs_file_flush(struct file *file, fl_owner_t id)
938 static int yaffs_file_flush(struct file *file)
941 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
943 yaffs_Device *dev = obj->myDev;
946 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->objectId,
947 obj->dirty ? "dirty" : "clean"));
949 yaffs_GrossLock(dev);
951 yaffs_FlushFile(obj, 1, 0);
953 yaffs_GrossUnlock(dev);
958 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
960 /* Lifted from jffs2 */
963 unsigned char *pg_buf;
969 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
970 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
971 (unsigned)PAGE_CACHE_SIZE));
973 obj = yaffs_DentryToObject(f->f_dentry);
977 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
978 BUG_ON(!PageLocked(pg));
985 /* FIXME: Can kmap fail? */
987 yaffs_GrossLock(dev);
989 ret = yaffs_ReadDataFromFile(obj, pg_buf,
990 pg->index << PAGE_CACHE_SHIFT,
993 yaffs_GrossUnlock(dev);
999 ClearPageUptodate(pg);
1002 SetPageUptodate(pg);
1006 flush_dcache_page(pg);
1009 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
1013 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1015 int ret = yaffs_readpage_nolock(f, pg);
1020 static int yaffs_readpage(struct file *f, struct page *pg)
1024 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
1025 ret=yaffs_readpage_unlock(f, pg);
1026 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
1030 /* writepage inspired by/stolen from smbfs */
1032 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1033 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1035 static int yaffs_writepage(struct page *page)
1039 struct address_space *mapping = page->mapping;
1040 struct inode *inode;
1041 unsigned long end_index;
1050 inode = mapping->host;
1053 i_size = i_size_read(inode);
1055 end_index = i_size >> PAGE_CACHE_SHIFT;
1057 if(page->index < end_index)
1058 nBytes = PAGE_CACHE_SIZE;
1060 nBytes = i_size & (PAGE_CACHE_SIZE -1);
1062 if (page->index > end_index || !nBytes) {
1064 (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
1065 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1066 (unsigned)inode->i_size));
1068 (TSTR(" -> don't care!!\n")));
1070 zero_user_segment(page,0,PAGE_CACHE_SIZE);
1071 set_page_writeback(page);
1073 end_page_writeback(page);
1078 if(nBytes != PAGE_CACHE_SIZE)
1079 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
1083 buffer = kmap(page);
1085 obj = yaffs_InodeToObject(inode);
1087 yaffs_GrossLock(dev);
1090 (TSTR("yaffs_writepage at %08x, size %08x\n"),
1091 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
1093 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
1094 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1096 nWritten = yaffs_WriteDataToFile(obj, buffer,
1097 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
1099 yaffs_MarkSuperBlockDirty(dev);
1102 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
1103 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1105 yaffs_GrossUnlock(dev);
1108 set_page_writeback(page);
1110 end_page_writeback(page);
1113 return (nWritten == nBytes) ? 0 : -ENOSPC;
1117 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1118 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1119 loff_t pos, unsigned len, unsigned flags,
1120 struct page **pagep, void **fsdata)
1122 struct page *pg = NULL;
1123 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1129 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1130 pg = grab_cache_page_write_begin(mapping, index, flags);
1132 pg = __grab_cache_page(mapping, index);
1141 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
1142 (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
1145 space_held = yaffs_hold_space(filp);
1152 /* Update page if required */
1154 if (!Page_Uptodate(pg))
1155 ret = yaffs_readpage_nolock(filp, pg);
1160 /* Happy path return */
1161 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
1167 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
1169 yaffs_release_space(filp);
1172 page_cache_release(pg);
1179 static int yaffs_prepare_write(struct file *f, struct page *pg,
1180 unsigned offset, unsigned to)
1182 T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
1184 if (!Page_Uptodate(pg))
1185 return yaffs_readpage_nolock(f, pg);
1190 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1191 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1192 loff_t pos, unsigned len, unsigned copied,
1193 struct page *pg, void *fsdadata)
1197 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1200 addr = kva + offset_into_page;
1203 ("yaffs_write_end addr %p pos %x nBytes %d\n",
1204 addr,(unsigned)pos, copied));
1206 ret = yaffs_file_write(filp, addr, copied, &pos);
1208 if (ret != copied) {
1210 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
1219 yaffs_release_space(filp);
1221 page_cache_release(pg);
1226 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1231 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1232 int nBytes = to - offset;
1235 unsigned spos = pos;
1239 addr = kva + offset;
1241 saddr = (unsigned) addr;
1244 (TSTR("yaffs_commit_write addr %x pos %x nBytes %d\n"),
1245 saddr, spos, nBytes));
1247 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1249 if (nWritten != nBytes) {
1251 (TSTR("yaffs_commit_write not same size nWritten %d nBytes %d\n"),
1261 (TSTR("yaffs_commit_write returning %d\n"),
1262 nWritten == nBytes ? 0 : nWritten));
1264 return nWritten == nBytes ? 0 : nWritten;
1269 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1274 /* Check mode against the variant type and attempt to repair if broken. */
1275 __u32 mode = obj->yst_mode;
1276 switch (obj->variantType) {
1277 case YAFFS_OBJECT_TYPE_FILE:
1278 if (!S_ISREG(mode)) {
1279 obj->yst_mode &= ~S_IFMT;
1280 obj->yst_mode |= S_IFREG;
1284 case YAFFS_OBJECT_TYPE_SYMLINK:
1285 if (!S_ISLNK(mode)) {
1286 obj->yst_mode &= ~S_IFMT;
1287 obj->yst_mode |= S_IFLNK;
1291 case YAFFS_OBJECT_TYPE_DIRECTORY:
1292 if (!S_ISDIR(mode)) {
1293 obj->yst_mode &= ~S_IFMT;
1294 obj->yst_mode |= S_IFDIR;
1298 case YAFFS_OBJECT_TYPE_UNKNOWN:
1299 case YAFFS_OBJECT_TYPE_HARDLINK:
1300 case YAFFS_OBJECT_TYPE_SPECIAL:
1306 inode->i_flags |= S_NOATIME;
1308 inode->i_ino = obj->objectId;
1309 inode->i_mode = obj->yst_mode;
1310 inode->i_uid = obj->yst_uid;
1311 inode->i_gid = obj->yst_gid;
1312 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1313 inode->i_blksize = inode->i_sb->s_blocksize;
1315 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1317 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1318 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1319 inode->i_atime.tv_nsec = 0;
1320 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1321 inode->i_mtime.tv_nsec = 0;
1322 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1323 inode->i_ctime.tv_nsec = 0;
1325 inode->i_rdev = obj->yst_rdev;
1326 inode->i_atime = obj->yst_atime;
1327 inode->i_mtime = obj->yst_mtime;
1328 inode->i_ctime = obj->yst_ctime;
1330 inode->i_size = yaffs_GetObjectFileLength(obj);
1331 inode->i_blocks = (inode->i_size + 511) >> 9;
1333 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1336 (TSTR("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n"),
1337 inode->i_mode, inode->i_uid, inode->i_gid,
1338 (int)inode->i_size, atomic_read(&inode->i_count)));
1340 switch (obj->yst_mode & S_IFMT) {
1341 default: /* fifo, device or socket */
1342 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1343 init_special_inode(inode, obj->yst_mode,
1344 old_decode_dev(obj->yst_rdev));
1346 init_special_inode(inode, obj->yst_mode,
1347 (dev_t) (obj->yst_rdev));
1350 case S_IFREG: /* file */
1351 inode->i_op = &yaffs_file_inode_operations;
1352 inode->i_fop = &yaffs_file_operations;
1353 inode->i_mapping->a_ops =
1354 &yaffs_file_address_operations;
1356 case S_IFDIR: /* directory */
1357 inode->i_op = &yaffs_dir_inode_operations;
1358 inode->i_fop = &yaffs_dir_operations;
1360 case S_IFLNK: /* symlink */
1361 inode->i_op = &yaffs_symlink_inode_operations;
1365 yaffs_InodeToObjectLV(inode) = obj;
1367 obj->myInode = inode;
1371 (TSTR("yaffs_FileInode invalid parameters\n")));
1376 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1379 struct inode *inode;
1383 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1390 (TSTR("yaffs_get_inode for NULL object!!\n")));
1396 (TSTR("yaffs_get_inode for object %d\n"), obj->objectId));
1398 inode = Y_IGET(sb, obj->objectId);
1402 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1403 /* iget also increments the inode's i_count */
1404 /* NB You can't be holding grossLock or deadlock will happen! */
1409 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1414 struct inode *inode;
1417 obj = yaffs_DentryToObject(f->f_dentry);
1421 yaffs_GrossLock(dev);
1423 inode = f->f_dentry->d_inode;
1425 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1426 ipos = inode->i_size;
1432 (TSTR("yaffs_file_write: hey obj is null!\n")));
1435 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1436 "to object %d at %d(%x)\n"),
1437 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1439 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1441 yaffs_MarkSuperBlockDirty(dev);
1444 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1445 (unsigned )n,(unsigned)n));
1450 if (ipos > inode->i_size) {
1451 inode->i_size = ipos;
1452 inode->i_blocks = (ipos + 511) >> 9;
1455 (TSTR("yaffs_file_write size updated to %d bytes, "
1457 ipos, (int)(inode->i_blocks)));
1461 yaffs_GrossUnlock(dev);
1462 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1465 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1466 /* For now we just assume few parallel writes and check against a small number. */
1467 /* Todo: need to do this with a counter to handle parallel reads better */
1469 static ssize_t yaffs_hold_space(struct file *f)
1477 obj = yaffs_DentryToObject(f->f_dentry);
1481 yaffs_GrossLock(dev);
1483 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1485 yaffs_GrossUnlock(dev);
1487 return (nFreeChunks > 20) ? 1 : 0;
1490 static void yaffs_release_space(struct file *f)
1496 obj = yaffs_DentryToObject(f->f_dentry);
1500 yaffs_GrossLock(dev);
1503 yaffs_GrossUnlock(dev);
1507 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1515 offset += i_size_read(file->f_path.dentry->d_inode);
1518 offset += file->f_pos;
1523 if (offset != file->f_pos)
1524 file->f_pos = offset;
1533 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1537 struct yaffs_SearchContext *sc;
1538 struct inode *inode = f->f_dentry->d_inode;
1539 unsigned long offset, curoffs;
1543 char name[YAFFS_MAX_NAME_LENGTH + 1];
1545 obj = yaffs_DentryToObject(f->f_dentry);
1548 yaffs_GrossLock(dev);
1550 yaffs_DeviceToLC(dev)->readdirProcess = current;
1554 sc = yaffs_NewSearch(obj);
1560 T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1564 (TSTR("yaffs_readdir: entry . ino %d \n"),
1565 (int)inode->i_ino));
1566 yaffs_GrossUnlock(dev);
1567 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0){
1568 yaffs_GrossLock(dev);
1571 yaffs_GrossLock(dev);
1577 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1578 (int)f->f_dentry->d_parent->d_inode->i_ino));
1579 yaffs_GrossUnlock(dev);
1580 if (filldir(dirent, "..", 2, offset,
1581 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
1582 yaffs_GrossLock(dev);
1585 yaffs_GrossLock(dev);
1592 /* If the directory has changed since the open or last call to
1593 readdir, rewind to after the 2 canned entries. */
1594 if (f->f_version != inode->i_version) {
1597 f->f_version = inode->i_version;
1600 while(sc->nextReturn){
1603 if (curoffs >= offset) {
1604 int this_inode = yaffs_GetObjectInode(l);
1605 int this_type = yaffs_GetObjectType(l);
1607 yaffs_GetObjectName(l, name,
1608 YAFFS_MAX_NAME_LENGTH + 1);
1610 (TSTR("yaffs_readdir: %s inode %d\n"),
1611 name, yaffs_GetObjectInode(l)));
1613 yaffs_GrossUnlock(dev);
1621 yaffs_GrossLock(dev);
1625 yaffs_GrossLock(dev);
1630 yaffs_SearchAdvance(sc);
1634 yaffs_EndSearch(sc);
1635 yaffs_DeviceToLC(dev)->readdirProcess = NULL;
1636 yaffs_GrossUnlock(dev);
1644 * File creation. Allocate an inode, and we're done..
1647 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1650 #define YCRED(x) (x->cred)
1653 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1654 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1657 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1661 struct inode *inode;
1663 yaffs_Object *obj = NULL;
1666 yaffs_Object *parent = yaffs_InodeToObject(dir);
1668 int error = -ENOSPC;
1669 uid_t uid = YCRED(current)->fsuid;
1670 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1672 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1677 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1678 parent->objectId, parent->variantType));
1681 (TSTR("yaffs_mknod: could not get parent object\n")));
1685 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1686 "mode %x dev %x\n"),
1687 dentry->d_name.name, mode, rdev));
1689 dev = parent->myDev;
1691 yaffs_GrossLock(dev);
1693 switch (mode & S_IFMT) {
1695 /* Special (socket, fifo, device...) */
1696 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1697 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1698 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1699 gid, old_encode_dev(rdev));
1701 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1705 case S_IFREG: /* file */
1706 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1707 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1710 case S_IFDIR: /* directory */
1712 (TSTR("yaffs_mknod: making directory\n")));
1713 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1716 case S_IFLNK: /* symlink */
1717 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1718 obj = NULL; /* Do we ever get here? */
1722 /* Can not call yaffs_get_inode() with gross lock held */
1723 yaffs_GrossUnlock(dev);
1726 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1727 d_instantiate(dentry, inode);
1728 update_dir_time(dir);
1730 (TSTR("yaffs_mknod created object %d count = %d\n"),
1731 obj->objectId, atomic_read(&inode->i_count)));
1733 yaffs_FillInodeFromObject(dir,parent);
1736 (TSTR("yaffs_mknod failed making object\n")));
1743 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1746 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1747 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1751 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1752 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1753 struct nameidata *n)
1755 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1758 T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1759 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1762 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1770 (TSTR("yaffs_unlink %d:%s\n"),
1772 dentry->d_name.name));
1773 obj = yaffs_InodeToObject(dir);
1776 yaffs_GrossLock(dev);
1778 retVal = yaffs_Unlink(obj, dentry->d_name.name);
1780 if (retVal == YAFFS_OK) {
1781 dentry->d_inode->i_nlink--;
1783 yaffs_GrossUnlock(dev);
1784 mark_inode_dirty(dentry->d_inode);
1785 update_dir_time(dir);
1788 yaffs_GrossUnlock(dev);
1795 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1796 struct dentry *dentry)
1798 struct inode *inode = old_dentry->d_inode;
1799 yaffs_Object *obj = NULL;
1800 yaffs_Object *link = NULL;
1803 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1805 obj = yaffs_InodeToObject(inode);
1808 yaffs_GrossLock(dev);
1810 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1811 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1815 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1816 d_instantiate(dentry, old_dentry->d_inode);
1817 atomic_inc(&old_dentry->d_inode->i_count);
1819 (TSTR("yaffs_link link count %d i_count %d\n"),
1820 old_dentry->d_inode->i_nlink,
1821 atomic_read(&old_dentry->d_inode->i_count)));
1824 yaffs_GrossUnlock(dev);
1827 update_dir_time(dir);
1834 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1835 const char *symname)
1839 uid_t uid = YCRED(current)->fsuid;
1840 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1842 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1844 dev = yaffs_InodeToObject(dir)->myDev;
1845 yaffs_GrossLock(dev);
1846 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1847 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1848 yaffs_GrossUnlock(dev);
1851 struct inode *inode;
1853 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1854 d_instantiate(dentry, inode);
1855 update_dir_time(dir);
1856 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1859 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1865 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1866 static int yaffs_sync_object(struct file *file, int datasync)
1868 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1875 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1876 struct dentry *dentry = file->f_path.dentry;
1879 obj = yaffs_DentryToObject(dentry);
1883 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1884 (TSTR("yaffs_sync_object\n")));
1885 yaffs_GrossLock(dev);
1886 yaffs_FlushFile(obj, 1, datasync);
1887 yaffs_GrossUnlock(dev);
1892 * The VFS layer already does all the dentry stuff for rename.
1894 * NB: POSIX says you can rename an object over an old object of the same name
1896 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1897 struct inode *new_dir, struct dentry *new_dentry)
1900 int retVal = YAFFS_FAIL;
1901 yaffs_Object *target;
1903 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1904 dev = yaffs_InodeToObject(old_dir)->myDev;
1906 yaffs_GrossLock(dev);
1908 /* Check if the target is an existing directory that is not empty. */
1909 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1910 new_dentry->d_name.name);
1914 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1915 !ylist_empty(&target->variant.directoryVariant.children)) {
1917 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1919 retVal = YAFFS_FAIL;
1921 /* Now does unlinking internally using shadowing mechanism */
1922 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_RenameObject\n")));
1924 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1925 old_dentry->d_name.name,
1926 yaffs_InodeToObject(new_dir),
1927 new_dentry->d_name.name);
1929 yaffs_GrossUnlock(dev);
1931 if (retVal == YAFFS_OK) {
1933 new_dentry->d_inode->i_nlink--;
1934 mark_inode_dirty(new_dentry->d_inode);
1937 update_dir_time(old_dir);
1938 if(old_dir != new_dir)
1939 update_dir_time(new_dir);
1946 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1948 struct inode *inode = dentry->d_inode;
1953 (TSTR("yaffs_setattr of object %d\n"),
1954 yaffs_InodeToObject(inode)->objectId));
1956 /* Fail if a requested resize >= 2GB */
1957 if (attr->ia_valid & ATTR_SIZE &&
1958 (attr->ia_size >> 31))
1962 error = inode_change_ok(inode, attr);
1966 error = yaffs_vfs_setattr(inode, attr);
1967 T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
1968 if (attr->ia_valid & ATTR_SIZE)
1969 truncate_inode_pages(&inode->i_data,attr->ia_size);
1971 dev = yaffs_InodeToObject(inode)->myDev;
1972 if (attr->ia_valid & ATTR_SIZE){
1973 T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
1974 (int)(attr->ia_size),(int)(attr->ia_size)));
1976 yaffs_GrossLock(dev);
1977 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1978 if(result == YAFFS_OK) {
1983 yaffs_GrossUnlock(dev);
1988 (TSTR("yaffs_setattr done returning %d\n"),error));
1993 #ifdef CONFIG_YAFFS_XATTR
1994 int yaffs_setxattr(struct dentry *dentry, const char *name,
1995 const void *value, size_t size, int flags)
1997 struct inode *inode = dentry->d_inode;
2000 yaffs_Object *obj = yaffs_InodeToObject(inode);
2003 (TSTR("yaffs_setxattr of object %d\n"),
2010 yaffs_GrossLock(dev);
2011 result = yaffs_SetXAttribute(obj, name, value, size, flags);
2012 if(result == YAFFS_OK)
2016 yaffs_GrossUnlock(dev);
2020 (TSTR("yaffs_setxattr done returning %d\n"),error));
2026 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
2029 struct inode *inode = dentry->d_inode;
2032 yaffs_Object *obj = yaffs_InodeToObject(inode);
2035 (TSTR("yaffs_getxattr of object %d\n"),
2041 yaffs_GrossLock(dev);
2042 error = yaffs_GetXAttribute(obj, name, buff, size);
2043 yaffs_GrossUnlock(dev);
2047 (TSTR("yaffs_getxattr done returning %d\n"),error));
2052 int yaffs_removexattr(struct dentry *dentry, const char *name)
2054 struct inode *inode = dentry->d_inode;
2057 yaffs_Object *obj = yaffs_InodeToObject(inode);
2060 (TSTR("yaffs_removexattr of object %d\n"),
2067 yaffs_GrossLock(dev);
2068 result = yaffs_RemoveXAttribute(obj, name);
2069 if(result == YAFFS_OK)
2073 yaffs_GrossUnlock(dev);
2077 (TSTR("yaffs_removexattr done returning %d\n"),error));
2082 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
2084 struct inode *inode = dentry->d_inode;
2087 yaffs_Object *obj = yaffs_InodeToObject(inode);
2090 (TSTR("yaffs_listxattr of object %d\n"),
2096 yaffs_GrossLock(dev);
2097 error = yaffs_ListXAttributes(obj, buff, size);
2098 yaffs_GrossUnlock(dev);
2102 (TSTR("yaffs_listxattr done returning %d\n"),error));
2110 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2111 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2113 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2114 struct super_block *sb = dentry->d_sb;
2115 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2116 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2118 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2120 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2122 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2125 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
2127 yaffs_GrossLock(dev);
2129 buf->f_type = YAFFS_MAGIC;
2130 buf->f_bsize = sb->s_blocksize;
2131 buf->f_namelen = 255;
2133 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
2134 /* Do this if chunk size is not a power of 2 */
2136 uint64_t bytesInDev;
2139 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
2140 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
2142 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
2143 buf->f_blocks = bytesInDev;
2145 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
2146 ((uint64_t)(dev->nDataBytesPerChunk));
2148 do_div(bytesFree, sb->s_blocksize);
2150 buf->f_bfree = bytesFree;
2152 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2155 (dev->param.endBlock - dev->param.startBlock + 1) *
2156 dev->param.nChunksPerBlock /
2157 (sb->s_blocksize / dev->nDataBytesPerChunk);
2159 yaffs_GetNumberOfFreeChunks(dev) /
2160 (sb->s_blocksize / dev->nDataBytesPerChunk);
2163 (dev->param.endBlock - dev->param.startBlock + 1) *
2164 dev->param.nChunksPerBlock *
2165 (dev->nDataBytesPerChunk / sb->s_blocksize);
2168 yaffs_GetNumberOfFreeChunks(dev) *
2169 (dev->nDataBytesPerChunk / sb->s_blocksize);
2174 buf->f_bavail = buf->f_bfree;
2176 yaffs_GrossUnlock(dev);
2182 static void yaffs_FlushInodes(struct super_block *sb)
2187 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
2188 obj = yaffs_InodeToObject(iptr);
2190 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
2192 yaffs_FlushFile(obj,1,0);
2198 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
2200 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2204 yaffs_FlushInodes(sb);
2205 yaffs_UpdateDirtyDirectories(dev);
2206 yaffs_FlushEntireDeviceCache(dev);
2208 yaffs_CheckpointSave(dev);
2212 static unsigned yaffs_bg_gc_urgency(yaffs_Device *dev)
2214 unsigned erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2215 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2216 unsigned scatteredFree = 0; /* Free chunks not in an erased block */
2218 if(erasedChunks < dev->nFreeChunks)
2219 scatteredFree = (dev->nFreeChunks - erasedChunks);
2221 if(!context->bgRunning)
2223 else if(scatteredFree < (dev->param.nChunksPerBlock * 2))
2225 else if(erasedChunks > dev->nFreeChunks/2)
2227 else if(erasedChunks > dev->nFreeChunks/4)
2233 static int yaffs_do_sync_fs(struct super_block *sb,
2234 int request_checkpoint)
2237 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2238 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2239 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2242 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2243 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
2245 sb->s_dirt ? "dirty" : "clean",
2246 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2247 oneshot_checkpoint ? " one-shot" : "" ));
2249 yaffs_GrossLock(dev);
2250 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2251 oneshot_checkpoint) &&
2252 !dev->isCheckpointed;
2254 if (sb->s_dirt || do_checkpoint) {
2255 yaffs_FlushSuperBlock(sb, !dev->isCheckpointed && do_checkpoint);
2257 if(oneshot_checkpoint)
2258 yaffs_auto_checkpoint &= ~4;
2260 yaffs_GrossUnlock(dev);
2266 * yaffs background thread functions .
2267 * yaffs_BackgroundThread() the thread function
2268 * yaffs_BackgroundStart() launches the background thread.
2269 * yaffs_BackgroundStop() cleans up the background thread.
2272 * The thread should only run after the yaffs is initialised
2273 * The thread should be stopped before yaffs is unmounted.
2274 * The thread should not do any writing while the fs is in read only.
2277 #ifdef YAFFS_COMPILE_BACKGROUND
2279 void yaffs_background_waker(unsigned long data)
2281 wake_up_process((struct task_struct *)data);
2284 static int yaffs_BackgroundThread(void *data)
2286 yaffs_Device *dev = (yaffs_Device *)data;
2287 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2288 unsigned long now = jiffies;
2289 unsigned long next_dir_update = now;
2290 unsigned long next_gc = now;
2291 unsigned long expires;
2292 unsigned int urgency;
2295 struct timer_list timer;
2297 T(YAFFS_TRACE_BACKGROUND,
2298 (TSTR("yaffs_background starting for dev %p\n"),
2303 while(context->bgRunning){
2304 T(YAFFS_TRACE_BACKGROUND,
2305 (TSTR("yaffs_background\n")));
2307 if(kthread_should_stop())
2313 yaffs_GrossLock(dev);
2317 if(time_after(now, next_dir_update)){
2318 yaffs_UpdateDirtyDirectories(dev);
2319 next_dir_update = now + HZ;
2322 if(time_after(now,next_gc)){
2323 if(!dev->isCheckpointed){
2324 urgency = yaffs_bg_gc_urgency(dev);
2325 gcResult = yaffs_BackgroundGarbageCollect(dev, urgency);
2327 next_gc = now + HZ/20+1;
2328 else if(urgency > 0)
2329 next_gc = now + HZ/10+1;
2331 next_gc = now + HZ * 2;
2333 * gc not running so set to next_dir_update
2334 * to cut down on wake ups
2336 next_gc = next_dir_update;
2338 yaffs_GrossUnlock(dev);
2340 expires = next_dir_update;
2341 if (time_before(next_gc,expires))
2343 if(time_before(expires,now))
2346 init_timer_on_stack(&timer);
2347 timer.expires = expires+1;
2348 timer.data = (unsigned long) current;
2349 timer.function = yaffs_background_waker;
2351 set_current_state(TASK_INTERRUPTIBLE);
2354 del_timer_sync(&timer);
2363 static int yaffs_BackgroundStart(yaffs_Device *dev)
2366 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2371 context->bgRunning = 1;
2373 context->bgThread = kthread_run(yaffs_BackgroundThread,
2374 (void *)dev,"yaffs-bg-%d",context->mount_id);
2376 if(IS_ERR(context->bgThread)){
2377 retval = PTR_ERR(context->bgThread);
2378 context->bgThread = NULL;
2379 context->bgRunning = 0;
2384 static void yaffs_BackgroundStop(yaffs_Device *dev)
2386 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToLC(dev);
2388 ctxt->bgRunning = 0;
2390 if( ctxt->bgThread){
2391 kthread_stop(ctxt->bgThread);
2392 ctxt->bgThread = NULL;
2396 static int yaffs_BackgroundThread(void *data)
2401 static int yaffs_BackgroundStart(yaffs_Device *dev)
2406 static void yaffs_BackgroundStop(yaffs_Device *dev)
2412 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2413 static void yaffs_write_super(struct super_block *sb)
2415 static int yaffs_write_super(struct super_block *sb)
2418 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2420 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2421 (TSTR("yaffs_write_super%s\n"),
2422 request_checkpoint ? " checkpt" : ""));
2424 yaffs_do_sync_fs(sb, request_checkpoint);
2426 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2432 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2433 static int yaffs_sync_fs(struct super_block *sb, int wait)
2435 static int yaffs_sync_fs(struct super_block *sb)
2438 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2440 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2441 (TSTR("yaffs_sync_fs%s\n"),
2442 request_checkpoint ? " checkpt" : ""));
2444 yaffs_do_sync_fs(sb, request_checkpoint);
2449 #ifdef YAFFS_USE_OWN_IGET
2451 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2453 struct inode *inode;
2455 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2458 (TSTR("yaffs_iget for %lu\n"), ino));
2460 inode = iget_locked(sb, ino);
2462 return ERR_PTR(-ENOMEM);
2463 if (!(inode->i_state & I_NEW))
2466 /* NB This is called as a side effect of other functions, but
2467 * we had to release the lock to prevent deadlocks, so
2468 * need to lock again.
2471 yaffs_GrossLock(dev);
2473 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2475 yaffs_FillInodeFromObject(inode, obj);
2477 yaffs_GrossUnlock(dev);
2479 unlock_new_inode(inode);
2485 static void yaffs_read_inode(struct inode *inode)
2487 /* NB This is called as a side effect of other functions, but
2488 * we had to release the lock to prevent deadlocks, so
2489 * need to lock again.
2493 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2496 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2498 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2499 yaffs_GrossLock(dev);
2501 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2503 yaffs_FillInodeFromObject(inode, obj);
2505 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2506 yaffs_GrossUnlock(dev);
2511 static YLIST_HEAD(yaffs_context_list);
2512 struct semaphore yaffs_context_lock;
2514 static void yaffs_put_super(struct super_block *sb)
2516 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2518 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2520 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2521 (TSTR("Shutting down yaffs background thread\n")));
2522 yaffs_BackgroundStop(dev);
2523 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2524 (TSTR("yaffs background thread shut down\n")));
2526 yaffs_GrossLock(dev);
2528 yaffs_FlushSuperBlock(sb,1);
2530 if (yaffs_DeviceToLC(dev)->putSuperFunc)
2531 yaffs_DeviceToLC(dev)->putSuperFunc(sb);
2534 yaffs_Deinitialise(dev);
2536 yaffs_GrossUnlock(dev);
2538 down(&yaffs_context_lock);
2539 ylist_del_init(&(yaffs_DeviceToLC(dev)->contextList));
2540 up(&yaffs_context_lock);
2542 if (yaffs_DeviceToLC(dev)->spareBuffer) {
2543 YFREE(yaffs_DeviceToLC(dev)->spareBuffer);
2544 yaffs_DeviceToLC(dev)->spareBuffer = NULL;
2551 static void yaffs_MTDPutSuper(struct super_block *sb)
2553 struct mtd_info *mtd = yaffs_DeviceToMtd(yaffs_SuperToDevice(sb));
2558 put_mtd_device(mtd);
2562 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2564 struct super_block *sb = yaffs_DeviceToLC(dev)->superBlock;
2566 T(YAFFS_TRACE_OS, (TSTR("yaffs_MarkSuperBlockDirty() sb = %p\n"), sb));
2573 int skip_checkpoint_read;
2574 int skip_checkpoint_write;
2577 int tags_ecc_overridden;
2578 int lazy_loading_enabled;
2579 int lazy_loading_overridden;
2580 int empty_lost_and_found;
2581 int empty_lost_and_found_overridden;
2584 #define MAX_OPT_LEN 30
2585 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2587 char cur_opt[MAX_OPT_LEN + 1];
2591 /* Parse through the options which is a comma seperated list */
2593 while (options_str && *options_str && !error) {
2594 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2597 while(*options_str == ',')
2600 while (*options_str && *options_str != ',') {
2601 if (p < MAX_OPT_LEN) {
2602 cur_opt[p] = *options_str;
2608 if (!strcmp(cur_opt, "inband-tags"))
2609 options->inband_tags = 1;
2610 else if (!strcmp(cur_opt, "tags-ecc-off")){
2611 options->tags_ecc_on = 0;
2612 options->tags_ecc_overridden=1;
2613 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2614 options->tags_ecc_on = 1;
2615 options->tags_ecc_overridden = 1;
2616 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2617 options->lazy_loading_enabled = 0;
2618 options->lazy_loading_overridden=1;
2619 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2620 options->lazy_loading_enabled = 1;
2621 options->lazy_loading_overridden = 1;
2622 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2623 options->empty_lost_and_found = 0;
2624 options->empty_lost_and_found_overridden=1;
2625 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2626 options->empty_lost_and_found = 1;
2627 options->empty_lost_and_found_overridden=1;
2628 } else if (!strcmp(cur_opt, "no-cache"))
2629 options->no_cache = 1;
2630 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2631 options->skip_checkpoint_read = 1;
2632 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2633 options->skip_checkpoint_write = 1;
2634 else if (!strcmp(cur_opt, "no-checkpoint")) {
2635 options->skip_checkpoint_read = 1;
2636 options->skip_checkpoint_write = 1;
2638 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2647 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2648 struct super_block *sb,
2649 void *data, int silent)
2652 struct inode *inode = NULL;
2653 struct dentry *root;
2654 yaffs_Device *dev = 0;
2655 char devname_buf[BDEVNAME_SIZE + 1];
2656 struct mtd_info *mtd;
2658 char *data_str = (char *)data;
2659 struct yaffs_LinuxContext *context = NULL;
2660 yaffs_DeviceParam *param;
2664 yaffs_options options;
2668 struct yaffs_LinuxContext *context_iterator;
2669 struct ylist_head *l;
2671 sb->s_magic = YAFFS_MAGIC;
2672 sb->s_op = &yaffs_super_ops;
2673 sb->s_flags |= MS_NOATIME;
2675 readOnly =((sb->s_flags & MS_RDONLY) != 0);
2678 #ifdef YAFFS_COMPILE_EXPORTFS
2679 sb->s_export_op = &yaffs_export_ops;
2683 printk(KERN_INFO "yaffs: sb is NULL\n");
2684 else if (!sb->s_dev)
2685 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2686 else if (!yaffs_devname(sb, devname_buf))
2687 printk(KERN_INFO "yaffs: devname is NULL\n");
2689 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2691 yaffs_devname(sb, devname_buf),
2692 readOnly ? "ro" : "rw");
2697 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2699 memset(&options, 0, sizeof(options));
2701 if (yaffs_parse_options(&options, data_str)) {
2702 /* Option parsing failed */
2707 sb->s_blocksize = PAGE_CACHE_SIZE;
2708 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2711 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffsVersion));
2713 (TSTR("yaffs_read_super: block size %d\n"),
2714 (int)(sb->s_blocksize)));
2716 T(YAFFS_TRACE_ALWAYS,
2717 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2718 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2719 yaffs_devname(sb, devname_buf)));
2721 /* Check it's an mtd device..... */
2722 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2723 return NULL; /* This isn't an mtd device */
2725 /* Get the device */
2726 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2728 T(YAFFS_TRACE_ALWAYS,
2729 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2733 /* Check it's NAND */
2734 if (mtd->type != MTD_NANDFLASH) {
2735 T(YAFFS_TRACE_ALWAYS,
2736 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2741 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2742 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2743 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2744 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2745 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2746 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2747 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2748 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2749 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2750 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2751 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2752 T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2754 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2757 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2759 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2760 T(YAFFS_TRACE_ALWAYS,
2761 (TSTR("yaffs: auto selecting yaffs2\n")));
2765 /* Added NCB 26/5/2006 for completeness */
2766 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2767 T(YAFFS_TRACE_ALWAYS,
2768 (TSTR("yaffs: auto selecting yaffs1\n")));
2774 if (yaffsVersion == 2) {
2775 /* Check for version 2 style functions */
2777 !mtd->block_isbad ||
2778 !mtd->block_markbad ||
2781 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2782 !mtd->read_oob || !mtd->write_oob) {
2785 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2787 T(YAFFS_TRACE_ALWAYS,
2788 (TSTR("yaffs: MTD device does not support required "
2793 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2794 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2795 !options.inband_tags) {
2796 T(YAFFS_TRACE_ALWAYS,
2797 (TSTR("yaffs: MTD device does not have the "
2798 "right page sizes\n")));
2802 /* Check for V1 style functions */
2806 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2807 !mtd->read_oob || !mtd->write_oob) {
2810 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2812 T(YAFFS_TRACE_ALWAYS,
2813 (TSTR("yaffs: MTD device does not support required "
2818 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2819 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2820 T(YAFFS_TRACE_ALWAYS,
2821 (TSTR("yaffs: MTD device does not support have the "
2822 "right page sizes\n")));
2827 /* OK, so if we got here, we have an MTD that's NAND and looks
2828 * like it has the right capabilities
2829 * Set the yaffs_Device up for mtd
2832 if (!readOnly && !(mtd->flags & MTD_WRITEABLE)){
2834 printk(KERN_INFO "yaffs: mtd is read only, setting superblock read only");
2835 sb->s_flags |= MS_RDONLY;
2838 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2839 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2841 if(!dev || !context ){
2851 /* Deep shit could not allocate device structure */
2852 T(YAFFS_TRACE_ALWAYS,
2853 (TSTR("yaffs_read_super: Failed trying to allocate "
2854 "yaffs_Device. \n")));
2857 memset(dev, 0, sizeof(yaffs_Device));
2858 param = &(dev->param);
2860 memset(context,0,sizeof(struct yaffs_LinuxContext));
2861 dev->osContext = context;
2862 YINIT_LIST_HEAD(&(context->contextList));
2864 context->superBlock = sb;
2866 dev->readOnly = readOnly;
2868 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2869 sb->s_fs_info = dev;
2871 sb->u.generic_sbp = dev;
2874 dev->driverContext = mtd;
2875 param->name = mtd->name;
2877 /* Set up the memory size parameters.... */
2879 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2881 param->startBlock = 0;
2882 param->endBlock = nBlocks - 1;
2883 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2884 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2885 param->nReservedBlocks = 5;
2886 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2887 param->inbandTags = options.inband_tags;
2889 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2890 param->disableLazyLoad = 1;
2892 #ifdef CONFIG_YAFFS_XATTR
2893 param->enableXattr = 1;
2895 if(options.lazy_loading_overridden)
2896 param->disableLazyLoad = !options.lazy_loading_enabled;
2898 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2899 param->noTagsECC = 1;
2902 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2904 param->deferDirectoryUpdate = 1;
2907 if(options.tags_ecc_overridden)
2908 param->noTagsECC = !options.tags_ecc_on;
2910 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2911 param->emptyLostAndFound = 1;
2914 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2915 param->refreshPeriod = 0;
2917 param->refreshPeriod = 500;
2920 if(options.empty_lost_and_found_overridden)
2921 param->emptyLostAndFound = options.empty_lost_and_found;
2923 /* ... and the functions. */
2924 if (yaffsVersion == 2) {
2925 param->writeChunkWithTagsToNAND =
2926 nandmtd2_WriteChunkWithTagsToNAND;
2927 param->readChunkWithTagsFromNAND =
2928 nandmtd2_ReadChunkWithTagsFromNAND;
2929 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2930 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2931 yaffs_DeviceToLC(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2932 param->isYaffs2 = 1;
2933 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2934 param->totalBytesPerChunk = mtd->writesize;
2935 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2937 param->totalBytesPerChunk = mtd->oobblock;
2938 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2940 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2942 param->startBlock = 0;
2943 param->endBlock = nBlocks - 1;
2945 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2946 /* use the MTD interface in yaffs_mtdif1.c */
2947 param->writeChunkWithTagsToNAND =
2948 nandmtd1_WriteChunkWithTagsToNAND;
2949 param->readChunkWithTagsFromNAND =
2950 nandmtd1_ReadChunkWithTagsFromNAND;
2951 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2952 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2954 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2955 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2957 param->isYaffs2 = 0;
2959 /* ... and common functions */
2960 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2961 param->initialiseNAND = nandmtd_InitialiseNAND;
2963 yaffs_DeviceToLC(dev)->putSuperFunc = yaffs_MTDPutSuper;
2965 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2966 param->gcControl = yaffs_gc_control_callback;
2968 yaffs_DeviceToLC(dev)->superBlock= sb;
2971 #ifndef CONFIG_YAFFS_DOES_ECC
2972 param->useNANDECC = 1;
2975 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2976 param->wideTnodesDisabled = 1;
2979 param->skipCheckpointRead = options.skip_checkpoint_read;
2980 param->skipCheckpointWrite = options.skip_checkpoint_write;
2982 down(&yaffs_context_lock);
2983 /* Get a mount id */
2985 for(mount_id=0; ! found; mount_id++){
2987 ylist_for_each(l,&yaffs_context_list){
2988 context_iterator = ylist_entry(l,struct yaffs_LinuxContext,contextList);
2989 if(context_iterator->mount_id == mount_id)
2993 context->mount_id = mount_id;
2995 ylist_add_tail(&(yaffs_DeviceToLC(dev)->contextList), &yaffs_context_list);
2996 up(&yaffs_context_lock);
2998 /* Directory search handling...*/
2999 YINIT_LIST_HEAD(&(yaffs_DeviceToLC(dev)->searchContexts));
3000 param->removeObjectCallback = yaffs_RemoveObjectCallback;
3002 init_MUTEX(&(yaffs_DeviceToLC(dev)->grossLock));
3004 yaffs_GrossLock(dev);
3006 err = yaffs_GutsInitialise(dev);
3009 (TSTR("yaffs_read_super: guts initialised %s\n"),
3010 (err == YAFFS_OK) ? "OK" : "FAILED"));
3013 yaffs_BackgroundStart(dev);
3015 if(!context->bgThread)
3016 param->deferDirectoryUpdate = 0;
3019 /* Release lock before yaffs_get_inode() */
3020 yaffs_GrossUnlock(dev);
3022 /* Create root inode */
3023 if (err == YAFFS_OK)
3024 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3030 inode->i_op = &yaffs_dir_inode_operations;
3031 inode->i_fop = &yaffs_dir_operations;
3033 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
3035 root = d_alloc_root(inode);
3037 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
3044 sb->s_dirt = !dev->isCheckpointed;
3045 T(YAFFS_TRACE_ALWAYS,
3046 (TSTR("yaffs_read_super: isCheckpointed %d\n"),
3047 dev->isCheckpointed));
3049 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
3054 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3055 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3058 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3061 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3062 static int yaffs_read_super(struct file_system_type *fs,
3063 int flags, const char *dev_name,
3064 void *data, struct vfsmount *mnt)
3067 return get_sb_bdev(fs, flags, dev_name, data,
3068 yaffs_internal_read_super_mtd, mnt);
3071 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3072 int flags, const char *dev_name,
3076 return get_sb_bdev(fs, flags, dev_name, data,
3077 yaffs_internal_read_super_mtd);
3081 static struct file_system_type yaffs_fs_type = {
3082 .owner = THIS_MODULE,
3084 .get_sb = yaffs_read_super,
3085 .kill_sb = kill_block_super,
3086 .fs_flags = FS_REQUIRES_DEV,
3089 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3092 return yaffs_internal_read_super(1, sb, data, silent);
3095 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3100 #ifdef CONFIG_YAFFS_YAFFS2
3102 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3103 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3106 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3109 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3110 static int yaffs2_read_super(struct file_system_type *fs,
3111 int flags, const char *dev_name, void *data,
3112 struct vfsmount *mnt)
3114 return get_sb_bdev(fs, flags, dev_name, data,
3115 yaffs2_internal_read_super_mtd, mnt);
3118 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3119 int flags, const char *dev_name,
3123 return get_sb_bdev(fs, flags, dev_name, data,
3124 yaffs2_internal_read_super_mtd);
3128 static struct file_system_type yaffs2_fs_type = {
3129 .owner = THIS_MODULE,
3131 .get_sb = yaffs2_read_super,
3132 .kill_sb = kill_block_super,
3133 .fs_flags = FS_REQUIRES_DEV,
3136 static struct super_block *yaffs2_read_super(struct super_block *sb,
3137 void *data, int silent)
3139 return yaffs_internal_read_super(2, sb, data, silent);
3142 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3146 #endif /* CONFIG_YAFFS_YAFFS2 */
3148 static struct proc_dir_entry *my_proc_entry;
3149 static struct proc_dir_entry *debug_proc_entry;
3151 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
3153 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
3154 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
3155 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
3156 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
3157 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
3158 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
3159 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
3160 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
3161 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
3162 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
3163 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
3164 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
3166 buf += sprintf(buf, "\n");
3172 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
3174 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3175 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3176 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3177 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3178 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3179 buf += sprintf(buf, "\n");
3180 buf += sprintf(buf, "nTnodes............ %d\n", dev->nTnodes);
3181 buf += sprintf(buf, "nObjects........... %d\n", dev->nObjects);
3182 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3183 buf += sprintf(buf, "\n");
3184 buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
3185 buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
3186 buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
3187 buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
3188 buf += sprintf(buf, "allGCs............. %u\n", dev->allGCs);
3189 buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGCs);
3190 buf += sprintf(buf, "oldestDirtyGCs..... %u\n", dev->oldestDirtyGCs);
3191 buf += sprintf(buf, "backgroundGCs...... %u\n", dev->backgroundGCs);
3192 buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
3193 buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
3194 buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
3195 buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
3196 buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
3197 buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
3198 buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
3199 buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
3200 buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
3201 buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
3203 sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
3208 static int yaffs_proc_read(char *page,
3210 off_t offset, int count, int *eof, void *data)
3212 struct ylist_head *item;
3217 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3218 * We use 'offset' (*ppos) to indicate where we are in devList.
3219 * This also assumes the user has posted a read buffer large
3220 * enough to hold the complete output; but that's life in /proc.
3225 /* Print header first */
3227 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__"\n");
3229 buf += sprintf(buf,"\n");
3233 down(&yaffs_context_lock);
3235 /* Locate and print the Nth entry. Order N-squared but N is small. */
3236 ylist_for_each(item, &yaffs_context_list) {
3237 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3238 yaffs_Device *dev = dc->dev;
3240 if (n < (step & ~1)) {
3245 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
3246 buf = yaffs_dump_dev_part0(buf, dev);
3248 buf = yaffs_dump_dev_part1(buf, dev);
3252 up(&yaffs_context_lock);
3255 return buf - page < count ? buf - page : count;
3258 static int yaffs_stats_proc_read(char *page,
3260 off_t offset, int count, int *eof, void *data)
3262 struct ylist_head *item;
3266 down(&yaffs_context_lock);
3268 /* Locate and print the Nth entry. Order N-squared but N is small. */
3269 ylist_for_each(item, &yaffs_context_list) {
3270 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3271 yaffs_Device *dev = dc->dev;
3275 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3277 buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
3278 n, dev->nFreeChunks, erasedChunks,
3279 dev->backgroundGCs, dev->oldestDirtyGCs,
3280 dev->nObjects, dev->nTnodes);
3282 up(&yaffs_context_lock);
3285 return buf - page < count ? buf - page : count;
3289 * Set the verbosity of the warnings and error messages.
3291 * Note that the names can only be a..z or _ with the current code.
3296 unsigned mask_bitfield;
3298 {"allocate", YAFFS_TRACE_ALLOCATE},
3299 {"always", YAFFS_TRACE_ALWAYS},
3300 {"background", YAFFS_TRACE_BACKGROUND},
3301 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3302 {"buffers", YAFFS_TRACE_BUFFERS},
3303 {"bug", YAFFS_TRACE_BUG},
3304 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3305 {"deletion", YAFFS_TRACE_DELETION},
3306 {"erase", YAFFS_TRACE_ERASE},
3307 {"error", YAFFS_TRACE_ERROR},
3308 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3309 {"gc", YAFFS_TRACE_GC},
3310 {"lock", YAFFS_TRACE_LOCK},
3311 {"mtd", YAFFS_TRACE_MTD},
3312 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3313 {"os", YAFFS_TRACE_OS},
3314 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3315 {"scan", YAFFS_TRACE_SCAN},
3316 {"tracing", YAFFS_TRACE_TRACING},
3317 {"sync", YAFFS_TRACE_SYNC},
3318 {"write", YAFFS_TRACE_WRITE},
3320 {"verify", YAFFS_TRACE_VERIFY},
3321 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3322 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3323 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3325 {"all", 0xffffffff},
3330 #define MAX_MASK_NAME_LENGTH 40
3331 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3332 unsigned long count, void *data)
3334 unsigned rg = 0, mask_bitfield;
3338 char substring[MAX_MASK_NAME_LENGTH + 1];
3344 rg = yaffs_traceMask;
3346 while (!done && (pos < count)) {
3348 while ((pos < count) && isspace(buf[pos]))
3365 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3367 if (end > buf + pos) {
3368 mask_name = "numeral";
3369 len = end - (buf + pos);
3373 for (x = buf + pos, i = 0;
3374 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3375 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3377 substring[i] = '\0';
3379 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3380 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3381 mask_name = mask_flags[i].mask_name;
3382 mask_bitfield = mask_flags[i].mask_bitfield;
3389 if (mask_name != NULL) {
3393 rg &= ~mask_bitfield;
3396 rg |= mask_bitfield;
3402 rg |= mask_bitfield;
3408 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3410 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
3412 if (rg & YAFFS_TRACE_ALWAYS) {
3413 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3415 flag = ((rg & mask_flags[i].mask_bitfield) ==
3416 mask_flags[i].mask_bitfield) ? '+' : '-';
3417 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3425 static int yaffs_proc_write(struct file *file, const char *buf,
3426 unsigned long count, void *data)
3428 return yaffs_proc_write_trace_options(file, buf, count, data);
3431 /* Stuff to handle installation of file systems */
3432 struct file_system_to_install {
3433 struct file_system_type *fst;
3437 static struct file_system_to_install fs_to_install[] = {
3438 {&yaffs_fs_type, 0},
3439 {&yaffs2_fs_type, 0},
3443 static int __init init_yaffs_fs(void)
3446 struct file_system_to_install *fsinst;
3448 T(YAFFS_TRACE_ALWAYS,
3449 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3451 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3452 T(YAFFS_TRACE_ALWAYS,
3453 (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3459 init_MUTEX(&yaffs_context_lock);
3461 /* Install the proc_fs entries */
3462 my_proc_entry = create_proc_entry("yaffs",
3466 if (my_proc_entry) {
3467 my_proc_entry->write_proc = yaffs_proc_write;
3468 my_proc_entry->read_proc = yaffs_proc_read;
3469 my_proc_entry->data = NULL;
3473 debug_proc_entry = create_proc_entry("yaffs_stats",
3477 if (debug_proc_entry) {
3478 debug_proc_entry->write_proc = NULL;
3479 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3480 debug_proc_entry->data = NULL;
3484 /* Now add the file system entries */
3486 fsinst = fs_to_install;
3488 while (fsinst->fst && !error) {
3489 error = register_filesystem(fsinst->fst);
3491 fsinst->installed = 1;
3495 /* Any errors? uninstall */
3497 fsinst = fs_to_install;
3499 while (fsinst->fst) {
3500 if (fsinst->installed) {
3501 unregister_filesystem(fsinst->fst);
3502 fsinst->installed = 0;
3511 static void __exit exit_yaffs_fs(void)
3514 struct file_system_to_install *fsinst;
3516 T(YAFFS_TRACE_ALWAYS,
3517 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3519 remove_proc_entry("yaffs", YPROC_ROOT);
3520 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3522 fsinst = fs_to_install;
3524 while (fsinst->fst) {
3525 if (fsinst->installed) {
3526 unregister_filesystem(fsinst->fst);
3527 fsinst->installed = 0;
3533 module_init(init_yaffs_fs)
3534 module_exit(exit_yaffs_fs)
3536 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3537 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3538 MODULE_LICENSE("GPL");