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