2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * Luc van OostenRyck for numerous patches.
10 * Nick Bane for numerous patches.
11 * Nick Bane for 2.5/2.6 integration.
12 * Andras Toth for mknod rdev issue.
13 * Michael Fischer for finding the problem with inode inconsistency.
14 * Some code bodily lifted from JFFS
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
23 * This is the file system front-end to YAFFS that hooks it up to
27 * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
29 * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
31 * >> inode->u.generic_ip points to the associated yaffs_Object.
34 #include <linux/version.h>
36 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
37 #define YAFFS_COMPILE_BACKGROUND
40 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28))
41 #define YAFFS_COMPILE_EXPORTFS
44 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
45 #define YAFFS_USE_SETATTR_COPY
46 #define YAFFS_USE_TRUNCATE_SETSIZE
48 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
49 #define YAFFS_HAS_EVICT_INODE
52 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
53 #define YAFFS_NEW_FOLLOW_LINK 1
55 #define YAFFS_NEW_FOLLOW_LINK 0
58 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
59 #include <linux/config.h>
62 #include <linux/kernel.h>
63 #include <linux/module.h>
64 #include <linux/slab.h>
65 #include <linux/init.h>
67 #include <linux/proc_fs.h>
68 #include <linux/smp_lock.h>
69 #include <linux/pagemap.h>
70 #include <linux/mtd/mtd.h>
71 #include <linux/interrupt.h>
72 #include <linux/string.h>
73 #include <linux/ctype.h>
75 #if (YAFFS_NEW_FOLLOW_LINK == 1)
76 #include <linux/namei.h>
79 #ifdef YAFFS_COMPILE_EXPORTFS
80 #include <linux/exportfs.h>
83 #ifdef YAFFS_COMPILE_BACKGROUND
84 #include <linux/kthread.h>
85 #include <linux/delay.h>
86 #include <linux/freezer.h>
89 #include <asm/div64.h>
91 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
93 #include <linux/statfs.h>
95 #define UnlockPage(p) unlock_page(p)
96 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
98 /* FIXME: use sb->s_id instead ? */
99 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
103 #include <linux/locks.h>
104 #define BDEVNAME_SIZE 0
105 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
107 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
108 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
114 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
115 #define YPROC_ROOT (&proc_root)
117 #define YPROC_ROOT NULL
120 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
121 #define Y_INIT_TIMER(a) init_timer(a)
123 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
126 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
127 #define WRITE_SIZE_STR "writesize"
128 #define WRITE_SIZE(mtd) ((mtd)->writesize)
130 #define WRITE_SIZE_STR "oobblock"
131 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
134 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
135 #define YAFFS_USE_WRITE_BEGIN_END 1
137 #define YAFFS_USE_WRITE_BEGIN_END 0
140 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
141 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
143 uint64_t result = partition_size;
144 do_div(result, block_size);
145 return (uint32_t)result;
148 #define YCALCBLOCKS(s, b) ((s)/(b))
151 #include <linux/uaccess.h>
152 #include <linux/mtd/mtd.h>
154 #include "yportenv.h"
155 #include "yaffs_trace.h"
156 #include "yaffs_guts.h"
158 #include "yaffs_linux.h"
160 #include "yaffs_mtdif.h"
161 #include "yaffs_mtdif1.h"
162 #include "yaffs_mtdif2.h"
164 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
165 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
166 unsigned int yaffs_auto_checkpoint = 1;
167 unsigned int yaffs_gc_control = 1;
168 unsigned int yaffs_bg_enable = 1;
170 /* Module Parameters */
171 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
172 module_param(yaffs_traceMask, uint, 0644);
173 module_param(yaffs_wr_attempts, uint, 0644);
174 module_param(yaffs_auto_checkpoint, uint, 0644);
175 module_param(yaffs_gc_control, uint, 0644);
176 module_param(yaffs_bg_enable, uint, 0644);
178 MODULE_PARM(yaffs_traceMask, "i");
179 MODULE_PARM(yaffs_wr_attempts, "i");
180 MODULE_PARM(yaffs_auto_checkpoint, "i");
181 MODULE_PARM(yaffs_gc_control, "i");
184 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
185 /* use iget and read_inode */
186 #define Y_IGET(sb, inum) iget((sb), (inum))
187 static void yaffs_read_inode(struct inode *inode);
190 /* Call local equivalent */
191 #define YAFFS_USE_OWN_IGET
192 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
194 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
197 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
198 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
200 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
203 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
204 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
206 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
207 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
209 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
213 #define update_dir_time(dir) do {\
214 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
217 static void yaffs_put_super(struct super_block *sb);
219 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
221 static ssize_t yaffs_hold_space(struct file *f);
222 static void yaffs_release_space(struct file *f);
224 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
225 static int yaffs_file_flush(struct file *file, fl_owner_t id);
227 static int yaffs_file_flush(struct file *file);
230 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
231 static int yaffs_sync_object(struct file *file, int datasync);
233 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
237 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
239 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
240 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
241 struct nameidata *n);
242 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
243 struct nameidata *n);
245 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
246 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
248 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
249 struct dentry *dentry);
250 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
251 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
252 const char *symname);
253 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
255 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
256 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
259 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
262 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
263 struct inode *new_dir, struct dentry *new_dentry);
264 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
266 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
267 static int yaffs_sync_fs(struct super_block *sb, int wait);
268 static void yaffs_write_super(struct super_block *sb);
270 static int yaffs_sync_fs(struct super_block *sb);
271 static int yaffs_write_super(struct super_block *sb);
274 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
275 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
276 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
277 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
279 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
282 #ifdef YAFFS_HAS_PUT_INODE
283 static void yaffs_put_inode(struct inode *inode);
286 #ifdef YAFFS_HAS_EVICT_INODE
287 static void yaffs_evict_inode(struct inode *);
289 static void yaffs_delete_inode(struct inode *);
290 static void yaffs_clear_inode(struct inode *);
293 static int yaffs_readpage(struct file *file, struct page *page);
294 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
295 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
297 static int yaffs_writepage(struct page *page);
300 #ifdef CONFIG_YAFFS_XATTR
301 int yaffs_setxattr(struct dentry *dentry, const char *name,
302 const void *value, size_t size, int flags);
303 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
305 int yaffs_removexattr(struct dentry *dentry, const char *name);
306 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
310 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
311 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
312 loff_t pos, unsigned len, unsigned flags,
313 struct page **pagep, void **fsdata);
314 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
315 loff_t pos, unsigned len, unsigned copied,
316 struct page *pg, void *fsdadata);
318 static int yaffs_prepare_write(struct file *f, struct page *pg,
319 unsigned offset, unsigned to);
320 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
325 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
327 #if (YAFFS_NEW_FOLLOW_LINK == 1)
328 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
329 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
331 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
334 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev);
336 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
338 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
341 static struct address_space_operations yaffs_file_address_operations = {
342 .readpage = yaffs_readpage,
343 .writepage = yaffs_writepage,
344 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
345 .write_begin = yaffs_write_begin,
346 .write_end = yaffs_write_end,
348 .prepare_write = yaffs_prepare_write,
349 .commit_write = yaffs_commit_write,
354 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
355 static const struct file_operations yaffs_file_operations = {
356 .read = do_sync_read,
357 .write = do_sync_write,
358 .aio_read = generic_file_aio_read,
359 .aio_write = generic_file_aio_write,
360 .mmap = generic_file_mmap,
361 .flush = yaffs_file_flush,
362 .fsync = yaffs_sync_object,
363 .splice_read = generic_file_splice_read,
364 .splice_write = generic_file_splice_write,
365 .llseek = generic_file_llseek,
368 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
370 static const struct file_operations yaffs_file_operations = {
371 .read = do_sync_read,
372 .write = do_sync_write,
373 .aio_read = generic_file_aio_read,
374 .aio_write = generic_file_aio_write,
375 .mmap = generic_file_mmap,
376 .flush = yaffs_file_flush,
377 .fsync = yaffs_sync_object,
378 .sendfile = generic_file_sendfile,
383 static const struct file_operations yaffs_file_operations = {
384 .read = generic_file_read,
385 .write = generic_file_write,
386 .mmap = generic_file_mmap,
387 .flush = yaffs_file_flush,
388 .fsync = yaffs_sync_object,
389 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
390 .sendfile = generic_file_sendfile,
395 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
396 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
398 void * kaddr = kmap_atomic(page, KM_USER0);
399 memset(kaddr + start, 0, end - start);
400 kunmap_atomic(kaddr, KM_USER0);
401 flush_dcache_page(page);
406 static const struct inode_operations yaffs_file_inode_operations = {
407 .setattr = yaffs_setattr,
408 #ifdef CONFIG_YAFFS_XATTR
409 .setxattr = yaffs_setxattr,
410 .getxattr = yaffs_getxattr,
411 .listxattr = yaffs_listxattr,
412 .removexattr = yaffs_removexattr,
416 static const struct inode_operations yaffs_symlink_inode_operations = {
417 .readlink = yaffs_readlink,
418 .follow_link = yaffs_follow_link,
419 #if (YAFFS_NEW_FOLLOW_LINK == 1)
420 .put_link = yaffs_put_link,
422 .setattr = yaffs_setattr,
423 #ifdef CONFIG_YAFFS_XATTR
424 .setxattr = yaffs_setxattr,
425 .getxattr = yaffs_getxattr,
426 .listxattr = yaffs_listxattr,
427 .removexattr = yaffs_removexattr,
431 static const struct inode_operations yaffs_dir_inode_operations = {
432 .create = yaffs_create,
433 .lookup = yaffs_lookup,
435 .unlink = yaffs_unlink,
436 .symlink = yaffs_symlink,
437 .mkdir = yaffs_mkdir,
438 .rmdir = yaffs_unlink,
439 .mknod = yaffs_mknod,
440 .rename = yaffs_rename,
441 .setattr = yaffs_setattr,
442 #ifdef CONFIG_YAFFS_XATTR
443 .setxattr = yaffs_setxattr,
444 .getxattr = yaffs_getxattr,
445 .listxattr = yaffs_listxattr,
446 .removexattr = yaffs_removexattr,
450 static const struct file_operations yaffs_dir_operations = {
451 .read = generic_read_dir,
452 .readdir = yaffs_readdir,
453 .fsync = yaffs_sync_object,
454 .llseek = yaffs_dir_llseek,
457 static const struct super_operations yaffs_super_ops = {
458 .statfs = yaffs_statfs,
460 #ifndef YAFFS_USE_OWN_IGET
461 .read_inode = yaffs_read_inode,
463 #ifdef YAFFS_HAS_PUT_INODE
464 .put_inode = yaffs_put_inode,
466 .put_super = yaffs_put_super,
467 #ifdef YAFFS_HAS_EVICT_INODE
468 .evict_inode = yaffs_evict_inode,
470 .delete_inode = yaffs_delete_inode,
471 .clear_inode = yaffs_clear_inode,
473 .sync_fs = yaffs_sync_fs,
474 .write_super = yaffs_write_super,
478 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
480 #ifdef YAFFS_USE_SETATTR_COPY
481 setattr_copy(inode,attr);
484 return inode_setattr(inode, attr);
489 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
491 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
492 truncate_setsize(inode,newsize);
495 return simple_setsize(inode, newsize);
500 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
502 return yaffs_gc_control;
505 static void yaffs_GrossLock(yaffs_Device *dev)
507 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
508 down(&(yaffs_DeviceToLC(dev)->grossLock));
509 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
512 static void yaffs_GrossUnlock(yaffs_Device *dev)
514 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
515 up(&(yaffs_DeviceToLC(dev)->grossLock));
518 #ifdef YAFFS_COMPILE_EXPORTFS
520 static struct inode *
521 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
523 return Y_IGET(sb, ino);
526 static struct dentry *
527 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
529 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
532 static struct dentry *
533 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
535 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
538 struct dentry *yaffs2_get_parent(struct dentry *dentry)
541 struct super_block *sb = dentry->d_inode->i_sb;
542 struct dentry *parent = ERR_PTR(-ENOENT);
544 unsigned long parent_ino;
546 yaffs_Object *parent_obj;
548 d_obj = yaffs_InodeToObject(dentry->d_inode);
551 parent_obj = d_obj->parent;
553 parent_ino = yaffs_GetObjectInode(parent_obj);
554 inode = Y_IGET(sb, parent_ino);
557 parent = ERR_CAST(inode);
559 parent = d_obtain_alias(inode);
560 if (!IS_ERR(parent)) {
561 parent = ERR_PTR(-ENOMEM);
571 /* Just declare a zero structure as a NULL value implies
572 * using the default functions of exportfs.
575 static struct export_operations yaffs_export_ops =
577 .fh_to_dentry = yaffs2_fh_to_dentry,
578 .fh_to_parent = yaffs2_fh_to_parent,
579 .get_parent = yaffs2_get_parent,
584 /*-----------------------------------------------------------------*/
585 /* Directory search context allows us to unlock access to yaffs during
586 * filldir without causing problems with the directory being modified.
587 * This is similar to the tried and tested mechanism used in yaffs direct.
589 * A search context iterates along a doubly linked list of siblings in the
590 * directory. If the iterating object is deleted then this would corrupt
591 * the list iteration, likely causing a crash. The search context avoids
592 * this by using the removeObjectCallback to move the search context to the
593 * next object before the object is deleted.
595 * Many readdirs (and thus seach conexts) may be alive simulateously so
596 * each yaffs_Device has a list of these.
598 * A seach context lives for the duration of a readdir.
600 * All these functions must be called while yaffs is locked.
603 struct yaffs_SearchContext {
605 yaffs_Object *dirObj;
606 yaffs_Object *nextReturn;
607 struct ylist_head others;
611 * yaffs_NewSearch() creates a new search context, initialises it and
612 * adds it to the device's search context list.
614 * Called at start of readdir.
616 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
618 yaffs_Device *dev = dir->myDev;
619 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
623 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
624 sc->nextReturn = NULL;
626 sc->nextReturn = ylist_entry(
627 dir->variant.directoryVariant.children.next,
628 yaffs_Object,siblings);
629 YINIT_LIST_HEAD(&sc->others);
630 ylist_add(&sc->others,&(yaffs_DeviceToLC(dev)->searchContexts));
636 * yaffs_EndSearch() disposes of a search context and cleans up.
638 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
641 ylist_del(&sc->others);
647 * yaffs_SearchAdvance() moves a search context to the next object.
648 * Called when the search iterates or when an object removal causes
649 * the search context to be moved to the next object.
651 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
656 if( sc->nextReturn == NULL ||
657 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
658 sc->nextReturn = NULL;
660 struct ylist_head *next = sc->nextReturn->siblings.next;
662 if( next == &sc->dirObj->variant.directoryVariant.children)
663 sc->nextReturn = NULL; /* end of list */
665 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
670 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
671 * We check open search contexts and advance any which are currently
672 * on the object being iterated.
674 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
677 struct ylist_head *i;
678 struct yaffs_SearchContext *sc;
679 struct ylist_head *search_contexts = &(yaffs_DeviceToLC(obj->myDev)->searchContexts);
682 /* Iterate through the directory search contexts.
683 * If any are currently on the object being removed, then advance
684 * the search context to the next object to prevent a hanging pointer.
686 ylist_for_each(i, search_contexts) {
688 sc = ylist_entry(i, struct yaffs_SearchContext,others);
689 if(sc->nextReturn == obj)
690 yaffs_SearchAdvance(sc);
697 /*-----------------------------------------------------------------*/
699 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
702 unsigned char *alias;
705 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
707 yaffs_GrossLock(dev);
709 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
711 yaffs_GrossUnlock(dev);
716 ret = vfs_readlink(dentry, buffer, buflen, alias);
721 #if (YAFFS_NEW_FOLLOW_LINK == 1)
722 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
724 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
727 unsigned char *alias;
729 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
731 yaffs_GrossLock(dev);
733 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
734 yaffs_GrossUnlock(dev);
741 #if (YAFFS_NEW_FOLLOW_LINK == 1)
742 nd_set_link(nd, alias);
747 ret = vfs_follow_link(nd, alias);
754 #if (YAFFS_NEW_FOLLOW_LINK == 1)
755 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) {
760 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
764 * Lookup is used to find objects in the fs
766 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
768 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
771 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
775 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
777 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
779 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
780 yaffs_GrossLock(dev);
783 (TSTR("yaffs_lookup for %d:%s\n"),
784 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
786 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
787 dentry->d_name.name);
789 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
791 /* Can't hold gross lock when calling yaffs_get_inode() */
792 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
793 yaffs_GrossUnlock(dev);
797 (TSTR("yaffs_lookup found %d\n"), obj->objectId));
799 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
803 (TSTR("yaffs_loookup dentry \n")));
804 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
805 * d_add even if NULL inode */
807 /*dget(dentry); // try to solve directory bug */
808 d_add(dentry, inode);
816 T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
820 /* added NCB for 2.5/6 compatability - forces add even if inode is
821 * NULL which creates dentry hash */
822 d_add(dentry, inode);
828 #ifdef YAFFS_HAS_PUT_INODE
830 /* For now put inode is just for debugging
831 * Put inode is called when the inode **structure** is put.
833 static void yaffs_put_inode(struct inode *inode)
836 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
837 atomic_read(&inode->i_count)));
843 static void yaffs_UnstitchObject(struct inode *inode, yaffs_Object *obj)
845 /* Clear the association between the inode and
849 yaffs_InodeToObjectLV(inode) = NULL;
851 /* If the object freeing was deferred, then the real
853 * This should fix the inode inconsistency problem.
855 yaffs_HandleDeferedFree(obj);
858 #ifdef YAFFS_HAS_EVICT_INODE
859 /* yaffs_evict_inode combines into one operation what was previously done in
860 * yaffs_clear_inode() and yaffs_delete_inode()
863 static void yaffs_evict_inode( struct inode *inode)
869 obj = yaffs_InodeToObject(inode);
872 (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
873 atomic_read(&inode->i_count),
874 obj ? "object exists" : "null object"));
876 if (!inode->i_nlink && !is_bad_inode(inode))
878 truncate_inode_pages(&inode->i_data,0);
879 end_writeback(inode);
883 yaffs_GrossLock(dev);
884 yaffs_DeleteObject(obj);
885 yaffs_GrossUnlock(dev);
889 yaffs_GrossLock(dev);
890 yaffs_UnstitchObject(inode,obj);
891 yaffs_GrossUnlock(dev);
898 /* clear is called to tell the fs to release any per-inode data it holds.
899 * The object might still exist on disk and is just being thrown out of the cache
900 * or else the object has actually been deleted and we're being called via
902 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
905 static void yaffs_clear_inode(struct inode *inode)
910 obj = yaffs_InodeToObject(inode);
913 (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
914 atomic_read(&inode->i_count),
915 obj ? "object exists" : "null object"));
919 yaffs_GrossLock(dev);
920 yaffs_UnstitchObject(inode,obj);
921 yaffs_GrossUnlock(dev);
926 /* delete is called when the link count is zero and the inode
927 * is put (ie. nobody wants to know about it anymore, time to
929 * NB Must call clear_inode()
931 static void yaffs_delete_inode(struct inode *inode)
933 yaffs_Object *obj = yaffs_InodeToObject(inode);
937 (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
938 atomic_read(&inode->i_count),
939 obj ? "object exists" : "null object"));
943 yaffs_GrossLock(dev);
944 yaffs_DeleteObject(obj);
945 yaffs_GrossUnlock(dev);
947 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
948 truncate_inode_pages(&inode->i_data, 0);
955 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
956 static int yaffs_file_flush(struct file *file, fl_owner_t id)
958 static int yaffs_file_flush(struct file *file)
961 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
963 yaffs_Device *dev = obj->myDev;
966 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->objectId,
967 obj->dirty ? "dirty" : "clean"));
969 yaffs_GrossLock(dev);
971 yaffs_FlushFile(obj, 1, 0);
973 yaffs_GrossUnlock(dev);
978 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
980 /* Lifted from jffs2 */
983 unsigned char *pg_buf;
989 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
990 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
991 (unsigned)PAGE_CACHE_SIZE));
993 obj = yaffs_DentryToObject(f->f_dentry);
997 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
998 BUG_ON(!PageLocked(pg));
1000 if (!PageLocked(pg))
1005 /* FIXME: Can kmap fail? */
1007 yaffs_GrossLock(dev);
1009 ret = yaffs_ReadDataFromFile(obj, pg_buf,
1010 pg->index << PAGE_CACHE_SHIFT,
1013 yaffs_GrossUnlock(dev);
1019 ClearPageUptodate(pg);
1022 SetPageUptodate(pg);
1026 flush_dcache_page(pg);
1029 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
1033 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1035 int ret = yaffs_readpage_nolock(f, pg);
1040 static int yaffs_readpage(struct file *f, struct page *pg)
1044 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
1045 ret=yaffs_readpage_unlock(f, pg);
1046 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
1050 /* writepage inspired by/stolen from smbfs */
1052 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1053 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1055 static int yaffs_writepage(struct page *page)
1059 struct address_space *mapping = page->mapping;
1060 struct inode *inode;
1061 unsigned long end_index;
1070 inode = mapping->host;
1073 i_size = i_size_read(inode);
1075 end_index = i_size >> PAGE_CACHE_SHIFT;
1077 if(page->index < end_index)
1078 nBytes = PAGE_CACHE_SIZE;
1080 nBytes = i_size & (PAGE_CACHE_SIZE -1);
1082 if (page->index > end_index || !nBytes) {
1084 (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
1085 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1086 (unsigned)inode->i_size));
1088 (TSTR(" -> don't care!!\n")));
1090 zero_user_segment(page,0,PAGE_CACHE_SIZE);
1091 set_page_writeback(page);
1093 end_page_writeback(page);
1098 if(nBytes != PAGE_CACHE_SIZE)
1099 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
1103 buffer = kmap(page);
1105 obj = yaffs_InodeToObject(inode);
1107 yaffs_GrossLock(dev);
1110 (TSTR("yaffs_writepage at %08x, size %08x\n"),
1111 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
1113 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
1114 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1116 nWritten = yaffs_WriteDataToFile(obj, buffer,
1117 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
1119 yaffs_MarkSuperBlockDirty(dev);
1122 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
1123 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1125 yaffs_GrossUnlock(dev);
1128 set_page_writeback(page);
1130 end_page_writeback(page);
1133 return (nWritten == nBytes) ? 0 : -ENOSPC;
1137 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1138 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1139 loff_t pos, unsigned len, unsigned flags,
1140 struct page **pagep, void **fsdata)
1142 struct page *pg = NULL;
1143 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1149 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1150 pg = grab_cache_page_write_begin(mapping, index, flags);
1152 pg = __grab_cache_page(mapping, index);
1161 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
1162 (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
1165 space_held = yaffs_hold_space(filp);
1172 /* Update page if required */
1174 if (!Page_Uptodate(pg))
1175 ret = yaffs_readpage_nolock(filp, pg);
1180 /* Happy path return */
1181 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
1187 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
1189 yaffs_release_space(filp);
1192 page_cache_release(pg);
1199 static int yaffs_prepare_write(struct file *f, struct page *pg,
1200 unsigned offset, unsigned to)
1202 T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
1204 if (!Page_Uptodate(pg))
1205 return yaffs_readpage_nolock(f, pg);
1210 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1211 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1212 loff_t pos, unsigned len, unsigned copied,
1213 struct page *pg, void *fsdadata)
1217 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1220 addr = kva + offset_into_page;
1223 ("yaffs_write_end addr %p pos %x nBytes %d\n",
1224 addr,(unsigned)pos, copied));
1226 ret = yaffs_file_write(filp, addr, copied, &pos);
1228 if (ret != copied) {
1230 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
1239 yaffs_release_space(filp);
1241 page_cache_release(pg);
1246 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1251 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1252 int nBytes = to - offset;
1255 unsigned spos = pos;
1259 addr = kva + offset;
1261 saddr = (unsigned) addr;
1264 (TSTR("yaffs_commit_write addr %x pos %x nBytes %d\n"),
1265 saddr, spos, nBytes));
1267 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1269 if (nWritten != nBytes) {
1271 (TSTR("yaffs_commit_write not same size nWritten %d nBytes %d\n"),
1281 (TSTR("yaffs_commit_write returning %d\n"),
1282 nWritten == nBytes ? 0 : nWritten));
1284 return nWritten == nBytes ? 0 : nWritten;
1289 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1294 /* Check mode against the variant type and attempt to repair if broken. */
1295 __u32 mode = obj->yst_mode;
1296 switch (obj->variantType) {
1297 case YAFFS_OBJECT_TYPE_FILE:
1298 if (!S_ISREG(mode)) {
1299 obj->yst_mode &= ~S_IFMT;
1300 obj->yst_mode |= S_IFREG;
1304 case YAFFS_OBJECT_TYPE_SYMLINK:
1305 if (!S_ISLNK(mode)) {
1306 obj->yst_mode &= ~S_IFMT;
1307 obj->yst_mode |= S_IFLNK;
1311 case YAFFS_OBJECT_TYPE_DIRECTORY:
1312 if (!S_ISDIR(mode)) {
1313 obj->yst_mode &= ~S_IFMT;
1314 obj->yst_mode |= S_IFDIR;
1318 case YAFFS_OBJECT_TYPE_UNKNOWN:
1319 case YAFFS_OBJECT_TYPE_HARDLINK:
1320 case YAFFS_OBJECT_TYPE_SPECIAL:
1326 inode->i_flags |= S_NOATIME;
1328 inode->i_ino = obj->objectId;
1329 inode->i_mode = obj->yst_mode;
1330 inode->i_uid = obj->yst_uid;
1331 inode->i_gid = obj->yst_gid;
1332 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1333 inode->i_blksize = inode->i_sb->s_blocksize;
1335 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1337 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1338 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1339 inode->i_atime.tv_nsec = 0;
1340 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1341 inode->i_mtime.tv_nsec = 0;
1342 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1343 inode->i_ctime.tv_nsec = 0;
1345 inode->i_rdev = obj->yst_rdev;
1346 inode->i_atime = obj->yst_atime;
1347 inode->i_mtime = obj->yst_mtime;
1348 inode->i_ctime = obj->yst_ctime;
1350 inode->i_size = yaffs_GetObjectFileLength(obj);
1351 inode->i_blocks = (inode->i_size + 511) >> 9;
1353 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1356 (TSTR("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n"),
1357 inode->i_mode, inode->i_uid, inode->i_gid,
1358 (int)inode->i_size, atomic_read(&inode->i_count)));
1360 switch (obj->yst_mode & S_IFMT) {
1361 default: /* fifo, device or socket */
1362 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1363 init_special_inode(inode, obj->yst_mode,
1364 old_decode_dev(obj->yst_rdev));
1366 init_special_inode(inode, obj->yst_mode,
1367 (dev_t) (obj->yst_rdev));
1370 case S_IFREG: /* file */
1371 inode->i_op = &yaffs_file_inode_operations;
1372 inode->i_fop = &yaffs_file_operations;
1373 inode->i_mapping->a_ops =
1374 &yaffs_file_address_operations;
1376 case S_IFDIR: /* directory */
1377 inode->i_op = &yaffs_dir_inode_operations;
1378 inode->i_fop = &yaffs_dir_operations;
1380 case S_IFLNK: /* symlink */
1381 inode->i_op = &yaffs_symlink_inode_operations;
1385 yaffs_InodeToObjectLV(inode) = obj;
1387 obj->myInode = inode;
1391 (TSTR("yaffs_FileInode invalid parameters\n")));
1396 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1399 struct inode *inode;
1403 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1410 (TSTR("yaffs_get_inode for NULL object!!\n")));
1416 (TSTR("yaffs_get_inode for object %d\n"), obj->objectId));
1418 inode = Y_IGET(sb, obj->objectId);
1422 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1423 /* iget also increments the inode's i_count */
1424 /* NB You can't be holding grossLock or deadlock will happen! */
1429 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1434 struct inode *inode;
1437 obj = yaffs_DentryToObject(f->f_dentry);
1441 yaffs_GrossLock(dev);
1443 inode = f->f_dentry->d_inode;
1445 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1446 ipos = inode->i_size;
1452 (TSTR("yaffs_file_write: hey obj is null!\n")));
1455 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1456 "to object %d at %d(%x)\n"),
1457 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1459 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1461 yaffs_MarkSuperBlockDirty(dev);
1464 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1465 (unsigned )n,(unsigned)n));
1470 if (ipos > inode->i_size) {
1471 inode->i_size = ipos;
1472 inode->i_blocks = (ipos + 511) >> 9;
1475 (TSTR("yaffs_file_write size updated to %d bytes, "
1477 ipos, (int)(inode->i_blocks)));
1481 yaffs_GrossUnlock(dev);
1482 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1485 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1486 /* For now we just assume few parallel writes and check against a small number. */
1487 /* Todo: need to do this with a counter to handle parallel reads better */
1489 static ssize_t yaffs_hold_space(struct file *f)
1497 obj = yaffs_DentryToObject(f->f_dentry);
1501 yaffs_GrossLock(dev);
1503 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1505 yaffs_GrossUnlock(dev);
1507 return (nFreeChunks > 20) ? 1 : 0;
1510 static void yaffs_release_space(struct file *f)
1516 obj = yaffs_DentryToObject(f->f_dentry);
1520 yaffs_GrossLock(dev);
1523 yaffs_GrossUnlock(dev);
1527 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1535 offset += i_size_read(file->f_path.dentry->d_inode);
1538 offset += file->f_pos;
1543 if (offset != file->f_pos)
1544 file->f_pos = offset;
1553 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1557 struct yaffs_SearchContext *sc;
1558 struct inode *inode = f->f_dentry->d_inode;
1559 unsigned long offset, curoffs;
1563 char name[YAFFS_MAX_NAME_LENGTH + 1];
1565 obj = yaffs_DentryToObject(f->f_dentry);
1568 yaffs_GrossLock(dev);
1570 yaffs_DeviceToLC(dev)->readdirProcess = current;
1574 sc = yaffs_NewSearch(obj);
1580 T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1584 (TSTR("yaffs_readdir: entry . ino %d \n"),
1585 (int)inode->i_ino));
1586 yaffs_GrossUnlock(dev);
1587 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0){
1588 yaffs_GrossLock(dev);
1591 yaffs_GrossLock(dev);
1597 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1598 (int)f->f_dentry->d_parent->d_inode->i_ino));
1599 yaffs_GrossUnlock(dev);
1600 if (filldir(dirent, "..", 2, offset,
1601 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
1602 yaffs_GrossLock(dev);
1605 yaffs_GrossLock(dev);
1612 /* If the directory has changed since the open or last call to
1613 readdir, rewind to after the 2 canned entries. */
1614 if (f->f_version != inode->i_version) {
1617 f->f_version = inode->i_version;
1620 while(sc->nextReturn){
1623 if (curoffs >= offset) {
1624 int this_inode = yaffs_GetObjectInode(l);
1625 int this_type = yaffs_GetObjectType(l);
1627 yaffs_GetObjectName(l, name,
1628 YAFFS_MAX_NAME_LENGTH + 1);
1630 (TSTR("yaffs_readdir: %s inode %d\n"),
1631 name, yaffs_GetObjectInode(l)));
1633 yaffs_GrossUnlock(dev);
1641 yaffs_GrossLock(dev);
1645 yaffs_GrossLock(dev);
1650 yaffs_SearchAdvance(sc);
1654 yaffs_EndSearch(sc);
1655 yaffs_DeviceToLC(dev)->readdirProcess = NULL;
1656 yaffs_GrossUnlock(dev);
1664 * File creation. Allocate an inode, and we're done..
1667 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1670 #define YCRED(x) (x->cred)
1673 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1674 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1677 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1681 struct inode *inode;
1683 yaffs_Object *obj = NULL;
1686 yaffs_Object *parent = yaffs_InodeToObject(dir);
1688 int error = -ENOSPC;
1689 uid_t uid = YCRED(current)->fsuid;
1690 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1692 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1697 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1698 parent->objectId, parent->variantType));
1701 (TSTR("yaffs_mknod: could not get parent object\n")));
1705 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1706 "mode %x dev %x\n"),
1707 dentry->d_name.name, mode, rdev));
1709 dev = parent->myDev;
1711 yaffs_GrossLock(dev);
1713 switch (mode & S_IFMT) {
1715 /* Special (socket, fifo, device...) */
1716 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1717 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1718 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1719 gid, old_encode_dev(rdev));
1721 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1725 case S_IFREG: /* file */
1726 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1727 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1730 case S_IFDIR: /* directory */
1732 (TSTR("yaffs_mknod: making directory\n")));
1733 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1736 case S_IFLNK: /* symlink */
1737 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1738 obj = NULL; /* Do we ever get here? */
1742 /* Can not call yaffs_get_inode() with gross lock held */
1743 yaffs_GrossUnlock(dev);
1746 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1747 d_instantiate(dentry, inode);
1748 update_dir_time(dir);
1750 (TSTR("yaffs_mknod created object %d count = %d\n"),
1751 obj->objectId, atomic_read(&inode->i_count)));
1753 yaffs_FillInodeFromObject(dir,parent);
1756 (TSTR("yaffs_mknod failed making object\n")));
1763 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1766 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1767 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1771 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1772 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1773 struct nameidata *n)
1775 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1778 T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1779 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1782 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1790 (TSTR("yaffs_unlink %d:%s\n"),
1792 dentry->d_name.name));
1793 obj = yaffs_InodeToObject(dir);
1796 yaffs_GrossLock(dev);
1798 retVal = yaffs_Unlink(obj, dentry->d_name.name);
1800 if (retVal == YAFFS_OK) {
1801 dentry->d_inode->i_nlink--;
1803 yaffs_GrossUnlock(dev);
1804 mark_inode_dirty(dentry->d_inode);
1805 update_dir_time(dir);
1808 yaffs_GrossUnlock(dev);
1815 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1816 struct dentry *dentry)
1818 struct inode *inode = old_dentry->d_inode;
1819 yaffs_Object *obj = NULL;
1820 yaffs_Object *link = NULL;
1823 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1825 obj = yaffs_InodeToObject(inode);
1828 yaffs_GrossLock(dev);
1830 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1831 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1835 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1836 d_instantiate(dentry, old_dentry->d_inode);
1837 atomic_inc(&old_dentry->d_inode->i_count);
1839 (TSTR("yaffs_link link count %d i_count %d\n"),
1840 old_dentry->d_inode->i_nlink,
1841 atomic_read(&old_dentry->d_inode->i_count)));
1844 yaffs_GrossUnlock(dev);
1847 update_dir_time(dir);
1854 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1855 const char *symname)
1859 uid_t uid = YCRED(current)->fsuid;
1860 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1862 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1864 dev = yaffs_InodeToObject(dir)->myDev;
1865 yaffs_GrossLock(dev);
1866 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1867 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1868 yaffs_GrossUnlock(dev);
1871 struct inode *inode;
1873 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1874 d_instantiate(dentry, inode);
1875 update_dir_time(dir);
1876 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1879 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1885 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1886 static int yaffs_sync_object(struct file *file, int datasync)
1888 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1895 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1896 struct dentry *dentry = file->f_path.dentry;
1899 obj = yaffs_DentryToObject(dentry);
1903 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1904 (TSTR("yaffs_sync_object\n")));
1905 yaffs_GrossLock(dev);
1906 yaffs_FlushFile(obj, 1, datasync);
1907 yaffs_GrossUnlock(dev);
1912 * The VFS layer already does all the dentry stuff for rename.
1914 * NB: POSIX says you can rename an object over an old object of the same name
1916 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1917 struct inode *new_dir, struct dentry *new_dentry)
1920 int retVal = YAFFS_FAIL;
1921 yaffs_Object *target;
1923 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1924 dev = yaffs_InodeToObject(old_dir)->myDev;
1926 yaffs_GrossLock(dev);
1928 /* Check if the target is an existing directory that is not empty. */
1929 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1930 new_dentry->d_name.name);
1934 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1935 !ylist_empty(&target->variant.directoryVariant.children)) {
1937 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1939 retVal = YAFFS_FAIL;
1941 /* Now does unlinking internally using shadowing mechanism */
1942 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_RenameObject\n")));
1944 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1945 old_dentry->d_name.name,
1946 yaffs_InodeToObject(new_dir),
1947 new_dentry->d_name.name);
1949 yaffs_GrossUnlock(dev);
1951 if (retVal == YAFFS_OK) {
1953 new_dentry->d_inode->i_nlink--;
1954 mark_inode_dirty(new_dentry->d_inode);
1957 update_dir_time(old_dir);
1958 if(old_dir != new_dir)
1959 update_dir_time(new_dir);
1966 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1968 struct inode *inode = dentry->d_inode;
1973 (TSTR("yaffs_setattr of object %d\n"),
1974 yaffs_InodeToObject(inode)->objectId));
1976 /* Fail if a requested resize >= 2GB */
1977 if (attr->ia_valid & ATTR_SIZE &&
1978 (attr->ia_size >> 31))
1982 error = inode_change_ok(inode, attr);
1986 error = yaffs_vfs_setattr(inode, attr);
1987 T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
1988 if (attr->ia_valid & ATTR_SIZE){
1989 yaffs_vfs_setsize(inode,attr->ia_size);
1990 inode->i_blocks = (inode->i_size + 511) >> 9;
1993 dev = yaffs_InodeToObject(inode)->myDev;
1994 if (attr->ia_valid & ATTR_SIZE){
1995 T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
1996 (int)(attr->ia_size),(int)(attr->ia_size)));
1998 yaffs_GrossLock(dev);
1999 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
2000 if(result == YAFFS_OK) {
2005 yaffs_GrossUnlock(dev);
2010 (TSTR("yaffs_setattr done returning %d\n"),error));
2015 #ifdef CONFIG_YAFFS_XATTR
2016 int yaffs_setxattr(struct dentry *dentry, const char *name,
2017 const void *value, size_t size, int flags)
2019 struct inode *inode = dentry->d_inode;
2022 yaffs_Object *obj = yaffs_InodeToObject(inode);
2025 (TSTR("yaffs_setxattr of object %d\n"),
2032 yaffs_GrossLock(dev);
2033 result = yaffs_SetXAttribute(obj, name, value, size, flags);
2034 if(result == YAFFS_OK)
2038 yaffs_GrossUnlock(dev);
2042 (TSTR("yaffs_setxattr done returning %d\n"),error));
2048 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
2051 struct inode *inode = dentry->d_inode;
2054 yaffs_Object *obj = yaffs_InodeToObject(inode);
2057 (TSTR("yaffs_getxattr \"%s\" from object %d\n"),
2058 name, obj->objectId));
2062 yaffs_GrossLock(dev);
2063 error = yaffs_GetXAttribute(obj, name, buff, size);
2064 yaffs_GrossUnlock(dev);
2068 (TSTR("yaffs_getxattr done returning %d\n"),error));
2073 int yaffs_removexattr(struct dentry *dentry, const char *name)
2075 struct inode *inode = dentry->d_inode;
2078 yaffs_Object *obj = yaffs_InodeToObject(inode);
2081 (TSTR("yaffs_removexattr of object %d\n"),
2088 yaffs_GrossLock(dev);
2089 result = yaffs_RemoveXAttribute(obj, name);
2090 if(result == YAFFS_OK)
2094 yaffs_GrossUnlock(dev);
2098 (TSTR("yaffs_removexattr done returning %d\n"),error));
2103 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
2105 struct inode *inode = dentry->d_inode;
2108 yaffs_Object *obj = yaffs_InodeToObject(inode);
2111 (TSTR("yaffs_listxattr of object %d\n"),
2117 yaffs_GrossLock(dev);
2118 error = yaffs_ListXAttributes(obj, buff, size);
2119 yaffs_GrossUnlock(dev);
2123 (TSTR("yaffs_listxattr done returning %d\n"),error));
2131 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2132 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2134 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2135 struct super_block *sb = dentry->d_sb;
2136 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2137 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2139 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2141 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2143 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2146 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
2148 yaffs_GrossLock(dev);
2150 buf->f_type = YAFFS_MAGIC;
2151 buf->f_bsize = sb->s_blocksize;
2152 buf->f_namelen = 255;
2154 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
2155 /* Do this if chunk size is not a power of 2 */
2157 uint64_t bytesInDev;
2160 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
2161 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
2163 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
2164 buf->f_blocks = bytesInDev;
2166 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
2167 ((uint64_t)(dev->nDataBytesPerChunk));
2169 do_div(bytesFree, sb->s_blocksize);
2171 buf->f_bfree = bytesFree;
2173 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2176 (dev->param.endBlock - dev->param.startBlock + 1) *
2177 dev->param.nChunksPerBlock /
2178 (sb->s_blocksize / dev->nDataBytesPerChunk);
2180 yaffs_GetNumberOfFreeChunks(dev) /
2181 (sb->s_blocksize / dev->nDataBytesPerChunk);
2184 (dev->param.endBlock - dev->param.startBlock + 1) *
2185 dev->param.nChunksPerBlock *
2186 (dev->nDataBytesPerChunk / sb->s_blocksize);
2189 yaffs_GetNumberOfFreeChunks(dev) *
2190 (dev->nDataBytesPerChunk / sb->s_blocksize);
2195 buf->f_bavail = buf->f_bfree;
2197 yaffs_GrossUnlock(dev);
2203 static void yaffs_FlushInodes(struct super_block *sb)
2208 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
2209 obj = yaffs_InodeToObject(iptr);
2211 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
2213 yaffs_FlushFile(obj,1,0);
2219 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
2221 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2225 yaffs_FlushInodes(sb);
2226 yaffs_UpdateDirtyDirectories(dev);
2227 yaffs_FlushEntireDeviceCache(dev);
2229 yaffs_CheckpointSave(dev);
2233 static unsigned yaffs_bg_gc_urgency(yaffs_Device *dev)
2235 unsigned erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2236 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2237 unsigned scatteredFree = 0; /* Free chunks not in an erased block */
2239 if(erasedChunks < dev->nFreeChunks)
2240 scatteredFree = (dev->nFreeChunks - erasedChunks);
2242 if(!context->bgRunning)
2244 else if(scatteredFree < (dev->param.nChunksPerBlock * 2))
2246 else if(erasedChunks > dev->nFreeChunks/2)
2248 else if(erasedChunks > dev->nFreeChunks/4)
2254 static int yaffs_do_sync_fs(struct super_block *sb,
2255 int request_checkpoint)
2258 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2259 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2260 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2263 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2264 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
2266 sb->s_dirt ? "dirty" : "clean",
2267 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2268 oneshot_checkpoint ? " one-shot" : "" ));
2270 yaffs_GrossLock(dev);
2271 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2272 oneshot_checkpoint) &&
2273 !dev->isCheckpointed;
2275 if (sb->s_dirt || do_checkpoint) {
2276 yaffs_FlushSuperBlock(sb, !dev->isCheckpointed && do_checkpoint);
2278 if(oneshot_checkpoint)
2279 yaffs_auto_checkpoint &= ~4;
2281 yaffs_GrossUnlock(dev);
2287 * yaffs background thread functions .
2288 * yaffs_BackgroundThread() the thread function
2289 * yaffs_BackgroundStart() launches the background thread.
2290 * yaffs_BackgroundStop() cleans up the background thread.
2293 * The thread should only run after the yaffs is initialised
2294 * The thread should be stopped before yaffs is unmounted.
2295 * The thread should not do any writing while the fs is in read only.
2298 #ifdef YAFFS_COMPILE_BACKGROUND
2300 void yaffs_background_waker(unsigned long data)
2302 wake_up_process((struct task_struct *)data);
2305 static int yaffs_BackgroundThread(void *data)
2307 yaffs_Device *dev = (yaffs_Device *)data;
2308 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2309 unsigned long now = jiffies;
2310 unsigned long next_dir_update = now;
2311 unsigned long next_gc = now;
2312 unsigned long expires;
2313 unsigned int urgency;
2316 struct timer_list timer;
2318 T(YAFFS_TRACE_BACKGROUND,
2319 (TSTR("yaffs_background starting for dev %p\n"),
2324 while(context->bgRunning){
2325 T(YAFFS_TRACE_BACKGROUND,
2326 (TSTR("yaffs_background\n")));
2328 if(kthread_should_stop())
2334 yaffs_GrossLock(dev);
2338 if(time_after(now, next_dir_update) && yaffs_bg_enable){
2339 yaffs_UpdateDirtyDirectories(dev);
2340 next_dir_update = now + HZ;
2343 if(time_after(now,next_gc) && yaffs_bg_enable){
2344 if(!dev->isCheckpointed){
2345 urgency = yaffs_bg_gc_urgency(dev);
2346 gcResult = yaffs_BackgroundGarbageCollect(dev, urgency);
2348 next_gc = now + HZ/20+1;
2349 else if(urgency > 0)
2350 next_gc = now + HZ/10+1;
2352 next_gc = now + HZ * 2;
2354 * gc not running so set to next_dir_update
2355 * to cut down on wake ups
2357 next_gc = next_dir_update;
2359 yaffs_GrossUnlock(dev);
2361 expires = next_dir_update;
2362 if (time_before(next_gc,expires))
2364 if(time_before(expires,now))
2367 Y_INIT_TIMER(&timer);
2368 timer.expires = expires+1;
2369 timer.data = (unsigned long) current;
2370 timer.function = yaffs_background_waker;
2372 set_current_state(TASK_INTERRUPTIBLE);
2375 del_timer_sync(&timer);
2384 static int yaffs_BackgroundStart(yaffs_Device *dev)
2387 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2392 context->bgRunning = 1;
2394 context->bgThread = kthread_run(yaffs_BackgroundThread,
2395 (void *)dev,"yaffs-bg-%d",context->mount_id);
2397 if(IS_ERR(context->bgThread)){
2398 retval = PTR_ERR(context->bgThread);
2399 context->bgThread = NULL;
2400 context->bgRunning = 0;
2405 static void yaffs_BackgroundStop(yaffs_Device *dev)
2407 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToLC(dev);
2409 ctxt->bgRunning = 0;
2411 if( ctxt->bgThread){
2412 kthread_stop(ctxt->bgThread);
2413 ctxt->bgThread = NULL;
2417 static int yaffs_BackgroundThread(void *data)
2422 static int yaffs_BackgroundStart(yaffs_Device *dev)
2427 static void yaffs_BackgroundStop(yaffs_Device *dev)
2433 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2434 static void yaffs_write_super(struct super_block *sb)
2436 static int yaffs_write_super(struct super_block *sb)
2439 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2441 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2442 (TSTR("yaffs_write_super%s\n"),
2443 request_checkpoint ? " checkpt" : ""));
2445 yaffs_do_sync_fs(sb, request_checkpoint);
2447 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2453 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2454 static int yaffs_sync_fs(struct super_block *sb, int wait)
2456 static int yaffs_sync_fs(struct super_block *sb)
2459 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2461 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2462 (TSTR("yaffs_sync_fs%s\n"),
2463 request_checkpoint ? " checkpt" : ""));
2465 yaffs_do_sync_fs(sb, request_checkpoint);
2470 #ifdef YAFFS_USE_OWN_IGET
2472 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2474 struct inode *inode;
2476 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2479 (TSTR("yaffs_iget for %lu\n"), ino));
2481 inode = iget_locked(sb, ino);
2483 return ERR_PTR(-ENOMEM);
2484 if (!(inode->i_state & I_NEW))
2487 /* NB This is called as a side effect of other functions, but
2488 * we had to release the lock to prevent deadlocks, so
2489 * need to lock again.
2492 yaffs_GrossLock(dev);
2494 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2496 yaffs_FillInodeFromObject(inode, obj);
2498 yaffs_GrossUnlock(dev);
2500 unlock_new_inode(inode);
2506 static void yaffs_read_inode(struct inode *inode)
2508 /* NB This is called as a side effect of other functions, but
2509 * we had to release the lock to prevent deadlocks, so
2510 * need to lock again.
2514 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2517 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2519 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2520 yaffs_GrossLock(dev);
2522 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2524 yaffs_FillInodeFromObject(inode, obj);
2526 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2527 yaffs_GrossUnlock(dev);
2532 static YLIST_HEAD(yaffs_context_list);
2533 struct semaphore yaffs_context_lock;
2535 static void yaffs_put_super(struct super_block *sb)
2537 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2539 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2541 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2542 (TSTR("Shutting down yaffs background thread\n")));
2543 yaffs_BackgroundStop(dev);
2544 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2545 (TSTR("yaffs background thread shut down\n")));
2547 yaffs_GrossLock(dev);
2549 yaffs_FlushSuperBlock(sb,1);
2551 if (yaffs_DeviceToLC(dev)->putSuperFunc)
2552 yaffs_DeviceToLC(dev)->putSuperFunc(sb);
2555 yaffs_Deinitialise(dev);
2557 yaffs_GrossUnlock(dev);
2559 down(&yaffs_context_lock);
2560 ylist_del_init(&(yaffs_DeviceToLC(dev)->contextList));
2561 up(&yaffs_context_lock);
2563 if (yaffs_DeviceToLC(dev)->spareBuffer) {
2564 YFREE(yaffs_DeviceToLC(dev)->spareBuffer);
2565 yaffs_DeviceToLC(dev)->spareBuffer = NULL;
2572 static void yaffs_MTDPutSuper(struct super_block *sb)
2574 struct mtd_info *mtd = yaffs_DeviceToMtd(yaffs_SuperToDevice(sb));
2579 put_mtd_device(mtd);
2583 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2585 struct super_block *sb = yaffs_DeviceToLC(dev)->superBlock;
2587 T(YAFFS_TRACE_OS, (TSTR("yaffs_MarkSuperBlockDirty() sb = %p\n"), sb));
2594 int skip_checkpoint_read;
2595 int skip_checkpoint_write;
2598 int tags_ecc_overridden;
2599 int lazy_loading_enabled;
2600 int lazy_loading_overridden;
2601 int empty_lost_and_found;
2602 int empty_lost_and_found_overridden;
2605 #define MAX_OPT_LEN 30
2606 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2608 char cur_opt[MAX_OPT_LEN + 1];
2612 /* Parse through the options which is a comma seperated list */
2614 while (options_str && *options_str && !error) {
2615 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2618 while(*options_str == ',')
2621 while (*options_str && *options_str != ',') {
2622 if (p < MAX_OPT_LEN) {
2623 cur_opt[p] = *options_str;
2629 if (!strcmp(cur_opt, "inband-tags"))
2630 options->inband_tags = 1;
2631 else if (!strcmp(cur_opt, "tags-ecc-off")){
2632 options->tags_ecc_on = 0;
2633 options->tags_ecc_overridden=1;
2634 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2635 options->tags_ecc_on = 1;
2636 options->tags_ecc_overridden = 1;
2637 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2638 options->lazy_loading_enabled = 0;
2639 options->lazy_loading_overridden=1;
2640 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2641 options->lazy_loading_enabled = 1;
2642 options->lazy_loading_overridden = 1;
2643 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2644 options->empty_lost_and_found = 0;
2645 options->empty_lost_and_found_overridden=1;
2646 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2647 options->empty_lost_and_found = 1;
2648 options->empty_lost_and_found_overridden=1;
2649 } else if (!strcmp(cur_opt, "no-cache"))
2650 options->no_cache = 1;
2651 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2652 options->skip_checkpoint_read = 1;
2653 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2654 options->skip_checkpoint_write = 1;
2655 else if (!strcmp(cur_opt, "no-checkpoint")) {
2656 options->skip_checkpoint_read = 1;
2657 options->skip_checkpoint_write = 1;
2659 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2668 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2669 struct super_block *sb,
2670 void *data, int silent)
2673 struct inode *inode = NULL;
2674 struct dentry *root;
2675 yaffs_Device *dev = 0;
2676 char devname_buf[BDEVNAME_SIZE + 1];
2677 struct mtd_info *mtd;
2679 char *data_str = (char *)data;
2680 struct yaffs_LinuxContext *context = NULL;
2681 yaffs_DeviceParam *param;
2685 yaffs_options options;
2689 struct yaffs_LinuxContext *context_iterator;
2690 struct ylist_head *l;
2692 sb->s_magic = YAFFS_MAGIC;
2693 sb->s_op = &yaffs_super_ops;
2694 sb->s_flags |= MS_NOATIME;
2696 readOnly =((sb->s_flags & MS_RDONLY) != 0);
2699 #ifdef YAFFS_COMPILE_EXPORTFS
2700 sb->s_export_op = &yaffs_export_ops;
2704 printk(KERN_INFO "yaffs: sb is NULL\n");
2705 else if (!sb->s_dev)
2706 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2707 else if (!yaffs_devname(sb, devname_buf))
2708 printk(KERN_INFO "yaffs: devname is NULL\n");
2710 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2712 yaffs_devname(sb, devname_buf),
2713 readOnly ? "ro" : "rw");
2718 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2720 memset(&options, 0, sizeof(options));
2722 if (yaffs_parse_options(&options, data_str)) {
2723 /* Option parsing failed */
2728 sb->s_blocksize = PAGE_CACHE_SIZE;
2729 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2732 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffsVersion));
2734 (TSTR("yaffs_read_super: block size %d\n"),
2735 (int)(sb->s_blocksize)));
2737 T(YAFFS_TRACE_ALWAYS,
2738 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2739 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2740 yaffs_devname(sb, devname_buf)));
2742 /* Check it's an mtd device..... */
2743 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2744 return NULL; /* This isn't an mtd device */
2746 /* Get the device */
2747 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2749 T(YAFFS_TRACE_ALWAYS,
2750 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2754 /* Check it's NAND */
2755 if (mtd->type != MTD_NANDFLASH) {
2756 T(YAFFS_TRACE_ALWAYS,
2757 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2762 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2763 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2764 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2765 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2766 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2767 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2768 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2769 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2770 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2771 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2772 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2773 T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2775 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2778 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2780 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2781 T(YAFFS_TRACE_ALWAYS,
2782 (TSTR("yaffs: auto selecting yaffs2\n")));
2786 /* Added NCB 26/5/2006 for completeness */
2787 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2788 T(YAFFS_TRACE_ALWAYS,
2789 (TSTR("yaffs: auto selecting yaffs1\n")));
2795 if (yaffsVersion == 2) {
2796 /* Check for version 2 style functions */
2798 !mtd->block_isbad ||
2799 !mtd->block_markbad ||
2802 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2803 !mtd->read_oob || !mtd->write_oob) {
2806 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2808 T(YAFFS_TRACE_ALWAYS,
2809 (TSTR("yaffs: MTD device does not support required "
2814 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2815 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2816 !options.inband_tags) {
2817 T(YAFFS_TRACE_ALWAYS,
2818 (TSTR("yaffs: MTD device does not have the "
2819 "right page sizes\n")));
2823 /* Check for V1 style functions */
2827 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2828 !mtd->read_oob || !mtd->write_oob) {
2831 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2833 T(YAFFS_TRACE_ALWAYS,
2834 (TSTR("yaffs: MTD device does not support required "
2839 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2840 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2841 T(YAFFS_TRACE_ALWAYS,
2842 (TSTR("yaffs: MTD device does not support have the "
2843 "right page sizes\n")));
2848 /* OK, so if we got here, we have an MTD that's NAND and looks
2849 * like it has the right capabilities
2850 * Set the yaffs_Device up for mtd
2853 if (!readOnly && !(mtd->flags & MTD_WRITEABLE)){
2855 printk(KERN_INFO "yaffs: mtd is read only, setting superblock read only");
2856 sb->s_flags |= MS_RDONLY;
2859 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2860 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2862 if(!dev || !context ){
2872 /* Deep shit could not allocate device structure */
2873 T(YAFFS_TRACE_ALWAYS,
2874 (TSTR("yaffs_read_super: Failed trying to allocate "
2875 "yaffs_Device. \n")));
2878 memset(dev, 0, sizeof(yaffs_Device));
2879 param = &(dev->param);
2881 memset(context,0,sizeof(struct yaffs_LinuxContext));
2882 dev->osContext = context;
2883 YINIT_LIST_HEAD(&(context->contextList));
2885 context->superBlock = sb;
2887 dev->readOnly = readOnly;
2889 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2890 sb->s_fs_info = dev;
2892 sb->u.generic_sbp = dev;
2895 dev->driverContext = mtd;
2896 param->name = mtd->name;
2898 /* Set up the memory size parameters.... */
2900 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2902 param->startBlock = 0;
2903 param->endBlock = nBlocks - 1;
2904 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2905 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2906 param->nReservedBlocks = 5;
2907 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2908 param->inbandTags = options.inband_tags;
2910 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2911 param->disableLazyLoad = 1;
2913 #ifdef CONFIG_YAFFS_XATTR
2914 param->enableXattr = 1;
2916 if(options.lazy_loading_overridden)
2917 param->disableLazyLoad = !options.lazy_loading_enabled;
2919 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2920 param->noTagsECC = 1;
2923 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2925 param->deferDirectoryUpdate = 1;
2928 if(options.tags_ecc_overridden)
2929 param->noTagsECC = !options.tags_ecc_on;
2931 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2932 param->emptyLostAndFound = 1;
2935 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2936 param->refreshPeriod = 0;
2938 param->refreshPeriod = 500;
2941 if(options.empty_lost_and_found_overridden)
2942 param->emptyLostAndFound = options.empty_lost_and_found;
2944 /* ... and the functions. */
2945 if (yaffsVersion == 2) {
2946 param->writeChunkWithTagsToNAND =
2947 nandmtd2_WriteChunkWithTagsToNAND;
2948 param->readChunkWithTagsFromNAND =
2949 nandmtd2_ReadChunkWithTagsFromNAND;
2950 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2951 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2952 yaffs_DeviceToLC(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2953 param->isYaffs2 = 1;
2954 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2955 param->totalBytesPerChunk = mtd->writesize;
2956 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2958 param->totalBytesPerChunk = mtd->oobblock;
2959 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2961 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2963 param->startBlock = 0;
2964 param->endBlock = nBlocks - 1;
2966 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2967 /* use the MTD interface in yaffs_mtdif1.c */
2968 param->writeChunkWithTagsToNAND =
2969 nandmtd1_WriteChunkWithTagsToNAND;
2970 param->readChunkWithTagsFromNAND =
2971 nandmtd1_ReadChunkWithTagsFromNAND;
2972 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2973 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2975 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2976 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2978 param->isYaffs2 = 0;
2980 /* ... and common functions */
2981 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2982 param->initialiseNAND = nandmtd_InitialiseNAND;
2984 yaffs_DeviceToLC(dev)->putSuperFunc = yaffs_MTDPutSuper;
2986 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2987 param->gcControl = yaffs_gc_control_callback;
2989 yaffs_DeviceToLC(dev)->superBlock= sb;
2992 #ifndef CONFIG_YAFFS_DOES_ECC
2993 param->useNANDECC = 1;
2996 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2997 param->wideTnodesDisabled = 1;
3000 param->skipCheckpointRead = options.skip_checkpoint_read;
3001 param->skipCheckpointWrite = options.skip_checkpoint_write;
3003 down(&yaffs_context_lock);
3004 /* Get a mount id */
3006 for(mount_id=0; ! found; mount_id++){
3008 ylist_for_each(l,&yaffs_context_list){
3009 context_iterator = ylist_entry(l,struct yaffs_LinuxContext,contextList);
3010 if(context_iterator->mount_id == mount_id)
3014 context->mount_id = mount_id;
3016 ylist_add_tail(&(yaffs_DeviceToLC(dev)->contextList), &yaffs_context_list);
3017 up(&yaffs_context_lock);
3019 /* Directory search handling...*/
3020 YINIT_LIST_HEAD(&(yaffs_DeviceToLC(dev)->searchContexts));
3021 param->removeObjectCallback = yaffs_RemoveObjectCallback;
3023 init_MUTEX(&(yaffs_DeviceToLC(dev)->grossLock));
3025 yaffs_GrossLock(dev);
3027 err = yaffs_GutsInitialise(dev);
3030 (TSTR("yaffs_read_super: guts initialised %s\n"),
3031 (err == YAFFS_OK) ? "OK" : "FAILED"));
3034 yaffs_BackgroundStart(dev);
3036 if(!context->bgThread)
3037 param->deferDirectoryUpdate = 0;
3040 /* Release lock before yaffs_get_inode() */
3041 yaffs_GrossUnlock(dev);
3043 /* Create root inode */
3044 if (err == YAFFS_OK)
3045 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3051 inode->i_op = &yaffs_dir_inode_operations;
3052 inode->i_fop = &yaffs_dir_operations;
3054 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
3056 root = d_alloc_root(inode);
3058 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
3065 sb->s_dirt = !dev->isCheckpointed;
3066 T(YAFFS_TRACE_ALWAYS,
3067 (TSTR("yaffs_read_super: isCheckpointed %d\n"),
3068 dev->isCheckpointed));
3070 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
3075 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3076 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3079 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3082 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3083 static int yaffs_read_super(struct file_system_type *fs,
3084 int flags, const char *dev_name,
3085 void *data, struct vfsmount *mnt)
3088 return get_sb_bdev(fs, flags, dev_name, data,
3089 yaffs_internal_read_super_mtd, mnt);
3092 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3093 int flags, const char *dev_name,
3097 return get_sb_bdev(fs, flags, dev_name, data,
3098 yaffs_internal_read_super_mtd);
3102 static struct file_system_type yaffs_fs_type = {
3103 .owner = THIS_MODULE,
3105 .get_sb = yaffs_read_super,
3106 .kill_sb = kill_block_super,
3107 .fs_flags = FS_REQUIRES_DEV,
3110 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3113 return yaffs_internal_read_super(1, sb, data, silent);
3116 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3121 #ifdef CONFIG_YAFFS_YAFFS2
3123 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3124 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3127 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3130 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3131 static int yaffs2_read_super(struct file_system_type *fs,
3132 int flags, const char *dev_name, void *data,
3133 struct vfsmount *mnt)
3135 return get_sb_bdev(fs, flags, dev_name, data,
3136 yaffs2_internal_read_super_mtd, mnt);
3139 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3140 int flags, const char *dev_name,
3144 return get_sb_bdev(fs, flags, dev_name, data,
3145 yaffs2_internal_read_super_mtd);
3149 static struct file_system_type yaffs2_fs_type = {
3150 .owner = THIS_MODULE,
3152 .get_sb = yaffs2_read_super,
3153 .kill_sb = kill_block_super,
3154 .fs_flags = FS_REQUIRES_DEV,
3157 static struct super_block *yaffs2_read_super(struct super_block *sb,
3158 void *data, int silent)
3160 return yaffs_internal_read_super(2, sb, data, silent);
3163 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3167 #endif /* CONFIG_YAFFS_YAFFS2 */
3169 static struct proc_dir_entry *my_proc_entry;
3170 static struct proc_dir_entry *debug_proc_entry;
3172 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
3174 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
3175 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
3176 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
3177 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
3178 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
3179 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
3180 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
3181 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
3182 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
3183 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
3184 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
3185 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
3187 buf += sprintf(buf, "\n");
3193 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
3195 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3196 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3197 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3198 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3199 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3200 buf += sprintf(buf, "\n");
3201 buf += sprintf(buf, "nTnodes............ %d\n", dev->nTnodes);
3202 buf += sprintf(buf, "nObjects........... %d\n", dev->nObjects);
3203 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3204 buf += sprintf(buf, "\n");
3205 buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
3206 buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
3207 buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
3208 buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
3209 buf += sprintf(buf, "allGCs............. %u\n", dev->allGCs);
3210 buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGCs);
3211 buf += sprintf(buf, "oldestDirtyGCs..... %u\n", dev->oldestDirtyGCs);
3212 buf += sprintf(buf, "nGCBlocks.......... %u\n", dev->nGCBlocks);
3213 buf += sprintf(buf, "backgroundGCs...... %u\n", dev->backgroundGCs);
3214 buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
3215 buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
3216 buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
3217 buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
3218 buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
3219 buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
3220 buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
3221 buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
3222 buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
3223 buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
3225 sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
3230 static int yaffs_proc_read(char *page,
3232 off_t offset, int count, int *eof, void *data)
3234 struct ylist_head *item;
3239 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3240 * We use 'offset' (*ppos) to indicate where we are in devList.
3241 * This also assumes the user has posted a read buffer large
3242 * enough to hold the complete output; but that's life in /proc.
3247 /* Print header first */
3249 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__"\n");
3251 buf += sprintf(buf,"\n");
3255 down(&yaffs_context_lock);
3257 /* Locate and print the Nth entry. Order N-squared but N is small. */
3258 ylist_for_each(item, &yaffs_context_list) {
3259 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3260 yaffs_Device *dev = dc->dev;
3262 if (n < (step & ~1)) {
3267 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
3268 buf = yaffs_dump_dev_part0(buf, dev);
3270 buf = yaffs_dump_dev_part1(buf, dev);
3274 up(&yaffs_context_lock);
3277 return buf - page < count ? buf - page : count;
3280 static int yaffs_stats_proc_read(char *page,
3282 off_t offset, int count, int *eof, void *data)
3284 struct ylist_head *item;
3288 down(&yaffs_context_lock);
3290 /* Locate and print the Nth entry. Order N-squared but N is small. */
3291 ylist_for_each(item, &yaffs_context_list) {
3292 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3293 yaffs_Device *dev = dc->dev;
3297 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3299 buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
3300 n, dev->nFreeChunks, erasedChunks,
3301 dev->backgroundGCs, dev->oldestDirtyGCs,
3302 dev->nObjects, dev->nTnodes);
3304 up(&yaffs_context_lock);
3307 return buf - page < count ? buf - page : count;
3311 * Set the verbosity of the warnings and error messages.
3313 * Note that the names can only be a..z or _ with the current code.
3318 unsigned mask_bitfield;
3320 {"allocate", YAFFS_TRACE_ALLOCATE},
3321 {"always", YAFFS_TRACE_ALWAYS},
3322 {"background", YAFFS_TRACE_BACKGROUND},
3323 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3324 {"buffers", YAFFS_TRACE_BUFFERS},
3325 {"bug", YAFFS_TRACE_BUG},
3326 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3327 {"deletion", YAFFS_TRACE_DELETION},
3328 {"erase", YAFFS_TRACE_ERASE},
3329 {"error", YAFFS_TRACE_ERROR},
3330 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3331 {"gc", YAFFS_TRACE_GC},
3332 {"lock", YAFFS_TRACE_LOCK},
3333 {"mtd", YAFFS_TRACE_MTD},
3334 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3335 {"os", YAFFS_TRACE_OS},
3336 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3337 {"scan", YAFFS_TRACE_SCAN},
3338 {"tracing", YAFFS_TRACE_TRACING},
3339 {"sync", YAFFS_TRACE_SYNC},
3340 {"write", YAFFS_TRACE_WRITE},
3342 {"verify", YAFFS_TRACE_VERIFY},
3343 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3344 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3345 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3347 {"all", 0xffffffff},
3352 #define MAX_MASK_NAME_LENGTH 40
3353 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3354 unsigned long count, void *data)
3356 unsigned rg = 0, mask_bitfield;
3360 char substring[MAX_MASK_NAME_LENGTH + 1];
3366 rg = yaffs_traceMask;
3368 while (!done && (pos < count)) {
3370 while ((pos < count) && isspace(buf[pos]))
3387 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3389 if (end > buf + pos) {
3390 mask_name = "numeral";
3391 len = end - (buf + pos);
3395 for (x = buf + pos, i = 0;
3396 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3397 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3399 substring[i] = '\0';
3401 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3402 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3403 mask_name = mask_flags[i].mask_name;
3404 mask_bitfield = mask_flags[i].mask_bitfield;
3411 if (mask_name != NULL) {
3415 rg &= ~mask_bitfield;
3418 rg |= mask_bitfield;
3424 rg |= mask_bitfield;
3430 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3432 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
3434 if (rg & YAFFS_TRACE_ALWAYS) {
3435 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3437 flag = ((rg & mask_flags[i].mask_bitfield) ==
3438 mask_flags[i].mask_bitfield) ? '+' : '-';
3439 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3447 static int yaffs_proc_write(struct file *file, const char *buf,
3448 unsigned long count, void *data)
3450 return yaffs_proc_write_trace_options(file, buf, count, data);
3453 /* Stuff to handle installation of file systems */
3454 struct file_system_to_install {
3455 struct file_system_type *fst;
3459 static struct file_system_to_install fs_to_install[] = {
3460 {&yaffs_fs_type, 0},
3461 {&yaffs2_fs_type, 0},
3465 static int __init init_yaffs_fs(void)
3468 struct file_system_to_install *fsinst;
3470 T(YAFFS_TRACE_ALWAYS,
3471 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3473 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3474 T(YAFFS_TRACE_ALWAYS,
3475 (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3481 init_MUTEX(&yaffs_context_lock);
3483 /* Install the proc_fs entries */
3484 my_proc_entry = create_proc_entry("yaffs",
3488 if (my_proc_entry) {
3489 my_proc_entry->write_proc = yaffs_proc_write;
3490 my_proc_entry->read_proc = yaffs_proc_read;
3491 my_proc_entry->data = NULL;
3495 debug_proc_entry = create_proc_entry("yaffs_stats",
3499 if (debug_proc_entry) {
3500 debug_proc_entry->write_proc = NULL;
3501 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3502 debug_proc_entry->data = NULL;
3506 /* Now add the file system entries */
3508 fsinst = fs_to_install;
3510 while (fsinst->fst && !error) {
3511 error = register_filesystem(fsinst->fst);
3513 fsinst->installed = 1;
3517 /* Any errors? uninstall */
3519 fsinst = fs_to_install;
3521 while (fsinst->fst) {
3522 if (fsinst->installed) {
3523 unregister_filesystem(fsinst->fst);
3524 fsinst->installed = 0;
3533 static void __exit exit_yaffs_fs(void)
3536 struct file_system_to_install *fsinst;
3538 T(YAFFS_TRACE_ALWAYS,
3539 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3541 remove_proc_entry("yaffs", YPROC_ROOT);
3542 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3544 fsinst = fs_to_install;
3546 while (fsinst->fst) {
3547 if (fsinst->installed) {
3548 unregister_filesystem(fsinst->fst);
3549 fsinst->installed = 0;
3555 module_init(init_yaffs_fs)
3556 module_exit(exit_yaffs_fs)
3558 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3559 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3560 MODULE_LICENSE("GPL");