blob: d5b875314005a36afe115ba7abd656bf6b3eff39 [file] [log] [blame]
Arve Hjønnevåg3de69a72010-05-18 20:35:30 -07001/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 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 struct yaffs_dev associated with
28 * this superblock
29 * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
30 * superblock
31 * >> inode->u.generic_ip points to the associated struct yaffs_obj.
32 */
33
34/*
35 * NB There are two variants of Linux VFS glue code. This variant supports
36 * a single version and should not include any multi-version code.
37 */
38#include <linux/version.h>
39
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/slab.h>
43#include <linux/init.h>
44#include <linux/fs.h>
45#include <linux/proc_fs.h>
46#include <linux/smp_lock.h>
47#include <linux/pagemap.h>
48#include <linux/mtd/mtd.h>
49#include <linux/interrupt.h>
50#include <linux/string.h>
51#include <linux/ctype.h>
52#include <linux/namei.h>
53#include <linux/exportfs.h>
54#include <linux/kthread.h>
55#include <linux/delay.h>
56#include <linux/freezer.h>
57
58#include <asm/div64.h>
59
60#include <linux/statfs.h>
61
62#define UnlockPage(p) unlock_page(p)
63#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
64
65#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
66
67#define YPROC_ROOT NULL
68
69#define Y_INIT_TIMER(a) init_timer_on_stack(a)
70
71#define WRITE_SIZE_STR "writesize"
72#define WRITE_SIZE(mtd) ((mtd)->writesize)
73
74static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
75{
76 uint64_t result = partition_size;
77 do_div(result, block_size);
78 return (uint32_t) result;
79}
80
81#include <linux/uaccess.h>
82#include <linux/mtd/mtd.h>
83
84#include "yportenv.h"
85#include "yaffs_trace.h"
86#include "yaffs_guts.h"
87#include "yaffs_attribs.h"
88
89#include "yaffs_linux.h"
90
91#include "yaffs_mtdif.h"
92#include "yaffs_mtdif1.h"
93#include "yaffs_mtdif2.h"
94
95unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
96unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
97unsigned int yaffs_auto_checkpoint = 1;
98unsigned int yaffs_gc_control = 1;
99unsigned int yaffs_bg_enable = 1;
100
101/* Module Parameters */
102module_param(yaffs_trace_mask, uint, 0644);
103module_param(yaffs_wr_attempts, uint, 0644);
104module_param(yaffs_auto_checkpoint, uint, 0644);
105module_param(yaffs_gc_control, uint, 0644);
106module_param(yaffs_bg_enable, uint, 0644);
107
108
109#define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
110#define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
111#define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
112#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
113
114#define update_dir_time(dir) do {\
115 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
116 } while(0)
117
118
119static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
120{
121 return yaffs_gc_control;
122}
123
124static void yaffs_gross_lock(struct yaffs_dev *dev)
125{
126 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
127 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
128 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
129}
130
131static void yaffs_gross_unlock(struct yaffs_dev *dev)
132{
133 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
134 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
135}
136
137static void yaffs_fill_inode_from_obj(struct inode *inode,
138 struct yaffs_obj *obj);
139
140static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
141{
142 struct inode *inode;
143 struct yaffs_obj *obj;
144 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
145
146 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
147
148 inode = iget_locked(sb, ino);
149 if (!inode)
150 return ERR_PTR(-ENOMEM);
151 if (!(inode->i_state & I_NEW))
152 return inode;
153
154 /* NB This is called as a side effect of other functions, but
155 * we had to release the lock to prevent deadlocks, so
156 * need to lock again.
157 */
158
159 yaffs_gross_lock(dev);
160
161 obj = yaffs_find_by_number(dev, inode->i_ino);
162
163 yaffs_fill_inode_from_obj(inode, obj);
164
165 yaffs_gross_unlock(dev);
166
167 unlock_new_inode(inode);
168 return inode;
169}
170
171struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
172 struct yaffs_obj *obj)
173{
174 struct inode *inode;
175
176 if (!sb) {
177 yaffs_trace(YAFFS_TRACE_OS,
178 "yaffs_get_inode for NULL super_block!!");
179 return NULL;
180
181 }
182
183 if (!obj) {
184 yaffs_trace(YAFFS_TRACE_OS,
185 "yaffs_get_inode for NULL object!!");
186 return NULL;
187
188 }
189
190 yaffs_trace(YAFFS_TRACE_OS,
191 "yaffs_get_inode for object %d",
192 obj->obj_id);
193
194 inode = yaffs_iget(sb, obj->obj_id);
195 if (IS_ERR(inode))
196 return NULL;
197
198 /* NB Side effect: iget calls back to yaffs_read_inode(). */
199 /* iget also increments the inode's i_count */
200 /* NB You can't be holding gross_lock or deadlock will happen! */
201
202 return inode;
203}
204
205static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
206 dev_t rdev)
207{
208 struct inode *inode;
209
210 struct yaffs_obj *obj = NULL;
211 struct yaffs_dev *dev;
212
213 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
214
215 int error = -ENOSPC;
216 uid_t uid = current->cred->fsuid;
217 gid_t gid =
218 (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
219
220 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
221 mode |= S_ISGID;
222
223 if (parent) {
224 yaffs_trace(YAFFS_TRACE_OS,
225 "yaffs_mknod: parent object %d type %d",
226 parent->obj_id, parent->variant_type);
227 } else {
228 yaffs_trace(YAFFS_TRACE_OS,
229 "yaffs_mknod: could not get parent object");
230 return -EPERM;
231 }
232
233 yaffs_trace(YAFFS_TRACE_OS,
234 "yaffs_mknod: making oject for %s, mode %x dev %x",
235 dentry->d_name.name, mode, rdev);
236
237 dev = parent->my_dev;
238
239 yaffs_gross_lock(dev);
240
241 switch (mode & S_IFMT) {
242 default:
243 /* Special (socket, fifo, device...) */
244 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
245 obj =
246 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
247 gid, old_encode_dev(rdev));
248 break;
249 case S_IFREG: /* file */
250 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
251 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
252 gid);
253 break;
254 case S_IFDIR: /* directory */
255 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
256 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
257 uid, gid);
258 break;
259 case S_IFLNK: /* symlink */
260 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
261 obj = NULL; /* Do we ever get here? */
262 break;
263 }
264
265 /* Can not call yaffs_get_inode() with gross lock held */
266 yaffs_gross_unlock(dev);
267
268 if (obj) {
269 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
270 d_instantiate(dentry, inode);
271 update_dir_time(dir);
272 yaffs_trace(YAFFS_TRACE_OS,
273 "yaffs_mknod created object %d count = %d",
274 obj->obj_id, atomic_read(&inode->i_count));
275 error = 0;
276 yaffs_fill_inode_from_obj(dir, parent);
277 } else {
278 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
279 error = -ENOMEM;
280 }
281
282 return error;
283}
284
285static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
286{
287 return yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
288}
289
290static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
291 struct nameidata *n)
292{
293 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
294}
295
296static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
297 struct dentry *dentry)
298{
299 struct inode *inode = old_dentry->d_inode;
300 struct yaffs_obj *obj = NULL;
301 struct yaffs_obj *link = NULL;
302 struct yaffs_dev *dev;
303
304 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
305
306 obj = yaffs_inode_to_obj(inode);
307 dev = obj->my_dev;
308
309 yaffs_gross_lock(dev);
310
311 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
312 link =
313 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
314 obj);
315
316 if (link) {
317 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
318 d_instantiate(dentry, old_dentry->d_inode);
319 atomic_inc(&old_dentry->d_inode->i_count);
320 yaffs_trace(YAFFS_TRACE_OS,
321 "yaffs_link link count %d i_count %d",
322 old_dentry->d_inode->i_nlink,
323 atomic_read(&old_dentry->d_inode->i_count));
324 }
325
326 yaffs_gross_unlock(dev);
327
328 if (link) {
329 update_dir_time(dir);
330 return 0;
331 }
332
333 return -EPERM;
334}
335
336static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
337 const char *symname)
338{
339 struct yaffs_obj *obj;
340 struct yaffs_dev *dev;
341 uid_t uid = current->cred->fsuid;
342 gid_t gid =
343 (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
344
345 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
346
347 dev = yaffs_inode_to_obj(dir)->my_dev;
348 yaffs_gross_lock(dev);
349 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
350 S_IFLNK | S_IRWXUGO, uid, gid, symname);
351 yaffs_gross_unlock(dev);
352
353 if (obj) {
354 struct inode *inode;
355
356 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
357 d_instantiate(dentry, inode);
358 update_dir_time(dir);
359 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
360 return 0;
361 } else {
362 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
363 }
364
365 return -ENOMEM;
366}
367
368static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
369 struct nameidata *n)
370{
371 struct yaffs_obj *obj;
372 struct inode *inode = NULL;
373
374 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
375
376 if (current != yaffs_dev_to_lc(dev)->readdir_process)
377 yaffs_gross_lock(dev);
378
379 yaffs_trace(YAFFS_TRACE_OS,
380 "yaffs_lookup for %d:%s",
381 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
382
383 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
384
385 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
386
387 /* Can't hold gross lock when calling yaffs_get_inode() */
388 if (current != yaffs_dev_to_lc(dev)->readdir_process)
389 yaffs_gross_unlock(dev);
390
391 if (obj) {
392 yaffs_trace(YAFFS_TRACE_OS,
393 "yaffs_lookup found %d", obj->obj_id);
394
395 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
396
397 if (inode) {
398 yaffs_trace(YAFFS_TRACE_OS, "yaffs_loookup dentry");
399 d_add(dentry, inode);
400 /* return dentry; */
401 return NULL;
402 }
403
404 } else {
405 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
406
407 }
408
409 d_add(dentry, inode);
410
411 return NULL;
412}
413
414static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
415{
416 int ret_val;
417
418 struct yaffs_dev *dev;
419 struct yaffs_obj *obj;
420
421 yaffs_trace(YAFFS_TRACE_OS,
422 "yaffs_unlink %d:%s",
423 (int)(dir->i_ino), dentry->d_name.name);
424 obj = yaffs_inode_to_obj(dir);
425 dev = obj->my_dev;
426
427 yaffs_gross_lock(dev);
428
429 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
430
431 if (ret_val == YAFFS_OK) {
432 dentry->d_inode->i_nlink--;
433 dir->i_version++;
434 yaffs_gross_unlock(dev);
435 mark_inode_dirty(dentry->d_inode);
436 update_dir_time(dir);
437 return 0;
438 }
439 yaffs_gross_unlock(dev);
440 return -ENOTEMPTY;
441}
442
443static int yaffs_sync_object(struct file *file, int datasync)
444{
445
446 struct yaffs_obj *obj;
447 struct yaffs_dev *dev;
448 struct dentry *dentry = file->f_path.dentry;
449
450 obj = yaffs_dentry_to_obj(dentry);
451
452 dev = obj->my_dev;
453
454 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, "yaffs_sync_object");
455 yaffs_gross_lock(dev);
456 yaffs_flush_file(obj, 1, datasync);
457 yaffs_gross_unlock(dev);
458 return 0;
459}
460/*
461 * The VFS layer already does all the dentry stuff for rename.
462 *
463 * NB: POSIX says you can rename an object over an old object of the same name
464 */
465static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
466 struct inode *new_dir, struct dentry *new_dentry)
467{
468 struct yaffs_dev *dev;
469 int ret_val = YAFFS_FAIL;
470 struct yaffs_obj *target;
471
472 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
473 dev = yaffs_inode_to_obj(old_dir)->my_dev;
474
475 yaffs_gross_lock(dev);
476
477 /* Check if the target is an existing directory that is not empty. */
478 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
479 new_dentry->d_name.name);
480
481 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
482 !list_empty(&target->variant.dir_variant.children)) {
483
484 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
485
486 ret_val = YAFFS_FAIL;
487 } else {
488 /* Now does unlinking internally using shadowing mechanism */
489 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
490
491 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
492 old_dentry->d_name.name,
493 yaffs_inode_to_obj(new_dir),
494 new_dentry->d_name.name);
495 }
496 yaffs_gross_unlock(dev);
497
498 if (ret_val == YAFFS_OK) {
499 if (target) {
500 new_dentry->d_inode->i_nlink--;
501 mark_inode_dirty(new_dentry->d_inode);
502 }
503
504 update_dir_time(old_dir);
505 if (old_dir != new_dir)
506 update_dir_time(new_dir);
507 return 0;
508 } else {
509 return -ENOTEMPTY;
510 }
511}
512
513static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
514{
515 struct inode *inode = dentry->d_inode;
516 int error = 0;
517 struct yaffs_dev *dev;
518
519 yaffs_trace(YAFFS_TRACE_OS,
520 "yaffs_setattr of object %d",
521 yaffs_inode_to_obj(inode)->obj_id);
522
523 /* Fail if a requested resize >= 2GB */
524 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
525 error = -EINVAL;
526
527 if (error == 0)
528 error = inode_change_ok(inode, attr);
529 if (error == 0) {
530 int result;
531 if (!error) {
532 setattr_copy(inode, attr);
533 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
534 if (attr->ia_valid & ATTR_SIZE) {
535 truncate_setsize(inode, attr->ia_size);
536 inode->i_blocks = (inode->i_size + 511) >> 9;
537 }
538 }
539 dev = yaffs_inode_to_obj(inode)->my_dev;
540 if (attr->ia_valid & ATTR_SIZE) {
541 yaffs_trace(YAFFS_TRACE_OS, "resize to %d(%x)",
542 (int)(attr->ia_size),
543 (int)(attr->ia_size));
544 }
545 yaffs_gross_lock(dev);
546 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
547 if (result == YAFFS_OK) {
548 error = 0;
549 } else {
550 error = -EPERM;
551 }
552 yaffs_gross_unlock(dev);
553
554 }
555
556 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
557
558 return error;
559}
560
561#ifdef CONFIG_YAFFS_XATTR
562static int yaffs_setxattr(struct dentry *dentry, const char *name,
563 const void *value, size_t size, int flags)
564{
565 struct inode *inode = dentry->d_inode;
566 int error = 0;
567 struct yaffs_dev *dev;
568 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
569
570 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
571
572 if (error == 0) {
573 int result;
574 dev = obj->my_dev;
575 yaffs_gross_lock(dev);
576 result = yaffs_set_xattrib(obj, name, value, size, flags);
577 if (result == YAFFS_OK)
578 error = 0;
579 else if (result < 0)
580 error = result;
581 yaffs_gross_unlock(dev);
582
583 }
584 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
585
586 return error;
587}
588
589static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, void *buff,
590 size_t size)
591{
592 struct inode *inode = dentry->d_inode;
593 int error = 0;
594 struct yaffs_dev *dev;
595 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
596
597 yaffs_trace(YAFFS_TRACE_OS,
598 "yaffs_getxattr \"%s\" from object %d",
599 name, obj->obj_id);
600
601 if (error == 0) {
602 dev = obj->my_dev;
603 yaffs_gross_lock(dev);
604 error = yaffs_get_xattrib(obj, name, buff, size);
605 yaffs_gross_unlock(dev);
606
607 }
608 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
609
610 return error;
611}
612
613static int yaffs_removexattr(struct dentry *dentry, const char *name)
614{
615 struct inode *inode = dentry->d_inode;
616 int error = 0;
617 struct yaffs_dev *dev;
618 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
619
620 yaffs_trace(YAFFS_TRACE_OS,
621 "yaffs_removexattr of object %d", obj->obj_id);
622
623 if (error == 0) {
624 int result;
625 dev = obj->my_dev;
626 yaffs_gross_lock(dev);
627 result = yaffs_remove_xattrib(obj, name);
628 if (result == YAFFS_OK)
629 error = 0;
630 else if (result < 0)
631 error = result;
632 yaffs_gross_unlock(dev);
633
634 }
635 yaffs_trace(YAFFS_TRACE_OS,
636 "yaffs_removexattr done returning %d", error);
637
638 return error;
639}
640
641static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
642{
643 struct inode *inode = dentry->d_inode;
644 int error = 0;
645 struct yaffs_dev *dev;
646 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
647
648 yaffs_trace(YAFFS_TRACE_OS,
649 "yaffs_listxattr of object %d", obj->obj_id);
650
651 if (error == 0) {
652 dev = obj->my_dev;
653 yaffs_gross_lock(dev);
654 error = yaffs_list_xattrib(obj, buff, size);
655 yaffs_gross_unlock(dev);
656
657 }
658 yaffs_trace(YAFFS_TRACE_OS,
659 "yaffs_listxattr done returning %d", error);
660
661 return error;
662}
663
664#endif
665
666static const struct inode_operations yaffs_dir_inode_operations = {
667 .create = yaffs_create,
668 .lookup = yaffs_lookup,
669 .link = yaffs_link,
670 .unlink = yaffs_unlink,
671 .symlink = yaffs_symlink,
672 .mkdir = yaffs_mkdir,
673 .rmdir = yaffs_unlink,
674 .mknod = yaffs_mknod,
675 .rename = yaffs_rename,
676 .setattr = yaffs_setattr,
677#ifdef CONFIG_YAFFS_XATTR
678 .setxattr = yaffs_setxattr,
679 .getxattr = yaffs_getxattr,
680 .listxattr = yaffs_listxattr,
681 .removexattr = yaffs_removexattr,
682#endif
683};
684/*-----------------------------------------------------------------*/
685/* Directory search context allows us to unlock access to yaffs during
686 * filldir without causing problems with the directory being modified.
687 * This is similar to the tried and tested mechanism used in yaffs direct.
688 *
689 * A search context iterates along a doubly linked list of siblings in the
690 * directory. If the iterating object is deleted then this would corrupt
691 * the list iteration, likely causing a crash. The search context avoids
692 * this by using the remove_obj_fn to move the search context to the
693 * next object before the object is deleted.
694 *
695 * Many readdirs (and thus seach conexts) may be alive simulateously so
696 * each struct yaffs_dev has a list of these.
697 *
698 * A seach context lives for the duration of a readdir.
699 *
700 * All these functions must be called while yaffs is locked.
701 */
702
703struct yaffs_search_context {
704 struct yaffs_dev *dev;
705 struct yaffs_obj *dir_obj;
706 struct yaffs_obj *next_return;
707 struct list_head others;
708};
709
710/*
711 * yaffs_new_search() creates a new search context, initialises it and
712 * adds it to the device's search context list.
713 *
714 * Called at start of readdir.
715 */
716static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
717{
718 struct yaffs_dev *dev = dir->my_dev;
719 struct yaffs_search_context *sc =
720 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
721 if (sc) {
722 sc->dir_obj = dir;
723 sc->dev = dev;
724 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
725 sc->next_return = NULL;
726 else
727 sc->next_return =
728 list_entry(dir->variant.dir_variant.children.next,
729 struct yaffs_obj, siblings);
730 INIT_LIST_HEAD(&sc->others);
731 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
732 }
733 return sc;
734}
735
736/*
737 * yaffs_search_end() disposes of a search context and cleans up.
738 */
739static void yaffs_search_end(struct yaffs_search_context *sc)
740{
741 if (sc) {
742 list_del(&sc->others);
743 kfree(sc);
744 }
745}
746
747/*
748 * yaffs_search_advance() moves a search context to the next object.
749 * Called when the search iterates or when an object removal causes
750 * the search context to be moved to the next object.
751 */
752static void yaffs_search_advance(struct yaffs_search_context *sc)
753{
754 if (!sc)
755 return;
756
757 if (sc->next_return == NULL ||
758 list_empty(&sc->dir_obj->variant.dir_variant.children))
759 sc->next_return = NULL;
760 else {
761 struct list_head *next = sc->next_return->siblings.next;
762
763 if (next == &sc->dir_obj->variant.dir_variant.children)
764 sc->next_return = NULL; /* end of list */
765 else
766 sc->next_return =
767 list_entry(next, struct yaffs_obj, siblings);
768 }
769}
770
771/*
772 * yaffs_remove_obj_callback() is called when an object is unlinked.
773 * We check open search contexts and advance any which are currently
774 * on the object being iterated.
775 */
776static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
777{
778
779 struct list_head *i;
780 struct yaffs_search_context *sc;
781 struct list_head *search_contexts =
782 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
783
784 /* Iterate through the directory search contexts.
785 * If any are currently on the object being removed, then advance
786 * the search context to the next object to prevent a hanging pointer.
787 */
788 list_for_each(i, search_contexts) {
789 if (i) {
790 sc = list_entry(i, struct yaffs_search_context, others);
791 if (sc->next_return == obj)
792 yaffs_search_advance(sc);
793 }
794 }
795
796}
797
798static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
799{
800 struct yaffs_obj *obj;
801 struct yaffs_dev *dev;
802 struct yaffs_search_context *sc;
803 struct inode *inode = f->f_dentry->d_inode;
804 unsigned long offset, curoffs;
805 struct yaffs_obj *l;
806 int ret_val = 0;
807
808 char name[YAFFS_MAX_NAME_LENGTH + 1];
809
810 obj = yaffs_dentry_to_obj(f->f_dentry);
811 dev = obj->my_dev;
812
813 yaffs_gross_lock(dev);
814
815 yaffs_dev_to_lc(dev)->readdir_process = current;
816
817 offset = f->f_pos;
818
819 sc = yaffs_new_search(obj);
820 if (!sc) {
821 ret_val = -ENOMEM;
822 goto out;
823 }
824
825 yaffs_trace(YAFFS_TRACE_OS,
826 "yaffs_readdir: starting at %d", (int)offset);
827
828 if (offset == 0) {
829 yaffs_trace(YAFFS_TRACE_OS,
830 "yaffs_readdir: entry . ino %d",
831 (int)inode->i_ino);
832 yaffs_gross_unlock(dev);
833 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
834 yaffs_gross_lock(dev);
835 goto out;
836 }
837 yaffs_gross_lock(dev);
838 offset++;
839 f->f_pos++;
840 }
841 if (offset == 1) {
842 yaffs_trace(YAFFS_TRACE_OS,
843 "yaffs_readdir: entry .. ino %d",
844 (int)f->f_dentry->d_parent->d_inode->i_ino);
845 yaffs_gross_unlock(dev);
846 if (filldir(dirent, "..", 2, offset,
847 f->f_dentry->d_parent->d_inode->i_ino,
848 DT_DIR) < 0) {
849 yaffs_gross_lock(dev);
850 goto out;
851 }
852 yaffs_gross_lock(dev);
853 offset++;
854 f->f_pos++;
855 }
856
857 curoffs = 1;
858
859 /* If the directory has changed since the open or last call to
860 readdir, rewind to after the 2 canned entries. */
861 if (f->f_version != inode->i_version) {
862 offset = 2;
863 f->f_pos = offset;
864 f->f_version = inode->i_version;
865 }
866
867 while (sc->next_return) {
868 curoffs++;
869 l = sc->next_return;
870 if (curoffs >= offset) {
871 int this_inode = yaffs_get_obj_inode(l);
872 int this_type = yaffs_get_obj_type(l);
873
874 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
875 yaffs_trace(YAFFS_TRACE_OS,
876 "yaffs_readdir: %s inode %d",
877 name, yaffs_get_obj_inode(l));
878
879 yaffs_gross_unlock(dev);
880
881 if (filldir(dirent,
882 name,
883 strlen(name),
884 offset, this_inode, this_type) < 0) {
885 yaffs_gross_lock(dev);
886 goto out;
887 }
888
889 yaffs_gross_lock(dev);
890
891 offset++;
892 f->f_pos++;
893 }
894 yaffs_search_advance(sc);
895 }
896
897out:
898 yaffs_search_end(sc);
899 yaffs_dev_to_lc(dev)->readdir_process = NULL;
900 yaffs_gross_unlock(dev);
901
902 return ret_val;
903}
904
905static const struct file_operations yaffs_dir_operations = {
906 .read = generic_read_dir,
907 .readdir = yaffs_readdir,
908 .fsync = yaffs_sync_object,
909 .llseek = generic_file_llseek,
910};
911
912
913
914static int yaffs_file_flush(struct file *file, fl_owner_t id)
915{
916 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
917
918 struct yaffs_dev *dev = obj->my_dev;
919
920 yaffs_trace(YAFFS_TRACE_OS,
921 "yaffs_file_flush object %d (%s)",
922 obj->obj_id, obj->dirty ? "dirty" : "clean");
923
924 yaffs_gross_lock(dev);
925
926 yaffs_flush_file(obj, 1, 0);
927
928 yaffs_gross_unlock(dev);
929
930 return 0;
931}
932
933static const struct file_operations yaffs_file_operations = {
934 .read = do_sync_read,
935 .write = do_sync_write,
936 .aio_read = generic_file_aio_read,
937 .aio_write = generic_file_aio_write,
938 .mmap = generic_file_mmap,
939 .flush = yaffs_file_flush,
940 .fsync = yaffs_sync_object,
941 .splice_read = generic_file_splice_read,
942 .splice_write = generic_file_splice_write,
943 .llseek = generic_file_llseek,
944};
945
946
947/* ExportFS support */
948static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
949 uint32_t generation)
950{
951 return yaffs_iget(sb, ino);
952}
953
954static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
955 struct fid *fid, int fh_len,
956 int fh_type)
957{
958 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
959 yaffs2_nfs_get_inode);
960}
961
962static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
963 struct fid *fid, int fh_len,
964 int fh_type)
965{
966 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
967 yaffs2_nfs_get_inode);
968}
969
970struct dentry *yaffs2_get_parent(struct dentry *dentry)
971{
972
973 struct super_block *sb = dentry->d_inode->i_sb;
974 struct dentry *parent = ERR_PTR(-ENOENT);
975 struct inode *inode;
976 unsigned long parent_ino;
977 struct yaffs_obj *d_obj;
978 struct yaffs_obj *parent_obj;
979
980 d_obj = yaffs_inode_to_obj(dentry->d_inode);
981
982 if (d_obj) {
983 parent_obj = d_obj->parent;
984 if (parent_obj) {
985 parent_ino = yaffs_get_obj_inode(parent_obj);
986 inode = yaffs_iget(sb, parent_ino);
987
988 if (IS_ERR(inode)) {
989 parent = ERR_CAST(inode);
990 } else {
991 parent = d_obtain_alias(inode);
992 if (!IS_ERR(parent)) {
993 parent = ERR_PTR(-ENOMEM);
994 iput(inode);
995 }
996 }
997 }
998 }
999
1000 return parent;
1001}
1002
1003/* Just declare a zero structure as a NULL value implies
1004 * using the default functions of exportfs.
1005 */
1006
1007static struct export_operations yaffs_export_ops = {
1008 .fh_to_dentry = yaffs2_fh_to_dentry,
1009 .fh_to_parent = yaffs2_fh_to_parent,
1010 .get_parent = yaffs2_get_parent,
1011};
1012
1013
1014/*-----------------------------------------------------------------*/
1015
1016static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1017 int buflen)
1018{
1019 unsigned char *alias;
1020 int ret;
1021
1022 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1023
1024 yaffs_gross_lock(dev);
1025
1026 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1027
1028 yaffs_gross_unlock(dev);
1029
1030 if (!alias)
1031 return -ENOMEM;
1032
1033 ret = vfs_readlink(dentry, buffer, buflen, alias);
1034 kfree(alias);
1035 return ret;
1036}
1037
1038static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1039{
1040 unsigned char *alias;
1041 void *ret;
1042 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1043
1044 yaffs_gross_lock(dev);
1045
1046 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1047 yaffs_gross_unlock(dev);
1048
1049 if (!alias) {
1050 ret = ERR_PTR(-ENOMEM);
1051 goto out;
1052 }
1053
1054 nd_set_link(nd, alias);
1055 ret = (void *)alias;
1056out:
1057 return ret;
1058}
1059
1060void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
1061{
1062 kfree(alias);
1063}
1064
1065
1066static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
1067{
1068 /* Clear the association between the inode and
1069 * the struct yaffs_obj.
1070 */
1071 obj->my_inode = NULL;
1072 yaffs_inode_to_obj_lv(inode) = NULL;
1073
1074 /* If the object freeing was deferred, then the real
1075 * free happens now.
1076 * This should fix the inode inconsistency problem.
1077 */
1078 yaffs_handle_defered_free(obj);
1079}
1080
1081/* yaffs_evict_inode combines into one operation what was previously done in
1082 * yaffs_clear_inode() and yaffs_delete_inode()
1083 *
1084 */
1085static void yaffs_evict_inode(struct inode *inode)
1086{
1087 struct yaffs_obj *obj;
1088 struct yaffs_dev *dev;
1089 int deleteme = 0;
1090
1091 obj = yaffs_inode_to_obj(inode);
1092
1093 yaffs_trace(YAFFS_TRACE_OS,
1094 "yaffs_evict_inode: ino %d, count %d %s",
1095 (int)inode->i_ino,
1096 atomic_read(&inode->i_count),
1097 obj ? "object exists" : "null object");
1098
1099 if (!inode->i_nlink && !is_bad_inode(inode))
1100 deleteme = 1;
1101 truncate_inode_pages(&inode->i_data, 0);
1102 end_writeback(inode);
1103
1104 if (deleteme && obj) {
1105 dev = obj->my_dev;
1106 yaffs_gross_lock(dev);
1107 yaffs_del_obj(obj);
1108 yaffs_gross_unlock(dev);
1109 }
1110 if (obj) {
1111 dev = obj->my_dev;
1112 yaffs_gross_lock(dev);
1113 yaffs_unstitch_obj(inode, obj);
1114 yaffs_gross_unlock(dev);
1115 }
1116
1117}
1118
1119static void yaffs_touch_super(struct yaffs_dev *dev)
1120{
1121 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
1122
1123 yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb);
1124 if (sb)
1125 sb->s_dirt = 1;
1126}
1127
1128static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1129{
1130 /* Lifted from jffs2 */
1131
1132 struct yaffs_obj *obj;
1133 unsigned char *pg_buf;
1134 int ret;
1135
1136 struct yaffs_dev *dev;
1137
1138 yaffs_trace(YAFFS_TRACE_OS,
1139 "yaffs_readpage_nolock at %08x, size %08x",
1140 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1141 (unsigned)PAGE_CACHE_SIZE);
1142
1143 obj = yaffs_dentry_to_obj(f->f_dentry);
1144
1145 dev = obj->my_dev;
1146
1147 BUG_ON(!PageLocked(pg));
1148
1149 pg_buf = kmap(pg);
1150 /* FIXME: Can kmap fail? */
1151
1152 yaffs_gross_lock(dev);
1153
1154 ret = yaffs_file_rd(obj, pg_buf,
1155 pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
1156
1157 yaffs_gross_unlock(dev);
1158
1159 if (ret >= 0)
1160 ret = 0;
1161
1162 if (ret) {
1163 ClearPageUptodate(pg);
1164 SetPageError(pg);
1165 } else {
1166 SetPageUptodate(pg);
1167 ClearPageError(pg);
1168 }
1169
1170 flush_dcache_page(pg);
1171 kunmap(pg);
1172
1173 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
1174 return ret;
1175}
1176
1177static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1178{
1179 int ret = yaffs_readpage_nolock(f, pg);
1180 UnlockPage(pg);
1181 return ret;
1182}
1183
1184static int yaffs_readpage(struct file *f, struct page *pg)
1185{
1186 int ret;
1187
1188 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
1189 ret = yaffs_readpage_unlock(f, pg);
1190 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
1191 return ret;
1192}
1193
1194/* writepage inspired by/stolen from smbfs */
1195
1196static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1197{
1198 struct yaffs_dev *dev;
1199 struct address_space *mapping = page->mapping;
1200 struct inode *inode;
1201 unsigned long end_index;
1202 char *buffer;
1203 struct yaffs_obj *obj;
1204 int n_written = 0;
1205 unsigned n_bytes;
1206 loff_t i_size;
1207
1208 if (!mapping)
1209 BUG();
1210 inode = mapping->host;
1211 if (!inode)
1212 BUG();
1213 i_size = i_size_read(inode);
1214
1215 end_index = i_size >> PAGE_CACHE_SHIFT;
1216
1217 if (page->index < end_index)
1218 n_bytes = PAGE_CACHE_SIZE;
1219 else {
1220 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
1221
1222 if (page->index > end_index || !n_bytes) {
1223 yaffs_trace(YAFFS_TRACE_OS,
1224 "yaffs_writepage at %08x, inode size = %08x!!!",
1225 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1226 (unsigned)inode->i_size);
1227 yaffs_trace(YAFFS_TRACE_OS,
1228 " -> don't care!!");
1229
1230 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1231 set_page_writeback(page);
1232 unlock_page(page);
1233 end_page_writeback(page);
1234 return 0;
1235 }
1236 }
1237
1238 if (n_bytes != PAGE_CACHE_SIZE)
1239 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
1240
1241 get_page(page);
1242
1243 buffer = kmap(page);
1244
1245 obj = yaffs_inode_to_obj(inode);
1246 dev = obj->my_dev;
1247 yaffs_gross_lock(dev);
1248
1249 yaffs_trace(YAFFS_TRACE_OS,
1250 "yaffs_writepage at %08x, size %08x",
1251 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes);
1252 yaffs_trace(YAFFS_TRACE_OS,
1253 "writepag0: obj = %05x, ino = %05x",
1254 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1255
1256 n_written = yaffs_wr_file(obj, buffer,
1257 page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
1258
1259 yaffs_touch_super(dev);
1260
1261 yaffs_trace(YAFFS_TRACE_OS,
1262 "writepag1: obj = %05x, ino = %05x",
1263 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1264
1265 yaffs_gross_unlock(dev);
1266
1267 kunmap(page);
1268 set_page_writeback(page);
1269 unlock_page(page);
1270 end_page_writeback(page);
1271 put_page(page);
1272
1273 return (n_written == n_bytes) ? 0 : -ENOSPC;
1274}
1275
1276/* Space holding and freeing is done to ensure we have space available for
1277 * write_begin/end.
1278 * For now we just assume few parallel writes and check against a small
1279 * number.
1280 * Todo: need to do this with a counter to handle parallel reads better.
1281 */
1282
1283static ssize_t yaffs_hold_space(struct file *f)
1284{
1285 struct yaffs_obj *obj;
1286 struct yaffs_dev *dev;
1287
1288 int n_free_chunks;
1289
1290 obj = yaffs_dentry_to_obj(f->f_dentry);
1291
1292 dev = obj->my_dev;
1293
1294 yaffs_gross_lock(dev);
1295
1296 n_free_chunks = yaffs_get_n_free_chunks(dev);
1297
1298 yaffs_gross_unlock(dev);
1299
1300 return (n_free_chunks > 20) ? 1 : 0;
1301}
1302
1303static void yaffs_release_space(struct file *f)
1304{
1305 struct yaffs_obj *obj;
1306 struct yaffs_dev *dev;
1307
1308 obj = yaffs_dentry_to_obj(f->f_dentry);
1309
1310 dev = obj->my_dev;
1311
1312 yaffs_gross_lock(dev);
1313
1314 yaffs_gross_unlock(dev);
1315}
1316
1317static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1318 loff_t pos, unsigned len, unsigned flags,
1319 struct page **pagep, void **fsdata)
1320{
1321 struct page *pg = NULL;
1322 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1323
1324 int ret = 0;
1325 int space_held = 0;
1326
1327 /* Get a page */
1328 pg = grab_cache_page_write_begin(mapping, index, flags);
1329
1330 *pagep = pg;
1331 if (!pg) {
1332 ret = -ENOMEM;
1333 goto out;
1334 }
1335 yaffs_trace(YAFFS_TRACE_OS,
1336 "start yaffs_write_begin index %d(%x) uptodate %d",
1337 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
1338
1339 /* Get fs space */
1340 space_held = yaffs_hold_space(filp);
1341
1342 if (!space_held) {
1343 ret = -ENOSPC;
1344 goto out;
1345 }
1346
1347 /* Update page if required */
1348
1349 if (!Page_Uptodate(pg))
1350 ret = yaffs_readpage_nolock(filp, pg);
1351
1352 if (ret)
1353 goto out;
1354
1355 /* Happy path return */
1356 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
1357
1358 return 0;
1359
1360out:
1361 yaffs_trace(YAFFS_TRACE_OS,
1362 "end yaffs_write_begin fail returning %d", ret);
1363 if (space_held)
1364 yaffs_release_space(filp);
1365 if (pg) {
1366 unlock_page(pg);
1367 page_cache_release(pg);
1368 }
1369 return ret;
1370}
1371
1372static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1373 loff_t * pos)
1374{
1375 struct yaffs_obj *obj;
1376 int n_written, ipos;
1377 struct inode *inode;
1378 struct yaffs_dev *dev;
1379
1380 obj = yaffs_dentry_to_obj(f->f_dentry);
1381
1382 dev = obj->my_dev;
1383
1384 yaffs_gross_lock(dev);
1385
1386 inode = f->f_dentry->d_inode;
1387
1388 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1389 ipos = inode->i_size;
1390 else
1391 ipos = *pos;
1392
1393 if (!obj)
1394 yaffs_trace(YAFFS_TRACE_OS,
1395 "yaffs_file_write: hey obj is null!");
1396 else
1397 yaffs_trace(YAFFS_TRACE_OS,
1398 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %d(%x)",
1399 (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos);
1400
1401 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1402
1403 yaffs_touch_super(dev);
1404
1405 yaffs_trace(YAFFS_TRACE_OS,
1406 "yaffs_file_write: %d(%x) bytes written",
1407 (unsigned)n, (unsigned)n);
1408
1409 if (n_written > 0) {
1410 ipos += n_written;
1411 *pos = ipos;
1412 if (ipos > inode->i_size) {
1413 inode->i_size = ipos;
1414 inode->i_blocks = (ipos + 511) >> 9;
1415
1416 yaffs_trace(YAFFS_TRACE_OS,
1417 "yaffs_file_write size updated to %d bytes, %d blocks",
1418 ipos, (int)(inode->i_blocks));
1419 }
1420
1421 }
1422 yaffs_gross_unlock(dev);
1423 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1424}
1425
1426static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1427 loff_t pos, unsigned len, unsigned copied,
1428 struct page *pg, void *fsdadata)
1429{
1430 int ret = 0;
1431 void *addr, *kva;
1432 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1433
1434 kva = kmap(pg);
1435 addr = kva + offset_into_page;
1436
1437 yaffs_trace(YAFFS_TRACE_OS,
1438 "yaffs_write_end addr %p pos %x n_bytes %d",
1439 addr, (unsigned)pos, copied);
1440
1441 ret = yaffs_file_write(filp, addr, copied, &pos);
1442
1443 if (ret != copied) {
1444 yaffs_trace(YAFFS_TRACE_OS,
1445 "yaffs_write_end not same size ret %d copied %d",
1446 ret, copied);
1447 SetPageError(pg);
1448 }
1449
1450 kunmap(pg);
1451
1452 yaffs_release_space(filp);
1453 unlock_page(pg);
1454 page_cache_release(pg);
1455 return ret;
1456}
1457
1458static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1459{
1460 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1461 struct super_block *sb = dentry->d_sb;
1462
1463 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
1464
1465 yaffs_gross_lock(dev);
1466
1467 buf->f_type = YAFFS_MAGIC;
1468 buf->f_bsize = sb->s_blocksize;
1469 buf->f_namelen = 255;
1470
1471 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
1472 /* Do this if chunk size is not a power of 2 */
1473
1474 uint64_t bytes_in_dev;
1475 uint64_t bytes_free;
1476
1477 bytes_in_dev =
1478 ((uint64_t)
1479 ((dev->param.end_block - dev->param.start_block +
1480 1))) * ((uint64_t) (dev->param.chunks_per_block *
1481 dev->data_bytes_per_chunk));
1482
1483 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
1484 buf->f_blocks = bytes_in_dev;
1485
1486 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
1487 ((uint64_t) (dev->data_bytes_per_chunk));
1488
1489 do_div(bytes_free, sb->s_blocksize);
1490
1491 buf->f_bfree = bytes_free;
1492
1493 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
1494
1495 buf->f_blocks =
1496 (dev->param.end_block - dev->param.start_block + 1) *
1497 dev->param.chunks_per_block /
1498 (sb->s_blocksize / dev->data_bytes_per_chunk);
1499 buf->f_bfree =
1500 yaffs_get_n_free_chunks(dev) /
1501 (sb->s_blocksize / dev->data_bytes_per_chunk);
1502 } else {
1503 buf->f_blocks =
1504 (dev->param.end_block - dev->param.start_block + 1) *
1505 dev->param.chunks_per_block *
1506 (dev->data_bytes_per_chunk / sb->s_blocksize);
1507
1508 buf->f_bfree =
1509 yaffs_get_n_free_chunks(dev) *
1510 (dev->data_bytes_per_chunk / sb->s_blocksize);
1511 }
1512
1513 buf->f_files = 0;
1514 buf->f_ffree = 0;
1515 buf->f_bavail = buf->f_bfree;
1516
1517 yaffs_gross_unlock(dev);
1518 return 0;
1519}
1520
1521static void yaffs_flush_inodes(struct super_block *sb)
1522{
1523 struct inode *iptr;
1524 struct yaffs_obj *obj;
1525
1526 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
1527 obj = yaffs_inode_to_obj(iptr);
1528 if (obj) {
1529 yaffs_trace(YAFFS_TRACE_OS,
1530 "flushing obj %d", obj->obj_id);
1531 yaffs_flush_file(obj, 1, 0);
1532 }
1533 }
1534}
1535
1536static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
1537{
1538 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1539 if (!dev)
1540 return;
1541
1542 yaffs_flush_inodes(sb);
1543 yaffs_update_dirty_dirs(dev);
1544 yaffs_flush_whole_cache(dev);
1545 if (do_checkpoint)
1546 yaffs_checkpoint_save(dev);
1547}
1548
1549static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
1550{
1551 unsigned erased_chunks =
1552 dev->n_erased_blocks * dev->param.chunks_per_block;
1553 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1554 unsigned scattered = 0; /* Free chunks not in an erased block */
1555
1556 if (erased_chunks < dev->n_free_chunks)
1557 scattered = (dev->n_free_chunks - erased_chunks);
1558
1559 if (!context->bg_running)
1560 return 0;
1561 else if (scattered < (dev->param.chunks_per_block * 2))
1562 return 0;
1563 else if (erased_chunks > dev->n_free_chunks / 2)
1564 return 0;
1565 else if (erased_chunks > dev->n_free_chunks / 4)
1566 return 1;
1567 else
1568 return 2;
1569}
1570
1571static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
1572{
1573
1574 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1575 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
1576 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
1577 int do_checkpoint;
1578
1579 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1580 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
1581 gc_urgent,
1582 sb->s_dirt ? "dirty" : "clean",
1583 request_checkpoint ? "checkpoint requested" : "no checkpoint",
1584 oneshot_checkpoint ? " one-shot" : "");
1585
1586 yaffs_gross_lock(dev);
1587 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
1588 oneshot_checkpoint) && !dev->is_checkpointed;
1589
1590 if (sb->s_dirt || do_checkpoint) {
1591 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
1592 sb->s_dirt = 0;
1593 if (oneshot_checkpoint)
1594 yaffs_auto_checkpoint &= ~4;
1595 }
1596 yaffs_gross_unlock(dev);
1597
1598 return 0;
1599}
1600
1601/*
1602 * yaffs background thread functions .
1603 * yaffs_bg_thread_fn() the thread function
1604 * yaffs_bg_start() launches the background thread.
1605 * yaffs_bg_stop() cleans up the background thread.
1606 *
1607 * NB:
1608 * The thread should only run after the yaffs is initialised
1609 * The thread should be stopped before yaffs is unmounted.
1610 * The thread should not do any writing while the fs is in read only.
1611 */
1612
1613void yaffs_background_waker(unsigned long data)
1614{
1615 wake_up_process((struct task_struct *)data);
1616}
1617
1618static int yaffs_bg_thread_fn(void *data)
1619{
1620 struct yaffs_dev *dev = (struct yaffs_dev *)data;
1621 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1622 unsigned long now = jiffies;
1623 unsigned long next_dir_update = now;
1624 unsigned long next_gc = now;
1625 unsigned long expires;
1626 unsigned int urgency;
1627
1628 int gc_result;
1629 struct timer_list timer;
1630
1631 yaffs_trace(YAFFS_TRACE_BACKGROUND,
1632 "yaffs_background starting for dev %p", (void *)dev);
1633
1634 set_freezable();
1635 while (context->bg_running) {
1636 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
1637
1638 if (kthread_should_stop())
1639 break;
1640
1641 if (try_to_freeze())
1642 continue;
1643
1644 yaffs_gross_lock(dev);
1645
1646 now = jiffies;
1647
1648 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
1649 yaffs_update_dirty_dirs(dev);
1650 next_dir_update = now + HZ;
1651 }
1652
1653 if (time_after(now, next_gc) && yaffs_bg_enable) {
1654 if (!dev->is_checkpointed) {
1655 urgency = yaffs_bg_gc_urgency(dev);
1656 gc_result = yaffs_bg_gc(dev, urgency);
1657 if (urgency > 1)
1658 next_gc = now + HZ / 20 + 1;
1659 else if (urgency > 0)
1660 next_gc = now + HZ / 10 + 1;
1661 else
1662 next_gc = now + HZ * 2;
1663 } else {
1664 /*
1665 * gc not running so set to next_dir_update
1666 * to cut down on wake ups
1667 */
1668 next_gc = next_dir_update;
1669 }
1670 }
1671 yaffs_gross_unlock(dev);
1672 expires = next_dir_update;
1673 if (time_before(next_gc, expires))
1674 expires = next_gc;
1675 if (time_before(expires, now))
1676 expires = now + HZ;
1677
1678 Y_INIT_TIMER(&timer);
1679 timer.expires = expires + 1;
1680 timer.data = (unsigned long)current;
1681 timer.function = yaffs_background_waker;
1682
1683 set_current_state(TASK_INTERRUPTIBLE);
1684 add_timer(&timer);
1685 schedule();
1686 del_timer_sync(&timer);
1687 }
1688
1689 return 0;
1690}
1691
1692static int yaffs_bg_start(struct yaffs_dev *dev)
1693{
1694 int retval = 0;
1695 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1696
1697 if (dev->read_only)
1698 return -1;
1699
1700 context->bg_running = 1;
1701
1702 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
1703 (void *)dev, "yaffs-bg-%d",
1704 context->mount_id);
1705
1706 if (IS_ERR(context->bg_thread)) {
1707 retval = PTR_ERR(context->bg_thread);
1708 context->bg_thread = NULL;
1709 context->bg_running = 0;
1710 }
1711 return retval;
1712}
1713
1714static void yaffs_bg_stop(struct yaffs_dev *dev)
1715{
1716 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
1717
1718 ctxt->bg_running = 0;
1719
1720 if (ctxt->bg_thread) {
1721 kthread_stop(ctxt->bg_thread);
1722 ctxt->bg_thread = NULL;
1723 }
1724}
1725
1726static void yaffs_write_super(struct super_block *sb)
1727{
1728 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
1729
1730 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1731 "yaffs_write_super%s",
1732 request_checkpoint ? " checkpt" : "");
1733
1734 yaffs_do_sync_fs(sb, request_checkpoint);
1735
1736}
1737
1738static int yaffs_sync_fs(struct super_block *sb, int wait)
1739{
1740 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
1741
1742 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1743 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
1744
1745 yaffs_do_sync_fs(sb, request_checkpoint);
1746
1747 return 0;
1748}
1749
1750
1751static LIST_HEAD(yaffs_context_list);
1752struct mutex yaffs_context_lock;
1753
1754
1755
1756struct yaffs_options {
1757 int inband_tags;
1758 int skip_checkpoint_read;
1759 int skip_checkpoint_write;
1760 int no_cache;
1761 int tags_ecc_on;
1762 int tags_ecc_overridden;
1763 int lazy_loading_enabled;
1764 int lazy_loading_overridden;
1765 int empty_lost_and_found;
1766 int empty_lost_and_found_overridden;
1767};
1768
1769#define MAX_OPT_LEN 30
1770static int yaffs_parse_options(struct yaffs_options *options,
1771 const char *options_str)
1772{
1773 char cur_opt[MAX_OPT_LEN + 1];
1774 int p;
1775 int error = 0;
1776
1777 /* Parse through the options which is a comma seperated list */
1778
1779 while (options_str && *options_str && !error) {
1780 memset(cur_opt, 0, MAX_OPT_LEN + 1);
1781 p = 0;
1782
1783 while (*options_str == ',')
1784 options_str++;
1785
1786 while (*options_str && *options_str != ',') {
1787 if (p < MAX_OPT_LEN) {
1788 cur_opt[p] = *options_str;
1789 p++;
1790 }
1791 options_str++;
1792 }
1793
1794 if (!strcmp(cur_opt, "inband-tags")) {
1795 options->inband_tags = 1;
1796 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
1797 options->tags_ecc_on = 0;
1798 options->tags_ecc_overridden = 1;
1799 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
1800 options->tags_ecc_on = 1;
1801 options->tags_ecc_overridden = 1;
1802 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
1803 options->lazy_loading_enabled = 0;
1804 options->lazy_loading_overridden = 1;
1805 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
1806 options->lazy_loading_enabled = 1;
1807 options->lazy_loading_overridden = 1;
1808 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
1809 options->empty_lost_and_found = 0;
1810 options->empty_lost_and_found_overridden = 1;
1811 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
1812 options->empty_lost_and_found = 1;
1813 options->empty_lost_and_found_overridden = 1;
1814 } else if (!strcmp(cur_opt, "no-cache")) {
1815 options->no_cache = 1;
1816 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
1817 options->skip_checkpoint_read = 1;
1818 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
1819 options->skip_checkpoint_write = 1;
1820 } else if (!strcmp(cur_opt, "no-checkpoint")) {
1821 options->skip_checkpoint_read = 1;
1822 options->skip_checkpoint_write = 1;
1823 } else {
1824 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
1825 cur_opt);
1826 error = 1;
1827 }
1828 }
1829
1830 return error;
1831}
1832
1833static struct address_space_operations yaffs_file_address_operations = {
1834 .readpage = yaffs_readpage,
1835 .writepage = yaffs_writepage,
1836 .write_begin = yaffs_write_begin,
1837 .write_end = yaffs_write_end,
1838};
1839
1840
1841
1842static const struct inode_operations yaffs_file_inode_operations = {
1843 .setattr = yaffs_setattr,
1844#ifdef CONFIG_YAFFS_XATTR
1845 .setxattr = yaffs_setxattr,
1846 .getxattr = yaffs_getxattr,
1847 .listxattr = yaffs_listxattr,
1848 .removexattr = yaffs_removexattr,
1849#endif
1850};
1851
1852static const struct inode_operations yaffs_symlink_inode_operations = {
1853 .readlink = yaffs_readlink,
1854 .follow_link = yaffs_follow_link,
1855 .put_link = yaffs_put_link,
1856 .setattr = yaffs_setattr,
1857#ifdef CONFIG_YAFFS_XATTR
1858 .setxattr = yaffs_setxattr,
1859 .getxattr = yaffs_getxattr,
1860 .listxattr = yaffs_listxattr,
1861 .removexattr = yaffs_removexattr,
1862#endif
1863};
1864
1865static void yaffs_fill_inode_from_obj(struct inode *inode,
1866 struct yaffs_obj *obj)
1867{
1868 if (inode && obj) {
1869
1870 /* Check mode against the variant type and attempt to repair if broken. */
1871 u32 mode = obj->yst_mode;
1872 switch (obj->variant_type) {
1873 case YAFFS_OBJECT_TYPE_FILE:
1874 if (!S_ISREG(mode)) {
1875 obj->yst_mode &= ~S_IFMT;
1876 obj->yst_mode |= S_IFREG;
1877 }
1878
1879 break;
1880 case YAFFS_OBJECT_TYPE_SYMLINK:
1881 if (!S_ISLNK(mode)) {
1882 obj->yst_mode &= ~S_IFMT;
1883 obj->yst_mode |= S_IFLNK;
1884 }
1885
1886 break;
1887 case YAFFS_OBJECT_TYPE_DIRECTORY:
1888 if (!S_ISDIR(mode)) {
1889 obj->yst_mode &= ~S_IFMT;
1890 obj->yst_mode |= S_IFDIR;
1891 }
1892
1893 break;
1894 case YAFFS_OBJECT_TYPE_UNKNOWN:
1895 case YAFFS_OBJECT_TYPE_HARDLINK:
1896 case YAFFS_OBJECT_TYPE_SPECIAL:
1897 default:
1898 /* TODO? */
1899 break;
1900 }
1901
1902 inode->i_flags |= S_NOATIME;
1903
1904 inode->i_ino = obj->obj_id;
1905 inode->i_mode = obj->yst_mode;
1906 inode->i_uid = obj->yst_uid;
1907 inode->i_gid = obj->yst_gid;
1908
1909 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1910
1911 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1912 inode->i_atime.tv_nsec = 0;
1913 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1914 inode->i_mtime.tv_nsec = 0;
1915 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1916 inode->i_ctime.tv_nsec = 0;
1917 inode->i_size = yaffs_get_obj_length(obj);
1918 inode->i_blocks = (inode->i_size + 511) >> 9;
1919
1920 inode->i_nlink = yaffs_get_obj_link_count(obj);
1921
1922 yaffs_trace(YAFFS_TRACE_OS,
1923 "yaffs_fill_inode mode %x uid %d gid %d size %d count %d",
1924 inode->i_mode, inode->i_uid, inode->i_gid,
1925 (int)inode->i_size, atomic_read(&inode->i_count));
1926
1927 switch (obj->yst_mode & S_IFMT) {
1928 default: /* fifo, device or socket */
1929 init_special_inode(inode, obj->yst_mode,
1930 old_decode_dev(obj->yst_rdev));
1931 break;
1932 case S_IFREG: /* file */
1933 inode->i_op = &yaffs_file_inode_operations;
1934 inode->i_fop = &yaffs_file_operations;
1935 inode->i_mapping->a_ops =
1936 &yaffs_file_address_operations;
1937 break;
1938 case S_IFDIR: /* directory */
1939 inode->i_op = &yaffs_dir_inode_operations;
1940 inode->i_fop = &yaffs_dir_operations;
1941 break;
1942 case S_IFLNK: /* symlink */
1943 inode->i_op = &yaffs_symlink_inode_operations;
1944 break;
1945 }
1946
1947 yaffs_inode_to_obj_lv(inode) = obj;
1948
1949 obj->my_inode = inode;
1950
1951 } else {
1952 yaffs_trace(YAFFS_TRACE_OS,
1953 "yaffs_fill_inode invalid parameters");
1954 }
1955}
1956
1957static void yaffs_put_super(struct super_block *sb)
1958{
1959 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1960
1961 yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
1962
1963 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1964 "Shutting down yaffs background thread");
1965 yaffs_bg_stop(dev);
1966 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1967 "yaffs background thread shut down");
1968
1969 yaffs_gross_lock(dev);
1970
1971 yaffs_flush_super(sb, 1);
1972
1973 if (yaffs_dev_to_lc(dev)->put_super_fn)
1974 yaffs_dev_to_lc(dev)->put_super_fn(sb);
1975
1976 yaffs_deinitialise(dev);
1977
1978 yaffs_gross_unlock(dev);
1979 mutex_lock(&yaffs_context_lock);
1980 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
1981 mutex_unlock(&yaffs_context_lock);
1982
1983 if (yaffs_dev_to_lc(dev)->spare_buffer) {
1984 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
1985 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
1986 }
1987
1988 kfree(dev);
1989}
1990
1991static void yaffs_mtd_put_super(struct super_block *sb)
1992{
1993 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
1994
1995 if (mtd->sync)
1996 mtd->sync(mtd);
1997
1998 put_mtd_device(mtd);
1999}
2000
2001static const struct super_operations yaffs_super_ops = {
2002 .statfs = yaffs_statfs,
2003 .put_super = yaffs_put_super,
2004 .evict_inode = yaffs_evict_inode,
2005 .sync_fs = yaffs_sync_fs,
2006 .write_super = yaffs_write_super,
2007};
2008
2009static struct super_block *yaffs_internal_read_super(int yaffs_version,
2010 struct super_block *sb,
2011 void *data, int silent)
2012{
2013 int n_blocks;
2014 struct inode *inode = NULL;
2015 struct dentry *root;
2016 struct yaffs_dev *dev = 0;
2017 char devname_buf[BDEVNAME_SIZE + 1];
2018 struct mtd_info *mtd;
2019 int err;
2020 char *data_str = (char *)data;
2021 struct yaffs_linux_context *context = NULL;
2022 struct yaffs_param *param;
2023
2024 int read_only = 0;
2025
2026 struct yaffs_options options;
2027
2028 unsigned mount_id;
2029 int found;
2030 struct yaffs_linux_context *context_iterator;
2031 struct list_head *l;
2032
2033 sb->s_magic = YAFFS_MAGIC;
2034 sb->s_op = &yaffs_super_ops;
2035 sb->s_flags |= MS_NOATIME;
2036
2037 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2038
2039 sb->s_export_op = &yaffs_export_ops;
2040
2041 if (!sb)
2042 printk(KERN_INFO "yaffs: sb is NULL\n");
2043 else if (!sb->s_dev)
2044 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2045 else if (!yaffs_devname(sb, devname_buf))
2046 printk(KERN_INFO "yaffs: devname is NULL\n");
2047 else
2048 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2049 sb->s_dev,
2050 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2051
2052 if (!data_str)
2053 data_str = "";
2054
2055 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2056
2057 memset(&options, 0, sizeof(options));
2058
2059 if (yaffs_parse_options(&options, data_str)) {
2060 /* Option parsing failed */
2061 return NULL;
2062 }
2063
2064 sb->s_blocksize = PAGE_CACHE_SIZE;
2065 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2066
2067 yaffs_trace(YAFFS_TRACE_OS,
2068 "yaffs_read_super: Using yaffs%d", yaffs_version);
2069 yaffs_trace(YAFFS_TRACE_OS,
2070 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2071
2072 yaffs_trace(YAFFS_TRACE_ALWAYS,
2073 "Attempting MTD mount of %u.%u,\"%s\"",
2074 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2075 yaffs_devname(sb, devname_buf));
2076
2077 /* Check it's an mtd device..... */
2078 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2079 return NULL; /* This isn't an mtd device */
2080
2081 /* Get the device */
2082 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2083 if (!mtd) {
2084 yaffs_trace(YAFFS_TRACE_ALWAYS,
2085 "MTD device #%u doesn't appear to exist",
2086 MINOR(sb->s_dev));
2087 return NULL;
2088 }
2089 /* Check it's NAND */
2090 if (mtd->type != MTD_NANDFLASH) {
2091 yaffs_trace(YAFFS_TRACE_ALWAYS,
2092 "MTD device is not NAND it's type %d",
2093 mtd->type);
2094 return NULL;
2095 }
2096
2097 yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase);
2098 yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read);
2099 yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write);
2100 yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob);
2101 yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob);
2102 yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad);
2103 yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad);
2104 yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
2105 yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
2106 yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
2107 yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
2108
2109#ifdef CONFIG_YAFFS_AUTO_YAFFS2
2110
2111 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2112 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2113 yaffs_version = 2;
2114 }
2115
2116 /* Added NCB 26/5/2006 for completeness */
2117 if (yaffs_version == 2 && !options.inband_tags
2118 && WRITE_SIZE(mtd) == 512) {
2119 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2120 yaffs_version = 1;
2121 }
2122#endif
2123
2124 if (yaffs_version == 2) {
2125 /* Check for version 2 style functions */
2126 if (!mtd->erase ||
2127 !mtd->block_isbad ||
2128 !mtd->block_markbad ||
2129 !mtd->read ||
2130 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2131 yaffs_trace(YAFFS_TRACE_ALWAYS,
2132 "MTD device does not support required functions");
2133 return NULL;
2134 }
2135
2136 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2137 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2138 !options.inband_tags) {
2139 yaffs_trace(YAFFS_TRACE_ALWAYS,
2140 "MTD device does not have the right page sizes");
2141 return NULL;
2142 }
2143 } else {
2144 /* Check for V1 style functions */
2145 if (!mtd->erase ||
2146 !mtd->read ||
2147 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2148 yaffs_trace(YAFFS_TRACE_ALWAYS,
2149 "MTD device does not support required functions");
2150 return NULL;
2151 }
2152
2153 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2154 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2155 yaffs_trace(YAFFS_TRACE_ALWAYS,
2156 "MTD device does not support have the right page sizes");
2157 return NULL;
2158 }
2159 }
2160
2161 /* OK, so if we got here, we have an MTD that's NAND and looks
2162 * like it has the right capabilities
2163 * Set the struct yaffs_dev up for mtd
2164 */
2165
2166 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2167 read_only = 1;
2168 printk(KERN_INFO
2169 "yaffs: mtd is read only, setting superblock read only");
2170 sb->s_flags |= MS_RDONLY;
2171 }
2172
2173 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2174 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2175
2176 if (!dev || !context) {
2177 if (dev)
2178 kfree(dev);
2179 if (context)
2180 kfree(context);
2181 dev = NULL;
2182 context = NULL;
2183 }
2184
2185 if (!dev) {
2186 /* Deep shit could not allocate device structure */
2187 yaffs_trace(YAFFS_TRACE_ALWAYS,
2188 "yaffs_read_super failed trying to allocate yaffs_dev");
2189 return NULL;
2190 }
2191 memset(dev, 0, sizeof(struct yaffs_dev));
2192 param = &(dev->param);
2193
2194 memset(context, 0, sizeof(struct yaffs_linux_context));
2195 dev->os_context = context;
2196 INIT_LIST_HEAD(&(context->context_list));
2197 context->dev = dev;
2198 context->super = sb;
2199
2200 dev->read_only = read_only;
2201
2202 sb->s_fs_info = dev;
2203
2204 dev->driver_context = mtd;
2205 param->name = mtd->name;
2206
2207 /* Set up the memory size parameters.... */
2208
2209 n_blocks =
2210 YCALCBLOCKS(mtd->size,
2211 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2212
2213 param->start_block = 0;
2214 param->end_block = n_blocks - 1;
2215 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2216 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2217 param->n_reserved_blocks = 5;
2218 param->n_caches = (options.no_cache) ? 0 : 10;
2219 param->inband_tags = options.inband_tags;
2220
2221#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2222 param->disable_lazy_load = 1;
2223#endif
2224#ifdef CONFIG_YAFFS_XATTR
2225 param->enable_xattr = 1;
2226#endif
2227 if (options.lazy_loading_overridden)
2228 param->disable_lazy_load = !options.lazy_loading_enabled;
2229
2230#ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2231 param->no_tags_ecc = 1;
2232#endif
2233
2234#ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2235#else
2236 param->defered_dir_update = 1;
2237#endif
2238
2239 if (options.tags_ecc_overridden)
2240 param->no_tags_ecc = !options.tags_ecc_on;
2241
2242#ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2243 param->empty_lost_n_found = 1;
2244#endif
2245
2246#ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2247 param->refresh_period = 0;
2248#else
2249 param->refresh_period = 500;
2250#endif
2251
2252#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2253 param->always_check_erased = 1;
2254#endif
2255
2256 if (options.empty_lost_and_found_overridden)
2257 param->empty_lost_n_found = options.empty_lost_and_found;
2258
2259 /* ... and the functions. */
2260 if (yaffs_version == 2) {
2261 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2262 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2263 param->bad_block_fn = nandmtd2_mark_block_bad;
2264 param->query_block_fn = nandmtd2_query_block;
2265 yaffs_dev_to_lc(dev)->spare_buffer =
2266 kmalloc(mtd->oobsize, GFP_NOFS);
2267 param->is_yaffs2 = 1;
2268 param->total_bytes_per_chunk = mtd->writesize;
2269 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2270 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2271
2272 param->start_block = 0;
2273 param->end_block = n_blocks - 1;
2274 } else {
2275 /* use the MTD interface in yaffs_mtdif1.c */
2276 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2277 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2278 param->bad_block_fn = nandmtd1_mark_block_bad;
2279 param->query_block_fn = nandmtd1_query_block;
2280 param->is_yaffs2 = 0;
2281 }
2282 /* ... and common functions */
2283 param->erase_fn = nandmtd_erase_block;
2284 param->initialise_flash_fn = nandmtd_initialise;
2285
2286 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2287
2288 param->sb_dirty_fn = yaffs_touch_super;
2289 param->gc_control = yaffs_gc_control_callback;
2290
2291 yaffs_dev_to_lc(dev)->super = sb;
2292
2293#ifndef CONFIG_YAFFS_DOES_ECC
2294 param->use_nand_ecc = 1;
2295#endif
2296
2297 param->skip_checkpt_rd = options.skip_checkpoint_read;
2298 param->skip_checkpt_wr = options.skip_checkpoint_write;
2299
2300 mutex_lock(&yaffs_context_lock);
2301 /* Get a mount id */
2302 found = 0;
2303 for (mount_id = 0; !found; mount_id++) {
2304 found = 1;
2305 list_for_each(l, &yaffs_context_list) {
2306 context_iterator =
2307 list_entry(l, struct yaffs_linux_context,
2308 context_list);
2309 if (context_iterator->mount_id == mount_id)
2310 found = 0;
2311 }
2312 }
2313 context->mount_id = mount_id;
2314
2315 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2316 &yaffs_context_list);
2317 mutex_unlock(&yaffs_context_lock);
2318
2319 /* Directory search handling... */
2320 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2321 param->remove_obj_fn = yaffs_remove_obj_callback;
2322
2323 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2324
2325 yaffs_gross_lock(dev);
2326
2327 err = yaffs_guts_initialise(dev);
2328
2329 yaffs_trace(YAFFS_TRACE_OS,
2330 "yaffs_read_super: guts initialised %s",
2331 (err == YAFFS_OK) ? "OK" : "FAILED");
2332
2333 if (err == YAFFS_OK)
2334 yaffs_bg_start(dev);
2335
2336 if (!context->bg_thread)
2337 param->defered_dir_update = 0;
2338
2339 /* Release lock before yaffs_get_inode() */
2340 yaffs_gross_unlock(dev);
2341
2342 /* Create root inode */
2343 if (err == YAFFS_OK)
2344 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2345
2346 if (!inode)
2347 return NULL;
2348
2349 inode->i_op = &yaffs_dir_inode_operations;
2350 inode->i_fop = &yaffs_dir_operations;
2351
2352 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2353
2354 root = d_alloc_root(inode);
2355
2356 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
2357
2358 if (!root) {
2359 iput(inode);
2360 return NULL;
2361 }
2362 sb->s_root = root;
2363 sb->s_dirt = !dev->is_checkpointed;
2364 yaffs_trace(YAFFS_TRACE_ALWAYS,
2365 "yaffs_read_super: is_checkpointed %d",
2366 dev->is_checkpointed);
2367
2368 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
2369 return sb;
2370}
2371
2372static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2373 int silent)
2374{
2375 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2376}
2377
2378static int yaffs_read_super(struct file_system_type *fs,
2379 int flags, const char *dev_name,
2380 void *data, struct vfsmount *mnt)
2381{
2382
2383 return get_sb_bdev(fs, flags, dev_name, data,
2384 yaffs_internal_read_super_mtd, mnt);
2385}
2386
2387static struct file_system_type yaffs_fs_type = {
2388 .owner = THIS_MODULE,
2389 .name = "yaffs",
2390 .get_sb = yaffs_read_super,
2391 .kill_sb = kill_block_super,
2392 .fs_flags = FS_REQUIRES_DEV,
2393};
2394
2395#ifdef CONFIG_YAFFS_YAFFS2
2396
2397static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2398 int silent)
2399{
2400 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2401}
2402
2403static int yaffs2_read_super(struct file_system_type *fs,
2404 int flags, const char *dev_name, void *data,
2405 struct vfsmount *mnt)
2406{
2407 return get_sb_bdev(fs, flags, dev_name, data,
2408 yaffs2_internal_read_super_mtd, mnt);
2409}
2410
2411static struct file_system_type yaffs2_fs_type = {
2412 .owner = THIS_MODULE,
2413 .name = "yaffs2",
2414 .get_sb = yaffs2_read_super,
2415 .kill_sb = kill_block_super,
2416 .fs_flags = FS_REQUIRES_DEV,
2417};
2418#endif /* CONFIG_YAFFS_YAFFS2 */
2419
2420static struct proc_dir_entry *my_proc_entry;
2421
2422static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
2423{
2424 struct yaffs_param *param = &dev->param;
2425 buf += sprintf(buf, "start_block........... %d\n", param->start_block);
2426 buf += sprintf(buf, "end_block............. %d\n", param->end_block);
2427 buf += sprintf(buf, "total_bytes_per_chunk. %d\n",
2428 param->total_bytes_per_chunk);
2429 buf += sprintf(buf, "use_nand_ecc.......... %d\n",
2430 param->use_nand_ecc);
2431 buf += sprintf(buf, "no_tags_ecc........... %d\n", param->no_tags_ecc);
2432 buf += sprintf(buf, "is_yaffs2............. %d\n", param->is_yaffs2);
2433 buf += sprintf(buf, "inband_tags........... %d\n", param->inband_tags);
2434 buf += sprintf(buf, "empty_lost_n_found.... %d\n",
2435 param->empty_lost_n_found);
2436 buf += sprintf(buf, "disable_lazy_load..... %d\n",
2437 param->disable_lazy_load);
2438 buf += sprintf(buf, "refresh_period........ %d\n",
2439 param->refresh_period);
2440 buf += sprintf(buf, "n_caches.............. %d\n", param->n_caches);
2441 buf += sprintf(buf, "n_reserved_blocks..... %d\n",
2442 param->n_reserved_blocks);
2443 buf += sprintf(buf, "always_check_erased... %d\n",
2444 param->always_check_erased);
2445
2446 return buf;
2447}
2448
2449static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
2450{
2451 buf +=
2452 sprintf(buf, "data_bytes_per_chunk.. %d\n",
2453 dev->data_bytes_per_chunk);
2454 buf += sprintf(buf, "chunk_grp_bits........ %d\n", dev->chunk_grp_bits);
2455 buf += sprintf(buf, "chunk_grp_size........ %d\n", dev->chunk_grp_size);
2456 buf +=
2457 sprintf(buf, "n_erased_blocks....... %d\n", dev->n_erased_blocks);
2458 buf +=
2459 sprintf(buf, "blocks_in_checkpt..... %d\n", dev->blocks_in_checkpt);
2460 buf += sprintf(buf, "\n");
2461 buf += sprintf(buf, "n_tnodes.............. %d\n", dev->n_tnodes);
2462 buf += sprintf(buf, "n_obj................. %d\n", dev->n_obj);
2463 buf += sprintf(buf, "n_free_chunks......... %d\n", dev->n_free_chunks);
2464 buf += sprintf(buf, "\n");
2465 buf += sprintf(buf, "n_page_writes......... %u\n", dev->n_page_writes);
2466 buf += sprintf(buf, "n_page_reads.......... %u\n", dev->n_page_reads);
2467 buf += sprintf(buf, "n_erasures............ %u\n", dev->n_erasures);
2468 buf += sprintf(buf, "n_gc_copies........... %u\n", dev->n_gc_copies);
2469 buf += sprintf(buf, "all_gcs............... %u\n", dev->all_gcs);
2470 buf +=
2471 sprintf(buf, "passive_gc_count...... %u\n", dev->passive_gc_count);
2472 buf +=
2473 sprintf(buf, "oldest_dirty_gc_count. %u\n",
2474 dev->oldest_dirty_gc_count);
2475 buf += sprintf(buf, "n_gc_blocks........... %u\n", dev->n_gc_blocks);
2476 buf += sprintf(buf, "bg_gcs................ %u\n", dev->bg_gcs);
2477 buf +=
2478 sprintf(buf, "n_retired_writes...... %u\n", dev->n_retired_writes);
2479 buf +=
2480 sprintf(buf, "n_retired_blocks...... %u\n", dev->n_retired_blocks);
2481 buf += sprintf(buf, "n_ecc_fixed........... %u\n", dev->n_ecc_fixed);
2482 buf += sprintf(buf, "n_ecc_unfixed......... %u\n", dev->n_ecc_unfixed);
2483 buf +=
2484 sprintf(buf, "n_tags_ecc_fixed...... %u\n", dev->n_tags_ecc_fixed);
2485 buf +=
2486 sprintf(buf, "n_tags_ecc_unfixed.... %u\n",
2487 dev->n_tags_ecc_unfixed);
2488 buf += sprintf(buf, "cache_hits............ %u\n", dev->cache_hits);
2489 buf +=
2490 sprintf(buf, "n_deleted_files....... %u\n", dev->n_deleted_files);
2491 buf +=
2492 sprintf(buf, "n_unlinked_files...... %u\n", dev->n_unlinked_files);
2493 buf += sprintf(buf, "refresh_count......... %u\n", dev->refresh_count);
2494 buf += sprintf(buf, "n_bg_deletions........ %u\n", dev->n_bg_deletions);
2495
2496 return buf;
2497}
2498
2499static int yaffs_proc_read(char *page,
2500 char **start,
2501 off_t offset, int count, int *eof, void *data)
2502{
2503 struct list_head *item;
2504 char *buf = page;
2505 int step = offset;
2506 int n = 0;
2507
2508 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2509 * We use 'offset' (*ppos) to indicate where we are in dev_list.
2510 * This also assumes the user has posted a read buffer large
2511 * enough to hold the complete output; but that's life in /proc.
2512 */
2513
2514 *(int *)start = 1;
2515
2516 /* Print header first */
2517 if (step == 0)
2518 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__ "\n");
2519 else if (step == 1)
2520 buf += sprintf(buf, "\n");
2521 else {
2522 step -= 2;
2523
2524 mutex_lock(&yaffs_context_lock);
2525
2526 /* Locate and print the Nth entry. Order N-squared but N is small. */
2527 list_for_each(item, &yaffs_context_list) {
2528 struct yaffs_linux_context *dc =
2529 list_entry(item, struct yaffs_linux_context,
2530 context_list);
2531 struct yaffs_dev *dev = dc->dev;
2532
2533 if (n < (step & ~1)) {
2534 n += 2;
2535 continue;
2536 }
2537 if ((step & 1) == 0) {
2538 buf +=
2539 sprintf(buf, "\nDevice %d \"%s\"\n", n,
2540 dev->param.name);
2541 buf = yaffs_dump_dev_part0(buf, dev);
2542 } else {
2543 buf = yaffs_dump_dev_part1(buf, dev);
2544 }
2545
2546 break;
2547 }
2548 mutex_unlock(&yaffs_context_lock);
2549 }
2550
2551 return buf - page < count ? buf - page : count;
2552}
2553
2554
2555/**
2556 * Set the verbosity of the warnings and error messages.
2557 *
2558 * Note that the names can only be a..z or _ with the current code.
2559 */
2560
2561static struct {
2562 char *mask_name;
2563 unsigned mask_bitfield;
2564} mask_flags[] = {
2565 {"allocate", YAFFS_TRACE_ALLOCATE},
2566 {"always", YAFFS_TRACE_ALWAYS},
2567 {"background", YAFFS_TRACE_BACKGROUND},
2568 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2569 {"buffers", YAFFS_TRACE_BUFFERS},
2570 {"bug", YAFFS_TRACE_BUG},
2571 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2572 {"deletion", YAFFS_TRACE_DELETION},
2573 {"erase", YAFFS_TRACE_ERASE},
2574 {"error", YAFFS_TRACE_ERROR},
2575 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2576 {"gc", YAFFS_TRACE_GC},
2577 {"lock", YAFFS_TRACE_LOCK},
2578 {"mtd", YAFFS_TRACE_MTD},
2579 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2580 {"os", YAFFS_TRACE_OS},
2581 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2582 {"scan", YAFFS_TRACE_SCAN},
2583 {"mount", YAFFS_TRACE_MOUNT},
2584 {"tracing", YAFFS_TRACE_TRACING},
2585 {"sync", YAFFS_TRACE_SYNC},
2586 {"write", YAFFS_TRACE_WRITE},
2587 {"verify", YAFFS_TRACE_VERIFY},
2588 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2589 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2590 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2591 {"all", 0xffffffff},
2592 {"none", 0},
2593 {NULL, 0},
2594};
2595
2596#define MAX_MASK_NAME_LENGTH 40
2597static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2598 unsigned long count, void *data)
2599{
2600 unsigned rg = 0, mask_bitfield;
2601 char *end;
2602 char *mask_name;
2603 const char *x;
2604 char substring[MAX_MASK_NAME_LENGTH + 1];
2605 int i;
2606 int done = 0;
2607 int add, len = 0;
2608 int pos = 0;
2609
2610 rg = yaffs_trace_mask;
2611
2612 while (!done && (pos < count)) {
2613 done = 1;
2614 while ((pos < count) && isspace(buf[pos]))
2615 pos++;
2616
2617 switch (buf[pos]) {
2618 case '+':
2619 case '-':
2620 case '=':
2621 add = buf[pos];
2622 pos++;
2623 break;
2624
2625 default:
2626 add = ' ';
2627 break;
2628 }
2629 mask_name = NULL;
2630
2631 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2632
2633 if (end > buf + pos) {
2634 mask_name = "numeral";
2635 len = end - (buf + pos);
2636 pos += len;
2637 done = 0;
2638 } else {
2639 for (x = buf + pos, i = 0;
2640 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2641 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2642 substring[i] = *x;
2643 substring[i] = '\0';
2644
2645 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2646 if (strcmp(substring, mask_flags[i].mask_name)
2647 == 0) {
2648 mask_name = mask_flags[i].mask_name;
2649 mask_bitfield =
2650 mask_flags[i].mask_bitfield;
2651 done = 0;
2652 break;
2653 }
2654 }
2655 }
2656
2657 if (mask_name != NULL) {
2658 done = 0;
2659 switch (add) {
2660 case '-':
2661 rg &= ~mask_bitfield;
2662 break;
2663 case '+':
2664 rg |= mask_bitfield;
2665 break;
2666 case '=':
2667 rg = mask_bitfield;
2668 break;
2669 default:
2670 rg |= mask_bitfield;
2671 break;
2672 }
2673 }
2674 }
2675
2676 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
2677
2678 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
2679
2680 if (rg & YAFFS_TRACE_ALWAYS) {
2681 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2682 char flag;
2683 flag = ((rg & mask_flags[i].mask_bitfield) ==
2684 mask_flags[i].mask_bitfield) ? '+' : '-';
2685 printk(KERN_DEBUG "%c%s\n", flag,
2686 mask_flags[i].mask_name);
2687 }
2688 }
2689
2690 return count;
2691}
2692
2693static int yaffs_proc_write(struct file *file, const char *buf,
2694 unsigned long count, void *data)
2695{
2696 return yaffs_proc_write_trace_options(file, buf, count, data);
2697}
2698
2699/* Stuff to handle installation of file systems */
2700struct file_system_to_install {
2701 struct file_system_type *fst;
2702 int installed;
2703};
2704
2705static struct file_system_to_install fs_to_install[] = {
2706 {&yaffs_fs_type, 0},
2707 {&yaffs2_fs_type, 0},
2708 {NULL, 0}
2709};
2710
2711static int __init init_yaffs_fs(void)
2712{
2713 int error = 0;
2714 struct file_system_to_install *fsinst;
2715
2716 yaffs_trace(YAFFS_TRACE_ALWAYS,
2717 "yaffs built " __DATE__ " " __TIME__ " Installing.");
2718
2719#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2720 yaffs_trace(YAFFS_TRACE_ALWAYS,
2721 "\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n");
2722#endif
2723
2724 mutex_init(&yaffs_context_lock);
2725
2726 /* Install the proc_fs entries */
2727 my_proc_entry = create_proc_entry("yaffs",
2728 S_IRUGO | S_IFREG, YPROC_ROOT);
2729
2730 if (my_proc_entry) {
2731 my_proc_entry->write_proc = yaffs_proc_write;
2732 my_proc_entry->read_proc = yaffs_proc_read;
2733 my_proc_entry->data = NULL;
2734 } else {
2735 return -ENOMEM;
2736 }
2737
2738
2739 /* Now add the file system entries */
2740
2741 fsinst = fs_to_install;
2742
2743 while (fsinst->fst && !error) {
2744 error = register_filesystem(fsinst->fst);
2745 if (!error)
2746 fsinst->installed = 1;
2747 fsinst++;
2748 }
2749
2750 /* Any errors? uninstall */
2751 if (error) {
2752 fsinst = fs_to_install;
2753
2754 while (fsinst->fst) {
2755 if (fsinst->installed) {
2756 unregister_filesystem(fsinst->fst);
2757 fsinst->installed = 0;
2758 }
2759 fsinst++;
2760 }
2761 }
2762
2763 return error;
2764}
2765
2766static void __exit exit_yaffs_fs(void)
2767{
2768
2769 struct file_system_to_install *fsinst;
2770
2771 yaffs_trace(YAFFS_TRACE_ALWAYS,
2772 "yaffs built " __DATE__ " " __TIME__ " removing.");
2773
2774 remove_proc_entry("yaffs", YPROC_ROOT);
2775
2776 fsinst = fs_to_install;
2777
2778 while (fsinst->fst) {
2779 if (fsinst->installed) {
2780 unregister_filesystem(fsinst->fst);
2781 fsinst->installed = 0;
2782 }
2783 fsinst++;
2784 }
2785}
2786
2787module_init(init_yaffs_fs)
2788 module_exit(exit_yaffs_fs)
2789
2790 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2791MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
2792MODULE_LICENSE("GPL");