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
45 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
46 #include <linux/config.h>
49 #include <linux/kernel.h>
50 #include <linux/module.h>
51 #include <linux/slab.h>
52 #include <linux/init.h>
54 #include <linux/proc_fs.h>
55 #include <linux/smp_lock.h>
56 #include <linux/pagemap.h>
57 #include <linux/mtd/mtd.h>
58 #include <linux/interrupt.h>
59 #include <linux/string.h>
60 #include <linux/ctype.h>
61 #include <linux/namei.h>
63 #ifdef YAFFS_COMPILE_EXPORTFS
64 #include <linux/exportfs.h>
67 #ifdef YAFFS_COMPILE_BACKGROUND
68 #include <linux/kthread.h>
69 #include <linux/delay.h>
70 #include <linux/freezer.h>
73 #include <asm/div64.h>
75 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
77 #include <linux/statfs.h>
79 #define UnlockPage(p) unlock_page(p)
80 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
82 /* FIXME: use sb->s_id instead ? */
83 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
87 #include <linux/locks.h>
88 #define BDEVNAME_SIZE 0
89 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
91 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
92 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
98 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
99 #define YPROC_ROOT (&proc_root)
101 #define YPROC_ROOT NULL
104 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
105 #define WRITE_SIZE_STR "writesize"
106 #define WRITE_SIZE(mtd) ((mtd)->writesize)
108 #define WRITE_SIZE_STR "oobblock"
109 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
112 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
113 #define YAFFS_USE_WRITE_BEGIN_END 1
115 #define YAFFS_USE_WRITE_BEGIN_END 0
118 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
119 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
121 uint64_t result = partition_size;
122 do_div(result, block_size);
123 return (uint32_t)result;
126 #define YCALCBLOCKS(s, b) ((s)/(b))
129 #include <linux/uaccess.h>
130 #include <linux/mtd/mtd.h>
132 #include "yportenv.h"
133 #include "yaffs_trace.h"
134 #include "yaffs_guts.h"
136 #include "yaffs_linux.h"
138 #include "yaffs_mtdif.h"
139 #include "yaffs_mtdif1.h"
140 #include "yaffs_mtdif2.h"
142 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
143 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
144 unsigned int yaffs_auto_checkpoint = 1;
145 unsigned int yaffs_gc_control = 1;
147 /* Module Parameters */
148 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
149 module_param(yaffs_traceMask, uint, 0644);
150 module_param(yaffs_wr_attempts, uint, 0644);
151 module_param(yaffs_auto_checkpoint, uint, 0644);
152 module_param(yaffs_gc_control, uint, 0644);
154 MODULE_PARM(yaffs_traceMask, "i");
155 MODULE_PARM(yaffs_wr_attempts, "i");
156 MODULE_PARM(yaffs_auto_checkpoint, "i");
157 MODULE_PARM(yaffs_gc_control, "i");
160 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
161 /* use iget and read_inode */
162 #define Y_IGET(sb, inum) iget((sb), (inum))
163 static void yaffs_read_inode(struct inode *inode);
166 /* Call local equivalent */
167 #define YAFFS_USE_OWN_IGET
168 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
170 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
173 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
174 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
176 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
179 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
180 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
182 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
183 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
185 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
189 #define update_dir_time(dir) do {\
190 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
193 static void yaffs_put_super(struct super_block *sb);
195 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
197 static ssize_t yaffs_hold_space(struct file *f);
198 static void yaffs_release_space(struct file *f);
200 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
201 static int yaffs_file_flush(struct file *file, fl_owner_t id);
203 static int yaffs_file_flush(struct file *file);
206 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
207 static int yaffs_sync_object(struct file *file, int datasync);
209 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
213 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
215 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
216 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
217 struct nameidata *n);
218 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
219 struct nameidata *n);
221 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
222 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
224 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
225 struct dentry *dentry);
226 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
227 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
228 const char *symname);
229 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
231 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
232 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
235 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
238 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
239 struct inode *new_dir, struct dentry *new_dentry);
240 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
242 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
243 static int yaffs_sync_fs(struct super_block *sb, int wait);
244 static void yaffs_write_super(struct super_block *sb);
246 static int yaffs_sync_fs(struct super_block *sb);
247 static int yaffs_write_super(struct super_block *sb);
250 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
251 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
252 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
253 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
255 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
258 #ifdef YAFFS_HAS_PUT_INODE
259 static void yaffs_put_inode(struct inode *inode);
262 static int yaffs_readpage(struct file *file, struct page *page);
263 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
264 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
266 static int yaffs_writepage(struct page *page);
270 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
271 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
272 loff_t pos, unsigned len, unsigned flags,
273 struct page **pagep, void **fsdata);
274 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
275 loff_t pos, unsigned len, unsigned copied,
276 struct page *pg, void *fsdadata);
278 static int yaffs_prepare_write(struct file *f, struct page *pg,
279 unsigned offset, unsigned to);
280 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
285 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
287 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
288 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
290 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
292 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
294 static struct address_space_operations yaffs_file_address_operations = {
295 .readpage = yaffs_readpage,
296 .writepage = yaffs_writepage,
297 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
298 .write_begin = yaffs_write_begin,
299 .write_end = yaffs_write_end,
301 .prepare_write = yaffs_prepare_write,
302 .commit_write = yaffs_commit_write,
306 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
307 static const struct file_operations yaffs_file_operations = {
308 .read = do_sync_read,
309 .write = do_sync_write,
310 .aio_read = generic_file_aio_read,
311 .aio_write = generic_file_aio_write,
312 .mmap = generic_file_mmap,
313 .flush = yaffs_file_flush,
314 .fsync = yaffs_sync_object,
315 .splice_read = generic_file_splice_read,
316 .splice_write = generic_file_splice_write,
317 .llseek = generic_file_llseek,
320 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
322 static const struct file_operations yaffs_file_operations = {
323 .read = do_sync_read,
324 .write = do_sync_write,
325 .aio_read = generic_file_aio_read,
326 .aio_write = generic_file_aio_write,
327 .mmap = generic_file_mmap,
328 .flush = yaffs_file_flush,
329 .fsync = yaffs_sync_object,
330 .sendfile = generic_file_sendfile,
335 static const struct file_operations yaffs_file_operations = {
336 .read = generic_file_read,
337 .write = generic_file_write,
338 .mmap = generic_file_mmap,
339 .flush = yaffs_file_flush,
340 .fsync = yaffs_sync_object,
341 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
342 .sendfile = generic_file_sendfile,
347 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
348 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
350 void * kaddr = kmap_atomic(page, KM_USER0);
351 memset(kaddr + start, 0, end - start);
352 kunmap_atomic(kaddr, KM_USER0);
353 flush_dcache_page(page);
358 static const struct inode_operations yaffs_file_inode_operations = {
359 .setattr = yaffs_setattr,
362 static const struct inode_operations yaffs_symlink_inode_operations = {
363 .readlink = yaffs_readlink,
364 .follow_link = yaffs_follow_link,
365 .setattr = yaffs_setattr,
368 static const struct inode_operations yaffs_dir_inode_operations = {
369 .create = yaffs_create,
370 .lookup = yaffs_lookup,
372 .unlink = yaffs_unlink,
373 .symlink = yaffs_symlink,
374 .mkdir = yaffs_mkdir,
375 .rmdir = yaffs_unlink,
376 .mknod = yaffs_mknod,
377 .rename = yaffs_rename,
378 .setattr = yaffs_setattr,
381 static const struct file_operations yaffs_dir_operations = {
382 .read = generic_read_dir,
383 .readdir = yaffs_readdir,
384 .fsync = yaffs_sync_object,
385 .llseek = yaffs_dir_llseek,
388 static const struct super_operations yaffs_super_ops = {
389 .statfs = yaffs_statfs,
391 #ifndef YAFFS_USE_OWN_IGET
392 .read_inode = yaffs_read_inode,
394 #ifdef YAFFS_HAS_PUT_INODE
395 .put_inode = yaffs_put_inode,
397 .put_super = yaffs_put_super,
398 .sync_fs = yaffs_sync_fs,
399 .write_super = yaffs_write_super,
402 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
404 return yaffs_gc_control;
407 static void yaffs_GrossLock(yaffs_Device *dev)
409 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
410 down(&(yaffs_DeviceToContext(dev)->grossLock));
411 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
414 static void yaffs_GrossUnlock(yaffs_Device *dev)
416 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
417 up(&(yaffs_DeviceToContext(dev)->grossLock));
420 #ifdef YAFFS_COMPILE_EXPORTFS
422 static struct inode *
423 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
425 return Y_IGET(sb, ino);
428 static struct dentry *
429 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
431 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
434 static struct dentry *
435 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
437 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
440 struct dentry *yaffs2_get_parent(struct dentry *dentry)
443 struct super_block *sb = dentry->d_inode->i_sb;
444 struct dentry *parent = ERR_PTR(-ENOENT);
446 unsigned long parent_ino;
448 yaffs_Object *parent_obj;
450 d_obj = yaffs_InodeToObject(dentry->d_inode);
453 parent_obj = d_obj->parent;
455 parent_ino = yaffs_GetObjectInode(parent_obj);
456 inode = Y_IGET(sb, parent_ino);
459 parent = ERR_CAST(inode);
461 parent = d_obtain_alias(inode);
462 if (!IS_ERR(parent)) {
463 parent = ERR_PTR(-ENOMEM);
473 /* Just declare a zero structure as a NULL value implies
474 * using the default functions of exportfs.
477 static struct export_operations yaffs_export_ops =
479 .fh_to_dentry = yaffs2_fh_to_dentry,
480 .fh_to_parent = yaffs2_fh_to_parent,
481 .get_parent = yaffs2_get_parent,
486 /*-----------------------------------------------------------------*/
487 /* Directory search context allows us to unlock access to yaffs during
488 * filldir without causing problems with the directory being modified.
489 * This is similar to the tried and tested mechanism used in yaffs direct.
491 * A search context iterates along a doubly linked list of siblings in the
492 * directory. If the iterating object is deleted then this would corrupt
493 * the list iteration, likely causing a crash. The search context avoids
494 * this by using the removeObjectCallback to move the search context to the
495 * next object before the object is deleted.
497 * Many readdirs (and thus seach conexts) may be alive simulateously so
498 * each yaffs_Device has a list of these.
500 * A seach context lives for the duration of a readdir.
502 * All these functions must be called while yaffs is locked.
505 struct yaffs_SearchContext {
507 yaffs_Object *dirObj;
508 yaffs_Object *nextReturn;
509 struct ylist_head others;
513 * yaffs_NewSearch() creates a new search context, initialises it and
514 * adds it to the device's search context list.
516 * Called at start of readdir.
518 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
520 yaffs_Device *dev = dir->myDev;
521 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
525 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
526 sc->nextReturn = NULL;
528 sc->nextReturn = ylist_entry(
529 dir->variant.directoryVariant.children.next,
530 yaffs_Object,siblings);
531 YINIT_LIST_HEAD(&sc->others);
532 ylist_add(&sc->others,&(yaffs_DeviceToContext(dev)->searchContexts));
538 * yaffs_EndSearch() disposes of a search context and cleans up.
540 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
543 ylist_del(&sc->others);
549 * yaffs_SearchAdvance() moves a search context to the next object.
550 * Called when the search iterates or when an object removal causes
551 * the search context to be moved to the next object.
553 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
558 if( sc->nextReturn == NULL ||
559 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
560 sc->nextReturn = NULL;
562 struct ylist_head *next = sc->nextReturn->siblings.next;
564 if( next == &sc->dirObj->variant.directoryVariant.children)
565 sc->nextReturn = NULL; /* end of list */
567 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
572 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
573 * We check open search contexts and advance any which are currently
574 * on the object being iterated.
576 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
579 struct ylist_head *i;
580 struct yaffs_SearchContext *sc;
581 struct ylist_head *search_contexts = &(yaffs_DeviceToContext(obj->myDev)->searchContexts);
584 /* Iterate through the directory search contexts.
585 * If any are currently on the object being removed, then advance
586 * the search context to the next object to prevent a hanging pointer.
588 ylist_for_each(i, search_contexts) {
590 sc = ylist_entry(i, struct yaffs_SearchContext,others);
591 if(sc->nextReturn == obj)
592 yaffs_SearchAdvance(sc);
599 /*-----------------------------------------------------------------*/
601 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
604 unsigned char *alias;
607 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
609 yaffs_GrossLock(dev);
611 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
613 yaffs_GrossUnlock(dev);
618 ret = vfs_readlink(dentry, buffer, buflen, alias);
623 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
624 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
626 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
629 unsigned char *alias;
631 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
633 yaffs_GrossLock(dev);
634 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 33))
635 alias = (yaffs_DentryToObject(dentry))->variant.symLinkVariant.alias;
637 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
640 yaffs_GrossUnlock(dev);
646 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 33))
647 nd_set_link(nd, alias);
650 ret = vfs_follow_link(nd, alias);
654 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
661 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
665 * Lookup is used to find objects in the fs
667 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
669 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
672 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
676 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
678 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
680 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
681 yaffs_GrossLock(dev);
684 (TSTR("yaffs_lookup for %d:%s\n"),
685 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
687 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
688 dentry->d_name.name);
690 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
692 /* Can't hold gross lock when calling yaffs_get_inode() */
693 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
694 yaffs_GrossUnlock(dev);
698 (TSTR("yaffs_lookup found %d\n"), obj->objectId));
700 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
704 (TSTR("yaffs_loookup dentry \n")));
705 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
706 * d_add even if NULL inode */
708 /*dget(dentry); // try to solve directory bug */
709 d_add(dentry, inode);
717 T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
721 /* added NCB for 2.5/6 compatability - forces add even if inode is
722 * NULL which creates dentry hash */
723 d_add(dentry, inode);
729 #ifdef YAFFS_HAS_PUT_INODE
731 /* For now put inode is just for debugging
732 * Put inode is called when the inode **structure** is put.
734 static void yaffs_put_inode(struct inode *inode)
737 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
738 atomic_read(&inode->i_count)));
744 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
745 static int yaffs_file_flush(struct file *file, fl_owner_t id)
747 static int yaffs_file_flush(struct file *file)
750 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
752 yaffs_Device *dev = obj->myDev;
755 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->objectId,
756 obj->dirty ? "dirty" : "clean"));
758 yaffs_GrossLock(dev);
760 yaffs_FlushFile(obj, 1, 0);
762 yaffs_GrossUnlock(dev);
767 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
769 /* Lifted from jffs2 */
772 unsigned char *pg_buf;
778 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
779 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
780 (unsigned)PAGE_CACHE_SIZE));
782 obj = yaffs_DentryToObject(f->f_dentry);
786 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
787 BUG_ON(!PageLocked(pg));
794 /* FIXME: Can kmap fail? */
796 yaffs_GrossLock(dev);
798 ret = yaffs_ReadDataFromFile(obj, pg_buf,
799 pg->index << PAGE_CACHE_SHIFT,
802 yaffs_GrossUnlock(dev);
808 ClearPageUptodate(pg);
815 flush_dcache_page(pg);
818 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
822 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
824 int ret = yaffs_readpage_nolock(f, pg);
829 static int yaffs_readpage(struct file *f, struct page *pg)
833 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
834 ret=yaffs_readpage_unlock(f, pg);
835 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
839 /* writepage inspired by/stolen from smbfs */
841 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
842 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
844 static int yaffs_writepage(struct page *page)
847 struct address_space *mapping = page->mapping;
849 unsigned long end_index;
858 inode = mapping->host;
861 i_size = i_size_read(inode);
863 end_index = i_size >> PAGE_CACHE_SHIFT;
865 if(page->index < end_index)
866 nBytes = PAGE_CACHE_SIZE;
868 nBytes = i_size & (PAGE_CACHE_SIZE -1);
870 if (page->index > end_index || !nBytes) {
872 (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
873 (unsigned)(page->index << PAGE_CACHE_SHIFT),
874 (unsigned)inode->i_size));
876 (TSTR(" -> don't care!!\n")));
878 zero_user_segment(page,0,PAGE_CACHE_SIZE);
879 set_page_writeback(page);
881 end_page_writeback(page);
886 if(nBytes != PAGE_CACHE_SIZE)
887 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
893 obj = yaffs_InodeToObject(inode);
894 yaffs_GrossLock(obj->myDev);
897 (TSTR("yaffs_writepage at %08x, size %08x\n"),
898 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
900 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
901 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
903 nWritten = yaffs_WriteDataToFile(obj, buffer,
904 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
907 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
908 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
910 yaffs_GrossUnlock(obj->myDev);
913 set_page_writeback(page);
915 end_page_writeback(page);
918 return (nWritten == nBytes) ? 0 : -ENOSPC;
922 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
923 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
924 loff_t pos, unsigned len, unsigned flags,
925 struct page **pagep, void **fsdata)
927 struct page *pg = NULL;
928 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
934 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
935 pg = grab_cache_page_write_begin(mapping, index, flags);
937 pg = __grab_cache_page(mapping, index);
946 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
947 (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
950 space_held = yaffs_hold_space(filp);
957 /* Update page if required */
959 if (!Page_Uptodate(pg))
960 ret = yaffs_readpage_nolock(filp, pg);
965 /* Happy path return */
966 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
972 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
974 yaffs_release_space(filp);
977 page_cache_release(pg);
984 static int yaffs_prepare_write(struct file *f, struct page *pg,
985 unsigned offset, unsigned to)
987 T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
989 if (!Page_Uptodate(pg))
990 return yaffs_readpage_nolock(f, pg);
995 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
996 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
997 loff_t pos, unsigned len, unsigned copied,
998 struct page *pg, void *fsdadata)
1002 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1005 addr = kva + offset_into_page;
1008 ("yaffs_write_end addr %p pos %x nBytes %d\n",
1009 addr,(unsigned)pos, copied));
1011 ret = yaffs_file_write(filp, addr, copied, &pos);
1013 if (ret != copied) {
1015 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
1024 yaffs_release_space(filp);
1026 page_cache_release(pg);
1031 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1036 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1037 int nBytes = to - offset;
1040 unsigned spos = pos;
1044 addr = kva + offset;
1046 saddr = (unsigned) addr;
1049 (TSTR("yaffs_commit_write addr %x pos %x nBytes %d\n"),
1050 saddr, spos, nBytes));
1052 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1054 if (nWritten != nBytes) {
1056 (TSTR("yaffs_commit_write not same size nWritten %d nBytes %d\n"),
1066 (TSTR("yaffs_commit_write returning %d\n"),
1067 nWritten == nBytes ? 0 : nWritten));
1069 return nWritten == nBytes ? 0 : nWritten;
1074 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1079 /* Check mode against the variant type and attempt to repair if broken. */
1080 __u32 mode = obj->yst_mode;
1081 switch (obj->variantType) {
1082 case YAFFS_OBJECT_TYPE_FILE:
1083 if (!S_ISREG(mode)) {
1084 obj->yst_mode &= ~S_IFMT;
1085 obj->yst_mode |= S_IFREG;
1089 case YAFFS_OBJECT_TYPE_SYMLINK:
1090 if (!S_ISLNK(mode)) {
1091 obj->yst_mode &= ~S_IFMT;
1092 obj->yst_mode |= S_IFLNK;
1096 case YAFFS_OBJECT_TYPE_DIRECTORY:
1097 if (!S_ISDIR(mode)) {
1098 obj->yst_mode &= ~S_IFMT;
1099 obj->yst_mode |= S_IFDIR;
1103 case YAFFS_OBJECT_TYPE_UNKNOWN:
1104 case YAFFS_OBJECT_TYPE_HARDLINK:
1105 case YAFFS_OBJECT_TYPE_SPECIAL:
1111 inode->i_flags |= S_NOATIME;
1113 inode->i_ino = obj->objectId;
1114 inode->i_mode = obj->yst_mode;
1115 inode->i_uid = obj->yst_uid;
1116 inode->i_gid = obj->yst_gid;
1117 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1118 inode->i_blksize = inode->i_sb->s_blocksize;
1120 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1122 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1123 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1124 inode->i_atime.tv_nsec = 0;
1125 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1126 inode->i_mtime.tv_nsec = 0;
1127 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1128 inode->i_ctime.tv_nsec = 0;
1130 inode->i_rdev = obj->yst_rdev;
1131 inode->i_atime = obj->yst_atime;
1132 inode->i_mtime = obj->yst_mtime;
1133 inode->i_ctime = obj->yst_ctime;
1135 inode->i_size = yaffs_GetObjectFileLength(obj);
1136 inode->i_blocks = (inode->i_size + 511) >> 9;
1138 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1141 (TSTR("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n"),
1142 inode->i_mode, inode->i_uid, inode->i_gid,
1143 (int)inode->i_size, atomic_read(&inode->i_count)));
1145 switch (obj->yst_mode & S_IFMT) {
1146 default: /* fifo, device or socket */
1147 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1148 init_special_inode(inode, obj->yst_mode,
1149 old_decode_dev(obj->yst_rdev));
1151 init_special_inode(inode, obj->yst_mode,
1152 (dev_t) (obj->yst_rdev));
1155 case S_IFREG: /* file */
1156 inode->i_op = &yaffs_file_inode_operations;
1157 inode->i_fop = &yaffs_file_operations;
1158 inode->i_mapping->a_ops =
1159 &yaffs_file_address_operations;
1161 case S_IFDIR: /* directory */
1162 inode->i_op = &yaffs_dir_inode_operations;
1163 inode->i_fop = &yaffs_dir_operations;
1165 case S_IFLNK: /* symlink */
1166 inode->i_op = &yaffs_symlink_inode_operations;
1170 yaffs_InodeToObjectLV(inode) = obj;
1172 obj->myInode = inode;
1176 (TSTR("yaffs_FileInode invalid parameters\n")));
1181 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1184 struct inode *inode;
1188 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1195 (TSTR("yaffs_get_inode for NULL object!!\n")));
1201 (TSTR("yaffs_get_inode for object %d\n"), obj->objectId));
1203 inode = Y_IGET(sb, obj->objectId);
1207 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1208 /* iget also increments the inode's i_count */
1209 /* NB You can't be holding grossLock or deadlock will happen! */
1214 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1219 struct inode *inode;
1222 obj = yaffs_DentryToObject(f->f_dentry);
1226 yaffs_GrossLock(dev);
1228 inode = f->f_dentry->d_inode;
1230 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1231 ipos = inode->i_size;
1237 (TSTR("yaffs_file_write: hey obj is null!\n")));
1240 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1241 "to object %d at %d(%x)\n"),
1242 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1244 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1247 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1248 (unsigned )n,(unsigned)n));
1253 if (ipos > inode->i_size) {
1254 inode->i_size = ipos;
1255 inode->i_blocks = (ipos + 511) >> 9;
1258 (TSTR("yaffs_file_write size updated to %d bytes, "
1260 ipos, (int)(inode->i_blocks)));
1264 yaffs_GrossUnlock(dev);
1265 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1268 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1269 /* For now we just assume few parallel writes and check against a small number. */
1270 /* Todo: need to do this with a counter to handle parallel reads better */
1272 static ssize_t yaffs_hold_space(struct file *f)
1280 obj = yaffs_DentryToObject(f->f_dentry);
1284 yaffs_GrossLock(dev);
1286 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1288 yaffs_GrossUnlock(dev);
1290 return (nFreeChunks > 20) ? 1 : 0;
1293 static void yaffs_release_space(struct file *f)
1299 obj = yaffs_DentryToObject(f->f_dentry);
1303 yaffs_GrossLock(dev);
1306 yaffs_GrossUnlock(dev);
1310 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1318 offset += i_size_read(file->f_path.dentry->d_inode);
1321 offset += file->f_pos;
1326 if (offset != file->f_pos)
1327 file->f_pos = offset;
1336 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1340 struct yaffs_SearchContext *sc;
1341 struct inode *inode = f->f_dentry->d_inode;
1342 unsigned long offset, curoffs;
1346 char name[YAFFS_MAX_NAME_LENGTH + 1];
1348 obj = yaffs_DentryToObject(f->f_dentry);
1351 yaffs_GrossLock(dev);
1353 yaffs_DeviceToContext(dev)->readdirProcess = current;
1357 sc = yaffs_NewSearch(obj);
1363 T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1367 (TSTR("yaffs_readdir: entry . ino %d \n"),
1368 (int)inode->i_ino));
1369 yaffs_GrossUnlock(dev);
1370 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
1372 yaffs_GrossLock(dev);
1378 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1379 (int)f->f_dentry->d_parent->d_inode->i_ino));
1380 yaffs_GrossUnlock(dev);
1381 if (filldir(dirent, "..", 2, offset,
1382 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
1384 yaffs_GrossLock(dev);
1391 /* If the directory has changed since the open or last call to
1392 readdir, rewind to after the 2 canned entries. */
1393 if (f->f_version != inode->i_version) {
1396 f->f_version = inode->i_version;
1399 while(sc->nextReturn){
1402 if (curoffs >= offset) {
1403 int this_inode = yaffs_GetObjectInode(l);
1404 int this_type = yaffs_GetObjectType(l);
1406 yaffs_GetObjectName(l, name,
1407 YAFFS_MAX_NAME_LENGTH + 1);
1409 (TSTR("yaffs_readdir: %s inode %d\n"),
1410 name, yaffs_GetObjectInode(l)));
1412 yaffs_GrossUnlock(dev);
1422 yaffs_GrossLock(dev);
1427 yaffs_SearchAdvance(sc);
1431 yaffs_DeviceToContext(dev)->readdirProcess = NULL;
1433 yaffs_GrossUnlock(dev);
1435 yaffs_EndSearch(sc);
1443 * File creation. Allocate an inode, and we're done..
1446 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1449 #define YCRED(x) (x->cred)
1452 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1453 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1456 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1460 struct inode *inode;
1462 yaffs_Object *obj = NULL;
1465 yaffs_Object *parent = yaffs_InodeToObject(dir);
1467 int error = -ENOSPC;
1468 uid_t uid = YCRED(current)->fsuid;
1469 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1471 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1476 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1477 parent->objectId, parent->variantType));
1480 (TSTR("yaffs_mknod: could not get parent object\n")));
1484 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1485 "mode %x dev %x\n"),
1486 dentry->d_name.name, mode, rdev));
1488 dev = parent->myDev;
1490 yaffs_GrossLock(dev);
1492 switch (mode & S_IFMT) {
1494 /* Special (socket, fifo, device...) */
1495 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1496 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1497 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1498 gid, old_encode_dev(rdev));
1500 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1504 case S_IFREG: /* file */
1505 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1506 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1509 case S_IFDIR: /* directory */
1511 (TSTR("yaffs_mknod: making directory\n")));
1512 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1515 case S_IFLNK: /* symlink */
1516 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1517 obj = NULL; /* Do we ever get here? */
1521 /* Can not call yaffs_get_inode() with gross lock held */
1522 yaffs_GrossUnlock(dev);
1525 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1526 d_instantiate(dentry, inode);
1527 update_dir_time(dir);
1529 (TSTR("yaffs_mknod created object %d count = %d\n"),
1530 obj->objectId, atomic_read(&inode->i_count)));
1532 yaffs_FillInodeFromObject(dir,parent);
1535 (TSTR("yaffs_mknod failed making object\n")));
1542 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1545 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1546 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1550 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1551 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1552 struct nameidata *n)
1554 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1557 T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1558 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1561 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1569 (TSTR("yaffs_unlink %d:%s\n"),
1571 dentry->d_name.name));
1572 obj = yaffs_InodeToObject(dir);
1575 yaffs_GrossLock(dev);
1577 retVal = yaffs_Unlink(obj, dentry->d_name.name);
1579 if (retVal == YAFFS_OK) {
1580 dentry->d_inode->i_nlink--;
1582 yaffs_GrossUnlock(dev);
1583 mark_inode_dirty(dentry->d_inode);
1584 update_dir_time(dir);
1587 yaffs_GrossUnlock(dev);
1594 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1595 struct dentry *dentry)
1597 struct inode *inode = old_dentry->d_inode;
1598 yaffs_Object *obj = NULL;
1599 yaffs_Object *link = NULL;
1602 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1604 obj = yaffs_InodeToObject(inode);
1607 yaffs_GrossLock(dev);
1609 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1610 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1614 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1615 d_instantiate(dentry, old_dentry->d_inode);
1616 atomic_inc(&old_dentry->d_inode->i_count);
1618 (TSTR("yaffs_link link count %d i_count %d\n"),
1619 old_dentry->d_inode->i_nlink,
1620 atomic_read(&old_dentry->d_inode->i_count)));
1623 yaffs_GrossUnlock(dev);
1626 update_dir_time(dir);
1633 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1634 const char *symname)
1638 uid_t uid = YCRED(current)->fsuid;
1639 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1641 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1643 dev = yaffs_InodeToObject(dir)->myDev;
1644 yaffs_GrossLock(dev);
1645 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1646 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1647 yaffs_GrossUnlock(dev);
1650 struct inode *inode;
1652 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1653 d_instantiate(dentry, inode);
1654 update_dir_time(dir);
1655 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1658 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1664 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1665 static int yaffs_sync_object(struct file *file, int datasync)
1667 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1674 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1675 struct dentry *dentry = file->f_path.dentry;
1678 obj = yaffs_DentryToObject(dentry);
1682 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1683 (TSTR("yaffs_sync_object\n")));
1684 yaffs_GrossLock(dev);
1685 yaffs_FlushFile(obj, 1, datasync);
1686 yaffs_GrossUnlock(dev);
1691 * The VFS layer already does all the dentry stuff for rename.
1693 * NB: POSIX says you can rename an object over an old object of the same name
1695 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1696 struct inode *new_dir, struct dentry *new_dentry)
1699 int retVal = YAFFS_FAIL;
1700 yaffs_Object *target;
1702 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1703 dev = yaffs_InodeToObject(old_dir)->myDev;
1705 yaffs_GrossLock(dev);
1707 /* Check if the target is an existing directory that is not empty. */
1708 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1709 new_dentry->d_name.name);
1713 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1714 !ylist_empty(&target->variant.directoryVariant.children)) {
1716 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1718 retVal = YAFFS_FAIL;
1720 /* Now does unlinking internally using shadowing mechanism */
1721 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_RenameObject\n")));
1723 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1724 old_dentry->d_name.name,
1725 yaffs_InodeToObject(new_dir),
1726 new_dentry->d_name.name);
1728 yaffs_GrossUnlock(dev);
1730 if (retVal == YAFFS_OK) {
1732 new_dentry->d_inode->i_nlink--;
1733 mark_inode_dirty(new_dentry->d_inode);
1736 update_dir_time(old_dir);
1737 if(old_dir != new_dir)
1738 update_dir_time(new_dir);
1745 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1747 struct inode *inode = dentry->d_inode;
1752 (TSTR("yaffs_setattr of object %d\n"),
1753 yaffs_InodeToObject(inode)->objectId));
1755 /* Fail if a requested resize >= 2GB */
1756 if (attr->ia_valid & ATTR_SIZE &&
1757 (attr->ia_size >> 31))
1761 error = inode_change_ok(inode, attr);
1765 // error = inode_setattr(inode, attr);
1766 T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
1767 if (attr->ia_valid & ATTR_SIZE)
1768 truncate_inode_pages(&inode->i_data,attr->ia_size);
1770 dev = yaffs_InodeToObject(inode)->myDev;
1771 if (attr->ia_valid & ATTR_SIZE){
1772 T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
1773 (int)(attr->ia_size),(int)(attr->ia_size)));
1775 yaffs_GrossLock(dev);
1776 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1777 if(result == YAFFS_OK) {
1782 yaffs_GrossUnlock(dev);
1787 (TSTR("yaffs_setattr done returning %d\n"),error));
1792 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1793 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1795 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1796 struct super_block *sb = dentry->d_sb;
1797 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1798 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1800 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1802 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1804 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1807 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
1809 yaffs_GrossLock(dev);
1811 buf->f_type = YAFFS_MAGIC;
1812 buf->f_bsize = sb->s_blocksize;
1813 buf->f_namelen = 255;
1815 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1816 /* Do this if chunk size is not a power of 2 */
1818 uint64_t bytesInDev;
1821 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
1822 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
1824 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1825 buf->f_blocks = bytesInDev;
1827 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1828 ((uint64_t)(dev->nDataBytesPerChunk));
1830 do_div(bytesFree, sb->s_blocksize);
1832 buf->f_bfree = bytesFree;
1834 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1837 (dev->param.endBlock - dev->param.startBlock + 1) *
1838 dev->param.nChunksPerBlock /
1839 (sb->s_blocksize / dev->nDataBytesPerChunk);
1841 yaffs_GetNumberOfFreeChunks(dev) /
1842 (sb->s_blocksize / dev->nDataBytesPerChunk);
1845 (dev->param.endBlock - dev->param.startBlock + 1) *
1846 dev->param.nChunksPerBlock *
1847 (dev->nDataBytesPerChunk / sb->s_blocksize);
1850 yaffs_GetNumberOfFreeChunks(dev) *
1851 (dev->nDataBytesPerChunk / sb->s_blocksize);
1856 buf->f_bavail = buf->f_bfree;
1858 yaffs_GrossUnlock(dev);
1864 static void yaffs_FlushInodes(struct super_block *sb)
1869 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1870 obj = yaffs_InodeToObject(iptr);
1872 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
1874 yaffs_FlushFile(obj,1,0);
1880 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
1882 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1886 yaffs_FlushInodes(sb);
1887 yaffs_UpdateDirtyDirectories(dev);
1888 yaffs_FlushEntireDeviceCache(dev);
1890 yaffs_CheckpointSave(dev);
1894 static unsigned yaffs_bg_gc_urgency(yaffs_Device *dev)
1896 unsigned erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
1897 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
1898 unsigned scatteredFree = 0; /* Free chunks not in an erased block */
1900 if(erasedChunks < dev->nFreeChunks)
1901 scatteredFree = (dev->nFreeChunks - erasedChunks);
1903 if(!context->bgRunning)
1905 else if(scatteredFree < (dev->param.nChunksPerBlock * 2))
1907 else if(erasedChunks > dev->nFreeChunks/2)
1909 else if(erasedChunks > dev->nFreeChunks/4)
1915 static int yaffs_do_sync_fs(struct super_block *sb,
1916 int request_checkpoint)
1919 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1920 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
1921 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
1924 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1925 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
1927 sb->s_dirt ? "dirty" : "clean",
1928 request_checkpoint ? "checkpoint requested" : "no checkpoint",
1929 oneshot_checkpoint ? " one-shot" : "" ));
1931 yaffs_GrossLock(dev);
1932 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
1933 oneshot_checkpoint) &&
1934 !dev->isCheckpointed;
1936 if (sb->s_dirt || do_checkpoint) {
1937 yaffs_FlushSuperBlock(sb, !dev->isCheckpointed && do_checkpoint);
1939 if(oneshot_checkpoint)
1940 yaffs_auto_checkpoint &= ~4;
1942 yaffs_GrossUnlock(dev);
1948 * yaffs background thread functions .
1949 * yaffs_BackgroundThread() the thread function
1950 * yaffs_BackgroundStart() launches the background thread.
1951 * yaffs_BackgroundStop() cleans up the background thread.
1954 * The thread should only run after the yaffs is initialised
1955 * The thread should be stopped before yaffs is unmounted.
1956 * The thread should not do any writing while the fs is in read only.
1959 #ifdef YAFFS_COMPILE_BACKGROUND
1961 void yaffs_background_waker(unsigned long data)
1963 wake_up_process((struct task_struct *)data);
1966 static int yaffs_BackgroundThread(void *data)
1968 yaffs_Device *dev = (yaffs_Device *)data;
1969 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
1970 unsigned long now = jiffies;
1971 unsigned long next_dir_update = now;
1972 unsigned long next_gc = now;
1973 unsigned long expires;
1974 unsigned int urgency;
1977 struct timer_list timer;
1979 T(YAFFS_TRACE_BACKGROUND,
1980 (TSTR("yaffs_background starting for dev %p\n"),
1985 while(context->bgRunning){
1986 T(YAFFS_TRACE_BACKGROUND,
1987 (TSTR("yaffs_background\n")));
1989 if(kthread_should_stop())
1995 yaffs_GrossLock(dev);
1999 if(time_after(now, next_dir_update)){
2000 yaffs_UpdateDirtyDirectories(dev);
2001 next_dir_update = now + HZ;
2004 if(time_after(now,next_gc)){
2005 if(!dev->isCheckpointed){
2006 urgency = yaffs_bg_gc_urgency(dev);
2007 gcResult = yaffs_BackgroundGarbageCollect(dev, urgency);
2009 next_gc = now + HZ/20+1;
2010 else if(urgency > 0)
2011 next_gc = now + HZ/10+1;
2013 next_gc = now + HZ * 2;
2015 * gc not running so set to next_dir_update
2016 * to cut down on wake ups
2018 next_gc = next_dir_update;
2020 yaffs_GrossUnlock(dev);
2022 expires = next_dir_update;
2023 if (time_before(next_gc,expires))
2025 if(time_before(expires,now))
2028 init_timer_on_stack(&timer);
2029 timer.expires = expires+1;
2030 timer.data = (unsigned long) current;
2031 timer.function = yaffs_background_waker;
2033 set_current_state(TASK_INTERRUPTIBLE);
2036 del_timer_sync(&timer);
2045 static int yaffs_BackgroundStart(yaffs_Device *dev)
2048 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
2050 context->bgRunning = 1;
2052 context->bgThread = kthread_run(yaffs_BackgroundThread,
2053 (void *)dev,"yaffs-bg");
2055 if(IS_ERR(context->bgThread)){
2056 retval = PTR_ERR(context->bgThread);
2057 context->bgThread = NULL;
2058 context->bgRunning = 0;
2063 static void yaffs_BackgroundStop(yaffs_Device *dev)
2065 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToContext(dev);
2067 ctxt->bgRunning = 0;
2069 if( ctxt->bgThread){
2070 kthread_stop(ctxt->bgThread);
2071 ctxt->bgThread = NULL;
2075 static int yaffs_BackgroundThread(void *data)
2080 static int yaffs_BackgroundStart(yaffs_Device *dev)
2085 static void yaffs_BackgroundStop(yaffs_Device *dev)
2091 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2092 static void yaffs_write_super(struct super_block *sb)
2094 static int yaffs_write_super(struct super_block *sb)
2097 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2099 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2100 (TSTR("yaffs_write_super%s\n"),
2101 request_checkpoint ? " checkpt" : ""));
2103 yaffs_do_sync_fs(sb, request_checkpoint);
2105 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2111 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2112 static int yaffs_sync_fs(struct super_block *sb, int wait)
2114 static int yaffs_sync_fs(struct super_block *sb)
2117 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2119 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2120 (TSTR("yaffs_sync_fs%s\n"),
2121 request_checkpoint ? " checkpt" : ""));
2123 yaffs_do_sync_fs(sb, request_checkpoint);
2128 #ifdef YAFFS_USE_OWN_IGET
2130 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2132 struct inode *inode;
2134 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2137 (TSTR("yaffs_iget for %lu\n"), ino));
2139 inode = iget_locked(sb, ino);
2141 return ERR_PTR(-ENOMEM);
2142 if (!(inode->i_state & I_NEW))
2145 /* NB This is called as a side effect of other functions, but
2146 * we had to release the lock to prevent deadlocks, so
2147 * need to lock again.
2150 yaffs_GrossLock(dev);
2152 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2154 yaffs_FillInodeFromObject(inode, obj);
2156 yaffs_GrossUnlock(dev);
2158 unlock_new_inode(inode);
2164 static void yaffs_read_inode(struct inode *inode)
2166 /* NB This is called as a side effect of other functions, but
2167 * we had to release the lock to prevent deadlocks, so
2168 * need to lock again.
2172 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2175 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2177 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
2178 yaffs_GrossLock(dev);
2180 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2182 yaffs_FillInodeFromObject(inode, obj);
2184 if(current != yaffs_DeviceToContext(dev)->readdirProcess)
2185 yaffs_GrossUnlock(dev);
2190 static YLIST_HEAD(yaffs_context_list);
2191 struct semaphore yaffs_context_lock;
2193 #if 0 /* not used */
2194 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2196 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2198 if (*flags & MS_RDONLY) {
2199 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2202 (TSTR("yaffs_remount_fs: %s: RO\n"), dev->name));
2204 yaffs_GrossLock(dev);
2206 yaffs_FlushSuperBlock(sb,1);
2211 yaffs_GrossUnlock(dev);
2214 (TSTR("yaffs_remount_fs: %s: RW\n"), dev->name));
2221 static void yaffs_put_super(struct super_block *sb)
2223 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2225 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2227 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2228 (TSTR("Shutting down yaffs background thread\n")));
2229 yaffs_BackgroundStop(dev);
2230 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2231 (TSTR("yaffs background thread shut down\n")));
2233 yaffs_GrossLock(dev);
2235 yaffs_FlushSuperBlock(sb,1);
2237 if (yaffs_DeviceToContext(dev)->putSuperFunc)
2238 yaffs_DeviceToContext(dev)->putSuperFunc(sb);
2241 yaffs_Deinitialise(dev);
2243 yaffs_GrossUnlock(dev);
2245 down(&yaffs_context_lock);
2246 ylist_del_init(&(yaffs_DeviceToContext(dev)->contextList));
2247 up(&yaffs_context_lock);
2249 if (yaffs_DeviceToContext(dev)->spareBuffer) {
2250 YFREE(yaffs_DeviceToContext(dev)->spareBuffer);
2251 yaffs_DeviceToContext(dev)->spareBuffer = NULL;
2258 static void yaffs_MTDPutSuper(struct super_block *sb)
2260 struct mtd_info *mtd = yaffs_DeviceToContext(yaffs_SuperToDevice(sb))->mtd;
2265 put_mtd_device(mtd);
2269 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2271 struct super_block *sb = yaffs_DeviceToContext(dev)->superBlock;
2273 T(YAFFS_TRACE_OS, (TSTR("yaffs_MarkSuperBlockDirty() sb = %p\n"), sb));
2280 int skip_checkpoint_read;
2281 int skip_checkpoint_write;
2284 int tags_ecc_overridden;
2285 int lazy_loading_enabled;
2286 int lazy_loading_overridden;
2287 int empty_lost_and_found;
2288 int empty_lost_and_found_overridden;
2291 #define MAX_OPT_LEN 30
2292 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2294 char cur_opt[MAX_OPT_LEN + 1];
2298 /* Parse through the options which is a comma seperated list */
2300 while (options_str && *options_str && !error) {
2301 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2304 while(*options_str == ',')
2307 while (*options_str && *options_str != ',') {
2308 if (p < MAX_OPT_LEN) {
2309 cur_opt[p] = *options_str;
2315 if (!strcmp(cur_opt, "inband-tags"))
2316 options->inband_tags = 1;
2317 else if (!strcmp(cur_opt, "tags-ecc-off")){
2318 options->tags_ecc_on = 0;
2319 options->tags_ecc_overridden=1;
2320 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2321 options->tags_ecc_on = 1;
2322 options->tags_ecc_overridden = 1;
2323 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2324 options->lazy_loading_enabled = 0;
2325 options->lazy_loading_overridden=1;
2326 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2327 options->lazy_loading_enabled = 1;
2328 options->lazy_loading_overridden = 1;
2329 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2330 options->empty_lost_and_found = 0;
2331 options->empty_lost_and_found_overridden=1;
2332 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2333 options->empty_lost_and_found = 1;
2334 options->empty_lost_and_found_overridden=1;
2335 } else if (!strcmp(cur_opt, "no-cache"))
2336 options->no_cache = 1;
2337 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2338 options->skip_checkpoint_read = 1;
2339 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2340 options->skip_checkpoint_write = 1;
2341 else if (!strcmp(cur_opt, "no-checkpoint")) {
2342 options->skip_checkpoint_read = 1;
2343 options->skip_checkpoint_write = 1;
2345 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2354 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2355 struct super_block *sb,
2356 void *data, int silent)
2359 struct inode *inode = NULL;
2360 struct dentry *root;
2361 yaffs_Device *dev = 0;
2362 char devname_buf[BDEVNAME_SIZE + 1];
2363 struct mtd_info *mtd;
2365 char *data_str = (char *)data;
2366 struct yaffs_LinuxContext *context = NULL;
2367 yaffs_DeviceParam *param;
2369 yaffs_options options;
2371 sb->s_magic = YAFFS_MAGIC;
2372 sb->s_op = &yaffs_super_ops;
2373 sb->s_flags |= MS_NOATIME;
2375 #ifdef YAFFS_COMPILE_EXPORTFS
2376 sb->s_export_op = &yaffs_export_ops;
2380 printk(KERN_INFO "yaffs: sb is NULL\n");
2381 else if (!sb->s_dev)
2382 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2383 else if (!yaffs_devname(sb, devname_buf))
2384 printk(KERN_INFO "yaffs: devname is NULL\n");
2386 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2388 yaffs_devname(sb, devname_buf));
2393 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2395 memset(&options, 0, sizeof(options));
2397 if (yaffs_parse_options(&options, data_str)) {
2398 /* Option parsing failed */
2403 sb->s_blocksize = PAGE_CACHE_SIZE;
2404 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2407 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffsVersion));
2409 (TSTR("yaffs_read_super: block size %d\n"),
2410 (int)(sb->s_blocksize)));
2412 T(YAFFS_TRACE_ALWAYS,
2413 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2414 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2415 yaffs_devname(sb, devname_buf)));
2417 /* Check it's an mtd device..... */
2418 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2419 return NULL; /* This isn't an mtd device */
2421 /* Get the device */
2422 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2424 T(YAFFS_TRACE_ALWAYS,
2425 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2429 /* Check it's NAND */
2430 if (mtd->type != MTD_NANDFLASH) {
2431 T(YAFFS_TRACE_ALWAYS,
2432 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2437 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2438 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2439 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2440 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2441 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2442 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2443 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2444 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2445 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2446 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2447 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2448 T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2450 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2453 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2455 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2456 T(YAFFS_TRACE_ALWAYS,
2457 (TSTR("yaffs: auto selecting yaffs2\n")));
2461 /* Added NCB 26/5/2006 for completeness */
2462 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2463 T(YAFFS_TRACE_ALWAYS,
2464 (TSTR("yaffs: auto selecting yaffs1\n")));
2470 if (yaffsVersion == 2) {
2471 /* Check for version 2 style functions */
2473 !mtd->block_isbad ||
2474 !mtd->block_markbad ||
2477 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2478 !mtd->read_oob || !mtd->write_oob) {
2481 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2483 T(YAFFS_TRACE_ALWAYS,
2484 (TSTR("yaffs: MTD device does not support required "
2489 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2490 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2491 !options.inband_tags) {
2492 T(YAFFS_TRACE_ALWAYS,
2493 (TSTR("yaffs: MTD device does not have the "
2494 "right page sizes\n")));
2498 /* Check for V1 style functions */
2502 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2503 !mtd->read_oob || !mtd->write_oob) {
2506 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2508 T(YAFFS_TRACE_ALWAYS,
2509 (TSTR("yaffs: MTD device does not support required "
2514 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2515 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2516 T(YAFFS_TRACE_ALWAYS,
2517 (TSTR("yaffs: MTD device does not support have the "
2518 "right page sizes\n")));
2523 /* OK, so if we got here, we have an MTD that's NAND and looks
2524 * like it has the right capabilities
2525 * Set the yaffs_Device up for mtd
2528 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2529 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2531 if(!dev || !context ){
2541 /* Deep shit could not allocate device structure */
2542 T(YAFFS_TRACE_ALWAYS,
2543 (TSTR("yaffs_read_super: Failed trying to allocate "
2544 "yaffs_Device. \n")));
2547 memset(dev, 0, sizeof(yaffs_Device));
2548 param = &(dev->param);
2550 memset(context,0,sizeof(struct yaffs_LinuxContext));
2551 dev->context = context;
2552 YINIT_LIST_HEAD(&(context->contextList));
2554 context->superBlock = sb;
2558 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2559 sb->s_fs_info = dev;
2561 sb->u.generic_sbp = dev;
2564 yaffs_DeviceToContext(dev)->mtd = mtd;
2565 param->name = mtd->name;
2567 /* Set up the memory size parameters.... */
2569 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2571 param->startBlock = 0;
2572 param->endBlock = nBlocks - 1;
2573 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2574 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2575 param->nReservedBlocks = 5;
2576 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2577 param->inbandTags = options.inband_tags;
2579 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2580 param->disableLazyLoad = 1;
2582 if(options.lazy_loading_overridden)
2583 param->disableLazyLoad = !options.lazy_loading_enabled;
2585 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2586 param->noTagsECC = 1;
2589 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2591 param->deferDirectoryUpdate = 1;
2594 if(options.tags_ecc_overridden)
2595 param->noTagsECC = !options.tags_ecc_on;
2597 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2598 param->emptyLostAndFound = 1;
2601 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2602 param->refreshPeriod = 0;
2604 param->refreshPeriod = 500;
2607 if(options.empty_lost_and_found_overridden)
2608 param->emptyLostAndFound = options.empty_lost_and_found;
2610 /* ... and the functions. */
2611 if (yaffsVersion == 2) {
2612 param->writeChunkWithTagsToNAND =
2613 nandmtd2_WriteChunkWithTagsToNAND;
2614 param->readChunkWithTagsFromNAND =
2615 nandmtd2_ReadChunkWithTagsFromNAND;
2616 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2617 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2618 yaffs_DeviceToContext(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2619 param->isYaffs2 = 1;
2620 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2621 param->totalBytesPerChunk = mtd->writesize;
2622 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2624 param->totalBytesPerChunk = mtd->oobblock;
2625 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2627 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2629 param->startBlock = 0;
2630 param->endBlock = nBlocks - 1;
2632 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2633 /* use the MTD interface in yaffs_mtdif1.c */
2634 param->writeChunkWithTagsToNAND =
2635 nandmtd1_WriteChunkWithTagsToNAND;
2636 param->readChunkWithTagsFromNAND =
2637 nandmtd1_ReadChunkWithTagsFromNAND;
2638 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2639 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2641 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2642 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2644 param->isYaffs2 = 0;
2646 /* ... and common functions */
2647 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2648 param->initialiseNAND = nandmtd_InitialiseNAND;
2650 yaffs_DeviceToContext(dev)->putSuperFunc = yaffs_MTDPutSuper;
2652 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2653 param->gcControl = yaffs_gc_control_callback;
2655 yaffs_DeviceToContext(dev)->superBlock= sb;
2658 #ifndef CONFIG_YAFFS_DOES_ECC
2659 param->useNANDECC = 1;
2662 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2663 param->wideTnodesDisabled = 1;
2666 param->skipCheckpointRead = options.skip_checkpoint_read;
2667 param->skipCheckpointWrite = options.skip_checkpoint_write;
2669 /* we assume this is protected by lock_kernel() in mount/umount */
2670 down(&yaffs_context_lock);
2671 ylist_add_tail(&(yaffs_DeviceToContext(dev)->contextList), &yaffs_context_list);
2672 up(&yaffs_context_lock);
2674 /* Directory search handling...*/
2675 YINIT_LIST_HEAD(&(yaffs_DeviceToContext(dev)->searchContexts));
2676 param->removeObjectCallback = yaffs_RemoveObjectCallback;
2678 init_MUTEX(&(yaffs_DeviceToContext(dev)->grossLock));
2680 yaffs_GrossLock(dev);
2682 err = yaffs_GutsInitialise(dev);
2685 (TSTR("yaffs_read_super: guts initialised %s\n"),
2686 (err == YAFFS_OK) ? "OK" : "FAILED"));
2689 yaffs_BackgroundStart(dev);
2691 if(!context->bgThread)
2692 param->deferDirectoryUpdate = 0;
2695 /* Release lock before yaffs_get_inode() */
2696 yaffs_GrossUnlock(dev);
2698 /* Create root inode */
2699 if (err == YAFFS_OK)
2700 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2706 inode->i_op = &yaffs_dir_inode_operations;
2707 inode->i_fop = &yaffs_dir_operations;
2709 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
2711 root = d_alloc_root(inode);
2713 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
2720 sb->s_dirt = !dev->isCheckpointed;
2721 T(YAFFS_TRACE_ALWAYS,
2722 (TSTR("yaffs_read_super: isCheckpointed %d\n"),
2723 dev->isCheckpointed));
2725 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
2730 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2731 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2734 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2737 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2738 static int yaffs_read_super(struct file_system_type *fs,
2739 int flags, const char *dev_name,
2740 void *data, struct vfsmount *mnt)
2743 return get_sb_bdev(fs, flags, dev_name, data,
2744 yaffs_internal_read_super_mtd, mnt);
2747 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2748 int flags, const char *dev_name,
2752 return get_sb_bdev(fs, flags, dev_name, data,
2753 yaffs_internal_read_super_mtd);
2757 static struct file_system_type yaffs_fs_type = {
2758 .owner = THIS_MODULE,
2760 .get_sb = yaffs_read_super,
2761 .kill_sb = kill_block_super,
2762 .fs_flags = FS_REQUIRES_DEV,
2765 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2768 return yaffs_internal_read_super(1, sb, data, silent);
2771 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2776 #ifdef CONFIG_YAFFS_YAFFS2
2778 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2779 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2782 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2785 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2786 static int yaffs2_read_super(struct file_system_type *fs,
2787 int flags, const char *dev_name, void *data,
2788 struct vfsmount *mnt)
2790 return get_sb_bdev(fs, flags, dev_name, data,
2791 yaffs2_internal_read_super_mtd, mnt);
2794 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2795 int flags, const char *dev_name,
2799 return get_sb_bdev(fs, flags, dev_name, data,
2800 yaffs2_internal_read_super_mtd);
2804 static struct file_system_type yaffs2_fs_type = {
2805 .owner = THIS_MODULE,
2807 .get_sb = yaffs2_read_super,
2808 .kill_sb = kill_block_super,
2809 .fs_flags = FS_REQUIRES_DEV,
2812 static struct super_block *yaffs2_read_super(struct super_block *sb,
2813 void *data, int silent)
2815 return yaffs_internal_read_super(2, sb, data, silent);
2818 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2822 #endif /* CONFIG_YAFFS_YAFFS2 */
2824 static struct proc_dir_entry *my_proc_entry;
2825 static struct proc_dir_entry *debug_proc_entry;
2827 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
2829 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
2830 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
2831 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
2832 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
2833 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
2834 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
2835 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
2836 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
2837 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
2838 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
2839 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
2840 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
2842 buf += sprintf(buf, "\n");
2848 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
2850 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2851 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2852 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2853 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2854 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2855 buf += sprintf(buf, "\n");
2856 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2857 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2858 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2859 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2860 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2861 buf += sprintf(buf, "\n");
2862 buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
2863 buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
2864 buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
2865 buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
2866 buf += sprintf(buf, "allGCs............. %u\n", dev->allGCs);
2867 buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGCs);
2868 buf += sprintf(buf, "oldestDirtyGCs..... %u\n", dev->oldestDirtyGCs);
2869 buf += sprintf(buf, "backgroundGCs...... %u\n", dev->backgroundGCs);
2870 buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
2871 buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
2872 buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
2873 buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
2874 buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
2875 buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
2876 buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
2877 buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
2878 buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
2879 buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
2881 sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
2886 static int yaffs_proc_read(char *page,
2888 off_t offset, int count, int *eof, void *data)
2890 struct ylist_head *item;
2895 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2896 * We use 'offset' (*ppos) to indicate where we are in devList.
2897 * This also assumes the user has posted a read buffer large
2898 * enough to hold the complete output; but that's life in /proc.
2903 /* Print header first */
2905 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__"\n");
2907 buf += sprintf(buf,"\n");
2911 down(&yaffs_context_lock);
2913 /* Locate and print the Nth entry. Order N-squared but N is small. */
2914 ylist_for_each(item, &yaffs_context_list) {
2915 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2916 yaffs_Device *dev = dc->dev;
2918 if (n < (step & ~1)) {
2923 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
2924 buf = yaffs_dump_dev_part0(buf, dev);
2926 buf = yaffs_dump_dev_part1(buf, dev);
2930 up(&yaffs_context_lock);
2933 return buf - page < count ? buf - page : count;
2936 static int yaffs_stats_proc_read(char *page,
2938 off_t offset, int count, int *eof, void *data)
2940 struct ylist_head *item;
2944 down(&yaffs_context_lock);
2946 /* Locate and print the Nth entry. Order N-squared but N is small. */
2947 ylist_for_each(item, &yaffs_context_list) {
2948 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2949 yaffs_Device *dev = dc->dev;
2955 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2956 nObjects = dev->nObjectsCreated -dev->nFreeObjects;
2957 nTnodes = dev->nTnodesCreated - dev->nFreeTnodes;
2960 buf += sprintf(buf,"%d, %d, %d, %u, %u, %d, %d\n",
2961 n, dev->nFreeChunks, erasedChunks,
2962 dev->backgroundGCs, dev->oldestDirtyGCs,
2965 up(&yaffs_context_lock);
2968 return buf - page < count ? buf - page : count;
2972 * Set the verbosity of the warnings and error messages.
2974 * Note that the names can only be a..z or _ with the current code.
2979 unsigned mask_bitfield;
2981 {"allocate", YAFFS_TRACE_ALLOCATE},
2982 {"always", YAFFS_TRACE_ALWAYS},
2983 {"background", YAFFS_TRACE_BACKGROUND},
2984 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2985 {"buffers", YAFFS_TRACE_BUFFERS},
2986 {"bug", YAFFS_TRACE_BUG},
2987 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2988 {"deletion", YAFFS_TRACE_DELETION},
2989 {"erase", YAFFS_TRACE_ERASE},
2990 {"error", YAFFS_TRACE_ERROR},
2991 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2992 {"gc", YAFFS_TRACE_GC},
2993 {"lock", YAFFS_TRACE_LOCK},
2994 {"mtd", YAFFS_TRACE_MTD},
2995 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2996 {"os", YAFFS_TRACE_OS},
2997 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2998 {"scan", YAFFS_TRACE_SCAN},
2999 {"tracing", YAFFS_TRACE_TRACING},
3000 {"sync", YAFFS_TRACE_SYNC},
3001 {"write", YAFFS_TRACE_WRITE},
3003 {"verify", YAFFS_TRACE_VERIFY},
3004 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3005 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3006 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3008 {"all", 0xffffffff},
3013 #define MAX_MASK_NAME_LENGTH 40
3014 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3015 unsigned long count, void *data)
3017 unsigned rg = 0, mask_bitfield;
3021 char substring[MAX_MASK_NAME_LENGTH + 1];
3027 rg = yaffs_traceMask;
3029 while (!done && (pos < count)) {
3031 while ((pos < count) && isspace(buf[pos]))
3048 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3050 if (end > buf + pos) {
3051 mask_name = "numeral";
3052 len = end - (buf + pos);
3056 for (x = buf + pos, i = 0;
3057 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3058 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3060 substring[i] = '\0';
3062 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3063 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3064 mask_name = mask_flags[i].mask_name;
3065 mask_bitfield = mask_flags[i].mask_bitfield;
3072 if (mask_name != NULL) {
3076 rg &= ~mask_bitfield;
3079 rg |= mask_bitfield;
3085 rg |= mask_bitfield;
3091 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3093 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
3095 if (rg & YAFFS_TRACE_ALWAYS) {
3096 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3098 flag = ((rg & mask_flags[i].mask_bitfield) ==
3099 mask_flags[i].mask_bitfield) ? '+' : '-';
3100 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3108 static int yaffs_proc_write(struct file *file, const char *buf,
3109 unsigned long count, void *data)
3111 return yaffs_proc_write_trace_options(file, buf, count, data);
3114 /* Stuff to handle installation of file systems */
3115 struct file_system_to_install {
3116 struct file_system_type *fst;
3120 static struct file_system_to_install fs_to_install[] = {
3121 {&yaffs_fs_type, 0},
3122 {&yaffs2_fs_type, 0},
3126 static int __init init_yaffs_fs(void)
3129 struct file_system_to_install *fsinst;
3131 T(YAFFS_TRACE_ALWAYS,
3132 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3134 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3135 T(YAFFS_TRACE_ALWAYS,
3136 (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3142 init_MUTEX(&yaffs_context_lock);
3144 /* Install the proc_fs entries */
3145 my_proc_entry = create_proc_entry("yaffs",
3149 if (my_proc_entry) {
3150 my_proc_entry->write_proc = yaffs_proc_write;
3151 my_proc_entry->read_proc = yaffs_proc_read;
3152 my_proc_entry->data = NULL;
3156 debug_proc_entry = create_proc_entry("yaffs_stats",
3160 if (debug_proc_entry) {
3161 debug_proc_entry->write_proc = NULL;
3162 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3163 debug_proc_entry->data = NULL;
3167 /* Now add the file system entries */
3169 fsinst = fs_to_install;
3171 while (fsinst->fst && !error) {
3172 error = register_filesystem(fsinst->fst);
3174 fsinst->installed = 1;
3178 /* Any errors? uninstall */
3180 fsinst = fs_to_install;
3182 while (fsinst->fst) {
3183 if (fsinst->installed) {
3184 unregister_filesystem(fsinst->fst);
3185 fsinst->installed = 0;
3194 static void __exit exit_yaffs_fs(void)
3197 struct file_system_to_install *fsinst;
3199 T(YAFFS_TRACE_ALWAYS,
3200 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3202 remove_proc_entry("yaffs", YPROC_ROOT);
3203 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3205 fsinst = fs_to_install;
3207 while (fsinst->fst) {
3208 if (fsinst->installed) {
3209 unregister_filesystem(fsinst->fst);
3210 fsinst->installed = 0;
3216 module_init(init_yaffs_fs)
3217 module_exit(exit_yaffs_fs)
3219 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3220 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3221 MODULE_LICENSE("GPL");