2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2009 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 const char *yaffs_fs_c_version =
35 "$Id: yaffs_fs.c,v 1.102 2010-03-15 23:10:33 charles Exp $";
36 extern const char *yaffs_guts_c_version;
38 #include <linux/version.h>
40 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
41 #define YAFFS_COMPILE_BACKGROUND
44 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28))
45 #define YAFFS_COMPILE_EXPORTFS
49 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
50 #include <linux/config.h>
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/slab.h>
55 #include <linux/init.h>
57 #include <linux/proc_fs.h>
58 #include <linux/smp_lock.h>
59 #include <linux/pagemap.h>
60 #include <linux/mtd/mtd.h>
61 #include <linux/interrupt.h>
62 #include <linux/string.h>
63 #include <linux/ctype.h>
65 #ifdef YAFFS_COMPILE_EXPORTFS
66 #include <linux/exportfs.h>
69 #ifdef YAFFS_COMPILE_BACKGROUND
70 #include <linux/kthread.h>
71 #include <linux/delay.h>
72 #include <linux/freezer.h>
76 #include "asm/div64.h"
81 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
83 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
84 #include <linux/statfs.h>
85 #define UnlockPage(p) unlock_page(p)
86 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
88 /* FIXME: use sb->s_id instead ? */
89 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
93 #include <linux/locks.h>
94 #define BDEVNAME_SIZE 0
95 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
97 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
98 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
104 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
105 #define YPROC_ROOT (&proc_root)
107 #define YPROC_ROOT NULL
110 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
111 #define WRITE_SIZE_STR "writesize"
112 #define WRITE_SIZE(mtd) ((mtd)->writesize)
114 #define WRITE_SIZE_STR "oobblock"
115 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
118 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
119 #define YAFFS_USE_WRITE_BEGIN_END 1
121 #define YAFFS_USE_WRITE_BEGIN_END 0
124 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
125 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
127 uint64_t result = partition_size;
128 do_div(result, block_size);
129 return (uint32_t)result;
132 #define YCALCBLOCKS(s, b) ((s)/(b))
135 #include <linux/uaccess.h>
137 #include "yportenv.h"
138 #include "yaffs_trace.h"
139 #include "yaffs_guts.h"
141 #include "yaffs_linux.h"
143 #include <linux/mtd/mtd.h>
144 #include "yaffs_mtdif.h"
145 #include "yaffs_mtdif1.h"
146 #include "yaffs_mtdif2.h"
148 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
149 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
150 unsigned int yaffs_auto_checkpoint = 1;
151 unsigned int yaffs_gc_control = 1;
153 /* Module Parameters */
154 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
155 module_param(yaffs_traceMask, uint, 0644);
156 module_param(yaffs_wr_attempts, uint, 0644);
157 module_param(yaffs_auto_checkpoint, uint, 0644);
158 module_param(yaffs_gc_control, uint, 0644);
160 MODULE_PARM(yaffs_traceMask, "i");
161 MODULE_PARM(yaffs_wr_attempts, "i");
162 MODULE_PARM(yaffs_auto_checkpoint, "i");
163 MODULE_PARM(yaffs_gc_control, "i");
166 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
167 /* use iget and read_inode */
168 #define Y_IGET(sb, inum) iget((sb), (inum))
169 static void yaffs_read_inode(struct inode *inode);
172 /* Call local equivalent */
173 #define YAFFS_USE_OWN_IGET
174 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
176 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
179 /*#define T(x) printk x */
181 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
182 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
184 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
187 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
188 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
190 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
191 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
193 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
197 #define update_dir_time(dir) do {\
198 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
201 static void yaffs_put_super(struct super_block *sb);
203 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
205 static ssize_t yaffs_hold_space(struct file *f);
206 static void yaffs_release_space(struct file *f);
208 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
209 static int yaffs_file_flush(struct file *file, fl_owner_t id);
211 static int yaffs_file_flush(struct file *file);
214 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
217 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
219 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
220 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
221 struct nameidata *n);
222 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
223 struct nameidata *n);
225 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
226 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
228 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
229 struct dentry *dentry);
230 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
231 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
232 const char *symname);
233 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
235 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
236 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
239 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
242 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
243 struct inode *new_dir, struct dentry *new_dentry);
244 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
246 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
247 static int yaffs_sync_fs(struct super_block *sb, int wait);
248 static void yaffs_write_super(struct super_block *sb);
250 static int yaffs_sync_fs(struct super_block *sb);
251 static int yaffs_write_super(struct super_block *sb);
254 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
255 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
256 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
257 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
259 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
262 #ifdef YAFFS_HAS_PUT_INODE
263 static void yaffs_put_inode(struct inode *inode);
266 static void yaffs_delete_inode(struct inode *);
267 static void yaffs_clear_inode(struct inode *);
269 static int yaffs_readpage(struct file *file, struct page *page);
270 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
271 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
273 static int yaffs_writepage(struct page *page);
277 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
278 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
279 loff_t pos, unsigned len, unsigned flags,
280 struct page **pagep, void **fsdata);
281 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
282 loff_t pos, unsigned len, unsigned copied,
283 struct page *pg, void *fsdadata);
285 static int yaffs_prepare_write(struct file *f, struct page *pg,
286 unsigned offset, unsigned to);
287 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
292 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
294 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
295 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
297 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
299 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
301 static struct address_space_operations yaffs_file_address_operations = {
302 .readpage = yaffs_readpage,
303 .writepage = yaffs_writepage,
304 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
305 .write_begin = yaffs_write_begin,
306 .write_end = yaffs_write_end,
308 .prepare_write = yaffs_prepare_write,
309 .commit_write = yaffs_commit_write,
313 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
314 static const struct file_operations yaffs_file_operations = {
315 .read = do_sync_read,
316 .write = do_sync_write,
317 .aio_read = generic_file_aio_read,
318 .aio_write = generic_file_aio_write,
319 .mmap = generic_file_mmap,
320 .flush = yaffs_file_flush,
321 .fsync = yaffs_sync_object,
322 .splice_read = generic_file_splice_read,
323 .splice_write = generic_file_splice_write,
324 .llseek = generic_file_llseek,
327 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
329 static const struct file_operations yaffs_file_operations = {
330 .read = do_sync_read,
331 .write = do_sync_write,
332 .aio_read = generic_file_aio_read,
333 .aio_write = generic_file_aio_write,
334 .mmap = generic_file_mmap,
335 .flush = yaffs_file_flush,
336 .fsync = yaffs_sync_object,
337 .sendfile = generic_file_sendfile,
342 static const struct file_operations yaffs_file_operations = {
343 .read = generic_file_read,
344 .write = generic_file_write,
345 .mmap = generic_file_mmap,
346 .flush = yaffs_file_flush,
347 .fsync = yaffs_sync_object,
348 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
349 .sendfile = generic_file_sendfile,
354 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
355 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
357 void * kaddr = kmap_atomic(page, KM_USER0);
358 memset(kaddr + start, 0, end - start);
359 kunmap_atomic(kaddr, KM_USER0);
360 flush_dcache_page(page);
365 static const struct inode_operations yaffs_file_inode_operations = {
366 .setattr = yaffs_setattr,
369 static const struct inode_operations yaffs_symlink_inode_operations = {
370 .readlink = yaffs_readlink,
371 .follow_link = yaffs_follow_link,
372 .setattr = yaffs_setattr,
375 static const struct inode_operations yaffs_dir_inode_operations = {
376 .create = yaffs_create,
377 .lookup = yaffs_lookup,
379 .unlink = yaffs_unlink,
380 .symlink = yaffs_symlink,
381 .mkdir = yaffs_mkdir,
382 .rmdir = yaffs_unlink,
383 .mknod = yaffs_mknod,
384 .rename = yaffs_rename,
385 .setattr = yaffs_setattr,
388 static const struct file_operations yaffs_dir_operations = {
389 .read = generic_read_dir,
390 .readdir = yaffs_readdir,
391 .fsync = yaffs_sync_object,
392 .llseek = yaffs_dir_llseek,
395 static const struct super_operations yaffs_super_ops = {
396 .statfs = yaffs_statfs,
398 #ifndef YAFFS_USE_OWN_IGET
399 .read_inode = yaffs_read_inode,
401 #ifdef YAFFS_HAS_PUT_INODE
402 .put_inode = yaffs_put_inode,
404 .put_super = yaffs_put_super,
405 .delete_inode = yaffs_delete_inode,
406 .clear_inode = yaffs_clear_inode,
407 .sync_fs = yaffs_sync_fs,
408 .write_super = yaffs_write_super,
411 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
413 return yaffs_gc_control;
416 static void yaffs_GrossLock(yaffs_Device *dev)
418 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
419 down(&(yaffs_DeviceToContext(dev)->grossLock));
420 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
423 static void yaffs_GrossUnlock(yaffs_Device *dev)
425 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
426 up(&(yaffs_DeviceToContext(dev)->grossLock));
429 #ifdef YAFFS_COMPILE_EXPORTFS
431 static struct inode *
432 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
434 return Y_IGET(sb, ino);
437 static struct dentry *
438 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
440 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
443 static struct dentry *
444 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
446 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
449 struct dentry *yaffs2_get_parent(struct dentry *dentry)
452 struct super_block *sb = dentry->d_inode->i_sb;
453 struct dentry *parent = ERR_PTR(-ENOENT);
455 unsigned long parent_ino;
457 yaffs_Object *parent_obj;
459 d_obj = yaffs_InodeToObject(dentry->d_inode);
462 parent_obj = d_obj->parent;
464 parent_ino = yaffs_GetObjectInode(parent_obj);
465 inode = Y_IGET(sb, parent_ino);
468 parent = ERR_CAST(inode);
470 parent = d_obtain_alias(inode);
471 if (!IS_ERR(parent)) {
472 parent = ERR_PTR(-ENOMEM);
482 /* Just declare a zero structure as a NULL value implies
483 * using the default functions of expfs.
486 static struct export_operations yaffs_export_ops =
488 .fh_to_dentry = yaffs2_fh_to_dentry,
489 .fh_to_parent = yaffs2_fh_to_parent,
490 .get_parent = yaffs2_get_parent,
495 /*-----------------------------------------------------------------*/
496 /* Directory search context allows us to unlock access to yaffs during
497 * filldir without causing problems with the directory being modified.
498 * This is similar to the tried and tested mechanism used in yaffs direct.
500 * A search context iterates along a doubly linked list of siblings in the
501 * directory. If the iterating object is deleted then this would corrupt
502 * the list iteration, likely causing a crash. The search context avoids
503 * this by using the removeObjectCallback to move the search context to the
504 * next object before the object is deleted.
506 * Many readdirs (and thus seach conexts) may be alive simulateously so
507 * each yaffs_Device has a list of these.
509 * A seach context lives for the duration of a readdir.
511 * All these functions must be called while yaffs is locked.
514 struct yaffs_SearchContext {
516 yaffs_Object *dirObj;
517 yaffs_Object *nextReturn;
518 struct ylist_head others;
522 * yaffs_NewSearch() creates a new search context, initialises it and
523 * adds it to the device's search context list.
525 * Called at start of readdir.
527 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
529 yaffs_Device *dev = dir->myDev;
530 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
534 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
535 sc->nextReturn = NULL;
537 sc->nextReturn = ylist_entry(
538 dir->variant.directoryVariant.children.next,
539 yaffs_Object,siblings);
540 YINIT_LIST_HEAD(&sc->others);
541 ylist_add(&sc->others,&(yaffs_DeviceToContext(dev)->searchContexts));
547 * yaffs_EndSearch() disposes of a search context and cleans up.
549 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
552 ylist_del(&sc->others);
558 * yaffs_SearchAdvance() moves a search context to the next object.
559 * Called when the search iterates or when an object removal causes
560 * the search context to be moved to the next object.
562 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
567 if( sc->nextReturn == NULL ||
568 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
569 sc->nextReturn = NULL;
571 struct ylist_head *next = sc->nextReturn->siblings.next;
573 if( next == &sc->dirObj->variant.directoryVariant.children)
574 sc->nextReturn = NULL; /* end of list */
576 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
581 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
582 * We check open search contexts and advance any which are currently
583 * on the object being iterated.
585 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
588 struct ylist_head *i;
589 struct yaffs_SearchContext *sc;
590 struct ylist_head *search_contexts = &(yaffs_DeviceToContext(obj->myDev)->searchContexts);
593 /* Iterate through the directory search contexts.
594 * If any are currently on the object being removed, then advance
595 * the search context to the next object to prevent a hanging pointer.
597 ylist_for_each(i, search_contexts) {
599 sc = ylist_entry(i, struct yaffs_SearchContext,others);
600 if(sc->nextReturn == obj)
601 yaffs_SearchAdvance(sc);
608 /*-----------------------------------------------------------------*/
610 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
613 unsigned char *alias;
616 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
618 yaffs_GrossLock(dev);
620 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
622 yaffs_GrossUnlock(dev);
627 ret = vfs_readlink(dentry, buffer, buflen, alias);
632 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
633 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
635 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
638 unsigned char *alias;
640 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
642 yaffs_GrossLock(dev);
644 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
646 yaffs_GrossUnlock(dev);
653 ret = vfs_follow_link(nd, alias);
656 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
663 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
667 * Lookup is used to find objects in the fs
669 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
671 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
674 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
678 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
680 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
682 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
683 yaffs_GrossLock(dev);
686 ("yaffs_lookup for %d:%s\n",
687 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
689 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
690 dentry->d_name.name);
692 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
694 /* Can't hold gross lock when calling yaffs_get_inode() */
695 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
696 yaffs_GrossUnlock(dev);
700 ("yaffs_lookup found %d\n", obj->objectId));
702 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
706 ("yaffs_loookup dentry \n"));
707 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
708 * d_add even if NULL inode */
710 /*dget(dentry); // try to solve directory bug */
711 d_add(dentry, inode);
719 T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
723 /* added NCB for 2.5/6 compatability - forces add even if inode is
724 * NULL which creates dentry hash */
725 d_add(dentry, inode);
731 #ifdef YAFFS_HAS_PUT_INODE
733 /* For now put inode is just for debugging
734 * Put inode is called when the inode **structure** is put.
736 static void yaffs_put_inode(struct inode *inode)
739 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
740 atomic_read(&inode->i_count)));
745 /* clear is called to tell the fs to release any per-inode data it holds */
746 static void yaffs_clear_inode(struct inode *inode)
751 obj = yaffs_InodeToObject(inode);
754 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
755 atomic_read(&inode->i_count),
756 obj ? "object exists" : "null object"));
760 yaffs_GrossLock(dev);
762 /* Clear the association between the inode and
766 yaffs_InodeToObjectLV(inode) = NULL;
768 /* If the object freeing was deferred, then the real
770 * This should fix the inode inconsistency problem.
773 yaffs_HandleDeferedFree(obj);
775 yaffs_GrossUnlock(dev);
780 /* delete is called when the link count is zero and the inode
781 * is put (ie. nobody wants to know about it anymore, time to
783 * NB Must call clear_inode()
785 static void yaffs_delete_inode(struct inode *inode)
787 yaffs_Object *obj = yaffs_InodeToObject(inode);
791 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
792 atomic_read(&inode->i_count),
793 obj ? "object exists" : "null object"));
797 yaffs_GrossLock(dev);
798 yaffs_DeleteObject(obj);
799 yaffs_GrossUnlock(dev);
801 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
802 truncate_inode_pages(&inode->i_data, 0);
807 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
808 static int yaffs_file_flush(struct file *file, fl_owner_t id)
810 static int yaffs_file_flush(struct file *file)
813 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
815 yaffs_Device *dev = obj->myDev;
818 ("yaffs_file_flush object %d (%s)\n", obj->objectId,
819 obj->dirty ? "dirty" : "clean"));
821 yaffs_GrossLock(dev);
823 yaffs_FlushFile(obj, 1, 0);
825 yaffs_GrossUnlock(dev);
830 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
832 /* Lifted from jffs2 */
835 unsigned char *pg_buf;
840 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock at %08x, size %08x\n",
841 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
842 (unsigned)PAGE_CACHE_SIZE));
844 obj = yaffs_DentryToObject(f->f_dentry);
848 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
849 BUG_ON(!PageLocked(pg));
856 /* FIXME: Can kmap fail? */
858 yaffs_GrossLock(dev);
860 ret = yaffs_ReadDataFromFile(obj, pg_buf,
861 pg->index << PAGE_CACHE_SHIFT,
864 yaffs_GrossUnlock(dev);
870 ClearPageUptodate(pg);
877 flush_dcache_page(pg);
880 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock done\n"));
884 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
886 int ret = yaffs_readpage_nolock(f, pg);
891 static int yaffs_readpage(struct file *f, struct page *pg)
895 T(YAFFS_TRACE_OS, ("yaffs_readpage\n"));
896 ret=yaffs_readpage_unlock(f, pg);
897 T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
901 /* writepage inspired by/stolen from smbfs */
903 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
904 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
906 static int yaffs_writepage(struct page *page)
909 struct address_space *mapping = page->mapping;
911 unsigned long end_index;
920 inode = mapping->host;
923 i_size = i_size_read(inode);
925 end_index = i_size >> PAGE_CACHE_SHIFT;
927 if(page->index < end_index)
928 nBytes = PAGE_CACHE_SIZE;
930 nBytes = i_size & (PAGE_CACHE_SIZE -1);
932 if (page->index > end_index || !nBytes) {
934 ("yaffs_writepage at %08x, inode size = %08x!!!\n",
935 (unsigned)(page->index << PAGE_CACHE_SHIFT),
936 (unsigned)inode->i_size));
938 (" -> don't care!!\n"));
940 zero_user_segment(page,0,PAGE_CACHE_SIZE);
941 set_page_writeback(page);
943 end_page_writeback(page);
948 if(nBytes != PAGE_CACHE_SIZE)
949 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
955 obj = yaffs_InodeToObject(inode);
956 yaffs_GrossLock(obj->myDev);
959 ("yaffs_writepage at %08x, size %08x\n",
960 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
962 ("writepag0: obj = %05x, ino = %05x\n",
963 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
965 nWritten = yaffs_WriteDataToFile(obj, buffer,
966 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
969 ("writepag1: obj = %05x, ino = %05x\n",
970 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
972 yaffs_GrossUnlock(obj->myDev);
975 set_page_writeback(page);
977 end_page_writeback(page);
980 return (nWritten == nBytes) ? 0 : -ENOSPC;
984 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
985 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
986 loff_t pos, unsigned len, unsigned flags,
987 struct page **pagep, void **fsdata)
989 struct page *pg = NULL;
990 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
996 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
997 pg = grab_cache_page_write_begin(mapping, index, flags);
999 pg = __grab_cache_page(mapping, index);
1007 T(YAFFS_TRACE_OS, ("start yaffs_write_begin index %d(%x) uptodate %d\n",(int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
1010 space_held = yaffs_hold_space(filp);
1017 /* Update page if required */
1019 if (!Page_Uptodate(pg))
1020 ret = yaffs_readpage_nolock(filp, pg);
1025 /* Happy path return */
1026 T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
1031 T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
1033 yaffs_release_space(filp);
1036 page_cache_release(pg);
1043 static int yaffs_prepare_write(struct file *f, struct page *pg,
1044 unsigned offset, unsigned to)
1046 T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
1048 if (!Page_Uptodate(pg))
1049 return yaffs_readpage_nolock(f, pg);
1054 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1055 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1056 loff_t pos, unsigned len, unsigned copied,
1057 struct page *pg, void *fsdadata)
1061 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1064 addr = kva + offset_into_page;
1067 ("yaffs_write_end addr %x pos %x nBytes %d\n",
1071 ret = yaffs_file_write(filp, addr, copied, &pos);
1073 if (ret != copied) {
1075 ("yaffs_write_end not same size ret %d copied %d\n",
1084 yaffs_release_space(filp);
1086 page_cache_release(pg);
1091 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1096 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1097 int nBytes = to - offset;
1100 unsigned spos = pos;
1104 addr = kva + offset;
1106 saddr = (unsigned) addr;
1109 ("yaffs_commit_write addr %x pos %x nBytes %d\n",
1110 saddr, spos, nBytes));
1112 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1114 if (nWritten != nBytes) {
1116 ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
1126 ("yaffs_commit_write returning %d\n",
1127 nWritten == nBytes ? 0 : nWritten));
1129 return nWritten == nBytes ? 0 : nWritten;
1134 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1139 /* Check mode against the variant type and attempt to repair if broken. */
1140 __u32 mode = obj->yst_mode;
1141 switch (obj->variantType) {
1142 case YAFFS_OBJECT_TYPE_FILE:
1143 if (!S_ISREG(mode)) {
1144 obj->yst_mode &= ~S_IFMT;
1145 obj->yst_mode |= S_IFREG;
1149 case YAFFS_OBJECT_TYPE_SYMLINK:
1150 if (!S_ISLNK(mode)) {
1151 obj->yst_mode &= ~S_IFMT;
1152 obj->yst_mode |= S_IFLNK;
1156 case YAFFS_OBJECT_TYPE_DIRECTORY:
1157 if (!S_ISDIR(mode)) {
1158 obj->yst_mode &= ~S_IFMT;
1159 obj->yst_mode |= S_IFDIR;
1163 case YAFFS_OBJECT_TYPE_UNKNOWN:
1164 case YAFFS_OBJECT_TYPE_HARDLINK:
1165 case YAFFS_OBJECT_TYPE_SPECIAL:
1171 inode->i_flags |= S_NOATIME;
1173 inode->i_ino = obj->objectId;
1174 inode->i_mode = obj->yst_mode;
1175 inode->i_uid = obj->yst_uid;
1176 inode->i_gid = obj->yst_gid;
1177 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1178 inode->i_blksize = inode->i_sb->s_blocksize;
1180 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1182 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1183 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1184 inode->i_atime.tv_nsec = 0;
1185 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1186 inode->i_mtime.tv_nsec = 0;
1187 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1188 inode->i_ctime.tv_nsec = 0;
1190 inode->i_rdev = obj->yst_rdev;
1191 inode->i_atime = obj->yst_atime;
1192 inode->i_mtime = obj->yst_mtime;
1193 inode->i_ctime = obj->yst_ctime;
1195 inode->i_size = yaffs_GetObjectFileLength(obj);
1196 inode->i_blocks = (inode->i_size + 511) >> 9;
1198 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1201 ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
1202 inode->i_mode, inode->i_uid, inode->i_gid,
1203 (int)inode->i_size, atomic_read(&inode->i_count)));
1205 switch (obj->yst_mode & S_IFMT) {
1206 default: /* fifo, device or socket */
1207 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1208 init_special_inode(inode, obj->yst_mode,
1209 old_decode_dev(obj->yst_rdev));
1211 init_special_inode(inode, obj->yst_mode,
1212 (dev_t) (obj->yst_rdev));
1215 case S_IFREG: /* file */
1216 inode->i_op = &yaffs_file_inode_operations;
1217 inode->i_fop = &yaffs_file_operations;
1218 inode->i_mapping->a_ops =
1219 &yaffs_file_address_operations;
1221 case S_IFDIR: /* directory */
1222 inode->i_op = &yaffs_dir_inode_operations;
1223 inode->i_fop = &yaffs_dir_operations;
1225 case S_IFLNK: /* symlink */
1226 inode->i_op = &yaffs_symlink_inode_operations;
1230 yaffs_InodeToObjectLV(inode) = obj;
1232 obj->myInode = inode;
1236 ("yaffs_FileInode invalid parameters\n"));
1241 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1244 struct inode *inode;
1248 ("yaffs_get_inode for NULL super_block!!\n"));
1255 ("yaffs_get_inode for NULL object!!\n"));
1261 ("yaffs_get_inode for object %d\n", obj->objectId));
1263 inode = Y_IGET(sb, obj->objectId);
1267 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1268 /* iget also increments the inode's i_count */
1269 /* NB You can't be holding grossLock or deadlock will happen! */
1274 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1279 struct inode *inode;
1282 obj = yaffs_DentryToObject(f->f_dentry);
1286 yaffs_GrossLock(dev);
1288 inode = f->f_dentry->d_inode;
1290 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1291 ipos = inode->i_size;
1297 ("yaffs_file_write: hey obj is null!\n"));
1300 ("yaffs_file_write about to write writing %u(%x) bytes"
1301 "to object %d at %d(%x)\n",
1302 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1304 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1307 ("yaffs_file_write: %d(%x) bytes written\n",
1308 (unsigned )n,(unsigned)n));
1313 if (ipos > inode->i_size) {
1314 inode->i_size = ipos;
1315 inode->i_blocks = (ipos + 511) >> 9;
1318 ("yaffs_file_write size updated to %d bytes, "
1320 ipos, (int)(inode->i_blocks)));
1324 yaffs_GrossUnlock(dev);
1325 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1328 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1329 /* For now we just assume few parallel writes and check against a small number. */
1330 /* Todo: need to do this with a counter to handle parallel reads better */
1332 static ssize_t yaffs_hold_space(struct file *f)
1340 obj = yaffs_DentryToObject(f->f_dentry);
1344 yaffs_GrossLock(dev);
1346 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1348 yaffs_GrossUnlock(dev);
1350 return (nFreeChunks > 20) ? 1 : 0;
1353 static void yaffs_release_space(struct file *f)
1359 obj = yaffs_DentryToObject(f->f_dentry);
1363 yaffs_GrossLock(dev);
1366 yaffs_GrossUnlock(dev);
1370 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1378 offset += i_size_read(file->f_path.dentry->d_inode);
1381 offset += file->f_pos;
1386 if (offset != file->f_pos)
1387 file->f_pos = offset;
1396 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1400 struct yaffs_SearchContext *sc;
1401 struct inode *inode = f->f_dentry->d_inode;
1402 unsigned long offset, curoffs;
1406 char name[YAFFS_MAX_NAME_LENGTH + 1];
1408 obj = yaffs_DentryToObject(f->f_dentry);
1411 yaffs_GrossLock(dev);
1413 yaffs_DeviceToContext(dev)->readdirProcess = current;
1417 sc = yaffs_NewSearch(obj);
1423 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
1427 ("yaffs_readdir: entry . ino %d \n",
1428 (int)inode->i_ino));
1429 yaffs_GrossUnlock(dev);
1430 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
1432 yaffs_GrossLock(dev);
1438 ("yaffs_readdir: entry .. ino %d \n",
1439 (int)f->f_dentry->d_parent->d_inode->i_ino));
1440 yaffs_GrossUnlock(dev);
1441 if (filldir(dirent, "..", 2, offset,
1442 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
1444 yaffs_GrossLock(dev);
1451 /* If the directory has changed since the open or last call to
1452 readdir, rewind to after the 2 canned entries. */
1453 if (f->f_version != inode->i_version) {
1456 f->f_version = inode->i_version;
1459 while(sc->nextReturn){
1462 if (curoffs >= offset) {
1463 int this_inode = yaffs_GetObjectInode(l);
1464 int this_type = yaffs_GetObjectType(l);
1466 yaffs_GetObjectName(l, name,
1467 YAFFS_MAX_NAME_LENGTH + 1);
1469 ("yaffs_readdir: %s inode %d\n", name,
1470 yaffs_GetObjectInode(l)));
1472 yaffs_GrossUnlock(dev);
1482 yaffs_GrossLock(dev);
1487 yaffs_SearchAdvance(sc);
1491 yaffs_DeviceToContext(dev)->readdirProcess = NULL;
1493 yaffs_GrossUnlock(dev);
1495 yaffs_EndSearch(sc);
1503 * File creation. Allocate an inode, and we're done..
1506 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1509 #define YCRED(x) (x->cred)
1512 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1513 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1516 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1520 struct inode *inode;
1522 yaffs_Object *obj = NULL;
1525 yaffs_Object *parent = yaffs_InodeToObject(dir);
1527 int error = -ENOSPC;
1528 uid_t uid = YCRED(current)->fsuid;
1529 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1531 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1536 ("yaffs_mknod: parent object %d type %d\n",
1537 parent->objectId, parent->variantType));
1540 ("yaffs_mknod: could not get parent object\n"));
1544 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1546 dentry->d_name.name, mode, rdev));
1548 dev = parent->myDev;
1550 yaffs_GrossLock(dev);
1552 switch (mode & S_IFMT) {
1554 /* Special (socket, fifo, device...) */
1555 T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
1556 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1557 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1558 gid, old_encode_dev(rdev));
1560 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1564 case S_IFREG: /* file */
1565 T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
1566 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1569 case S_IFDIR: /* directory */
1571 ("yaffs_mknod: making directory\n"));
1572 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1575 case S_IFLNK: /* symlink */
1576 T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
1577 obj = NULL; /* Do we ever get here? */
1581 /* Can not call yaffs_get_inode() with gross lock held */
1582 yaffs_GrossUnlock(dev);
1585 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1586 d_instantiate(dentry, inode);
1587 update_dir_time(dir);
1589 ("yaffs_mknod created object %d count = %d\n",
1590 obj->objectId, atomic_read(&inode->i_count)));
1592 yaffs_FillInodeFromObject(dir,parent);
1595 ("yaffs_mknod failed making object\n"));
1602 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1605 T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
1606 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1610 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1611 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1612 struct nameidata *n)
1614 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1617 T(YAFFS_TRACE_OS, ("yaffs_create\n"));
1618 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1621 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1629 ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1630 dentry->d_name.name));
1631 obj = yaffs_InodeToObject(dir);
1634 yaffs_GrossLock(dev);
1636 retVal = yaffs_Unlink(obj, dentry->d_name.name);
1638 if (retVal == YAFFS_OK) {
1639 dentry->d_inode->i_nlink--;
1641 yaffs_GrossUnlock(dev);
1642 mark_inode_dirty(dentry->d_inode);
1643 update_dir_time(dir);
1646 yaffs_GrossUnlock(dev);
1653 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1654 struct dentry *dentry)
1656 struct inode *inode = old_dentry->d_inode;
1657 yaffs_Object *obj = NULL;
1658 yaffs_Object *link = NULL;
1661 T(YAFFS_TRACE_OS, ("yaffs_link\n"));
1663 obj = yaffs_InodeToObject(inode);
1666 yaffs_GrossLock(dev);
1668 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1669 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1673 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1674 d_instantiate(dentry, old_dentry->d_inode);
1675 atomic_inc(&old_dentry->d_inode->i_count);
1677 ("yaffs_link link count %d i_count %d\n",
1678 old_dentry->d_inode->i_nlink,
1679 atomic_read(&old_dentry->d_inode->i_count)));
1682 yaffs_GrossUnlock(dev);
1685 update_dir_time(dir);
1692 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1693 const char *symname)
1697 uid_t uid = YCRED(current)->fsuid;
1698 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1700 T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
1702 dev = yaffs_InodeToObject(dir)->myDev;
1703 yaffs_GrossLock(dev);
1704 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1705 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1706 yaffs_GrossUnlock(dev);
1709 struct inode *inode;
1711 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1712 d_instantiate(dentry, inode);
1713 update_dir_time(dir);
1714 T(YAFFS_TRACE_OS, ("symlink created OK\n"));
1717 T(YAFFS_TRACE_OS, ("symlink not created\n"));
1723 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1730 obj = yaffs_DentryToObject(dentry);
1734 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_object\n"));
1735 yaffs_GrossLock(dev);
1736 yaffs_FlushFile(obj, 1, datasync);
1737 yaffs_GrossUnlock(dev);
1742 * The VFS layer already does all the dentry stuff for rename.
1744 * NB: POSIX says you can rename an object over an old object of the same name
1746 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1747 struct inode *new_dir, struct dentry *new_dentry)
1750 int retVal = YAFFS_FAIL;
1751 yaffs_Object *target;
1753 T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
1754 dev = yaffs_InodeToObject(old_dir)->myDev;
1756 yaffs_GrossLock(dev);
1758 /* Check if the target is an existing directory that is not empty. */
1759 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1760 new_dentry->d_name.name);
1764 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1765 !ylist_empty(&target->variant.directoryVariant.children)) {
1767 T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
1769 retVal = YAFFS_FAIL;
1771 /* Now does unlinking internally using shadowing mechanism */
1772 T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
1774 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1775 old_dentry->d_name.name,
1776 yaffs_InodeToObject(new_dir),
1777 new_dentry->d_name.name);
1779 yaffs_GrossUnlock(dev);
1781 if (retVal == YAFFS_OK) {
1783 new_dentry->d_inode->i_nlink--;
1784 mark_inode_dirty(new_dentry->d_inode);
1787 update_dir_time(old_dir);
1788 if(old_dir != new_dir)
1789 update_dir_time(new_dir);
1796 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1798 struct inode *inode = dentry->d_inode;
1803 ("yaffs_setattr of object %d\n",
1804 yaffs_InodeToObject(inode)->objectId));
1806 /* Fail if a requested resize >= 2GB */
1807 if (attr->ia_valid & ATTR_SIZE &&
1808 (attr->ia_size >> 31))
1812 error = inode_change_ok(inode, attr);
1816 error = inode_setattr(inode, attr);
1817 T(YAFFS_TRACE_OS,("inode_setattr called\n"));
1818 if (attr->ia_valid & ATTR_SIZE)
1819 truncate_inode_pages(&inode->i_data,attr->ia_size);
1821 dev = yaffs_InodeToObject(inode)->myDev;
1822 if (attr->ia_valid & ATTR_SIZE){
1823 T(YAFFS_TRACE_OS,("resize to %d(%x)\n",(int)(attr->ia_size),(int)(attr->ia_size)));
1825 yaffs_GrossLock(dev);
1826 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1827 if(result == YAFFS_OK) {
1832 yaffs_GrossUnlock(dev);
1837 ("yaffs_setattr done returning %d\n",error));
1842 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1843 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1845 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1846 struct super_block *sb = dentry->d_sb;
1847 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1848 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1850 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1852 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1854 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1857 T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
1859 yaffs_GrossLock(dev);
1861 buf->f_type = YAFFS_MAGIC;
1862 buf->f_bsize = sb->s_blocksize;
1863 buf->f_namelen = 255;
1865 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1866 /* Do this if chunk size is not a power of 2 */
1868 uint64_t bytesInDev;
1871 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
1872 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
1874 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1875 buf->f_blocks = bytesInDev;
1877 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1878 ((uint64_t)(dev->nDataBytesPerChunk));
1880 do_div(bytesFree, sb->s_blocksize);
1882 buf->f_bfree = bytesFree;
1884 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1887 (dev->param.endBlock - dev->param.startBlock + 1) *
1888 dev->param.nChunksPerBlock /
1889 (sb->s_blocksize / dev->nDataBytesPerChunk);
1891 yaffs_GetNumberOfFreeChunks(dev) /
1892 (sb->s_blocksize / dev->nDataBytesPerChunk);
1895 (dev->param.endBlock - dev->param.startBlock + 1) *
1896 dev->param.nChunksPerBlock *
1897 (dev->nDataBytesPerChunk / sb->s_blocksize);
1900 yaffs_GetNumberOfFreeChunks(dev) *
1901 (dev->nDataBytesPerChunk / sb->s_blocksize);
1906 buf->f_bavail = buf->f_bfree;
1908 yaffs_GrossUnlock(dev);
1914 static void yaffs_FlushInodes(struct super_block *sb)
1919 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1920 obj = yaffs_InodeToObject(iptr);
1922 T(YAFFS_TRACE_OS, ("flushing obj %d\n",obj->objectId));
1923 yaffs_FlushFile(obj,1,0);
1929 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
1931 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1935 yaffs_FlushInodes(sb);
1936 yaffs_UpdateDirtyDirectories(dev);
1937 yaffs_FlushEntireDeviceCache(dev);
1939 yaffs_CheckpointSave(dev);
1942 static int yaffs_do_sync_fs(struct super_block *sb, int do_checkpoint)
1945 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1946 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
1948 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1949 ("yaffs_do_sync_fs: %s %s%s\n",
1950 sb->s_dirt ? "dirty" : "clean",
1951 do_checkpoint ? "with checkpoint" : "no checkpoint",
1952 oneshot_checkpoint ? " one-shot" : "" ));
1954 if (sb->s_dirt || oneshot_checkpoint) {
1955 yaffs_GrossLock(dev);
1956 yaffs_FlushSuperBlock(sb,do_checkpoint);
1957 yaffs_GrossUnlock(dev);
1961 if(oneshot_checkpoint)
1962 yaffs_auto_checkpoint &= ~4;
1968 * yaffs background thread functions .
1969 * yaffs_BackgroundThread() the thread function
1970 * yaffs_BackgroundStart() launches the background thread.
1971 * yaffs_BackgroundStop() cleans up the background thread.
1974 * The thread should only run after the yaffs is initialised
1975 * The thread should be stopped before yaffs is unmounted.
1976 * The thread should not do any writing while the fs is in read only.
1979 #ifdef YAFFS_COMPILE_BACKGROUND
1981 void yaffs_background_waker(unsigned long data)
1983 wake_up_process((struct task_struct *)data);
1986 static int yaffs_BackgroundThread(void *data)
1988 yaffs_Device *dev = (yaffs_Device *)data;
1989 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
1990 unsigned long now = jiffies;
1991 unsigned long next_dir_update = now;
1992 unsigned long next_gc = now;
1993 unsigned long expires;
1996 unsigned int erasedChunks;
1997 struct timer_list timer;
1999 T(YAFFS_TRACE_BACKGROUND,
2000 ("yaffs_background starting for dev %p\n",
2005 while(context->bgRunning){
2006 T(YAFFS_TRACE_BACKGROUND,
2007 ("yaffs_background\n"));
2009 if(kthread_should_stop())
2015 yaffs_GrossLock(dev);
2019 if(time_after(now, next_dir_update) &&
2020 !dev->isCheckpointed){
2021 yaffs_UpdateDirtyDirectories(dev);
2022 next_dir_update = now + HZ;
2025 if(time_after(now,next_gc) &&
2026 ! dev->isCheckpointed){
2027 gcResult = yaffs_BackgroundGarbageCollect(dev);
2028 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2029 if(erasedChunks < dev->nFreeChunks/4)
2030 next_gc = now + HZ/50+1;
2031 else if(erasedChunks < dev->nFreeChunks/2)
2032 next_gc = now + HZ/20+1;
2034 next_gc = now + HZ * 2;
2036 yaffs_GrossUnlock(dev);
2038 expires = next_dir_update;
2039 if (time_before(next_gc,expires))
2041 if(time_before(expires,now))
2045 timer.expires = expires+1;
2046 timer.data = (unsigned long) current;
2047 timer.function = yaffs_background_waker;
2049 set_current_state(TASK_INTERRUPTIBLE);
2052 del_timer_sync(&timer);
2061 static int yaffs_BackgroundStart(yaffs_Device *dev)
2065 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
2067 context->bgRunning = 1;
2069 context->bgThread = kthread_run(yaffs_BackgroundThread,(void *)dev,"yaffs_%x",(unsigned)dev);
2071 if(IS_ERR(context->bgThread)){
2072 retval = PTR_ERR(context->bgThread);
2073 context->bgThread = NULL;
2074 context->bgRunning = 0;
2079 static void yaffs_BackgroundStop(yaffs_Device *dev)
2081 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToContext(dev);
2083 ctxt->bgRunning = 0;
2085 if( ctxt->bgThread){
2086 kthread_stop(ctxt->bgThread);
2087 ctxt->bgThread = NULL;
2091 static int yaffs_BackgroundThread(void *data)
2096 static int yaffs_BackgroundStart(yaffs_Device *dev)
2101 static void yaffs_BackgroundStop(yaffs_Device *dev)
2107 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2108 static void yaffs_write_super(struct super_block *sb)
2110 static int yaffs_write_super(struct super_block *sb)
2114 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_write_super\n"));
2115 yaffs_do_sync_fs(sb, yaffs_auto_checkpoint >= 2);
2117 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2123 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2124 static int yaffs_sync_fs(struct super_block *sb, int wait)
2126 static int yaffs_sync_fs(struct super_block *sb)
2129 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_fs\n"));
2131 yaffs_do_sync_fs(sb,yaffs_auto_checkpoint >= 1);
2136 #ifdef YAFFS_USE_OWN_IGET
2138 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2140 struct inode *inode;
2142 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2145 ("yaffs_iget for %lu\n", ino));
2147 inode = iget_locked(sb, ino);
2149 return ERR_PTR(-ENOMEM);
2150 if (!(inode->i_state & I_NEW))
2153 /* NB This is called as a side effect of other functions, but
2154 * we had to release the lock to prevent deadlocks, so
2155 * need to lock again.
2158 yaffs_GrossLock(dev);
2160 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2162 yaffs_FillInodeFromObject(inode, obj);
2164 yaffs_GrossUnlock(dev);
2166 unlock_new_inode(inode);
2172 static void yaffs_read_inode(struct inode *inode)
2174 /* NB This is called as a side effect of other functions, but
2175 * we had to release the lock to prevent deadlocks, so
2176 * need to lock again.
2180 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2183 ("yaffs_read_inode for %d\n", (int)inode->i_ino));
2185 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
2186 yaffs_GrossLock(dev);
2188 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2190 yaffs_FillInodeFromObject(inode, obj);
2192 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
2193 yaffs_GrossUnlock(dev);
2198 static YLIST_HEAD(yaffs_context_list);
2199 struct semaphore yaffs_context_lock;
2201 #if 0 /* not used */
2202 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2204 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2206 if (*flags & MS_RDONLY) {
2207 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2210 ("yaffs_remount_fs: %s: RO\n", dev->name));
2212 yaffs_GrossLock(dev);
2214 yaffs_FlushSuperBlock(sb,1);
2219 yaffs_GrossUnlock(dev);
2222 ("yaffs_remount_fs: %s: RW\n", dev->name));
2229 static void yaffs_put_super(struct super_block *sb)
2231 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2233 T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
2235 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND, ("Shutting down yaffs background thread\n"));
2236 yaffs_BackgroundStop(dev);
2237 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND, ("yaffs background thread shut down\n"));
2239 yaffs_GrossLock(dev);
2241 yaffs_FlushSuperBlock(sb,1);
2243 if (yaffs_DeviceToContext(dev)->putSuperFunc)
2244 yaffs_DeviceToContext(dev)->putSuperFunc(sb);
2247 yaffs_Deinitialise(dev);
2249 yaffs_GrossUnlock(dev);
2251 down(&yaffs_context_lock);
2252 ylist_del_init(&(yaffs_DeviceToContext(dev)->contextList));
2253 up(&yaffs_context_lock);
2255 if (yaffs_DeviceToContext(dev)->spareBuffer) {
2256 YFREE(yaffs_DeviceToContext(dev)->spareBuffer);
2257 yaffs_DeviceToContext(dev)->spareBuffer = NULL;
2264 static void yaffs_MTDPutSuper(struct super_block *sb)
2266 struct mtd_info *mtd = yaffs_DeviceToContext(yaffs_SuperToDevice(sb))->mtd;
2271 put_mtd_device(mtd);
2275 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2277 struct super_block *sb = yaffs_DeviceToContext(dev)->superBlock;
2279 T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
2286 int skip_checkpoint_read;
2287 int skip_checkpoint_write;
2290 int tags_ecc_overridden;
2291 int lazy_loading_enabled;
2292 int lazy_loading_overridden;
2293 int empty_lost_and_found;
2294 int empty_lost_and_found_overridden;
2297 #define MAX_OPT_LEN 30
2298 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2300 char cur_opt[MAX_OPT_LEN + 1];
2304 /* Parse through the options which is a comma seperated list */
2306 while (options_str && *options_str && !error) {
2307 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2310 while(*options_str == ',')
2313 while (*options_str && *options_str != ',') {
2314 if (p < MAX_OPT_LEN) {
2315 cur_opt[p] = *options_str;
2321 if (!strcmp(cur_opt, "inband-tags"))
2322 options->inband_tags = 1;
2323 else if (!strcmp(cur_opt, "tags-ecc-off")){
2324 options->tags_ecc_on = 0;
2325 options->tags_ecc_overridden=1;
2326 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2327 options->tags_ecc_on = 1;
2328 options->tags_ecc_overridden = 1;
2329 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2330 options->lazy_loading_enabled = 0;
2331 options->lazy_loading_overridden=1;
2332 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2333 options->lazy_loading_enabled = 1;
2334 options->lazy_loading_overridden = 1;
2335 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2336 options->empty_lost_and_found = 0;
2337 options->empty_lost_and_found_overridden=1;
2338 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2339 options->empty_lost_and_found = 1;
2340 options->empty_lost_and_found_overridden=1;
2341 } else if (!strcmp(cur_opt, "no-cache"))
2342 options->no_cache = 1;
2343 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2344 options->skip_checkpoint_read = 1;
2345 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2346 options->skip_checkpoint_write = 1;
2347 else if (!strcmp(cur_opt, "no-checkpoint")) {
2348 options->skip_checkpoint_read = 1;
2349 options->skip_checkpoint_write = 1;
2351 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2360 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2361 struct super_block *sb,
2362 void *data, int silent)
2365 struct inode *inode = NULL;
2366 struct dentry *root;
2367 yaffs_Device *dev = 0;
2368 char devname_buf[BDEVNAME_SIZE + 1];
2369 struct mtd_info *mtd;
2371 char *data_str = (char *)data;
2372 struct yaffs_LinuxContext *context = NULL;
2373 yaffs_DeviceParam *param;
2375 yaffs_options options;
2377 sb->s_magic = YAFFS_MAGIC;
2378 sb->s_op = &yaffs_super_ops;
2379 sb->s_flags |= MS_NOATIME;
2381 #ifdef YAFFS_COMPILE_EXPORTFS
2382 sb->s_export_op = &yaffs_export_ops;
2386 printk(KERN_INFO "yaffs: sb is NULL\n");
2387 else if (!sb->s_dev)
2388 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2389 else if (!yaffs_devname(sb, devname_buf))
2390 printk(KERN_INFO "yaffs: devname is NULL\n");
2392 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2394 yaffs_devname(sb, devname_buf));
2399 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2401 memset(&options, 0, sizeof(options));
2403 if (yaffs_parse_options(&options, data_str)) {
2404 /* Option parsing failed */
2409 sb->s_blocksize = PAGE_CACHE_SIZE;
2410 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2411 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2413 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2415 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2417 ("yaffs: Write verification disabled. All guarantees "
2418 "null and void\n"));
2421 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2423 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2424 yaffs_devname(sb, devname_buf)));
2426 /* Check it's an mtd device..... */
2427 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2428 return NULL; /* This isn't an mtd device */
2430 /* Get the device */
2431 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2433 T(YAFFS_TRACE_ALWAYS,
2434 ("yaffs: MTD device #%u doesn't appear to exist\n",
2438 /* Check it's NAND */
2439 if (mtd->type != MTD_NANDFLASH) {
2440 T(YAFFS_TRACE_ALWAYS,
2441 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
2445 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
2446 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
2447 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
2448 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
2449 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
2450 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
2451 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
2452 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2453 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
2454 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
2455 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2456 T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
2458 T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
2461 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2463 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2464 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
2468 /* Added NCB 26/5/2006 for completeness */
2469 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2470 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
2476 if (yaffsVersion == 2) {
2477 /* Check for version 2 style functions */
2479 !mtd->block_isbad ||
2480 !mtd->block_markbad ||
2483 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2484 !mtd->read_oob || !mtd->write_oob) {
2487 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2489 T(YAFFS_TRACE_ALWAYS,
2490 ("yaffs: MTD device does not support required "
2495 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2496 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2497 !options.inband_tags) {
2498 T(YAFFS_TRACE_ALWAYS,
2499 ("yaffs: MTD device does not have the "
2500 "right page sizes\n"));
2504 /* Check for V1 style functions */
2508 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2509 !mtd->read_oob || !mtd->write_oob) {
2512 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2514 T(YAFFS_TRACE_ALWAYS,
2515 ("yaffs: MTD device does not support required "
2520 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2521 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2522 T(YAFFS_TRACE_ALWAYS,
2523 ("yaffs: MTD device does not support have the "
2524 "right page sizes\n"));
2529 /* OK, so if we got here, we have an MTD that's NAND and looks
2530 * like it has the right capabilities
2531 * Set the yaffs_Device up for mtd
2534 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2535 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2537 if(!dev || !context ){
2547 /* Deep shit could not allocate device structure */
2548 T(YAFFS_TRACE_ALWAYS,
2549 ("yaffs_read_super: Failed trying to allocate "
2550 "yaffs_Device. \n"));
2553 memset(dev, 0, sizeof(yaffs_Device));
2554 param = &(dev->param);
2556 memset(context,0,sizeof(struct yaffs_LinuxContext));
2557 dev->context = context;
2558 YINIT_LIST_HEAD(&(context->contextList));
2560 context->superBlock = sb;
2564 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2565 sb->s_fs_info = dev;
2567 sb->u.generic_sbp = dev;
2570 yaffs_DeviceToContext(dev)->mtd = mtd;
2571 param->name = mtd->name;
2573 /* Set up the memory size parameters.... */
2575 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2577 param->startBlock = 0;
2578 param->endBlock = nBlocks - 1;
2579 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2580 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2581 param->nReservedBlocks = 5;
2582 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2583 param->inbandTags = options.inband_tags;
2585 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2586 param->disableLazyLoad = 1;
2588 if(options.lazy_loading_overridden)
2589 param->disableLazyLoad = !options.lazy_loading_enabled;
2591 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2592 param->noTagsECC = 1;
2595 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2597 param->deferDirectoryUpdate = 1;
2600 if(options.tags_ecc_overridden)
2601 param->noTagsECC = !options.tags_ecc_on;
2603 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2604 param->emptyLostAndFound = 1;
2607 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2608 param->refreshPeriod = 0;
2610 param->refreshPeriod = 100;
2613 if(options.empty_lost_and_found_overridden)
2614 param->emptyLostAndFound = options.empty_lost_and_found;
2616 /* ... and the functions. */
2617 if (yaffsVersion == 2) {
2618 param->writeChunkWithTagsToNAND =
2619 nandmtd2_WriteChunkWithTagsToNAND;
2620 param->readChunkWithTagsFromNAND =
2621 nandmtd2_ReadChunkWithTagsFromNAND;
2622 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2623 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2624 yaffs_DeviceToContext(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2625 param->isYaffs2 = 1;
2626 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2627 param->totalBytesPerChunk = mtd->writesize;
2628 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2630 param->totalBytesPerChunk = mtd->oobblock;
2631 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2633 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2635 param->startBlock = 0;
2636 param->endBlock = nBlocks - 1;
2638 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2639 /* use the MTD interface in yaffs_mtdif1.c */
2640 param->writeChunkWithTagsToNAND =
2641 nandmtd1_WriteChunkWithTagsToNAND;
2642 param->readChunkWithTagsFromNAND =
2643 nandmtd1_ReadChunkWithTagsFromNAND;
2644 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2645 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2647 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2648 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2650 param->isYaffs2 = 0;
2652 /* ... and common functions */
2653 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2654 param->initialiseNAND = nandmtd_InitialiseNAND;
2656 yaffs_DeviceToContext(dev)->putSuperFunc = yaffs_MTDPutSuper;
2658 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2659 param->gcControl = yaffs_gc_control_callback;
2661 yaffs_DeviceToContext(dev)->superBlock= sb;
2664 #ifndef CONFIG_YAFFS_DOES_ECC
2665 param->useNANDECC = 1;
2668 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2669 param->wideTnodesDisabled = 1;
2672 param->skipCheckpointRead = options.skip_checkpoint_read;
2673 param->skipCheckpointWrite = options.skip_checkpoint_write;
2675 /* we assume this is protected by lock_kernel() in mount/umount */
2676 down(&yaffs_context_lock);
2677 ylist_add_tail(&(yaffs_DeviceToContext(dev)->contextList), &yaffs_context_list);
2678 up(&yaffs_context_lock);
2680 /* Directory search handling...*/
2681 YINIT_LIST_HEAD(&(yaffs_DeviceToContext(dev)->searchContexts));
2682 param->removeObjectCallback = yaffs_RemoveObjectCallback;
2684 init_MUTEX(&(yaffs_DeviceToContext(dev)->grossLock));
2686 yaffs_GrossLock(dev);
2688 err = yaffs_GutsInitialise(dev);
2691 ("yaffs_read_super: guts initialised %s\n",
2692 (err == YAFFS_OK) ? "OK" : "FAILED"));
2695 yaffs_BackgroundStart(dev);
2697 if(!context->bgThread)
2698 param->deferDirectoryUpdate = 0;
2701 /* Release lock before yaffs_get_inode() */
2702 yaffs_GrossUnlock(dev);
2704 /* Create root inode */
2705 if (err == YAFFS_OK)
2706 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2712 inode->i_op = &yaffs_dir_inode_operations;
2713 inode->i_fop = &yaffs_dir_operations;
2715 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
2717 root = d_alloc_root(inode);
2719 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
2726 sb->s_dirt = !dev->isCheckpointed;
2727 T(YAFFS_TRACE_ALWAYS,
2728 ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
2730 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
2735 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2736 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2739 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2742 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2743 static int yaffs_read_super(struct file_system_type *fs,
2744 int flags, const char *dev_name,
2745 void *data, struct vfsmount *mnt)
2748 return get_sb_bdev(fs, flags, dev_name, data,
2749 yaffs_internal_read_super_mtd, mnt);
2752 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2753 int flags, const char *dev_name,
2757 return get_sb_bdev(fs, flags, dev_name, data,
2758 yaffs_internal_read_super_mtd);
2762 static struct file_system_type yaffs_fs_type = {
2763 .owner = THIS_MODULE,
2765 .get_sb = yaffs_read_super,
2766 .kill_sb = kill_block_super,
2767 .fs_flags = FS_REQUIRES_DEV,
2770 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2773 return yaffs_internal_read_super(1, sb, data, silent);
2776 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2781 #ifdef CONFIG_YAFFS_YAFFS2
2783 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2784 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2787 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2790 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2791 static int yaffs2_read_super(struct file_system_type *fs,
2792 int flags, const char *dev_name, void *data,
2793 struct vfsmount *mnt)
2795 return get_sb_bdev(fs, flags, dev_name, data,
2796 yaffs2_internal_read_super_mtd, mnt);
2799 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2800 int flags, const char *dev_name,
2804 return get_sb_bdev(fs, flags, dev_name, data,
2805 yaffs2_internal_read_super_mtd);
2809 static struct file_system_type yaffs2_fs_type = {
2810 .owner = THIS_MODULE,
2812 .get_sb = yaffs2_read_super,
2813 .kill_sb = kill_block_super,
2814 .fs_flags = FS_REQUIRES_DEV,
2817 static struct super_block *yaffs2_read_super(struct super_block *sb,
2818 void *data, int silent)
2820 return yaffs_internal_read_super(2, sb, data, silent);
2823 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2827 #endif /* CONFIG_YAFFS_YAFFS2 */
2829 static struct proc_dir_entry *my_proc_entry;
2830 static struct proc_dir_entry *debug_proc_entry;
2832 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
2834 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
2835 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
2836 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
2837 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
2838 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
2839 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
2840 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
2841 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
2842 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
2843 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
2844 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
2845 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
2847 buf += sprintf(buf, "\n");
2853 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
2855 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2856 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2857 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2858 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2859 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2860 buf += sprintf(buf, "\n");
2861 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2862 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2863 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2864 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2865 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2866 buf += sprintf(buf, "\n");
2867 buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
2868 buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
2869 buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
2870 buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
2871 buf += sprintf(buf, "garbageCollections. %u\n", dev->garbageCollections);
2872 buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGarbageCollections);
2873 buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
2874 buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
2875 buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
2876 buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
2877 buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
2878 buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
2879 buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
2880 buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
2881 buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
2882 buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
2884 sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
2889 static int yaffs_proc_read(char *page,
2891 off_t offset, int count, int *eof, void *data)
2893 struct ylist_head *item;
2898 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2899 * We use 'offset' (*ppos) to indicate where we are in devList.
2900 * This also assumes the user has posted a read buffer large
2901 * enough to hold the complete output; but that's life in /proc.
2906 /* Print header first */
2908 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2909 "\n%s\n%s\n", yaffs_fs_c_version,
2910 yaffs_guts_c_version);
2912 buf += sprintf(buf,"\n");
2916 down(&yaffs_context_lock);
2918 /* Locate and print the Nth entry. Order N-squared but N is small. */
2919 ylist_for_each(item, &yaffs_context_list) {
2920 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2921 yaffs_Device *dev = dc->dev;
2923 if (n < (step & ~1)) {
2928 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
2929 buf = yaffs_dump_dev_part0(buf, dev);
2931 buf = yaffs_dump_dev_part1(buf, dev);
2935 up(&yaffs_context_lock);
2938 return buf - page < count ? buf - page : count;
2941 static int yaffs_debug_proc_read(char *page,
2943 off_t offset, int count, int *eof, void *data)
2945 struct ylist_head *item;
2949 down(&yaffs_context_lock);
2951 /* Locate and print the Nth entry. Order N-squared but N is small. */
2952 ylist_for_each(item, &yaffs_context_list) {
2953 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2954 yaffs_Device *dev = dc->dev;
2960 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2961 nObjects = dev->nObjectsCreated -dev->nFreeObjects;
2962 nTnodes = dev->nTnodesCreated - dev->nFreeTnodes;
2965 buf += sprintf(buf,"%d, %d, %d, %d, %d\n",
2966 n, dev->nFreeChunks, erasedChunks,
2969 up(&yaffs_context_lock);
2972 return buf - page < count ? buf - page : count;
2976 * Set the verbosity of the warnings and error messages.
2978 * Note that the names can only be a..z or _ with the current code.
2983 unsigned mask_bitfield;
2985 {"allocate", YAFFS_TRACE_ALLOCATE},
2986 {"always", YAFFS_TRACE_ALWAYS},
2987 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2988 {"buffers", YAFFS_TRACE_BUFFERS},
2989 {"bug", YAFFS_TRACE_BUG},
2990 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2991 {"deletion", YAFFS_TRACE_DELETION},
2992 {"erase", YAFFS_TRACE_ERASE},
2993 {"error", YAFFS_TRACE_ERROR},
2994 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2995 {"gc", YAFFS_TRACE_GC},
2996 {"mtd", YAFFS_TRACE_MTD},
2997 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2998 {"os", YAFFS_TRACE_OS},
2999 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3000 {"scan", YAFFS_TRACE_SCAN},
3001 {"tracing", YAFFS_TRACE_TRACING},
3002 {"sync", YAFFS_TRACE_SYNC},
3004 {"background", YAFFS_TRACE_BACKGROUND},
3006 {"verify", YAFFS_TRACE_VERIFY},
3007 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3008 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3009 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3011 {"write", YAFFS_TRACE_WRITE},
3012 {"all", 0xffffffff},
3017 #define MAX_MASK_NAME_LENGTH 40
3018 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3019 unsigned long count, void *data)
3021 unsigned rg = 0, mask_bitfield;
3025 char substring[MAX_MASK_NAME_LENGTH + 1];
3031 rg = yaffs_traceMask;
3033 while (!done && (pos < count)) {
3035 while ((pos < count) && isspace(buf[pos]))
3052 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3054 if (end > buf + pos) {
3055 mask_name = "numeral";
3056 len = end - (buf + pos);
3060 for (x = buf + pos, i = 0;
3061 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3062 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3064 substring[i] = '\0';
3066 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3067 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3068 mask_name = mask_flags[i].mask_name;
3069 mask_bitfield = mask_flags[i].mask_bitfield;
3076 if (mask_name != NULL) {
3080 rg &= ~mask_bitfield;
3083 rg |= mask_bitfield;
3089 rg |= mask_bitfield;
3095 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3097 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
3099 if (rg & YAFFS_TRACE_ALWAYS) {
3100 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3102 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3103 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3111 static int yaffs_proc_write(struct file *file, const char *buf,
3112 unsigned long count, void *data)
3114 return yaffs_proc_write_trace_options(file, buf, count, data);
3117 /* Stuff to handle installation of file systems */
3118 struct file_system_to_install {
3119 struct file_system_type *fst;
3123 static struct file_system_to_install fs_to_install[] = {
3124 {&yaffs_fs_type, 0},
3125 {&yaffs2_fs_type, 0},
3129 static int __init init_yaffs_fs(void)
3132 struct file_system_to_install *fsinst;
3134 T(YAFFS_TRACE_ALWAYS,
3135 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3137 init_MUTEX(&yaffs_context_lock);
3139 /* Install the proc_fs entries */
3140 my_proc_entry = create_proc_entry("yaffs",
3144 if (my_proc_entry) {
3145 my_proc_entry->write_proc = yaffs_proc_write;
3146 my_proc_entry->read_proc = yaffs_proc_read;
3147 my_proc_entry->data = NULL;
3151 debug_proc_entry = create_proc_entry("yaffs_debug",
3155 if (debug_proc_entry) {
3156 debug_proc_entry->write_proc = NULL;
3157 debug_proc_entry->read_proc = yaffs_debug_proc_read;
3158 debug_proc_entry->data = NULL;
3162 /* Now add the file system entries */
3164 fsinst = fs_to_install;
3166 while (fsinst->fst && !error) {
3167 error = register_filesystem(fsinst->fst);
3169 fsinst->installed = 1;
3173 /* Any errors? uninstall */
3175 fsinst = fs_to_install;
3177 while (fsinst->fst) {
3178 if (fsinst->installed) {
3179 unregister_filesystem(fsinst->fst);
3180 fsinst->installed = 0;
3189 static void __exit exit_yaffs_fs(void)
3192 struct file_system_to_install *fsinst;
3194 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3197 remove_proc_entry("yaffs", YPROC_ROOT);
3198 remove_proc_entry("yaffs_debug", YPROC_ROOT);
3200 fsinst = fs_to_install;
3202 while (fsinst->fst) {
3203 if (fsinst->installed) {
3204 unregister_filesystem(fsinst->fst);
3205 fsinst->installed = 0;
3211 module_init(init_yaffs_fs)
3212 module_exit(exit_yaffs_fs)
3214 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3215 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
3216 MODULE_LICENSE("GPL");