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"
180 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
181 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
182 unsigned int yaffs_auto_checkpoint = 1;
183 unsigned int yaffs_gc_control = 1;
184 unsigned int yaffs_bg_enable = 1;
185 unsigned int yaffs_auto_select = 1;
186 /* Module Parameters */
187 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
188 module_param(yaffs_trace_mask, uint, 0644);
189 module_param(yaffs_wr_attempts, uint, 0644);
190 module_param(yaffs_auto_checkpoint, uint, 0644);
191 module_param(yaffs_gc_control, uint, 0644);
192 module_param(yaffs_bg_enable, uint, 0644);
194 MODULE_PARM(yaffs_trace_mask, "i");
195 MODULE_PARM(yaffs_wr_attempts, "i");
196 MODULE_PARM(yaffs_auto_checkpoint, "i");
197 MODULE_PARM(yaffs_gc_control, "i");
200 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
201 /* use iget and read_inode */
202 #define Y_IGET(sb, inum) iget((sb), (inum))
203 static void yaffs_read_inode(struct inode *inode);
206 /* Call local equivalent */
207 #define YAFFS_USE_OWN_IGET
208 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
210 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
213 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
214 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
216 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
219 #define yaffs_inode_to_obj(iptr) \
220 ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
221 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
223 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
224 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
226 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
229 #define update_dir_time(dir) do {\
230 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
233 static void yaffs_put_super(struct super_block *sb);
235 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
237 static ssize_t yaffs_hold_space(struct file *f);
238 static void yaffs_release_space(struct file *f);
240 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
241 static int yaffs_file_flush(struct file *file, fl_owner_t id);
243 static int yaffs_file_flush(struct file *file);
246 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
247 static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync);
248 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
249 static int yaffs_sync_object(struct file *file, int datasync);
251 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
255 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
257 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
258 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
259 struct nameidata *n);
260 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
261 struct nameidata *n);
263 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
264 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
266 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
267 struct dentry *dentry);
268 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
269 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
270 const char *symname);
271 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
273 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
274 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
277 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
280 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
281 struct inode *new_dir, struct dentry *new_dentry);
282 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
284 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
285 static int yaffs_sync_fs(struct super_block *sb, int wait);
286 static void yaffs_write_super(struct super_block *sb);
288 static int yaffs_sync_fs(struct super_block *sb);
289 static int yaffs_write_super(struct super_block *sb);
292 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
293 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
294 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
295 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
297 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
300 #ifdef YAFFS_HAS_PUT_INODE
301 static void yaffs_put_inode(struct inode *inode);
304 #ifdef YAFFS_HAS_EVICT_INODE
305 static void yaffs_evict_inode(struct inode *);
307 static void yaffs_delete_inode(struct inode *);
308 static void yaffs_clear_inode(struct inode *);
311 static int yaffs_readpage(struct file *file, struct page *page);
312 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
313 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
315 static int yaffs_writepage(struct page *page);
318 static int yaffs_setxattr(struct dentry *dentry, const char *name,
319 const void *value, size_t size, int flags);
320 static ssize_t yaffs_getxattr(struct dentry *dentry, const char *name,
321 void *buff, size_t size);
322 static int yaffs_removexattr(struct dentry *dentry, const char *name);
323 static ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
325 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
326 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
327 loff_t pos, unsigned len, unsigned flags,
328 struct page **pagep, void **fsdata);
329 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
330 loff_t pos, unsigned len, unsigned copied,
331 struct page *pg, void *fsdadata);
333 static int yaffs_prepare_write(struct file *f, struct page *pg,
334 unsigned offset, unsigned to);
335 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
340 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
342 #if (YAFFS_NEW_FOLLOW_LINK == 1)
343 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
344 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
346 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
349 static void yaffs_touch_super(struct yaffs_dev *dev);
351 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
353 static struct address_space_operations yaffs_file_address_operations = {
354 .readpage = yaffs_readpage,
355 .writepage = yaffs_writepage,
356 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
357 .write_begin = yaffs_write_begin,
358 .write_end = yaffs_write_end,
360 .prepare_write = yaffs_prepare_write,
361 .commit_write = yaffs_commit_write,
365 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
366 static const struct file_operations yaffs_file_operations = {
367 .read = do_sync_read,
368 .write = do_sync_write,
369 .aio_read = generic_file_aio_read,
370 .aio_write = generic_file_aio_write,
371 .mmap = generic_file_mmap,
372 .flush = yaffs_file_flush,
373 .fsync = yaffs_sync_object,
374 .splice_read = generic_file_splice_read,
375 .splice_write = generic_file_splice_write,
376 .llseek = generic_file_llseek,
379 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
381 static const struct file_operations yaffs_file_operations = {
382 .read = do_sync_read,
383 .write = do_sync_write,
384 .aio_read = generic_file_aio_read,
385 .aio_write = generic_file_aio_write,
386 .mmap = generic_file_mmap,
387 .flush = yaffs_file_flush,
388 .fsync = yaffs_sync_object,
389 .sendfile = generic_file_sendfile,
394 static const struct file_operations yaffs_file_operations = {
395 .read = generic_file_read,
396 .write = generic_file_write,
397 .mmap = generic_file_mmap,
398 .flush = yaffs_file_flush,
399 .fsync = yaffs_sync_object,
400 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
401 .sendfile = generic_file_sendfile,
406 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
407 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
409 void *kaddr = kmap_atomic(page, KM_USER0);
410 memset(kaddr + start, 0, end - start);
411 kunmap_atomic(kaddr, KM_USER0);
412 flush_dcache_page(page);
416 static const struct inode_operations yaffs_file_inode_operations = {
417 .setattr = yaffs_setattr,
418 .setxattr = yaffs_setxattr,
419 .getxattr = yaffs_getxattr,
420 .listxattr = yaffs_listxattr,
421 .removexattr = yaffs_removexattr,
424 static const struct inode_operations yaffs_symlink_inode_operations = {
425 .readlink = yaffs_readlink,
426 .follow_link = yaffs_follow_link,
427 #if (YAFFS_NEW_FOLLOW_LINK == 1)
428 .put_link = yaffs_put_link,
430 .setattr = yaffs_setattr,
431 .setxattr = yaffs_setxattr,
432 .getxattr = yaffs_getxattr,
433 .listxattr = yaffs_listxattr,
434 .removexattr = yaffs_removexattr,
437 static const struct inode_operations yaffs_dir_inode_operations = {
438 .create = yaffs_create,
439 .lookup = yaffs_lookup,
441 .unlink = yaffs_unlink,
442 .symlink = yaffs_symlink,
443 .mkdir = yaffs_mkdir,
444 .rmdir = yaffs_unlink,
445 .mknod = yaffs_mknod,
446 .rename = yaffs_rename,
447 .setattr = yaffs_setattr,
448 .setxattr = yaffs_setxattr,
449 .getxattr = yaffs_getxattr,
450 .listxattr = yaffs_listxattr,
451 .removexattr = yaffs_removexattr,
454 static const struct file_operations yaffs_dir_operations = {
455 .read = generic_read_dir,
456 .readdir = yaffs_readdir,
457 .fsync = yaffs_sync_object,
458 .llseek = generic_file_llseek,
461 static const struct super_operations yaffs_super_ops = {
462 .statfs = yaffs_statfs,
464 #ifndef YAFFS_USE_OWN_IGET
465 .read_inode = yaffs_read_inode,
467 #ifdef YAFFS_HAS_PUT_INODE
468 .put_inode = yaffs_put_inode,
470 .put_super = yaffs_put_super,
471 #ifdef YAFFS_HAS_EVICT_INODE
472 .evict_inode = yaffs_evict_inode,
474 .delete_inode = yaffs_delete_inode,
475 .clear_inode = yaffs_clear_inode,
477 .sync_fs = yaffs_sync_fs,
478 .write_super = yaffs_write_super,
481 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
483 #ifdef YAFFS_USE_SETATTR_COPY
484 setattr_copy(inode, attr);
487 return inode_setattr(inode, attr);
492 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
494 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
495 truncate_setsize(inode, newsize);
498 truncate_inode_pages(&inode->i_data, newsize);
504 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
506 return yaffs_gc_control;
509 static void yaffs_gross_lock(struct yaffs_dev *dev)
511 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
512 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
513 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
516 static void yaffs_gross_unlock(struct yaffs_dev *dev)
518 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
519 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
522 #ifdef YAFFS_COMPILE_EXPORTFS
524 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
527 return Y_IGET(sb, ino);
530 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
531 struct fid *fid, int fh_len,
534 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
535 yaffs2_nfs_get_inode);
538 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
539 struct fid *fid, int fh_len,
542 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
543 yaffs2_nfs_get_inode);
546 struct dentry *yaffs2_get_parent(struct dentry *dentry)
549 struct super_block *sb = dentry->d_inode->i_sb;
550 struct dentry *parent = ERR_PTR(-ENOENT);
552 unsigned long parent_ino;
553 struct yaffs_obj *d_obj;
554 struct yaffs_obj *parent_obj;
556 d_obj = yaffs_inode_to_obj(dentry->d_inode);
559 parent_obj = d_obj->parent;
561 parent_ino = yaffs_get_obj_inode(parent_obj);
562 inode = Y_IGET(sb, parent_ino);
565 parent = ERR_CAST(inode);
567 parent = d_obtain_alias(inode);
568 if (!IS_ERR(parent)) {
569 parent = ERR_PTR(-ENOMEM);
579 /* Just declare a zero structure as a NULL value implies
580 * using the default functions of exportfs.
583 static struct export_operations yaffs_export_ops = {
584 .fh_to_dentry = yaffs2_fh_to_dentry,
585 .fh_to_parent = yaffs2_fh_to_parent,
586 .get_parent = yaffs2_get_parent,
591 /*-----------------------------------------------------------------*/
592 /* Directory search context allows us to unlock access to yaffs during
593 * filldir without causing problems with the directory being modified.
594 * This is similar to the tried and tested mechanism used in yaffs direct.
596 * A search context iterates along a doubly linked list of siblings in the
597 * directory. If the iterating object is deleted then this would corrupt
598 * the list iteration, likely causing a crash. The search context avoids
599 * this by using the remove_obj_fn to move the search context to the
600 * next object before the object is deleted.
602 * Many readdirs (and thus seach conexts) may be alive simulateously so
603 * each struct yaffs_dev has a list of these.
605 * A seach context lives for the duration of a readdir.
607 * All these functions must be called while yaffs is locked.
610 struct yaffs_search_context {
611 struct yaffs_dev *dev;
612 struct yaffs_obj *dir_obj;
613 struct yaffs_obj *next_return;
614 struct list_head others;
618 * yaffs_new_search() creates a new search context, initialises it and
619 * adds it to the device's search context list.
621 * Called at start of readdir.
623 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
625 struct yaffs_dev *dev = dir->my_dev;
626 struct yaffs_search_context *sc =
627 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
631 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
632 sc->next_return = NULL;
635 list_entry(dir->variant.dir_variant.children.next,
636 struct yaffs_obj, siblings);
637 INIT_LIST_HEAD(&sc->others);
638 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
644 * yaffs_search_end() disposes of a search context and cleans up.
646 static void yaffs_search_end(struct yaffs_search_context *sc)
649 list_del(&sc->others);
655 * yaffs_search_advance() moves a search context to the next object.
656 * Called when the search iterates or when an object removal causes
657 * the search context to be moved to the next object.
659 static void yaffs_search_advance(struct yaffs_search_context *sc)
664 if (sc->next_return == NULL ||
665 list_empty(&sc->dir_obj->variant.dir_variant.children))
666 sc->next_return = NULL;
668 struct list_head *next = sc->next_return->siblings.next;
670 if (next == &sc->dir_obj->variant.dir_variant.children)
671 sc->next_return = NULL; /* end of list */
674 list_entry(next, struct yaffs_obj, siblings);
679 * yaffs_remove_obj_callback() is called when an object is unlinked.
680 * We check open search contexts and advance any which are currently
681 * on the object being iterated.
683 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
687 struct yaffs_search_context *sc;
688 struct list_head *search_contexts =
689 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
691 /* Iterate through the directory search contexts.
692 * If any are currently on the object being removed, then advance
693 * the search context to the next object to prevent a hanging pointer.
695 list_for_each(i, search_contexts) {
696 sc = list_entry(i, struct yaffs_search_context, others);
697 if (sc->next_return == obj)
698 yaffs_search_advance(sc);
703 /*-----------------------------------------------------------------*/
705 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
708 unsigned char *alias;
711 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
713 yaffs_gross_lock(dev);
715 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
717 yaffs_gross_unlock(dev);
722 ret = vfs_readlink(dentry, buffer, buflen, alias);
727 #if (YAFFS_NEW_FOLLOW_LINK == 1)
728 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
732 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
736 unsigned char *alias;
738 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
740 yaffs_gross_lock(dev);
742 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
743 yaffs_gross_unlock(dev);
749 #if (YAFFS_NEW_FOLLOW_LINK == 1)
750 nd_set_link(nd, alias);
754 ret = ERR_PTR(ret_int);
757 ret = vfs_follow_link(nd, alias);
766 #if (YAFFS_NEW_FOLLOW_LINK == 1)
767 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
773 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
774 struct yaffs_obj *obj);
777 * Lookup is used to find objects in the fs
779 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
781 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
784 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
787 struct yaffs_obj *obj;
788 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
790 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
792 if (current != yaffs_dev_to_lc(dev)->readdir_process)
793 yaffs_gross_lock(dev);
795 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
796 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
798 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
800 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
802 /* Can't hold gross lock when calling yaffs_get_inode() */
803 if (current != yaffs_dev_to_lc(dev)->readdir_process)
804 yaffs_gross_unlock(dev);
807 yaffs_trace(YAFFS_TRACE_OS,
808 "yaffs_lookup found %d", obj->obj_id);
810 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
812 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
816 /* added NCB for 2.5/6 compatability - forces add even if inode is
817 * NULL which creates dentry hash */
818 d_add(dentry, inode);
823 #ifdef YAFFS_HAS_PUT_INODE
825 /* For now put inode is just for debugging
826 * Put inode is called when the inode **structure** is put.
828 static void yaffs_put_inode(struct inode *inode)
830 yaffs_trace(YAFFS_TRACE_OS,
831 "yaffs_put_inode: ino %d, count %d"),
832 (int)inode->i_ino, atomic_read(&inode->i_count);
837 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
839 /* Clear the association between the inode and
840 * the struct yaffs_obj.
842 obj->my_inode = NULL;
843 yaffs_inode_to_obj_lv(inode) = NULL;
845 /* If the object freeing was deferred, then the real
847 * This should fix the inode inconsistency problem.
849 yaffs_handle_defered_free(obj);
852 #ifdef YAFFS_HAS_EVICT_INODE
853 /* yaffs_evict_inode combines into one operation what was previously done in
854 * yaffs_clear_inode() and yaffs_delete_inode()
857 static void yaffs_evict_inode(struct inode *inode)
859 struct yaffs_obj *obj;
860 struct yaffs_dev *dev;
863 obj = yaffs_inode_to_obj(inode);
865 yaffs_trace(YAFFS_TRACE_OS,
866 "yaffs_evict_inode: ino %d, count %d %s",
867 (int)inode->i_ino, atomic_read(&inode->i_count),
868 obj ? "object exists" : "null object");
870 if (!inode->i_nlink && !is_bad_inode(inode))
872 truncate_inode_pages(&inode->i_data, 0);
873 end_writeback(inode);
875 if (deleteme && obj) {
877 yaffs_gross_lock(dev);
879 yaffs_gross_unlock(dev);
883 yaffs_gross_lock(dev);
884 yaffs_unstitch_obj(inode, obj);
885 yaffs_gross_unlock(dev);
890 /* clear is called to tell the fs to release any per-inode data it holds.
891 * The object might still exist on disk and is just being thrown out of the cache
892 * or else the object has actually been deleted and we're being called via
894 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
897 static void yaffs_clear_inode(struct inode *inode)
899 struct yaffs_obj *obj;
900 struct yaffs_dev *dev;
902 obj = yaffs_inode_to_obj(inode);
904 yaffs_trace(YAFFS_TRACE_OS,
905 "yaffs_clear_inode: ino %d, count %d %s",
906 (int)inode->i_ino, atomic_read(&inode->i_count),
907 obj ? "object exists" : "null object");
911 yaffs_gross_lock(dev);
912 yaffs_unstitch_obj(inode, obj);
913 yaffs_gross_unlock(dev);
918 /* delete is called when the link count is zero and the inode
919 * is put (ie. nobody wants to know about it anymore, time to
921 * NB Must call clear_inode()
923 static void yaffs_delete_inode(struct inode *inode)
925 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
926 struct yaffs_dev *dev;
928 yaffs_trace(YAFFS_TRACE_OS,
929 "yaffs_delete_inode: ino %d, count %d %s",
930 (int)inode->i_ino, atomic_read(&inode->i_count),
931 obj ? "object exists" : "null object");
935 yaffs_gross_lock(dev);
937 yaffs_gross_unlock(dev);
939 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
940 truncate_inode_pages(&inode->i_data, 0);
946 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
947 static int yaffs_file_flush(struct file *file, fl_owner_t id)
949 static int yaffs_file_flush(struct file *file)
952 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
954 struct yaffs_dev *dev = obj->my_dev;
956 yaffs_trace(YAFFS_TRACE_OS,
957 "yaffs_file_flush object %d (%s)",
959 obj->dirty ? "dirty" : "clean");
961 yaffs_gross_lock(dev);
963 yaffs_flush_file(obj, 1, 0);
965 yaffs_gross_unlock(dev);
970 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
972 /* Lifted from jffs2 */
974 struct yaffs_obj *obj;
975 unsigned char *pg_buf;
977 loff_t pos = ((loff_t) pg->index) << PAGE_CACHE_SHIFT;
978 struct yaffs_dev *dev;
980 yaffs_trace(YAFFS_TRACE_OS,
981 "yaffs_readpage_nolock at %lld, size %08x",
983 (unsigned)PAGE_CACHE_SIZE);
985 obj = yaffs_dentry_to_obj(f->f_dentry);
989 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
990 BUG_ON(!PageLocked(pg));
997 /* FIXME: Can kmap fail? */
999 yaffs_gross_lock(dev);
1001 ret = yaffs_file_rd(obj, pg_buf, pos, PAGE_CACHE_SIZE);
1003 yaffs_gross_unlock(dev);
1009 ClearPageUptodate(pg);
1012 SetPageUptodate(pg);
1016 flush_dcache_page(pg);
1019 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
1023 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1025 int ret = yaffs_readpage_nolock(f, pg);
1030 static int yaffs_readpage(struct file *f, struct page *pg)
1034 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
1035 ret = yaffs_readpage_unlock(f, pg);
1036 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
1040 /* writepage inspired by/stolen from smbfs */
1042 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1043 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1045 static int yaffs_writepage(struct page *page)
1048 struct yaffs_dev *dev;
1049 struct address_space *mapping = page->mapping;
1050 struct inode *inode;
1051 unsigned long end_index;
1053 struct yaffs_obj *obj;
1060 inode = mapping->host;
1063 i_size = i_size_read(inode);
1065 end_index = i_size >> PAGE_CACHE_SHIFT;
1067 if (page->index < end_index)
1068 n_bytes = PAGE_CACHE_SIZE;
1070 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
1072 if (page->index > end_index || !n_bytes) {
1073 yaffs_trace(YAFFS_TRACE_OS,
1074 "yaffs_writepage at %lld, inode size = %lld!!",
1075 ((loff_t)page->index) << PAGE_CACHE_SHIFT,
1077 yaffs_trace(YAFFS_TRACE_OS,
1078 " -> don't care!!");
1080 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1081 set_page_writeback(page);
1083 end_page_writeback(page);
1088 if (n_bytes != PAGE_CACHE_SIZE)
1089 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
1093 buffer = kmap(page);
1095 obj = yaffs_inode_to_obj(inode);
1097 yaffs_gross_lock(dev);
1099 yaffs_trace(YAFFS_TRACE_OS,
1100 "yaffs_writepage at %lld, size %08x",
1101 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes);
1102 yaffs_trace(YAFFS_TRACE_OS,
1103 "writepag0: obj = %lld, ino = %lld",
1104 obj->variant.file_variant.file_size, inode->i_size);
1106 n_written = yaffs_wr_file(obj, buffer,
1107 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes, 0);
1109 yaffs_touch_super(dev);
1111 yaffs_trace(YAFFS_TRACE_OS,
1112 "writepag1: obj = %lld, ino = %lld",
1113 obj->variant.file_variant.file_size, inode->i_size);
1115 yaffs_gross_unlock(dev);
1118 set_page_writeback(page);
1120 end_page_writeback(page);
1123 return (n_written == n_bytes) ? 0 : -ENOSPC;
1126 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1127 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1128 loff_t pos, unsigned len, unsigned flags,
1129 struct page **pagep, void **fsdata)
1131 struct page *pg = NULL;
1132 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1138 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1139 pg = grab_cache_page_write_begin(mapping, index, flags);
1141 pg = __grab_cache_page(mapping, index);
1149 yaffs_trace(YAFFS_TRACE_OS,
1150 "start yaffs_write_begin index %d(%x) uptodate %d",
1151 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
1154 space_held = yaffs_hold_space(filp);
1161 /* Update page if required */
1163 if (!Page_Uptodate(pg))
1164 ret = yaffs_readpage_nolock(filp, pg);
1169 /* Happy path return */
1170 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
1175 yaffs_trace(YAFFS_TRACE_OS,
1176 "end yaffs_write_begin fail returning %d", ret);
1178 yaffs_release_space(filp);
1181 page_cache_release(pg);
1188 static int yaffs_prepare_write(struct file *f, struct page *pg,
1189 unsigned offset, unsigned to)
1191 yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
1193 if (!Page_Uptodate(pg))
1194 return yaffs_readpage_nolock(f, pg);
1199 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1200 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1201 loff_t pos, unsigned len, unsigned copied,
1202 struct page *pg, void *fsdadata)
1206 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1209 addr = kva + offset_into_page;
1211 yaffs_trace(YAFFS_TRACE_OS,
1212 "yaffs_write_end addr %p pos %lld n_bytes %d",
1215 ret = yaffs_file_write(filp, addr, copied, &pos);
1217 if (ret != copied) {
1218 yaffs_trace(YAFFS_TRACE_OS,
1219 "yaffs_write_end not same size ret %d copied %d",
1226 yaffs_release_space(filp);
1228 page_cache_release(pg);
1233 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1238 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1239 int n_bytes = to - offset;
1243 addr = kva + offset;
1245 yaffs_trace(YAFFS_TRACE_OS,
1246 "yaffs_commit_write addr %p pos %lld n_bytes %d",
1247 addr, pos, n_bytes);
1249 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
1251 if (n_written != n_bytes) {
1252 yaffs_trace(YAFFS_TRACE_OS,
1253 "yaffs_commit_write not same size n_written %d n_bytes %d",
1254 n_written, n_bytes);
1259 yaffs_trace(YAFFS_TRACE_OS,
1260 "yaffs_commit_write returning %d",
1261 n_written == n_bytes ? 0 : n_written);
1263 return n_written == n_bytes ? 0 : n_written;
1267 static void yaffs_fill_inode_from_obj(struct inode *inode,
1268 struct yaffs_obj *obj)
1272 /* Check mode against the variant type and attempt to repair if broken. */
1273 u32 mode = obj->yst_mode;
1274 switch (obj->variant_type) {
1275 case YAFFS_OBJECT_TYPE_FILE:
1276 if (!S_ISREG(mode)) {
1277 obj->yst_mode &= ~S_IFMT;
1278 obj->yst_mode |= S_IFREG;
1282 case YAFFS_OBJECT_TYPE_SYMLINK:
1283 if (!S_ISLNK(mode)) {
1284 obj->yst_mode &= ~S_IFMT;
1285 obj->yst_mode |= S_IFLNK;
1289 case YAFFS_OBJECT_TYPE_DIRECTORY:
1290 if (!S_ISDIR(mode)) {
1291 obj->yst_mode &= ~S_IFMT;
1292 obj->yst_mode |= S_IFDIR;
1296 case YAFFS_OBJECT_TYPE_UNKNOWN:
1297 case YAFFS_OBJECT_TYPE_HARDLINK:
1298 case YAFFS_OBJECT_TYPE_SPECIAL:
1304 inode->i_flags |= S_NOATIME;
1306 inode->i_ino = obj->obj_id;
1307 inode->i_mode = obj->yst_mode;
1308 inode->i_uid = obj->yst_uid;
1309 inode->i_gid = obj->yst_gid;
1310 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1311 inode->i_blksize = inode->i_sb->s_blocksize;
1313 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1315 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1316 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1317 inode->i_atime.tv_nsec = 0;
1318 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1319 inode->i_mtime.tv_nsec = 0;
1320 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1321 inode->i_ctime.tv_nsec = 0;
1323 inode->i_rdev = obj->yst_rdev;
1324 inode->i_atime = obj->yst_atime;
1325 inode->i_mtime = obj->yst_mtime;
1326 inode->i_ctime = obj->yst_ctime;
1328 inode->i_size = yaffs_get_obj_length(obj);
1329 inode->i_blocks = (inode->i_size + 511) >> 9;
1331 set_nlink(inode, yaffs_get_obj_link_count(obj));
1333 yaffs_trace(YAFFS_TRACE_OS,
1334 "yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
1335 inode->i_mode, inode->i_uid, inode->i_gid,
1336 inode->i_size, atomic_read(&inode->i_count));
1338 switch (obj->yst_mode & S_IFMT) {
1339 default: /* fifo, device or socket */
1340 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1341 init_special_inode(inode, obj->yst_mode,
1342 old_decode_dev(obj->yst_rdev));
1344 init_special_inode(inode, obj->yst_mode,
1345 (dev_t) (obj->yst_rdev));
1348 case S_IFREG: /* file */
1349 inode->i_op = &yaffs_file_inode_operations;
1350 inode->i_fop = &yaffs_file_operations;
1351 inode->i_mapping->a_ops =
1352 &yaffs_file_address_operations;
1354 case S_IFDIR: /* directory */
1355 inode->i_op = &yaffs_dir_inode_operations;
1356 inode->i_fop = &yaffs_dir_operations;
1358 case S_IFLNK: /* symlink */
1359 inode->i_op = &yaffs_symlink_inode_operations;
1363 yaffs_inode_to_obj_lv(inode) = obj;
1365 obj->my_inode = inode;
1368 yaffs_trace(YAFFS_TRACE_OS,
1369 "yaffs_fill_inode invalid parameters");
1374 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1375 struct yaffs_obj *obj)
1377 struct inode *inode;
1380 yaffs_trace(YAFFS_TRACE_OS,
1381 "yaffs_get_inode for NULL super_block!!");
1387 yaffs_trace(YAFFS_TRACE_OS,
1388 "yaffs_get_inode for NULL object!!");
1393 yaffs_trace(YAFFS_TRACE_OS,
1394 "yaffs_get_inode for object %d", obj->obj_id);
1396 inode = Y_IGET(sb, obj->obj_id);
1400 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1401 /* iget also increments the inode's i_count */
1402 /* NB You can't be holding gross_lock or deadlock will happen! */
1407 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1410 struct yaffs_obj *obj;
1413 struct inode *inode;
1414 struct yaffs_dev *dev;
1416 obj = yaffs_dentry_to_obj(f->f_dentry);
1419 yaffs_trace(YAFFS_TRACE_OS,
1420 "yaffs_file_write: hey obj is null!");
1426 yaffs_gross_lock(dev);
1428 inode = f->f_dentry->d_inode;
1430 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1431 ipos = inode->i_size;
1435 yaffs_trace(YAFFS_TRACE_OS,
1436 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %lld",
1437 (unsigned)n, (unsigned)n, obj->obj_id, ipos);
1439 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1441 yaffs_touch_super(dev);
1443 yaffs_trace(YAFFS_TRACE_OS,
1444 "yaffs_file_write: %d(%x) bytes written",
1445 (unsigned)n, (unsigned)n);
1447 if (n_written > 0) {
1450 if (ipos > inode->i_size) {
1451 inode->i_size = ipos;
1452 inode->i_blocks = (ipos + 511) >> 9;
1454 yaffs_trace(YAFFS_TRACE_OS,
1455 "yaffs_file_write size updated to %lld bytes, %d blocks",
1456 ipos, (int)(inode->i_blocks));
1460 yaffs_gross_unlock(dev);
1461 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1464 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1465 /* For now we just assume few parallel writes and check against a small number. */
1466 /* Todo: need to do this with a counter to handle parallel reads better */
1468 static ssize_t yaffs_hold_space(struct file *f)
1470 struct yaffs_obj *obj;
1471 struct yaffs_dev *dev;
1475 obj = yaffs_dentry_to_obj(f->f_dentry);
1479 yaffs_gross_lock(dev);
1481 n_free_chunks = yaffs_get_n_free_chunks(dev);
1483 yaffs_gross_unlock(dev);
1485 return (n_free_chunks > 20) ? 1 : 0;
1488 static void yaffs_release_space(struct file *f)
1490 struct yaffs_obj *obj;
1491 struct yaffs_dev *dev;
1493 obj = yaffs_dentry_to_obj(f->f_dentry);
1497 yaffs_gross_lock(dev);
1499 yaffs_gross_unlock(dev);
1502 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1504 struct yaffs_obj *obj;
1505 struct yaffs_dev *dev;
1506 struct yaffs_search_context *sc;
1507 struct inode *inode = f->f_dentry->d_inode;
1508 unsigned long offset, curoffs;
1509 struct yaffs_obj *l;
1512 char name[YAFFS_MAX_NAME_LENGTH + 1];
1514 obj = yaffs_dentry_to_obj(f->f_dentry);
1517 yaffs_gross_lock(dev);
1519 yaffs_dev_to_lc(dev)->readdir_process = current;
1523 sc = yaffs_new_search(obj);
1529 yaffs_trace(YAFFS_TRACE_OS,
1530 "yaffs_readdir: starting at %d", (int)offset);
1533 yaffs_trace(YAFFS_TRACE_OS,
1534 "yaffs_readdir: entry . ino %d",
1536 yaffs_gross_unlock(dev);
1537 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1538 yaffs_gross_lock(dev);
1541 yaffs_gross_lock(dev);
1546 yaffs_trace(YAFFS_TRACE_OS,
1547 "yaffs_readdir: entry .. ino %d",
1548 (int)f->f_dentry->d_parent->d_inode->i_ino);
1549 yaffs_gross_unlock(dev);
1550 if (filldir(dirent, "..", 2, offset,
1551 f->f_dentry->d_parent->d_inode->i_ino,
1553 yaffs_gross_lock(dev);
1556 yaffs_gross_lock(dev);
1563 /* If the directory has changed since the open or last call to
1564 readdir, rewind to after the 2 canned entries. */
1565 if (f->f_version != inode->i_version) {
1568 f->f_version = inode->i_version;
1571 while (sc->next_return) {
1573 l = sc->next_return;
1574 if (curoffs >= offset) {
1575 int this_inode = yaffs_get_obj_inode(l);
1576 int this_type = yaffs_get_obj_type(l);
1578 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1579 yaffs_trace(YAFFS_TRACE_OS,
1580 "yaffs_readdir: %s inode %d",
1581 name, yaffs_get_obj_inode(l));
1583 yaffs_gross_unlock(dev);
1588 offset, this_inode, this_type) < 0) {
1589 yaffs_gross_lock(dev);
1593 yaffs_gross_lock(dev);
1598 yaffs_search_advance(sc);
1602 yaffs_search_end(sc);
1603 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1604 yaffs_gross_unlock(dev);
1610 * File creation. Allocate an inode, and we're done..
1613 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1616 #define YCRED(x) (x->cred)
1619 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1620 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1623 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1627 struct inode *inode;
1629 struct yaffs_obj *obj = NULL;
1630 struct yaffs_dev *dev;
1632 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1634 int error = -ENOSPC;
1635 uid_t uid = YCRED(current)->fsuid;
1637 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1639 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1643 yaffs_trace(YAFFS_TRACE_OS,
1644 "yaffs_mknod: parent object %d type %d",
1645 parent->obj_id, parent->variant_type);
1647 yaffs_trace(YAFFS_TRACE_OS,
1648 "yaffs_mknod: could not get parent object");
1652 yaffs_trace(YAFFS_TRACE_OS,
1653 "yaffs_mknod: making oject for %s, mode %x dev %x",
1654 dentry->d_name.name, mode, rdev);
1656 dev = parent->my_dev;
1658 yaffs_gross_lock(dev);
1660 switch (mode & S_IFMT) {
1662 /* Special (socket, fifo, device...) */
1663 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
1664 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1666 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1667 gid, old_encode_dev(rdev));
1670 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1674 case S_IFREG: /* file */
1675 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
1676 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1679 case S_IFDIR: /* directory */
1680 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
1681 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1684 case S_IFLNK: /* symlink */
1685 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
1686 obj = NULL; /* Do we ever get here? */
1690 /* Can not call yaffs_get_inode() with gross lock held */
1691 yaffs_gross_unlock(dev);
1694 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1695 d_instantiate(dentry, inode);
1696 update_dir_time(dir);
1697 yaffs_trace(YAFFS_TRACE_OS,
1698 "yaffs_mknod created object %d count = %d",
1699 obj->obj_id, atomic_read(&inode->i_count));
1701 yaffs_fill_inode_from_obj(dir, parent);
1703 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
1710 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1713 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
1714 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1718 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1719 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1720 struct nameidata *n)
1722 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1725 yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
1726 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1729 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1733 struct yaffs_dev *dev;
1734 struct yaffs_obj *obj;
1736 yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
1737 (int)(dir->i_ino), dentry->d_name.name);
1738 obj = yaffs_inode_to_obj(dir);
1741 yaffs_gross_lock(dev);
1743 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1745 if (ret_val == YAFFS_OK) {
1746 inode_dec_link_count(dentry->d_inode);
1748 yaffs_gross_unlock(dev);
1749 update_dir_time(dir);
1752 yaffs_gross_unlock(dev);
1759 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1760 struct dentry *dentry)
1762 struct inode *inode = old_dentry->d_inode;
1763 struct yaffs_obj *obj = NULL;
1764 struct yaffs_obj *link = NULL;
1765 struct yaffs_dev *dev;
1767 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
1769 obj = yaffs_inode_to_obj(inode);
1772 yaffs_gross_lock(dev);
1774 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1776 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1780 set_nlink(old_dentry->d_inode, yaffs_get_obj_link_count(obj));
1781 d_instantiate(dentry, old_dentry->d_inode);
1782 atomic_inc(&old_dentry->d_inode->i_count);
1783 yaffs_trace(YAFFS_TRACE_OS,
1784 "yaffs_link link count %d i_count %d",
1785 old_dentry->d_inode->i_nlink,
1786 atomic_read(&old_dentry->d_inode->i_count));
1789 yaffs_gross_unlock(dev);
1792 update_dir_time(dir);
1799 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1800 const char *symname)
1802 struct yaffs_obj *obj;
1803 struct yaffs_dev *dev;
1804 uid_t uid = YCRED(current)->fsuid;
1806 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1808 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
1810 if (strnlen(dentry->d_name.name, YAFFS_MAX_NAME_LENGTH + 1) >
1811 YAFFS_MAX_NAME_LENGTH)
1812 return -ENAMETOOLONG;
1814 if (strnlen(symname, YAFFS_MAX_ALIAS_LENGTH + 1) >
1815 YAFFS_MAX_ALIAS_LENGTH)
1816 return -ENAMETOOLONG;
1818 dev = yaffs_inode_to_obj(dir)->my_dev;
1819 yaffs_gross_lock(dev);
1820 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1821 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1822 yaffs_gross_unlock(dev);
1825 struct inode *inode;
1827 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1828 d_instantiate(dentry, inode);
1829 update_dir_time(dir);
1830 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
1833 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
1839 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
1840 static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync)
1841 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1842 static int yaffs_sync_object(struct file *file, int datasync)
1844 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1849 struct yaffs_obj *obj;
1850 struct yaffs_dev *dev;
1851 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1852 struct dentry *dentry = file->f_path.dentry;
1855 obj = yaffs_dentry_to_obj(dentry);
1859 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1860 "yaffs_sync_object");
1861 yaffs_gross_lock(dev);
1862 yaffs_flush_file(obj, 1, datasync);
1863 yaffs_gross_unlock(dev);
1868 * The VFS layer already does all the dentry stuff for rename.
1870 * NB: POSIX says you can rename an object over an old object of the same name
1872 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1873 struct inode *new_dir, struct dentry *new_dentry)
1875 struct yaffs_dev *dev;
1876 int ret_val = YAFFS_FAIL;
1877 struct yaffs_obj *target;
1879 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
1880 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1882 yaffs_gross_lock(dev);
1884 /* Check if the target is an existing directory that is not empty. */
1885 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1886 new_dentry->d_name.name);
1888 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1889 !list_empty(&target->variant.dir_variant.children)) {
1891 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
1893 ret_val = YAFFS_FAIL;
1895 /* Now does unlinking internally using shadowing mechanism */
1896 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
1898 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1899 old_dentry->d_name.name,
1900 yaffs_inode_to_obj(new_dir),
1901 new_dentry->d_name.name);
1903 yaffs_gross_unlock(dev);
1905 if (ret_val == YAFFS_OK) {
1907 inode_dec_link_count(new_dentry->d_inode);
1909 update_dir_time(old_dir);
1910 if (old_dir != new_dir)
1911 update_dir_time(new_dir);
1918 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1920 struct inode *inode = dentry->d_inode;
1922 struct yaffs_dev *dev;
1924 yaffs_trace(YAFFS_TRACE_OS,
1925 "yaffs_setattr of object %d",
1926 yaffs_inode_to_obj(inode)->obj_id);
1928 /* Fail if a requested resize >= 2GB */
1929 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
1934 error = inode_change_ok(inode, attr);
1938 error = yaffs_vfs_setattr(inode, attr);
1939 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
1940 if (attr->ia_valid & ATTR_SIZE) {
1941 yaffs_vfs_setsize(inode, attr->ia_size);
1942 inode->i_blocks = (inode->i_size + 511) >> 9;
1945 dev = yaffs_inode_to_obj(inode)->my_dev;
1946 if (attr->ia_valid & ATTR_SIZE) {
1947 yaffs_trace(YAFFS_TRACE_OS,
1949 (int)(attr->ia_size),
1950 (int)(attr->ia_size));
1952 yaffs_gross_lock(dev);
1953 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
1954 if (result == YAFFS_OK) {
1959 yaffs_gross_unlock(dev);
1963 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
1968 static int yaffs_setxattr(struct dentry *dentry, const char *name,
1969 const void *value, size_t size, int flags)
1971 struct inode *inode = dentry->d_inode;
1973 struct yaffs_dev *dev;
1974 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1976 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
1981 yaffs_gross_lock(dev);
1982 result = yaffs_set_xattrib(obj, name, value, size, flags);
1983 if (result == YAFFS_OK)
1985 else if (result < 0)
1987 yaffs_gross_unlock(dev);
1990 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
1995 static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
1996 void *buff, size_t size)
1998 struct inode *inode = dentry->d_inode;
2000 struct yaffs_dev *dev;
2001 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2003 yaffs_trace(YAFFS_TRACE_OS,
2004 "yaffs_getxattr \"%s\" from object %d",
2009 yaffs_gross_lock(dev);
2010 error = yaffs_get_xattrib(obj, name, buff, size);
2011 yaffs_gross_unlock(dev);
2014 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
2019 static int yaffs_removexattr(struct dentry *dentry, const char *name)
2021 struct inode *inode = dentry->d_inode;
2023 struct yaffs_dev *dev;
2024 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2026 yaffs_trace(YAFFS_TRACE_OS,
2027 "yaffs_removexattr of object %d", obj->obj_id);
2032 yaffs_gross_lock(dev);
2033 result = yaffs_remove_xattrib(obj, name);
2034 if (result == YAFFS_OK)
2036 else if (result < 0)
2038 yaffs_gross_unlock(dev);
2041 yaffs_trace(YAFFS_TRACE_OS,
2042 "yaffs_removexattr done returning %d", error);
2047 static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
2049 struct inode *inode = dentry->d_inode;
2051 struct yaffs_dev *dev;
2052 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2054 yaffs_trace(YAFFS_TRACE_OS,
2055 "yaffs_listxattr of object %d", obj->obj_id);
2059 yaffs_gross_lock(dev);
2060 error = yaffs_list_xattrib(obj, buff, size);
2061 yaffs_gross_unlock(dev);
2064 yaffs_trace(YAFFS_TRACE_OS,
2065 "yaffs_listxattr done returning %d", error);
2071 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2072 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2074 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2075 struct super_block *sb = dentry->d_sb;
2076 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2077 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2079 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2081 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2083 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2086 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
2088 yaffs_gross_lock(dev);
2090 buf->f_type = YAFFS_MAGIC;
2091 buf->f_bsize = sb->s_blocksize;
2092 buf->f_namelen = 255;
2094 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2095 /* Do this if chunk size is not a power of 2 */
2097 uint64_t bytes_in_dev;
2098 uint64_t bytes_free;
2102 ((dev->param.end_block - dev->param.start_block +
2103 1))) * ((uint64_t) (dev->param.chunks_per_block *
2104 dev->data_bytes_per_chunk));
2106 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2107 buf->f_blocks = bytes_in_dev;
2109 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
2110 ((uint64_t) (dev->data_bytes_per_chunk));
2112 do_div(bytes_free, sb->s_blocksize);
2114 buf->f_bfree = bytes_free;
2116 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2119 (dev->param.end_block - dev->param.start_block + 1) *
2120 dev->param.chunks_per_block /
2121 (sb->s_blocksize / dev->data_bytes_per_chunk);
2123 yaffs_get_n_free_chunks(dev) /
2124 (sb->s_blocksize / dev->data_bytes_per_chunk);
2127 (dev->param.end_block - dev->param.start_block + 1) *
2128 dev->param.chunks_per_block *
2129 (dev->data_bytes_per_chunk / sb->s_blocksize);
2132 yaffs_get_n_free_chunks(dev) *
2133 (dev->data_bytes_per_chunk / sb->s_blocksize);
2138 buf->f_bavail = buf->f_bfree;
2140 yaffs_gross_unlock(dev);
2144 static void yaffs_flush_inodes(struct super_block *sb)
2147 struct yaffs_obj *obj;
2149 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
2150 obj = yaffs_inode_to_obj(iptr);
2152 yaffs_trace(YAFFS_TRACE_OS,
2155 yaffs_flush_file(obj, 1, 0);
2160 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2162 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2166 yaffs_flush_inodes(sb);
2167 yaffs_update_dirty_dirs(dev);
2168 yaffs_flush_whole_cache(dev);
2170 yaffs_checkpoint_save(dev);
2173 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2175 unsigned erased_chunks =
2176 dev->n_erased_blocks * dev->param.chunks_per_block;
2177 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2178 unsigned scattered = 0; /* Free chunks not in an erased block */
2180 if (erased_chunks < dev->n_free_chunks)
2181 scattered = (dev->n_free_chunks - erased_chunks);
2183 if (!context->bg_running)
2185 else if (scattered < (dev->param.chunks_per_block * 2))
2187 else if (erased_chunks > dev->n_free_chunks / 2)
2189 else if (erased_chunks > dev->n_free_chunks / 4)
2195 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
2198 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2199 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2200 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2203 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2204 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
2206 sb->s_dirt ? "dirty" : "clean",
2207 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2208 oneshot_checkpoint ? " one-shot" : "");
2210 yaffs_gross_lock(dev);
2211 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2212 oneshot_checkpoint) && !dev->is_checkpointed;
2214 if (sb->s_dirt || do_checkpoint) {
2215 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2217 if (oneshot_checkpoint)
2218 yaffs_auto_checkpoint &= ~4;
2220 yaffs_gross_unlock(dev);
2226 * yaffs background thread functions .
2227 * yaffs_bg_thread_fn() the thread function
2228 * yaffs_bg_start() launches the background thread.
2229 * yaffs_bg_stop() cleans up the background thread.
2232 * The thread should only run after the yaffs is initialised
2233 * The thread should be stopped before yaffs is unmounted.
2234 * The thread should not do any writing while the fs is in read only.
2237 #ifdef YAFFS_COMPILE_BACKGROUND
2239 void yaffs_background_waker(unsigned long data)
2241 wake_up_process((struct task_struct *)data);
2244 static int yaffs_bg_thread_fn(void *data)
2246 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2247 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2248 unsigned long now = jiffies;
2249 unsigned long next_dir_update = now;
2250 unsigned long next_gc = now;
2251 unsigned long expires;
2252 unsigned int urgency;
2255 struct timer_list timer;
2257 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2258 "yaffs_background starting for dev %p", (void *)dev);
2260 #ifdef YAFFS_COMPILE_FREEZER
2263 while (context->bg_running) {
2264 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
2266 if (kthread_should_stop())
2269 #ifdef YAFFS_COMPILE_FREEZER
2270 if (try_to_freeze())
2273 yaffs_gross_lock(dev);
2277 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
2278 yaffs_update_dirty_dirs(dev);
2279 next_dir_update = now + HZ;
2282 if (time_after(now, next_gc) && yaffs_bg_enable) {
2283 if (!dev->is_checkpointed) {
2284 urgency = yaffs_bg_gc_urgency(dev);
2285 gc_result = yaffs_bg_gc(dev, urgency);
2287 next_gc = now + HZ / 20 + 1;
2288 else if (urgency > 0)
2289 next_gc = now + HZ / 10 + 1;
2291 next_gc = now + HZ * 2;
2294 * gc not running so set to next_dir_update
2295 * to cut down on wake ups
2297 next_gc = next_dir_update;
2300 yaffs_gross_unlock(dev);
2302 expires = next_dir_update;
2303 if (time_before(next_gc, expires))
2305 if (time_before(expires, now))
2308 Y_INIT_TIMER(&timer);
2309 timer.expires = expires + 1;
2310 timer.data = (unsigned long)current;
2311 timer.function = yaffs_background_waker;
2313 set_current_state(TASK_INTERRUPTIBLE);
2316 del_timer_sync(&timer);
2325 static int yaffs_bg_start(struct yaffs_dev *dev)
2328 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2333 context->bg_running = 1;
2335 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2336 (void *)dev, "yaffs-bg-%d",
2339 if (IS_ERR(context->bg_thread)) {
2340 retval = PTR_ERR(context->bg_thread);
2341 context->bg_thread = NULL;
2342 context->bg_running = 0;
2347 static void yaffs_bg_stop(struct yaffs_dev *dev)
2349 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2351 ctxt->bg_running = 0;
2353 if (ctxt->bg_thread) {
2354 kthread_stop(ctxt->bg_thread);
2355 ctxt->bg_thread = NULL;
2359 static int yaffs_bg_thread_fn(void *data)
2364 static int yaffs_bg_start(struct yaffs_dev *dev)
2369 static void yaffs_bg_stop(struct yaffs_dev *dev)
2374 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2375 static void yaffs_write_super(struct super_block *sb)
2377 static int yaffs_write_super(struct super_block *sb)
2380 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2382 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2383 "yaffs_write_super %s",
2384 request_checkpoint ? " checkpt" : "");
2386 yaffs_do_sync_fs(sb, request_checkpoint);
2388 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2393 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2394 static int yaffs_sync_fs(struct super_block *sb, int wait)
2396 static int yaffs_sync_fs(struct super_block *sb)
2399 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2401 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2402 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
2404 yaffs_do_sync_fs(sb, request_checkpoint);
2409 #ifdef YAFFS_USE_OWN_IGET
2411 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2413 struct inode *inode;
2414 struct yaffs_obj *obj;
2415 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2417 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
2419 inode = iget_locked(sb, ino);
2421 return ERR_PTR(-ENOMEM);
2422 if (!(inode->i_state & I_NEW))
2425 /* NB This is called as a side effect of other functions, but
2426 * we had to release the lock to prevent deadlocks, so
2427 * need to lock again.
2430 yaffs_gross_lock(dev);
2432 obj = yaffs_find_by_number(dev, inode->i_ino);
2434 yaffs_fill_inode_from_obj(inode, obj);
2436 yaffs_gross_unlock(dev);
2438 unlock_new_inode(inode);
2444 static void yaffs_read_inode(struct inode *inode)
2446 /* NB This is called as a side effect of other functions, but
2447 * we had to release the lock to prevent deadlocks, so
2448 * need to lock again.
2451 struct yaffs_obj *obj;
2452 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
2454 yaffs_trace(YAFFS_TRACE_OS,
2455 "yaffs_read_inode for %d", (int)inode->i_ino);
2457 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2458 yaffs_gross_lock(dev);
2460 obj = yaffs_find_by_number(dev, inode->i_ino);
2462 yaffs_fill_inode_from_obj(inode, obj);
2464 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2465 yaffs_gross_unlock(dev);
2470 static LIST_HEAD(yaffs_context_list);
2471 struct mutex yaffs_context_lock;
2473 static void yaffs_put_super(struct super_block *sb)
2475 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2477 yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
2479 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2480 "Shutting down yaffs background thread");
2482 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2483 "yaffs background thread shut down");
2485 yaffs_gross_lock(dev);
2487 yaffs_flush_super(sb, 1);
2489 if (yaffs_dev_to_lc(dev)->put_super_fn)
2490 yaffs_dev_to_lc(dev)->put_super_fn(sb);
2492 yaffs_deinitialise(dev);
2494 yaffs_gross_unlock(dev);
2496 mutex_lock(&yaffs_context_lock);
2497 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2498 mutex_unlock(&yaffs_context_lock);
2500 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2501 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
2502 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2508 static void yaffs_mtd_put_super(struct super_block *sb)
2510 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
2515 put_mtd_device(mtd);
2518 static void yaffs_touch_super(struct yaffs_dev *dev)
2520 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
2522 yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb);
2527 struct yaffs_options {
2529 int skip_checkpoint_read;
2530 int skip_checkpoint_write;
2533 int tags_ecc_overridden;
2534 int lazy_loading_enabled;
2535 int lazy_loading_overridden;
2536 int empty_lost_and_found;
2537 int empty_lost_and_found_overridden;
2538 int disable_summary;
2541 #define MAX_OPT_LEN 30
2542 static int yaffs_parse_options(struct yaffs_options *options,
2543 const char *options_str)
2545 char cur_opt[MAX_OPT_LEN + 1];
2549 /* Parse through the options which is a comma seperated list */
2551 while (options_str && *options_str && !error) {
2552 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2555 while (*options_str == ',')
2558 while (*options_str && *options_str != ',') {
2559 if (p < MAX_OPT_LEN) {
2560 cur_opt[p] = *options_str;
2566 if (!strcmp(cur_opt, "inband-tags")) {
2567 options->inband_tags = 1;
2568 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2569 options->tags_ecc_on = 0;
2570 options->tags_ecc_overridden = 1;
2571 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2572 options->tags_ecc_on = 1;
2573 options->tags_ecc_overridden = 1;
2574 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2575 options->lazy_loading_enabled = 0;
2576 options->lazy_loading_overridden = 1;
2577 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2578 options->lazy_loading_enabled = 1;
2579 options->lazy_loading_overridden = 1;
2580 } else if (!strcmp(cur_opt, "disable-summary")) {
2581 options->disable_summary = 1;
2582 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2583 options->empty_lost_and_found = 0;
2584 options->empty_lost_and_found_overridden = 1;
2585 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2586 options->empty_lost_and_found = 1;
2587 options->empty_lost_and_found_overridden = 1;
2588 } else if (!strcmp(cur_opt, "no-cache")) {
2589 options->no_cache = 1;
2590 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2591 options->skip_checkpoint_read = 1;
2592 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2593 options->skip_checkpoint_write = 1;
2594 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2595 options->skip_checkpoint_read = 1;
2596 options->skip_checkpoint_write = 1;
2598 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2607 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2608 struct super_block *sb,
2609 void *data, int silent)
2612 struct inode *inode = NULL;
2613 struct dentry *root;
2614 struct yaffs_dev *dev = 0;
2615 char devname_buf[BDEVNAME_SIZE + 1];
2616 struct mtd_info *mtd;
2618 char *data_str = (char *)data;
2619 struct yaffs_linux_context *context = NULL;
2620 struct yaffs_param *param;
2624 struct yaffs_options options;
2628 struct yaffs_linux_context *context_iterator;
2629 struct list_head *l;
2632 printk(KERN_INFO "yaffs: sb is NULL\n");
2636 sb->s_magic = YAFFS_MAGIC;
2637 sb->s_op = &yaffs_super_ops;
2638 sb->s_flags |= MS_NOATIME;
2640 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2642 #ifdef YAFFS_COMPILE_EXPORTFS
2643 sb->s_export_op = &yaffs_export_ops;
2647 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2648 else if (!yaffs_devname(sb, devname_buf))
2649 printk(KERN_INFO "yaffs: devname is NULL\n");
2651 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2653 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2658 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2660 memset(&options, 0, sizeof(options));
2662 if (yaffs_parse_options(&options, data_str)) {
2663 /* Option parsing failed */
2667 sb->s_blocksize = PAGE_CACHE_SIZE;
2668 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2670 yaffs_trace(YAFFS_TRACE_OS,
2671 "yaffs_read_super: Using yaffs%d", yaffs_version);
2672 yaffs_trace(YAFFS_TRACE_OS,
2673 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2675 yaffs_trace(YAFFS_TRACE_ALWAYS,
2676 "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
2677 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2678 yaffs_devname(sb, devname_buf));
2680 /* Check it's an mtd device..... */
2681 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2682 return NULL; /* This isn't an mtd device */
2684 /* Get the device */
2685 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2687 yaffs_trace(YAFFS_TRACE_ALWAYS,
2688 "yaffs: MTD device #%u doesn't appear to exist",
2692 /* Check it's NAND */
2693 if (mtd->type != MTD_NANDFLASH) {
2694 yaffs_trace(YAFFS_TRACE_ALWAYS,
2695 "yaffs: MTD device is not NAND it's type %d",
2700 yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase);
2701 yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read);
2702 yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write);
2703 yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob);
2704 yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob);
2705 yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad);
2706 yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad);
2707 yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
2708 yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
2709 yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
2710 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2711 yaffs_trace(YAFFS_TRACE_OS, " size %u", mtd->size);
2713 yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
2716 if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2717 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2721 /* Added NCB 26/5/2006 for completeness */
2722 if (yaffs_version == 2 && !options.inband_tags
2723 && WRITE_SIZE(mtd) == 512) {
2724 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2728 if (yaffs_version == 2) {
2729 /* Check for version 2 style functions */
2731 !mtd->block_isbad ||
2732 !mtd->block_markbad || !mtd->read || !mtd->write ||
2733 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2734 !mtd->read_oob || !mtd->write_oob) {
2737 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2739 yaffs_trace(YAFFS_TRACE_ALWAYS,
2740 "MTD device does not support required functions"
2745 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2746 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2747 !options.inband_tags) {
2748 yaffs_trace(YAFFS_TRACE_ALWAYS,
2749 "MTD device does not have the right page sizes"
2754 /* Check for V1 style functions */
2755 if (!mtd->erase || !mtd->read || !mtd->write ||
2756 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2757 !mtd->read_oob || !mtd->write_oob) {
2760 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2762 yaffs_trace(YAFFS_TRACE_ALWAYS,
2763 "MTD device does not support required functions"
2768 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2769 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2770 yaffs_trace(YAFFS_TRACE_ALWAYS,
2771 "MTD device does not support have the right page sizes"
2777 /* OK, so if we got here, we have an MTD that's NAND and looks
2778 * like it has the right capabilities
2779 * Set the struct yaffs_dev up for mtd
2782 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2785 "yaffs: mtd is read only, setting superblock read only\n"
2787 sb->s_flags |= MS_RDONLY;
2790 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2791 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2793 if (!dev || !context) {
2803 /* Deep shit could not allocate device structure */
2804 yaffs_trace(YAFFS_TRACE_ALWAYS,
2805 "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
2809 memset(dev, 0, sizeof(struct yaffs_dev));
2810 param = &(dev->param);
2812 memset(context, 0, sizeof(struct yaffs_linux_context));
2813 dev->os_context = context;
2814 INIT_LIST_HEAD(&(context->context_list));
2816 context->super = sb;
2818 dev->read_only = read_only;
2820 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2821 sb->s_fs_info = dev;
2823 sb->u.generic_sbp = dev;
2827 dev->driver_context = mtd;
2828 param->name = mtd->name;
2830 /* Set up the memory size parameters.... */
2833 param->n_reserved_blocks = 5;
2834 param->n_caches = (options.no_cache) ? 0 : 10;
2835 param->inband_tags = options.inband_tags;
2837 param->enable_xattr = 1;
2838 if (options.lazy_loading_overridden)
2839 param->disable_lazy_load = !options.lazy_loading_enabled;
2841 param->defered_dir_update = 1;
2843 if (options.tags_ecc_overridden)
2844 param->no_tags_ecc = !options.tags_ecc_on;
2846 param->empty_lost_n_found = 1;
2847 param->refresh_period = 500;
2848 param->disable_summary = options.disable_summary;
2850 if (options.empty_lost_and_found_overridden)
2851 param->empty_lost_n_found = options.empty_lost_and_found;
2853 /* ... and the functions. */
2854 if (yaffs_version == 2) {
2855 param->is_yaffs2 = 1;
2856 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2857 param->total_bytes_per_chunk = mtd->writesize;
2858 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2860 param->total_bytes_per_chunk = mtd->oobblock;
2861 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
2863 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2865 param->start_block = 0;
2866 param->end_block = n_blocks - 1;
2868 param->is_yaffs2 = 0;
2869 n_blocks = YCALCBLOCKS(mtd->size,
2870 YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
2872 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2873 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2876 param->start_block = 0;
2877 param->end_block = n_blocks - 1;
2879 yaffs_mtd_drv_install(dev);
2881 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2883 param->sb_dirty_fn = yaffs_touch_super;
2884 param->gc_control = yaffs_gc_control_callback;
2886 yaffs_dev_to_lc(dev)->super = sb;
2888 param->use_nand_ecc = 1;
2890 param->skip_checkpt_rd = options.skip_checkpoint_read;
2891 param->skip_checkpt_wr = options.skip_checkpoint_write;
2893 mutex_lock(&yaffs_context_lock);
2894 /* Get a mount id */
2896 for (mount_id = 0; !found; mount_id++) {
2898 list_for_each(l, &yaffs_context_list) {
2900 list_entry(l, struct yaffs_linux_context,
2902 if (context_iterator->mount_id == mount_id)
2906 context->mount_id = mount_id;
2908 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2909 &yaffs_context_list);
2910 mutex_unlock(&yaffs_context_lock);
2912 /* Directory search handling... */
2913 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2914 param->remove_obj_fn = yaffs_remove_obj_callback;
2916 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2918 yaffs_gross_lock(dev);
2920 err = yaffs_guts_initialise(dev);
2922 yaffs_trace(YAFFS_TRACE_OS,
2923 "yaffs_read_super: guts initialised %s",
2924 (err == YAFFS_OK) ? "OK" : "FAILED");
2926 if (err == YAFFS_OK)
2927 yaffs_bg_start(dev);
2929 if (!context->bg_thread)
2930 param->defered_dir_update = 0;
2932 sb->s_maxbytes = yaffs_max_file_size(dev);
2934 /* Release lock before yaffs_get_inode() */
2935 yaffs_gross_unlock(dev);
2937 /* Create root inode */
2938 if (err == YAFFS_OK)
2939 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2944 inode->i_op = &yaffs_dir_inode_operations;
2945 inode->i_fop = &yaffs_dir_operations;
2947 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2949 root = d_alloc_root(inode);
2951 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
2958 sb->s_dirt = !dev->is_checkpointed;
2959 yaffs_trace(YAFFS_TRACE_ALWAYS,
2960 "yaffs_read_super: is_checkpointed %d",
2961 dev->is_checkpointed);
2963 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
2967 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2968 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2971 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2974 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
2975 static struct dentry *yaffs_mount(struct file_system_type *fs_type, int flags,
2976 const char *dev_name, void *data)
2978 return mount_bdev(fs_type, flags, dev_name, data, yaffs_internal_read_super_mtd);
2980 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2981 static int yaffs_read_super(struct file_system_type *fs,
2982 int flags, const char *dev_name,
2983 void *data, struct vfsmount *mnt)
2986 return get_sb_bdev(fs, flags, dev_name, data,
2987 yaffs_internal_read_super_mtd, mnt);
2990 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2991 int flags, const char *dev_name,
2995 return get_sb_bdev(fs, flags, dev_name, data,
2996 yaffs_internal_read_super_mtd);
3000 static struct file_system_type yaffs_fs_type = {
3001 .owner = THIS_MODULE,
3003 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3004 .mount = yaffs_mount,
3006 .get_sb = yaffs_read_super,
3008 .kill_sb = kill_block_super,
3009 .fs_flags = FS_REQUIRES_DEV,
3012 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3015 return yaffs_internal_read_super(1, sb, data, silent);
3018 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3023 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3024 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3027 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3030 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3031 static struct dentry *yaffs2_mount(struct file_system_type *fs_type, int flags,
3032 const char *dev_name, void *data)
3034 return mount_bdev(fs_type, flags, dev_name, data, yaffs2_internal_read_super_mtd);
3036 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3037 static int yaffs2_read_super(struct file_system_type *fs,
3038 int flags, const char *dev_name, void *data,
3039 struct vfsmount *mnt)
3041 return get_sb_bdev(fs, flags, dev_name, data,
3042 yaffs2_internal_read_super_mtd, mnt);
3045 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3046 int flags, const char *dev_name,
3050 return get_sb_bdev(fs, flags, dev_name, data,
3051 yaffs2_internal_read_super_mtd);
3055 static struct file_system_type yaffs2_fs_type = {
3056 .owner = THIS_MODULE,
3058 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3059 .mount = yaffs2_mount,
3061 .get_sb = yaffs2_read_super,
3063 .kill_sb = kill_block_super,
3064 .fs_flags = FS_REQUIRES_DEV,
3067 static struct super_block *yaffs2_read_super(struct super_block *sb,
3068 void *data, int silent)
3070 return yaffs_internal_read_super(2, sb, data, silent);
3073 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3078 static struct proc_dir_entry *my_proc_entry;
3080 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3082 struct yaffs_param *param = &dev->param;
3084 buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
3085 buf += sprintf(buf, "end_block............ %d\n", param->end_block);
3086 buf += sprintf(buf, "total_bytes_per_chunk %d\n",
3087 param->total_bytes_per_chunk);
3088 buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
3089 buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
3090 buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
3091 buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
3092 buf += sprintf(buf, "empty_lost_n_found... %d\n",
3093 param->empty_lost_n_found);
3094 buf += sprintf(buf, "disable_lazy_load.... %d\n",
3095 param->disable_lazy_load);
3096 buf += sprintf(buf, "refresh_period....... %d\n",
3097 param->refresh_period);
3098 buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
3099 buf += sprintf(buf, "n_reserved_blocks.... %d\n",
3100 param->n_reserved_blocks);
3101 buf += sprintf(buf, "always_check_erased.. %d\n",
3102 param->always_check_erased);
3103 buf += sprintf(buf, "\n");
3108 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3110 buf += sprintf(buf, "max file size....... %lld\n",
3111 (long long) yaffs_max_file_size(dev));
3112 buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
3113 dev->data_bytes_per_chunk);
3114 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3115 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3116 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3117 buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
3118 dev->blocks_in_checkpt);
3119 buf += sprintf(buf, "\n");
3120 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3121 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3122 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3123 buf += sprintf(buf, "\n");
3124 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3125 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3126 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3127 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3128 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3129 buf += sprintf(buf, "passive_gc_count..... %u\n",
3130 dev->passive_gc_count);
3131 buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
3132 dev->oldest_dirty_gc_count);
3133 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3134 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3135 buf += sprintf(buf, "n_retried_writes..... %u\n",
3136 dev->n_retried_writes);
3137 buf += sprintf(buf, "n_retired_blocks..... %u\n",
3138 dev->n_retired_blocks);
3139 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3140 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3141 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
3142 dev->n_tags_ecc_fixed);
3143 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
3144 dev->n_tags_ecc_unfixed);
3145 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3146 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3147 buf += sprintf(buf, "n_unlinked_files..... %u\n",
3148 dev->n_unlinked_files);
3149 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3150 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3151 buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
3152 buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
3157 static int yaffs_proc_read(char *page,
3159 off_t offset, int count, int *eof, void *data)
3161 struct list_head *item;
3166 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3167 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3168 * This also assumes the user has posted a read buffer large
3169 * enough to hold the complete output; but that's life in /proc.
3174 /* Print header first */
3178 "Multi-version YAFFS built:" __DATE__ " " __TIME__
3181 buf += sprintf(buf, "\n");
3185 mutex_lock(&yaffs_context_lock);
3187 /* Locate and print the Nth entry. Order N-squared but N is small. */
3188 list_for_each(item, &yaffs_context_list) {
3189 struct yaffs_linux_context *dc =
3190 list_entry(item, struct yaffs_linux_context,
3192 struct yaffs_dev *dev = dc->dev;
3194 if (n < (step & ~1)) {
3198 if ((step & 1) == 0) {
3200 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3202 buf = yaffs_dump_dev_part0(buf, dev);
3204 buf = yaffs_dump_dev_part1(buf, dev);
3209 mutex_unlock(&yaffs_context_lock);
3212 return buf - page < count ? buf - page : count;
3216 * Set the verbosity of the warnings and error messages.
3218 * Note that the names can only be a..z or _ with the current code.
3223 unsigned mask_bitfield;
3225 {"allocate", YAFFS_TRACE_ALLOCATE},
3226 {"always", YAFFS_TRACE_ALWAYS},
3227 {"background", YAFFS_TRACE_BACKGROUND},
3228 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3229 {"buffers", YAFFS_TRACE_BUFFERS},
3230 {"bug", YAFFS_TRACE_BUG},
3231 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3232 {"deletion", YAFFS_TRACE_DELETION},
3233 {"erase", YAFFS_TRACE_ERASE},
3234 {"error", YAFFS_TRACE_ERROR},
3235 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3236 {"gc", YAFFS_TRACE_GC},
3237 {"lock", YAFFS_TRACE_LOCK},
3238 {"mtd", YAFFS_TRACE_MTD},
3239 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3240 {"os", YAFFS_TRACE_OS},
3241 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3242 {"scan", YAFFS_TRACE_SCAN},
3243 {"mount", YAFFS_TRACE_MOUNT},
3244 {"tracing", YAFFS_TRACE_TRACING},
3245 {"sync", YAFFS_TRACE_SYNC},
3246 {"write", YAFFS_TRACE_WRITE},
3247 {"verify", YAFFS_TRACE_VERIFY},
3248 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3249 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3250 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3251 {"all", 0xffffffff},
3256 #define MAX_MASK_NAME_LENGTH 40
3257 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3258 unsigned long count, void *data)
3260 unsigned rg = 0, mask_bitfield;
3264 char substring[MAX_MASK_NAME_LENGTH + 1];
3270 rg = yaffs_trace_mask;
3272 while (!done && (pos < count)) {
3274 while ((pos < count) && isspace(buf[pos]))
3291 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3293 if (end > buf + pos) {
3294 mask_name = "numeral";
3295 len = end - (buf + pos);
3299 for (x = buf + pos, i = 0;
3300 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3301 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3303 substring[i] = '\0';
3305 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3306 if (strcmp(substring, mask_flags[i].mask_name)
3308 mask_name = mask_flags[i].mask_name;
3310 mask_flags[i].mask_bitfield;
3317 if (mask_name != NULL) {
3321 rg &= ~mask_bitfield;
3324 rg |= mask_bitfield;
3330 rg |= mask_bitfield;
3336 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3338 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3340 if (rg & YAFFS_TRACE_ALWAYS) {
3341 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3343 flag = ((rg & mask_flags[i].mask_bitfield) ==
3344 mask_flags[i].mask_bitfield) ? '+' : '-';
3345 printk(KERN_DEBUG "%c%s\n", flag,
3346 mask_flags[i].mask_name);
3353 static int yaffs_proc_write(struct file *file, const char *buf,
3354 unsigned long count, void *data)
3356 return yaffs_proc_write_trace_options(file, buf, count, data);
3359 /* Stuff to handle installation of file systems */
3360 struct file_system_to_install {
3361 struct file_system_type *fst;
3365 static struct file_system_to_install fs_to_install[] = {
3366 {&yaffs_fs_type, 0},
3367 {&yaffs2_fs_type, 0},
3371 static int __init init_yaffs_fs(void)
3374 struct file_system_to_install *fsinst;
3376 yaffs_trace(YAFFS_TRACE_ALWAYS,
3377 "yaffs built " __DATE__ " " __TIME__ " Installing.");
3379 mutex_init(&yaffs_context_lock);
3381 /* Install the proc_fs entries */
3382 my_proc_entry = create_proc_entry("yaffs",
3383 S_IRUGO | S_IFREG, YPROC_ROOT);
3385 if (my_proc_entry) {
3386 my_proc_entry->write_proc = yaffs_proc_write;
3387 my_proc_entry->read_proc = yaffs_proc_read;
3388 my_proc_entry->data = NULL;
3393 /* Now add the file system entries */
3395 fsinst = fs_to_install;
3397 while (fsinst->fst && !error) {
3398 error = register_filesystem(fsinst->fst);
3400 fsinst->installed = 1;
3404 /* Any errors? uninstall */
3406 fsinst = fs_to_install;
3408 while (fsinst->fst) {
3409 if (fsinst->installed) {
3410 unregister_filesystem(fsinst->fst);
3411 fsinst->installed = 0;
3420 static void __exit exit_yaffs_fs(void)
3423 struct file_system_to_install *fsinst;
3425 yaffs_trace(YAFFS_TRACE_ALWAYS,
3426 "yaffs built " __DATE__ " " __TIME__ " removing.");
3428 remove_proc_entry("yaffs", YPROC_ROOT);
3430 fsinst = fs_to_install;
3432 while (fsinst->fst) {
3433 if (fsinst->installed) {
3434 unregister_filesystem(fsinst->fst);
3435 fsinst->installed = 0;
3441 module_init(init_yaffs_fs)
3442 module_exit(exit_yaffs_fs)
3444 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3445 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
3446 MODULE_LICENSE("GPL");