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