generic-2.6: sync yaffs code with the official CVS tree
[openwrt/staging/dedeckeh.git] / target / linux / generic-2.6 / files / fs / yaffs2 / yaffs_fs.c
1 /*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 * Acknowledgements:
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
15 *
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.
19 */
20
21 /*
22 *
23 * This is the file system front-end to YAFFS that hooks it up to
24 * the VFS.
25 *
26 * Special notes:
27 * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
28 * this superblock
29 * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
30 * superblock
31 * >> inode->u.generic_ip points to the associated yaffs_Object.
32 */
33
34 const char *yaffs_fs_c_version =
35 "$Id: yaffs_fs.c,v 1.66 2008-05-05 07:58:58 charles Exp $";
36 extern const char *yaffs_guts_c_version;
37
38 #include <linux/version.h>
39 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
40 #include <linux/config.h>
41 #endif
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/init.h>
46 #include <linux/list.h>
47 #include <linux/fs.h>
48 #include <linux/proc_fs.h>
49 #include <linux/smp_lock.h>
50 #include <linux/pagemap.h>
51 #include <linux/mtd/mtd.h>
52 #include <linux/interrupt.h>
53 #include <linux/string.h>
54 #include <linux/ctype.h>
55
56 #include "asm/div64.h"
57
58 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
59
60 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
61 #include <asm/statfs.h>
62 #define UnlockPage(p) unlock_page(p)
63 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
64
65 /* FIXME: use sb->s_id instead ? */
66 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
67
68 #else
69
70 #include <linux/locks.h>
71 #define BDEVNAME_SIZE 0
72 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
73
74 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
75 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
76 #define __user
77 #endif
78
79 #endif
80
81 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
82 #define WRITE_SIZE_STR "writesize"
83 #define WRITE_SIZE(mtd) (mtd)->writesize
84 #else
85 #define WRITE_SIZE_STR "oobblock"
86 #define WRITE_SIZE(mtd) (mtd)->oobblock
87 #endif
88
89 #include <asm/uaccess.h>
90
91 #include "yportenv.h"
92 #include "yaffs_guts.h"
93
94 #include <linux/mtd/mtd.h>
95 #include "yaffs_mtdif.h"
96 #include "yaffs_mtdif1.h"
97 #include "yaffs_mtdif2.h"
98
99 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
100 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
101
102 /* Module Parameters */
103 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
104 module_param(yaffs_traceMask,uint,0644);
105 module_param(yaffs_wr_attempts,uint,0644);
106 #else
107 MODULE_PARM(yaffs_traceMask,"i");
108 MODULE_PARM(yaffs_wr_attempts,"i");
109 #endif
110
111 /*#define T(x) printk x */
112
113 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
114 #define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
115 #else
116 #define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
117 #endif
118
119 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
120 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
121
122 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
123 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
124 #else
125 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
126 #endif
127
128 static void yaffs_put_super(struct super_block *sb);
129
130 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
131 loff_t * pos);
132
133 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
134 static int yaffs_file_flush(struct file *file, fl_owner_t id);
135 #else
136 static int yaffs_file_flush(struct file *file);
137 #endif
138
139 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
140 int datasync);
141
142 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
143
144 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
145 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
146 struct nameidata *n);
147 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
148 struct nameidata *n);
149 #else
150 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
151 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
152 #endif
153 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
154 struct dentry *dentry);
155 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
156 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
157 const char *symname);
158 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
159
160 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
161 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
162 dev_t dev);
163 #else
164 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
165 int dev);
166 #endif
167 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
168 struct inode *new_dir, struct dentry *new_dentry);
169 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
170
171 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
172 static int yaffs_sync_fs(struct super_block *sb, int wait);
173 static void yaffs_write_super(struct super_block *sb);
174 #else
175 static int yaffs_sync_fs(struct super_block *sb);
176 static int yaffs_write_super(struct super_block *sb);
177 #endif
178
179 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
180 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
181 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
182 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
183 #else
184 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
185 #endif
186 static void yaffs_read_inode(struct inode *inode);
187
188 static void yaffs_put_inode(struct inode *inode);
189 static void yaffs_delete_inode(struct inode *);
190 static void yaffs_clear_inode(struct inode *);
191
192 static int yaffs_readpage(struct file *file, struct page *page);
193 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
194 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
195 #else
196 static int yaffs_writepage(struct page *page);
197 #endif
198 static int yaffs_prepare_write(struct file *f, struct page *pg,
199 unsigned offset, unsigned to);
200 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
201 unsigned to);
202
203 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
204 int buflen);
205 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
206 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
207 #else
208 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
209 #endif
210
211 static struct address_space_operations yaffs_file_address_operations = {
212 .readpage = yaffs_readpage,
213 .writepage = yaffs_writepage,
214 .prepare_write = yaffs_prepare_write,
215 .commit_write = yaffs_commit_write,
216 };
217
218 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,22))
219 static struct file_operations yaffs_file_operations = {
220 .read = do_sync_read,
221 .write = do_sync_write,
222 .aio_read = generic_file_aio_read,
223 .aio_write = generic_file_aio_write,
224 .mmap = generic_file_mmap,
225 .flush = yaffs_file_flush,
226 .fsync = yaffs_sync_object,
227 .splice_read = generic_file_splice_read,
228 .splice_write = generic_file_splice_write,
229 };
230
231 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
232
233 static struct file_operations yaffs_file_operations = {
234 .read = do_sync_read,
235 .write = do_sync_write,
236 .aio_read = generic_file_aio_read,
237 .aio_write = generic_file_aio_write,
238 .mmap = generic_file_mmap,
239 .flush = yaffs_file_flush,
240 .fsync = yaffs_sync_object,
241 .sendfile = generic_file_sendfile,
242 };
243
244 #else
245
246 static struct file_operations yaffs_file_operations = {
247 .read = generic_file_read,
248 .write = generic_file_write,
249 .mmap = generic_file_mmap,
250 .flush = yaffs_file_flush,
251 .fsync = yaffs_sync_object,
252 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
253 .sendfile = generic_file_sendfile,
254 #endif
255 };
256 #endif
257
258 static struct inode_operations yaffs_file_inode_operations = {
259 .setattr = yaffs_setattr,
260 };
261
262 static struct inode_operations yaffs_symlink_inode_operations = {
263 .readlink = yaffs_readlink,
264 .follow_link = yaffs_follow_link,
265 .setattr = yaffs_setattr,
266 };
267
268 static struct inode_operations yaffs_dir_inode_operations = {
269 .create = yaffs_create,
270 .lookup = yaffs_lookup,
271 .link = yaffs_link,
272 .unlink = yaffs_unlink,
273 .symlink = yaffs_symlink,
274 .mkdir = yaffs_mkdir,
275 .rmdir = yaffs_unlink,
276 .mknod = yaffs_mknod,
277 .rename = yaffs_rename,
278 .setattr = yaffs_setattr,
279 };
280
281 static struct file_operations yaffs_dir_operations = {
282 .read = generic_read_dir,
283 .readdir = yaffs_readdir,
284 .fsync = yaffs_sync_object,
285 };
286
287 static struct super_operations yaffs_super_ops = {
288 .statfs = yaffs_statfs,
289 .read_inode = yaffs_read_inode,
290 .put_inode = yaffs_put_inode,
291 .put_super = yaffs_put_super,
292 .delete_inode = yaffs_delete_inode,
293 .clear_inode = yaffs_clear_inode,
294 .sync_fs = yaffs_sync_fs,
295 .write_super = yaffs_write_super,
296 };
297
298 static void yaffs_GrossLock(yaffs_Device * dev)
299 {
300 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
301
302 down(&dev->grossLock);
303 }
304
305 static void yaffs_GrossUnlock(yaffs_Device * dev)
306 {
307 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
308 up(&dev->grossLock);
309
310 }
311
312 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
313 int buflen)
314 {
315 unsigned char *alias;
316 int ret;
317
318 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
319
320 yaffs_GrossLock(dev);
321
322 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
323
324 yaffs_GrossUnlock(dev);
325
326 if (!alias)
327 return -ENOMEM;
328
329 ret = vfs_readlink(dentry, buffer, buflen, alias);
330 kfree(alias);
331 return ret;
332 }
333
334 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
335 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
336 #else
337 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
338 #endif
339 {
340 unsigned char *alias;
341 int ret;
342 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
343
344 yaffs_GrossLock(dev);
345
346 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
347
348 yaffs_GrossUnlock(dev);
349
350 if (!alias)
351 {
352 ret = -ENOMEM;
353 goto out;
354 }
355
356 ret = vfs_follow_link(nd, alias);
357 kfree(alias);
358 out:
359 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
360 return ERR_PTR (ret);
361 #else
362 return ret;
363 #endif
364 }
365
366 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
367 yaffs_Object * obj);
368
369 /*
370 * Lookup is used to find objects in the fs
371 */
372 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
373
374 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
375 struct nameidata *n)
376 #else
377 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
378 #endif
379 {
380 yaffs_Object *obj;
381 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
382
383 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
384
385 yaffs_GrossLock(dev);
386
387 T(YAFFS_TRACE_OS,
388 (KERN_DEBUG "yaffs_lookup for %d:%s\n",
389 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
390
391 obj =
392 yaffs_FindObjectByName(yaffs_InodeToObject(dir),
393 dentry->d_name.name);
394
395 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
396
397 /* Can't hold gross lock when calling yaffs_get_inode() */
398 yaffs_GrossUnlock(dev);
399
400 if (obj) {
401 T(YAFFS_TRACE_OS,
402 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
403
404 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
405
406 if (inode) {
407 T(YAFFS_TRACE_OS,
408 (KERN_DEBUG "yaffs_loookup dentry \n"));
409 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
410 * d_add even if NULL inode */
411 #if 0
412 /*dget(dentry); // try to solve directory bug */
413 d_add(dentry, inode);
414
415 /* return dentry; */
416 return NULL;
417 #endif
418 }
419
420 } else {
421 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
422
423 }
424
425 /* added NCB for 2.5/6 compatability - forces add even if inode is
426 * NULL which creates dentry hash */
427 d_add(dentry, inode);
428
429 return NULL;
430 /* return (ERR_PTR(-EIO)); */
431
432 }
433
434 /* For now put inode is just for debugging
435 * Put inode is called when the inode **structure** is put.
436 */
437 static void yaffs_put_inode(struct inode *inode)
438 {
439 T(YAFFS_TRACE_OS,
440 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
441 atomic_read(&inode->i_count)));
442
443 }
444
445 /* clear is called to tell the fs to release any per-inode data it holds */
446 static void yaffs_clear_inode(struct inode *inode)
447 {
448 yaffs_Object *obj;
449 yaffs_Device *dev;
450
451 obj = yaffs_InodeToObject(inode);
452
453 T(YAFFS_TRACE_OS,
454 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
455 atomic_read(&inode->i_count),
456 obj ? "object exists" : "null object"));
457
458 if (obj) {
459 dev = obj->myDev;
460 yaffs_GrossLock(dev);
461
462 /* Clear the association between the inode and
463 * the yaffs_Object.
464 */
465 obj->myInode = NULL;
466 yaffs_InodeToObjectLV(inode) = NULL;
467
468 /* If the object freeing was deferred, then the real
469 * free happens now.
470 * This should fix the inode inconsistency problem.
471 */
472
473 yaffs_HandleDeferedFree(obj);
474
475 yaffs_GrossUnlock(dev);
476 }
477
478 }
479
480 /* delete is called when the link count is zero and the inode
481 * is put (ie. nobody wants to know about it anymore, time to
482 * delete the file).
483 * NB Must call clear_inode()
484 */
485 static void yaffs_delete_inode(struct inode *inode)
486 {
487 yaffs_Object *obj = yaffs_InodeToObject(inode);
488 yaffs_Device *dev;
489
490 T(YAFFS_TRACE_OS,
491 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
492 atomic_read(&inode->i_count),
493 obj ? "object exists" : "null object"));
494
495 if (obj) {
496 dev = obj->myDev;
497 yaffs_GrossLock(dev);
498 yaffs_DeleteFile(obj);
499 yaffs_GrossUnlock(dev);
500 }
501 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
502 truncate_inode_pages (&inode->i_data, 0);
503 #endif
504 clear_inode(inode);
505 }
506
507 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
508 static int yaffs_file_flush(struct file *file, fl_owner_t id)
509 #else
510 static int yaffs_file_flush(struct file *file)
511 #endif
512 {
513 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
514
515 yaffs_Device *dev = obj->myDev;
516
517 T(YAFFS_TRACE_OS,
518 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
519 obj->dirty ? "dirty" : "clean"));
520
521 yaffs_GrossLock(dev);
522
523 yaffs_FlushFile(obj, 1);
524
525 yaffs_GrossUnlock(dev);
526
527 return 0;
528 }
529
530 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
531 {
532 /* Lifted from jffs2 */
533
534 yaffs_Object *obj;
535 unsigned char *pg_buf;
536 int ret;
537
538 yaffs_Device *dev;
539
540 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
541 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
542 (unsigned)PAGE_CACHE_SIZE));
543
544 obj = yaffs_DentryToObject(f->f_dentry);
545
546 dev = obj->myDev;
547
548 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
549 BUG_ON(!PageLocked(pg));
550 #else
551 if (!PageLocked(pg))
552 PAGE_BUG(pg);
553 #endif
554
555 pg_buf = kmap(pg);
556 /* FIXME: Can kmap fail? */
557
558 yaffs_GrossLock(dev);
559
560 ret =
561 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
562 PAGE_CACHE_SIZE);
563
564 yaffs_GrossUnlock(dev);
565
566 if (ret >= 0)
567 ret = 0;
568
569 if (ret) {
570 ClearPageUptodate(pg);
571 SetPageError(pg);
572 } else {
573 SetPageUptodate(pg);
574 ClearPageError(pg);
575 }
576
577 flush_dcache_page(pg);
578 kunmap(pg);
579
580 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
581 return ret;
582 }
583
584 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
585 {
586 int ret = yaffs_readpage_nolock(f, pg);
587 UnlockPage(pg);
588 return ret;
589 }
590
591 static int yaffs_readpage(struct file *f, struct page *pg)
592 {
593 return yaffs_readpage_unlock(f, pg);
594 }
595
596 /* writepage inspired by/stolen from smbfs */
597
598 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
599 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
600 #else
601 static int yaffs_writepage(struct page *page)
602 #endif
603 {
604 struct address_space *mapping = page->mapping;
605 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
606 struct inode *inode;
607 unsigned long end_index;
608 char *buffer;
609 yaffs_Object *obj;
610 int nWritten = 0;
611 unsigned nBytes;
612
613 if (!mapping)
614 BUG();
615 inode = mapping->host;
616 if (!inode)
617 BUG();
618
619 if (offset > inode->i_size) {
620 T(YAFFS_TRACE_OS,
621 (KERN_DEBUG
622 "yaffs_writepage at %08x, inode size = %08x!!!\n",
623 (unsigned)(page->index << PAGE_CACHE_SHIFT),
624 (unsigned)inode->i_size));
625 T(YAFFS_TRACE_OS,
626 (KERN_DEBUG " -> don't care!!\n"));
627 unlock_page(page);
628 return 0;
629 }
630
631 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
632
633 /* easy case */
634 if (page->index < end_index) {
635 nBytes = PAGE_CACHE_SIZE;
636 } else {
637 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
638 }
639
640 get_page(page);
641
642 buffer = kmap(page);
643
644 obj = yaffs_InodeToObject(inode);
645 yaffs_GrossLock(obj->myDev);
646
647 T(YAFFS_TRACE_OS,
648 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
649 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
650 T(YAFFS_TRACE_OS,
651 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
652 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
653
654 nWritten =
655 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
656 nBytes, 0);
657
658 T(YAFFS_TRACE_OS,
659 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
660 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
661
662 yaffs_GrossUnlock(obj->myDev);
663
664 kunmap(page);
665 SetPageUptodate(page);
666 UnlockPage(page);
667 put_page(page);
668
669 return (nWritten == nBytes) ? 0 : -ENOSPC;
670 }
671
672 static int yaffs_prepare_write(struct file *f, struct page *pg,
673 unsigned offset, unsigned to)
674 {
675
676 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
677 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
678 return yaffs_readpage_nolock(f, pg);
679
680 return 0;
681
682 }
683
684 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
685 unsigned to)
686 {
687
688 void *addr = page_address(pg) + offset;
689 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
690 int nBytes = to - offset;
691 int nWritten;
692
693 unsigned spos = pos;
694 unsigned saddr = (unsigned)addr;
695
696 T(YAFFS_TRACE_OS,
697 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
698 spos, nBytes));
699
700 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
701
702 if (nWritten != nBytes) {
703 T(YAFFS_TRACE_OS,
704 (KERN_DEBUG
705 "yaffs_commit_write not same size nWritten %d nBytes %d\n",
706 nWritten, nBytes));
707 SetPageError(pg);
708 ClearPageUptodate(pg);
709 } else {
710 SetPageUptodate(pg);
711 }
712
713 T(YAFFS_TRACE_OS,
714 (KERN_DEBUG "yaffs_commit_write returning %d\n",
715 nWritten == nBytes ? 0 : nWritten));
716
717 return nWritten == nBytes ? 0 : nWritten;
718
719 }
720
721 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
722 {
723 if (inode && obj) {
724
725
726 /* Check mode against the variant type and attempt to repair if broken. */
727 __u32 mode = obj->yst_mode;
728 switch( obj->variantType ){
729 case YAFFS_OBJECT_TYPE_FILE :
730 if( ! S_ISREG(mode) ){
731 obj->yst_mode &= ~S_IFMT;
732 obj->yst_mode |= S_IFREG;
733 }
734
735 break;
736 case YAFFS_OBJECT_TYPE_SYMLINK :
737 if( ! S_ISLNK(mode) ){
738 obj->yst_mode &= ~S_IFMT;
739 obj->yst_mode |= S_IFLNK;
740 }
741
742 break;
743 case YAFFS_OBJECT_TYPE_DIRECTORY :
744 if( ! S_ISDIR(mode) ){
745 obj->yst_mode &= ~S_IFMT;
746 obj->yst_mode |= S_IFDIR;
747 }
748
749 break;
750 case YAFFS_OBJECT_TYPE_UNKNOWN :
751 case YAFFS_OBJECT_TYPE_HARDLINK :
752 case YAFFS_OBJECT_TYPE_SPECIAL :
753 default:
754 /* TODO? */
755 break;
756 }
757
758 inode->i_flags |= S_NOATIME;
759
760 inode->i_ino = obj->objectId;
761 inode->i_mode = obj->yst_mode;
762 inode->i_uid = obj->yst_uid;
763 inode->i_gid = obj->yst_gid;
764 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
765 inode->i_blksize = inode->i_sb->s_blocksize;
766 #endif
767 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
768
769 inode->i_rdev = old_decode_dev(obj->yst_rdev);
770 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
771 inode->i_atime.tv_nsec = 0;
772 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
773 inode->i_mtime.tv_nsec = 0;
774 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
775 inode->i_ctime.tv_nsec = 0;
776 #else
777 inode->i_rdev = obj->yst_rdev;
778 inode->i_atime = obj->yst_atime;
779 inode->i_mtime = obj->yst_mtime;
780 inode->i_ctime = obj->yst_ctime;
781 #endif
782 inode->i_size = yaffs_GetObjectFileLength(obj);
783 inode->i_blocks = (inode->i_size + 511) >> 9;
784
785 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
786
787 T(YAFFS_TRACE_OS,
788 (KERN_DEBUG
789 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
790 inode->i_mode, inode->i_uid, inode->i_gid,
791 (int)inode->i_size, atomic_read(&inode->i_count)));
792
793 switch (obj->yst_mode & S_IFMT) {
794 default: /* fifo, device or socket */
795 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
796 init_special_inode(inode, obj->yst_mode,
797 old_decode_dev(obj->yst_rdev));
798 #else
799 init_special_inode(inode, obj->yst_mode,
800 (dev_t) (obj->yst_rdev));
801 #endif
802 break;
803 case S_IFREG: /* file */
804 inode->i_op = &yaffs_file_inode_operations;
805 inode->i_fop = &yaffs_file_operations;
806 inode->i_mapping->a_ops =
807 &yaffs_file_address_operations;
808 break;
809 case S_IFDIR: /* directory */
810 inode->i_op = &yaffs_dir_inode_operations;
811 inode->i_fop = &yaffs_dir_operations;
812 break;
813 case S_IFLNK: /* symlink */
814 inode->i_op = &yaffs_symlink_inode_operations;
815 break;
816 }
817
818 yaffs_InodeToObjectLV(inode) = obj;
819
820 obj->myInode = inode;
821
822 } else {
823 T(YAFFS_TRACE_OS,
824 (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
825 }
826
827 }
828
829 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
830 yaffs_Object * obj)
831 {
832 struct inode *inode;
833
834 if (!sb) {
835 T(YAFFS_TRACE_OS,
836 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
837 return NULL;
838
839 }
840
841 if (!obj) {
842 T(YAFFS_TRACE_OS,
843 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
844 return NULL;
845
846 }
847
848 T(YAFFS_TRACE_OS,
849 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
850
851 inode = iget(sb, obj->objectId);
852
853 /* NB Side effect: iget calls back to yaffs_read_inode(). */
854 /* iget also increments the inode's i_count */
855 /* NB You can't be holding grossLock or deadlock will happen! */
856
857 return inode;
858 }
859
860 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
861 loff_t * pos)
862 {
863 yaffs_Object *obj;
864 int nWritten, ipos;
865 struct inode *inode;
866 yaffs_Device *dev;
867
868 obj = yaffs_DentryToObject(f->f_dentry);
869
870 dev = obj->myDev;
871
872 yaffs_GrossLock(dev);
873
874 inode = f->f_dentry->d_inode;
875
876 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
877 ipos = inode->i_size;
878 } else {
879 ipos = *pos;
880 }
881
882 if (!obj) {
883 T(YAFFS_TRACE_OS,
884 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
885 } else {
886 T(YAFFS_TRACE_OS,
887 (KERN_DEBUG
888 "yaffs_file_write about to write writing %d bytes"
889 "to object %d at %d\n",
890 n, obj->objectId, ipos));
891 }
892
893 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
894
895 T(YAFFS_TRACE_OS,
896 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
897 n, nWritten, ipos));
898 if (nWritten > 0) {
899 ipos += nWritten;
900 *pos = ipos;
901 if (ipos > inode->i_size) {
902 inode->i_size = ipos;
903 inode->i_blocks = (ipos + 511) >> 9;
904
905 T(YAFFS_TRACE_OS,
906 (KERN_DEBUG
907 "yaffs_file_write size updated to %d bytes, "
908 "%d blocks\n",
909 ipos, (int)(inode->i_blocks)));
910 }
911
912 }
913 yaffs_GrossUnlock(dev);
914 return nWritten == 0 ? -ENOSPC : nWritten;
915 }
916
917 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
918 {
919 yaffs_Object *obj;
920 yaffs_Device *dev;
921 struct inode *inode = f->f_dentry->d_inode;
922 unsigned long offset, curoffs;
923 struct list_head *i;
924 yaffs_Object *l;
925
926 char name[YAFFS_MAX_NAME_LENGTH + 1];
927
928 obj = yaffs_DentryToObject(f->f_dentry);
929 dev = obj->myDev;
930
931 yaffs_GrossLock(dev);
932
933 offset = f->f_pos;
934
935 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
936
937 if (offset == 0) {
938 T(YAFFS_TRACE_OS,
939 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
940 (int)inode->i_ino));
941 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
942 < 0) {
943 goto out;
944 }
945 offset++;
946 f->f_pos++;
947 }
948 if (offset == 1) {
949 T(YAFFS_TRACE_OS,
950 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
951 (int)f->f_dentry->d_parent->d_inode->i_ino));
952 if (filldir
953 (dirent, "..", 2, offset,
954 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
955 goto out;
956 }
957 offset++;
958 f->f_pos++;
959 }
960
961 curoffs = 1;
962
963 /* If the directory has changed since the open or last call to
964 readdir, rewind to after the 2 canned entries. */
965
966 if (f->f_version != inode->i_version) {
967 offset = 2;
968 f->f_pos = offset;
969 f->f_version = inode->i_version;
970 }
971
972 list_for_each(i, &obj->variant.directoryVariant.children) {
973 curoffs++;
974 if (curoffs >= offset) {
975 l = list_entry(i, yaffs_Object, siblings);
976
977 yaffs_GetObjectName(l, name,
978 YAFFS_MAX_NAME_LENGTH + 1);
979 T(YAFFS_TRACE_OS,
980 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
981 yaffs_GetObjectInode(l)));
982
983 if (filldir(dirent,
984 name,
985 strlen(name),
986 offset,
987 yaffs_GetObjectInode(l),
988 yaffs_GetObjectType(l))
989 < 0) {
990 goto up_and_out;
991 }
992
993 offset++;
994 f->f_pos++;
995 }
996 }
997
998 up_and_out:
999 out:
1000
1001 yaffs_GrossUnlock(dev);
1002
1003 return 0;
1004 }
1005
1006 /*
1007 * File creation. Allocate an inode, and we're done..
1008 */
1009 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1010 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1011 dev_t rdev)
1012 #else
1013 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1014 int rdev)
1015 #endif
1016 {
1017 struct inode *inode;
1018
1019 yaffs_Object *obj = NULL;
1020 yaffs_Device *dev;
1021
1022 yaffs_Object *parent = yaffs_InodeToObject(dir);
1023
1024 int error = -ENOSPC;
1025 uid_t uid = current->fsuid;
1026 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1027
1028 if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1029 mode |= S_ISGID;
1030
1031 if (parent) {
1032 T(YAFFS_TRACE_OS,
1033 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
1034 parent->objectId, parent->variantType));
1035 } else {
1036 T(YAFFS_TRACE_OS,
1037 (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
1038 return -EPERM;
1039 }
1040
1041 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1042 "mode %x dev %x\n",
1043 dentry->d_name.name, mode, rdev));
1044
1045 dev = parent->myDev;
1046
1047 yaffs_GrossLock(dev);
1048
1049 switch (mode & S_IFMT) {
1050 default:
1051 /* Special (socket, fifo, device...) */
1052 T(YAFFS_TRACE_OS, (KERN_DEBUG
1053 "yaffs_mknod: making special\n"));
1054 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1055 obj =
1056 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1057 gid, old_encode_dev(rdev));
1058 #else
1059 obj =
1060 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1061 gid, rdev);
1062 #endif
1063 break;
1064 case S_IFREG: /* file */
1065 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1066 obj =
1067 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1068 gid);
1069 break;
1070 case S_IFDIR: /* directory */
1071 T(YAFFS_TRACE_OS,
1072 (KERN_DEBUG "yaffs_mknod: making directory\n"));
1073 obj =
1074 yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1075 uid, gid);
1076 break;
1077 case S_IFLNK: /* symlink */
1078 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1079 obj = NULL; /* Do we ever get here? */
1080 break;
1081 }
1082
1083 /* Can not call yaffs_get_inode() with gross lock held */
1084 yaffs_GrossUnlock(dev);
1085
1086 if (obj) {
1087 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1088 d_instantiate(dentry, inode);
1089 T(YAFFS_TRACE_OS,
1090 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
1091 obj->objectId, atomic_read(&inode->i_count)));
1092 error = 0;
1093 } else {
1094 T(YAFFS_TRACE_OS,
1095 (KERN_DEBUG "yaffs_mknod failed making object\n"));
1096 error = -ENOMEM;
1097 }
1098
1099 return error;
1100 }
1101
1102 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1103 {
1104 int retVal;
1105 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
1106 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1107 #if 0
1108 /* attempt to fix dir bug - didn't work */
1109 if (!retVal) {
1110 dget(dentry);
1111 }
1112 #endif
1113 return retVal;
1114 }
1115
1116 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1117 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1118 struct nameidata *n)
1119 #else
1120 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1121 #endif
1122 {
1123 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1124 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1125 }
1126
1127 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1128 {
1129 int retVal;
1130
1131 yaffs_Device *dev;
1132
1133 T(YAFFS_TRACE_OS,
1134 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1135 dentry->d_name.name));
1136
1137 dev = yaffs_InodeToObject(dir)->myDev;
1138
1139 yaffs_GrossLock(dev);
1140
1141 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1142
1143 if (retVal == YAFFS_OK) {
1144 dentry->d_inode->i_nlink--;
1145 dir->i_version++;
1146 yaffs_GrossUnlock(dev);
1147 mark_inode_dirty(dentry->d_inode);
1148 return 0;
1149 }
1150 yaffs_GrossUnlock(dev);
1151 return -ENOTEMPTY;
1152 }
1153
1154 /*
1155 * Create a link...
1156 */
1157 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1158 struct dentry *dentry)
1159 {
1160 struct inode *inode = old_dentry->d_inode;
1161 yaffs_Object *obj = NULL;
1162 yaffs_Object *link = NULL;
1163 yaffs_Device *dev;
1164
1165 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1166
1167 obj = yaffs_InodeToObject(inode);
1168 dev = obj->myDev;
1169
1170 yaffs_GrossLock(dev);
1171
1172 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1173 {
1174 link =
1175 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1176 obj);
1177 }
1178
1179 if (link) {
1180 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1181 d_instantiate(dentry, old_dentry->d_inode);
1182 atomic_inc(&old_dentry->d_inode->i_count);
1183 T(YAFFS_TRACE_OS,
1184 (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1185 old_dentry->d_inode->i_nlink,
1186 atomic_read(&old_dentry->d_inode->i_count)));
1187
1188 }
1189
1190 yaffs_GrossUnlock(dev);
1191
1192 if (link) {
1193
1194 return 0;
1195 }
1196
1197 return -EPERM;
1198 }
1199
1200 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1201 const char *symname)
1202 {
1203 yaffs_Object *obj;
1204 yaffs_Device *dev;
1205 uid_t uid = current->fsuid;
1206 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1207
1208 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1209
1210 dev = yaffs_InodeToObject(dir)->myDev;
1211 yaffs_GrossLock(dev);
1212 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1213 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1214 yaffs_GrossUnlock(dev);
1215
1216 if (obj) {
1217
1218 struct inode *inode;
1219
1220 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1221 d_instantiate(dentry, inode);
1222 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1223 return 0;
1224 } else {
1225 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1226
1227 }
1228
1229 return -ENOMEM;
1230 }
1231
1232 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1233 int datasync)
1234 {
1235
1236 yaffs_Object *obj;
1237 yaffs_Device *dev;
1238
1239 obj = yaffs_DentryToObject(dentry);
1240
1241 dev = obj->myDev;
1242
1243 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1244 yaffs_GrossLock(dev);
1245 yaffs_FlushFile(obj, 1);
1246 yaffs_GrossUnlock(dev);
1247 return 0;
1248 }
1249
1250 /*
1251 * The VFS layer already does all the dentry stuff for rename.
1252 *
1253 * NB: POSIX says you can rename an object over an old object of the same name
1254 */
1255 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1256 struct inode *new_dir, struct dentry *new_dentry)
1257 {
1258 yaffs_Device *dev;
1259 int retVal = YAFFS_FAIL;
1260 yaffs_Object *target;
1261
1262 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1263 dev = yaffs_InodeToObject(old_dir)->myDev;
1264
1265 yaffs_GrossLock(dev);
1266
1267 /* Check if the target is an existing directory that is not empty. */
1268 target =
1269 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1270 new_dentry->d_name.name);
1271
1272
1273
1274 if (target &&
1275 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1276 !list_empty(&target->variant.directoryVariant.children)) {
1277
1278 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1279
1280 retVal = YAFFS_FAIL;
1281 } else {
1282
1283 /* Now does unlinking internally using shadowing mechanism */
1284 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1285
1286 retVal =
1287 yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1288 old_dentry->d_name.name,
1289 yaffs_InodeToObject(new_dir),
1290 new_dentry->d_name.name);
1291
1292 }
1293 yaffs_GrossUnlock(dev);
1294
1295 if (retVal == YAFFS_OK) {
1296 if(target) {
1297 new_dentry->d_inode->i_nlink--;
1298 mark_inode_dirty(new_dentry->d_inode);
1299 }
1300
1301 return 0;
1302 } else {
1303 return -ENOTEMPTY;
1304 }
1305
1306 }
1307
1308 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1309 {
1310 struct inode *inode = dentry->d_inode;
1311 int error;
1312 yaffs_Device *dev;
1313
1314 T(YAFFS_TRACE_OS,
1315 (KERN_DEBUG "yaffs_setattr of object %d\n",
1316 yaffs_InodeToObject(inode)->objectId));
1317
1318 if ((error = inode_change_ok(inode, attr)) == 0) {
1319
1320 dev = yaffs_InodeToObject(inode)->myDev;
1321 yaffs_GrossLock(dev);
1322 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1323 YAFFS_OK) {
1324 error = 0;
1325 } else {
1326 error = -EPERM;
1327 }
1328 yaffs_GrossUnlock(dev);
1329 if (!error)
1330 error = inode_setattr(inode, attr);
1331 }
1332 return error;
1333 }
1334
1335 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1336 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1337 {
1338 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1339 struct super_block *sb = dentry->d_sb;
1340 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1341 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1342 {
1343 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1344 #else
1345 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1346 {
1347 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1348 #endif
1349
1350 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1351
1352 yaffs_GrossLock(dev);
1353
1354 buf->f_type = YAFFS_MAGIC;
1355 buf->f_bsize = sb->s_blocksize;
1356 buf->f_namelen = 255;
1357
1358 if(dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)){
1359 /* Do this if chunk size is not a power of 2 */
1360
1361 uint64_t bytesInDev;
1362 uint64_t bytesFree;
1363
1364 bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock +1))) *
1365 ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
1366
1367 do_div(bytesInDev,sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1368 buf->f_blocks = bytesInDev;
1369
1370 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1371 ((uint64_t)(dev->nDataBytesPerChunk));
1372
1373 do_div(bytesFree,sb->s_blocksize);
1374
1375 buf->f_bfree = bytesFree;
1376
1377 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1378
1379 buf->f_blocks =
1380 (dev->endBlock - dev->startBlock + 1) *
1381 dev->nChunksPerBlock /
1382 (sb->s_blocksize / dev->nDataBytesPerChunk);
1383 buf->f_bfree =
1384 yaffs_GetNumberOfFreeChunks(dev) /
1385 (sb->s_blocksize / dev->nDataBytesPerChunk);
1386 } else {
1387 buf->f_blocks =
1388 (dev->endBlock - dev->startBlock + 1) *
1389 dev->nChunksPerBlock *
1390 (dev->nDataBytesPerChunk / sb->s_blocksize);
1391
1392 buf->f_bfree =
1393 yaffs_GetNumberOfFreeChunks(dev) *
1394 (dev->nDataBytesPerChunk / sb->s_blocksize);
1395 }
1396
1397
1398 buf->f_files = 0;
1399 buf->f_ffree = 0;
1400 buf->f_bavail = buf->f_bfree;
1401
1402 yaffs_GrossUnlock(dev);
1403 return 0;
1404 }
1405
1406
1407 /**
1408 static int yaffs_do_sync_fs(struct super_block *sb)
1409 {
1410
1411 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1412 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
1413
1414 if(sb->s_dirt) {
1415 yaffs_GrossLock(dev);
1416
1417 if(dev)
1418 yaffs_CheckpointSave(dev);
1419
1420 yaffs_GrossUnlock(dev);
1421
1422 sb->s_dirt = 0;
1423 }
1424 return 0;
1425 }
1426 **/
1427
1428 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1429 static void yaffs_write_super(struct super_block *sb)
1430 #else
1431 static int yaffs_write_super(struct super_block *sb)
1432 #endif
1433 {
1434
1435 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
1436 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
1437 return 0; /* yaffs_do_sync_fs(sb);*/
1438 #endif
1439 }
1440
1441
1442 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1443 static int yaffs_sync_fs(struct super_block *sb, int wait)
1444 #else
1445 static int yaffs_sync_fs(struct super_block *sb)
1446 #endif
1447 {
1448
1449 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
1450
1451 return 0; /* yaffs_do_sync_fs(sb);*/
1452
1453 }
1454
1455
1456 static void yaffs_read_inode(struct inode *inode)
1457 {
1458 /* NB This is called as a side effect of other functions, but
1459 * we had to release the lock to prevent deadlocks, so
1460 * need to lock again.
1461 */
1462
1463 yaffs_Object *obj;
1464 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1465
1466 T(YAFFS_TRACE_OS,
1467 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1468
1469 yaffs_GrossLock(dev);
1470
1471 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1472
1473 yaffs_FillInodeFromObject(inode, obj);
1474
1475 yaffs_GrossUnlock(dev);
1476 }
1477
1478 static LIST_HEAD(yaffs_dev_list);
1479
1480 #if 0 // not used
1481 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1482 {
1483 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1484
1485 if( *flags & MS_RDONLY ) {
1486 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1487
1488 T(YAFFS_TRACE_OS,
1489 (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
1490
1491 yaffs_GrossLock(dev);
1492
1493 yaffs_FlushEntireDeviceCache(dev);
1494
1495 yaffs_CheckpointSave(dev);
1496
1497 if (mtd->sync)
1498 mtd->sync(mtd);
1499
1500 yaffs_GrossUnlock(dev);
1501 }
1502 else {
1503 T(YAFFS_TRACE_OS,
1504 (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
1505 }
1506
1507 return 0;
1508 }
1509 #endif
1510
1511 static void yaffs_put_super(struct super_block *sb)
1512 {
1513 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1514
1515 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
1516
1517 yaffs_GrossLock(dev);
1518
1519 yaffs_FlushEntireDeviceCache(dev);
1520
1521 yaffs_CheckpointSave(dev);
1522
1523 if (dev->putSuperFunc) {
1524 dev->putSuperFunc(sb);
1525 }
1526
1527 yaffs_Deinitialise(dev);
1528
1529 yaffs_GrossUnlock(dev);
1530
1531 /* we assume this is protected by lock_kernel() in mount/umount */
1532 list_del(&dev->devList);
1533
1534 if(dev->spareBuffer){
1535 YFREE(dev->spareBuffer);
1536 dev->spareBuffer = NULL;
1537 }
1538
1539 kfree(dev);
1540 }
1541
1542
1543 static void yaffs_MTDPutSuper(struct super_block *sb)
1544 {
1545
1546 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1547
1548 if (mtd->sync) {
1549 mtd->sync(mtd);
1550 }
1551
1552 put_mtd_device(mtd);
1553 }
1554
1555
1556 static void yaffs_MarkSuperBlockDirty(void *vsb)
1557 {
1558 struct super_block *sb = (struct super_block *)vsb;
1559
1560 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
1561 // if(sb)
1562 // sb->s_dirt = 1;
1563 }
1564
1565 typedef struct {
1566 int inband_tags;
1567 int skip_checkpoint_read;
1568 int skip_checkpoint_write;
1569 int no_cache;
1570 } yaffs_options;
1571
1572 #define MAX_OPT_LEN 20
1573 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
1574 {
1575 char cur_opt[MAX_OPT_LEN+1];
1576 int p;
1577 int error = 0;
1578
1579 /* Parse through the options which is a comma seperated list */
1580
1581 while(options_str && *options_str && !error){
1582 memset(cur_opt,0,MAX_OPT_LEN+1);
1583 p = 0;
1584
1585 while(*options_str && *options_str != ','){
1586 if(p < MAX_OPT_LEN){
1587 cur_opt[p] = *options_str;
1588 p++;
1589 }
1590 options_str++;
1591 }
1592
1593 if(!strcmp(cur_opt,"inband-tags"))
1594 options->inband_tags = 1;
1595 else if(!strcmp(cur_opt,"no-cache"))
1596 options->no_cache = 1;
1597 else if(!strcmp(cur_opt,"no-checkpoint-read"))
1598 options->skip_checkpoint_read = 1;
1599 else if(!strcmp(cur_opt,"no-checkpoint-write"))
1600 options->skip_checkpoint_write = 1;
1601 else if(!strcmp(cur_opt,"no-checkpoint")){
1602 options->skip_checkpoint_read = 1;
1603 options->skip_checkpoint_write = 1;
1604 } else {
1605 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
1606 error = 1;
1607 }
1608
1609 }
1610
1611 return error;
1612 }
1613
1614 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1615 struct super_block *sb,
1616 void *data, int silent)
1617 {
1618 int nBlocks;
1619 struct inode *inode = NULL;
1620 struct dentry *root;
1621 yaffs_Device *dev = 0;
1622 char devname_buf[BDEVNAME_SIZE + 1];
1623 struct mtd_info *mtd;
1624 int err;
1625 char *data_str = (char *)data;
1626
1627 yaffs_options options;
1628
1629 sb->s_magic = YAFFS_MAGIC;
1630 sb->s_op = &yaffs_super_ops;
1631 sb->s_flags |= MS_NOATIME;
1632
1633 if (!sb)
1634 printk(KERN_INFO "yaffs: sb is NULL\n");
1635 else if (!sb->s_dev)
1636 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1637 else if (!yaffs_devname(sb, devname_buf))
1638 printk(KERN_INFO "yaffs: devname is NULL\n");
1639 else
1640 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1641 sb->s_dev,
1642 yaffs_devname(sb, devname_buf));
1643
1644 if(!data_str)
1645 data_str = "";
1646
1647 printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
1648
1649 memset(&options,0,sizeof(options));
1650
1651 if(yaffs_parse_options(&options,data_str)){
1652 /* Option parsing failed */
1653 return NULL;
1654 }
1655
1656
1657 sb->s_blocksize = PAGE_CACHE_SIZE;
1658 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1659 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1660 T(YAFFS_TRACE_OS,
1661 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1662
1663 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1664 T(YAFFS_TRACE_OS,
1665 ("yaffs: Write verification disabled. All guarantees "
1666 "null and void\n"));
1667 #endif
1668
1669 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1670 "\"%s\"\n",
1671 MAJOR(sb->s_dev), MINOR(sb->s_dev),
1672 yaffs_devname(sb, devname_buf)));
1673
1674 /* Check it's an mtd device..... */
1675 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1676 return NULL; /* This isn't an mtd device */
1677 }
1678 /* Get the device */
1679 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1680 if (!mtd) {
1681 T(YAFFS_TRACE_ALWAYS,
1682 ("yaffs: MTD device #%u doesn't appear to exist\n",
1683 MINOR(sb->s_dev)));
1684 return NULL;
1685 }
1686 /* Check it's NAND */
1687 if (mtd->type != MTD_NANDFLASH) {
1688 T(YAFFS_TRACE_ALWAYS,
1689 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1690 return NULL;
1691 }
1692
1693 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1694 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1695 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1696 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1697 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1698 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1699 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1700 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
1701 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1702 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1703 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1704
1705 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1706
1707 if (yaffsVersion == 1 &&
1708 WRITE_SIZE(mtd) >= 2048) {
1709 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1710 yaffsVersion = 2;
1711 }
1712
1713 /* Added NCB 26/5/2006 for completeness */
1714 if (yaffsVersion == 2 &&
1715 !options.inband_tags &&
1716 WRITE_SIZE(mtd) == 512){
1717 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
1718 yaffsVersion = 1;
1719 }
1720
1721 #endif
1722
1723 if (yaffsVersion == 2) {
1724 /* Check for version 2 style functions */
1725 if (!mtd->erase ||
1726 !mtd->block_isbad ||
1727 !mtd->block_markbad ||
1728 !mtd->read ||
1729 !mtd->write ||
1730 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1731 !mtd->read_oob || !mtd->write_oob) {
1732 #else
1733 !mtd->write_ecc ||
1734 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1735 #endif
1736 T(YAFFS_TRACE_ALWAYS,
1737 ("yaffs: MTD device does not support required "
1738 "functions\n"));;
1739 return NULL;
1740 }
1741
1742 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1743 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
1744 !options.inband_tags) {
1745 T(YAFFS_TRACE_ALWAYS,
1746 ("yaffs: MTD device does not have the "
1747 "right page sizes\n"));
1748 return NULL;
1749 }
1750 } else {
1751 /* Check for V1 style functions */
1752 if (!mtd->erase ||
1753 !mtd->read ||
1754 !mtd->write ||
1755 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1756 !mtd->read_oob || !mtd->write_oob) {
1757 #else
1758 !mtd->write_ecc ||
1759 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1760 #endif
1761 T(YAFFS_TRACE_ALWAYS,
1762 ("yaffs: MTD device does not support required "
1763 "functions\n"));;
1764 return NULL;
1765 }
1766
1767 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
1768 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1769 T(YAFFS_TRACE_ALWAYS,
1770 ("yaffs: MTD device does not support have the "
1771 "right page sizes\n"));
1772 return NULL;
1773 }
1774 }
1775
1776 /* OK, so if we got here, we have an MTD that's NAND and looks
1777 * like it has the right capabilities
1778 * Set the yaffs_Device up for mtd
1779 */
1780
1781 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1782 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1783 #else
1784 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1785 #endif
1786 if (!dev) {
1787 /* Deep shit could not allocate device structure */
1788 T(YAFFS_TRACE_ALWAYS,
1789 ("yaffs_read_super: Failed trying to allocate "
1790 "yaffs_Device. \n"));
1791 return NULL;
1792 }
1793
1794 memset(dev, 0, sizeof(yaffs_Device));
1795 dev->genericDevice = mtd;
1796 dev->name = mtd->name;
1797
1798 /* Set up the memory size parameters.... */
1799
1800 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1801 dev->startBlock = 0;
1802 dev->endBlock = nBlocks - 1;
1803 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1804 dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1805 dev->nReservedBlocks = 5;
1806 dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
1807 dev->inbandTags = options.inband_tags;
1808
1809 /* ... and the functions. */
1810 if (yaffsVersion == 2) {
1811 dev->writeChunkWithTagsToNAND =
1812 nandmtd2_WriteChunkWithTagsToNAND;
1813 dev->readChunkWithTagsFromNAND =
1814 nandmtd2_ReadChunkWithTagsFromNAND;
1815 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1816 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1817 dev->spareBuffer = YMALLOC(mtd->oobsize);
1818 dev->isYaffs2 = 1;
1819 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1820 dev->totalBytesPerChunk = mtd->writesize;
1821 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
1822 #else
1823 dev->totalBytesPerChunk = mtd->oobblock;
1824 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1825 #endif
1826 nBlocks = mtd->size / mtd->erasesize;
1827
1828 dev->startBlock = 0;
1829 dev->endBlock = nBlocks - 1;
1830 } else {
1831 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1832 /* use the MTD interface in yaffs_mtdif1.c */
1833 dev->writeChunkWithTagsToNAND =
1834 nandmtd1_WriteChunkWithTagsToNAND;
1835 dev->readChunkWithTagsFromNAND =
1836 nandmtd1_ReadChunkWithTagsFromNAND;
1837 dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
1838 dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
1839 #else
1840 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1841 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1842 #endif
1843 dev->isYaffs2 = 0;
1844 }
1845 /* ... and common functions */
1846 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1847 dev->initialiseNAND = nandmtd_InitialiseNAND;
1848
1849 dev->putSuperFunc = yaffs_MTDPutSuper;
1850
1851 dev->superBlock = (void *)sb;
1852 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
1853
1854
1855 #ifndef CONFIG_YAFFS_DOES_ECC
1856 dev->useNANDECC = 1;
1857 #endif
1858
1859 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1860 dev->wideTnodesDisabled = 1;
1861 #endif
1862
1863 dev->skipCheckpointRead = options.skip_checkpoint_read;
1864 dev->skipCheckpointWrite = options.skip_checkpoint_write;
1865
1866 /* we assume this is protected by lock_kernel() in mount/umount */
1867 list_add_tail(&dev->devList, &yaffs_dev_list);
1868
1869 init_MUTEX(&dev->grossLock);
1870
1871 yaffs_GrossLock(dev);
1872
1873 err = yaffs_GutsInitialise(dev);
1874
1875 T(YAFFS_TRACE_OS,
1876 ("yaffs_read_super: guts initialised %s\n",
1877 (err == YAFFS_OK) ? "OK" : "FAILED"));
1878
1879 /* Release lock before yaffs_get_inode() */
1880 yaffs_GrossUnlock(dev);
1881
1882 /* Create root inode */
1883 if (err == YAFFS_OK)
1884 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1885 yaffs_Root(dev));
1886
1887 if (!inode)
1888 return NULL;
1889
1890 inode->i_op = &yaffs_dir_inode_operations;
1891 inode->i_fop = &yaffs_dir_operations;
1892
1893 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1894
1895 root = d_alloc_root(inode);
1896
1897 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1898
1899 if (!root) {
1900 iput(inode);
1901 return NULL;
1902 }
1903 sb->s_root = root;
1904
1905 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1906 return sb;
1907 }
1908
1909
1910 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1911 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
1912 int silent)
1913 {
1914 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
1915 }
1916
1917 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1918 static int yaffs_read_super(struct file_system_type *fs,
1919 int flags, const char *dev_name,
1920 void *data, struct vfsmount *mnt)
1921 {
1922
1923 return get_sb_bdev(fs, flags, dev_name, data,
1924 yaffs_internal_read_super_mtd, mnt);
1925 }
1926 #else
1927 static struct super_block *yaffs_read_super(struct file_system_type *fs,
1928 int flags, const char *dev_name,
1929 void *data)
1930 {
1931
1932 return get_sb_bdev(fs, flags, dev_name, data,
1933 yaffs_internal_read_super_mtd);
1934 }
1935 #endif
1936
1937 static struct file_system_type yaffs_fs_type = {
1938 .owner = THIS_MODULE,
1939 .name = "yaffs",
1940 .get_sb = yaffs_read_super,
1941 .kill_sb = kill_block_super,
1942 .fs_flags = FS_REQUIRES_DEV,
1943 };
1944 #else
1945 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
1946 int silent)
1947 {
1948 return yaffs_internal_read_super(1, sb, data, silent);
1949 }
1950
1951 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
1952 FS_REQUIRES_DEV);
1953 #endif
1954
1955
1956 #ifdef CONFIG_YAFFS_YAFFS2
1957
1958 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1959 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
1960 int silent)
1961 {
1962 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
1963 }
1964
1965 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1966 static int yaffs2_read_super(struct file_system_type *fs,
1967 int flags, const char *dev_name, void *data,
1968 struct vfsmount *mnt)
1969 {
1970 return get_sb_bdev(fs, flags, dev_name, data,
1971 yaffs2_internal_read_super_mtd, mnt);
1972 }
1973 #else
1974 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
1975 int flags, const char *dev_name,
1976 void *data)
1977 {
1978
1979 return get_sb_bdev(fs, flags, dev_name, data,
1980 yaffs2_internal_read_super_mtd);
1981 }
1982 #endif
1983
1984 static struct file_system_type yaffs2_fs_type = {
1985 .owner = THIS_MODULE,
1986 .name = "yaffs2",
1987 .get_sb = yaffs2_read_super,
1988 .kill_sb = kill_block_super,
1989 .fs_flags = FS_REQUIRES_DEV,
1990 };
1991 #else
1992 static struct super_block *yaffs2_read_super(struct super_block *sb,
1993 void *data, int silent)
1994 {
1995 return yaffs_internal_read_super(2, sb, data, silent);
1996 }
1997
1998 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
1999 FS_REQUIRES_DEV);
2000 #endif
2001
2002 #endif /* CONFIG_YAFFS_YAFFS2 */
2003
2004 static struct proc_dir_entry *my_proc_entry;
2005
2006 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
2007 {
2008 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
2009 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
2010 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
2011 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2012 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2013 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2014 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2015 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
2016 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2017 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2018 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2019 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2020 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2021 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2022 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2023 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2024 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2025 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2026 buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2027 buf += sprintf(buf, "passiveGCs......... %d\n",
2028 dev->passiveGarbageCollections);
2029 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2030 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
2031 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2032 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2033 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2034 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2035 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2036 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2037 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2038 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2039 buf +=
2040 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2041 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
2042 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
2043 buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
2044
2045 return buf;
2046 }
2047
2048 static int yaffs_proc_read(char *page,
2049 char **start,
2050 off_t offset, int count, int *eof, void *data)
2051 {
2052 struct list_head *item;
2053 char *buf = page;
2054 int step = offset;
2055 int n = 0;
2056
2057 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2058 * We use 'offset' (*ppos) to indicate where we are in devList.
2059 * This also assumes the user has posted a read buffer large
2060 * enough to hold the complete output; but that's life in /proc.
2061 */
2062
2063 *(int *)start = 1;
2064
2065 /* Print header first */
2066 if (step == 0) {
2067 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2068 "\n%s\n%s\n", yaffs_fs_c_version,
2069 yaffs_guts_c_version);
2070 }
2071
2072 /* hold lock_kernel while traversing yaffs_dev_list */
2073 lock_kernel();
2074
2075 /* Locate and print the Nth entry. Order N-squared but N is small. */
2076 list_for_each(item, &yaffs_dev_list) {
2077 yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
2078 if (n < step) {
2079 n++;
2080 continue;
2081 }
2082 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
2083 buf = yaffs_dump_dev(buf, dev);
2084 break;
2085 }
2086 unlock_kernel();
2087
2088 return buf - page < count ? buf - page : count;
2089 }
2090
2091 /**
2092 * Set the verbosity of the warnings and error messages.
2093 *
2094 * Note that the names can only be a..z or _ with the current code.
2095 */
2096
2097 static struct {
2098 char *mask_name;
2099 unsigned mask_bitfield;
2100 } mask_flags[] = {
2101 {"allocate", YAFFS_TRACE_ALLOCATE},
2102 {"always", YAFFS_TRACE_ALWAYS},
2103 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2104 {"buffers", YAFFS_TRACE_BUFFERS},
2105 {"bug", YAFFS_TRACE_BUG},
2106 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2107 {"deletion", YAFFS_TRACE_DELETION},
2108 {"erase", YAFFS_TRACE_ERASE},
2109 {"error", YAFFS_TRACE_ERROR},
2110 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2111 {"gc", YAFFS_TRACE_GC},
2112 {"mtd", YAFFS_TRACE_MTD},
2113 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2114 {"os", YAFFS_TRACE_OS},
2115 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2116 {"scan", YAFFS_TRACE_SCAN},
2117 {"tracing", YAFFS_TRACE_TRACING},
2118
2119 {"verify", YAFFS_TRACE_VERIFY},
2120 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2121 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2122 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2123
2124 {"write", YAFFS_TRACE_WRITE},
2125 {"all", 0xffffffff},
2126 {"none", 0},
2127 {NULL, 0},
2128 };
2129
2130 #define MAX_MASK_NAME_LENGTH 40
2131 static int yaffs_proc_write(struct file *file, const char *buf,
2132 unsigned long count, void *data)
2133 {
2134 unsigned rg = 0, mask_bitfield;
2135 char *end;
2136 char *mask_name;
2137 const char *x;
2138 char substring[MAX_MASK_NAME_LENGTH+1];
2139 int i;
2140 int done = 0;
2141 int add, len = 0;
2142 int pos = 0;
2143
2144 rg = yaffs_traceMask;
2145
2146 while (!done && (pos < count)) {
2147 done = 1;
2148 while ((pos < count) && isspace(buf[pos])) {
2149 pos++;
2150 }
2151
2152 switch (buf[pos]) {
2153 case '+':
2154 case '-':
2155 case '=':
2156 add = buf[pos];
2157 pos++;
2158 break;
2159
2160 default:
2161 add = ' ';
2162 break;
2163 }
2164 mask_name = NULL;
2165
2166 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2167 if (end > buf + pos) {
2168 mask_name = "numeral";
2169 len = end - (buf + pos);
2170 pos += len;
2171 done = 0;
2172 } else {
2173 for(x = buf + pos, i = 0;
2174 (*x == '_' || (*x >='a' && *x <= 'z')) &&
2175 i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2176 substring[i] = *x;
2177 substring[i] = '\0';
2178
2179 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2180 if(strcmp(substring,mask_flags[i].mask_name) == 0){
2181 mask_name = mask_flags[i].mask_name;
2182 mask_bitfield = mask_flags[i].mask_bitfield;
2183 done = 0;
2184 break;
2185 }
2186 }
2187 }
2188
2189 if (mask_name != NULL) {
2190 done = 0;
2191 switch(add) {
2192 case '-':
2193 rg &= ~mask_bitfield;
2194 break;
2195 case '+':
2196 rg |= mask_bitfield;
2197 break;
2198 case '=':
2199 rg = mask_bitfield;
2200 break;
2201 default:
2202 rg |= mask_bitfield;
2203 break;
2204 }
2205 }
2206 }
2207
2208 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2209
2210 printk("new trace = 0x%08X\n",yaffs_traceMask);
2211
2212 if (rg & YAFFS_TRACE_ALWAYS) {
2213 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2214 char flag;
2215 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2216 printk("%c%s\n", flag, mask_flags[i].mask_name);
2217 }
2218 }
2219
2220 return count;
2221 }
2222
2223 /* Stuff to handle installation of file systems */
2224 struct file_system_to_install {
2225 struct file_system_type *fst;
2226 int installed;
2227 };
2228
2229 static struct file_system_to_install fs_to_install[] = {
2230 //#ifdef CONFIG_YAFFS_YAFFS1
2231 {&yaffs_fs_type, 0},
2232 //#endif
2233 //#ifdef CONFIG_YAFFS_YAFFS2
2234 {&yaffs2_fs_type, 0},
2235 //#endif
2236 {NULL, 0}
2237 };
2238
2239 static int __init init_yaffs_fs(void)
2240 {
2241 int error = 0;
2242 struct file_system_to_install *fsinst;
2243
2244 T(YAFFS_TRACE_ALWAYS,
2245 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2246
2247 /* Install the proc_fs entry */
2248 my_proc_entry = create_proc_entry("yaffs",
2249 S_IRUGO | S_IFREG,
2250 &proc_root);
2251
2252 if (my_proc_entry) {
2253 my_proc_entry->write_proc = yaffs_proc_write;
2254 my_proc_entry->read_proc = yaffs_proc_read;
2255 my_proc_entry->data = NULL;
2256 } else {
2257 return -ENOMEM;
2258 }
2259
2260 /* Now add the file system entries */
2261
2262 fsinst = fs_to_install;
2263
2264 while (fsinst->fst && !error) {
2265 error = register_filesystem(fsinst->fst);
2266 if (!error) {
2267 fsinst->installed = 1;
2268 }
2269 fsinst++;
2270 }
2271
2272 /* Any errors? uninstall */
2273 if (error) {
2274 fsinst = fs_to_install;
2275
2276 while (fsinst->fst) {
2277 if (fsinst->installed) {
2278 unregister_filesystem(fsinst->fst);
2279 fsinst->installed = 0;
2280 }
2281 fsinst++;
2282 }
2283 }
2284
2285 return error;
2286 }
2287
2288 static void __exit exit_yaffs_fs(void)
2289 {
2290
2291 struct file_system_to_install *fsinst;
2292
2293 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2294 " removing. \n"));
2295
2296 remove_proc_entry("yaffs", &proc_root);
2297
2298 fsinst = fs_to_install;
2299
2300 while (fsinst->fst) {
2301 if (fsinst->installed) {
2302 unregister_filesystem(fsinst->fst);
2303 fsinst->installed = 0;
2304 }
2305 fsinst++;
2306 }
2307
2308 }
2309
2310 module_init(init_yaffs_fs)
2311 module_exit(exit_yaffs_fs)
2312
2313 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2314 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2315 MODULE_LICENSE("GPL");