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 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
76 #include <linux/smp_lock.h>
78 #include <linux/pagemap.h>
79 #include <linux/mtd/mtd.h>
80 #include <linux/interrupt.h>
81 #include <linux/string.h>
82 #include <linux/ctype.h>
84 #if (YAFFS_NEW_FOLLOW_LINK == 1)
85 #include <linux/namei.h>
88 #ifdef YAFFS_COMPILE_EXPORTFS
89 #include <linux/exportfs.h>
92 #ifdef YAFFS_COMPILE_BACKGROUND
93 #include <linux/kthread.h>
94 #include <linux/delay.h>
96 #ifdef YAFFS_COMPILE_FREEZER
97 #include <linux/freezer.h>
100 #include <asm/div64.h>
102 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
104 #include <linux/statfs.h>
106 #define UnlockPage(p) unlock_page(p)
107 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
109 /* FIXME: use sb->s_id instead ? */
110 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
114 #include <linux/locks.h>
115 #define BDEVNAME_SIZE 0
116 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
118 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
119 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
125 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
126 #define YPROC_ROOT (&proc_root)
128 #define YPROC_ROOT NULL
131 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
132 #define Y_INIT_TIMER(a) init_timer(a)
134 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
137 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
138 #define WRITE_SIZE_STR "writesize"
139 #define WRITE_SIZE(mtd) ((mtd)->writesize)
141 #define WRITE_SIZE_STR "oobblock"
142 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
145 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
146 #define YAFFS_USE_WRITE_BEGIN_END 1
148 #define YAFFS_USE_WRITE_BEGIN_END 0
153 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
154 #define set_nlink(inode, count) do { (inode)->i_nlink = (count); } while(0)
157 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
158 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
160 uint64_t result = partition_size;
161 do_div(result, block_size);
162 return (uint32_t) result;
165 #define YCALCBLOCKS(s, b) ((s)/(b))
168 #include <linux/uaccess.h>
169 #include <linux/mtd/mtd.h>
171 #include "yportenv.h"
172 #include "yaffs_trace.h"
173 #include "yaffs_guts.h"
174 #include "yaffs_attribs.h"
176 #include "yaffs_linux.h"
178 #include "yaffs_mtdif.h"
179 #include "yaffs_mtdif1.h"
180 #include "yaffs_mtdif2.h"
182 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
183 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
184 unsigned int yaffs_auto_checkpoint = 1;
185 unsigned int yaffs_gc_control = 1;
186 unsigned int yaffs_bg_enable = 1;
187 unsigned int yaffs_auto_select = 1;
188 /* Module Parameters */
189 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
190 module_param(yaffs_trace_mask, uint, 0644);
191 module_param(yaffs_wr_attempts, uint, 0644);
192 module_param(yaffs_auto_checkpoint, uint, 0644);
193 module_param(yaffs_gc_control, uint, 0644);
194 module_param(yaffs_bg_enable, uint, 0644);
196 MODULE_PARM(yaffs_trace_mask, "i");
197 MODULE_PARM(yaffs_wr_attempts, "i");
198 MODULE_PARM(yaffs_auto_checkpoint, "i");
199 MODULE_PARM(yaffs_gc_control, "i");
202 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
203 /* use iget and read_inode */
204 #define Y_IGET(sb, inum) iget((sb), (inum))
205 static void yaffs_read_inode(struct inode *inode);
208 /* Call local equivalent */
209 #define YAFFS_USE_OWN_IGET
210 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
212 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
215 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
216 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
218 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
221 #define yaffs_inode_to_obj(iptr) \
222 ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
223 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
225 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
226 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
228 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
231 #define update_dir_time(dir) do {\
232 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
235 static void yaffs_put_super(struct super_block *sb);
237 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
239 static ssize_t yaffs_hold_space(struct file *f);
240 static void yaffs_release_space(struct file *f);
242 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
243 static int yaffs_file_flush(struct file *file, fl_owner_t id);
245 static int yaffs_file_flush(struct file *file);
248 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
249 static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync);
250 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
251 static int yaffs_sync_object(struct file *file, int datasync);
253 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
257 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
259 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
260 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
261 struct nameidata *n);
262 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
263 struct nameidata *n);
265 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
266 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
268 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
269 struct dentry *dentry);
270 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
271 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
272 const char *symname);
273 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
275 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
276 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
279 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
282 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
283 struct inode *new_dir, struct dentry *new_dentry);
284 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
286 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
287 static int yaffs_sync_fs(struct super_block *sb, int wait);
288 static void yaffs_write_super(struct super_block *sb);
290 static int yaffs_sync_fs(struct super_block *sb);
291 static int yaffs_write_super(struct super_block *sb);
294 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
295 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
296 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
297 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
299 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
302 #ifdef YAFFS_HAS_PUT_INODE
303 static void yaffs_put_inode(struct inode *inode);
306 #ifdef YAFFS_HAS_EVICT_INODE
307 static void yaffs_evict_inode(struct inode *);
309 static void yaffs_delete_inode(struct inode *);
310 static void yaffs_clear_inode(struct inode *);
313 static int yaffs_readpage(struct file *file, struct page *page);
314 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
315 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
317 static int yaffs_writepage(struct page *page);
320 static int yaffs_setxattr(struct dentry *dentry, const char *name,
321 const void *value, size_t size, int flags);
322 static ssize_t yaffs_getxattr(struct dentry *dentry, const char *name,
323 void *buff, size_t size);
324 static int yaffs_removexattr(struct dentry *dentry, const char *name);
325 static ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
327 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
328 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
329 loff_t pos, unsigned len, unsigned flags,
330 struct page **pagep, void **fsdata);
331 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
332 loff_t pos, unsigned len, unsigned copied,
333 struct page *pg, void *fsdadata);
335 static int yaffs_prepare_write(struct file *f, struct page *pg,
336 unsigned offset, unsigned to);
337 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
342 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
344 #if (YAFFS_NEW_FOLLOW_LINK == 1)
345 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
346 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
348 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
351 static void yaffs_touch_super(struct yaffs_dev *dev);
353 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
355 static struct address_space_operations yaffs_file_address_operations = {
356 .readpage = yaffs_readpage,
357 .writepage = yaffs_writepage,
358 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
359 .write_begin = yaffs_write_begin,
360 .write_end = yaffs_write_end,
362 .prepare_write = yaffs_prepare_write,
363 .commit_write = yaffs_commit_write,
367 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
368 static const struct file_operations yaffs_file_operations = {
369 .read = do_sync_read,
370 .write = do_sync_write,
371 .aio_read = generic_file_aio_read,
372 .aio_write = generic_file_aio_write,
373 .mmap = generic_file_mmap,
374 .flush = yaffs_file_flush,
375 .fsync = yaffs_sync_object,
376 .splice_read = generic_file_splice_read,
377 .splice_write = generic_file_splice_write,
378 .llseek = generic_file_llseek,
381 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
383 static const struct file_operations yaffs_file_operations = {
384 .read = do_sync_read,
385 .write = do_sync_write,
386 .aio_read = generic_file_aio_read,
387 .aio_write = generic_file_aio_write,
388 .mmap = generic_file_mmap,
389 .flush = yaffs_file_flush,
390 .fsync = yaffs_sync_object,
391 .sendfile = generic_file_sendfile,
396 static const struct file_operations yaffs_file_operations = {
397 .read = generic_file_read,
398 .write = generic_file_write,
399 .mmap = generic_file_mmap,
400 .flush = yaffs_file_flush,
401 .fsync = yaffs_sync_object,
402 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
403 .sendfile = generic_file_sendfile,
408 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
409 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
411 void *kaddr = kmap_atomic(page, KM_USER0);
412 memset(kaddr + start, 0, end - start);
413 kunmap_atomic(kaddr, KM_USER0);
414 flush_dcache_page(page);
418 static const struct inode_operations yaffs_file_inode_operations = {
419 .setattr = yaffs_setattr,
420 .setxattr = yaffs_setxattr,
421 .getxattr = yaffs_getxattr,
422 .listxattr = yaffs_listxattr,
423 .removexattr = yaffs_removexattr,
426 static const struct inode_operations yaffs_symlink_inode_operations = {
427 .readlink = yaffs_readlink,
428 .follow_link = yaffs_follow_link,
429 #if (YAFFS_NEW_FOLLOW_LINK == 1)
430 .put_link = yaffs_put_link,
432 .setattr = yaffs_setattr,
433 .setxattr = yaffs_setxattr,
434 .getxattr = yaffs_getxattr,
435 .listxattr = yaffs_listxattr,
436 .removexattr = yaffs_removexattr,
439 static const struct inode_operations yaffs_dir_inode_operations = {
440 .create = yaffs_create,
441 .lookup = yaffs_lookup,
443 .unlink = yaffs_unlink,
444 .symlink = yaffs_symlink,
445 .mkdir = yaffs_mkdir,
446 .rmdir = yaffs_unlink,
447 .mknod = yaffs_mknod,
448 .rename = yaffs_rename,
449 .setattr = yaffs_setattr,
450 .setxattr = yaffs_setxattr,
451 .getxattr = yaffs_getxattr,
452 .listxattr = yaffs_listxattr,
453 .removexattr = yaffs_removexattr,
456 static const struct file_operations yaffs_dir_operations = {
457 .read = generic_read_dir,
458 .readdir = yaffs_readdir,
459 .fsync = yaffs_sync_object,
460 .llseek = generic_file_llseek,
463 static const struct super_operations yaffs_super_ops = {
464 .statfs = yaffs_statfs,
466 #ifndef YAFFS_USE_OWN_IGET
467 .read_inode = yaffs_read_inode,
469 #ifdef YAFFS_HAS_PUT_INODE
470 .put_inode = yaffs_put_inode,
472 .put_super = yaffs_put_super,
473 #ifdef YAFFS_HAS_EVICT_INODE
474 .evict_inode = yaffs_evict_inode,
476 .delete_inode = yaffs_delete_inode,
477 .clear_inode = yaffs_clear_inode,
479 .sync_fs = yaffs_sync_fs,
480 .write_super = yaffs_write_super,
483 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
485 #ifdef YAFFS_USE_SETATTR_COPY
486 setattr_copy(inode, attr);
489 return inode_setattr(inode, attr);
494 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
496 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
497 truncate_setsize(inode, newsize);
500 truncate_inode_pages(&inode->i_data, newsize);
506 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
508 return yaffs_gc_control;
511 static void yaffs_gross_lock(struct yaffs_dev *dev)
513 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
514 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
515 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
518 static void yaffs_gross_unlock(struct yaffs_dev *dev)
520 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
521 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
524 #ifdef YAFFS_COMPILE_EXPORTFS
526 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
529 return Y_IGET(sb, ino);
532 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
533 struct fid *fid, int fh_len,
536 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
537 yaffs2_nfs_get_inode);
540 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
541 struct fid *fid, int fh_len,
544 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
545 yaffs2_nfs_get_inode);
548 struct dentry *yaffs2_get_parent(struct dentry *dentry)
551 struct super_block *sb = dentry->d_inode->i_sb;
552 struct dentry *parent = ERR_PTR(-ENOENT);
554 unsigned long parent_ino;
555 struct yaffs_obj *d_obj;
556 struct yaffs_obj *parent_obj;
558 d_obj = yaffs_inode_to_obj(dentry->d_inode);
561 parent_obj = d_obj->parent;
563 parent_ino = yaffs_get_obj_inode(parent_obj);
564 inode = Y_IGET(sb, parent_ino);
567 parent = ERR_CAST(inode);
569 parent = d_obtain_alias(inode);
570 if (!IS_ERR(parent)) {
571 parent = ERR_PTR(-ENOMEM);
581 /* Just declare a zero structure as a NULL value implies
582 * using the default functions of exportfs.
585 static struct export_operations yaffs_export_ops = {
586 .fh_to_dentry = yaffs2_fh_to_dentry,
587 .fh_to_parent = yaffs2_fh_to_parent,
588 .get_parent = yaffs2_get_parent,
593 /*-----------------------------------------------------------------*/
594 /* Directory search context allows us to unlock access to yaffs during
595 * filldir without causing problems with the directory being modified.
596 * This is similar to the tried and tested mechanism used in yaffs direct.
598 * A search context iterates along a doubly linked list of siblings in the
599 * directory. If the iterating object is deleted then this would corrupt
600 * the list iteration, likely causing a crash. The search context avoids
601 * this by using the remove_obj_fn to move the search context to the
602 * next object before the object is deleted.
604 * Many readdirs (and thus seach conexts) may be alive simulateously so
605 * each struct yaffs_dev has a list of these.
607 * A seach context lives for the duration of a readdir.
609 * All these functions must be called while yaffs is locked.
612 struct yaffs_search_context {
613 struct yaffs_dev *dev;
614 struct yaffs_obj *dir_obj;
615 struct yaffs_obj *next_return;
616 struct list_head others;
620 * yaffs_new_search() creates a new search context, initialises it and
621 * adds it to the device's search context list.
623 * Called at start of readdir.
625 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
627 struct yaffs_dev *dev = dir->my_dev;
628 struct yaffs_search_context *sc =
629 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
633 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
634 sc->next_return = NULL;
637 list_entry(dir->variant.dir_variant.children.next,
638 struct yaffs_obj, siblings);
639 INIT_LIST_HEAD(&sc->others);
640 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
646 * yaffs_search_end() disposes of a search context and cleans up.
648 static void yaffs_search_end(struct yaffs_search_context *sc)
651 list_del(&sc->others);
657 * yaffs_search_advance() moves a search context to the next object.
658 * Called when the search iterates or when an object removal causes
659 * the search context to be moved to the next object.
661 static void yaffs_search_advance(struct yaffs_search_context *sc)
666 if (sc->next_return == NULL ||
667 list_empty(&sc->dir_obj->variant.dir_variant.children))
668 sc->next_return = NULL;
670 struct list_head *next = sc->next_return->siblings.next;
672 if (next == &sc->dir_obj->variant.dir_variant.children)
673 sc->next_return = NULL; /* end of list */
676 list_entry(next, struct yaffs_obj, siblings);
681 * yaffs_remove_obj_callback() is called when an object is unlinked.
682 * We check open search contexts and advance any which are currently
683 * on the object being iterated.
685 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
689 struct yaffs_search_context *sc;
690 struct list_head *search_contexts =
691 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
693 /* Iterate through the directory search contexts.
694 * If any are currently on the object being removed, then advance
695 * the search context to the next object to prevent a hanging pointer.
697 list_for_each(i, search_contexts) {
698 sc = list_entry(i, struct yaffs_search_context, others);
699 if (sc->next_return == obj)
700 yaffs_search_advance(sc);
705 /*-----------------------------------------------------------------*/
707 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
710 unsigned char *alias;
713 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
715 yaffs_gross_lock(dev);
717 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
719 yaffs_gross_unlock(dev);
724 ret = vfs_readlink(dentry, buffer, buflen, alias);
729 #if (YAFFS_NEW_FOLLOW_LINK == 1)
730 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
734 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
738 unsigned char *alias;
740 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
742 yaffs_gross_lock(dev);
744 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
745 yaffs_gross_unlock(dev);
751 #if (YAFFS_NEW_FOLLOW_LINK == 1)
752 nd_set_link(nd, alias);
756 ret = ERR_PTR(ret_int);
759 ret = vfs_follow_link(nd, alias);
768 #if (YAFFS_NEW_FOLLOW_LINK == 1)
769 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
775 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
776 struct yaffs_obj *obj);
779 * Lookup is used to find objects in the fs
781 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
783 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
786 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
789 struct yaffs_obj *obj;
790 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
792 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
794 if (current != yaffs_dev_to_lc(dev)->readdir_process)
795 yaffs_gross_lock(dev);
797 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
798 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
800 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
802 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
804 /* Can't hold gross lock when calling yaffs_get_inode() */
805 if (current != yaffs_dev_to_lc(dev)->readdir_process)
806 yaffs_gross_unlock(dev);
809 yaffs_trace(YAFFS_TRACE_OS,
810 "yaffs_lookup found %d", obj->obj_id);
812 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
814 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
818 /* added NCB for 2.5/6 compatability - forces add even if inode is
819 * NULL which creates dentry hash */
820 d_add(dentry, inode);
825 #ifdef YAFFS_HAS_PUT_INODE
827 /* For now put inode is just for debugging
828 * Put inode is called when the inode **structure** is put.
830 static void yaffs_put_inode(struct inode *inode)
832 yaffs_trace(YAFFS_TRACE_OS,
833 "yaffs_put_inode: ino %d, count %d"),
834 (int)inode->i_ino, atomic_read(&inode->i_count);
839 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
841 /* Clear the association between the inode and
842 * the struct yaffs_obj.
844 obj->my_inode = NULL;
845 yaffs_inode_to_obj_lv(inode) = NULL;
847 /* If the object freeing was deferred, then the real
849 * This should fix the inode inconsistency problem.
851 yaffs_handle_defered_free(obj);
854 #ifdef YAFFS_HAS_EVICT_INODE
855 /* yaffs_evict_inode combines into one operation what was previously done in
856 * yaffs_clear_inode() and yaffs_delete_inode()
859 static void yaffs_evict_inode(struct inode *inode)
861 struct yaffs_obj *obj;
862 struct yaffs_dev *dev;
865 obj = yaffs_inode_to_obj(inode);
867 yaffs_trace(YAFFS_TRACE_OS,
868 "yaffs_evict_inode: ino %d, count %d %s",
869 (int)inode->i_ino, atomic_read(&inode->i_count),
870 obj ? "object exists" : "null object");
872 if (!inode->i_nlink && !is_bad_inode(inode))
874 truncate_inode_pages(&inode->i_data, 0);
875 end_writeback(inode);
877 if (deleteme && obj) {
879 yaffs_gross_lock(dev);
881 yaffs_gross_unlock(dev);
885 yaffs_gross_lock(dev);
886 yaffs_unstitch_obj(inode, obj);
887 yaffs_gross_unlock(dev);
892 /* clear is called to tell the fs to release any per-inode data it holds.
893 * The object might still exist on disk and is just being thrown out of the cache
894 * or else the object has actually been deleted and we're being called via
896 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
899 static void yaffs_clear_inode(struct inode *inode)
901 struct yaffs_obj *obj;
902 struct yaffs_dev *dev;
904 obj = yaffs_inode_to_obj(inode);
906 yaffs_trace(YAFFS_TRACE_OS,
907 "yaffs_clear_inode: ino %d, count %d %s",
908 (int)inode->i_ino, atomic_read(&inode->i_count),
909 obj ? "object exists" : "null object");
913 yaffs_gross_lock(dev);
914 yaffs_unstitch_obj(inode, obj);
915 yaffs_gross_unlock(dev);
920 /* delete is called when the link count is zero and the inode
921 * is put (ie. nobody wants to know about it anymore, time to
923 * NB Must call clear_inode()
925 static void yaffs_delete_inode(struct inode *inode)
927 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
928 struct yaffs_dev *dev;
930 yaffs_trace(YAFFS_TRACE_OS,
931 "yaffs_delete_inode: ino %d, count %d %s",
932 (int)inode->i_ino, atomic_read(&inode->i_count),
933 obj ? "object exists" : "null object");
937 yaffs_gross_lock(dev);
939 yaffs_gross_unlock(dev);
941 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
942 truncate_inode_pages(&inode->i_data, 0);
948 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
949 static int yaffs_file_flush(struct file *file, fl_owner_t id)
951 static int yaffs_file_flush(struct file *file)
954 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
956 struct yaffs_dev *dev = obj->my_dev;
958 yaffs_trace(YAFFS_TRACE_OS,
959 "yaffs_file_flush object %d (%s)",
961 obj->dirty ? "dirty" : "clean");
963 yaffs_gross_lock(dev);
965 yaffs_flush_file(obj, 1, 0);
967 yaffs_gross_unlock(dev);
972 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
974 /* Lifted from jffs2 */
976 struct yaffs_obj *obj;
977 unsigned char *pg_buf;
979 loff_t pos = ((loff_t) pg->index) << PAGE_CACHE_SHIFT;
981 struct yaffs_dev *dev;
983 yaffs_trace(YAFFS_TRACE_OS,
984 "yaffs_readpage_nolock at %lld, size %08x",
986 (unsigned)PAGE_CACHE_SIZE);
988 obj = yaffs_dentry_to_obj(f->f_dentry);
992 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
993 BUG_ON(!PageLocked(pg));
1000 /* FIXME: Can kmap fail? */
1002 yaffs_gross_lock(dev);
1004 ret = yaffs_file_rd(obj, pg_buf, pos, PAGE_CACHE_SIZE);
1006 yaffs_gross_unlock(dev);
1012 ClearPageUptodate(pg);
1015 SetPageUptodate(pg);
1019 flush_dcache_page(pg);
1022 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
1026 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1028 int ret = yaffs_readpage_nolock(f, pg);
1033 static int yaffs_readpage(struct file *f, struct page *pg)
1037 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
1038 ret = yaffs_readpage_unlock(f, pg);
1039 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
1043 /* writepage inspired by/stolen from smbfs */
1045 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1046 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1048 static int yaffs_writepage(struct page *page)
1051 struct yaffs_dev *dev;
1052 struct address_space *mapping = page->mapping;
1053 struct inode *inode;
1054 unsigned long end_index;
1056 struct yaffs_obj *obj;
1063 inode = mapping->host;
1066 i_size = i_size_read(inode);
1068 end_index = i_size >> PAGE_CACHE_SHIFT;
1070 if (page->index < end_index)
1071 n_bytes = PAGE_CACHE_SIZE;
1073 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
1075 if (page->index > end_index || !n_bytes) {
1076 yaffs_trace(YAFFS_TRACE_OS,
1077 "yaffs_writepage at %lld, inode size = %lld!!",
1078 ((loff_t)page->index) << PAGE_CACHE_SHIFT,
1080 yaffs_trace(YAFFS_TRACE_OS,
1081 " -> don't care!!");
1083 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1084 set_page_writeback(page);
1086 end_page_writeback(page);
1091 if (n_bytes != PAGE_CACHE_SIZE)
1092 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
1096 buffer = kmap(page);
1098 obj = yaffs_inode_to_obj(inode);
1100 yaffs_gross_lock(dev);
1102 yaffs_trace(YAFFS_TRACE_OS,
1103 "yaffs_writepage at %lld, size %08x",
1104 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes);
1105 yaffs_trace(YAFFS_TRACE_OS,
1106 "writepag0: obj = %lld, ino = %lld",
1107 obj->variant.file_variant.file_size, inode->i_size);
1109 n_written = yaffs_wr_file(obj, buffer,
1110 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes, 0);
1112 yaffs_touch_super(dev);
1114 yaffs_trace(YAFFS_TRACE_OS,
1115 "writepag1: obj = %lld, ino = %lld",
1116 obj->variant.file_variant.file_size, inode->i_size);
1118 yaffs_gross_unlock(dev);
1121 set_page_writeback(page);
1123 end_page_writeback(page);
1126 return (n_written == n_bytes) ? 0 : -ENOSPC;
1129 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1130 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1131 loff_t pos, unsigned len, unsigned flags,
1132 struct page **pagep, void **fsdata)
1134 struct page *pg = NULL;
1135 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1141 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1142 pg = grab_cache_page_write_begin(mapping, index, flags);
1144 pg = __grab_cache_page(mapping, index);
1152 yaffs_trace(YAFFS_TRACE_OS,
1153 "start yaffs_write_begin index %d(%x) uptodate %d",
1154 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
1157 space_held = yaffs_hold_space(filp);
1164 /* Update page if required */
1166 if (!Page_Uptodate(pg))
1167 ret = yaffs_readpage_nolock(filp, pg);
1172 /* Happy path return */
1173 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
1178 yaffs_trace(YAFFS_TRACE_OS,
1179 "end yaffs_write_begin fail returning %d", ret);
1181 yaffs_release_space(filp);
1184 page_cache_release(pg);
1191 static int yaffs_prepare_write(struct file *f, struct page *pg,
1192 unsigned offset, unsigned to)
1194 yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
1196 if (!Page_Uptodate(pg))
1197 return yaffs_readpage_nolock(f, pg);
1202 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1203 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1204 loff_t pos, unsigned len, unsigned copied,
1205 struct page *pg, void *fsdadata)
1209 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1212 addr = kva + offset_into_page;
1214 yaffs_trace(YAFFS_TRACE_OS,
1215 "yaffs_write_end addr %p pos %lld n_bytes %d",
1218 ret = yaffs_file_write(filp, addr, copied, &pos);
1220 if (ret != copied) {
1221 yaffs_trace(YAFFS_TRACE_OS,
1222 "yaffs_write_end not same size ret %d copied %d",
1229 yaffs_release_space(filp);
1231 page_cache_release(pg);
1236 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1241 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1242 int n_bytes = to - offset;
1246 addr = kva + offset;
1248 yaffs_trace(YAFFS_TRACE_OS,
1249 "yaffs_commit_write addr %p pos %lld n_bytes %d",
1250 addr, pos, n_bytes);
1252 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
1254 if (n_written != n_bytes) {
1255 yaffs_trace(YAFFS_TRACE_OS,
1256 "yaffs_commit_write not same size n_written %d n_bytes %d",
1257 n_written, n_bytes);
1262 yaffs_trace(YAFFS_TRACE_OS,
1263 "yaffs_commit_write returning %d",
1264 n_written == n_bytes ? 0 : n_written);
1266 return n_written == n_bytes ? 0 : n_written;
1270 static void yaffs_fill_inode_from_obj(struct inode *inode,
1271 struct yaffs_obj *obj)
1275 /* Check mode against the variant type and attempt to repair if broken. */
1276 u32 mode = obj->yst_mode;
1277 switch (obj->variant_type) {
1278 case YAFFS_OBJECT_TYPE_FILE:
1279 if (!S_ISREG(mode)) {
1280 obj->yst_mode &= ~S_IFMT;
1281 obj->yst_mode |= S_IFREG;
1285 case YAFFS_OBJECT_TYPE_SYMLINK:
1286 if (!S_ISLNK(mode)) {
1287 obj->yst_mode &= ~S_IFMT;
1288 obj->yst_mode |= S_IFLNK;
1292 case YAFFS_OBJECT_TYPE_DIRECTORY:
1293 if (!S_ISDIR(mode)) {
1294 obj->yst_mode &= ~S_IFMT;
1295 obj->yst_mode |= S_IFDIR;
1299 case YAFFS_OBJECT_TYPE_UNKNOWN:
1300 case YAFFS_OBJECT_TYPE_HARDLINK:
1301 case YAFFS_OBJECT_TYPE_SPECIAL:
1307 inode->i_flags |= S_NOATIME;
1309 inode->i_ino = obj->obj_id;
1310 inode->i_mode = obj->yst_mode;
1311 inode->i_uid = obj->yst_uid;
1312 inode->i_gid = obj->yst_gid;
1313 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1314 inode->i_blksize = inode->i_sb->s_blocksize;
1316 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1318 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1319 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1320 inode->i_atime.tv_nsec = 0;
1321 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1322 inode->i_mtime.tv_nsec = 0;
1323 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1324 inode->i_ctime.tv_nsec = 0;
1326 inode->i_rdev = obj->yst_rdev;
1327 inode->i_atime = obj->yst_atime;
1328 inode->i_mtime = obj->yst_mtime;
1329 inode->i_ctime = obj->yst_ctime;
1331 inode->i_size = yaffs_get_obj_length(obj);
1332 inode->i_blocks = (inode->i_size + 511) >> 9;
1334 set_nlink(inode, yaffs_get_obj_link_count(obj));
1336 yaffs_trace(YAFFS_TRACE_OS,
1337 "yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
1338 inode->i_mode, inode->i_uid, inode->i_gid,
1339 inode->i_size, atomic_read(&inode->i_count));
1341 switch (obj->yst_mode & S_IFMT) {
1342 default: /* fifo, device or socket */
1343 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1344 init_special_inode(inode, obj->yst_mode,
1345 old_decode_dev(obj->yst_rdev));
1347 init_special_inode(inode, obj->yst_mode,
1348 (dev_t) (obj->yst_rdev));
1351 case S_IFREG: /* file */
1352 inode->i_op = &yaffs_file_inode_operations;
1353 inode->i_fop = &yaffs_file_operations;
1354 inode->i_mapping->a_ops =
1355 &yaffs_file_address_operations;
1357 case S_IFDIR: /* directory */
1358 inode->i_op = &yaffs_dir_inode_operations;
1359 inode->i_fop = &yaffs_dir_operations;
1361 case S_IFLNK: /* symlink */
1362 inode->i_op = &yaffs_symlink_inode_operations;
1366 yaffs_inode_to_obj_lv(inode) = obj;
1368 obj->my_inode = inode;
1371 yaffs_trace(YAFFS_TRACE_OS,
1372 "yaffs_fill_inode invalid parameters");
1377 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1378 struct yaffs_obj *obj)
1380 struct inode *inode;
1383 yaffs_trace(YAFFS_TRACE_OS,
1384 "yaffs_get_inode for NULL super_block!!");
1390 yaffs_trace(YAFFS_TRACE_OS,
1391 "yaffs_get_inode for NULL object!!");
1396 yaffs_trace(YAFFS_TRACE_OS,
1397 "yaffs_get_inode for object %d", obj->obj_id);
1399 inode = Y_IGET(sb, obj->obj_id);
1403 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1404 /* iget also increments the inode's i_count */
1405 /* NB You can't be holding gross_lock or deadlock will happen! */
1410 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1413 struct yaffs_obj *obj;
1416 struct inode *inode;
1417 struct yaffs_dev *dev;
1419 obj = yaffs_dentry_to_obj(f->f_dentry);
1422 yaffs_trace(YAFFS_TRACE_OS,
1423 "yaffs_file_write: hey obj is null!");
1429 yaffs_gross_lock(dev);
1431 inode = f->f_dentry->d_inode;
1433 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1434 ipos = inode->i_size;
1438 yaffs_trace(YAFFS_TRACE_OS,
1439 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %lld",
1440 (unsigned)n, (unsigned)n, obj->obj_id, ipos);
1442 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1444 yaffs_touch_super(dev);
1446 yaffs_trace(YAFFS_TRACE_OS,
1447 "yaffs_file_write: %d(%x) bytes written",
1448 (unsigned)n, (unsigned)n);
1450 if (n_written > 0) {
1453 if (ipos > inode->i_size) {
1454 inode->i_size = ipos;
1455 inode->i_blocks = (ipos + 511) >> 9;
1457 yaffs_trace(YAFFS_TRACE_OS,
1458 "yaffs_file_write size updated to %lld bytes, %d blocks",
1459 ipos, (int)(inode->i_blocks));
1463 yaffs_gross_unlock(dev);
1464 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1467 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1468 /* For now we just assume few parallel writes and check against a small number. */
1469 /* Todo: need to do this with a counter to handle parallel reads better */
1471 static ssize_t yaffs_hold_space(struct file *f)
1473 struct yaffs_obj *obj;
1474 struct yaffs_dev *dev;
1478 obj = yaffs_dentry_to_obj(f->f_dentry);
1482 yaffs_gross_lock(dev);
1484 n_free_chunks = yaffs_get_n_free_chunks(dev);
1486 yaffs_gross_unlock(dev);
1488 return (n_free_chunks > 20) ? 1 : 0;
1491 static void yaffs_release_space(struct file *f)
1493 struct yaffs_obj *obj;
1494 struct yaffs_dev *dev;
1496 obj = yaffs_dentry_to_obj(f->f_dentry);
1500 yaffs_gross_lock(dev);
1502 yaffs_gross_unlock(dev);
1505 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1507 struct yaffs_obj *obj;
1508 struct yaffs_dev *dev;
1509 struct yaffs_search_context *sc;
1510 struct inode *inode = f->f_dentry->d_inode;
1511 unsigned long offset, curoffs;
1512 struct yaffs_obj *l;
1515 char name[YAFFS_MAX_NAME_LENGTH + 1];
1517 obj = yaffs_dentry_to_obj(f->f_dentry);
1520 yaffs_gross_lock(dev);
1522 yaffs_dev_to_lc(dev)->readdir_process = current;
1526 sc = yaffs_new_search(obj);
1532 yaffs_trace(YAFFS_TRACE_OS,
1533 "yaffs_readdir: starting at %d", (int)offset);
1536 yaffs_trace(YAFFS_TRACE_OS,
1537 "yaffs_readdir: entry . ino %d",
1539 yaffs_gross_unlock(dev);
1540 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1541 yaffs_gross_lock(dev);
1544 yaffs_gross_lock(dev);
1549 yaffs_trace(YAFFS_TRACE_OS,
1550 "yaffs_readdir: entry .. ino %d",
1551 (int)f->f_dentry->d_parent->d_inode->i_ino);
1552 yaffs_gross_unlock(dev);
1553 if (filldir(dirent, "..", 2, offset,
1554 f->f_dentry->d_parent->d_inode->i_ino,
1556 yaffs_gross_lock(dev);
1559 yaffs_gross_lock(dev);
1566 /* If the directory has changed since the open or last call to
1567 readdir, rewind to after the 2 canned entries. */
1568 if (f->f_version != inode->i_version) {
1571 f->f_version = inode->i_version;
1574 while (sc->next_return) {
1576 l = sc->next_return;
1577 if (curoffs >= offset) {
1578 int this_inode = yaffs_get_obj_inode(l);
1579 int this_type = yaffs_get_obj_type(l);
1581 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1582 yaffs_trace(YAFFS_TRACE_OS,
1583 "yaffs_readdir: %s inode %d",
1584 name, yaffs_get_obj_inode(l));
1586 yaffs_gross_unlock(dev);
1591 offset, this_inode, this_type) < 0) {
1592 yaffs_gross_lock(dev);
1596 yaffs_gross_lock(dev);
1601 yaffs_search_advance(sc);
1605 yaffs_search_end(sc);
1606 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1607 yaffs_gross_unlock(dev);
1613 * File creation. Allocate an inode, and we're done..
1616 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1619 #define YCRED(x) (x->cred)
1622 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1623 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1626 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1630 struct inode *inode;
1632 struct yaffs_obj *obj = NULL;
1633 struct yaffs_dev *dev;
1635 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1637 int error = -ENOSPC;
1638 uid_t uid = YCRED(current)->fsuid;
1640 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1642 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1646 yaffs_trace(YAFFS_TRACE_OS,
1647 "yaffs_mknod: parent object %d type %d",
1648 parent->obj_id, parent->variant_type);
1650 yaffs_trace(YAFFS_TRACE_OS,
1651 "yaffs_mknod: could not get parent object");
1655 yaffs_trace(YAFFS_TRACE_OS,
1656 "yaffs_mknod: making oject for %s, mode %x dev %x",
1657 dentry->d_name.name, mode, rdev);
1659 dev = parent->my_dev;
1661 yaffs_gross_lock(dev);
1663 switch (mode & S_IFMT) {
1665 /* Special (socket, fifo, device...) */
1666 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
1667 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1669 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1670 gid, old_encode_dev(rdev));
1673 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1677 case S_IFREG: /* file */
1678 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
1679 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1682 case S_IFDIR: /* directory */
1683 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
1684 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1687 case S_IFLNK: /* symlink */
1688 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
1689 obj = NULL; /* Do we ever get here? */
1693 /* Can not call yaffs_get_inode() with gross lock held */
1694 yaffs_gross_unlock(dev);
1697 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1698 d_instantiate(dentry, inode);
1699 update_dir_time(dir);
1700 yaffs_trace(YAFFS_TRACE_OS,
1701 "yaffs_mknod created object %d count = %d",
1702 obj->obj_id, atomic_read(&inode->i_count));
1704 yaffs_fill_inode_from_obj(dir, parent);
1706 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
1713 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1716 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
1717 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1721 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1722 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1723 struct nameidata *n)
1725 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1728 yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
1729 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1732 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1736 struct yaffs_dev *dev;
1737 struct yaffs_obj *obj;
1739 yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
1740 (int)(dir->i_ino), dentry->d_name.name);
1741 obj = yaffs_inode_to_obj(dir);
1744 yaffs_gross_lock(dev);
1746 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1748 if (ret_val == YAFFS_OK) {
1749 inode_dec_link_count(dentry->d_inode);
1751 yaffs_gross_unlock(dev);
1752 update_dir_time(dir);
1755 yaffs_gross_unlock(dev);
1762 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1763 struct dentry *dentry)
1765 struct inode *inode = old_dentry->d_inode;
1766 struct yaffs_obj *obj = NULL;
1767 struct yaffs_obj *link = NULL;
1768 struct yaffs_dev *dev;
1770 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
1772 obj = yaffs_inode_to_obj(inode);
1775 yaffs_gross_lock(dev);
1777 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1779 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1783 set_nlink(old_dentry->d_inode, yaffs_get_obj_link_count(obj));
1784 d_instantiate(dentry, old_dentry->d_inode);
1785 atomic_inc(&old_dentry->d_inode->i_count);
1786 yaffs_trace(YAFFS_TRACE_OS,
1787 "yaffs_link link count %d i_count %d",
1788 old_dentry->d_inode->i_nlink,
1789 atomic_read(&old_dentry->d_inode->i_count));
1792 yaffs_gross_unlock(dev);
1795 update_dir_time(dir);
1802 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1803 const char *symname)
1805 struct yaffs_obj *obj;
1806 struct yaffs_dev *dev;
1807 uid_t uid = YCRED(current)->fsuid;
1809 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1811 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
1813 if (strnlen(dentry->d_name.name, YAFFS_MAX_NAME_LENGTH + 1) >
1814 YAFFS_MAX_NAME_LENGTH)
1815 return -ENAMETOOLONG;
1817 if (strnlen(symname, YAFFS_MAX_ALIAS_LENGTH + 1) >
1818 YAFFS_MAX_ALIAS_LENGTH)
1819 return -ENAMETOOLONG;
1821 dev = yaffs_inode_to_obj(dir)->my_dev;
1822 yaffs_gross_lock(dev);
1823 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1824 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1825 yaffs_gross_unlock(dev);
1828 struct inode *inode;
1830 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1831 d_instantiate(dentry, inode);
1832 update_dir_time(dir);
1833 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
1836 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
1842 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
1843 static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync)
1844 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1845 static int yaffs_sync_object(struct file *file, int datasync)
1847 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1852 struct yaffs_obj *obj;
1853 struct yaffs_dev *dev;
1854 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1855 struct dentry *dentry = file->f_path.dentry;
1858 obj = yaffs_dentry_to_obj(dentry);
1862 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1863 "yaffs_sync_object");
1864 yaffs_gross_lock(dev);
1865 yaffs_flush_file(obj, 1, datasync);
1866 yaffs_gross_unlock(dev);
1871 * The VFS layer already does all the dentry stuff for rename.
1873 * NB: POSIX says you can rename an object over an old object of the same name
1875 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1876 struct inode *new_dir, struct dentry *new_dentry)
1878 struct yaffs_dev *dev;
1879 int ret_val = YAFFS_FAIL;
1880 struct yaffs_obj *target;
1882 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
1883 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1885 yaffs_gross_lock(dev);
1887 /* Check if the target is an existing directory that is not empty. */
1888 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1889 new_dentry->d_name.name);
1891 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1892 !list_empty(&target->variant.dir_variant.children)) {
1894 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
1896 ret_val = YAFFS_FAIL;
1898 /* Now does unlinking internally using shadowing mechanism */
1899 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
1901 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1902 old_dentry->d_name.name,
1903 yaffs_inode_to_obj(new_dir),
1904 new_dentry->d_name.name);
1906 yaffs_gross_unlock(dev);
1908 if (ret_val == YAFFS_OK) {
1910 inode_dec_link_count(new_dentry->d_inode);
1912 update_dir_time(old_dir);
1913 if (old_dir != new_dir)
1914 update_dir_time(new_dir);
1921 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1923 struct inode *inode = dentry->d_inode;
1925 struct yaffs_dev *dev;
1927 yaffs_trace(YAFFS_TRACE_OS,
1928 "yaffs_setattr of object %d",
1929 yaffs_inode_to_obj(inode)->obj_id);
1931 /* Fail if a requested resize >= 2GB */
1932 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
1937 error = inode_change_ok(inode, attr);
1941 error = yaffs_vfs_setattr(inode, attr);
1942 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
1943 if (attr->ia_valid & ATTR_SIZE) {
1944 yaffs_vfs_setsize(inode, attr->ia_size);
1945 inode->i_blocks = (inode->i_size + 511) >> 9;
1948 dev = yaffs_inode_to_obj(inode)->my_dev;
1949 if (attr->ia_valid & ATTR_SIZE) {
1950 yaffs_trace(YAFFS_TRACE_OS,
1952 (int)(attr->ia_size),
1953 (int)(attr->ia_size));
1955 yaffs_gross_lock(dev);
1956 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
1957 if (result == YAFFS_OK) {
1962 yaffs_gross_unlock(dev);
1966 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
1971 static int yaffs_setxattr(struct dentry *dentry, const char *name,
1972 const void *value, size_t size, int flags)
1974 struct inode *inode = dentry->d_inode;
1976 struct yaffs_dev *dev;
1977 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1979 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
1984 yaffs_gross_lock(dev);
1985 result = yaffs_set_xattrib(obj, name, value, size, flags);
1986 if (result == YAFFS_OK)
1988 else if (result < 0)
1990 yaffs_gross_unlock(dev);
1993 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
1998 static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
1999 void *buff, size_t size)
2001 struct inode *inode = dentry->d_inode;
2003 struct yaffs_dev *dev;
2004 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2006 yaffs_trace(YAFFS_TRACE_OS,
2007 "yaffs_getxattr \"%s\" from object %d",
2012 yaffs_gross_lock(dev);
2013 error = yaffs_get_xattrib(obj, name, buff, size);
2014 yaffs_gross_unlock(dev);
2017 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
2022 static int yaffs_removexattr(struct dentry *dentry, const char *name)
2024 struct inode *inode = dentry->d_inode;
2026 struct yaffs_dev *dev;
2027 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2029 yaffs_trace(YAFFS_TRACE_OS,
2030 "yaffs_removexattr of object %d", obj->obj_id);
2035 yaffs_gross_lock(dev);
2036 result = yaffs_remove_xattrib(obj, name);
2037 if (result == YAFFS_OK)
2039 else if (result < 0)
2041 yaffs_gross_unlock(dev);
2044 yaffs_trace(YAFFS_TRACE_OS,
2045 "yaffs_removexattr done returning %d", error);
2050 static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
2052 struct inode *inode = dentry->d_inode;
2054 struct yaffs_dev *dev;
2055 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2057 yaffs_trace(YAFFS_TRACE_OS,
2058 "yaffs_listxattr of object %d", obj->obj_id);
2062 yaffs_gross_lock(dev);
2063 error = yaffs_list_xattrib(obj, buff, size);
2064 yaffs_gross_unlock(dev);
2067 yaffs_trace(YAFFS_TRACE_OS,
2068 "yaffs_listxattr done returning %d", error);
2074 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2075 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2077 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2078 struct super_block *sb = dentry->d_sb;
2079 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2080 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2082 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2084 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2086 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2089 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
2091 yaffs_gross_lock(dev);
2093 buf->f_type = YAFFS_MAGIC;
2094 buf->f_bsize = sb->s_blocksize;
2095 buf->f_namelen = 255;
2097 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2098 /* Do this if chunk size is not a power of 2 */
2100 uint64_t bytes_in_dev;
2101 uint64_t bytes_free;
2105 ((dev->param.end_block - dev->param.start_block +
2106 1))) * ((uint64_t) (dev->param.chunks_per_block *
2107 dev->data_bytes_per_chunk));
2109 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2110 buf->f_blocks = bytes_in_dev;
2112 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
2113 ((uint64_t) (dev->data_bytes_per_chunk));
2115 do_div(bytes_free, sb->s_blocksize);
2117 buf->f_bfree = bytes_free;
2119 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2122 (dev->param.end_block - dev->param.start_block + 1) *
2123 dev->param.chunks_per_block /
2124 (sb->s_blocksize / dev->data_bytes_per_chunk);
2126 yaffs_get_n_free_chunks(dev) /
2127 (sb->s_blocksize / dev->data_bytes_per_chunk);
2130 (dev->param.end_block - dev->param.start_block + 1) *
2131 dev->param.chunks_per_block *
2132 (dev->data_bytes_per_chunk / sb->s_blocksize);
2135 yaffs_get_n_free_chunks(dev) *
2136 (dev->data_bytes_per_chunk / sb->s_blocksize);
2141 buf->f_bavail = buf->f_bfree;
2143 yaffs_gross_unlock(dev);
2147 static void yaffs_flush_inodes(struct super_block *sb)
2150 struct yaffs_obj *obj;
2152 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
2153 obj = yaffs_inode_to_obj(iptr);
2155 yaffs_trace(YAFFS_TRACE_OS,
2158 yaffs_flush_file(obj, 1, 0);
2163 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2165 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2169 yaffs_flush_inodes(sb);
2170 yaffs_update_dirty_dirs(dev);
2171 yaffs_flush_whole_cache(dev);
2173 yaffs_checkpoint_save(dev);
2176 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2178 unsigned erased_chunks =
2179 dev->n_erased_blocks * dev->param.chunks_per_block;
2180 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2181 unsigned scattered = 0; /* Free chunks not in an erased block */
2183 if (erased_chunks < dev->n_free_chunks)
2184 scattered = (dev->n_free_chunks - erased_chunks);
2186 if (!context->bg_running)
2188 else if (scattered < (dev->param.chunks_per_block * 2))
2190 else if (erased_chunks > dev->n_free_chunks / 2)
2192 else if (erased_chunks > dev->n_free_chunks / 4)
2198 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
2201 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2202 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2203 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2206 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2207 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
2209 sb->s_dirt ? "dirty" : "clean",
2210 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2211 oneshot_checkpoint ? " one-shot" : "");
2213 yaffs_gross_lock(dev);
2214 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2215 oneshot_checkpoint) && !dev->is_checkpointed;
2217 if (sb->s_dirt || do_checkpoint) {
2218 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2220 if (oneshot_checkpoint)
2221 yaffs_auto_checkpoint &= ~4;
2223 yaffs_gross_unlock(dev);
2229 * yaffs background thread functions .
2230 * yaffs_bg_thread_fn() the thread function
2231 * yaffs_bg_start() launches the background thread.
2232 * yaffs_bg_stop() cleans up the background thread.
2235 * The thread should only run after the yaffs is initialised
2236 * The thread should be stopped before yaffs is unmounted.
2237 * The thread should not do any writing while the fs is in read only.
2240 #ifdef YAFFS_COMPILE_BACKGROUND
2242 void yaffs_background_waker(unsigned long data)
2244 wake_up_process((struct task_struct *)data);
2247 static int yaffs_bg_thread_fn(void *data)
2249 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2250 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2251 unsigned long now = jiffies;
2252 unsigned long next_dir_update = now;
2253 unsigned long next_gc = now;
2254 unsigned long expires;
2255 unsigned int urgency;
2258 struct timer_list timer;
2260 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2261 "yaffs_background starting for dev %p", (void *)dev);
2263 #ifdef YAFFS_COMPILE_FREEZER
2266 while (context->bg_running) {
2267 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
2269 if (kthread_should_stop())
2272 #ifdef YAFFS_COMPILE_FREEZER
2273 if (try_to_freeze())
2276 yaffs_gross_lock(dev);
2280 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
2281 yaffs_update_dirty_dirs(dev);
2282 next_dir_update = now + HZ;
2285 if (time_after(now, next_gc) && yaffs_bg_enable) {
2286 if (!dev->is_checkpointed) {
2287 urgency = yaffs_bg_gc_urgency(dev);
2288 gc_result = yaffs_bg_gc(dev, urgency);
2290 next_gc = now + HZ / 20 + 1;
2291 else if (urgency > 0)
2292 next_gc = now + HZ / 10 + 1;
2294 next_gc = now + HZ * 2;
2297 * gc not running so set to next_dir_update
2298 * to cut down on wake ups
2300 next_gc = next_dir_update;
2303 yaffs_gross_unlock(dev);
2305 expires = next_dir_update;
2306 if (time_before(next_gc, expires))
2308 if (time_before(expires, now))
2311 Y_INIT_TIMER(&timer);
2312 timer.expires = expires + 1;
2313 timer.data = (unsigned long)current;
2314 timer.function = yaffs_background_waker;
2316 set_current_state(TASK_INTERRUPTIBLE);
2319 del_timer_sync(&timer);
2328 static int yaffs_bg_start(struct yaffs_dev *dev)
2331 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2336 context->bg_running = 1;
2338 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2339 (void *)dev, "yaffs-bg-%d",
2342 if (IS_ERR(context->bg_thread)) {
2343 retval = PTR_ERR(context->bg_thread);
2344 context->bg_thread = NULL;
2345 context->bg_running = 0;
2350 static void yaffs_bg_stop(struct yaffs_dev *dev)
2352 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2354 ctxt->bg_running = 0;
2356 if (ctxt->bg_thread) {
2357 kthread_stop(ctxt->bg_thread);
2358 ctxt->bg_thread = NULL;
2362 static int yaffs_bg_thread_fn(void *data)
2367 static int yaffs_bg_start(struct yaffs_dev *dev)
2372 static void yaffs_bg_stop(struct yaffs_dev *dev)
2377 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2378 static void yaffs_write_super(struct super_block *sb)
2380 static int yaffs_write_super(struct super_block *sb)
2383 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2385 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2386 "yaffs_write_super %s",
2387 request_checkpoint ? " checkpt" : "");
2389 yaffs_do_sync_fs(sb, request_checkpoint);
2391 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2396 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2397 static int yaffs_sync_fs(struct super_block *sb, int wait)
2399 static int yaffs_sync_fs(struct super_block *sb)
2402 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2404 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2405 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
2407 yaffs_do_sync_fs(sb, request_checkpoint);
2412 #ifdef YAFFS_USE_OWN_IGET
2414 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2416 struct inode *inode;
2417 struct yaffs_obj *obj;
2418 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2420 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
2422 inode = iget_locked(sb, ino);
2424 return ERR_PTR(-ENOMEM);
2425 if (!(inode->i_state & I_NEW))
2428 /* NB This is called as a side effect of other functions, but
2429 * we had to release the lock to prevent deadlocks, so
2430 * need to lock again.
2433 yaffs_gross_lock(dev);
2435 obj = yaffs_find_by_number(dev, inode->i_ino);
2437 yaffs_fill_inode_from_obj(inode, obj);
2439 yaffs_gross_unlock(dev);
2441 unlock_new_inode(inode);
2447 static void yaffs_read_inode(struct inode *inode)
2449 /* NB This is called as a side effect of other functions, but
2450 * we had to release the lock to prevent deadlocks, so
2451 * need to lock again.
2454 struct yaffs_obj *obj;
2455 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
2457 yaffs_trace(YAFFS_TRACE_OS,
2458 "yaffs_read_inode for %d", (int)inode->i_ino);
2460 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2461 yaffs_gross_lock(dev);
2463 obj = yaffs_find_by_number(dev, inode->i_ino);
2465 yaffs_fill_inode_from_obj(inode, obj);
2467 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2468 yaffs_gross_unlock(dev);
2473 static LIST_HEAD(yaffs_context_list);
2474 struct mutex yaffs_context_lock;
2476 static void yaffs_put_super(struct super_block *sb)
2478 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2480 yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
2482 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2483 "Shutting down yaffs background thread");
2485 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2486 "yaffs background thread shut down");
2488 yaffs_gross_lock(dev);
2490 yaffs_flush_super(sb, 1);
2492 if (yaffs_dev_to_lc(dev)->put_super_fn)
2493 yaffs_dev_to_lc(dev)->put_super_fn(sb);
2495 yaffs_deinitialise(dev);
2497 yaffs_gross_unlock(dev);
2499 mutex_lock(&yaffs_context_lock);
2500 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2501 mutex_unlock(&yaffs_context_lock);
2503 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2504 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
2505 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2511 static void yaffs_mtd_put_super(struct super_block *sb)
2513 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
2518 put_mtd_device(mtd);
2521 static void yaffs_touch_super(struct yaffs_dev *dev)
2523 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
2525 yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb);
2530 struct yaffs_options {
2532 int skip_checkpoint_read;
2533 int skip_checkpoint_write;
2536 int tags_ecc_overridden;
2537 int lazy_loading_enabled;
2538 int lazy_loading_overridden;
2539 int empty_lost_and_found;
2540 int empty_lost_and_found_overridden;
2541 int disable_summary;
2544 #define MAX_OPT_LEN 30
2545 static int yaffs_parse_options(struct yaffs_options *options,
2546 const char *options_str)
2548 char cur_opt[MAX_OPT_LEN + 1];
2552 /* Parse through the options which is a comma seperated list */
2554 while (options_str && *options_str && !error) {
2555 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2558 while (*options_str == ',')
2561 while (*options_str && *options_str != ',') {
2562 if (p < MAX_OPT_LEN) {
2563 cur_opt[p] = *options_str;
2569 if (!strcmp(cur_opt, "inband-tags")) {
2570 options->inband_tags = 1;
2571 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2572 options->tags_ecc_on = 0;
2573 options->tags_ecc_overridden = 1;
2574 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2575 options->tags_ecc_on = 1;
2576 options->tags_ecc_overridden = 1;
2577 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2578 options->lazy_loading_enabled = 0;
2579 options->lazy_loading_overridden = 1;
2580 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2581 options->lazy_loading_enabled = 1;
2582 options->lazy_loading_overridden = 1;
2583 } else if (!strcmp(cur_opt, "disable-summary")) {
2584 options->disable_summary = 1;
2585 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2586 options->empty_lost_and_found = 0;
2587 options->empty_lost_and_found_overridden = 1;
2588 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2589 options->empty_lost_and_found = 1;
2590 options->empty_lost_and_found_overridden = 1;
2591 } else if (!strcmp(cur_opt, "no-cache")) {
2592 options->no_cache = 1;
2593 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2594 options->skip_checkpoint_read = 1;
2595 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2596 options->skip_checkpoint_write = 1;
2597 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2598 options->skip_checkpoint_read = 1;
2599 options->skip_checkpoint_write = 1;
2601 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2610 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2611 struct super_block *sb,
2612 void *data, int silent)
2615 struct inode *inode = NULL;
2616 struct dentry *root;
2617 struct yaffs_dev *dev = 0;
2618 char devname_buf[BDEVNAME_SIZE + 1];
2619 struct mtd_info *mtd;
2621 char *data_str = (char *)data;
2622 struct yaffs_linux_context *context = NULL;
2623 struct yaffs_param *param;
2627 struct yaffs_options options;
2631 struct yaffs_linux_context *context_iterator;
2632 struct list_head *l;
2635 printk(KERN_INFO "yaffs: sb is NULL\n");
2639 sb->s_magic = YAFFS_MAGIC;
2640 sb->s_op = &yaffs_super_ops;
2641 sb->s_flags |= MS_NOATIME;
2643 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2645 #ifdef YAFFS_COMPILE_EXPORTFS
2646 sb->s_export_op = &yaffs_export_ops;
2650 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2651 else if (!yaffs_devname(sb, devname_buf))
2652 printk(KERN_INFO "yaffs: devname is NULL\n");
2654 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2656 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2661 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2663 memset(&options, 0, sizeof(options));
2665 if (yaffs_parse_options(&options, data_str)) {
2666 /* Option parsing failed */
2670 sb->s_blocksize = PAGE_CACHE_SIZE;
2671 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2673 yaffs_trace(YAFFS_TRACE_OS,
2674 "yaffs_read_super: Using yaffs%d", yaffs_version);
2675 yaffs_trace(YAFFS_TRACE_OS,
2676 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2678 yaffs_trace(YAFFS_TRACE_ALWAYS,
2679 "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
2680 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2681 yaffs_devname(sb, devname_buf));
2683 /* Check it's an mtd device..... */
2684 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2685 return NULL; /* This isn't an mtd device */
2687 /* Get the device */
2688 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2690 yaffs_trace(YAFFS_TRACE_ALWAYS,
2691 "yaffs: MTD device #%u doesn't appear to exist",
2695 /* Check it's NAND */
2696 if (mtd->type != MTD_NANDFLASH) {
2697 yaffs_trace(YAFFS_TRACE_ALWAYS,
2698 "yaffs: MTD device is not NAND it's type %d",
2703 yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase);
2704 yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read);
2705 yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write);
2706 yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob);
2707 yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob);
2708 yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad);
2709 yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad);
2710 yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
2711 yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
2712 yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
2713 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2714 yaffs_trace(YAFFS_TRACE_OS, " size %u", mtd->size);
2716 yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
2719 if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2720 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2724 /* Added NCB 26/5/2006 for completeness */
2725 if (yaffs_version == 2 && !options.inband_tags
2726 && WRITE_SIZE(mtd) == 512) {
2727 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2731 if (yaffs_version == 2) {
2732 /* Check for version 2 style functions */
2734 !mtd->block_isbad ||
2735 !mtd->block_markbad || !mtd->read || !mtd->write ||
2736 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2737 !mtd->read_oob || !mtd->write_oob) {
2740 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2742 yaffs_trace(YAFFS_TRACE_ALWAYS,
2743 "MTD device does not support required functions"
2748 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2749 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2750 !options.inband_tags) {
2751 yaffs_trace(YAFFS_TRACE_ALWAYS,
2752 "MTD device does not have the right page sizes"
2757 /* Check for V1 style functions */
2758 if (!mtd->erase || !mtd->read || !mtd->write ||
2759 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2760 !mtd->read_oob || !mtd->write_oob) {
2763 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2765 yaffs_trace(YAFFS_TRACE_ALWAYS,
2766 "MTD device does not support required functions"
2771 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2772 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2773 yaffs_trace(YAFFS_TRACE_ALWAYS,
2774 "MTD device does not support have the right page sizes"
2780 /* OK, so if we got here, we have an MTD that's NAND and looks
2781 * like it has the right capabilities
2782 * Set the struct yaffs_dev up for mtd
2785 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2788 "yaffs: mtd is read only, setting superblock read only\n"
2790 sb->s_flags |= MS_RDONLY;
2793 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2794 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2796 if (!dev || !context) {
2806 /* Deep shit could not allocate device structure */
2807 yaffs_trace(YAFFS_TRACE_ALWAYS,
2808 "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
2812 memset(dev, 0, sizeof(struct yaffs_dev));
2813 param = &(dev->param);
2815 memset(context, 0, sizeof(struct yaffs_linux_context));
2816 dev->os_context = context;
2817 INIT_LIST_HEAD(&(context->context_list));
2819 context->super = sb;
2821 dev->read_only = read_only;
2823 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2824 sb->s_fs_info = dev;
2826 sb->u.generic_sbp = dev;
2830 dev->driver_context = mtd;
2831 param->name = mtd->name;
2833 /* Set up the memory size parameters.... */
2836 YCALCBLOCKS(mtd->size,
2837 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2839 param->start_block = 0;
2840 param->end_block = n_blocks - 1;
2841 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2842 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2843 param->n_reserved_blocks = 5;
2844 param->n_caches = (options.no_cache) ? 0 : 10;
2845 param->inband_tags = options.inband_tags;
2847 param->enable_xattr = 1;
2848 if (options.lazy_loading_overridden)
2849 param->disable_lazy_load = !options.lazy_loading_enabled;
2851 param->defered_dir_update = 1;
2853 if (options.tags_ecc_overridden)
2854 param->no_tags_ecc = !options.tags_ecc_on;
2856 param->empty_lost_n_found = 1;
2857 param->refresh_period = 500;
2858 param->disable_summary = options.disable_summary;
2860 if (options.empty_lost_and_found_overridden)
2861 param->empty_lost_n_found = options.empty_lost_and_found;
2863 /* ... and the functions. */
2864 if (yaffs_version == 2) {
2865 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2866 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2867 param->bad_block_fn = nandmtd2_mark_block_bad;
2868 param->query_block_fn = nandmtd2_query_block;
2869 yaffs_dev_to_lc(dev)->spare_buffer =
2870 kmalloc(mtd->oobsize, GFP_NOFS);
2871 param->is_yaffs2 = 1;
2872 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2873 param->total_bytes_per_chunk = mtd->writesize;
2874 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2876 param->total_bytes_per_chunk = mtd->oobblock;
2877 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
2879 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2881 param->start_block = 0;
2882 param->end_block = n_blocks - 1;
2884 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2885 /* use the MTD interface in yaffs_mtdif1.c */
2886 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2887 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2888 param->bad_block_fn = nandmtd1_mark_block_bad;
2889 param->query_block_fn = nandmtd1_query_block;
2891 param->write_chunk_fn = nandmtd_write_chunk;
2892 param->read_chunk_fn = nandmtd_read_chunk;
2894 param->is_yaffs2 = 0;
2896 /* ... and common functions */
2897 param->erase_fn = nandmtd_erase_block;
2898 param->initialise_flash_fn = nandmtd_initialise;
2900 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2902 param->sb_dirty_fn = yaffs_touch_super;
2903 param->gc_control = yaffs_gc_control_callback;
2905 yaffs_dev_to_lc(dev)->super = sb;
2907 param->use_nand_ecc = 1;
2909 param->skip_checkpt_rd = options.skip_checkpoint_read;
2910 param->skip_checkpt_wr = options.skip_checkpoint_write;
2912 mutex_lock(&yaffs_context_lock);
2913 /* Get a mount id */
2915 for (mount_id = 0; !found; mount_id++) {
2917 list_for_each(l, &yaffs_context_list) {
2919 list_entry(l, struct yaffs_linux_context,
2921 if (context_iterator->mount_id == mount_id)
2925 context->mount_id = mount_id;
2927 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2928 &yaffs_context_list);
2929 mutex_unlock(&yaffs_context_lock);
2931 /* Directory search handling... */
2932 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2933 param->remove_obj_fn = yaffs_remove_obj_callback;
2935 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2937 yaffs_gross_lock(dev);
2939 err = yaffs_guts_initialise(dev);
2941 yaffs_trace(YAFFS_TRACE_OS,
2942 "yaffs_read_super: guts initialised %s",
2943 (err == YAFFS_OK) ? "OK" : "FAILED");
2945 if (err == YAFFS_OK)
2946 yaffs_bg_start(dev);
2948 if (!context->bg_thread)
2949 param->defered_dir_update = 0;
2951 sb->s_maxbytes = yaffs_max_file_size(dev);
2953 /* Release lock before yaffs_get_inode() */
2954 yaffs_gross_unlock(dev);
2956 /* Create root inode */
2957 if (err == YAFFS_OK)
2958 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2963 inode->i_op = &yaffs_dir_inode_operations;
2964 inode->i_fop = &yaffs_dir_operations;
2966 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2968 root = d_alloc_root(inode);
2970 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
2977 sb->s_dirt = !dev->is_checkpointed;
2978 yaffs_trace(YAFFS_TRACE_ALWAYS,
2979 "yaffs_read_super: is_checkpointed %d",
2980 dev->is_checkpointed);
2982 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
2986 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2987 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2990 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2993 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
2994 static struct dentry *yaffs_mount(struct file_system_type *fs_type, int flags,
2995 const char *dev_name, void *data)
2997 return mount_bdev(fs_type, flags, dev_name, data, yaffs_internal_read_super_mtd);
2999 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3000 static int yaffs_read_super(struct file_system_type *fs,
3001 int flags, const char *dev_name,
3002 void *data, struct vfsmount *mnt)
3005 return get_sb_bdev(fs, flags, dev_name, data,
3006 yaffs_internal_read_super_mtd, mnt);
3009 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3010 int flags, const char *dev_name,
3014 return get_sb_bdev(fs, flags, dev_name, data,
3015 yaffs_internal_read_super_mtd);
3019 static struct file_system_type yaffs_fs_type = {
3020 .owner = THIS_MODULE,
3022 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3023 .mount = yaffs_mount,
3025 .get_sb = yaffs_read_super,
3027 .kill_sb = kill_block_super,
3028 .fs_flags = FS_REQUIRES_DEV,
3031 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3034 return yaffs_internal_read_super(1, sb, data, silent);
3037 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3042 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3043 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3046 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3049 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3050 static struct dentry *yaffs2_mount(struct file_system_type *fs_type, int flags,
3051 const char *dev_name, void *data)
3053 return mount_bdev(fs_type, flags, dev_name, data, yaffs2_internal_read_super_mtd);
3055 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3056 static int yaffs2_read_super(struct file_system_type *fs,
3057 int flags, const char *dev_name, void *data,
3058 struct vfsmount *mnt)
3060 return get_sb_bdev(fs, flags, dev_name, data,
3061 yaffs2_internal_read_super_mtd, mnt);
3064 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3065 int flags, const char *dev_name,
3069 return get_sb_bdev(fs, flags, dev_name, data,
3070 yaffs2_internal_read_super_mtd);
3074 static struct file_system_type yaffs2_fs_type = {
3075 .owner = THIS_MODULE,
3077 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3078 .mount = yaffs2_mount,
3080 .get_sb = yaffs2_read_super,
3082 .kill_sb = kill_block_super,
3083 .fs_flags = FS_REQUIRES_DEV,
3086 static struct super_block *yaffs2_read_super(struct super_block *sb,
3087 void *data, int silent)
3089 return yaffs_internal_read_super(2, sb, data, silent);
3092 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3097 static struct proc_dir_entry *my_proc_entry;
3099 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3101 struct yaffs_param *param = &dev->param;
3103 buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
3104 buf += sprintf(buf, "end_block............ %d\n", param->end_block);
3105 buf += sprintf(buf, "total_bytes_per_chunk %d\n",
3106 param->total_bytes_per_chunk);
3107 buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
3108 buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
3109 buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
3110 buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
3111 buf += sprintf(buf, "empty_lost_n_found... %d\n",
3112 param->empty_lost_n_found);
3113 buf += sprintf(buf, "disable_lazy_load.... %d\n",
3114 param->disable_lazy_load);
3115 buf += sprintf(buf, "refresh_period....... %d\n",
3116 param->refresh_period);
3117 buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
3118 buf += sprintf(buf, "n_reserved_blocks.... %d\n",
3119 param->n_reserved_blocks);
3120 buf += sprintf(buf, "always_check_erased.. %d\n",
3121 param->always_check_erased);
3122 buf += sprintf(buf, "\n");
3127 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3129 buf += sprintf(buf, "max file size....... %lld\n",
3130 (long long) yaffs_max_file_size(dev));
3131 buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
3132 dev->data_bytes_per_chunk);
3133 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3134 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3135 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3136 buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
3137 dev->blocks_in_checkpt);
3138 buf += sprintf(buf, "\n");
3139 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3140 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3141 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3142 buf += sprintf(buf, "\n");
3143 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3144 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3145 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3146 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3147 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3148 buf += sprintf(buf, "passive_gc_count..... %u\n",
3149 dev->passive_gc_count);
3150 buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
3151 dev->oldest_dirty_gc_count);
3152 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3153 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3154 buf += sprintf(buf, "n_retried_writes..... %u\n",
3155 dev->n_retried_writes);
3156 buf += sprintf(buf, "n_retired_blocks..... %u\n",
3157 dev->n_retired_blocks);
3158 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3159 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3160 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
3161 dev->n_tags_ecc_fixed);
3162 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
3163 dev->n_tags_ecc_unfixed);
3164 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3165 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3166 buf += sprintf(buf, "n_unlinked_files..... %u\n",
3167 dev->n_unlinked_files);
3168 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3169 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3170 buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
3171 buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
3176 static int yaffs_proc_read(char *page,
3178 off_t offset, int count, int *eof, void *data)
3180 struct list_head *item;
3185 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3186 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3187 * This also assumes the user has posted a read buffer large
3188 * enough to hold the complete output; but that's life in /proc.
3193 /* Print header first */
3197 "Multi-version YAFFS built:" __DATE__ " " __TIME__
3200 buf += sprintf(buf, "\n");
3204 mutex_lock(&yaffs_context_lock);
3206 /* Locate and print the Nth entry. Order N-squared but N is small. */
3207 list_for_each(item, &yaffs_context_list) {
3208 struct yaffs_linux_context *dc =
3209 list_entry(item, struct yaffs_linux_context,
3211 struct yaffs_dev *dev = dc->dev;
3213 if (n < (step & ~1)) {
3217 if ((step & 1) == 0) {
3219 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3221 buf = yaffs_dump_dev_part0(buf, dev);
3223 buf = yaffs_dump_dev_part1(buf, dev);
3228 mutex_unlock(&yaffs_context_lock);
3231 return buf - page < count ? buf - page : count;
3235 * Set the verbosity of the warnings and error messages.
3237 * Note that the names can only be a..z or _ with the current code.
3242 unsigned mask_bitfield;
3244 {"allocate", YAFFS_TRACE_ALLOCATE},
3245 {"always", YAFFS_TRACE_ALWAYS},
3246 {"background", YAFFS_TRACE_BACKGROUND},
3247 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3248 {"buffers", YAFFS_TRACE_BUFFERS},
3249 {"bug", YAFFS_TRACE_BUG},
3250 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3251 {"deletion", YAFFS_TRACE_DELETION},
3252 {"erase", YAFFS_TRACE_ERASE},
3253 {"error", YAFFS_TRACE_ERROR},
3254 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3255 {"gc", YAFFS_TRACE_GC},
3256 {"lock", YAFFS_TRACE_LOCK},
3257 {"mtd", YAFFS_TRACE_MTD},
3258 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3259 {"os", YAFFS_TRACE_OS},
3260 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3261 {"scan", YAFFS_TRACE_SCAN},
3262 {"mount", YAFFS_TRACE_MOUNT},
3263 {"tracing", YAFFS_TRACE_TRACING},
3264 {"sync", YAFFS_TRACE_SYNC},
3265 {"write", YAFFS_TRACE_WRITE},
3266 {"verify", YAFFS_TRACE_VERIFY},
3267 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3268 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3269 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3270 {"all", 0xffffffff},
3275 #define MAX_MASK_NAME_LENGTH 40
3276 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3277 unsigned long count, void *data)
3279 unsigned rg = 0, mask_bitfield;
3283 char substring[MAX_MASK_NAME_LENGTH + 1];
3289 rg = yaffs_trace_mask;
3291 while (!done && (pos < count)) {
3293 while ((pos < count) && isspace(buf[pos]))
3310 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3312 if (end > buf + pos) {
3313 mask_name = "numeral";
3314 len = end - (buf + pos);
3318 for (x = buf + pos, i = 0;
3319 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3320 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3322 substring[i] = '\0';
3324 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3325 if (strcmp(substring, mask_flags[i].mask_name)
3327 mask_name = mask_flags[i].mask_name;
3329 mask_flags[i].mask_bitfield;
3336 if (mask_name != NULL) {
3340 rg &= ~mask_bitfield;
3343 rg |= mask_bitfield;
3349 rg |= mask_bitfield;
3355 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3357 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3359 if (rg & YAFFS_TRACE_ALWAYS) {
3360 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3362 flag = ((rg & mask_flags[i].mask_bitfield) ==
3363 mask_flags[i].mask_bitfield) ? '+' : '-';
3364 printk(KERN_DEBUG "%c%s\n", flag,
3365 mask_flags[i].mask_name);
3372 static int yaffs_proc_write(struct file *file, const char *buf,
3373 unsigned long count, void *data)
3375 return yaffs_proc_write_trace_options(file, buf, count, data);
3378 /* Stuff to handle installation of file systems */
3379 struct file_system_to_install {
3380 struct file_system_type *fst;
3384 static struct file_system_to_install fs_to_install[] = {
3385 {&yaffs_fs_type, 0},
3386 {&yaffs2_fs_type, 0},
3390 static int __init init_yaffs_fs(void)
3393 struct file_system_to_install *fsinst;
3395 yaffs_trace(YAFFS_TRACE_ALWAYS,
3396 "yaffs built " __DATE__ " " __TIME__ " Installing.");
3398 mutex_init(&yaffs_context_lock);
3400 /* Install the proc_fs entries */
3401 my_proc_entry = create_proc_entry("yaffs",
3402 S_IRUGO | S_IFREG, YPROC_ROOT);
3404 if (my_proc_entry) {
3405 my_proc_entry->write_proc = yaffs_proc_write;
3406 my_proc_entry->read_proc = yaffs_proc_read;
3407 my_proc_entry->data = NULL;
3412 /* Now add the file system entries */
3414 fsinst = fs_to_install;
3416 while (fsinst->fst && !error) {
3417 error = register_filesystem(fsinst->fst);
3419 fsinst->installed = 1;
3423 /* Any errors? uninstall */
3425 fsinst = fs_to_install;
3427 while (fsinst->fst) {
3428 if (fsinst->installed) {
3429 unregister_filesystem(fsinst->fst);
3430 fsinst->installed = 0;
3439 static void __exit exit_yaffs_fs(void)
3442 struct file_system_to_install *fsinst;
3444 yaffs_trace(YAFFS_TRACE_ALWAYS,
3445 "yaffs built " __DATE__ " " __TIME__ " removing.");
3447 remove_proc_entry("yaffs", YPROC_ROOT);
3449 fsinst = fs_to_install;
3451 while (fsinst->fst) {
3452 if (fsinst->installed) {
3453 unregister_filesystem(fsinst->fst);
3454 fsinst->installed = 0;
3460 module_init(init_yaffs_fs)
3461 module_exit(exit_yaffs_fs)
3463 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3464 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
3465 MODULE_LICENSE("GPL");