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 struct yaffs_dev associated with
29 * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
31 * >> inode->u.generic_ip points to the associated struct yaffs_obj.
35 * There are two variants of the VFS glue code. This variant should compile
36 * for any version of Linux.
38 #include <linux/version.h>
40 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
41 #define YAFFS_COMPILE_BACKGROUND
42 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23))
43 #define YAFFS_COMPILE_FREEZER
47 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
48 #define YAFFS_COMPILE_EXPORTFS
51 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
52 #define YAFFS_USE_SETATTR_COPY
53 #define YAFFS_USE_TRUNCATE_SETSIZE
55 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
56 #define YAFFS_HAS_EVICT_INODE
59 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
60 #define YAFFS_NEW_FOLLOW_LINK 1
62 #define YAFFS_NEW_FOLLOW_LINK 0
65 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
66 #include <linux/config.h>
69 #include <linux/kernel.h>
70 #include <linux/module.h>
71 #include <linux/slab.h>
72 #include <linux/init.h>
74 #include <linux/proc_fs.h>
75 #include <linux/smp_lock.h>
76 #include <linux/pagemap.h>
77 #include <linux/mtd/mtd.h>
78 #include <linux/interrupt.h>
79 #include <linux/string.h>
80 #include <linux/ctype.h>
82 #if (YAFFS_NEW_FOLLOW_LINK == 1)
83 #include <linux/namei.h>
86 #ifdef YAFFS_COMPILE_EXPORTFS
87 #include <linux/exportfs.h>
90 #ifdef YAFFS_COMPILE_BACKGROUND
91 #include <linux/kthread.h>
92 #include <linux/delay.h>
94 #ifdef YAFFS_COMPILE_FREEZER
95 #include <linux/freezer.h>
98 #include <asm/div64.h>
100 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
102 #include <linux/statfs.h>
104 #define UnlockPage(p) unlock_page(p)
105 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
107 /* FIXME: use sb->s_id instead ? */
108 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
112 #include <linux/locks.h>
113 #define BDEVNAME_SIZE 0
114 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
116 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
117 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
123 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
124 #define YPROC_ROOT (&proc_root)
126 #define YPROC_ROOT NULL
129 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
130 #define Y_INIT_TIMER(a) init_timer(a)
132 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
135 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
136 #define WRITE_SIZE_STR "writesize"
137 #define WRITE_SIZE(mtd) ((mtd)->writesize)
139 #define WRITE_SIZE_STR "oobblock"
140 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
143 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
144 #define YAFFS_USE_WRITE_BEGIN_END 1
146 #define YAFFS_USE_WRITE_BEGIN_END 0
149 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
150 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
152 uint64_t result = partition_size;
153 do_div(result, block_size);
154 return (uint32_t) result;
157 #define YCALCBLOCKS(s, b) ((s)/(b))
160 #include <linux/uaccess.h>
161 #include <linux/mtd/mtd.h>
163 #include "yportenv.h"
164 #include "yaffs_trace.h"
165 #include "yaffs_guts.h"
166 #include "yaffs_attribs.h"
168 #include "yaffs_linux.h"
170 #include "yaffs_mtdif.h"
171 #include "yaffs_mtdif1.h"
172 #include "yaffs_mtdif2.h"
174 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
175 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
176 unsigned int yaffs_auto_checkpoint = 1;
177 unsigned int yaffs_gc_control = 1;
178 unsigned int yaffs_bg_enable = 1;
180 /* Module Parameters */
181 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
182 module_param(yaffs_trace_mask, uint, 0644);
183 module_param(yaffs_wr_attempts, uint, 0644);
184 module_param(yaffs_auto_checkpoint, uint, 0644);
185 module_param(yaffs_gc_control, uint, 0644);
186 module_param(yaffs_bg_enable, uint, 0644);
188 MODULE_PARM(yaffs_trace_mask, "i");
189 MODULE_PARM(yaffs_wr_attempts, "i");
190 MODULE_PARM(yaffs_auto_checkpoint, "i");
191 MODULE_PARM(yaffs_gc_control, "i");
194 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
195 /* use iget and read_inode */
196 #define Y_IGET(sb, inum) iget((sb), (inum))
197 static void yaffs_read_inode(struct inode *inode);
200 /* Call local equivalent */
201 #define YAFFS_USE_OWN_IGET
202 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
204 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
207 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
208 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
210 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
213 #define yaffs_inode_to_obj(iptr) \
214 ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
215 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
217 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
218 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
220 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
223 #define update_dir_time(dir) do {\
224 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
227 static void yaffs_put_super(struct super_block *sb);
229 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
231 static ssize_t yaffs_hold_space(struct file *f);
232 static void yaffs_release_space(struct file *f);
234 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
235 static int yaffs_file_flush(struct file *file, fl_owner_t id);
237 static int yaffs_file_flush(struct file *file);
240 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
241 static int yaffs_sync_object(struct file *file, int datasync);
243 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
247 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
249 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
250 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
251 struct nameidata *n);
252 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
253 struct nameidata *n);
255 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
256 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
258 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
259 struct dentry *dentry);
260 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
261 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
262 const char *symname);
263 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
265 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
266 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
269 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
272 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
273 struct inode *new_dir, struct dentry *new_dentry);
274 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
276 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
277 static int yaffs_sync_fs(struct super_block *sb, int wait);
278 static void yaffs_write_super(struct super_block *sb);
280 static int yaffs_sync_fs(struct super_block *sb);
281 static int yaffs_write_super(struct super_block *sb);
284 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
285 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
286 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
287 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
289 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
292 #ifdef YAFFS_HAS_PUT_INODE
293 static void yaffs_put_inode(struct inode *inode);
296 #ifdef YAFFS_HAS_EVICT_INODE
297 static void yaffs_evict_inode(struct inode *);
299 static void yaffs_delete_inode(struct inode *);
300 static void yaffs_clear_inode(struct inode *);
303 static int yaffs_readpage(struct file *file, struct page *page);
304 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
305 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
307 static int yaffs_writepage(struct page *page);
310 #ifdef CONFIG_YAFFS_XATTR
311 static int yaffs_setxattr(struct dentry *dentry, const char *name,
312 const void *value, size_t size, int flags);
313 static ssize_t yaffs_getxattr(struct dentry *dentry, const char *name,
314 void *buff, size_t size);
315 static int yaffs_removexattr(struct dentry *dentry, const char *name);
316 static ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
319 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
320 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
321 loff_t pos, unsigned len, unsigned flags,
322 struct page **pagep, void **fsdata);
323 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
324 loff_t pos, unsigned len, unsigned copied,
325 struct page *pg, void *fsdadata);
327 static int yaffs_prepare_write(struct file *f, struct page *pg,
328 unsigned offset, unsigned to);
329 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
334 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
336 #if (YAFFS_NEW_FOLLOW_LINK == 1)
337 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
338 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
340 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
343 static void yaffs_touch_super(struct yaffs_dev *dev);
345 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
347 static struct address_space_operations yaffs_file_address_operations = {
348 .readpage = yaffs_readpage,
349 .writepage = yaffs_writepage,
350 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
351 .write_begin = yaffs_write_begin,
352 .write_end = yaffs_write_end,
354 .prepare_write = yaffs_prepare_write,
355 .commit_write = yaffs_commit_write,
359 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
360 static const struct file_operations yaffs_file_operations = {
361 .read = do_sync_read,
362 .write = do_sync_write,
363 .aio_read = generic_file_aio_read,
364 .aio_write = generic_file_aio_write,
365 .mmap = generic_file_mmap,
366 .flush = yaffs_file_flush,
367 .fsync = yaffs_sync_object,
368 .splice_read = generic_file_splice_read,
369 .splice_write = generic_file_splice_write,
370 .llseek = generic_file_llseek,
373 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
375 static const struct file_operations yaffs_file_operations = {
376 .read = do_sync_read,
377 .write = do_sync_write,
378 .aio_read = generic_file_aio_read,
379 .aio_write = generic_file_aio_write,
380 .mmap = generic_file_mmap,
381 .flush = yaffs_file_flush,
382 .fsync = yaffs_sync_object,
383 .sendfile = generic_file_sendfile,
388 static const struct file_operations yaffs_file_operations = {
389 .read = generic_file_read,
390 .write = generic_file_write,
391 .mmap = generic_file_mmap,
392 .flush = yaffs_file_flush,
393 .fsync = yaffs_sync_object,
394 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
395 .sendfile = generic_file_sendfile,
400 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
401 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
403 void *kaddr = kmap_atomic(page, KM_USER0);
404 memset(kaddr + start, 0, end - start);
405 kunmap_atomic(kaddr, KM_USER0);
406 flush_dcache_page(page);
410 static const struct inode_operations yaffs_file_inode_operations = {
411 .setattr = yaffs_setattr,
412 #ifdef CONFIG_YAFFS_XATTR
413 .setxattr = yaffs_setxattr,
414 .getxattr = yaffs_getxattr,
415 .listxattr = yaffs_listxattr,
416 .removexattr = yaffs_removexattr,
420 static const struct inode_operations yaffs_symlink_inode_operations = {
421 .readlink = yaffs_readlink,
422 .follow_link = yaffs_follow_link,
423 #if (YAFFS_NEW_FOLLOW_LINK == 1)
424 .put_link = yaffs_put_link,
426 .setattr = yaffs_setattr,
427 #ifdef CONFIG_YAFFS_XATTR
428 .setxattr = yaffs_setxattr,
429 .getxattr = yaffs_getxattr,
430 .listxattr = yaffs_listxattr,
431 .removexattr = yaffs_removexattr,
435 static const struct inode_operations yaffs_dir_inode_operations = {
436 .create = yaffs_create,
437 .lookup = yaffs_lookup,
439 .unlink = yaffs_unlink,
440 .symlink = yaffs_symlink,
441 .mkdir = yaffs_mkdir,
442 .rmdir = yaffs_unlink,
443 .mknod = yaffs_mknod,
444 .rename = yaffs_rename,
445 .setattr = yaffs_setattr,
446 #ifdef CONFIG_YAFFS_XATTR
447 .setxattr = yaffs_setxattr,
448 .getxattr = yaffs_getxattr,
449 .listxattr = yaffs_listxattr,
450 .removexattr = yaffs_removexattr,
454 static const struct file_operations yaffs_dir_operations = {
455 .read = generic_read_dir,
456 .readdir = yaffs_readdir,
457 .fsync = yaffs_sync_object,
458 .llseek = generic_file_llseek,
461 static const struct super_operations yaffs_super_ops = {
462 .statfs = yaffs_statfs,
464 #ifndef YAFFS_USE_OWN_IGET
465 .read_inode = yaffs_read_inode,
467 #ifdef YAFFS_HAS_PUT_INODE
468 .put_inode = yaffs_put_inode,
470 .put_super = yaffs_put_super,
471 #ifdef YAFFS_HAS_EVICT_INODE
472 .evict_inode = yaffs_evict_inode,
474 .delete_inode = yaffs_delete_inode,
475 .clear_inode = yaffs_clear_inode,
477 .sync_fs = yaffs_sync_fs,
478 .write_super = yaffs_write_super,
481 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
483 #ifdef YAFFS_USE_SETATTR_COPY
484 setattr_copy(inode, attr);
487 return inode_setattr(inode, attr);
492 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
494 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
495 truncate_setsize(inode, newsize);
498 truncate_inode_pages(&inode->i_data, newsize);
504 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
506 return yaffs_gc_control;
509 static void yaffs_gross_lock(struct yaffs_dev *dev)
511 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
512 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
513 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
516 static void yaffs_gross_unlock(struct yaffs_dev *dev)
518 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
519 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
522 #ifdef YAFFS_COMPILE_EXPORTFS
524 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
527 return Y_IGET(sb, ino);
530 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
531 struct fid *fid, int fh_len,
534 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
535 yaffs2_nfs_get_inode);
538 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
539 struct fid *fid, int fh_len,
542 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
543 yaffs2_nfs_get_inode);
546 struct dentry *yaffs2_get_parent(struct dentry *dentry)
549 struct super_block *sb = dentry->d_inode->i_sb;
550 struct dentry *parent = ERR_PTR(-ENOENT);
552 unsigned long parent_ino;
553 struct yaffs_obj *d_obj;
554 struct yaffs_obj *parent_obj;
556 d_obj = yaffs_inode_to_obj(dentry->d_inode);
559 parent_obj = d_obj->parent;
561 parent_ino = yaffs_get_obj_inode(parent_obj);
562 inode = Y_IGET(sb, parent_ino);
565 parent = ERR_CAST(inode);
567 parent = d_obtain_alias(inode);
568 if (!IS_ERR(parent)) {
569 parent = ERR_PTR(-ENOMEM);
579 /* Just declare a zero structure as a NULL value implies
580 * using the default functions of exportfs.
583 static struct export_operations yaffs_export_ops = {
584 .fh_to_dentry = yaffs2_fh_to_dentry,
585 .fh_to_parent = yaffs2_fh_to_parent,
586 .get_parent = yaffs2_get_parent,
591 /*-----------------------------------------------------------------*/
592 /* Directory search context allows us to unlock access to yaffs during
593 * filldir without causing problems with the directory being modified.
594 * This is similar to the tried and tested mechanism used in yaffs direct.
596 * A search context iterates along a doubly linked list of siblings in the
597 * directory. If the iterating object is deleted then this would corrupt
598 * the list iteration, likely causing a crash. The search context avoids
599 * this by using the remove_obj_fn to move the search context to the
600 * next object before the object is deleted.
602 * Many readdirs (and thus seach conexts) may be alive simulateously so
603 * each struct yaffs_dev has a list of these.
605 * A seach context lives for the duration of a readdir.
607 * All these functions must be called while yaffs is locked.
610 struct yaffs_search_context {
611 struct yaffs_dev *dev;
612 struct yaffs_obj *dir_obj;
613 struct yaffs_obj *next_return;
614 struct list_head others;
618 * yaffs_new_search() creates a new search context, initialises it and
619 * adds it to the device's search context list.
621 * Called at start of readdir.
623 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
625 struct yaffs_dev *dev = dir->my_dev;
626 struct yaffs_search_context *sc =
627 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
631 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
632 sc->next_return = NULL;
635 list_entry(dir->variant.dir_variant.children.next,
636 struct yaffs_obj, siblings);
637 INIT_LIST_HEAD(&sc->others);
638 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
644 * yaffs_search_end() disposes of a search context and cleans up.
646 static void yaffs_search_end(struct yaffs_search_context *sc)
649 list_del(&sc->others);
655 * yaffs_search_advance() moves a search context to the next object.
656 * Called when the search iterates or when an object removal causes
657 * the search context to be moved to the next object.
659 static void yaffs_search_advance(struct yaffs_search_context *sc)
664 if (sc->next_return == NULL ||
665 list_empty(&sc->dir_obj->variant.dir_variant.children))
666 sc->next_return = NULL;
668 struct list_head *next = sc->next_return->siblings.next;
670 if (next == &sc->dir_obj->variant.dir_variant.children)
671 sc->next_return = NULL; /* end of list */
674 list_entry(next, struct yaffs_obj, siblings);
679 * yaffs_remove_obj_callback() is called when an object is unlinked.
680 * We check open search contexts and advance any which are currently
681 * on the object being iterated.
683 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
687 struct yaffs_search_context *sc;
688 struct list_head *search_contexts =
689 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
691 /* Iterate through the directory search contexts.
692 * If any are currently on the object being removed, then advance
693 * the search context to the next object to prevent a hanging pointer.
695 list_for_each(i, search_contexts) {
696 sc = list_entry(i, struct yaffs_search_context, others);
697 if (sc->next_return == obj)
698 yaffs_search_advance(sc);
703 /*-----------------------------------------------------------------*/
705 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
708 unsigned char *alias;
711 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
713 yaffs_gross_lock(dev);
715 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
717 yaffs_gross_unlock(dev);
722 ret = vfs_readlink(dentry, buffer, buflen, alias);
727 #if (YAFFS_NEW_FOLLOW_LINK == 1)
728 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
732 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
736 unsigned char *alias;
738 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
740 yaffs_gross_lock(dev);
742 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
743 yaffs_gross_unlock(dev);
749 #if (YAFFS_NEW_FOLLOW_LINK == 1)
750 nd_set_link(nd, alias);
754 ret = ERR_PTR(ret_int);
757 ret = vfs_follow_link(nd, alias);
766 #if (YAFFS_NEW_FOLLOW_LINK == 1)
767 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
773 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
774 struct yaffs_obj *obj);
777 * Lookup is used to find objects in the fs
779 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
781 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
784 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
787 struct yaffs_obj *obj;
788 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
790 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
792 if (current != yaffs_dev_to_lc(dev)->readdir_process)
793 yaffs_gross_lock(dev);
795 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
796 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
798 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
800 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
802 /* Can't hold gross lock when calling yaffs_get_inode() */
803 if (current != yaffs_dev_to_lc(dev)->readdir_process)
804 yaffs_gross_unlock(dev);
807 yaffs_trace(YAFFS_TRACE_OS,
808 "yaffs_lookup found %d", obj->obj_id);
810 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
812 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
816 /* added NCB for 2.5/6 compatability - forces add even if inode is
817 * NULL which creates dentry hash */
818 d_add(dentry, inode);
823 #ifdef YAFFS_HAS_PUT_INODE
825 /* For now put inode is just for debugging
826 * Put inode is called when the inode **structure** is put.
828 static void yaffs_put_inode(struct inode *inode)
830 yaffs_trace(YAFFS_TRACE_OS,
831 "yaffs_put_inode: ino %d, count %d"),
832 (int)inode->i_ino, atomic_read(&inode->i_count);
837 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
839 /* Clear the association between the inode and
840 * the struct yaffs_obj.
842 obj->my_inode = NULL;
843 yaffs_inode_to_obj_lv(inode) = NULL;
845 /* If the object freeing was deferred, then the real
847 * This should fix the inode inconsistency problem.
849 yaffs_handle_defered_free(obj);
852 #ifdef YAFFS_HAS_EVICT_INODE
853 /* yaffs_evict_inode combines into one operation what was previously done in
854 * yaffs_clear_inode() and yaffs_delete_inode()
857 static void yaffs_evict_inode(struct inode *inode)
859 struct yaffs_obj *obj;
860 struct yaffs_dev *dev;
863 obj = yaffs_inode_to_obj(inode);
865 yaffs_trace(YAFFS_TRACE_OS,
866 "yaffs_evict_inode: ino %d, count %d %s",
867 (int)inode->i_ino, atomic_read(&inode->i_count),
868 obj ? "object exists" : "null object");
870 if (!inode->i_nlink && !is_bad_inode(inode))
872 truncate_inode_pages(&inode->i_data, 0);
873 end_writeback(inode);
875 if (deleteme && obj) {
877 yaffs_gross_lock(dev);
879 yaffs_gross_unlock(dev);
883 yaffs_gross_lock(dev);
884 yaffs_unstitch_obj(inode, obj);
885 yaffs_gross_unlock(dev);
890 /* clear is called to tell the fs to release any per-inode data it holds.
891 * The object might still exist on disk and is just being thrown out of the cache
892 * or else the object has actually been deleted and we're being called via
894 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
897 static void yaffs_clear_inode(struct inode *inode)
899 struct yaffs_obj *obj;
900 struct yaffs_dev *dev;
902 obj = yaffs_inode_to_obj(inode);
904 yaffs_trace(YAFFS_TRACE_OS,
905 "yaffs_clear_inode: ino %d, count %d %s",
906 (int)inode->i_ino, atomic_read(&inode->i_count),
907 obj ? "object exists" : "null object");
911 yaffs_gross_lock(dev);
912 yaffs_unstitch_obj(inode, obj);
913 yaffs_gross_unlock(dev);
918 /* delete is called when the link count is zero and the inode
919 * is put (ie. nobody wants to know about it anymore, time to
921 * NB Must call clear_inode()
923 static void yaffs_delete_inode(struct inode *inode)
925 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
926 struct yaffs_dev *dev;
928 yaffs_trace(YAFFS_TRACE_OS,
929 "yaffs_delete_inode: ino %d, count %d %s",
930 (int)inode->i_ino, atomic_read(&inode->i_count),
931 obj ? "object exists" : "null object");
935 yaffs_gross_lock(dev);
937 yaffs_gross_unlock(dev);
939 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
940 truncate_inode_pages(&inode->i_data, 0);
946 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
947 static int yaffs_file_flush(struct file *file, fl_owner_t id)
949 static int yaffs_file_flush(struct file *file)
952 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
954 struct yaffs_dev *dev = obj->my_dev;
956 yaffs_trace(YAFFS_TRACE_OS,
957 "yaffs_file_flush object %d (%s)",
959 obj->dirty ? "dirty" : "clean");
961 yaffs_gross_lock(dev);
963 yaffs_flush_file(obj, 1, 0);
965 yaffs_gross_unlock(dev);
970 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
972 /* Lifted from jffs2 */
974 struct yaffs_obj *obj;
975 unsigned char *pg_buf;
978 struct yaffs_dev *dev;
980 yaffs_trace(YAFFS_TRACE_OS,
981 "yaffs_readpage_nolock at %08x, size %08x",
982 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
983 (unsigned)PAGE_CACHE_SIZE);
985 obj = yaffs_dentry_to_obj(f->f_dentry);
989 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
990 BUG_ON(!PageLocked(pg));
997 /* FIXME: Can kmap fail? */
999 yaffs_gross_lock(dev);
1001 ret = yaffs_file_rd(obj, pg_buf,
1002 pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
1004 yaffs_gross_unlock(dev);
1010 ClearPageUptodate(pg);
1013 SetPageUptodate(pg);
1017 flush_dcache_page(pg);
1020 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
1024 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1026 int ret = yaffs_readpage_nolock(f, pg);
1031 static int yaffs_readpage(struct file *f, struct page *pg)
1035 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
1036 ret = yaffs_readpage_unlock(f, pg);
1037 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
1041 /* writepage inspired by/stolen from smbfs */
1043 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1044 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1046 static int yaffs_writepage(struct page *page)
1049 struct yaffs_dev *dev;
1050 struct address_space *mapping = page->mapping;
1051 struct inode *inode;
1052 unsigned long end_index;
1054 struct yaffs_obj *obj;
1061 inode = mapping->host;
1064 i_size = i_size_read(inode);
1066 end_index = i_size >> PAGE_CACHE_SHIFT;
1068 if (page->index < end_index)
1069 n_bytes = PAGE_CACHE_SIZE;
1071 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
1073 if (page->index > end_index || !n_bytes) {
1074 yaffs_trace(YAFFS_TRACE_OS,
1075 "yaffs_writepage at %08x, inode size = %08x!!",
1076 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1077 (unsigned)inode->i_size);
1078 yaffs_trace(YAFFS_TRACE_OS,
1079 " -> don't care!!");
1081 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1082 set_page_writeback(page);
1084 end_page_writeback(page);
1089 if (n_bytes != PAGE_CACHE_SIZE)
1090 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
1094 buffer = kmap(page);
1096 obj = yaffs_inode_to_obj(inode);
1098 yaffs_gross_lock(dev);
1100 yaffs_trace(YAFFS_TRACE_OS,
1101 "yaffs_writepage at %08x, size %08x",
1102 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes);
1103 yaffs_trace(YAFFS_TRACE_OS,
1104 "writepag0: obj = %05x, ino = %05x",
1105 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1107 n_written = yaffs_wr_file(obj, buffer,
1108 page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
1110 yaffs_touch_super(dev);
1112 yaffs_trace(YAFFS_TRACE_OS,
1113 "writepag1: obj = %05x, ino = %05x",
1114 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1116 yaffs_gross_unlock(dev);
1119 set_page_writeback(page);
1121 end_page_writeback(page);
1124 return (n_written == n_bytes) ? 0 : -ENOSPC;
1127 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1128 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1129 loff_t pos, unsigned len, unsigned flags,
1130 struct page **pagep, void **fsdata)
1132 struct page *pg = NULL;
1133 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1139 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1140 pg = grab_cache_page_write_begin(mapping, index, flags);
1142 pg = __grab_cache_page(mapping, index);
1150 yaffs_trace(YAFFS_TRACE_OS,
1151 "start yaffs_write_begin index %d(%x) uptodate %d",
1152 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
1155 space_held = yaffs_hold_space(filp);
1162 /* Update page if required */
1164 if (!Page_Uptodate(pg))
1165 ret = yaffs_readpage_nolock(filp, pg);
1170 /* Happy path return */
1171 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
1176 yaffs_trace(YAFFS_TRACE_OS,
1177 "end yaffs_write_begin fail returning %d", ret);
1179 yaffs_release_space(filp);
1182 page_cache_release(pg);
1189 static int yaffs_prepare_write(struct file *f, struct page *pg,
1190 unsigned offset, unsigned to)
1192 yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
1194 if (!Page_Uptodate(pg))
1195 return yaffs_readpage_nolock(f, pg);
1200 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1201 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1202 loff_t pos, unsigned len, unsigned copied,
1203 struct page *pg, void *fsdadata)
1207 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1210 addr = kva + offset_into_page;
1212 yaffs_trace(YAFFS_TRACE_OS,
1213 "yaffs_write_end addr %p pos %x n_bytes %d",
1214 addr, (unsigned)pos, copied);
1216 ret = yaffs_file_write(filp, addr, copied, &pos);
1218 if (ret != copied) {
1219 yaffs_trace(YAFFS_TRACE_OS,
1220 "yaffs_write_end not same size ret %d copied %d",
1227 yaffs_release_space(filp);
1229 page_cache_release(pg);
1234 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1239 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1240 int n_bytes = to - offset;
1243 unsigned spos = pos;
1247 addr = kva + offset;
1249 saddr = (unsigned)addr;
1251 yaffs_trace(YAFFS_TRACE_OS,
1252 "yaffs_commit_write addr %x pos %x n_bytes %d",
1253 saddr, spos, n_bytes);
1255 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
1257 if (n_written != n_bytes) {
1258 yaffs_trace(YAFFS_TRACE_OS,
1259 "yaffs_commit_write not same size n_written %d n_bytes %d",
1260 n_written, n_bytes);
1265 yaffs_trace(YAFFS_TRACE_OS,
1266 "yaffs_commit_write returning %d",
1267 n_written == n_bytes ? 0 : n_written);
1269 return n_written == n_bytes ? 0 : n_written;
1273 static void yaffs_fill_inode_from_obj(struct inode *inode,
1274 struct yaffs_obj *obj)
1278 /* Check mode against the variant type and attempt to repair if broken. */
1279 u32 mode = obj->yst_mode;
1280 switch (obj->variant_type) {
1281 case YAFFS_OBJECT_TYPE_FILE:
1282 if (!S_ISREG(mode)) {
1283 obj->yst_mode &= ~S_IFMT;
1284 obj->yst_mode |= S_IFREG;
1288 case YAFFS_OBJECT_TYPE_SYMLINK:
1289 if (!S_ISLNK(mode)) {
1290 obj->yst_mode &= ~S_IFMT;
1291 obj->yst_mode |= S_IFLNK;
1295 case YAFFS_OBJECT_TYPE_DIRECTORY:
1296 if (!S_ISDIR(mode)) {
1297 obj->yst_mode &= ~S_IFMT;
1298 obj->yst_mode |= S_IFDIR;
1302 case YAFFS_OBJECT_TYPE_UNKNOWN:
1303 case YAFFS_OBJECT_TYPE_HARDLINK:
1304 case YAFFS_OBJECT_TYPE_SPECIAL:
1310 inode->i_flags |= S_NOATIME;
1312 inode->i_ino = obj->obj_id;
1313 inode->i_mode = obj->yst_mode;
1314 inode->i_uid = obj->yst_uid;
1315 inode->i_gid = obj->yst_gid;
1316 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1317 inode->i_blksize = inode->i_sb->s_blocksize;
1319 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1321 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1322 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1323 inode->i_atime.tv_nsec = 0;
1324 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1325 inode->i_mtime.tv_nsec = 0;
1326 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1327 inode->i_ctime.tv_nsec = 0;
1329 inode->i_rdev = obj->yst_rdev;
1330 inode->i_atime = obj->yst_atime;
1331 inode->i_mtime = obj->yst_mtime;
1332 inode->i_ctime = obj->yst_ctime;
1334 inode->i_size = yaffs_get_obj_length(obj);
1335 inode->i_blocks = (inode->i_size + 511) >> 9;
1337 inode->i_nlink = yaffs_get_obj_link_count(obj);
1339 yaffs_trace(YAFFS_TRACE_OS,
1340 "yaffs_fill_inode mode %x uid %d gid %d size %d count %d",
1341 inode->i_mode, inode->i_uid, inode->i_gid,
1342 (int)inode->i_size, atomic_read(&inode->i_count));
1344 switch (obj->yst_mode & S_IFMT) {
1345 default: /* fifo, device or socket */
1346 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1347 init_special_inode(inode, obj->yst_mode,
1348 old_decode_dev(obj->yst_rdev));
1350 init_special_inode(inode, obj->yst_mode,
1351 (dev_t) (obj->yst_rdev));
1354 case S_IFREG: /* file */
1355 inode->i_op = &yaffs_file_inode_operations;
1356 inode->i_fop = &yaffs_file_operations;
1357 inode->i_mapping->a_ops =
1358 &yaffs_file_address_operations;
1360 case S_IFDIR: /* directory */
1361 inode->i_op = &yaffs_dir_inode_operations;
1362 inode->i_fop = &yaffs_dir_operations;
1364 case S_IFLNK: /* symlink */
1365 inode->i_op = &yaffs_symlink_inode_operations;
1369 yaffs_inode_to_obj_lv(inode) = obj;
1371 obj->my_inode = inode;
1374 yaffs_trace(YAFFS_TRACE_OS,
1375 "yaffs_fill_inode invalid parameters");
1380 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1381 struct yaffs_obj *obj)
1383 struct inode *inode;
1386 yaffs_trace(YAFFS_TRACE_OS,
1387 "yaffs_get_inode for NULL super_block!!");
1393 yaffs_trace(YAFFS_TRACE_OS,
1394 "yaffs_get_inode for NULL object!!");
1399 yaffs_trace(YAFFS_TRACE_OS,
1400 "yaffs_get_inode for object %d", obj->obj_id);
1402 inode = Y_IGET(sb, obj->obj_id);
1406 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1407 /* iget also increments the inode's i_count */
1408 /* NB You can't be holding gross_lock or deadlock will happen! */
1413 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1416 struct yaffs_obj *obj;
1417 int n_written, ipos;
1418 struct inode *inode;
1419 struct yaffs_dev *dev;
1421 obj = yaffs_dentry_to_obj(f->f_dentry);
1424 yaffs_trace(YAFFS_TRACE_OS,
1425 "yaffs_file_write: hey obj is null!");
1431 yaffs_gross_lock(dev);
1433 inode = f->f_dentry->d_inode;
1435 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1436 ipos = inode->i_size;
1440 yaffs_trace(YAFFS_TRACE_OS,
1441 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %d(%x)",
1442 (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos);
1444 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1446 yaffs_touch_super(dev);
1448 yaffs_trace(YAFFS_TRACE_OS,
1449 "yaffs_file_write: %d(%x) bytes written",
1450 (unsigned)n, (unsigned)n);
1452 if (n_written > 0) {
1455 if (ipos > inode->i_size) {
1456 inode->i_size = ipos;
1457 inode->i_blocks = (ipos + 511) >> 9;
1459 yaffs_trace(YAFFS_TRACE_OS,
1460 "yaffs_file_write size updated to %d bytes, %d blocks",
1461 ipos, (int)(inode->i_blocks));
1465 yaffs_gross_unlock(dev);
1466 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1469 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1470 /* For now we just assume few parallel writes and check against a small number. */
1471 /* Todo: need to do this with a counter to handle parallel reads better */
1473 static ssize_t yaffs_hold_space(struct file *f)
1475 struct yaffs_obj *obj;
1476 struct yaffs_dev *dev;
1480 obj = yaffs_dentry_to_obj(f->f_dentry);
1484 yaffs_gross_lock(dev);
1486 n_free_chunks = yaffs_get_n_free_chunks(dev);
1488 yaffs_gross_unlock(dev);
1490 return (n_free_chunks > 20) ? 1 : 0;
1493 static void yaffs_release_space(struct file *f)
1495 struct yaffs_obj *obj;
1496 struct yaffs_dev *dev;
1498 obj = yaffs_dentry_to_obj(f->f_dentry);
1502 yaffs_gross_lock(dev);
1504 yaffs_gross_unlock(dev);
1507 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1509 struct yaffs_obj *obj;
1510 struct yaffs_dev *dev;
1511 struct yaffs_search_context *sc;
1512 struct inode *inode = f->f_dentry->d_inode;
1513 unsigned long offset, curoffs;
1514 struct yaffs_obj *l;
1517 char name[YAFFS_MAX_NAME_LENGTH + 1];
1519 obj = yaffs_dentry_to_obj(f->f_dentry);
1522 yaffs_gross_lock(dev);
1524 yaffs_dev_to_lc(dev)->readdir_process = current;
1528 sc = yaffs_new_search(obj);
1534 yaffs_trace(YAFFS_TRACE_OS,
1535 "yaffs_readdir: starting at %d", (int)offset);
1538 yaffs_trace(YAFFS_TRACE_OS,
1539 "yaffs_readdir: entry . ino %d",
1541 yaffs_gross_unlock(dev);
1542 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1543 yaffs_gross_lock(dev);
1546 yaffs_gross_lock(dev);
1551 yaffs_trace(YAFFS_TRACE_OS,
1552 "yaffs_readdir: entry .. ino %d",
1553 (int)f->f_dentry->d_parent->d_inode->i_ino);
1554 yaffs_gross_unlock(dev);
1555 if (filldir(dirent, "..", 2, offset,
1556 f->f_dentry->d_parent->d_inode->i_ino,
1558 yaffs_gross_lock(dev);
1561 yaffs_gross_lock(dev);
1568 /* If the directory has changed since the open or last call to
1569 readdir, rewind to after the 2 canned entries. */
1570 if (f->f_version != inode->i_version) {
1573 f->f_version = inode->i_version;
1576 while (sc->next_return) {
1578 l = sc->next_return;
1579 if (curoffs >= offset) {
1580 int this_inode = yaffs_get_obj_inode(l);
1581 int this_type = yaffs_get_obj_type(l);
1583 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1584 yaffs_trace(YAFFS_TRACE_OS,
1585 "yaffs_readdir: %s inode %d",
1586 name, yaffs_get_obj_inode(l));
1588 yaffs_gross_unlock(dev);
1593 offset, this_inode, this_type) < 0) {
1594 yaffs_gross_lock(dev);
1598 yaffs_gross_lock(dev);
1603 yaffs_search_advance(sc);
1607 yaffs_search_end(sc);
1608 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1609 yaffs_gross_unlock(dev);
1615 * File creation. Allocate an inode, and we're done..
1618 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1621 #define YCRED(x) (x->cred)
1624 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1625 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1628 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1632 struct inode *inode;
1634 struct yaffs_obj *obj = NULL;
1635 struct yaffs_dev *dev;
1637 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1639 int error = -ENOSPC;
1640 uid_t uid = YCRED(current)->fsuid;
1642 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1644 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1648 yaffs_trace(YAFFS_TRACE_OS,
1649 "yaffs_mknod: parent object %d type %d",
1650 parent->obj_id, parent->variant_type);
1652 yaffs_trace(YAFFS_TRACE_OS,
1653 "yaffs_mknod: could not get parent object");
1657 yaffs_trace(YAFFS_TRACE_OS,
1658 "yaffs_mknod: making oject for %s, mode %x dev %x",
1659 dentry->d_name.name, mode, rdev);
1661 dev = parent->my_dev;
1663 yaffs_gross_lock(dev);
1665 switch (mode & S_IFMT) {
1667 /* Special (socket, fifo, device...) */
1668 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
1669 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1671 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1672 gid, old_encode_dev(rdev));
1675 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1679 case S_IFREG: /* file */
1680 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
1681 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1684 case S_IFDIR: /* directory */
1685 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
1686 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1689 case S_IFLNK: /* symlink */
1690 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
1691 obj = NULL; /* Do we ever get here? */
1695 /* Can not call yaffs_get_inode() with gross lock held */
1696 yaffs_gross_unlock(dev);
1699 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1700 d_instantiate(dentry, inode);
1701 update_dir_time(dir);
1702 yaffs_trace(YAFFS_TRACE_OS,
1703 "yaffs_mknod created object %d count = %d",
1704 obj->obj_id, atomic_read(&inode->i_count));
1706 yaffs_fill_inode_from_obj(dir, parent);
1708 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
1715 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1718 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
1719 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1723 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1724 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1725 struct nameidata *n)
1727 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1730 yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
1731 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1734 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1738 struct yaffs_dev *dev;
1739 struct yaffs_obj *obj;
1741 yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
1742 (int)(dir->i_ino), dentry->d_name.name);
1743 obj = yaffs_inode_to_obj(dir);
1746 yaffs_gross_lock(dev);
1748 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1750 if (ret_val == YAFFS_OK) {
1751 dentry->d_inode->i_nlink--;
1753 yaffs_gross_unlock(dev);
1754 mark_inode_dirty(dentry->d_inode);
1755 update_dir_time(dir);
1758 yaffs_gross_unlock(dev);
1765 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1766 struct dentry *dentry)
1768 struct inode *inode = old_dentry->d_inode;
1769 struct yaffs_obj *obj = NULL;
1770 struct yaffs_obj *link = NULL;
1771 struct yaffs_dev *dev;
1773 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
1775 obj = yaffs_inode_to_obj(inode);
1778 yaffs_gross_lock(dev);
1780 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1782 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1786 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
1787 d_instantiate(dentry, old_dentry->d_inode);
1788 atomic_inc(&old_dentry->d_inode->i_count);
1789 yaffs_trace(YAFFS_TRACE_OS,
1790 "yaffs_link link count %d i_count %d",
1791 old_dentry->d_inode->i_nlink,
1792 atomic_read(&old_dentry->d_inode->i_count));
1795 yaffs_gross_unlock(dev);
1798 update_dir_time(dir);
1805 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1806 const char *symname)
1808 struct yaffs_obj *obj;
1809 struct yaffs_dev *dev;
1810 uid_t uid = YCRED(current)->fsuid;
1812 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1814 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
1816 dev = yaffs_inode_to_obj(dir)->my_dev;
1817 yaffs_gross_lock(dev);
1818 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1819 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1820 yaffs_gross_unlock(dev);
1823 struct inode *inode;
1825 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1826 d_instantiate(dentry, inode);
1827 update_dir_time(dir);
1828 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
1831 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
1837 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1838 static int yaffs_sync_object(struct file *file, int datasync)
1840 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1845 struct yaffs_obj *obj;
1846 struct yaffs_dev *dev;
1847 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1848 struct dentry *dentry = file->f_path.dentry;
1851 obj = yaffs_dentry_to_obj(dentry);
1855 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1856 "yaffs_sync_object");
1857 yaffs_gross_lock(dev);
1858 yaffs_flush_file(obj, 1, datasync);
1859 yaffs_gross_unlock(dev);
1864 * The VFS layer already does all the dentry stuff for rename.
1866 * NB: POSIX says you can rename an object over an old object of the same name
1868 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1869 struct inode *new_dir, struct dentry *new_dentry)
1871 struct yaffs_dev *dev;
1872 int ret_val = YAFFS_FAIL;
1873 struct yaffs_obj *target;
1875 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
1876 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1878 yaffs_gross_lock(dev);
1880 /* Check if the target is an existing directory that is not empty. */
1881 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1882 new_dentry->d_name.name);
1884 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1885 !list_empty(&target->variant.dir_variant.children)) {
1887 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
1889 ret_val = YAFFS_FAIL;
1891 /* Now does unlinking internally using shadowing mechanism */
1892 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
1894 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1895 old_dentry->d_name.name,
1896 yaffs_inode_to_obj(new_dir),
1897 new_dentry->d_name.name);
1899 yaffs_gross_unlock(dev);
1901 if (ret_val == YAFFS_OK) {
1903 new_dentry->d_inode->i_nlink--;
1904 mark_inode_dirty(new_dentry->d_inode);
1907 update_dir_time(old_dir);
1908 if (old_dir != new_dir)
1909 update_dir_time(new_dir);
1916 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1918 struct inode *inode = dentry->d_inode;
1920 struct yaffs_dev *dev;
1922 yaffs_trace(YAFFS_TRACE_OS,
1923 "yaffs_setattr of object %d",
1924 yaffs_inode_to_obj(inode)->obj_id);
1926 /* Fail if a requested resize >= 2GB */
1927 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
1931 error = inode_change_ok(inode, attr);
1935 error = yaffs_vfs_setattr(inode, attr);
1936 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
1937 if (attr->ia_valid & ATTR_SIZE) {
1938 yaffs_vfs_setsize(inode, attr->ia_size);
1939 inode->i_blocks = (inode->i_size + 511) >> 9;
1942 dev = yaffs_inode_to_obj(inode)->my_dev;
1943 if (attr->ia_valid & ATTR_SIZE) {
1944 yaffs_trace(YAFFS_TRACE_OS,
1946 (int)(attr->ia_size),
1947 (int)(attr->ia_size));
1949 yaffs_gross_lock(dev);
1950 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
1951 if (result == YAFFS_OK) {
1956 yaffs_gross_unlock(dev);
1960 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
1965 #ifdef CONFIG_YAFFS_XATTR
1966 static int yaffs_setxattr(struct dentry *dentry, const char *name,
1967 const void *value, size_t size, int flags)
1969 struct inode *inode = dentry->d_inode;
1971 struct yaffs_dev *dev;
1972 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1974 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
1979 yaffs_gross_lock(dev);
1980 result = yaffs_set_xattrib(obj, name, value, size, flags);
1981 if (result == YAFFS_OK)
1983 else if (result < 0)
1985 yaffs_gross_unlock(dev);
1988 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
1993 static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
1994 void *buff, size_t size)
1996 struct inode *inode = dentry->d_inode;
1998 struct yaffs_dev *dev;
1999 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2001 yaffs_trace(YAFFS_TRACE_OS,
2002 "yaffs_getxattr \"%s\" from object %d",
2007 yaffs_gross_lock(dev);
2008 error = yaffs_get_xattrib(obj, name, buff, size);
2009 yaffs_gross_unlock(dev);
2012 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
2017 static int yaffs_removexattr(struct dentry *dentry, const char *name)
2019 struct inode *inode = dentry->d_inode;
2021 struct yaffs_dev *dev;
2022 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2024 yaffs_trace(YAFFS_TRACE_OS,
2025 "yaffs_removexattr of object %d", obj->obj_id);
2030 yaffs_gross_lock(dev);
2031 result = yaffs_remove_xattrib(obj, name);
2032 if (result == YAFFS_OK)
2034 else if (result < 0)
2036 yaffs_gross_unlock(dev);
2039 yaffs_trace(YAFFS_TRACE_OS,
2040 "yaffs_removexattr done returning %d", error);
2045 static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
2047 struct inode *inode = dentry->d_inode;
2049 struct yaffs_dev *dev;
2050 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2052 yaffs_trace(YAFFS_TRACE_OS,
2053 "yaffs_listxattr of object %d", obj->obj_id);
2057 yaffs_gross_lock(dev);
2058 error = yaffs_list_xattrib(obj, buff, size);
2059 yaffs_gross_unlock(dev);
2062 yaffs_trace(YAFFS_TRACE_OS,
2063 "yaffs_listxattr done returning %d", error);
2070 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2071 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2073 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2074 struct super_block *sb = dentry->d_sb;
2075 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2076 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2078 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2080 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2082 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2085 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
2087 yaffs_gross_lock(dev);
2089 buf->f_type = YAFFS_MAGIC;
2090 buf->f_bsize = sb->s_blocksize;
2091 buf->f_namelen = 255;
2093 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2094 /* Do this if chunk size is not a power of 2 */
2096 uint64_t bytes_in_dev;
2097 uint64_t bytes_free;
2101 ((dev->param.end_block - dev->param.start_block +
2102 1))) * ((uint64_t) (dev->param.chunks_per_block *
2103 dev->data_bytes_per_chunk));
2105 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2106 buf->f_blocks = bytes_in_dev;
2108 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
2109 ((uint64_t) (dev->data_bytes_per_chunk));
2111 do_div(bytes_free, sb->s_blocksize);
2113 buf->f_bfree = bytes_free;
2115 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2118 (dev->param.end_block - dev->param.start_block + 1) *
2119 dev->param.chunks_per_block /
2120 (sb->s_blocksize / dev->data_bytes_per_chunk);
2122 yaffs_get_n_free_chunks(dev) /
2123 (sb->s_blocksize / dev->data_bytes_per_chunk);
2126 (dev->param.end_block - dev->param.start_block + 1) *
2127 dev->param.chunks_per_block *
2128 (dev->data_bytes_per_chunk / sb->s_blocksize);
2131 yaffs_get_n_free_chunks(dev) *
2132 (dev->data_bytes_per_chunk / sb->s_blocksize);
2137 buf->f_bavail = buf->f_bfree;
2139 yaffs_gross_unlock(dev);
2143 static void yaffs_flush_inodes(struct super_block *sb)
2146 struct yaffs_obj *obj;
2148 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
2149 obj = yaffs_inode_to_obj(iptr);
2151 yaffs_trace(YAFFS_TRACE_OS,
2154 yaffs_flush_file(obj, 1, 0);
2159 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2161 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2165 yaffs_flush_inodes(sb);
2166 yaffs_update_dirty_dirs(dev);
2167 yaffs_flush_whole_cache(dev);
2169 yaffs_checkpoint_save(dev);
2172 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2174 unsigned erased_chunks =
2175 dev->n_erased_blocks * dev->param.chunks_per_block;
2176 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2177 unsigned scattered = 0; /* Free chunks not in an erased block */
2179 if (erased_chunks < dev->n_free_chunks)
2180 scattered = (dev->n_free_chunks - erased_chunks);
2182 if (!context->bg_running)
2184 else if (scattered < (dev->param.chunks_per_block * 2))
2186 else if (erased_chunks > dev->n_free_chunks / 2)
2188 else if (erased_chunks > dev->n_free_chunks / 4)
2194 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
2197 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2198 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2199 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2202 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2203 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
2205 sb->s_dirt ? "dirty" : "clean",
2206 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2207 oneshot_checkpoint ? " one-shot" : "");
2209 yaffs_gross_lock(dev);
2210 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2211 oneshot_checkpoint) && !dev->is_checkpointed;
2213 if (sb->s_dirt || do_checkpoint) {
2214 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2216 if (oneshot_checkpoint)
2217 yaffs_auto_checkpoint &= ~4;
2219 yaffs_gross_unlock(dev);
2225 * yaffs background thread functions .
2226 * yaffs_bg_thread_fn() the thread function
2227 * yaffs_bg_start() launches the background thread.
2228 * yaffs_bg_stop() cleans up the background thread.
2231 * The thread should only run after the yaffs is initialised
2232 * The thread should be stopped before yaffs is unmounted.
2233 * The thread should not do any writing while the fs is in read only.
2236 #ifdef YAFFS_COMPILE_BACKGROUND
2238 void yaffs_background_waker(unsigned long data)
2240 wake_up_process((struct task_struct *)data);
2243 static int yaffs_bg_thread_fn(void *data)
2245 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2246 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2247 unsigned long now = jiffies;
2248 unsigned long next_dir_update = now;
2249 unsigned long next_gc = now;
2250 unsigned long expires;
2251 unsigned int urgency;
2254 struct timer_list timer;
2256 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2257 "yaffs_background starting for dev %p", (void *)dev);
2259 #ifdef YAFFS_COMPILE_FREEZER
2262 while (context->bg_running) {
2263 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
2265 if (kthread_should_stop())
2268 #ifdef YAFFS_COMPILE_FREEZER
2269 if (try_to_freeze())
2272 yaffs_gross_lock(dev);
2276 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
2277 yaffs_update_dirty_dirs(dev);
2278 next_dir_update = now + HZ;
2281 if (time_after(now, next_gc) && yaffs_bg_enable) {
2282 if (!dev->is_checkpointed) {
2283 urgency = yaffs_bg_gc_urgency(dev);
2284 gc_result = yaffs_bg_gc(dev, urgency);
2286 next_gc = now + HZ / 20 + 1;
2287 else if (urgency > 0)
2288 next_gc = now + HZ / 10 + 1;
2290 next_gc = now + HZ * 2;
2293 * gc not running so set to next_dir_update
2294 * to cut down on wake ups
2296 next_gc = next_dir_update;
2299 yaffs_gross_unlock(dev);
2301 expires = next_dir_update;
2302 if (time_before(next_gc, expires))
2304 if (time_before(expires, now))
2307 Y_INIT_TIMER(&timer);
2308 timer.expires = expires + 1;
2309 timer.data = (unsigned long)current;
2310 timer.function = yaffs_background_waker;
2312 set_current_state(TASK_INTERRUPTIBLE);
2315 del_timer_sync(&timer);
2324 static int yaffs_bg_start(struct yaffs_dev *dev)
2327 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2332 context->bg_running = 1;
2334 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2335 (void *)dev, "yaffs-bg-%d",
2338 if (IS_ERR(context->bg_thread)) {
2339 retval = PTR_ERR(context->bg_thread);
2340 context->bg_thread = NULL;
2341 context->bg_running = 0;
2346 static void yaffs_bg_stop(struct yaffs_dev *dev)
2348 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2350 ctxt->bg_running = 0;
2352 if (ctxt->bg_thread) {
2353 kthread_stop(ctxt->bg_thread);
2354 ctxt->bg_thread = NULL;
2358 static int yaffs_bg_thread_fn(void *data)
2363 static int yaffs_bg_start(struct yaffs_dev *dev)
2368 static void yaffs_bg_stop(struct yaffs_dev *dev)
2373 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2374 static void yaffs_write_super(struct super_block *sb)
2376 static int yaffs_write_super(struct super_block *sb)
2379 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2381 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2382 "yaffs_write_super %s",
2383 request_checkpoint ? " checkpt" : "");
2385 yaffs_do_sync_fs(sb, request_checkpoint);
2387 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2392 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2393 static int yaffs_sync_fs(struct super_block *sb, int wait)
2395 static int yaffs_sync_fs(struct super_block *sb)
2398 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2400 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2401 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
2403 yaffs_do_sync_fs(sb, request_checkpoint);
2408 #ifdef YAFFS_USE_OWN_IGET
2410 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2412 struct inode *inode;
2413 struct yaffs_obj *obj;
2414 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2416 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
2418 inode = iget_locked(sb, ino);
2420 return ERR_PTR(-ENOMEM);
2421 if (!(inode->i_state & I_NEW))
2424 /* NB This is called as a side effect of other functions, but
2425 * we had to release the lock to prevent deadlocks, so
2426 * need to lock again.
2429 yaffs_gross_lock(dev);
2431 obj = yaffs_find_by_number(dev, inode->i_ino);
2433 yaffs_fill_inode_from_obj(inode, obj);
2435 yaffs_gross_unlock(dev);
2437 unlock_new_inode(inode);
2443 static void yaffs_read_inode(struct inode *inode)
2445 /* NB This is called as a side effect of other functions, but
2446 * we had to release the lock to prevent deadlocks, so
2447 * need to lock again.
2450 struct yaffs_obj *obj;
2451 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
2453 yaffs_trace(YAFFS_TRACE_OS,
2454 "yaffs_read_inode for %d", (int)inode->i_ino);
2456 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2457 yaffs_gross_lock(dev);
2459 obj = yaffs_find_by_number(dev, inode->i_ino);
2461 yaffs_fill_inode_from_obj(inode, obj);
2463 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2464 yaffs_gross_unlock(dev);
2469 static LIST_HEAD(yaffs_context_list);
2470 struct mutex yaffs_context_lock;
2472 static void yaffs_put_super(struct super_block *sb)
2474 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2476 yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
2478 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2479 "Shutting down yaffs background thread");
2481 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2482 "yaffs background thread shut down");
2484 yaffs_gross_lock(dev);
2486 yaffs_flush_super(sb, 1);
2488 if (yaffs_dev_to_lc(dev)->put_super_fn)
2489 yaffs_dev_to_lc(dev)->put_super_fn(sb);
2491 yaffs_deinitialise(dev);
2493 yaffs_gross_unlock(dev);
2495 mutex_lock(&yaffs_context_lock);
2496 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2497 mutex_unlock(&yaffs_context_lock);
2499 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2500 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
2501 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2507 static void yaffs_mtd_put_super(struct super_block *sb)
2509 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
2514 put_mtd_device(mtd);
2517 static void yaffs_touch_super(struct yaffs_dev *dev)
2519 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
2521 yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb);
2526 struct yaffs_options {
2528 int skip_checkpoint_read;
2529 int skip_checkpoint_write;
2532 int tags_ecc_overridden;
2533 int lazy_loading_enabled;
2534 int lazy_loading_overridden;
2535 int empty_lost_and_found;
2536 int empty_lost_and_found_overridden;
2539 #define MAX_OPT_LEN 30
2540 static int yaffs_parse_options(struct yaffs_options *options,
2541 const char *options_str)
2543 char cur_opt[MAX_OPT_LEN + 1];
2547 /* Parse through the options which is a comma seperated list */
2549 while (options_str && *options_str && !error) {
2550 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2553 while (*options_str == ',')
2556 while (*options_str && *options_str != ',') {
2557 if (p < MAX_OPT_LEN) {
2558 cur_opt[p] = *options_str;
2564 if (!strcmp(cur_opt, "inband-tags")) {
2565 options->inband_tags = 1;
2566 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2567 options->tags_ecc_on = 0;
2568 options->tags_ecc_overridden = 1;
2569 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2570 options->tags_ecc_on = 1;
2571 options->tags_ecc_overridden = 1;
2572 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2573 options->lazy_loading_enabled = 0;
2574 options->lazy_loading_overridden = 1;
2575 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2576 options->lazy_loading_enabled = 1;
2577 options->lazy_loading_overridden = 1;
2578 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2579 options->empty_lost_and_found = 0;
2580 options->empty_lost_and_found_overridden = 1;
2581 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2582 options->empty_lost_and_found = 1;
2583 options->empty_lost_and_found_overridden = 1;
2584 } else if (!strcmp(cur_opt, "no-cache")) {
2585 options->no_cache = 1;
2586 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2587 options->skip_checkpoint_read = 1;
2588 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2589 options->skip_checkpoint_write = 1;
2590 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2591 options->skip_checkpoint_read = 1;
2592 options->skip_checkpoint_write = 1;
2594 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2603 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2604 struct super_block *sb,
2605 void *data, int silent)
2608 struct inode *inode = NULL;
2609 struct dentry *root;
2610 struct yaffs_dev *dev = 0;
2611 char devname_buf[BDEVNAME_SIZE + 1];
2612 struct mtd_info *mtd;
2614 char *data_str = (char *)data;
2615 struct yaffs_linux_context *context = NULL;
2616 struct yaffs_param *param;
2620 struct yaffs_options options;
2624 struct yaffs_linux_context *context_iterator;
2625 struct list_head *l;
2628 printk(KERN_INFO "yaffs: sb is NULL\n");
2632 sb->s_magic = YAFFS_MAGIC;
2633 sb->s_op = &yaffs_super_ops;
2634 sb->s_flags |= MS_NOATIME;
2636 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2638 #ifdef YAFFS_COMPILE_EXPORTFS
2639 sb->s_export_op = &yaffs_export_ops;
2643 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2644 else if (!yaffs_devname(sb, devname_buf))
2645 printk(KERN_INFO "yaffs: devname is NULL\n");
2647 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2649 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2654 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2656 memset(&options, 0, sizeof(options));
2658 if (yaffs_parse_options(&options, data_str)) {
2659 /* Option parsing failed */
2663 sb->s_blocksize = PAGE_CACHE_SIZE;
2664 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2666 yaffs_trace(YAFFS_TRACE_OS,
2667 "yaffs_read_super: Using yaffs%d", yaffs_version);
2668 yaffs_trace(YAFFS_TRACE_OS,
2669 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2671 yaffs_trace(YAFFS_TRACE_ALWAYS,
2672 "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
2673 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2674 yaffs_devname(sb, devname_buf));
2676 /* Check it's an mtd device..... */
2677 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2678 return NULL; /* This isn't an mtd device */
2680 /* Get the device */
2681 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2683 yaffs_trace(YAFFS_TRACE_ALWAYS,
2684 "yaffs: MTD device #%u doesn't appear to exist",
2688 /* Check it's NAND */
2689 if (mtd->type != MTD_NANDFLASH) {
2690 yaffs_trace(YAFFS_TRACE_ALWAYS,
2691 "yaffs: MTD device is not NAND it's type %d",
2696 yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase);
2697 yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read);
2698 yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write);
2699 yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob);
2700 yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob);
2701 yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad);
2702 yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad);
2703 yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
2704 yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
2705 yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
2706 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2707 yaffs_trace(YAFFS_TRACE_OS, " size %u", mtd->size);
2709 yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
2712 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2714 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2715 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2719 /* Added NCB 26/5/2006 for completeness */
2720 if (yaffs_version == 2 && !options.inband_tags
2721 && WRITE_SIZE(mtd) == 512) {
2722 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2727 if (yaffs_version == 2) {
2728 /* Check for version 2 style functions */
2730 !mtd->block_isbad ||
2731 !mtd->block_markbad || !mtd->read || !mtd->write ||
2732 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2733 !mtd->read_oob || !mtd->write_oob) {
2736 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2738 yaffs_trace(YAFFS_TRACE_ALWAYS,
2739 "MTD device does not support required functions"
2744 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2745 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2746 !options.inband_tags) {
2747 yaffs_trace(YAFFS_TRACE_ALWAYS,
2748 "MTD device does not have the right page sizes"
2753 /* Check for V1 style functions */
2754 if (!mtd->erase || !mtd->read || !mtd->write ||
2755 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2756 !mtd->read_oob || !mtd->write_oob) {
2759 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2761 yaffs_trace(YAFFS_TRACE_ALWAYS,
2762 "MTD device does not support required functions"
2767 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2768 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2769 yaffs_trace(YAFFS_TRACE_ALWAYS,
2770 "MTD device does not support have the right page sizes"
2776 /* OK, so if we got here, we have an MTD that's NAND and looks
2777 * like it has the right capabilities
2778 * Set the struct yaffs_dev up for mtd
2781 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2784 "yaffs: mtd is read only, setting superblock read only\n"
2786 sb->s_flags |= MS_RDONLY;
2789 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2790 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2792 if (!dev || !context) {
2802 /* Deep shit could not allocate device structure */
2803 yaffs_trace(YAFFS_TRACE_ALWAYS,
2804 "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
2808 memset(dev, 0, sizeof(struct yaffs_dev));
2809 param = &(dev->param);
2811 memset(context, 0, sizeof(struct yaffs_linux_context));
2812 dev->os_context = context;
2813 INIT_LIST_HEAD(&(context->context_list));
2815 context->super = sb;
2817 dev->read_only = read_only;
2819 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2820 sb->s_fs_info = dev;
2822 sb->u.generic_sbp = dev;
2825 dev->driver_context = mtd;
2826 param->name = mtd->name;
2828 /* Set up the memory size parameters.... */
2831 YCALCBLOCKS(mtd->size,
2832 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2834 param->start_block = 0;
2835 param->end_block = n_blocks - 1;
2836 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2837 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2838 param->n_reserved_blocks = 5;
2839 param->n_caches = (options.no_cache) ? 0 : 10;
2840 param->inband_tags = options.inband_tags;
2842 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2843 param->disable_lazy_load = 1;
2845 #ifdef CONFIG_YAFFS_XATTR
2846 param->enable_xattr = 1;
2848 if (options.lazy_loading_overridden)
2849 param->disable_lazy_load = !options.lazy_loading_enabled;
2851 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2852 param->no_tags_ecc = 1;
2855 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2857 param->defered_dir_update = 1;
2860 if (options.tags_ecc_overridden)
2861 param->no_tags_ecc = !options.tags_ecc_on;
2863 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2864 param->empty_lost_n_found = 1;
2867 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2868 param->refresh_period = 0;
2870 param->refresh_period = 500;
2873 #ifdef CONFIG_YAFFS__ALWAYS_CHECK_CHUNK_ERASED
2874 param->always_check_erased = 1;
2877 if (options.empty_lost_and_found_overridden)
2878 param->empty_lost_n_found = options.empty_lost_and_found;
2880 /* ... and the functions. */
2881 if (yaffs_version == 2) {
2882 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2883 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2884 param->bad_block_fn = nandmtd2_mark_block_bad;
2885 param->query_block_fn = nandmtd2_query_block;
2886 yaffs_dev_to_lc(dev)->spare_buffer =
2887 kmalloc(mtd->oobsize, GFP_NOFS);
2888 param->is_yaffs2 = 1;
2889 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2890 param->total_bytes_per_chunk = mtd->writesize;
2891 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2893 param->total_bytes_per_chunk = mtd->oobblock;
2894 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
2896 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2898 param->start_block = 0;
2899 param->end_block = n_blocks - 1;
2901 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2902 /* use the MTD interface in yaffs_mtdif1.c */
2903 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2904 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2905 param->bad_block_fn = nandmtd1_mark_block_bad;
2906 param->query_block_fn = nandmtd1_query_block;
2908 param->write_chunk_fn = nandmtd_write_chunk;
2909 param->read_chunk_fn = nandmtd_read_chunk;
2911 param->is_yaffs2 = 0;
2913 /* ... and common functions */
2914 param->erase_fn = nandmtd_erase_block;
2915 param->initialise_flash_fn = nandmtd_initialise;
2917 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2919 param->sb_dirty_fn = yaffs_touch_super;
2920 param->gc_control = yaffs_gc_control_callback;
2922 yaffs_dev_to_lc(dev)->super = sb;
2924 #ifndef CONFIG_YAFFS_DOES_ECC
2925 param->use_nand_ecc = 1;
2928 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2929 param->wide_tnodes_disabled = 1;
2932 param->skip_checkpt_rd = options.skip_checkpoint_read;
2933 param->skip_checkpt_wr = options.skip_checkpoint_write;
2935 mutex_lock(&yaffs_context_lock);
2936 /* Get a mount id */
2938 for (mount_id = 0; !found; mount_id++) {
2940 list_for_each(l, &yaffs_context_list) {
2942 list_entry(l, struct yaffs_linux_context,
2944 if (context_iterator->mount_id == mount_id)
2948 context->mount_id = mount_id;
2950 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2951 &yaffs_context_list);
2952 mutex_unlock(&yaffs_context_lock);
2954 /* Directory search handling... */
2955 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2956 param->remove_obj_fn = yaffs_remove_obj_callback;
2958 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2960 yaffs_gross_lock(dev);
2962 err = yaffs_guts_initialise(dev);
2964 yaffs_trace(YAFFS_TRACE_OS,
2965 "yaffs_read_super: guts initialised %s",
2966 (err == YAFFS_OK) ? "OK" : "FAILED");
2968 if (err == YAFFS_OK)
2969 yaffs_bg_start(dev);
2971 if (!context->bg_thread)
2972 param->defered_dir_update = 0;
2974 /* Release lock before yaffs_get_inode() */
2975 yaffs_gross_unlock(dev);
2977 /* Create root inode */
2978 if (err == YAFFS_OK)
2979 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2984 inode->i_op = &yaffs_dir_inode_operations;
2985 inode->i_fop = &yaffs_dir_operations;
2987 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2989 root = d_alloc_root(inode);
2991 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
2998 sb->s_dirt = !dev->is_checkpointed;
2999 yaffs_trace(YAFFS_TRACE_ALWAYS,
3000 "yaffs_read_super: is_checkpointed %d",
3001 dev->is_checkpointed);
3003 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
3007 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3008 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3011 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3014 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3015 static int yaffs_read_super(struct file_system_type *fs,
3016 int flags, const char *dev_name,
3017 void *data, struct vfsmount *mnt)
3020 return get_sb_bdev(fs, flags, dev_name, data,
3021 yaffs_internal_read_super_mtd, mnt);
3024 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3025 int flags, const char *dev_name,
3029 return get_sb_bdev(fs, flags, dev_name, data,
3030 yaffs_internal_read_super_mtd);
3034 static struct file_system_type yaffs_fs_type = {
3035 .owner = THIS_MODULE,
3037 .get_sb = yaffs_read_super,
3038 .kill_sb = kill_block_super,
3039 .fs_flags = FS_REQUIRES_DEV,
3042 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3045 return yaffs_internal_read_super(1, sb, data, silent);
3048 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3052 #ifdef CONFIG_YAFFS_YAFFS2
3054 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3055 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3058 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3061 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3062 static int yaffs2_read_super(struct file_system_type *fs,
3063 int flags, const char *dev_name, void *data,
3064 struct vfsmount *mnt)
3066 return get_sb_bdev(fs, flags, dev_name, data,
3067 yaffs2_internal_read_super_mtd, mnt);
3070 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3071 int flags, const char *dev_name,
3075 return get_sb_bdev(fs, flags, dev_name, data,
3076 yaffs2_internal_read_super_mtd);
3080 static struct file_system_type yaffs2_fs_type = {
3081 .owner = THIS_MODULE,
3083 .get_sb = yaffs2_read_super,
3084 .kill_sb = kill_block_super,
3085 .fs_flags = FS_REQUIRES_DEV,
3088 static struct super_block *yaffs2_read_super(struct super_block *sb,
3089 void *data, int silent)
3091 return yaffs_internal_read_super(2, sb, data, silent);
3094 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3098 #endif /* CONFIG_YAFFS_YAFFS2 */
3100 static struct proc_dir_entry *my_proc_entry;
3102 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3104 struct yaffs_param *param = &dev->param;
3106 buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
3107 buf += sprintf(buf, "end_block............ %d\n", param->end_block);
3108 buf += sprintf(buf, "total_bytes_per_chunk %d\n",
3109 param->total_bytes_per_chunk);
3110 buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
3111 buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
3112 buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
3113 buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
3114 buf += sprintf(buf, "empty_lost_n_found... %d\n",
3115 param->empty_lost_n_found);
3116 buf += sprintf(buf, "disable_lazy_load.... %d\n",
3117 param->disable_lazy_load);
3118 buf += sprintf(buf, "refresh_period....... %d\n",
3119 param->refresh_period);
3120 buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
3121 buf += sprintf(buf, "n_reserved_blocks.... %d\n",
3122 param->n_reserved_blocks);
3123 buf += sprintf(buf, "always_check_erased.. %d\n",
3124 param->always_check_erased);
3125 buf += sprintf(buf, "\n");
3130 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3132 buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
3133 dev->data_bytes_per_chunk);
3134 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3135 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3136 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3137 buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
3138 dev->blocks_in_checkpt);
3139 buf += sprintf(buf, "\n");
3140 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3141 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3142 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3143 buf += sprintf(buf, "\n");
3144 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3145 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3146 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3147 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3148 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3149 buf += sprintf(buf, "passive_gc_count..... %u\n",
3150 dev->passive_gc_count);
3151 buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
3152 dev->oldest_dirty_gc_count);
3153 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3154 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3155 buf += sprintf(buf, "n_retired_writes..... %u\n",
3156 dev->n_retired_writes);
3157 buf += sprintf(buf, "n_retired_blocks..... %u\n",
3158 dev->n_retired_blocks);
3159 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3160 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3161 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
3162 dev->n_tags_ecc_fixed);
3163 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
3164 dev->n_tags_ecc_unfixed);
3165 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3166 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3167 buf += sprintf(buf, "n_unlinked_files..... %u\n",
3168 dev->n_unlinked_files);
3169 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3170 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3175 static int yaffs_proc_read(char *page,
3177 off_t offset, int count, int *eof, void *data)
3179 struct list_head *item;
3184 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3185 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3186 * This also assumes the user has posted a read buffer large
3187 * enough to hold the complete output; but that's life in /proc.
3192 /* Print header first */
3196 "Multi-version YAFFS built:" __DATE__ " " __TIME__
3199 buf += sprintf(buf, "\n");
3203 mutex_lock(&yaffs_context_lock);
3205 /* Locate and print the Nth entry. Order N-squared but N is small. */
3206 list_for_each(item, &yaffs_context_list) {
3207 struct yaffs_linux_context *dc =
3208 list_entry(item, struct yaffs_linux_context,
3210 struct yaffs_dev *dev = dc->dev;
3212 if (n < (step & ~1)) {
3216 if ((step & 1) == 0) {
3218 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3220 buf = yaffs_dump_dev_part0(buf, dev);
3222 buf = yaffs_dump_dev_part1(buf, dev);
3227 mutex_unlock(&yaffs_context_lock);
3230 return buf - page < count ? buf - page : count;
3234 * Set the verbosity of the warnings and error messages.
3236 * Note that the names can only be a..z or _ with the current code.
3241 unsigned mask_bitfield;
3243 {"allocate", YAFFS_TRACE_ALLOCATE},
3244 {"always", YAFFS_TRACE_ALWAYS},
3245 {"background", YAFFS_TRACE_BACKGROUND},
3246 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3247 {"buffers", YAFFS_TRACE_BUFFERS},
3248 {"bug", YAFFS_TRACE_BUG},
3249 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3250 {"deletion", YAFFS_TRACE_DELETION},
3251 {"erase", YAFFS_TRACE_ERASE},
3252 {"error", YAFFS_TRACE_ERROR},
3253 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3254 {"gc", YAFFS_TRACE_GC},
3255 {"lock", YAFFS_TRACE_LOCK},
3256 {"mtd", YAFFS_TRACE_MTD},
3257 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3258 {"os", YAFFS_TRACE_OS},
3259 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3260 {"scan", YAFFS_TRACE_SCAN},
3261 {"mount", YAFFS_TRACE_MOUNT},
3262 {"tracing", YAFFS_TRACE_TRACING},
3263 {"sync", YAFFS_TRACE_SYNC},
3264 {"write", YAFFS_TRACE_WRITE},
3265 {"verify", YAFFS_TRACE_VERIFY},
3266 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3267 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3268 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3269 {"all", 0xffffffff},
3274 #define MAX_MASK_NAME_LENGTH 40
3275 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3276 unsigned long count, void *data)
3278 unsigned rg = 0, mask_bitfield;
3282 char substring[MAX_MASK_NAME_LENGTH + 1];
3288 rg = yaffs_trace_mask;
3290 while (!done && (pos < count)) {
3292 while ((pos < count) && isspace(buf[pos]))
3309 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3311 if (end > buf + pos) {
3312 mask_name = "numeral";
3313 len = end - (buf + pos);
3317 for (x = buf + pos, i = 0;
3318 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3319 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3321 substring[i] = '\0';
3323 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3324 if (strcmp(substring, mask_flags[i].mask_name)
3326 mask_name = mask_flags[i].mask_name;
3328 mask_flags[i].mask_bitfield;
3335 if (mask_name != NULL) {
3339 rg &= ~mask_bitfield;
3342 rg |= mask_bitfield;
3348 rg |= mask_bitfield;
3354 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3356 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3358 if (rg & YAFFS_TRACE_ALWAYS) {
3359 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3361 flag = ((rg & mask_flags[i].mask_bitfield) ==
3362 mask_flags[i].mask_bitfield) ? '+' : '-';
3363 printk(KERN_DEBUG "%c%s\n", flag,
3364 mask_flags[i].mask_name);
3371 static int yaffs_proc_write(struct file *file, const char *buf,
3372 unsigned long count, void *data)
3374 return yaffs_proc_write_trace_options(file, buf, count, data);
3377 /* Stuff to handle installation of file systems */
3378 struct file_system_to_install {
3379 struct file_system_type *fst;
3383 static struct file_system_to_install fs_to_install[] = {
3384 {&yaffs_fs_type, 0},
3385 {&yaffs2_fs_type, 0},
3389 static int __init init_yaffs_fs(void)
3392 struct file_system_to_install *fsinst;
3394 yaffs_trace(YAFFS_TRACE_ALWAYS,
3395 "yaffs built " __DATE__ " " __TIME__ " Installing.");
3397 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3398 yaffs_trace(YAFFS_TRACE_ALWAYS,
3399 " \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n"
3403 mutex_init(&yaffs_context_lock);
3405 /* Install the proc_fs entries */
3406 my_proc_entry = create_proc_entry("yaffs",
3407 S_IRUGO | S_IFREG, YPROC_ROOT);
3409 if (my_proc_entry) {
3410 my_proc_entry->write_proc = yaffs_proc_write;
3411 my_proc_entry->read_proc = yaffs_proc_read;
3412 my_proc_entry->data = NULL;
3417 /* Now add the file system entries */
3419 fsinst = fs_to_install;
3421 while (fsinst->fst && !error) {
3422 error = register_filesystem(fsinst->fst);
3424 fsinst->installed = 1;
3428 /* Any errors? uninstall */
3430 fsinst = fs_to_install;
3432 while (fsinst->fst) {
3433 if (fsinst->installed) {
3434 unregister_filesystem(fsinst->fst);
3435 fsinst->installed = 0;
3444 static void __exit exit_yaffs_fs(void)
3447 struct file_system_to_install *fsinst;
3449 yaffs_trace(YAFFS_TRACE_ALWAYS,
3450 "yaffs built " __DATE__ " " __TIME__ " removing.");
3452 remove_proc_entry("yaffs", YPROC_ROOT);
3454 fsinst = fs_to_install;
3456 while (fsinst->fst) {
3457 if (fsinst->installed) {
3458 unregister_filesystem(fsinst->fst);
3459 fsinst->installed = 0;
3465 module_init(init_yaffs_fs)
3466 module_exit(exit_yaffs_fs)
3468 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3469 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3470 MODULE_LICENSE("GPL");