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