blob: f56e12ca34465f3493ca83deac96244ab20c6890 [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
Sujit Reddy Thumma4e239c02011-12-07 15:13:45 +053068#define Y_INIT_TIMER(a, b, c) setup_deferrable_timer_on_stack(a, b, c)
Arve Hjønnevåg82379112010-05-18 20:35:30 -070069
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
Sujit Reddy Thumma4e239c02011-12-07 15:13:45 +05301677 Y_INIT_TIMER(&timer, yaffs_background_waker,
1678 (unsigned long)current);
Arve Hjønnevåg82379112010-05-18 20:35:30 -07001679 timer.expires = expires + 1;
Arve Hjønnevåg82379112010-05-18 20:35:30 -07001680
1681 set_current_state(TASK_INTERRUPTIBLE);
1682 add_timer(&timer);
1683 schedule();
1684 del_timer_sync(&timer);
1685 }
1686
1687 return 0;
1688}
1689
1690static int yaffs_bg_start(struct yaffs_dev *dev)
1691{
1692 int retval = 0;
1693 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1694
1695 if (dev->read_only)
1696 return -1;
1697
1698 context->bg_running = 1;
1699
1700 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
1701 (void *)dev, "yaffs-bg-%d",
1702 context->mount_id);
1703
1704 if (IS_ERR(context->bg_thread)) {
1705 retval = PTR_ERR(context->bg_thread);
1706 context->bg_thread = NULL;
1707 context->bg_running = 0;
1708 }
1709 return retval;
1710}
1711
1712static void yaffs_bg_stop(struct yaffs_dev *dev)
1713{
1714 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
1715
1716 ctxt->bg_running = 0;
1717
1718 if (ctxt->bg_thread) {
1719 kthread_stop(ctxt->bg_thread);
1720 ctxt->bg_thread = NULL;
1721 }
1722}
1723
1724static void yaffs_write_super(struct super_block *sb)
1725{
1726 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
1727
1728 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1729 "yaffs_write_super%s",
1730 request_checkpoint ? " checkpt" : "");
1731
1732 yaffs_do_sync_fs(sb, request_checkpoint);
1733
1734}
1735
1736static int yaffs_sync_fs(struct super_block *sb, int wait)
1737{
1738 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
1739
1740 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1741 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
1742
1743 yaffs_do_sync_fs(sb, request_checkpoint);
1744
1745 return 0;
1746}
1747
1748
1749static LIST_HEAD(yaffs_context_list);
1750struct mutex yaffs_context_lock;
1751
1752
1753
1754struct yaffs_options {
1755 int inband_tags;
1756 int skip_checkpoint_read;
1757 int skip_checkpoint_write;
1758 int no_cache;
1759 int tags_ecc_on;
1760 int tags_ecc_overridden;
1761 int lazy_loading_enabled;
1762 int lazy_loading_overridden;
1763 int empty_lost_and_found;
1764 int empty_lost_and_found_overridden;
1765};
1766
1767#define MAX_OPT_LEN 30
1768static int yaffs_parse_options(struct yaffs_options *options,
1769 const char *options_str)
1770{
1771 char cur_opt[MAX_OPT_LEN + 1];
1772 int p;
1773 int error = 0;
1774
1775 /* Parse through the options which is a comma seperated list */
1776
1777 while (options_str && *options_str && !error) {
1778 memset(cur_opt, 0, MAX_OPT_LEN + 1);
1779 p = 0;
1780
1781 while (*options_str == ',')
1782 options_str++;
1783
1784 while (*options_str && *options_str != ',') {
1785 if (p < MAX_OPT_LEN) {
1786 cur_opt[p] = *options_str;
1787 p++;
1788 }
1789 options_str++;
1790 }
1791
1792 if (!strcmp(cur_opt, "inband-tags")) {
1793 options->inband_tags = 1;
1794 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
1795 options->tags_ecc_on = 0;
1796 options->tags_ecc_overridden = 1;
1797 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
1798 options->tags_ecc_on = 1;
1799 options->tags_ecc_overridden = 1;
1800 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
1801 options->lazy_loading_enabled = 0;
1802 options->lazy_loading_overridden = 1;
1803 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
1804 options->lazy_loading_enabled = 1;
1805 options->lazy_loading_overridden = 1;
1806 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
1807 options->empty_lost_and_found = 0;
1808 options->empty_lost_and_found_overridden = 1;
1809 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
1810 options->empty_lost_and_found = 1;
1811 options->empty_lost_and_found_overridden = 1;
1812 } else if (!strcmp(cur_opt, "no-cache")) {
1813 options->no_cache = 1;
1814 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
1815 options->skip_checkpoint_read = 1;
1816 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
1817 options->skip_checkpoint_write = 1;
1818 } else if (!strcmp(cur_opt, "no-checkpoint")) {
1819 options->skip_checkpoint_read = 1;
1820 options->skip_checkpoint_write = 1;
1821 } else {
1822 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
1823 cur_opt);
1824 error = 1;
1825 }
1826 }
1827
1828 return error;
1829}
1830
1831static struct address_space_operations yaffs_file_address_operations = {
1832 .readpage = yaffs_readpage,
1833 .writepage = yaffs_writepage,
1834 .write_begin = yaffs_write_begin,
1835 .write_end = yaffs_write_end,
1836};
1837
1838
1839
1840static const struct inode_operations yaffs_file_inode_operations = {
1841 .setattr = yaffs_setattr,
1842#ifdef CONFIG_YAFFS_XATTR
1843 .setxattr = yaffs_setxattr,
1844 .getxattr = yaffs_getxattr,
1845 .listxattr = yaffs_listxattr,
1846 .removexattr = yaffs_removexattr,
1847#endif
1848};
1849
1850static const struct inode_operations yaffs_symlink_inode_operations = {
1851 .readlink = yaffs_readlink,
1852 .follow_link = yaffs_follow_link,
1853 .put_link = yaffs_put_link,
1854 .setattr = yaffs_setattr,
1855#ifdef CONFIG_YAFFS_XATTR
1856 .setxattr = yaffs_setxattr,
1857 .getxattr = yaffs_getxattr,
1858 .listxattr = yaffs_listxattr,
1859 .removexattr = yaffs_removexattr,
1860#endif
1861};
1862
1863static void yaffs_fill_inode_from_obj(struct inode *inode,
1864 struct yaffs_obj *obj)
1865{
1866 if (inode && obj) {
1867
1868 /* Check mode against the variant type and attempt to repair if broken. */
1869 u32 mode = obj->yst_mode;
1870 switch (obj->variant_type) {
1871 case YAFFS_OBJECT_TYPE_FILE:
1872 if (!S_ISREG(mode)) {
1873 obj->yst_mode &= ~S_IFMT;
1874 obj->yst_mode |= S_IFREG;
1875 }
1876
1877 break;
1878 case YAFFS_OBJECT_TYPE_SYMLINK:
1879 if (!S_ISLNK(mode)) {
1880 obj->yst_mode &= ~S_IFMT;
1881 obj->yst_mode |= S_IFLNK;
1882 }
1883
1884 break;
1885 case YAFFS_OBJECT_TYPE_DIRECTORY:
1886 if (!S_ISDIR(mode)) {
1887 obj->yst_mode &= ~S_IFMT;
1888 obj->yst_mode |= S_IFDIR;
1889 }
1890
1891 break;
1892 case YAFFS_OBJECT_TYPE_UNKNOWN:
1893 case YAFFS_OBJECT_TYPE_HARDLINK:
1894 case YAFFS_OBJECT_TYPE_SPECIAL:
1895 default:
1896 /* TODO? */
1897 break;
1898 }
1899
1900 inode->i_flags |= S_NOATIME;
1901
1902 inode->i_ino = obj->obj_id;
1903 inode->i_mode = obj->yst_mode;
1904 inode->i_uid = obj->yst_uid;
1905 inode->i_gid = obj->yst_gid;
1906
1907 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1908
1909 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1910 inode->i_atime.tv_nsec = 0;
1911 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1912 inode->i_mtime.tv_nsec = 0;
1913 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1914 inode->i_ctime.tv_nsec = 0;
1915 inode->i_size = yaffs_get_obj_length(obj);
1916 inode->i_blocks = (inode->i_size + 511) >> 9;
1917
1918 inode->i_nlink = yaffs_get_obj_link_count(obj);
1919
1920 yaffs_trace(YAFFS_TRACE_OS,
1921 "yaffs_fill_inode mode %x uid %d gid %d size %d count %d",
1922 inode->i_mode, inode->i_uid, inode->i_gid,
1923 (int)inode->i_size, atomic_read(&inode->i_count));
1924
1925 switch (obj->yst_mode & S_IFMT) {
1926 default: /* fifo, device or socket */
1927 init_special_inode(inode, obj->yst_mode,
1928 old_decode_dev(obj->yst_rdev));
1929 break;
1930 case S_IFREG: /* file */
1931 inode->i_op = &yaffs_file_inode_operations;
1932 inode->i_fop = &yaffs_file_operations;
1933 inode->i_mapping->a_ops =
1934 &yaffs_file_address_operations;
1935 break;
1936 case S_IFDIR: /* directory */
1937 inode->i_op = &yaffs_dir_inode_operations;
1938 inode->i_fop = &yaffs_dir_operations;
1939 break;
1940 case S_IFLNK: /* symlink */
1941 inode->i_op = &yaffs_symlink_inode_operations;
1942 break;
1943 }
1944
1945 yaffs_inode_to_obj_lv(inode) = obj;
1946
1947 obj->my_inode = inode;
1948
1949 } else {
1950 yaffs_trace(YAFFS_TRACE_OS,
1951 "yaffs_fill_inode invalid parameters");
1952 }
1953}
1954
1955static void yaffs_put_super(struct super_block *sb)
1956{
1957 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1958
1959 yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
1960
1961 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1962 "Shutting down yaffs background thread");
1963 yaffs_bg_stop(dev);
1964 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1965 "yaffs background thread shut down");
1966
1967 yaffs_gross_lock(dev);
1968
1969 yaffs_flush_super(sb, 1);
1970
1971 if (yaffs_dev_to_lc(dev)->put_super_fn)
1972 yaffs_dev_to_lc(dev)->put_super_fn(sb);
1973
1974 yaffs_deinitialise(dev);
1975
1976 yaffs_gross_unlock(dev);
1977 mutex_lock(&yaffs_context_lock);
1978 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
1979 mutex_unlock(&yaffs_context_lock);
1980
1981 if (yaffs_dev_to_lc(dev)->spare_buffer) {
1982 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
1983 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
1984 }
1985
1986 kfree(dev);
1987}
1988
1989static void yaffs_mtd_put_super(struct super_block *sb)
1990{
1991 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
1992
1993 if (mtd->sync)
1994 mtd->sync(mtd);
1995
1996 put_mtd_device(mtd);
1997}
1998
1999static const struct super_operations yaffs_super_ops = {
2000 .statfs = yaffs_statfs,
2001 .put_super = yaffs_put_super,
2002 .evict_inode = yaffs_evict_inode,
2003 .sync_fs = yaffs_sync_fs,
2004 .write_super = yaffs_write_super,
2005};
2006
2007static struct super_block *yaffs_internal_read_super(int yaffs_version,
2008 struct super_block *sb,
2009 void *data, int silent)
2010{
2011 int n_blocks;
2012 struct inode *inode = NULL;
2013 struct dentry *root;
2014 struct yaffs_dev *dev = 0;
2015 char devname_buf[BDEVNAME_SIZE + 1];
2016 struct mtd_info *mtd;
2017 int err;
2018 char *data_str = (char *)data;
2019 struct yaffs_linux_context *context = NULL;
2020 struct yaffs_param *param;
2021
2022 int read_only = 0;
2023
2024 struct yaffs_options options;
2025
2026 unsigned mount_id;
2027 int found;
2028 struct yaffs_linux_context *context_iterator;
2029 struct list_head *l;
2030
2031 sb->s_magic = YAFFS_MAGIC;
2032 sb->s_op = &yaffs_super_ops;
2033 sb->s_flags |= MS_NOATIME;
2034
2035 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2036
2037 sb->s_export_op = &yaffs_export_ops;
2038
2039 if (!sb)
2040 printk(KERN_INFO "yaffs: sb is NULL\n");
2041 else if (!sb->s_dev)
2042 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2043 else if (!yaffs_devname(sb, devname_buf))
2044 printk(KERN_INFO "yaffs: devname is NULL\n");
2045 else
2046 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2047 sb->s_dev,
2048 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2049
2050 if (!data_str)
2051 data_str = "";
2052
2053 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2054
2055 memset(&options, 0, sizeof(options));
2056
2057 if (yaffs_parse_options(&options, data_str)) {
2058 /* Option parsing failed */
2059 return NULL;
2060 }
2061
2062 sb->s_blocksize = PAGE_CACHE_SIZE;
2063 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2064
2065 yaffs_trace(YAFFS_TRACE_OS,
2066 "yaffs_read_super: Using yaffs%d", yaffs_version);
2067 yaffs_trace(YAFFS_TRACE_OS,
2068 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2069
2070 yaffs_trace(YAFFS_TRACE_ALWAYS,
2071 "Attempting MTD mount of %u.%u,\"%s\"",
2072 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2073 yaffs_devname(sb, devname_buf));
2074
2075 /* Check it's an mtd device..... */
2076 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2077 return NULL; /* This isn't an mtd device */
2078
2079 /* Get the device */
2080 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2081 if (!mtd) {
2082 yaffs_trace(YAFFS_TRACE_ALWAYS,
2083 "MTD device #%u doesn't appear to exist",
2084 MINOR(sb->s_dev));
2085 return NULL;
2086 }
2087 /* Check it's NAND */
2088 if (mtd->type != MTD_NANDFLASH) {
2089 yaffs_trace(YAFFS_TRACE_ALWAYS,
2090 "MTD device is not NAND it's type %d",
2091 mtd->type);
2092 return NULL;
2093 }
2094
2095 yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase);
2096 yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read);
2097 yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write);
2098 yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob);
2099 yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob);
2100 yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad);
2101 yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad);
2102 yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
2103 yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
2104 yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
2105 yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
2106
2107#ifdef CONFIG_YAFFS_AUTO_YAFFS2
2108
2109 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2110 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2111 yaffs_version = 2;
2112 }
2113
2114 /* Added NCB 26/5/2006 for completeness */
2115 if (yaffs_version == 2 && !options.inband_tags
2116 && WRITE_SIZE(mtd) == 512) {
2117 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2118 yaffs_version = 1;
2119 }
2120#endif
2121
2122 if (yaffs_version == 2) {
2123 /* Check for version 2 style functions */
2124 if (!mtd->erase ||
2125 !mtd->block_isbad ||
2126 !mtd->block_markbad ||
2127 !mtd->read ||
2128 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2129 yaffs_trace(YAFFS_TRACE_ALWAYS,
2130 "MTD device does not support required functions");
2131 return NULL;
2132 }
2133
2134 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2135 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2136 !options.inband_tags) {
2137 yaffs_trace(YAFFS_TRACE_ALWAYS,
2138 "MTD device does not have the right page sizes");
2139 return NULL;
2140 }
2141 } else {
2142 /* Check for V1 style functions */
2143 if (!mtd->erase ||
2144 !mtd->read ||
2145 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2146 yaffs_trace(YAFFS_TRACE_ALWAYS,
2147 "MTD device does not support required functions");
2148 return NULL;
2149 }
2150
2151 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2152 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2153 yaffs_trace(YAFFS_TRACE_ALWAYS,
2154 "MTD device does not support have the right page sizes");
2155 return NULL;
2156 }
2157 }
2158
2159 /* OK, so if we got here, we have an MTD that's NAND and looks
2160 * like it has the right capabilities
2161 * Set the struct yaffs_dev up for mtd
2162 */
2163
2164 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2165 read_only = 1;
2166 printk(KERN_INFO
2167 "yaffs: mtd is read only, setting superblock read only");
2168 sb->s_flags |= MS_RDONLY;
2169 }
2170
2171 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2172 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2173
2174 if (!dev || !context) {
2175 if (dev)
2176 kfree(dev);
2177 if (context)
2178 kfree(context);
2179 dev = NULL;
2180 context = NULL;
2181 }
2182
2183 if (!dev) {
2184 /* Deep shit could not allocate device structure */
2185 yaffs_trace(YAFFS_TRACE_ALWAYS,
2186 "yaffs_read_super failed trying to allocate yaffs_dev");
2187 return NULL;
2188 }
2189 memset(dev, 0, sizeof(struct yaffs_dev));
2190 param = &(dev->param);
2191
2192 memset(context, 0, sizeof(struct yaffs_linux_context));
2193 dev->os_context = context;
2194 INIT_LIST_HEAD(&(context->context_list));
2195 context->dev = dev;
2196 context->super = sb;
2197
2198 dev->read_only = read_only;
2199
2200 sb->s_fs_info = dev;
2201
2202 dev->driver_context = mtd;
2203 param->name = mtd->name;
2204
2205 /* Set up the memory size parameters.... */
2206
2207 n_blocks =
2208 YCALCBLOCKS(mtd->size,
2209 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2210
2211 param->start_block = 0;
2212 param->end_block = n_blocks - 1;
2213 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2214 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2215 param->n_reserved_blocks = 5;
2216 param->n_caches = (options.no_cache) ? 0 : 10;
2217 param->inband_tags = options.inband_tags;
2218
2219#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2220 param->disable_lazy_load = 1;
2221#endif
2222#ifdef CONFIG_YAFFS_XATTR
2223 param->enable_xattr = 1;
2224#endif
2225 if (options.lazy_loading_overridden)
2226 param->disable_lazy_load = !options.lazy_loading_enabled;
2227
2228#ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2229 param->no_tags_ecc = 1;
2230#endif
2231
2232#ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2233#else
2234 param->defered_dir_update = 1;
2235#endif
2236
2237 if (options.tags_ecc_overridden)
2238 param->no_tags_ecc = !options.tags_ecc_on;
2239
2240#ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2241 param->empty_lost_n_found = 1;
2242#endif
2243
2244#ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2245 param->refresh_period = 0;
2246#else
2247 param->refresh_period = 500;
2248#endif
2249
2250#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2251 param->always_check_erased = 1;
2252#endif
2253
2254 if (options.empty_lost_and_found_overridden)
2255 param->empty_lost_n_found = options.empty_lost_and_found;
2256
2257 /* ... and the functions. */
2258 if (yaffs_version == 2) {
2259 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2260 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2261 param->bad_block_fn = nandmtd2_mark_block_bad;
2262 param->query_block_fn = nandmtd2_query_block;
2263 yaffs_dev_to_lc(dev)->spare_buffer =
2264 kmalloc(mtd->oobsize, GFP_NOFS);
2265 param->is_yaffs2 = 1;
2266 param->total_bytes_per_chunk = mtd->writesize;
2267 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2268 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2269
2270 param->start_block = 0;
2271 param->end_block = n_blocks - 1;
2272 } else {
2273 /* use the MTD interface in yaffs_mtdif1.c */
2274 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2275 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2276 param->bad_block_fn = nandmtd1_mark_block_bad;
2277 param->query_block_fn = nandmtd1_query_block;
2278 param->is_yaffs2 = 0;
2279 }
2280 /* ... and common functions */
2281 param->erase_fn = nandmtd_erase_block;
2282 param->initialise_flash_fn = nandmtd_initialise;
2283
2284 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2285
2286 param->sb_dirty_fn = yaffs_touch_super;
2287 param->gc_control = yaffs_gc_control_callback;
2288
2289 yaffs_dev_to_lc(dev)->super = sb;
2290
2291#ifndef CONFIG_YAFFS_DOES_ECC
2292 param->use_nand_ecc = 1;
2293#endif
2294
2295 param->skip_checkpt_rd = options.skip_checkpoint_read;
2296 param->skip_checkpt_wr = options.skip_checkpoint_write;
2297
2298 mutex_lock(&yaffs_context_lock);
2299 /* Get a mount id */
2300 found = 0;
2301 for (mount_id = 0; !found; mount_id++) {
2302 found = 1;
2303 list_for_each(l, &yaffs_context_list) {
2304 context_iterator =
2305 list_entry(l, struct yaffs_linux_context,
2306 context_list);
2307 if (context_iterator->mount_id == mount_id)
2308 found = 0;
2309 }
2310 }
2311 context->mount_id = mount_id;
2312
2313 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2314 &yaffs_context_list);
2315 mutex_unlock(&yaffs_context_lock);
2316
2317 /* Directory search handling... */
2318 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2319 param->remove_obj_fn = yaffs_remove_obj_callback;
2320
2321 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2322
2323 yaffs_gross_lock(dev);
2324
2325 err = yaffs_guts_initialise(dev);
2326
2327 yaffs_trace(YAFFS_TRACE_OS,
2328 "yaffs_read_super: guts initialised %s",
2329 (err == YAFFS_OK) ? "OK" : "FAILED");
2330
2331 if (err == YAFFS_OK)
2332 yaffs_bg_start(dev);
2333
2334 if (!context->bg_thread)
2335 param->defered_dir_update = 0;
2336
2337 /* Release lock before yaffs_get_inode() */
2338 yaffs_gross_unlock(dev);
2339
2340 /* Create root inode */
2341 if (err == YAFFS_OK)
2342 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2343
2344 if (!inode)
2345 return NULL;
2346
2347 inode->i_op = &yaffs_dir_inode_operations;
2348 inode->i_fop = &yaffs_dir_operations;
2349
2350 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2351
2352 root = d_alloc_root(inode);
2353
2354 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
2355
2356 if (!root) {
2357 iput(inode);
2358 return NULL;
2359 }
2360 sb->s_root = root;
2361 sb->s_dirt = !dev->is_checkpointed;
2362 yaffs_trace(YAFFS_TRACE_ALWAYS,
2363 "yaffs_read_super: is_checkpointed %d",
2364 dev->is_checkpointed);
2365
2366 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
2367 return sb;
2368}
2369
2370static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2371 int silent)
2372{
2373 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2374}
2375
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002376static struct dentry *yaffs_mount(struct file_system_type *fs, int flags,
2377 const char *dev_name, void *data)
Arve Hjønnevåg82379112010-05-18 20:35:30 -07002378{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002379 return mount_bdev(fs, flags, dev_name, data,
2380 yaffs_internal_read_super_mtd);
Arve Hjønnevåg82379112010-05-18 20:35:30 -07002381}
2382
2383static struct file_system_type yaffs_fs_type = {
2384 .owner = THIS_MODULE,
2385 .name = "yaffs",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002386 .mount = yaffs_mount,
Arve Hjønnevåg82379112010-05-18 20:35:30 -07002387 .kill_sb = kill_block_super,
2388 .fs_flags = FS_REQUIRES_DEV,
2389};
2390
2391#ifdef CONFIG_YAFFS_YAFFS2
2392
2393static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2394 int silent)
2395{
2396 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2397}
2398
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002399static struct dentry *yaffs2_mount(struct file_system_type *fs,
2400 int flags, const char *dev_name, void *data)
Arve Hjønnevåg82379112010-05-18 20:35:30 -07002401{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002402 return mount_bdev(fs, flags, dev_name, data,
2403 yaffs2_internal_read_super_mtd);
Arve Hjønnevåg82379112010-05-18 20:35:30 -07002404}
2405
2406static struct file_system_type yaffs2_fs_type = {
2407 .owner = THIS_MODULE,
2408 .name = "yaffs2",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002409 .mount = yaffs2_mount,
Arve Hjønnevåg82379112010-05-18 20:35:30 -07002410 .kill_sb = kill_block_super,
2411 .fs_flags = FS_REQUIRES_DEV,
2412};
2413#endif /* CONFIG_YAFFS_YAFFS2 */
2414
2415static struct proc_dir_entry *my_proc_entry;
2416
2417static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
2418{
2419 struct yaffs_param *param = &dev->param;
2420 buf += sprintf(buf, "start_block........... %d\n", param->start_block);
2421 buf += sprintf(buf, "end_block............. %d\n", param->end_block);
2422 buf += sprintf(buf, "total_bytes_per_chunk. %d\n",
2423 param->total_bytes_per_chunk);
2424 buf += sprintf(buf, "use_nand_ecc.......... %d\n",
2425 param->use_nand_ecc);
2426 buf += sprintf(buf, "no_tags_ecc........... %d\n", param->no_tags_ecc);
2427 buf += sprintf(buf, "is_yaffs2............. %d\n", param->is_yaffs2);
2428 buf += sprintf(buf, "inband_tags........... %d\n", param->inband_tags);
2429 buf += sprintf(buf, "empty_lost_n_found.... %d\n",
2430 param->empty_lost_n_found);
2431 buf += sprintf(buf, "disable_lazy_load..... %d\n",
2432 param->disable_lazy_load);
2433 buf += sprintf(buf, "refresh_period........ %d\n",
2434 param->refresh_period);
2435 buf += sprintf(buf, "n_caches.............. %d\n", param->n_caches);
2436 buf += sprintf(buf, "n_reserved_blocks..... %d\n",
2437 param->n_reserved_blocks);
2438 buf += sprintf(buf, "always_check_erased... %d\n",
2439 param->always_check_erased);
2440
2441 return buf;
2442}
2443
2444static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
2445{
2446 buf +=
2447 sprintf(buf, "data_bytes_per_chunk.. %d\n",
2448 dev->data_bytes_per_chunk);
2449 buf += sprintf(buf, "chunk_grp_bits........ %d\n", dev->chunk_grp_bits);
2450 buf += sprintf(buf, "chunk_grp_size........ %d\n", dev->chunk_grp_size);
2451 buf +=
2452 sprintf(buf, "n_erased_blocks....... %d\n", dev->n_erased_blocks);
2453 buf +=
2454 sprintf(buf, "blocks_in_checkpt..... %d\n", dev->blocks_in_checkpt);
2455 buf += sprintf(buf, "\n");
2456 buf += sprintf(buf, "n_tnodes.............. %d\n", dev->n_tnodes);
2457 buf += sprintf(buf, "n_obj................. %d\n", dev->n_obj);
2458 buf += sprintf(buf, "n_free_chunks......... %d\n", dev->n_free_chunks);
2459 buf += sprintf(buf, "\n");
2460 buf += sprintf(buf, "n_page_writes......... %u\n", dev->n_page_writes);
2461 buf += sprintf(buf, "n_page_reads.......... %u\n", dev->n_page_reads);
2462 buf += sprintf(buf, "n_erasures............ %u\n", dev->n_erasures);
2463 buf += sprintf(buf, "n_gc_copies........... %u\n", dev->n_gc_copies);
2464 buf += sprintf(buf, "all_gcs............... %u\n", dev->all_gcs);
2465 buf +=
2466 sprintf(buf, "passive_gc_count...... %u\n", dev->passive_gc_count);
2467 buf +=
2468 sprintf(buf, "oldest_dirty_gc_count. %u\n",
2469 dev->oldest_dirty_gc_count);
2470 buf += sprintf(buf, "n_gc_blocks........... %u\n", dev->n_gc_blocks);
2471 buf += sprintf(buf, "bg_gcs................ %u\n", dev->bg_gcs);
2472 buf +=
2473 sprintf(buf, "n_retired_writes...... %u\n", dev->n_retired_writes);
2474 buf +=
2475 sprintf(buf, "n_retired_blocks...... %u\n", dev->n_retired_blocks);
2476 buf += sprintf(buf, "n_ecc_fixed........... %u\n", dev->n_ecc_fixed);
2477 buf += sprintf(buf, "n_ecc_unfixed......... %u\n", dev->n_ecc_unfixed);
2478 buf +=
2479 sprintf(buf, "n_tags_ecc_fixed...... %u\n", dev->n_tags_ecc_fixed);
2480 buf +=
2481 sprintf(buf, "n_tags_ecc_unfixed.... %u\n",
2482 dev->n_tags_ecc_unfixed);
2483 buf += sprintf(buf, "cache_hits............ %u\n", dev->cache_hits);
2484 buf +=
2485 sprintf(buf, "n_deleted_files....... %u\n", dev->n_deleted_files);
2486 buf +=
2487 sprintf(buf, "n_unlinked_files...... %u\n", dev->n_unlinked_files);
2488 buf += sprintf(buf, "refresh_count......... %u\n", dev->refresh_count);
2489 buf += sprintf(buf, "n_bg_deletions........ %u\n", dev->n_bg_deletions);
2490
2491 return buf;
2492}
2493
2494static int yaffs_proc_read(char *page,
2495 char **start,
2496 off_t offset, int count, int *eof, void *data)
2497{
2498 struct list_head *item;
2499 char *buf = page;
2500 int step = offset;
2501 int n = 0;
2502
2503 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2504 * We use 'offset' (*ppos) to indicate where we are in dev_list.
2505 * This also assumes the user has posted a read buffer large
2506 * enough to hold the complete output; but that's life in /proc.
2507 */
2508
2509 *(int *)start = 1;
2510
2511 /* Print header first */
2512 if (step == 0)
2513 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__ "\n");
2514 else if (step == 1)
2515 buf += sprintf(buf, "\n");
2516 else {
2517 step -= 2;
2518
2519 mutex_lock(&yaffs_context_lock);
2520
2521 /* Locate and print the Nth entry. Order N-squared but N is small. */
2522 list_for_each(item, &yaffs_context_list) {
2523 struct yaffs_linux_context *dc =
2524 list_entry(item, struct yaffs_linux_context,
2525 context_list);
2526 struct yaffs_dev *dev = dc->dev;
2527
2528 if (n < (step & ~1)) {
2529 n += 2;
2530 continue;
2531 }
2532 if ((step & 1) == 0) {
2533 buf +=
2534 sprintf(buf, "\nDevice %d \"%s\"\n", n,
2535 dev->param.name);
2536 buf = yaffs_dump_dev_part0(buf, dev);
2537 } else {
2538 buf = yaffs_dump_dev_part1(buf, dev);
2539 }
2540
2541 break;
2542 }
2543 mutex_unlock(&yaffs_context_lock);
2544 }
2545
2546 return buf - page < count ? buf - page : count;
2547}
2548
2549
2550/**
2551 * Set the verbosity of the warnings and error messages.
2552 *
2553 * Note that the names can only be a..z or _ with the current code.
2554 */
2555
2556static struct {
2557 char *mask_name;
2558 unsigned mask_bitfield;
2559} mask_flags[] = {
2560 {"allocate", YAFFS_TRACE_ALLOCATE},
2561 {"always", YAFFS_TRACE_ALWAYS},
2562 {"background", YAFFS_TRACE_BACKGROUND},
2563 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2564 {"buffers", YAFFS_TRACE_BUFFERS},
2565 {"bug", YAFFS_TRACE_BUG},
2566 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2567 {"deletion", YAFFS_TRACE_DELETION},
2568 {"erase", YAFFS_TRACE_ERASE},
2569 {"error", YAFFS_TRACE_ERROR},
2570 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2571 {"gc", YAFFS_TRACE_GC},
2572 {"lock", YAFFS_TRACE_LOCK},
2573 {"mtd", YAFFS_TRACE_MTD},
2574 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2575 {"os", YAFFS_TRACE_OS},
2576 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2577 {"scan", YAFFS_TRACE_SCAN},
2578 {"mount", YAFFS_TRACE_MOUNT},
2579 {"tracing", YAFFS_TRACE_TRACING},
2580 {"sync", YAFFS_TRACE_SYNC},
2581 {"write", YAFFS_TRACE_WRITE},
2582 {"verify", YAFFS_TRACE_VERIFY},
2583 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2584 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2585 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2586 {"all", 0xffffffff},
2587 {"none", 0},
2588 {NULL, 0},
2589};
2590
2591#define MAX_MASK_NAME_LENGTH 40
2592static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2593 unsigned long count, void *data)
2594{
2595 unsigned rg = 0, mask_bitfield;
2596 char *end;
2597 char *mask_name;
2598 const char *x;
2599 char substring[MAX_MASK_NAME_LENGTH + 1];
2600 int i;
2601 int done = 0;
2602 int add, len = 0;
2603 int pos = 0;
2604
2605 rg = yaffs_trace_mask;
2606
2607 while (!done && (pos < count)) {
2608 done = 1;
2609 while ((pos < count) && isspace(buf[pos]))
2610 pos++;
2611
2612 switch (buf[pos]) {
2613 case '+':
2614 case '-':
2615 case '=':
2616 add = buf[pos];
2617 pos++;
2618 break;
2619
2620 default:
2621 add = ' ';
2622 break;
2623 }
2624 mask_name = NULL;
2625
2626 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2627
2628 if (end > buf + pos) {
2629 mask_name = "numeral";
2630 len = end - (buf + pos);
2631 pos += len;
2632 done = 0;
2633 } else {
2634 for (x = buf + pos, i = 0;
2635 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2636 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2637 substring[i] = *x;
2638 substring[i] = '\0';
2639
2640 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2641 if (strcmp(substring, mask_flags[i].mask_name)
2642 == 0) {
2643 mask_name = mask_flags[i].mask_name;
2644 mask_bitfield =
2645 mask_flags[i].mask_bitfield;
2646 done = 0;
2647 break;
2648 }
2649 }
2650 }
2651
2652 if (mask_name != NULL) {
2653 done = 0;
2654 switch (add) {
2655 case '-':
2656 rg &= ~mask_bitfield;
2657 break;
2658 case '+':
2659 rg |= mask_bitfield;
2660 break;
2661 case '=':
2662 rg = mask_bitfield;
2663 break;
2664 default:
2665 rg |= mask_bitfield;
2666 break;
2667 }
2668 }
2669 }
2670
2671 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
2672
2673 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
2674
2675 if (rg & YAFFS_TRACE_ALWAYS) {
2676 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2677 char flag;
2678 flag = ((rg & mask_flags[i].mask_bitfield) ==
2679 mask_flags[i].mask_bitfield) ? '+' : '-';
2680 printk(KERN_DEBUG "%c%s\n", flag,
2681 mask_flags[i].mask_name);
2682 }
2683 }
2684
2685 return count;
2686}
2687
2688static int yaffs_proc_write(struct file *file, const char *buf,
2689 unsigned long count, void *data)
2690{
2691 return yaffs_proc_write_trace_options(file, buf, count, data);
2692}
2693
2694/* Stuff to handle installation of file systems */
2695struct file_system_to_install {
2696 struct file_system_type *fst;
2697 int installed;
2698};
2699
2700static struct file_system_to_install fs_to_install[] = {
2701 {&yaffs_fs_type, 0},
2702 {&yaffs2_fs_type, 0},
2703 {NULL, 0}
2704};
2705
2706static int __init init_yaffs_fs(void)
2707{
2708 int error = 0;
2709 struct file_system_to_install *fsinst;
2710
2711 yaffs_trace(YAFFS_TRACE_ALWAYS,
2712 "yaffs built " __DATE__ " " __TIME__ " Installing.");
2713
2714#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2715 yaffs_trace(YAFFS_TRACE_ALWAYS,
2716 "\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n");
2717#endif
2718
2719 mutex_init(&yaffs_context_lock);
2720
2721 /* Install the proc_fs entries */
2722 my_proc_entry = create_proc_entry("yaffs",
2723 S_IRUGO | S_IFREG, YPROC_ROOT);
2724
2725 if (my_proc_entry) {
2726 my_proc_entry->write_proc = yaffs_proc_write;
2727 my_proc_entry->read_proc = yaffs_proc_read;
2728 my_proc_entry->data = NULL;
2729 } else {
2730 return -ENOMEM;
2731 }
2732
2733
2734 /* Now add the file system entries */
2735
2736 fsinst = fs_to_install;
2737
2738 while (fsinst->fst && !error) {
2739 error = register_filesystem(fsinst->fst);
2740 if (!error)
2741 fsinst->installed = 1;
2742 fsinst++;
2743 }
2744
2745 /* Any errors? uninstall */
2746 if (error) {
2747 fsinst = fs_to_install;
2748
2749 while (fsinst->fst) {
2750 if (fsinst->installed) {
2751 unregister_filesystem(fsinst->fst);
2752 fsinst->installed = 0;
2753 }
2754 fsinst++;
2755 }
2756 }
2757
2758 return error;
2759}
2760
2761static void __exit exit_yaffs_fs(void)
2762{
2763
2764 struct file_system_to_install *fsinst;
2765
2766 yaffs_trace(YAFFS_TRACE_ALWAYS,
2767 "yaffs built " __DATE__ " " __TIME__ " removing.");
2768
2769 remove_proc_entry("yaffs", YPROC_ROOT);
2770
2771 fsinst = fs_to_install;
2772
2773 while (fsinst->fst) {
2774 if (fsinst->installed) {
2775 unregister_filesystem(fsinst->fst);
2776 fsinst->installed = 0;
2777 }
2778 fsinst++;
2779 }
2780}
2781
2782module_init(init_yaffs_fs)
2783 module_exit(exit_yaffs_fs)
2784
2785 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2786MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
2787MODULE_LICENSE("GPL");