2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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;
179 unsigned int yaffs_auto_select = 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 static int yaffs_setxattr(struct dentry *dentry, const char *name,
311 const void *value, size_t size, int flags);
312 static ssize_t yaffs_getxattr(struct dentry *dentry, const char *name,
313 void *buff, size_t size);
314 static int yaffs_removexattr(struct dentry *dentry, const char *name);
315 static ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
317 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
318 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
319 loff_t pos, unsigned len, unsigned flags,
320 struct page **pagep, void **fsdata);
321 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
322 loff_t pos, unsigned len, unsigned copied,
323 struct page *pg, void *fsdadata);
325 static int yaffs_prepare_write(struct file *f, struct page *pg,
326 unsigned offset, unsigned to);
327 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
332 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
334 #if (YAFFS_NEW_FOLLOW_LINK == 1)
335 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
336 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
338 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
341 static void yaffs_touch_super(struct yaffs_dev *dev);
343 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
345 static struct address_space_operations yaffs_file_address_operations = {
346 .readpage = yaffs_readpage,
347 .writepage = yaffs_writepage,
348 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
349 .write_begin = yaffs_write_begin,
350 .write_end = yaffs_write_end,
352 .prepare_write = yaffs_prepare_write,
353 .commit_write = yaffs_commit_write,
357 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
358 static const struct file_operations yaffs_file_operations = {
359 .read = do_sync_read,
360 .write = do_sync_write,
361 .aio_read = generic_file_aio_read,
362 .aio_write = generic_file_aio_write,
363 .mmap = generic_file_mmap,
364 .flush = yaffs_file_flush,
365 .fsync = yaffs_sync_object,
366 .splice_read = generic_file_splice_read,
367 .splice_write = generic_file_splice_write,
368 .llseek = generic_file_llseek,
371 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
373 static const struct file_operations yaffs_file_operations = {
374 .read = do_sync_read,
375 .write = do_sync_write,
376 .aio_read = generic_file_aio_read,
377 .aio_write = generic_file_aio_write,
378 .mmap = generic_file_mmap,
379 .flush = yaffs_file_flush,
380 .fsync = yaffs_sync_object,
381 .sendfile = generic_file_sendfile,
386 static const struct file_operations yaffs_file_operations = {
387 .read = generic_file_read,
388 .write = generic_file_write,
389 .mmap = generic_file_mmap,
390 .flush = yaffs_file_flush,
391 .fsync = yaffs_sync_object,
392 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
393 .sendfile = generic_file_sendfile,
398 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
399 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
401 void *kaddr = kmap_atomic(page, KM_USER0);
402 memset(kaddr + start, 0, end - start);
403 kunmap_atomic(kaddr, KM_USER0);
404 flush_dcache_page(page);
408 static const struct inode_operations yaffs_file_inode_operations = {
409 .setattr = yaffs_setattr,
410 .setxattr = yaffs_setxattr,
411 .getxattr = yaffs_getxattr,
412 .listxattr = yaffs_listxattr,
413 .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 .setxattr = yaffs_setxattr,
424 .getxattr = yaffs_getxattr,
425 .listxattr = yaffs_listxattr,
426 .removexattr = yaffs_removexattr,
429 static const struct inode_operations yaffs_dir_inode_operations = {
430 .create = yaffs_create,
431 .lookup = yaffs_lookup,
433 .unlink = yaffs_unlink,
434 .symlink = yaffs_symlink,
435 .mkdir = yaffs_mkdir,
436 .rmdir = yaffs_unlink,
437 .mknod = yaffs_mknod,
438 .rename = yaffs_rename,
439 .setattr = yaffs_setattr,
440 .setxattr = yaffs_setxattr,
441 .getxattr = yaffs_getxattr,
442 .listxattr = yaffs_listxattr,
443 .removexattr = yaffs_removexattr,
446 static const struct file_operations yaffs_dir_operations = {
447 .read = generic_read_dir,
448 .readdir = yaffs_readdir,
449 .fsync = yaffs_sync_object,
450 .llseek = generic_file_llseek,
453 static const struct super_operations yaffs_super_ops = {
454 .statfs = yaffs_statfs,
456 #ifndef YAFFS_USE_OWN_IGET
457 .read_inode = yaffs_read_inode,
459 #ifdef YAFFS_HAS_PUT_INODE
460 .put_inode = yaffs_put_inode,
462 .put_super = yaffs_put_super,
463 #ifdef YAFFS_HAS_EVICT_INODE
464 .evict_inode = yaffs_evict_inode,
466 .delete_inode = yaffs_delete_inode,
467 .clear_inode = yaffs_clear_inode,
469 .sync_fs = yaffs_sync_fs,
470 .write_super = yaffs_write_super,
473 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
475 #ifdef YAFFS_USE_SETATTR_COPY
476 setattr_copy(inode, attr);
479 return inode_setattr(inode, attr);
484 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
486 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
487 truncate_setsize(inode, newsize);
490 truncate_inode_pages(&inode->i_data, newsize);
496 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
498 return yaffs_gc_control;
501 static void yaffs_gross_lock(struct yaffs_dev *dev)
503 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
504 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
505 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
508 static void yaffs_gross_unlock(struct yaffs_dev *dev)
510 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
511 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
514 #ifdef YAFFS_COMPILE_EXPORTFS
516 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
519 return Y_IGET(sb, ino);
522 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
523 struct fid *fid, int fh_len,
526 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
527 yaffs2_nfs_get_inode);
530 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
531 struct fid *fid, int fh_len,
534 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
535 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;
545 struct yaffs_obj *d_obj;
546 struct yaffs_obj *parent_obj;
548 d_obj = yaffs_inode_to_obj(dentry->d_inode);
551 parent_obj = d_obj->parent;
553 parent_ino = yaffs_get_obj_inode(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 = {
576 .fh_to_dentry = yaffs2_fh_to_dentry,
577 .fh_to_parent = yaffs2_fh_to_parent,
578 .get_parent = yaffs2_get_parent,
583 /*-----------------------------------------------------------------*/
584 /* Directory search context allows us to unlock access to yaffs during
585 * filldir without causing problems with the directory being modified.
586 * This is similar to the tried and tested mechanism used in yaffs direct.
588 * A search context iterates along a doubly linked list of siblings in the
589 * directory. If the iterating object is deleted then this would corrupt
590 * the list iteration, likely causing a crash. The search context avoids
591 * this by using the remove_obj_fn to move the search context to the
592 * next object before the object is deleted.
594 * Many readdirs (and thus seach conexts) may be alive simulateously so
595 * each struct yaffs_dev has a list of these.
597 * A seach context lives for the duration of a readdir.
599 * All these functions must be called while yaffs is locked.
602 struct yaffs_search_context {
603 struct yaffs_dev *dev;
604 struct yaffs_obj *dir_obj;
605 struct yaffs_obj *next_return;
606 struct list_head others;
610 * yaffs_new_search() creates a new search context, initialises it and
611 * adds it to the device's search context list.
613 * Called at start of readdir.
615 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
617 struct yaffs_dev *dev = dir->my_dev;
618 struct yaffs_search_context *sc =
619 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
623 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
624 sc->next_return = NULL;
627 list_entry(dir->variant.dir_variant.children.next,
628 struct yaffs_obj, siblings);
629 INIT_LIST_HEAD(&sc->others);
630 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
636 * yaffs_search_end() disposes of a search context and cleans up.
638 static void yaffs_search_end(struct yaffs_search_context *sc)
641 list_del(&sc->others);
647 * yaffs_search_advance() 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_search_advance(struct yaffs_search_context *sc)
656 if (sc->next_return == NULL ||
657 list_empty(&sc->dir_obj->variant.dir_variant.children))
658 sc->next_return = NULL;
660 struct list_head *next = sc->next_return->siblings.next;
662 if (next == &sc->dir_obj->variant.dir_variant.children)
663 sc->next_return = NULL; /* end of list */
666 list_entry(next, struct yaffs_obj, siblings);
671 * yaffs_remove_obj_callback() is called when an object is unlinked.
672 * We check open search contexts and advance any which are currently
673 * on the object being iterated.
675 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
679 struct yaffs_search_context *sc;
680 struct list_head *search_contexts =
681 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
683 /* Iterate through the directory search contexts.
684 * If any are currently on the object being removed, then advance
685 * the search context to the next object to prevent a hanging pointer.
687 list_for_each(i, search_contexts) {
688 sc = list_entry(i, struct yaffs_search_context, others);
689 if (sc->next_return == obj)
690 yaffs_search_advance(sc);
695 /*-----------------------------------------------------------------*/
697 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
700 unsigned char *alias;
703 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
705 yaffs_gross_lock(dev);
707 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
709 yaffs_gross_unlock(dev);
714 ret = vfs_readlink(dentry, buffer, buflen, alias);
719 #if (YAFFS_NEW_FOLLOW_LINK == 1)
720 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
724 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
728 unsigned char *alias;
730 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
732 yaffs_gross_lock(dev);
734 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
735 yaffs_gross_unlock(dev);
741 #if (YAFFS_NEW_FOLLOW_LINK == 1)
742 nd_set_link(nd, alias);
746 ret = ERR_PTR(ret_int);
749 ret = vfs_follow_link(nd, alias);
758 #if (YAFFS_NEW_FOLLOW_LINK == 1)
759 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
765 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
766 struct yaffs_obj *obj);
769 * Lookup is used to find objects in the fs
771 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
773 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
776 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
779 struct yaffs_obj *obj;
780 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
782 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
784 if (current != yaffs_dev_to_lc(dev)->readdir_process)
785 yaffs_gross_lock(dev);
787 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
788 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
790 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
792 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
794 /* Can't hold gross lock when calling yaffs_get_inode() */
795 if (current != yaffs_dev_to_lc(dev)->readdir_process)
796 yaffs_gross_unlock(dev);
799 yaffs_trace(YAFFS_TRACE_OS,
800 "yaffs_lookup found %d", obj->obj_id);
802 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
804 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
808 /* added NCB for 2.5/6 compatability - forces add even if inode is
809 * NULL which creates dentry hash */
810 d_add(dentry, inode);
815 #ifdef YAFFS_HAS_PUT_INODE
817 /* For now put inode is just for debugging
818 * Put inode is called when the inode **structure** is put.
820 static void yaffs_put_inode(struct inode *inode)
822 yaffs_trace(YAFFS_TRACE_OS,
823 "yaffs_put_inode: ino %d, count %d"),
824 (int)inode->i_ino, atomic_read(&inode->i_count);
829 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
831 /* Clear the association between the inode and
832 * the struct yaffs_obj.
834 obj->my_inode = NULL;
835 yaffs_inode_to_obj_lv(inode) = NULL;
837 /* If the object freeing was deferred, then the real
839 * This should fix the inode inconsistency problem.
841 yaffs_handle_defered_free(obj);
844 #ifdef YAFFS_HAS_EVICT_INODE
845 /* yaffs_evict_inode combines into one operation what was previously done in
846 * yaffs_clear_inode() and yaffs_delete_inode()
849 static void yaffs_evict_inode(struct inode *inode)
851 struct yaffs_obj *obj;
852 struct yaffs_dev *dev;
855 obj = yaffs_inode_to_obj(inode);
857 yaffs_trace(YAFFS_TRACE_OS,
858 "yaffs_evict_inode: ino %d, count %d %s",
859 (int)inode->i_ino, atomic_read(&inode->i_count),
860 obj ? "object exists" : "null object");
862 if (!inode->i_nlink && !is_bad_inode(inode))
864 truncate_inode_pages(&inode->i_data, 0);
865 end_writeback(inode);
867 if (deleteme && obj) {
869 yaffs_gross_lock(dev);
871 yaffs_gross_unlock(dev);
875 yaffs_gross_lock(dev);
876 yaffs_unstitch_obj(inode, obj);
877 yaffs_gross_unlock(dev);
882 /* clear is called to tell the fs to release any per-inode data it holds.
883 * The object might still exist on disk and is just being thrown out of the cache
884 * or else the object has actually been deleted and we're being called via
886 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
889 static void yaffs_clear_inode(struct inode *inode)
891 struct yaffs_obj *obj;
892 struct yaffs_dev *dev;
894 obj = yaffs_inode_to_obj(inode);
896 yaffs_trace(YAFFS_TRACE_OS,
897 "yaffs_clear_inode: ino %d, count %d %s",
898 (int)inode->i_ino, atomic_read(&inode->i_count),
899 obj ? "object exists" : "null object");
903 yaffs_gross_lock(dev);
904 yaffs_unstitch_obj(inode, obj);
905 yaffs_gross_unlock(dev);
910 /* delete is called when the link count is zero and the inode
911 * is put (ie. nobody wants to know about it anymore, time to
913 * NB Must call clear_inode()
915 static void yaffs_delete_inode(struct inode *inode)
917 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
918 struct yaffs_dev *dev;
920 yaffs_trace(YAFFS_TRACE_OS,
921 "yaffs_delete_inode: ino %d, count %d %s",
922 (int)inode->i_ino, atomic_read(&inode->i_count),
923 obj ? "object exists" : "null object");
927 yaffs_gross_lock(dev);
929 yaffs_gross_unlock(dev);
931 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
932 truncate_inode_pages(&inode->i_data, 0);
938 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
939 static int yaffs_file_flush(struct file *file, fl_owner_t id)
941 static int yaffs_file_flush(struct file *file)
944 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
946 struct yaffs_dev *dev = obj->my_dev;
948 yaffs_trace(YAFFS_TRACE_OS,
949 "yaffs_file_flush object %d (%s)",
951 obj->dirty ? "dirty" : "clean");
953 yaffs_gross_lock(dev);
955 yaffs_flush_file(obj, 1, 0);
957 yaffs_gross_unlock(dev);
962 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
964 /* Lifted from jffs2 */
966 struct yaffs_obj *obj;
967 unsigned char *pg_buf;
970 struct yaffs_dev *dev;
972 yaffs_trace(YAFFS_TRACE_OS,
973 "yaffs_readpage_nolock at %08x, size %08x",
974 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
975 (unsigned)PAGE_CACHE_SIZE);
977 obj = yaffs_dentry_to_obj(f->f_dentry);
981 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
982 BUG_ON(!PageLocked(pg));
989 /* FIXME: Can kmap fail? */
991 yaffs_gross_lock(dev);
993 ret = yaffs_file_rd(obj, pg_buf,
994 pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
996 yaffs_gross_unlock(dev);
1002 ClearPageUptodate(pg);
1005 SetPageUptodate(pg);
1009 flush_dcache_page(pg);
1012 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
1016 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1018 int ret = yaffs_readpage_nolock(f, pg);
1023 static int yaffs_readpage(struct file *f, struct page *pg)
1027 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
1028 ret = yaffs_readpage_unlock(f, pg);
1029 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
1033 /* writepage inspired by/stolen from smbfs */
1035 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1036 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1038 static int yaffs_writepage(struct page *page)
1041 struct yaffs_dev *dev;
1042 struct address_space *mapping = page->mapping;
1043 struct inode *inode;
1044 unsigned long end_index;
1046 struct yaffs_obj *obj;
1053 inode = mapping->host;
1056 i_size = i_size_read(inode);
1058 end_index = i_size >> PAGE_CACHE_SHIFT;
1060 if (page->index < end_index)
1061 n_bytes = PAGE_CACHE_SIZE;
1063 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
1065 if (page->index > end_index || !n_bytes) {
1066 yaffs_trace(YAFFS_TRACE_OS,
1067 "yaffs_writepage at %08x, inode size = %08x!!",
1068 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1069 (unsigned)inode->i_size);
1070 yaffs_trace(YAFFS_TRACE_OS,
1071 " -> don't care!!");
1073 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1074 set_page_writeback(page);
1076 end_page_writeback(page);
1081 if (n_bytes != PAGE_CACHE_SIZE)
1082 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
1086 buffer = kmap(page);
1088 obj = yaffs_inode_to_obj(inode);
1090 yaffs_gross_lock(dev);
1092 yaffs_trace(YAFFS_TRACE_OS,
1093 "yaffs_writepage at %08x, size %08x",
1094 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes);
1095 yaffs_trace(YAFFS_TRACE_OS,
1096 "writepag0: obj = %05x, ino = %05x",
1097 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1099 n_written = yaffs_wr_file(obj, buffer,
1100 page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
1102 yaffs_touch_super(dev);
1104 yaffs_trace(YAFFS_TRACE_OS,
1105 "writepag1: obj = %05x, ino = %05x",
1106 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1108 yaffs_gross_unlock(dev);
1111 set_page_writeback(page);
1113 end_page_writeback(page);
1116 return (n_written == n_bytes) ? 0 : -ENOSPC;
1119 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1120 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1121 loff_t pos, unsigned len, unsigned flags,
1122 struct page **pagep, void **fsdata)
1124 struct page *pg = NULL;
1125 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1131 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1132 pg = grab_cache_page_write_begin(mapping, index, flags);
1134 pg = __grab_cache_page(mapping, index);
1142 yaffs_trace(YAFFS_TRACE_OS,
1143 "start yaffs_write_begin index %d(%x) uptodate %d",
1144 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
1147 space_held = yaffs_hold_space(filp);
1154 /* Update page if required */
1156 if (!Page_Uptodate(pg))
1157 ret = yaffs_readpage_nolock(filp, pg);
1162 /* Happy path return */
1163 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
1168 yaffs_trace(YAFFS_TRACE_OS,
1169 "end yaffs_write_begin fail returning %d", ret);
1171 yaffs_release_space(filp);
1174 page_cache_release(pg);
1181 static int yaffs_prepare_write(struct file *f, struct page *pg,
1182 unsigned offset, unsigned to)
1184 yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
1186 if (!Page_Uptodate(pg))
1187 return yaffs_readpage_nolock(f, pg);
1192 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1193 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1194 loff_t pos, unsigned len, unsigned copied,
1195 struct page *pg, void *fsdadata)
1199 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1202 addr = kva + offset_into_page;
1204 yaffs_trace(YAFFS_TRACE_OS,
1205 "yaffs_write_end addr %p pos %x n_bytes %d",
1206 addr, (unsigned)pos, copied);
1208 ret = yaffs_file_write(filp, addr, copied, &pos);
1210 if (ret != copied) {
1211 yaffs_trace(YAFFS_TRACE_OS,
1212 "yaffs_write_end not same size ret %d copied %d",
1219 yaffs_release_space(filp);
1221 page_cache_release(pg);
1226 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1231 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1232 int n_bytes = to - offset;
1235 unsigned spos = pos;
1239 addr = kva + offset;
1241 saddr = (unsigned)addr;
1243 yaffs_trace(YAFFS_TRACE_OS,
1244 "yaffs_commit_write addr %x pos %x n_bytes %d",
1245 saddr, spos, n_bytes);
1247 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
1249 if (n_written != n_bytes) {
1250 yaffs_trace(YAFFS_TRACE_OS,
1251 "yaffs_commit_write not same size n_written %d n_bytes %d",
1252 n_written, n_bytes);
1257 yaffs_trace(YAFFS_TRACE_OS,
1258 "yaffs_commit_write returning %d",
1259 n_written == n_bytes ? 0 : n_written);
1261 return n_written == n_bytes ? 0 : n_written;
1265 static void yaffs_fill_inode_from_obj(struct inode *inode,
1266 struct yaffs_obj *obj)
1270 /* Check mode against the variant type and attempt to repair if broken. */
1271 u32 mode = obj->yst_mode;
1272 switch (obj->variant_type) {
1273 case YAFFS_OBJECT_TYPE_FILE:
1274 if (!S_ISREG(mode)) {
1275 obj->yst_mode &= ~S_IFMT;
1276 obj->yst_mode |= S_IFREG;
1280 case YAFFS_OBJECT_TYPE_SYMLINK:
1281 if (!S_ISLNK(mode)) {
1282 obj->yst_mode &= ~S_IFMT;
1283 obj->yst_mode |= S_IFLNK;
1287 case YAFFS_OBJECT_TYPE_DIRECTORY:
1288 if (!S_ISDIR(mode)) {
1289 obj->yst_mode &= ~S_IFMT;
1290 obj->yst_mode |= S_IFDIR;
1294 case YAFFS_OBJECT_TYPE_UNKNOWN:
1295 case YAFFS_OBJECT_TYPE_HARDLINK:
1296 case YAFFS_OBJECT_TYPE_SPECIAL:
1302 inode->i_flags |= S_NOATIME;
1304 inode->i_ino = obj->obj_id;
1305 inode->i_mode = obj->yst_mode;
1306 inode->i_uid = obj->yst_uid;
1307 inode->i_gid = obj->yst_gid;
1308 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1309 inode->i_blksize = inode->i_sb->s_blocksize;
1311 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1313 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1314 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1315 inode->i_atime.tv_nsec = 0;
1316 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1317 inode->i_mtime.tv_nsec = 0;
1318 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1319 inode->i_ctime.tv_nsec = 0;
1321 inode->i_rdev = obj->yst_rdev;
1322 inode->i_atime = obj->yst_atime;
1323 inode->i_mtime = obj->yst_mtime;
1324 inode->i_ctime = obj->yst_ctime;
1326 inode->i_size = yaffs_get_obj_length(obj);
1327 inode->i_blocks = (inode->i_size + 511) >> 9;
1329 inode->i_nlink = yaffs_get_obj_link_count(obj);
1331 yaffs_trace(YAFFS_TRACE_OS,
1332 "yaffs_fill_inode mode %x uid %d gid %d size %d count %d",
1333 inode->i_mode, inode->i_uid, inode->i_gid,
1334 (int)inode->i_size, atomic_read(&inode->i_count));
1336 switch (obj->yst_mode & S_IFMT) {
1337 default: /* fifo, device or socket */
1338 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1339 init_special_inode(inode, obj->yst_mode,
1340 old_decode_dev(obj->yst_rdev));
1342 init_special_inode(inode, obj->yst_mode,
1343 (dev_t) (obj->yst_rdev));
1346 case S_IFREG: /* file */
1347 inode->i_op = &yaffs_file_inode_operations;
1348 inode->i_fop = &yaffs_file_operations;
1349 inode->i_mapping->a_ops =
1350 &yaffs_file_address_operations;
1352 case S_IFDIR: /* directory */
1353 inode->i_op = &yaffs_dir_inode_operations;
1354 inode->i_fop = &yaffs_dir_operations;
1356 case S_IFLNK: /* symlink */
1357 inode->i_op = &yaffs_symlink_inode_operations;
1361 yaffs_inode_to_obj_lv(inode) = obj;
1363 obj->my_inode = inode;
1366 yaffs_trace(YAFFS_TRACE_OS,
1367 "yaffs_fill_inode invalid parameters");
1372 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1373 struct yaffs_obj *obj)
1375 struct inode *inode;
1378 yaffs_trace(YAFFS_TRACE_OS,
1379 "yaffs_get_inode for NULL super_block!!");
1385 yaffs_trace(YAFFS_TRACE_OS,
1386 "yaffs_get_inode for NULL object!!");
1391 yaffs_trace(YAFFS_TRACE_OS,
1392 "yaffs_get_inode for object %d", obj->obj_id);
1394 inode = Y_IGET(sb, obj->obj_id);
1398 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1399 /* iget also increments the inode's i_count */
1400 /* NB You can't be holding gross_lock or deadlock will happen! */
1405 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1408 struct yaffs_obj *obj;
1409 int n_written, ipos;
1410 struct inode *inode;
1411 struct yaffs_dev *dev;
1413 obj = yaffs_dentry_to_obj(f->f_dentry);
1416 yaffs_trace(YAFFS_TRACE_OS,
1417 "yaffs_file_write: hey obj is null!");
1423 yaffs_gross_lock(dev);
1425 inode = f->f_dentry->d_inode;
1427 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1428 ipos = inode->i_size;
1432 yaffs_trace(YAFFS_TRACE_OS,
1433 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %d(%x)",
1434 (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos);
1436 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1438 yaffs_touch_super(dev);
1440 yaffs_trace(YAFFS_TRACE_OS,
1441 "yaffs_file_write: %d(%x) bytes written",
1442 (unsigned)n, (unsigned)n);
1444 if (n_written > 0) {
1447 if (ipos > inode->i_size) {
1448 inode->i_size = ipos;
1449 inode->i_blocks = (ipos + 511) >> 9;
1451 yaffs_trace(YAFFS_TRACE_OS,
1452 "yaffs_file_write size updated to %d bytes, %d blocks",
1453 ipos, (int)(inode->i_blocks));
1457 yaffs_gross_unlock(dev);
1458 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1461 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1462 /* For now we just assume few parallel writes and check against a small number. */
1463 /* Todo: need to do this with a counter to handle parallel reads better */
1465 static ssize_t yaffs_hold_space(struct file *f)
1467 struct yaffs_obj *obj;
1468 struct yaffs_dev *dev;
1472 obj = yaffs_dentry_to_obj(f->f_dentry);
1476 yaffs_gross_lock(dev);
1478 n_free_chunks = yaffs_get_n_free_chunks(dev);
1480 yaffs_gross_unlock(dev);
1482 return (n_free_chunks > 20) ? 1 : 0;
1485 static void yaffs_release_space(struct file *f)
1487 struct yaffs_obj *obj;
1488 struct yaffs_dev *dev;
1490 obj = yaffs_dentry_to_obj(f->f_dentry);
1494 yaffs_gross_lock(dev);
1496 yaffs_gross_unlock(dev);
1499 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1501 struct yaffs_obj *obj;
1502 struct yaffs_dev *dev;
1503 struct yaffs_search_context *sc;
1504 struct inode *inode = f->f_dentry->d_inode;
1505 unsigned long offset, curoffs;
1506 struct yaffs_obj *l;
1509 char name[YAFFS_MAX_NAME_LENGTH + 1];
1511 obj = yaffs_dentry_to_obj(f->f_dentry);
1514 yaffs_gross_lock(dev);
1516 yaffs_dev_to_lc(dev)->readdir_process = current;
1520 sc = yaffs_new_search(obj);
1526 yaffs_trace(YAFFS_TRACE_OS,
1527 "yaffs_readdir: starting at %d", (int)offset);
1530 yaffs_trace(YAFFS_TRACE_OS,
1531 "yaffs_readdir: entry . ino %d",
1533 yaffs_gross_unlock(dev);
1534 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1535 yaffs_gross_lock(dev);
1538 yaffs_gross_lock(dev);
1543 yaffs_trace(YAFFS_TRACE_OS,
1544 "yaffs_readdir: entry .. ino %d",
1545 (int)f->f_dentry->d_parent->d_inode->i_ino);
1546 yaffs_gross_unlock(dev);
1547 if (filldir(dirent, "..", 2, offset,
1548 f->f_dentry->d_parent->d_inode->i_ino,
1550 yaffs_gross_lock(dev);
1553 yaffs_gross_lock(dev);
1560 /* If the directory has changed since the open or last call to
1561 readdir, rewind to after the 2 canned entries. */
1562 if (f->f_version != inode->i_version) {
1565 f->f_version = inode->i_version;
1568 while (sc->next_return) {
1570 l = sc->next_return;
1571 if (curoffs >= offset) {
1572 int this_inode = yaffs_get_obj_inode(l);
1573 int this_type = yaffs_get_obj_type(l);
1575 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1576 yaffs_trace(YAFFS_TRACE_OS,
1577 "yaffs_readdir: %s inode %d",
1578 name, yaffs_get_obj_inode(l));
1580 yaffs_gross_unlock(dev);
1585 offset, this_inode, this_type) < 0) {
1586 yaffs_gross_lock(dev);
1590 yaffs_gross_lock(dev);
1595 yaffs_search_advance(sc);
1599 yaffs_search_end(sc);
1600 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1601 yaffs_gross_unlock(dev);
1607 * File creation. Allocate an inode, and we're done..
1610 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1613 #define YCRED(x) (x->cred)
1616 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1617 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1620 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1624 struct inode *inode;
1626 struct yaffs_obj *obj = NULL;
1627 struct yaffs_dev *dev;
1629 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1631 int error = -ENOSPC;
1632 uid_t uid = YCRED(current)->fsuid;
1634 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1636 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1640 yaffs_trace(YAFFS_TRACE_OS,
1641 "yaffs_mknod: parent object %d type %d",
1642 parent->obj_id, parent->variant_type);
1644 yaffs_trace(YAFFS_TRACE_OS,
1645 "yaffs_mknod: could not get parent object");
1649 yaffs_trace(YAFFS_TRACE_OS,
1650 "yaffs_mknod: making oject for %s, mode %x dev %x",
1651 dentry->d_name.name, mode, rdev);
1653 dev = parent->my_dev;
1655 yaffs_gross_lock(dev);
1657 switch (mode & S_IFMT) {
1659 /* Special (socket, fifo, device...) */
1660 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
1661 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1663 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1664 gid, old_encode_dev(rdev));
1667 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1671 case S_IFREG: /* file */
1672 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
1673 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1676 case S_IFDIR: /* directory */
1677 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
1678 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1681 case S_IFLNK: /* symlink */
1682 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
1683 obj = NULL; /* Do we ever get here? */
1687 /* Can not call yaffs_get_inode() with gross lock held */
1688 yaffs_gross_unlock(dev);
1691 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1692 d_instantiate(dentry, inode);
1693 update_dir_time(dir);
1694 yaffs_trace(YAFFS_TRACE_OS,
1695 "yaffs_mknod created object %d count = %d",
1696 obj->obj_id, atomic_read(&inode->i_count));
1698 yaffs_fill_inode_from_obj(dir, parent);
1700 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
1707 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1710 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
1711 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1715 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1716 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1717 struct nameidata *n)
1719 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1722 yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
1723 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1726 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1730 struct yaffs_dev *dev;
1731 struct yaffs_obj *obj;
1733 yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
1734 (int)(dir->i_ino), dentry->d_name.name);
1735 obj = yaffs_inode_to_obj(dir);
1738 yaffs_gross_lock(dev);
1740 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1742 if (ret_val == YAFFS_OK) {
1743 dentry->d_inode->i_nlink--;
1745 yaffs_gross_unlock(dev);
1746 mark_inode_dirty(dentry->d_inode);
1747 update_dir_time(dir);
1750 yaffs_gross_unlock(dev);
1757 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1758 struct dentry *dentry)
1760 struct inode *inode = old_dentry->d_inode;
1761 struct yaffs_obj *obj = NULL;
1762 struct yaffs_obj *link = NULL;
1763 struct yaffs_dev *dev;
1765 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
1767 obj = yaffs_inode_to_obj(inode);
1770 yaffs_gross_lock(dev);
1772 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1774 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1778 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
1779 d_instantiate(dentry, old_dentry->d_inode);
1780 atomic_inc(&old_dentry->d_inode->i_count);
1781 yaffs_trace(YAFFS_TRACE_OS,
1782 "yaffs_link link count %d i_count %d",
1783 old_dentry->d_inode->i_nlink,
1784 atomic_read(&old_dentry->d_inode->i_count));
1787 yaffs_gross_unlock(dev);
1790 update_dir_time(dir);
1797 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1798 const char *symname)
1800 struct yaffs_obj *obj;
1801 struct yaffs_dev *dev;
1802 uid_t uid = YCRED(current)->fsuid;
1804 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1806 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
1808 dev = yaffs_inode_to_obj(dir)->my_dev;
1809 yaffs_gross_lock(dev);
1810 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1811 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1812 yaffs_gross_unlock(dev);
1815 struct inode *inode;
1817 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1818 d_instantiate(dentry, inode);
1819 update_dir_time(dir);
1820 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
1823 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
1829 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1830 static int yaffs_sync_object(struct file *file, int datasync)
1832 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1837 struct yaffs_obj *obj;
1838 struct yaffs_dev *dev;
1839 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1840 struct dentry *dentry = file->f_path.dentry;
1843 obj = yaffs_dentry_to_obj(dentry);
1847 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1848 "yaffs_sync_object");
1849 yaffs_gross_lock(dev);
1850 yaffs_flush_file(obj, 1, datasync);
1851 yaffs_gross_unlock(dev);
1856 * The VFS layer already does all the dentry stuff for rename.
1858 * NB: POSIX says you can rename an object over an old object of the same name
1860 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1861 struct inode *new_dir, struct dentry *new_dentry)
1863 struct yaffs_dev *dev;
1864 int ret_val = YAFFS_FAIL;
1865 struct yaffs_obj *target;
1867 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
1868 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1870 yaffs_gross_lock(dev);
1872 /* Check if the target is an existing directory that is not empty. */
1873 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1874 new_dentry->d_name.name);
1876 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1877 !list_empty(&target->variant.dir_variant.children)) {
1879 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
1881 ret_val = YAFFS_FAIL;
1883 /* Now does unlinking internally using shadowing mechanism */
1884 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
1886 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1887 old_dentry->d_name.name,
1888 yaffs_inode_to_obj(new_dir),
1889 new_dentry->d_name.name);
1891 yaffs_gross_unlock(dev);
1893 if (ret_val == YAFFS_OK) {
1895 new_dentry->d_inode->i_nlink--;
1896 mark_inode_dirty(new_dentry->d_inode);
1899 update_dir_time(old_dir);
1900 if (old_dir != new_dir)
1901 update_dir_time(new_dir);
1908 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1910 struct inode *inode = dentry->d_inode;
1912 struct yaffs_dev *dev;
1914 yaffs_trace(YAFFS_TRACE_OS,
1915 "yaffs_setattr of object %d",
1916 yaffs_inode_to_obj(inode)->obj_id);
1918 /* Fail if a requested resize >= 2GB */
1919 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
1923 error = inode_change_ok(inode, attr);
1927 error = yaffs_vfs_setattr(inode, attr);
1928 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
1929 if (attr->ia_valid & ATTR_SIZE) {
1930 yaffs_vfs_setsize(inode, attr->ia_size);
1931 inode->i_blocks = (inode->i_size + 511) >> 9;
1934 dev = yaffs_inode_to_obj(inode)->my_dev;
1935 if (attr->ia_valid & ATTR_SIZE) {
1936 yaffs_trace(YAFFS_TRACE_OS,
1938 (int)(attr->ia_size),
1939 (int)(attr->ia_size));
1941 yaffs_gross_lock(dev);
1942 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
1943 if (result == YAFFS_OK) {
1948 yaffs_gross_unlock(dev);
1952 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
1957 static int yaffs_setxattr(struct dentry *dentry, const char *name,
1958 const void *value, size_t size, int flags)
1960 struct inode *inode = dentry->d_inode;
1962 struct yaffs_dev *dev;
1963 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1965 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
1970 yaffs_gross_lock(dev);
1971 result = yaffs_set_xattrib(obj, name, value, size, flags);
1972 if (result == YAFFS_OK)
1974 else if (result < 0)
1976 yaffs_gross_unlock(dev);
1979 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
1984 static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
1985 void *buff, size_t size)
1987 struct inode *inode = dentry->d_inode;
1989 struct yaffs_dev *dev;
1990 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1992 yaffs_trace(YAFFS_TRACE_OS,
1993 "yaffs_getxattr \"%s\" from object %d",
1998 yaffs_gross_lock(dev);
1999 error = yaffs_get_xattrib(obj, name, buff, size);
2000 yaffs_gross_unlock(dev);
2003 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
2008 static int yaffs_removexattr(struct dentry *dentry, const char *name)
2010 struct inode *inode = dentry->d_inode;
2012 struct yaffs_dev *dev;
2013 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2015 yaffs_trace(YAFFS_TRACE_OS,
2016 "yaffs_removexattr of object %d", obj->obj_id);
2021 yaffs_gross_lock(dev);
2022 result = yaffs_remove_xattrib(obj, name);
2023 if (result == YAFFS_OK)
2025 else if (result < 0)
2027 yaffs_gross_unlock(dev);
2030 yaffs_trace(YAFFS_TRACE_OS,
2031 "yaffs_removexattr done returning %d", error);
2036 static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
2038 struct inode *inode = dentry->d_inode;
2040 struct yaffs_dev *dev;
2041 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2043 yaffs_trace(YAFFS_TRACE_OS,
2044 "yaffs_listxattr of object %d", obj->obj_id);
2048 yaffs_gross_lock(dev);
2049 error = yaffs_list_xattrib(obj, buff, size);
2050 yaffs_gross_unlock(dev);
2053 yaffs_trace(YAFFS_TRACE_OS,
2054 "yaffs_listxattr done returning %d", error);
2060 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2061 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2063 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2064 struct super_block *sb = dentry->d_sb;
2065 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2066 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2068 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2070 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2072 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2075 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
2077 yaffs_gross_lock(dev);
2079 buf->f_type = YAFFS_MAGIC;
2080 buf->f_bsize = sb->s_blocksize;
2081 buf->f_namelen = 255;
2083 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2084 /* Do this if chunk size is not a power of 2 */
2086 uint64_t bytes_in_dev;
2087 uint64_t bytes_free;
2091 ((dev->param.end_block - dev->param.start_block +
2092 1))) * ((uint64_t) (dev->param.chunks_per_block *
2093 dev->data_bytes_per_chunk));
2095 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2096 buf->f_blocks = bytes_in_dev;
2098 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
2099 ((uint64_t) (dev->data_bytes_per_chunk));
2101 do_div(bytes_free, sb->s_blocksize);
2103 buf->f_bfree = bytes_free;
2105 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2108 (dev->param.end_block - dev->param.start_block + 1) *
2109 dev->param.chunks_per_block /
2110 (sb->s_blocksize / dev->data_bytes_per_chunk);
2112 yaffs_get_n_free_chunks(dev) /
2113 (sb->s_blocksize / dev->data_bytes_per_chunk);
2116 (dev->param.end_block - dev->param.start_block + 1) *
2117 dev->param.chunks_per_block *
2118 (dev->data_bytes_per_chunk / sb->s_blocksize);
2121 yaffs_get_n_free_chunks(dev) *
2122 (dev->data_bytes_per_chunk / sb->s_blocksize);
2127 buf->f_bavail = buf->f_bfree;
2129 yaffs_gross_unlock(dev);
2133 static void yaffs_flush_inodes(struct super_block *sb)
2136 struct yaffs_obj *obj;
2138 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
2139 obj = yaffs_inode_to_obj(iptr);
2141 yaffs_trace(YAFFS_TRACE_OS,
2144 yaffs_flush_file(obj, 1, 0);
2149 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2151 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2155 yaffs_flush_inodes(sb);
2156 yaffs_update_dirty_dirs(dev);
2157 yaffs_flush_whole_cache(dev);
2159 yaffs_checkpoint_save(dev);
2162 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2164 unsigned erased_chunks =
2165 dev->n_erased_blocks * dev->param.chunks_per_block;
2166 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2167 unsigned scattered = 0; /* Free chunks not in an erased block */
2169 if (erased_chunks < dev->n_free_chunks)
2170 scattered = (dev->n_free_chunks - erased_chunks);
2172 if (!context->bg_running)
2174 else if (scattered < (dev->param.chunks_per_block * 2))
2176 else if (erased_chunks > dev->n_free_chunks / 2)
2178 else if (erased_chunks > dev->n_free_chunks / 4)
2184 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
2187 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2188 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2189 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2192 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2193 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
2195 sb->s_dirt ? "dirty" : "clean",
2196 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2197 oneshot_checkpoint ? " one-shot" : "");
2199 yaffs_gross_lock(dev);
2200 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2201 oneshot_checkpoint) && !dev->is_checkpointed;
2203 if (sb->s_dirt || do_checkpoint) {
2204 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2206 if (oneshot_checkpoint)
2207 yaffs_auto_checkpoint &= ~4;
2209 yaffs_gross_unlock(dev);
2215 * yaffs background thread functions .
2216 * yaffs_bg_thread_fn() the thread function
2217 * yaffs_bg_start() launches the background thread.
2218 * yaffs_bg_stop() cleans up the background thread.
2221 * The thread should only run after the yaffs is initialised
2222 * The thread should be stopped before yaffs is unmounted.
2223 * The thread should not do any writing while the fs is in read only.
2226 #ifdef YAFFS_COMPILE_BACKGROUND
2228 void yaffs_background_waker(unsigned long data)
2230 wake_up_process((struct task_struct *)data);
2233 static int yaffs_bg_thread_fn(void *data)
2235 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2236 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2237 unsigned long now = jiffies;
2238 unsigned long next_dir_update = now;
2239 unsigned long next_gc = now;
2240 unsigned long expires;
2241 unsigned int urgency;
2244 struct timer_list timer;
2246 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2247 "yaffs_background starting for dev %p", (void *)dev);
2249 #ifdef YAFFS_COMPILE_FREEZER
2252 while (context->bg_running) {
2253 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
2255 if (kthread_should_stop())
2258 #ifdef YAFFS_COMPILE_FREEZER
2259 if (try_to_freeze())
2262 yaffs_gross_lock(dev);
2266 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
2267 yaffs_update_dirty_dirs(dev);
2268 next_dir_update = now + HZ;
2271 if (time_after(now, next_gc) && yaffs_bg_enable) {
2272 if (!dev->is_checkpointed) {
2273 urgency = yaffs_bg_gc_urgency(dev);
2274 gc_result = yaffs_bg_gc(dev, urgency);
2276 next_gc = now + HZ / 20 + 1;
2277 else if (urgency > 0)
2278 next_gc = now + HZ / 10 + 1;
2280 next_gc = now + HZ * 2;
2283 * gc not running so set to next_dir_update
2284 * to cut down on wake ups
2286 next_gc = next_dir_update;
2289 yaffs_gross_unlock(dev);
2291 expires = next_dir_update;
2292 if (time_before(next_gc, expires))
2294 if (time_before(expires, now))
2297 Y_INIT_TIMER(&timer);
2298 timer.expires = expires + 1;
2299 timer.data = (unsigned long)current;
2300 timer.function = yaffs_background_waker;
2302 set_current_state(TASK_INTERRUPTIBLE);
2305 del_timer_sync(&timer);
2314 static int yaffs_bg_start(struct yaffs_dev *dev)
2317 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2322 context->bg_running = 1;
2324 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2325 (void *)dev, "yaffs-bg-%d",
2328 if (IS_ERR(context->bg_thread)) {
2329 retval = PTR_ERR(context->bg_thread);
2330 context->bg_thread = NULL;
2331 context->bg_running = 0;
2336 static void yaffs_bg_stop(struct yaffs_dev *dev)
2338 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2340 ctxt->bg_running = 0;
2342 if (ctxt->bg_thread) {
2343 kthread_stop(ctxt->bg_thread);
2344 ctxt->bg_thread = NULL;
2348 static int yaffs_bg_thread_fn(void *data)
2353 static int yaffs_bg_start(struct yaffs_dev *dev)
2358 static void yaffs_bg_stop(struct yaffs_dev *dev)
2363 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2364 static void yaffs_write_super(struct super_block *sb)
2366 static int yaffs_write_super(struct super_block *sb)
2369 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2371 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2372 "yaffs_write_super %s",
2373 request_checkpoint ? " checkpt" : "");
2375 yaffs_do_sync_fs(sb, request_checkpoint);
2377 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2382 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2383 static int yaffs_sync_fs(struct super_block *sb, int wait)
2385 static int yaffs_sync_fs(struct super_block *sb)
2388 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2390 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2391 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
2393 yaffs_do_sync_fs(sb, request_checkpoint);
2398 #ifdef YAFFS_USE_OWN_IGET
2400 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2402 struct inode *inode;
2403 struct yaffs_obj *obj;
2404 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2406 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
2408 inode = iget_locked(sb, ino);
2410 return ERR_PTR(-ENOMEM);
2411 if (!(inode->i_state & I_NEW))
2414 /* NB This is called as a side effect of other functions, but
2415 * we had to release the lock to prevent deadlocks, so
2416 * need to lock again.
2419 yaffs_gross_lock(dev);
2421 obj = yaffs_find_by_number(dev, inode->i_ino);
2423 yaffs_fill_inode_from_obj(inode, obj);
2425 yaffs_gross_unlock(dev);
2427 unlock_new_inode(inode);
2433 static void yaffs_read_inode(struct inode *inode)
2435 /* NB This is called as a side effect of other functions, but
2436 * we had to release the lock to prevent deadlocks, so
2437 * need to lock again.
2440 struct yaffs_obj *obj;
2441 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
2443 yaffs_trace(YAFFS_TRACE_OS,
2444 "yaffs_read_inode for %d", (int)inode->i_ino);
2446 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2447 yaffs_gross_lock(dev);
2449 obj = yaffs_find_by_number(dev, inode->i_ino);
2451 yaffs_fill_inode_from_obj(inode, obj);
2453 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2454 yaffs_gross_unlock(dev);
2459 static LIST_HEAD(yaffs_context_list);
2460 struct mutex yaffs_context_lock;
2462 static void yaffs_put_super(struct super_block *sb)
2464 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2466 yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
2468 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2469 "Shutting down yaffs background thread");
2471 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2472 "yaffs background thread shut down");
2474 yaffs_gross_lock(dev);
2476 yaffs_flush_super(sb, 1);
2478 if (yaffs_dev_to_lc(dev)->put_super_fn)
2479 yaffs_dev_to_lc(dev)->put_super_fn(sb);
2481 yaffs_deinitialise(dev);
2483 yaffs_gross_unlock(dev);
2485 mutex_lock(&yaffs_context_lock);
2486 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2487 mutex_unlock(&yaffs_context_lock);
2489 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2490 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
2491 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2497 static void yaffs_mtd_put_super(struct super_block *sb)
2499 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
2504 put_mtd_device(mtd);
2507 static void yaffs_touch_super(struct yaffs_dev *dev)
2509 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
2511 yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb);
2516 struct yaffs_options {
2518 int skip_checkpoint_read;
2519 int skip_checkpoint_write;
2522 int tags_ecc_overridden;
2523 int lazy_loading_enabled;
2524 int lazy_loading_overridden;
2525 int empty_lost_and_found;
2526 int empty_lost_and_found_overridden;
2527 int disable_summary;
2530 #define MAX_OPT_LEN 30
2531 static int yaffs_parse_options(struct yaffs_options *options,
2532 const char *options_str)
2534 char cur_opt[MAX_OPT_LEN + 1];
2538 /* Parse through the options which is a comma seperated list */
2540 while (options_str && *options_str && !error) {
2541 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2544 while (*options_str == ',')
2547 while (*options_str && *options_str != ',') {
2548 if (p < MAX_OPT_LEN) {
2549 cur_opt[p] = *options_str;
2555 if (!strcmp(cur_opt, "inband-tags")) {
2556 options->inband_tags = 1;
2557 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2558 options->tags_ecc_on = 0;
2559 options->tags_ecc_overridden = 1;
2560 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2561 options->tags_ecc_on = 1;
2562 options->tags_ecc_overridden = 1;
2563 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2564 options->lazy_loading_enabled = 0;
2565 options->lazy_loading_overridden = 1;
2566 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2567 options->lazy_loading_enabled = 1;
2568 options->lazy_loading_overridden = 1;
2569 } else if (!strcmp(cur_opt, "disable_summary")) {
2570 options->disable_summary = 1;
2571 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2572 options->empty_lost_and_found = 0;
2573 options->empty_lost_and_found_overridden = 1;
2574 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2575 options->empty_lost_and_found = 1;
2576 options->empty_lost_and_found_overridden = 1;
2577 } else if (!strcmp(cur_opt, "no-cache")) {
2578 options->no_cache = 1;
2579 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2580 options->skip_checkpoint_read = 1;
2581 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2582 options->skip_checkpoint_write = 1;
2583 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2584 options->skip_checkpoint_read = 1;
2585 options->skip_checkpoint_write = 1;
2587 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2596 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2597 struct super_block *sb,
2598 void *data, int silent)
2601 struct inode *inode = NULL;
2602 struct dentry *root;
2603 struct yaffs_dev *dev = 0;
2604 char devname_buf[BDEVNAME_SIZE + 1];
2605 struct mtd_info *mtd;
2607 char *data_str = (char *)data;
2608 struct yaffs_linux_context *context = NULL;
2609 struct yaffs_param *param;
2613 struct yaffs_options options;
2617 struct yaffs_linux_context *context_iterator;
2618 struct list_head *l;
2621 printk(KERN_INFO "yaffs: sb is NULL\n");
2625 sb->s_magic = YAFFS_MAGIC;
2626 sb->s_op = &yaffs_super_ops;
2627 sb->s_flags |= MS_NOATIME;
2629 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2631 #ifdef YAFFS_COMPILE_EXPORTFS
2632 sb->s_export_op = &yaffs_export_ops;
2636 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2637 else if (!yaffs_devname(sb, devname_buf))
2638 printk(KERN_INFO "yaffs: devname is NULL\n");
2640 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2642 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2647 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2649 memset(&options, 0, sizeof(options));
2651 if (yaffs_parse_options(&options, data_str)) {
2652 /* Option parsing failed */
2656 sb->s_blocksize = PAGE_CACHE_SIZE;
2657 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2659 yaffs_trace(YAFFS_TRACE_OS,
2660 "yaffs_read_super: Using yaffs%d", yaffs_version);
2661 yaffs_trace(YAFFS_TRACE_OS,
2662 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2664 yaffs_trace(YAFFS_TRACE_ALWAYS,
2665 "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
2666 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2667 yaffs_devname(sb, devname_buf));
2669 /* Check it's an mtd device..... */
2670 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2671 return NULL; /* This isn't an mtd device */
2673 /* Get the device */
2674 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2676 yaffs_trace(YAFFS_TRACE_ALWAYS,
2677 "yaffs: MTD device #%u doesn't appear to exist",
2681 /* Check it's NAND */
2682 if (mtd->type != MTD_NANDFLASH) {
2683 yaffs_trace(YAFFS_TRACE_ALWAYS,
2684 "yaffs: MTD device is not NAND it's type %d",
2689 yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase);
2690 yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read);
2691 yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write);
2692 yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob);
2693 yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob);
2694 yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad);
2695 yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad);
2696 yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
2697 yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
2698 yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
2699 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2700 yaffs_trace(YAFFS_TRACE_OS, " size %u", mtd->size);
2702 yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
2705 if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2706 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2710 /* Added NCB 26/5/2006 for completeness */
2711 if (yaffs_version == 2 && !options.inband_tags
2712 && WRITE_SIZE(mtd) == 512) {
2713 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2717 if (yaffs_version == 2) {
2718 /* Check for version 2 style functions */
2720 !mtd->block_isbad ||
2721 !mtd->block_markbad || !mtd->read || !mtd->write ||
2722 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2723 !mtd->read_oob || !mtd->write_oob) {
2726 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2728 yaffs_trace(YAFFS_TRACE_ALWAYS,
2729 "MTD device does not support required functions"
2734 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2735 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2736 !options.inband_tags) {
2737 yaffs_trace(YAFFS_TRACE_ALWAYS,
2738 "MTD device does not have the right page sizes"
2743 /* Check for V1 style functions */
2744 if (!mtd->erase || !mtd->read || !mtd->write ||
2745 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2746 !mtd->read_oob || !mtd->write_oob) {
2749 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2751 yaffs_trace(YAFFS_TRACE_ALWAYS,
2752 "MTD device does not support required functions"
2757 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2758 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2759 yaffs_trace(YAFFS_TRACE_ALWAYS,
2760 "MTD device does not support have the right page sizes"
2766 /* OK, so if we got here, we have an MTD that's NAND and looks
2767 * like it has the right capabilities
2768 * Set the struct yaffs_dev up for mtd
2771 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2774 "yaffs: mtd is read only, setting superblock read only\n"
2776 sb->s_flags |= MS_RDONLY;
2779 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2780 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2782 if (!dev || !context) {
2792 /* Deep shit could not allocate device structure */
2793 yaffs_trace(YAFFS_TRACE_ALWAYS,
2794 "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
2798 memset(dev, 0, sizeof(struct yaffs_dev));
2799 param = &(dev->param);
2801 memset(context, 0, sizeof(struct yaffs_linux_context));
2802 dev->os_context = context;
2803 INIT_LIST_HEAD(&(context->context_list));
2805 context->super = sb;
2807 dev->read_only = read_only;
2809 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2810 sb->s_fs_info = dev;
2812 sb->u.generic_sbp = dev;
2815 dev->driver_context = mtd;
2816 param->name = mtd->name;
2818 /* Set up the memory size parameters.... */
2821 YCALCBLOCKS(mtd->size,
2822 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2824 param->start_block = 0;
2825 param->end_block = n_blocks - 1;
2826 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2827 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2828 param->n_reserved_blocks = 5;
2829 param->n_caches = (options.no_cache) ? 0 : 10;
2830 param->inband_tags = options.inband_tags;
2832 param->enable_xattr = 1;
2833 if (options.lazy_loading_overridden)
2834 param->disable_lazy_load = !options.lazy_loading_enabled;
2836 param->defered_dir_update = 1;
2838 if (options.tags_ecc_overridden)
2839 param->no_tags_ecc = !options.tags_ecc_on;
2841 param->empty_lost_n_found = 1;
2842 param->refresh_period = 500;
2843 param->disable_summary = options.disable_summary;
2845 if (options.empty_lost_and_found_overridden)
2846 param->empty_lost_n_found = options.empty_lost_and_found;
2848 /* ... and the functions. */
2849 if (yaffs_version == 2) {
2850 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2851 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2852 param->bad_block_fn = nandmtd2_mark_block_bad;
2853 param->query_block_fn = nandmtd2_query_block;
2854 yaffs_dev_to_lc(dev)->spare_buffer =
2855 kmalloc(mtd->oobsize, GFP_NOFS);
2856 param->is_yaffs2 = 1;
2857 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2858 param->total_bytes_per_chunk = mtd->writesize;
2859 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2861 param->total_bytes_per_chunk = mtd->oobblock;
2862 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
2864 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2866 param->start_block = 0;
2867 param->end_block = n_blocks - 1;
2869 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2870 /* use the MTD interface in yaffs_mtdif1.c */
2871 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2872 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2873 param->bad_block_fn = nandmtd1_mark_block_bad;
2874 param->query_block_fn = nandmtd1_query_block;
2876 param->write_chunk_fn = nandmtd_write_chunk;
2877 param->read_chunk_fn = nandmtd_read_chunk;
2879 param->is_yaffs2 = 0;
2881 /* ... and common functions */
2882 param->erase_fn = nandmtd_erase_block;
2883 param->initialise_flash_fn = nandmtd_initialise;
2885 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2887 param->sb_dirty_fn = yaffs_touch_super;
2888 param->gc_control = yaffs_gc_control_callback;
2890 yaffs_dev_to_lc(dev)->super = sb;
2892 param->use_nand_ecc = 1;
2894 param->skip_checkpt_rd = options.skip_checkpoint_read;
2895 param->skip_checkpt_wr = options.skip_checkpoint_write;
2897 mutex_lock(&yaffs_context_lock);
2898 /* Get a mount id */
2900 for (mount_id = 0; !found; mount_id++) {
2902 list_for_each(l, &yaffs_context_list) {
2904 list_entry(l, struct yaffs_linux_context,
2906 if (context_iterator->mount_id == mount_id)
2910 context->mount_id = mount_id;
2912 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2913 &yaffs_context_list);
2914 mutex_unlock(&yaffs_context_lock);
2916 /* Directory search handling... */
2917 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2918 param->remove_obj_fn = yaffs_remove_obj_callback;
2920 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2922 yaffs_gross_lock(dev);
2924 err = yaffs_guts_initialise(dev);
2926 yaffs_trace(YAFFS_TRACE_OS,
2927 "yaffs_read_super: guts initialised %s",
2928 (err == YAFFS_OK) ? "OK" : "FAILED");
2930 if (err == YAFFS_OK)
2931 yaffs_bg_start(dev);
2933 if (!context->bg_thread)
2934 param->defered_dir_update = 0;
2936 /* Release lock before yaffs_get_inode() */
2937 yaffs_gross_unlock(dev);
2939 /* Create root inode */
2940 if (err == YAFFS_OK)
2941 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2946 inode->i_op = &yaffs_dir_inode_operations;
2947 inode->i_fop = &yaffs_dir_operations;
2949 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2951 root = d_alloc_root(inode);
2953 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
2960 sb->s_dirt = !dev->is_checkpointed;
2961 yaffs_trace(YAFFS_TRACE_ALWAYS,
2962 "yaffs_read_super: is_checkpointed %d",
2963 dev->is_checkpointed);
2965 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
2969 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2970 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2973 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2976 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2977 static int yaffs_read_super(struct file_system_type *fs,
2978 int flags, const char *dev_name,
2979 void *data, struct vfsmount *mnt)
2982 return get_sb_bdev(fs, flags, dev_name, data,
2983 yaffs_internal_read_super_mtd, mnt);
2986 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2987 int flags, const char *dev_name,
2991 return get_sb_bdev(fs, flags, dev_name, data,
2992 yaffs_internal_read_super_mtd);
2996 static struct file_system_type yaffs_fs_type = {
2997 .owner = THIS_MODULE,
2999 .get_sb = yaffs_read_super,
3000 .kill_sb = kill_block_super,
3001 .fs_flags = FS_REQUIRES_DEV,
3004 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3007 return yaffs_internal_read_super(1, sb, data, silent);
3010 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3015 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3016 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3019 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3022 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3023 static int yaffs2_read_super(struct file_system_type *fs,
3024 int flags, const char *dev_name, void *data,
3025 struct vfsmount *mnt)
3027 return get_sb_bdev(fs, flags, dev_name, data,
3028 yaffs2_internal_read_super_mtd, mnt);
3031 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3032 int flags, const char *dev_name,
3036 return get_sb_bdev(fs, flags, dev_name, data,
3037 yaffs2_internal_read_super_mtd);
3041 static struct file_system_type yaffs2_fs_type = {
3042 .owner = THIS_MODULE,
3044 .get_sb = yaffs2_read_super,
3045 .kill_sb = kill_block_super,
3046 .fs_flags = FS_REQUIRES_DEV,
3049 static struct super_block *yaffs2_read_super(struct super_block *sb,
3050 void *data, int silent)
3052 return yaffs_internal_read_super(2, sb, data, silent);
3055 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3060 static struct proc_dir_entry *my_proc_entry;
3062 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3064 struct yaffs_param *param = &dev->param;
3066 buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
3067 buf += sprintf(buf, "end_block............ %d\n", param->end_block);
3068 buf += sprintf(buf, "total_bytes_per_chunk %d\n",
3069 param->total_bytes_per_chunk);
3070 buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
3071 buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
3072 buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
3073 buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
3074 buf += sprintf(buf, "empty_lost_n_found... %d\n",
3075 param->empty_lost_n_found);
3076 buf += sprintf(buf, "disable_lazy_load.... %d\n",
3077 param->disable_lazy_load);
3078 buf += sprintf(buf, "refresh_period....... %d\n",
3079 param->refresh_period);
3080 buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
3081 buf += sprintf(buf, "n_reserved_blocks.... %d\n",
3082 param->n_reserved_blocks);
3083 buf += sprintf(buf, "always_check_erased.. %d\n",
3084 param->always_check_erased);
3085 buf += sprintf(buf, "\n");
3090 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3092 buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
3093 dev->data_bytes_per_chunk);
3094 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3095 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3096 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3097 buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
3098 dev->blocks_in_checkpt);
3099 buf += sprintf(buf, "\n");
3100 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3101 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3102 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3103 buf += sprintf(buf, "\n");
3104 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3105 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3106 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3107 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3108 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3109 buf += sprintf(buf, "passive_gc_count..... %u\n",
3110 dev->passive_gc_count);
3111 buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
3112 dev->oldest_dirty_gc_count);
3113 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3114 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3115 buf += sprintf(buf, "n_retired_writes..... %u\n",
3116 dev->n_retired_writes);
3117 buf += sprintf(buf, "n_retired_blocks..... %u\n",
3118 dev->n_retired_blocks);
3119 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3120 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3121 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
3122 dev->n_tags_ecc_fixed);
3123 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
3124 dev->n_tags_ecc_unfixed);
3125 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3126 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3127 buf += sprintf(buf, "n_unlinked_files..... %u\n",
3128 dev->n_unlinked_files);
3129 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3130 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3131 buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
3132 buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
3137 static int yaffs_proc_read(char *page,
3139 off_t offset, int count, int *eof, void *data)
3141 struct list_head *item;
3146 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3147 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3148 * This also assumes the user has posted a read buffer large
3149 * enough to hold the complete output; but that's life in /proc.
3154 /* Print header first */
3158 "Multi-version YAFFS built:" __DATE__ " " __TIME__
3161 buf += sprintf(buf, "\n");
3165 mutex_lock(&yaffs_context_lock);
3167 /* Locate and print the Nth entry. Order N-squared but N is small. */
3168 list_for_each(item, &yaffs_context_list) {
3169 struct yaffs_linux_context *dc =
3170 list_entry(item, struct yaffs_linux_context,
3172 struct yaffs_dev *dev = dc->dev;
3174 if (n < (step & ~1)) {
3178 if ((step & 1) == 0) {
3180 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3182 buf = yaffs_dump_dev_part0(buf, dev);
3184 buf = yaffs_dump_dev_part1(buf, dev);
3189 mutex_unlock(&yaffs_context_lock);
3192 return buf - page < count ? buf - page : count;
3196 * Set the verbosity of the warnings and error messages.
3198 * Note that the names can only be a..z or _ with the current code.
3203 unsigned mask_bitfield;
3205 {"allocate", YAFFS_TRACE_ALLOCATE},
3206 {"always", YAFFS_TRACE_ALWAYS},
3207 {"background", YAFFS_TRACE_BACKGROUND},
3208 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3209 {"buffers", YAFFS_TRACE_BUFFERS},
3210 {"bug", YAFFS_TRACE_BUG},
3211 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3212 {"deletion", YAFFS_TRACE_DELETION},
3213 {"erase", YAFFS_TRACE_ERASE},
3214 {"error", YAFFS_TRACE_ERROR},
3215 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3216 {"gc", YAFFS_TRACE_GC},
3217 {"lock", YAFFS_TRACE_LOCK},
3218 {"mtd", YAFFS_TRACE_MTD},
3219 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3220 {"os", YAFFS_TRACE_OS},
3221 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3222 {"scan", YAFFS_TRACE_SCAN},
3223 {"mount", YAFFS_TRACE_MOUNT},
3224 {"tracing", YAFFS_TRACE_TRACING},
3225 {"sync", YAFFS_TRACE_SYNC},
3226 {"write", YAFFS_TRACE_WRITE},
3227 {"verify", YAFFS_TRACE_VERIFY},
3228 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3229 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3230 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3231 {"all", 0xffffffff},
3236 #define MAX_MASK_NAME_LENGTH 40
3237 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3238 unsigned long count, void *data)
3240 unsigned rg = 0, mask_bitfield;
3244 char substring[MAX_MASK_NAME_LENGTH + 1];
3250 rg = yaffs_trace_mask;
3252 while (!done && (pos < count)) {
3254 while ((pos < count) && isspace(buf[pos]))
3271 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3273 if (end > buf + pos) {
3274 mask_name = "numeral";
3275 len = end - (buf + pos);
3279 for (x = buf + pos, i = 0;
3280 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3281 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3283 substring[i] = '\0';
3285 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3286 if (strcmp(substring, mask_flags[i].mask_name)
3288 mask_name = mask_flags[i].mask_name;
3290 mask_flags[i].mask_bitfield;
3297 if (mask_name != NULL) {
3301 rg &= ~mask_bitfield;
3304 rg |= mask_bitfield;
3310 rg |= mask_bitfield;
3316 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3318 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3320 if (rg & YAFFS_TRACE_ALWAYS) {
3321 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3323 flag = ((rg & mask_flags[i].mask_bitfield) ==
3324 mask_flags[i].mask_bitfield) ? '+' : '-';
3325 printk(KERN_DEBUG "%c%s\n", flag,
3326 mask_flags[i].mask_name);
3333 static int yaffs_proc_write(struct file *file, const char *buf,
3334 unsigned long count, void *data)
3336 return yaffs_proc_write_trace_options(file, buf, count, data);
3339 /* Stuff to handle installation of file systems */
3340 struct file_system_to_install {
3341 struct file_system_type *fst;
3345 static struct file_system_to_install fs_to_install[] = {
3346 {&yaffs_fs_type, 0},
3347 {&yaffs2_fs_type, 0},
3351 static int __init init_yaffs_fs(void)
3354 struct file_system_to_install *fsinst;
3356 yaffs_trace(YAFFS_TRACE_ALWAYS,
3357 "yaffs built " __DATE__ " " __TIME__ " Installing.");
3359 mutex_init(&yaffs_context_lock);
3361 /* Install the proc_fs entries */
3362 my_proc_entry = create_proc_entry("yaffs",
3363 S_IRUGO | S_IFREG, YPROC_ROOT);
3365 if (my_proc_entry) {
3366 my_proc_entry->write_proc = yaffs_proc_write;
3367 my_proc_entry->read_proc = yaffs_proc_read;
3368 my_proc_entry->data = NULL;
3373 /* Now add the file system entries */
3375 fsinst = fs_to_install;
3377 while (fsinst->fst && !error) {
3378 error = register_filesystem(fsinst->fst);
3380 fsinst->installed = 1;
3384 /* Any errors? uninstall */
3386 fsinst = fs_to_install;
3388 while (fsinst->fst) {
3389 if (fsinst->installed) {
3390 unregister_filesystem(fsinst->fst);
3391 fsinst->installed = 0;
3400 static void __exit exit_yaffs_fs(void)
3403 struct file_system_to_install *fsinst;
3405 yaffs_trace(YAFFS_TRACE_ALWAYS,
3406 "yaffs built " __DATE__ " " __TIME__ " removing.");
3408 remove_proc_entry("yaffs", YPROC_ROOT);
3410 fsinst = fs_to_install;
3412 while (fsinst->fst) {
3413 if (fsinst->installed) {
3414 unregister_filesystem(fsinst->fst);
3415 fsinst->installed = 0;
3421 module_init(init_yaffs_fs)
3422 module_exit(exit_yaffs_fs)
3424 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3425 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
3426 MODULE_LICENSE("GPL");