• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2009 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 yaffs_Device associated with
28  *         this superblock
29  * >> 2.6: sb->s_fs_info  points to the yaffs_Device associated with this
30  *         superblock
31  * >> inode->u.generic_ip points to the associated yaffs_Object.
32  */
33 
34 const char *yaffs_fs_c_version =
35     "$Id$";
36 extern const char *yaffs_guts_c_version;
37 
38 #include <linux/version.h>
39 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
40 #include <linux/config.h>
41 #endif
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/init.h>
46 #include <linux/fs.h>
47 #include <linux/proc_fs.h>
48 #include <linux/smp_lock.h>
49 #include <linux/pagemap.h>
50 #include <linux/mtd/mtd.h>
51 #include <linux/interrupt.h>
52 #include <linux/string.h>
53 #include <linux/ctype.h>
54 
55 #include "asm/div64.h"
56 
57 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
58 
59 #include <linux/statfs.h>	/* Added NCB 15-8-2003 */
60 #include <linux/statfs.h>
61 #define UnlockPage(p) unlock_page(p)
62 #define Page_Uptodate(page)	test_bit(PG_uptodate, &(page)->flags)
63 
64 /* FIXME: use sb->s_id instead ? */
65 #define yaffs_devname(sb, buf)	bdevname(sb->s_bdev, buf)
66 
67 #else
68 
69 #include <linux/locks.h>
70 #define	BDEVNAME_SIZE		0
71 #define	yaffs_devname(sb, buf)	kdevname(sb->s_dev)
72 
73 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
74 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
75 #define __user
76 #endif
77 
78 #endif
79 
80 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
81 #define YPROC_ROOT  (&proc_root)
82 #else
83 #define YPROC_ROOT  NULL
84 #endif
85 
86 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
87 #define WRITE_SIZE_STR "writesize"
88 #define WRITE_SIZE(mtd) ((mtd)->writesize)
89 #else
90 #define WRITE_SIZE_STR "oobblock"
91 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
92 #endif
93 
94 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
95 #define YAFFS_USE_WRITE_BEGIN_END 1
96 #else
97 #define YAFFS_USE_WRITE_BEGIN_END 0
98 #endif
99 
100 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
YCALCBLOCKS(uint64_t partition_size,uint32_t block_size)101 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
102 {
103 	uint64_t result = partition_size;
104 	do_div(result, block_size);
105 	return (uint32_t)result;
106 }
107 #else
108 #define YCALCBLOCKS(s, b) ((s)/(b))
109 #endif
110 
111 #include <linux/uaccess.h>
112 
113 #include "yportenv.h"
114 #include "yaffs_guts.h"
115 
116 #include <linux/mtd/mtd.h>
117 #include "yaffs_mtdif.h"
118 #include "yaffs_mtdif1.h"
119 #include "yaffs_mtdif2.h"
120 
121 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
122 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
123 unsigned int yaffs_auto_checkpoint = 1;
124 
125 /* Module Parameters */
126 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
127 module_param(yaffs_traceMask, uint, 0644);
128 module_param(yaffs_wr_attempts, uint, 0644);
129 module_param(yaffs_auto_checkpoint, uint, 0644);
130 #else
131 MODULE_PARM(yaffs_traceMask, "i");
132 MODULE_PARM(yaffs_wr_attempts, "i");
133 MODULE_PARM(yaffs_auto_checkpoint, "i");
134 #endif
135 
136 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
137 /* use iget and read_inode */
138 #define Y_IGET(sb, inum) iget((sb), (inum))
139 static void yaffs_read_inode(struct inode *inode);
140 
141 #else
142 /* Call local equivalent */
143 #define YAFFS_USE_OWN_IGET
144 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
145 
146 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
147 #endif
148 
149 /*#define T(x) printk x */
150 
151 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
152 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
153 #else
154 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
155 #endif
156 
157 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
158 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
159 
160 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
161 #define yaffs_SuperToDevice(sb)	((yaffs_Device *)sb->s_fs_info)
162 #else
163 #define yaffs_SuperToDevice(sb)	((yaffs_Device *)sb->u.generic_sbp)
164 #endif
165 
166 
167 #define update_dir_time(dir) do {\
168 			(dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
169 		} while(0)
170 
171 static void yaffs_put_super(struct super_block *sb);
172 
173 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
174 				loff_t *pos);
175 static ssize_t yaffs_hold_space(struct file *f);
176 static void yaffs_release_space(struct file *f);
177 
178 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
179 static int yaffs_file_flush(struct file *file, fl_owner_t id);
180 #else
181 static int yaffs_file_flush(struct file *file);
182 #endif
183 
184 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
185 				int datasync);
186 
187 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
188 
189 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
190 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
191 			struct nameidata *n);
192 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
193 					struct nameidata *n);
194 #else
195 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
196 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
197 #endif
198 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
199 			struct dentry *dentry);
200 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
201 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
202 			const char *symname);
203 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
204 
205 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
206 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
207 			dev_t dev);
208 #else
209 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
210 			int dev);
211 #endif
212 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
213 			struct inode *new_dir, struct dentry *new_dentry);
214 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
215 
216 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
217 static int yaffs_sync_fs(struct super_block *sb, int wait);
218 static void yaffs_write_super(struct super_block *sb);
219 #else
220 static int yaffs_sync_fs(struct super_block *sb);
221 static int yaffs_write_super(struct super_block *sb);
222 #endif
223 
224 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
225 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
226 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
227 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
228 #else
229 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
230 #endif
231 
232 #ifdef YAFFS_HAS_PUT_INODE
233 static void yaffs_put_inode(struct inode *inode);
234 #endif
235 
236 static void yaffs_delete_inode(struct inode *);
237 static void yaffs_clear_inode(struct inode *);
238 
239 static int yaffs_readpage(struct file *file, struct page *page);
240 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
241 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
242 #else
243 static int yaffs_writepage(struct page *page);
244 #endif
245 
246 
247 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
248 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
249 				loff_t pos, unsigned len, unsigned flags,
250 				struct page **pagep, void **fsdata);
251 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
252 				loff_t pos, unsigned len, unsigned copied,
253 				struct page *pg, void *fsdadata);
254 #else
255 static int yaffs_prepare_write(struct file *f, struct page *pg,
256 				unsigned offset, unsigned to);
257 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
258 				unsigned to);
259 
260 #endif
261 
262 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
263 				int buflen);
264 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
265 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
266 #else
267 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
268 #endif
269 
270 static struct address_space_operations yaffs_file_address_operations = {
271 	.readpage = yaffs_readpage,
272 	.writepage = yaffs_writepage,
273 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
274 	.write_begin = yaffs_write_begin,
275 	.write_end = yaffs_write_end,
276 #else
277 	.prepare_write = yaffs_prepare_write,
278 	.commit_write = yaffs_commit_write,
279 #endif
280 };
281 
282 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
283 static const struct file_operations yaffs_file_operations = {
284 	.read = do_sync_read,
285 	.write = do_sync_write,
286 	.aio_read = generic_file_aio_read,
287 	.aio_write = generic_file_aio_write,
288 	.mmap = generic_file_mmap,
289 	.flush = yaffs_file_flush,
290 	.fsync = yaffs_sync_object,
291 	.splice_read = generic_file_splice_read,
292 	.splice_write = generic_file_splice_write,
293 	.llseek = generic_file_llseek,
294 };
295 
296 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
297 
298 static const struct file_operations yaffs_file_operations = {
299 	.read = do_sync_read,
300 	.write = do_sync_write,
301 	.aio_read = generic_file_aio_read,
302 	.aio_write = generic_file_aio_write,
303 	.mmap = generic_file_mmap,
304 	.flush = yaffs_file_flush,
305 	.fsync = yaffs_sync_object,
306 	.sendfile = generic_file_sendfile,
307 };
308 
309 #else
310 
311 static const struct file_operations yaffs_file_operations = {
312 	.read = generic_file_read,
313 	.write = generic_file_write,
314 	.mmap = generic_file_mmap,
315 	.flush = yaffs_file_flush,
316 	.fsync = yaffs_sync_object,
317 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
318 	.sendfile = generic_file_sendfile,
319 #endif
320 };
321 #endif
322 
323 static const struct inode_operations yaffs_file_inode_operations = {
324 	.setattr = yaffs_setattr,
325 };
326 
327 static const struct inode_operations yaffs_symlink_inode_operations = {
328 	.readlink = yaffs_readlink,
329 	.follow_link = yaffs_follow_link,
330 	.setattr = yaffs_setattr,
331 };
332 
333 static const struct inode_operations yaffs_dir_inode_operations = {
334 	.create = yaffs_create,
335 	.lookup = yaffs_lookup,
336 	.link = yaffs_link,
337 	.unlink = yaffs_unlink,
338 	.symlink = yaffs_symlink,
339 	.mkdir = yaffs_mkdir,
340 	.rmdir = yaffs_unlink,
341 	.mknod = yaffs_mknod,
342 	.rename = yaffs_rename,
343 	.setattr = yaffs_setattr,
344 };
345 
346 static const struct file_operations yaffs_dir_operations = {
347 	.read = generic_read_dir,
348 	.readdir = yaffs_readdir,
349 	.fsync = yaffs_sync_object,
350 };
351 
352 static const struct super_operations yaffs_super_ops = {
353 	.statfs = yaffs_statfs,
354 
355 #ifndef YAFFS_USE_OWN_IGET
356 	.read_inode = yaffs_read_inode,
357 #endif
358 #ifdef YAFFS_HAS_PUT_INODE
359 	.put_inode = yaffs_put_inode,
360 #endif
361 	.put_super = yaffs_put_super,
362 	.delete_inode = yaffs_delete_inode,
363 	.clear_inode = yaffs_clear_inode,
364 	.sync_fs = yaffs_sync_fs,
365 	.write_super = yaffs_write_super,
366 };
367 
yaffs_GrossLock(yaffs_Device * dev)368 static void yaffs_GrossLock(yaffs_Device *dev)
369 {
370 	T(YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
371 	down(&dev->grossLock);
372 	T(YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
373 }
374 
yaffs_GrossUnlock(yaffs_Device * dev)375 static void yaffs_GrossUnlock(yaffs_Device *dev)
376 {
377 	T(YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
378 	up(&dev->grossLock);
379 }
380 
yaffs_readlink(struct dentry * dentry,char __user * buffer,int buflen)381 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
382 			int buflen)
383 {
384 	unsigned char *alias;
385 	int ret;
386 
387 	yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
388 
389 	yaffs_GrossLock(dev);
390 
391 	alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
392 
393 	yaffs_GrossUnlock(dev);
394 
395 	if (!alias)
396 		return -ENOMEM;
397 
398 	ret = vfs_readlink(dentry, buffer, buflen, alias);
399 	kfree(alias);
400 	return ret;
401 }
402 
403 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
yaffs_follow_link(struct dentry * dentry,struct nameidata * nd)404 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
405 #else
406 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
407 #endif
408 {
409 	unsigned char *alias;
410 	int ret;
411 	yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
412 
413 	yaffs_GrossLock(dev);
414 
415 	alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
416 
417 	yaffs_GrossUnlock(dev);
418 
419 	if (!alias) {
420 		ret = -ENOMEM;
421 		goto out;
422 	}
423 
424 	ret = vfs_follow_link(nd, alias);
425 	kfree(alias);
426 out:
427 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
428 	return ERR_PTR(ret);
429 #else
430 	return ret;
431 #endif
432 }
433 
434 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
435 				yaffs_Object *obj);
436 
437 /*
438  * Lookup is used to find objects in the fs
439  */
440 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
441 
yaffs_lookup(struct inode * dir,struct dentry * dentry,struct nameidata * n)442 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
443 				struct nameidata *n)
444 #else
445 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
446 #endif
447 {
448 	yaffs_Object *obj;
449 	struct inode *inode = NULL;	/* NCB 2.5/2.6 needs NULL here */
450 
451 	yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
452 
453 	yaffs_GrossLock(dev);
454 
455 	T(YAFFS_TRACE_OS,
456 		("yaffs_lookup for %d:%s\n",
457 		yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
458 
459 	obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
460 					dentry->d_name.name);
461 
462 	obj = yaffs_GetEquivalentObject(obj);	/* in case it was a hardlink */
463 
464 	/* Can't hold gross lock when calling yaffs_get_inode() */
465 	yaffs_GrossUnlock(dev);
466 
467 	if (obj) {
468 		T(YAFFS_TRACE_OS,
469 			("yaffs_lookup found %d\n", obj->objectId));
470 
471 		inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
472 
473 		if (inode) {
474 			T(YAFFS_TRACE_OS,
475 				("yaffs_loookup dentry \n"));
476 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
477  * d_add even if NULL inode */
478 #if 0
479 			/*dget(dentry); // try to solve directory bug */
480 			d_add(dentry, inode);
481 
482 			/* return dentry; */
483 			return NULL;
484 #endif
485 		}
486 
487 	} else {
488 		T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
489 
490 	}
491 
492 /* added NCB for 2.5/6 compatability - forces add even if inode is
493  * NULL which creates dentry hash */
494 	d_add(dentry, inode);
495 
496 	return NULL;
497 }
498 
499 
500 #ifdef YAFFS_HAS_PUT_INODE
501 
502 /* For now put inode is just for debugging
503  * Put inode is called when the inode **structure** is put.
504  */
yaffs_put_inode(struct inode * inode)505 static void yaffs_put_inode(struct inode *inode)
506 {
507 	T(YAFFS_TRACE_OS,
508 		("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
509 		atomic_read(&inode->i_count)));
510 
511 }
512 #endif
513 
514 /* clear is called to tell the fs to release any per-inode data it holds */
yaffs_clear_inode(struct inode * inode)515 static void yaffs_clear_inode(struct inode *inode)
516 {
517 	yaffs_Object *obj;
518 	yaffs_Device *dev;
519 
520 	obj = yaffs_InodeToObject(inode);
521 
522 	T(YAFFS_TRACE_OS,
523 		("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
524 		atomic_read(&inode->i_count),
525 		obj ? "object exists" : "null object"));
526 
527 	if (obj) {
528 		dev = obj->myDev;
529 		yaffs_GrossLock(dev);
530 
531 		/* Clear the association between the inode and
532 		 * the yaffs_Object.
533 		 */
534 		obj->myInode = NULL;
535 		yaffs_InodeToObjectLV(inode) = NULL;
536 
537 		/* If the object freeing was deferred, then the real
538 		 * free happens now.
539 		 * This should fix the inode inconsistency problem.
540 		 */
541 
542 		yaffs_HandleDeferedFree(obj);
543 
544 		yaffs_GrossUnlock(dev);
545 	}
546 
547 }
548 
549 /* delete is called when the link count is zero and the inode
550  * is put (ie. nobody wants to know about it anymore, time to
551  * delete the file).
552  * NB Must call clear_inode()
553  */
yaffs_delete_inode(struct inode * inode)554 static void yaffs_delete_inode(struct inode *inode)
555 {
556 	yaffs_Object *obj = yaffs_InodeToObject(inode);
557 	yaffs_Device *dev;
558 
559 	T(YAFFS_TRACE_OS,
560 		("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
561 		atomic_read(&inode->i_count),
562 		obj ? "object exists" : "null object"));
563 
564 	if (obj) {
565 		dev = obj->myDev;
566 		yaffs_GrossLock(dev);
567 		yaffs_DeleteObject(obj);
568 		yaffs_GrossUnlock(dev);
569 	}
570 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
571 	truncate_inode_pages(&inode->i_data, 0);
572 #endif
573 	clear_inode(inode);
574 }
575 
576 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
yaffs_file_flush(struct file * file,fl_owner_t id)577 static int yaffs_file_flush(struct file *file, fl_owner_t id)
578 #else
579 static int yaffs_file_flush(struct file *file)
580 #endif
581 {
582 	yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
583 
584 	yaffs_Device *dev = obj->myDev;
585 
586 	T(YAFFS_TRACE_OS,
587 		("yaffs_file_flush object %d (%s)\n", obj->objectId,
588 		obj->dirty ? "dirty" : "clean"));
589 
590 	yaffs_GrossLock(dev);
591 
592 	yaffs_FlushFile(obj, 1);
593 
594 	yaffs_GrossUnlock(dev);
595 
596 	return 0;
597 }
598 
yaffs_readpage_nolock(struct file * f,struct page * pg)599 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
600 {
601 	/* Lifted from jffs2 */
602 
603 	yaffs_Object *obj;
604 	unsigned char *pg_buf;
605 	int ret;
606 
607 	yaffs_Device *dev;
608 
609 	T(YAFFS_TRACE_OS, ("yaffs_readpage at %08x, size %08x\n",
610 			(unsigned)(pg->index << PAGE_CACHE_SHIFT),
611 			(unsigned)PAGE_CACHE_SIZE));
612 
613 	obj = yaffs_DentryToObject(f->f_dentry);
614 
615 	dev = obj->myDev;
616 
617 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
618 	BUG_ON(!PageLocked(pg));
619 #else
620 	if (!PageLocked(pg))
621 		PAGE_BUG(pg);
622 #endif
623 
624 	pg_buf = kmap(pg);
625 	/* FIXME: Can kmap fail? */
626 
627 	yaffs_GrossLock(dev);
628 
629 	ret = yaffs_ReadDataFromFile(obj, pg_buf,
630 				pg->index << PAGE_CACHE_SHIFT,
631 				PAGE_CACHE_SIZE);
632 
633 	yaffs_GrossUnlock(dev);
634 
635 	if (ret >= 0)
636 		ret = 0;
637 
638 	if (ret) {
639 		ClearPageUptodate(pg);
640 		SetPageError(pg);
641 	} else {
642 		SetPageUptodate(pg);
643 		ClearPageError(pg);
644 	}
645 
646 	flush_dcache_page(pg);
647 	kunmap(pg);
648 
649 	T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
650 	return ret;
651 }
652 
yaffs_readpage_unlock(struct file * f,struct page * pg)653 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
654 {
655 	int ret = yaffs_readpage_nolock(f, pg);
656 	UnlockPage(pg);
657 	return ret;
658 }
659 
yaffs_readpage(struct file * f,struct page * pg)660 static int yaffs_readpage(struct file *f, struct page *pg)
661 {
662 	return yaffs_readpage_unlock(f, pg);
663 }
664 
665 /* writepage inspired by/stolen from smbfs */
666 
667 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
yaffs_writepage(struct page * page,struct writeback_control * wbc)668 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
669 #else
670 static int yaffs_writepage(struct page *page)
671 #endif
672 {
673 	struct address_space *mapping = page->mapping;
674 	loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
675 	struct inode *inode;
676 	unsigned long end_index;
677 	char *buffer;
678 	yaffs_Object *obj;
679 	int nWritten = 0;
680 	unsigned nBytes;
681 
682 	if (!mapping)
683 		BUG();
684 	inode = mapping->host;
685 	if (!inode)
686 		BUG();
687 
688 	if (offset > inode->i_size) {
689 		T(YAFFS_TRACE_OS,
690 			("yaffs_writepage at %08x, inode size = %08x!!!\n",
691 			(unsigned)(page->index << PAGE_CACHE_SHIFT),
692 			(unsigned)inode->i_size));
693 		T(YAFFS_TRACE_OS,
694 			("                -> don't care!!\n"));
695 		unlock_page(page);
696 		return 0;
697 	}
698 
699 	end_index = inode->i_size >> PAGE_CACHE_SHIFT;
700 
701 	/* easy case */
702 	if (page->index < end_index)
703 		nBytes = PAGE_CACHE_SIZE;
704 	else
705 		nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
706 
707 	get_page(page);
708 
709 	buffer = kmap(page);
710 
711 	obj = yaffs_InodeToObject(inode);
712 	yaffs_GrossLock(obj->myDev);
713 
714 	T(YAFFS_TRACE_OS,
715 		("yaffs_writepage at %08x, size %08x\n",
716 		(unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
717 	T(YAFFS_TRACE_OS,
718 		("writepag0: obj = %05x, ino = %05x\n",
719 		(int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
720 
721 	nWritten = yaffs_WriteDataToFile(obj, buffer,
722 			page->index << PAGE_CACHE_SHIFT, nBytes, 0);
723 
724 	T(YAFFS_TRACE_OS,
725 		("writepag1: obj = %05x, ino = %05x\n",
726 		(int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
727 
728 	yaffs_GrossUnlock(obj->myDev);
729 
730 	kunmap(page);
731 	SetPageUptodate(page);
732 	UnlockPage(page);
733 	put_page(page);
734 
735 	return (nWritten == nBytes) ? 0 : -ENOSPC;
736 }
737 
738 
739 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
yaffs_write_begin(struct file * filp,struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata)740 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
741 				loff_t pos, unsigned len, unsigned flags,
742 				struct page **pagep, void **fsdata)
743 {
744 	struct page *pg = NULL;
745 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
746 	uint32_t offset = pos & (PAGE_CACHE_SIZE - 1);
747 	uint32_t to = offset + len;
748 
749 	int ret = 0;
750 	int space_held = 0;
751 
752 	T(YAFFS_TRACE_OS, ("start yaffs_write_begin\n"));
753 	/* Get a page */
754 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28)
755 	pg = grab_cache_page_write_begin(mapping, index, flags);
756 #else
757 	pg = __grab_cache_page(mapping, index);
758 #endif
759 
760 	*pagep = pg;
761 	if (!pg) {
762 		ret =  -ENOMEM;
763 		goto out;
764 	}
765 	/* Get fs space */
766 	space_held = yaffs_hold_space(filp);
767 
768 	if (!space_held) {
769 		ret = -ENOSPC;
770 		goto out;
771 	}
772 
773 	/* Update page if required */
774 
775 	if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
776 		ret = yaffs_readpage_nolock(filp, pg);
777 
778 	if (ret)
779 		goto out;
780 
781 	/* Happy path return */
782 	T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
783 
784 	return 0;
785 
786 out:
787 	T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
788 	if (space_held)
789 		yaffs_release_space(filp);
790 	if (pg) {
791 		unlock_page(pg);
792 		page_cache_release(pg);
793 	}
794 	return ret;
795 }
796 
797 #else
798 
yaffs_prepare_write(struct file * f,struct page * pg,unsigned offset,unsigned to)799 static int yaffs_prepare_write(struct file *f, struct page *pg,
800 				unsigned offset, unsigned to)
801 {
802 	T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
803 
804 	if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
805 		return yaffs_readpage_nolock(f, pg);
806 	return 0;
807 }
808 #endif
809 
810 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
yaffs_write_end(struct file * filp,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * pg,void * fsdadata)811 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
812 				loff_t pos, unsigned len, unsigned copied,
813 				struct page *pg, void *fsdadata)
814 {
815 	int ret = 0;
816 	void *addr, *kva;
817 	uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
818 
819 	kva = kmap(pg);
820 	addr = kva + offset_into_page;
821 
822 	T(YAFFS_TRACE_OS,
823 		("yaffs_write_end addr %x pos %x nBytes %d\n",
824 		(unsigned) addr,
825 		(int)pos, copied));
826 
827 	ret = yaffs_file_write(filp, addr, copied, &pos);
828 
829 	if (ret != copied) {
830 		T(YAFFS_TRACE_OS,
831 			("yaffs_write_end not same size ret %d  copied %d\n",
832 			ret, copied));
833 		SetPageError(pg);
834 		ClearPageUptodate(pg);
835 	} else {
836 		SetPageUptodate(pg);
837 	}
838 
839 	kunmap(pg);
840 
841 	yaffs_release_space(filp);
842 	unlock_page(pg);
843 	page_cache_release(pg);
844 	return ret;
845 }
846 #else
847 
yaffs_commit_write(struct file * f,struct page * pg,unsigned offset,unsigned to)848 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
849 				unsigned to)
850 {
851 	void *addr, *kva;
852 
853 	loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
854 	int nBytes = to - offset;
855 	int nWritten;
856 
857 	unsigned spos = pos;
858 	unsigned saddr;
859 
860 	kva = kmap(pg);
861 	addr = kva + offset;
862 
863 	saddr = (unsigned) addr;
864 
865 	T(YAFFS_TRACE_OS,
866 		("yaffs_commit_write addr %x pos %x nBytes %d\n",
867 		saddr, spos, nBytes));
868 
869 	nWritten = yaffs_file_write(f, addr, nBytes, &pos);
870 
871 	if (nWritten != nBytes) {
872 		T(YAFFS_TRACE_OS,
873 			("yaffs_commit_write not same size nWritten %d  nBytes %d\n",
874 			nWritten, nBytes));
875 		SetPageError(pg);
876 		ClearPageUptodate(pg);
877 	} else {
878 		SetPageUptodate(pg);
879 	}
880 
881 	kunmap(pg);
882 
883 	T(YAFFS_TRACE_OS,
884 		("yaffs_commit_write returning %d\n",
885 		nWritten == nBytes ? 0 : nWritten));
886 
887 	return nWritten == nBytes ? 0 : nWritten;
888 }
889 #endif
890 
891 
yaffs_FillInodeFromObject(struct inode * inode,yaffs_Object * obj)892 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
893 {
894 	if (inode && obj) {
895 
896 
897 		/* Check mode against the variant type and attempt to repair if broken. */
898 		__u32 mode = obj->yst_mode;
899 		switch (obj->variantType) {
900 		case YAFFS_OBJECT_TYPE_FILE:
901 			if (!S_ISREG(mode)) {
902 				obj->yst_mode &= ~S_IFMT;
903 				obj->yst_mode |= S_IFREG;
904 			}
905 
906 			break;
907 		case YAFFS_OBJECT_TYPE_SYMLINK:
908 			if (!S_ISLNK(mode)) {
909 				obj->yst_mode &= ~S_IFMT;
910 				obj->yst_mode |= S_IFLNK;
911 			}
912 
913 			break;
914 		case YAFFS_OBJECT_TYPE_DIRECTORY:
915 			if (!S_ISDIR(mode)) {
916 				obj->yst_mode &= ~S_IFMT;
917 				obj->yst_mode |= S_IFDIR;
918 			}
919 
920 			break;
921 		case YAFFS_OBJECT_TYPE_UNKNOWN:
922 		case YAFFS_OBJECT_TYPE_HARDLINK:
923 		case YAFFS_OBJECT_TYPE_SPECIAL:
924 		default:
925 			/* TODO? */
926 			break;
927 		}
928 
929 		inode->i_flags |= S_NOATIME;
930 
931 		inode->i_ino = obj->objectId;
932 		inode->i_mode = obj->yst_mode;
933 		inode->i_uid = obj->yst_uid;
934 		inode->i_gid = obj->yst_gid;
935 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
936 		inode->i_blksize = inode->i_sb->s_blocksize;
937 #endif
938 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
939 
940 		inode->i_rdev = old_decode_dev(obj->yst_rdev);
941 		inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
942 		inode->i_atime.tv_nsec = 0;
943 		inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
944 		inode->i_mtime.tv_nsec = 0;
945 		inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
946 		inode->i_ctime.tv_nsec = 0;
947 #else
948 		inode->i_rdev = obj->yst_rdev;
949 		inode->i_atime = obj->yst_atime;
950 		inode->i_mtime = obj->yst_mtime;
951 		inode->i_ctime = obj->yst_ctime;
952 #endif
953 		inode->i_size = yaffs_GetObjectFileLength(obj);
954 		inode->i_blocks = (inode->i_size + 511) >> 9;
955 
956 		inode->i_nlink = yaffs_GetObjectLinkCount(obj);
957 
958 		T(YAFFS_TRACE_OS,
959 			("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
960 			inode->i_mode, inode->i_uid, inode->i_gid,
961 			(int)inode->i_size, atomic_read(&inode->i_count)));
962 
963 		switch (obj->yst_mode & S_IFMT) {
964 		default:	/* fifo, device or socket */
965 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
966 			init_special_inode(inode, obj->yst_mode,
967 					old_decode_dev(obj->yst_rdev));
968 #else
969 			init_special_inode(inode, obj->yst_mode,
970 					(dev_t) (obj->yst_rdev));
971 #endif
972 			break;
973 		case S_IFREG:	/* file */
974 			inode->i_op = &yaffs_file_inode_operations;
975 			inode->i_fop = &yaffs_file_operations;
976 			inode->i_mapping->a_ops =
977 				&yaffs_file_address_operations;
978 			break;
979 		case S_IFDIR:	/* directory */
980 			inode->i_op = &yaffs_dir_inode_operations;
981 			inode->i_fop = &yaffs_dir_operations;
982 			break;
983 		case S_IFLNK:	/* symlink */
984 			inode->i_op = &yaffs_symlink_inode_operations;
985 			break;
986 		}
987 
988 		yaffs_InodeToObjectLV(inode) = obj;
989 
990 		obj->myInode = inode;
991 
992 	} else {
993 		T(YAFFS_TRACE_OS,
994 			("yaffs_FileInode invalid parameters\n"));
995 	}
996 
997 }
998 
yaffs_get_inode(struct super_block * sb,int mode,int dev,yaffs_Object * obj)999 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1000 				yaffs_Object *obj)
1001 {
1002 	struct inode *inode;
1003 
1004 	if (!sb) {
1005 		T(YAFFS_TRACE_OS,
1006 			("yaffs_get_inode for NULL super_block!!\n"));
1007 		return NULL;
1008 
1009 	}
1010 
1011 	if (!obj) {
1012 		T(YAFFS_TRACE_OS,
1013 			("yaffs_get_inode for NULL object!!\n"));
1014 		return NULL;
1015 
1016 	}
1017 
1018 	T(YAFFS_TRACE_OS,
1019 		("yaffs_get_inode for object %d\n", obj->objectId));
1020 
1021 	inode = Y_IGET(sb, obj->objectId);
1022 	if (IS_ERR(inode))
1023 		return NULL;
1024 
1025 	/* NB Side effect: iget calls back to yaffs_read_inode(). */
1026 	/* iget also increments the inode's i_count */
1027 	/* NB You can't be holding grossLock or deadlock will happen! */
1028 
1029 	return inode;
1030 }
1031 
yaffs_file_write(struct file * f,const char * buf,size_t n,loff_t * pos)1032 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1033 				loff_t *pos)
1034 {
1035 	yaffs_Object *obj;
1036 	int nWritten, ipos;
1037 	struct inode *inode;
1038 	yaffs_Device *dev;
1039 
1040 	obj = yaffs_DentryToObject(f->f_dentry);
1041 
1042 	dev = obj->myDev;
1043 
1044 	yaffs_GrossLock(dev);
1045 
1046 	inode = f->f_dentry->d_inode;
1047 
1048 	if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1049 		ipos = inode->i_size;
1050 	else
1051 		ipos = *pos;
1052 
1053 	if (!obj)
1054 		T(YAFFS_TRACE_OS,
1055 			("yaffs_file_write: hey obj is null!\n"));
1056 	else
1057 		T(YAFFS_TRACE_OS,
1058 			("yaffs_file_write about to write writing %zu bytes"
1059 			"to object %d at %d\n",
1060 			n, obj->objectId, ipos));
1061 
1062 	nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1063 
1064 	T(YAFFS_TRACE_OS,
1065 		("yaffs_file_write writing %zu bytes, %d written at %d\n",
1066 		n, nWritten, ipos));
1067 
1068 	if (nWritten > 0) {
1069 		ipos += nWritten;
1070 		*pos = ipos;
1071 		if (ipos > inode->i_size) {
1072 			inode->i_size = ipos;
1073 			inode->i_blocks = (ipos + 511) >> 9;
1074 
1075 			T(YAFFS_TRACE_OS,
1076 				("yaffs_file_write size updated to %d bytes, "
1077 				"%d blocks\n",
1078 				ipos, (int)(inode->i_blocks)));
1079 		}
1080 
1081 	}
1082 	yaffs_GrossUnlock(dev);
1083 	return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1084 }
1085 
1086 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1087 /* For now we just assume few parallel writes and check against a small number. */
1088 /* Todo: need to do this with a counter to handle parallel reads better */
1089 
yaffs_hold_space(struct file * f)1090 static ssize_t yaffs_hold_space(struct file *f)
1091 {
1092 	yaffs_Object *obj;
1093 	yaffs_Device *dev;
1094 
1095 	int nFreeChunks;
1096 
1097 
1098 	obj = yaffs_DentryToObject(f->f_dentry);
1099 
1100 	dev = obj->myDev;
1101 
1102 	yaffs_GrossLock(dev);
1103 
1104 	nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1105 
1106 	yaffs_GrossUnlock(dev);
1107 
1108 	return (nFreeChunks > 20) ? 1 : 0;
1109 }
1110 
yaffs_release_space(struct file * f)1111 static void yaffs_release_space(struct file *f)
1112 {
1113 	yaffs_Object *obj;
1114 	yaffs_Device *dev;
1115 
1116 
1117 	obj = yaffs_DentryToObject(f->f_dentry);
1118 
1119 	dev = obj->myDev;
1120 
1121 	yaffs_GrossLock(dev);
1122 
1123 
1124 	yaffs_GrossUnlock(dev);
1125 }
1126 
yaffs_readdir(struct file * f,void * dirent,filldir_t filldir)1127 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1128 {
1129 	yaffs_Object *obj;
1130 	yaffs_Device *dev;
1131 	struct inode *inode = f->f_dentry->d_inode;
1132 	unsigned long offset, curoffs;
1133 	struct ylist_head *i;
1134 	yaffs_Object *l;
1135 
1136 	char name[YAFFS_MAX_NAME_LENGTH + 1];
1137 
1138 	obj = yaffs_DentryToObject(f->f_dentry);
1139 	dev = obj->myDev;
1140 
1141 	yaffs_GrossLock(dev);
1142 
1143 	offset = f->f_pos;
1144 
1145 	T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
1146 
1147 	if (offset == 0) {
1148 		T(YAFFS_TRACE_OS,
1149 			("yaffs_readdir: entry . ino %d \n",
1150 			(int)inode->i_ino));
1151 		if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
1152 			goto out;
1153 		offset++;
1154 		f->f_pos++;
1155 	}
1156 	if (offset == 1) {
1157 		T(YAFFS_TRACE_OS,
1158 			("yaffs_readdir: entry .. ino %d \n",
1159 			(int)f->f_dentry->d_parent->d_inode->i_ino));
1160 		if (filldir(dirent, "..", 2, offset,
1161 			f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
1162 			goto out;
1163 		offset++;
1164 		f->f_pos++;
1165 	}
1166 
1167 	curoffs = 1;
1168 
1169 	/* If the directory has changed since the open or last call to
1170 	   readdir, rewind to after the 2 canned entries. */
1171 
1172 	if (f->f_version != inode->i_version) {
1173 		offset = 2;
1174 		f->f_pos = offset;
1175 		f->f_version = inode->i_version;
1176 	}
1177 
1178 	ylist_for_each(i, &obj->variant.directoryVariant.children) {
1179 		curoffs++;
1180 		if (curoffs >= offset) {
1181 			l = ylist_entry(i, yaffs_Object, siblings);
1182 
1183 			yaffs_GetObjectName(l, name,
1184 					    YAFFS_MAX_NAME_LENGTH + 1);
1185 			T(YAFFS_TRACE_OS,
1186 			  ("yaffs_readdir: %s inode %d\n", name,
1187 			   yaffs_GetObjectInode(l)));
1188 
1189 			if (filldir(dirent,
1190 					name,
1191 					strlen(name),
1192 					offset,
1193 					yaffs_GetObjectInode(l),
1194 					yaffs_GetObjectType(l)) < 0)
1195 				goto up_and_out;
1196 
1197 			offset++;
1198 			f->f_pos++;
1199 		}
1200 	}
1201 
1202 up_and_out:
1203 out:
1204 	yaffs_GrossUnlock(dev);
1205 
1206 	return 0;
1207 }
1208 
1209 /*
1210  * File creation. Allocate an inode, and we're done..
1211  */
1212 
1213 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1214 #define YCRED(x) x
1215 #else
1216 #define YCRED(x) (x->cred)
1217 #endif
1218 
1219 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
yaffs_mknod(struct inode * dir,struct dentry * dentry,int mode,dev_t rdev)1220 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1221 			dev_t rdev)
1222 #else
1223 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1224 			int rdev)
1225 #endif
1226 {
1227 	struct inode *inode;
1228 
1229 	yaffs_Object *obj = NULL;
1230 	yaffs_Device *dev;
1231 
1232 	yaffs_Object *parent = yaffs_InodeToObject(dir);
1233 
1234 	int error = -ENOSPC;
1235 	uid_t uid = YCRED(current)->fsuid;
1236 	gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1237 
1238 	if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1239 		mode |= S_ISGID;
1240 
1241 	if (parent) {
1242 		T(YAFFS_TRACE_OS,
1243 			("yaffs_mknod: parent object %d type %d\n",
1244 			parent->objectId, parent->variantType));
1245 	} else {
1246 		T(YAFFS_TRACE_OS,
1247 			("yaffs_mknod: could not get parent object\n"));
1248 		return -EPERM;
1249 	}
1250 
1251 	T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1252 			"mode %x dev %x\n",
1253 			dentry->d_name.name, mode, rdev));
1254 
1255 	dev = parent->myDev;
1256 
1257 	yaffs_GrossLock(dev);
1258 
1259 	switch (mode & S_IFMT) {
1260 	default:
1261 		/* Special (socket, fifo, device...) */
1262 		T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
1263 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1264 		obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1265 				gid, old_encode_dev(rdev));
1266 #else
1267 		obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1268 				gid, rdev);
1269 #endif
1270 		break;
1271 	case S_IFREG:		/* file          */
1272 		T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
1273 		obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1274 				gid);
1275 		break;
1276 	case S_IFDIR:		/* directory */
1277 		T(YAFFS_TRACE_OS,
1278 			("yaffs_mknod: making directory\n"));
1279 		obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1280 					uid, gid);
1281 		break;
1282 	case S_IFLNK:		/* symlink */
1283 		T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
1284 		obj = NULL;	/* Do we ever get here? */
1285 		break;
1286 	}
1287 
1288 	/* Can not call yaffs_get_inode() with gross lock held */
1289 	yaffs_GrossUnlock(dev);
1290 
1291 	if (obj) {
1292 		inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1293 		d_instantiate(dentry, inode);
1294 		update_dir_time(dir);
1295 		T(YAFFS_TRACE_OS,
1296 			("yaffs_mknod created object %d count = %d\n",
1297 			obj->objectId, atomic_read(&inode->i_count)));
1298 		error = 0;
1299 	} else {
1300 		T(YAFFS_TRACE_OS,
1301 			("yaffs_mknod failed making object\n"));
1302 		error = -ENOMEM;
1303 	}
1304 
1305 	return error;
1306 }
1307 
yaffs_mkdir(struct inode * dir,struct dentry * dentry,int mode)1308 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1309 {
1310 	int retVal;
1311 	T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
1312 	retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1313 	return retVal;
1314 }
1315 
1316 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
yaffs_create(struct inode * dir,struct dentry * dentry,int mode,struct nameidata * n)1317 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1318 			struct nameidata *n)
1319 #else
1320 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1321 #endif
1322 {
1323 	T(YAFFS_TRACE_OS, ("yaffs_create\n"));
1324 	return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1325 }
1326 
yaffs_unlink(struct inode * dir,struct dentry * dentry)1327 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1328 {
1329 	int retVal;
1330 
1331 	yaffs_Device *dev;
1332 
1333 	T(YAFFS_TRACE_OS,
1334 		("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1335 		dentry->d_name.name));
1336 
1337 	dev = yaffs_InodeToObject(dir)->myDev;
1338 
1339 	yaffs_GrossLock(dev);
1340 
1341 	retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1342 
1343 	if (retVal == YAFFS_OK) {
1344 		dentry->d_inode->i_nlink--;
1345 		dir->i_version++;
1346 		yaffs_GrossUnlock(dev);
1347 		mark_inode_dirty(dentry->d_inode);
1348 		update_dir_time(dir);
1349 		return 0;
1350 	}
1351 	yaffs_GrossUnlock(dev);
1352 	return -ENOTEMPTY;
1353 }
1354 
1355 /*
1356  * Create a link...
1357  */
yaffs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)1358 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1359 			struct dentry *dentry)
1360 {
1361 	struct inode *inode = old_dentry->d_inode;
1362 	yaffs_Object *obj = NULL;
1363 	yaffs_Object *link = NULL;
1364 	yaffs_Device *dev;
1365 
1366 	T(YAFFS_TRACE_OS, ("yaffs_link\n"));
1367 
1368 	obj = yaffs_InodeToObject(inode);
1369 	dev = obj->myDev;
1370 
1371 	yaffs_GrossLock(dev);
1372 
1373 	if (!S_ISDIR(inode->i_mode))		/* Don't link directories */
1374 		link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1375 			obj);
1376 
1377 	if (link) {
1378 		old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1379 		d_instantiate(dentry, old_dentry->d_inode);
1380 		atomic_inc(&old_dentry->d_inode->i_count);
1381 		T(YAFFS_TRACE_OS,
1382 			("yaffs_link link count %d i_count %d\n",
1383 			old_dentry->d_inode->i_nlink,
1384 			atomic_read(&old_dentry->d_inode->i_count)));
1385 	}
1386 
1387 	yaffs_GrossUnlock(dev);
1388 
1389 	if (link){
1390 		update_dir_time(dir);
1391 		return 0;
1392 	}
1393 
1394 	return -EPERM;
1395 }
1396 
yaffs_symlink(struct inode * dir,struct dentry * dentry,const char * symname)1397 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1398 				const char *symname)
1399 {
1400 	yaffs_Object *obj;
1401 	yaffs_Device *dev;
1402 	uid_t uid = YCRED(current)->fsuid;
1403 	gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1404 
1405 	T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
1406 
1407 	dev = yaffs_InodeToObject(dir)->myDev;
1408 	yaffs_GrossLock(dev);
1409 	obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1410 				S_IFLNK | S_IRWXUGO, uid, gid, symname);
1411 	yaffs_GrossUnlock(dev);
1412 
1413 	if (obj) {
1414 		struct inode *inode;
1415 
1416 		inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1417 		d_instantiate(dentry, inode);
1418 		update_dir_time(dir);
1419 		T(YAFFS_TRACE_OS, ("symlink created OK\n"));
1420 		return 0;
1421 	} else {
1422 		T(YAFFS_TRACE_OS, ("symlink not created\n"));
1423 	}
1424 
1425 	return -ENOMEM;
1426 }
1427 
yaffs_sync_object(struct file * file,struct dentry * dentry,int datasync)1428 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1429 				int datasync)
1430 {
1431 
1432 	yaffs_Object *obj;
1433 	yaffs_Device *dev;
1434 
1435 	obj = yaffs_DentryToObject(dentry);
1436 
1437 	dev = obj->myDev;
1438 
1439 	T(YAFFS_TRACE_OS, ("yaffs_sync_object\n"));
1440 	yaffs_GrossLock(dev);
1441 	yaffs_FlushFile(obj, 1);
1442 	yaffs_GrossUnlock(dev);
1443 	return 0;
1444 }
1445 
1446 /*
1447  * The VFS layer already does all the dentry stuff for rename.
1448  *
1449  * NB: POSIX says you can rename an object over an old object of the same name
1450  */
yaffs_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)1451 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1452 			struct inode *new_dir, struct dentry *new_dentry)
1453 {
1454 	yaffs_Device *dev;
1455 	int retVal = YAFFS_FAIL;
1456 	yaffs_Object *target;
1457 
1458 	T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
1459 	dev = yaffs_InodeToObject(old_dir)->myDev;
1460 
1461 	yaffs_GrossLock(dev);
1462 
1463 	/* Check if the target is an existing directory that is not empty. */
1464 	target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1465 				new_dentry->d_name.name);
1466 
1467 
1468 
1469 	if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1470 		!ylist_empty(&target->variant.directoryVariant.children)) {
1471 
1472 		T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
1473 
1474 		retVal = YAFFS_FAIL;
1475 	} else {
1476 		/* Now does unlinking internally using shadowing mechanism */
1477 		T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
1478 
1479 		retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1480 				old_dentry->d_name.name,
1481 				yaffs_InodeToObject(new_dir),
1482 				new_dentry->d_name.name);
1483 	}
1484 	yaffs_GrossUnlock(dev);
1485 
1486 	if (retVal == YAFFS_OK) {
1487 		if (target) {
1488 			new_dentry->d_inode->i_nlink--;
1489 			mark_inode_dirty(new_dentry->d_inode);
1490 		}
1491 
1492 		update_dir_time(old_dir);
1493 		if(old_dir != new_dir)
1494 			update_dir_time(new_dir);
1495 		return 0;
1496 	} else {
1497 		return -ENOTEMPTY;
1498 	}
1499 }
1500 
yaffs_setattr(struct dentry * dentry,struct iattr * attr)1501 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1502 {
1503 	struct inode *inode = dentry->d_inode;
1504 	int error;
1505 	yaffs_Device *dev;
1506 
1507 	T(YAFFS_TRACE_OS,
1508 		("yaffs_setattr of object %d\n",
1509 		yaffs_InodeToObject(inode)->objectId));
1510 
1511 	error = inode_change_ok(inode, attr);
1512 	if (error == 0) {
1513 		dev = yaffs_InodeToObject(inode)->myDev;
1514 		yaffs_GrossLock(dev);
1515 		if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1516 				YAFFS_OK) {
1517 			error = 0;
1518 		} else {
1519 			error = -EPERM;
1520 		}
1521 		yaffs_GrossUnlock(dev);
1522 		if (!error)
1523 			error = inode_setattr(inode, attr);
1524 	}
1525 	return error;
1526 }
1527 
1528 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
yaffs_statfs(struct dentry * dentry,struct kstatfs * buf)1529 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1530 {
1531 	yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1532 	struct super_block *sb = dentry->d_sb;
1533 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1534 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1535 {
1536 	yaffs_Device *dev = yaffs_SuperToDevice(sb);
1537 #else
1538 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1539 {
1540 	yaffs_Device *dev = yaffs_SuperToDevice(sb);
1541 #endif
1542 
1543 	T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
1544 
1545 	yaffs_GrossLock(dev);
1546 
1547 	buf->f_type = YAFFS_MAGIC;
1548 	buf->f_bsize = sb->s_blocksize;
1549 	buf->f_namelen = 255;
1550 
1551 	if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1552 		/* Do this if chunk size is not a power of 2 */
1553 
1554 		uint64_t bytesInDev;
1555 		uint64_t bytesFree;
1556 
1557 		bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock + 1))) *
1558 			((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
1559 
1560 		do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1561 		buf->f_blocks = bytesInDev;
1562 
1563 		bytesFree  = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1564 			((uint64_t)(dev->nDataBytesPerChunk));
1565 
1566 		do_div(bytesFree, sb->s_blocksize);
1567 
1568 		buf->f_bfree = bytesFree;
1569 
1570 	} else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1571 
1572 		buf->f_blocks =
1573 			(dev->endBlock - dev->startBlock + 1) *
1574 			dev->nChunksPerBlock /
1575 			(sb->s_blocksize / dev->nDataBytesPerChunk);
1576 		buf->f_bfree =
1577 			yaffs_GetNumberOfFreeChunks(dev) /
1578 			(sb->s_blocksize / dev->nDataBytesPerChunk);
1579 	} else {
1580 		buf->f_blocks =
1581 			(dev->endBlock - dev->startBlock + 1) *
1582 			dev->nChunksPerBlock *
1583 			(dev->nDataBytesPerChunk / sb->s_blocksize);
1584 
1585 		buf->f_bfree =
1586 			yaffs_GetNumberOfFreeChunks(dev) *
1587 			(dev->nDataBytesPerChunk / sb->s_blocksize);
1588 	}
1589 
1590 	buf->f_files = 0;
1591 	buf->f_ffree = 0;
1592 	buf->f_bavail = buf->f_bfree;
1593 
1594 	yaffs_GrossUnlock(dev);
1595 	return 0;
1596 }
1597 
1598 
1599 static int yaffs_do_sync_fs(struct super_block *sb)
1600 {
1601 
1602 	yaffs_Device *dev = yaffs_SuperToDevice(sb);
1603 	T(YAFFS_TRACE_OS, ("yaffs_do_sync_fs\n"));
1604 
1605 	if (sb->s_dirt) {
1606 		yaffs_GrossLock(dev);
1607 
1608 		if (dev) {
1609 			yaffs_FlushEntireDeviceCache(dev);
1610 			yaffs_CheckpointSave(dev);
1611 		}
1612 
1613 		yaffs_GrossUnlock(dev);
1614 
1615 		sb->s_dirt = 0;
1616 	}
1617 	return 0;
1618 }
1619 
1620 
1621 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1622 static void yaffs_write_super(struct super_block *sb)
1623 #else
1624 static int yaffs_write_super(struct super_block *sb)
1625 #endif
1626 {
1627 
1628 	T(YAFFS_TRACE_OS, ("yaffs_write_super\n"));
1629 	if (yaffs_auto_checkpoint >= 2)
1630 		yaffs_do_sync_fs(sb);
1631 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
1632 	return 0;
1633 #endif
1634 }
1635 
1636 
1637 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1638 static int yaffs_sync_fs(struct super_block *sb, int wait)
1639 #else
1640 static int yaffs_sync_fs(struct super_block *sb)
1641 #endif
1642 {
1643 	T(YAFFS_TRACE_OS, ("yaffs_sync_fs\n"));
1644 
1645 	if (yaffs_auto_checkpoint >= 1)
1646 		yaffs_do_sync_fs(sb);
1647 
1648 	return 0;
1649 }
1650 
1651 #ifdef YAFFS_USE_OWN_IGET
1652 
1653 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1654 {
1655 	struct inode *inode;
1656 	yaffs_Object *obj;
1657 	yaffs_Device *dev = yaffs_SuperToDevice(sb);
1658 
1659 	T(YAFFS_TRACE_OS,
1660 		("yaffs_iget for %lu\n", ino));
1661 
1662 	inode = iget_locked(sb, ino);
1663 	if (!inode)
1664 		return ERR_PTR(-ENOMEM);
1665 	if (!(inode->i_state & I_NEW))
1666 		return inode;
1667 
1668 	/* NB This is called as a side effect of other functions, but
1669 	 * we had to release the lock to prevent deadlocks, so
1670 	 * need to lock again.
1671 	 */
1672 
1673 	yaffs_GrossLock(dev);
1674 
1675 	obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1676 
1677 	yaffs_FillInodeFromObject(inode, obj);
1678 
1679 	yaffs_GrossUnlock(dev);
1680 
1681 	unlock_new_inode(inode);
1682 	return inode;
1683 }
1684 
1685 #else
1686 
1687 static void yaffs_read_inode(struct inode *inode)
1688 {
1689 	/* NB This is called as a side effect of other functions, but
1690 	 * we had to release the lock to prevent deadlocks, so
1691 	 * need to lock again.
1692 	 */
1693 
1694 	yaffs_Object *obj;
1695 	yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1696 
1697 	T(YAFFS_TRACE_OS,
1698 		("yaffs_read_inode for %d\n", (int)inode->i_ino));
1699 
1700 	yaffs_GrossLock(dev);
1701 
1702 	obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1703 
1704 	yaffs_FillInodeFromObject(inode, obj);
1705 
1706 	yaffs_GrossUnlock(dev);
1707 }
1708 
1709 #endif
1710 
1711 static YLIST_HEAD(yaffs_dev_list);
1712 
1713 #if 0 /* not used */
1714 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1715 {
1716 	yaffs_Device    *dev = yaffs_SuperToDevice(sb);
1717 
1718 	if (*flags & MS_RDONLY) {
1719 		struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1720 
1721 		T(YAFFS_TRACE_OS,
1722 			("yaffs_remount_fs: %s: RO\n", dev->name));
1723 
1724 		yaffs_GrossLock(dev);
1725 
1726 		yaffs_FlushEntireDeviceCache(dev);
1727 
1728 		yaffs_CheckpointSave(dev);
1729 
1730 		if (mtd->sync)
1731 			mtd->sync(mtd);
1732 
1733 		yaffs_GrossUnlock(dev);
1734 	} else {
1735 		T(YAFFS_TRACE_OS,
1736 			("yaffs_remount_fs: %s: RW\n", dev->name));
1737 	}
1738 
1739 	return 0;
1740 }
1741 #endif
1742 
1743 static void yaffs_put_super(struct super_block *sb)
1744 {
1745 	yaffs_Device *dev = yaffs_SuperToDevice(sb);
1746 
1747 	T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
1748 
1749 	yaffs_GrossLock(dev);
1750 
1751 	yaffs_FlushEntireDeviceCache(dev);
1752 
1753 	yaffs_CheckpointSave(dev);
1754 
1755 	if (dev->putSuperFunc)
1756 		dev->putSuperFunc(sb);
1757 
1758 	yaffs_Deinitialise(dev);
1759 
1760 	yaffs_GrossUnlock(dev);
1761 
1762 	/* we assume this is protected by lock_kernel() in mount/umount */
1763 	ylist_del(&dev->devList);
1764 
1765 	if (dev->spareBuffer) {
1766 		YFREE(dev->spareBuffer);
1767 		dev->spareBuffer = NULL;
1768 	}
1769 
1770 	kfree(dev);
1771 }
1772 
1773 
1774 static void yaffs_MTDPutSuper(struct super_block *sb)
1775 {
1776 	struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1777 
1778 	if (mtd->sync)
1779 		mtd->sync(mtd);
1780 
1781 	put_mtd_device(mtd);
1782 }
1783 
1784 
1785 static void yaffs_MarkSuperBlockDirty(void *vsb)
1786 {
1787 	struct super_block *sb = (struct super_block *)vsb;
1788 
1789 	T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
1790 	if (sb)
1791 		sb->s_dirt = 1;
1792 }
1793 
1794 typedef struct {
1795 	int inband_tags;
1796 	int skip_checkpoint_read;
1797 	int skip_checkpoint_write;
1798 	int no_cache;
1799 } yaffs_options;
1800 
1801 #define MAX_OPT_LEN 20
1802 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
1803 {
1804 	char cur_opt[MAX_OPT_LEN + 1];
1805 	int p;
1806 	int error = 0;
1807 
1808 	/* Parse through the options which is a comma seperated list */
1809 
1810 	while (options_str && *options_str && !error) {
1811 		memset(cur_opt, 0, MAX_OPT_LEN + 1);
1812 		p = 0;
1813 
1814 		while (*options_str && *options_str != ',') {
1815 			if (p < MAX_OPT_LEN) {
1816 				cur_opt[p] = *options_str;
1817 				p++;
1818 			}
1819 			options_str++;
1820 		}
1821 
1822 		if (!strcmp(cur_opt, "inband-tags"))
1823 			options->inband_tags = 1;
1824 		else if (!strcmp(cur_opt, "no-cache"))
1825 			options->no_cache = 1;
1826 		else if (!strcmp(cur_opt, "no-checkpoint-read"))
1827 			options->skip_checkpoint_read = 1;
1828 		else if (!strcmp(cur_opt, "no-checkpoint-write"))
1829 			options->skip_checkpoint_write = 1;
1830 		else if (!strcmp(cur_opt, "no-checkpoint")) {
1831 			options->skip_checkpoint_read = 1;
1832 			options->skip_checkpoint_write = 1;
1833 		} else {
1834 			printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
1835 					cur_opt);
1836 			error = 1;
1837 		}
1838 	}
1839 
1840 	return error;
1841 }
1842 
1843 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1844 						struct super_block *sb,
1845 						void *data, int silent)
1846 {
1847 	int nBlocks;
1848 	struct inode *inode = NULL;
1849 	struct dentry *root;
1850 	yaffs_Device *dev = 0;
1851 	char devname_buf[BDEVNAME_SIZE + 1];
1852 	struct mtd_info *mtd;
1853 	int err;
1854 	char *data_str = (char *)data;
1855 
1856 	yaffs_options options;
1857 
1858 	sb->s_magic = YAFFS_MAGIC;
1859 	sb->s_op = &yaffs_super_ops;
1860 	sb->s_flags |= MS_NOATIME;
1861 
1862 	if (!sb)
1863 		printk(KERN_INFO "yaffs: sb is NULL\n");
1864 	else if (!sb->s_dev)
1865 		printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1866 	else if (!yaffs_devname(sb, devname_buf))
1867 		printk(KERN_INFO "yaffs: devname is NULL\n");
1868 	else
1869 		printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1870 		       sb->s_dev,
1871 		       yaffs_devname(sb, devname_buf));
1872 
1873 	if (!data_str)
1874 		data_str = "";
1875 
1876 	printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
1877 
1878 	memset(&options, 0, sizeof(options));
1879 
1880 	if (yaffs_parse_options(&options, data_str)) {
1881 		/* Option parsing failed */
1882 		return NULL;
1883 	}
1884 
1885 
1886 	sb->s_blocksize = PAGE_CACHE_SIZE;
1887 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1888 	T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1889 	T(YAFFS_TRACE_OS,
1890 	  ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1891 
1892 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1893 	T(YAFFS_TRACE_OS,
1894 	  ("yaffs: Write verification disabled. All guarantees "
1895 	   "null and void\n"));
1896 #endif
1897 
1898 	T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1899 			       "\"%s\"\n",
1900 			       MAJOR(sb->s_dev), MINOR(sb->s_dev),
1901 			       yaffs_devname(sb, devname_buf)));
1902 
1903 	/* Check it's an mtd device..... */
1904 	if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
1905 		return NULL;	/* This isn't an mtd device */
1906 
1907 	/* Get the device */
1908 	mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1909 	if (!mtd) {
1910 		T(YAFFS_TRACE_ALWAYS,
1911 		  ("yaffs: MTD device #%u doesn't appear to exist\n",
1912 		   MINOR(sb->s_dev)));
1913 		return NULL;
1914 	}
1915 	/* Check it's NAND */
1916 	if (mtd->type != MTD_NANDFLASH) {
1917 		T(YAFFS_TRACE_ALWAYS,
1918 		  ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1919 		return NULL;
1920 	}
1921 
1922 	T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1923 	T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1924 	T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1925 	T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1926 	T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1927 	T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1928 	T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1929 	T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
1930 	T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1931 	T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1932 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1933 	T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
1934 #else
1935 	T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
1936 #endif
1937 
1938 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1939 
1940 	if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
1941 		T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
1942 		yaffsVersion = 2;
1943 	}
1944 
1945 	/* Added NCB 26/5/2006 for completeness */
1946 	if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
1947 		T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
1948 		yaffsVersion = 1;
1949 	}
1950 
1951 #endif
1952 
1953 	if (yaffsVersion == 2) {
1954 		/* Check for version 2 style functions */
1955 		if (!mtd->erase ||
1956 		    !mtd->block_isbad ||
1957 		    !mtd->block_markbad ||
1958 		    !mtd->read ||
1959 		    !mtd->write ||
1960 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1961 		    !mtd->read_oob || !mtd->write_oob) {
1962 #else
1963 		    !mtd->write_ecc ||
1964 		    !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1965 #endif
1966 			T(YAFFS_TRACE_ALWAYS,
1967 			  ("yaffs: MTD device does not support required "
1968 			   "functions\n"));;
1969 			return NULL;
1970 		}
1971 
1972 		if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1973 		    mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
1974 		    !options.inband_tags) {
1975 			T(YAFFS_TRACE_ALWAYS,
1976 			  ("yaffs: MTD device does not have the "
1977 			   "right page sizes\n"));
1978 			return NULL;
1979 		}
1980 	} else {
1981 		/* Check for V1 style functions */
1982 		if (!mtd->erase ||
1983 		    !mtd->read ||
1984 		    !mtd->write ||
1985 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1986 		    !mtd->read_oob || !mtd->write_oob) {
1987 #else
1988 		    !mtd->write_ecc ||
1989 		    !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1990 #endif
1991 			T(YAFFS_TRACE_ALWAYS,
1992 			  ("yaffs: MTD device does not support required "
1993 			   "functions\n"));;
1994 			return NULL;
1995 		}
1996 
1997 		if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
1998 		    mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1999 			T(YAFFS_TRACE_ALWAYS,
2000 			  ("yaffs: MTD device does not support have the "
2001 			   "right page sizes\n"));
2002 			return NULL;
2003 		}
2004 	}
2005 
2006 	/* OK, so if we got here, we have an MTD that's NAND and looks
2007 	 * like it has the right capabilities
2008 	 * Set the yaffs_Device up for mtd
2009 	 */
2010 
2011 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2012 	sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2013 #else
2014 	sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2015 #endif
2016 	if (!dev) {
2017 		/* Deep shit could not allocate device structure */
2018 		T(YAFFS_TRACE_ALWAYS,
2019 		  ("yaffs_read_super: Failed trying to allocate "
2020 		   "yaffs_Device. \n"));
2021 		return NULL;
2022 	}
2023 
2024 	memset(dev, 0, sizeof(yaffs_Device));
2025 	dev->genericDevice = mtd;
2026 	dev->name = mtd->name;
2027 
2028 	/* Set up the memory size parameters.... */
2029 
2030 	nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2031 
2032 	dev->startBlock = 0;
2033 	dev->endBlock = nBlocks - 1;
2034 	dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2035 	dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2036 	dev->nReservedBlocks = 5;
2037 	dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
2038 	dev->inbandTags = options.inband_tags;
2039 
2040 	/* ... and the functions. */
2041 	if (yaffsVersion == 2) {
2042 		dev->writeChunkWithTagsToNAND =
2043 		    nandmtd2_WriteChunkWithTagsToNAND;
2044 		dev->readChunkWithTagsFromNAND =
2045 		    nandmtd2_ReadChunkWithTagsFromNAND;
2046 		dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2047 		dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
2048 		dev->spareBuffer = YMALLOC(mtd->oobsize);
2049 		dev->isYaffs2 = 1;
2050 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2051 		dev->totalBytesPerChunk = mtd->writesize;
2052 		dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2053 #else
2054 		dev->totalBytesPerChunk = mtd->oobblock;
2055 		dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2056 #endif
2057 		nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2058 
2059 		dev->startBlock = 0;
2060 		dev->endBlock = nBlocks - 1;
2061 	} else {
2062 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2063 		/* use the MTD interface in yaffs_mtdif1.c */
2064 		dev->writeChunkWithTagsToNAND =
2065 			nandmtd1_WriteChunkWithTagsToNAND;
2066 		dev->readChunkWithTagsFromNAND =
2067 			nandmtd1_ReadChunkWithTagsFromNAND;
2068 		dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2069 		dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
2070 #else
2071 		dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2072 		dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2073 #endif
2074 		dev->isYaffs2 = 0;
2075 	}
2076 	/* ... and common functions */
2077 	dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2078 	dev->initialiseNAND = nandmtd_InitialiseNAND;
2079 
2080 	dev->putSuperFunc = yaffs_MTDPutSuper;
2081 
2082 	dev->superBlock = (void *)sb;
2083 	dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2084 
2085 
2086 #ifndef CONFIG_YAFFS_DOES_ECC
2087 	dev->useNANDECC = 1;
2088 #endif
2089 
2090 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2091 	dev->wideTnodesDisabled = 1;
2092 #endif
2093 
2094 	dev->skipCheckpointRead = options.skip_checkpoint_read;
2095 	dev->skipCheckpointWrite = options.skip_checkpoint_write;
2096 
2097 	/* we assume this is protected by lock_kernel() in mount/umount */
2098 	ylist_add_tail(&dev->devList, &yaffs_dev_list);
2099 
2100 	init_MUTEX(&dev->grossLock);
2101 
2102 	yaffs_GrossLock(dev);
2103 
2104 	err = yaffs_GutsInitialise(dev);
2105 
2106 	T(YAFFS_TRACE_OS,
2107 	  ("yaffs_read_super: guts initialised %s\n",
2108 	   (err == YAFFS_OK) ? "OK" : "FAILED"));
2109 
2110 	/* Release lock before yaffs_get_inode() */
2111 	yaffs_GrossUnlock(dev);
2112 
2113 	/* Create root inode */
2114 	if (err == YAFFS_OK)
2115 		inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2116 					yaffs_Root(dev));
2117 
2118 	if (!inode)
2119 		return NULL;
2120 
2121 	inode->i_op = &yaffs_dir_inode_operations;
2122 	inode->i_fop = &yaffs_dir_operations;
2123 
2124 	T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
2125 
2126 	root = d_alloc_root(inode);
2127 
2128 	T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
2129 
2130 	if (!root) {
2131 		iput(inode);
2132 		return NULL;
2133 	}
2134 	sb->s_root = root;
2135 	sb->s_dirt = !dev->isCheckpointed;
2136 	T(YAFFS_TRACE_ALWAYS,
2137 	  ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
2138 
2139 	T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
2140 	return sb;
2141 }
2142 
2143 
2144 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2145 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2146 					 int silent)
2147 {
2148 	return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2149 }
2150 
2151 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2152 static int yaffs_read_super(struct file_system_type *fs,
2153 			    int flags, const char *dev_name,
2154 			    void *data, struct vfsmount *mnt)
2155 {
2156 
2157 	return get_sb_bdev(fs, flags, dev_name, data,
2158 			   yaffs_internal_read_super_mtd, mnt);
2159 }
2160 #else
2161 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2162 					    int flags, const char *dev_name,
2163 					    void *data)
2164 {
2165 
2166 	return get_sb_bdev(fs, flags, dev_name, data,
2167 			   yaffs_internal_read_super_mtd);
2168 }
2169 #endif
2170 
2171 static struct file_system_type yaffs_fs_type = {
2172 	.owner = THIS_MODULE,
2173 	.name = "yaffs",
2174 	.get_sb = yaffs_read_super,
2175 	.kill_sb = kill_block_super,
2176 	.fs_flags = FS_REQUIRES_DEV,
2177 };
2178 #else
2179 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2180 					    int silent)
2181 {
2182 	return yaffs_internal_read_super(1, sb, data, silent);
2183 }
2184 
2185 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2186 		      FS_REQUIRES_DEV);
2187 #endif
2188 
2189 
2190 #ifdef CONFIG_YAFFS_YAFFS2
2191 
2192 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2193 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2194 					  int silent)
2195 {
2196 	return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2197 }
2198 
2199 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2200 static int yaffs2_read_super(struct file_system_type *fs,
2201 			int flags, const char *dev_name, void *data,
2202 			struct vfsmount *mnt)
2203 {
2204 	return get_sb_bdev(fs, flags, dev_name, data,
2205 			yaffs2_internal_read_super_mtd, mnt);
2206 }
2207 #else
2208 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2209 					     int flags, const char *dev_name,
2210 					     void *data)
2211 {
2212 
2213 	return get_sb_bdev(fs, flags, dev_name, data,
2214 			   yaffs2_internal_read_super_mtd);
2215 }
2216 #endif
2217 
2218 static struct file_system_type yaffs2_fs_type = {
2219 	.owner = THIS_MODULE,
2220 	.name = "yaffs2",
2221 	.get_sb = yaffs2_read_super,
2222 	.kill_sb = kill_block_super,
2223 	.fs_flags = FS_REQUIRES_DEV,
2224 };
2225 #else
2226 static struct super_block *yaffs2_read_super(struct super_block *sb,
2227 					     void *data, int silent)
2228 {
2229 	return yaffs_internal_read_super(2, sb, data, silent);
2230 }
2231 
2232 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2233 		      FS_REQUIRES_DEV);
2234 #endif
2235 
2236 #endif				/* CONFIG_YAFFS_YAFFS2 */
2237 
2238 static struct proc_dir_entry *my_proc_entry;
2239 
2240 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
2241 {
2242 	buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
2243 	buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
2244 	buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
2245 	buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2246 	buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2247 	buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2248 	buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2249 	buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
2250 	buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2251 	buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2252 	buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2253 	buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2254 	buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2255 	buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2256 	buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2257 	buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2258 	buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2259 	buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2260 	buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2261 	buf += sprintf(buf, "passiveGCs......... %d\n",
2262 		    dev->passiveGarbageCollections);
2263 	buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2264 	buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
2265 	buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2266 	buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2267 	buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2268 	buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2269 	buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2270 	buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2271 	buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2272 	buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2273 	buf +=
2274 	    sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2275 	buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
2276 	buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
2277 	buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
2278 
2279 	return buf;
2280 }
2281 
2282 static int yaffs_proc_read(char *page,
2283 			   char **start,
2284 			   off_t offset, int count, int *eof, void *data)
2285 {
2286 	struct ylist_head *item;
2287 	char *buf = page;
2288 	int step = offset;
2289 	int n = 0;
2290 
2291 	/* Get proc_file_read() to step 'offset' by one on each sucessive call.
2292 	 * We use 'offset' (*ppos) to indicate where we are in devList.
2293 	 * This also assumes the user has posted a read buffer large
2294 	 * enough to hold the complete output; but that's life in /proc.
2295 	 */
2296 
2297 	*(int *)start = 1;
2298 
2299 	/* Print header first */
2300 	if (step == 0) {
2301 		buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2302 			       "\n%s\n%s\n", yaffs_fs_c_version,
2303 			       yaffs_guts_c_version);
2304 	}
2305 
2306 	/* hold lock_kernel while traversing yaffs_dev_list */
2307 	lock_kernel();
2308 
2309 	/* Locate and print the Nth entry.  Order N-squared but N is small. */
2310 	ylist_for_each(item, &yaffs_dev_list) {
2311 		yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
2312 		if (n < step) {
2313 			n++;
2314 			continue;
2315 		}
2316 		buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
2317 		buf = yaffs_dump_dev(buf, dev);
2318 		break;
2319 	}
2320 	unlock_kernel();
2321 
2322 	return buf - page < count ? buf - page : count;
2323 }
2324 
2325 /**
2326  * Set the verbosity of the warnings and error messages.
2327  *
2328  * Note that the names can only be a..z or _ with the current code.
2329  */
2330 
2331 static struct {
2332 	char *mask_name;
2333 	unsigned mask_bitfield;
2334 } mask_flags[] = {
2335 	{"allocate", YAFFS_TRACE_ALLOCATE},
2336 	{"always", YAFFS_TRACE_ALWAYS},
2337 	{"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2338 	{"buffers", YAFFS_TRACE_BUFFERS},
2339 	{"bug", YAFFS_TRACE_BUG},
2340 	{"checkpt", YAFFS_TRACE_CHECKPOINT},
2341 	{"deletion", YAFFS_TRACE_DELETION},
2342 	{"erase", YAFFS_TRACE_ERASE},
2343 	{"error", YAFFS_TRACE_ERROR},
2344 	{"gc_detail", YAFFS_TRACE_GC_DETAIL},
2345 	{"gc", YAFFS_TRACE_GC},
2346 	{"mtd", YAFFS_TRACE_MTD},
2347 	{"nandaccess", YAFFS_TRACE_NANDACCESS},
2348 	{"os", YAFFS_TRACE_OS},
2349 	{"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2350 	{"scan", YAFFS_TRACE_SCAN},
2351 	{"tracing", YAFFS_TRACE_TRACING},
2352 
2353 	{"verify", YAFFS_TRACE_VERIFY},
2354 	{"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2355 	{"verify_full", YAFFS_TRACE_VERIFY_FULL},
2356 	{"verify_all", YAFFS_TRACE_VERIFY_ALL},
2357 
2358 	{"write", YAFFS_TRACE_WRITE},
2359 	{"all", 0xffffffff},
2360 	{"none", 0},
2361 	{NULL, 0},
2362 };
2363 
2364 #define MAX_MASK_NAME_LENGTH 40
2365 static int yaffs_proc_write(struct file *file, const char *buf,
2366 					 unsigned long count, void *data)
2367 {
2368 	unsigned rg = 0, mask_bitfield;
2369 	char *end;
2370 	char *mask_name;
2371 	const char *x;
2372 	char substring[MAX_MASK_NAME_LENGTH + 1];
2373 	int i;
2374 	int done = 0;
2375 	int add, len = 0;
2376 	int pos = 0;
2377 
2378 	rg = yaffs_traceMask;
2379 
2380 	while (!done && (pos < count)) {
2381 		done = 1;
2382 		while ((pos < count) && isspace(buf[pos]))
2383 			pos++;
2384 
2385 		switch (buf[pos]) {
2386 		case '+':
2387 		case '-':
2388 		case '=':
2389 			add = buf[pos];
2390 			pos++;
2391 			break;
2392 
2393 		default:
2394 			add = ' ';
2395 			break;
2396 		}
2397 		mask_name = NULL;
2398 
2399 		mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2400 
2401 		if (end > buf + pos) {
2402 			mask_name = "numeral";
2403 			len = end - (buf + pos);
2404 			pos += len;
2405 			done = 0;
2406 		} else {
2407 			for (x = buf + pos, i = 0;
2408 			    (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2409 			    i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2410 				substring[i] = *x;
2411 			substring[i] = '\0';
2412 
2413 			for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2414 				if (strcmp(substring, mask_flags[i].mask_name) == 0) {
2415 					mask_name = mask_flags[i].mask_name;
2416 					mask_bitfield = mask_flags[i].mask_bitfield;
2417 					done = 0;
2418 					break;
2419 				}
2420 			}
2421 		}
2422 
2423 		if (mask_name != NULL) {
2424 			done = 0;
2425 			switch (add) {
2426 			case '-':
2427 				rg &= ~mask_bitfield;
2428 				break;
2429 			case '+':
2430 				rg |= mask_bitfield;
2431 				break;
2432 			case '=':
2433 				rg = mask_bitfield;
2434 				break;
2435 			default:
2436 				rg |= mask_bitfield;
2437 				break;
2438 			}
2439 		}
2440 	}
2441 
2442 	yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2443 
2444 	printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
2445 
2446 	if (rg & YAFFS_TRACE_ALWAYS) {
2447 		for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2448 			char flag;
2449 			flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2450 			printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
2451 		}
2452 	}
2453 
2454 	return count;
2455 }
2456 
2457 /* Stuff to handle installation of file systems */
2458 struct file_system_to_install {
2459 	struct file_system_type *fst;
2460 	int installed;
2461 };
2462 
2463 static struct file_system_to_install fs_to_install[] = {
2464 	{&yaffs_fs_type, 0},
2465 	{&yaffs2_fs_type, 0},
2466 	{NULL, 0}
2467 };
2468 
2469 static int __init init_yaffs_fs(void)
2470 {
2471 	int error = 0;
2472 	struct file_system_to_install *fsinst;
2473 
2474 	T(YAFFS_TRACE_ALWAYS,
2475 	  ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2476 
2477 	/* Install the proc_fs entry */
2478 	my_proc_entry = create_proc_entry("yaffs",
2479 					       S_IRUGO | S_IFREG,
2480 					       YPROC_ROOT);
2481 
2482 	if (my_proc_entry) {
2483 		my_proc_entry->write_proc = yaffs_proc_write;
2484 		my_proc_entry->read_proc = yaffs_proc_read;
2485 		my_proc_entry->data = NULL;
2486 	} else
2487 		return -ENOMEM;
2488 
2489 	/* Now add the file system entries */
2490 
2491 	fsinst = fs_to_install;
2492 
2493 	while (fsinst->fst && !error) {
2494 		error = register_filesystem(fsinst->fst);
2495 		if (!error)
2496 			fsinst->installed = 1;
2497 		fsinst++;
2498 	}
2499 
2500 	/* Any errors? uninstall  */
2501 	if (error) {
2502 		fsinst = fs_to_install;
2503 
2504 		while (fsinst->fst) {
2505 			if (fsinst->installed) {
2506 				unregister_filesystem(fsinst->fst);
2507 				fsinst->installed = 0;
2508 			}
2509 			fsinst++;
2510 		}
2511 	}
2512 
2513 	return error;
2514 }
2515 
2516 static void __exit exit_yaffs_fs(void)
2517 {
2518 
2519 	struct file_system_to_install *fsinst;
2520 
2521 	T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2522 			       " removing. \n"));
2523 
2524 	remove_proc_entry("yaffs", YPROC_ROOT);
2525 
2526 	fsinst = fs_to_install;
2527 
2528 	while (fsinst->fst) {
2529 		if (fsinst->installed) {
2530 			unregister_filesystem(fsinst->fst);
2531 			fsinst->installed = 0;
2532 		}
2533 		fsinst++;
2534 	}
2535 }
2536 
2537 module_init(init_yaffs_fs)
2538 module_exit(exit_yaffs_fs)
2539 
2540 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2541 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2542 MODULE_LICENSE("GPL");
2543