blob: d29ecb530c638427fe53758d16d27e20bb671594 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2018 Aleph One Ltd.
5 *
6 * Created by Charles Manning <charles@aleph1.co.uk>
7 * Acknowledgements:
8 * Luc van OostenRyck for numerous patches.
9 * Nick Bane for numerous patches.
10 * Nick Bane for 2.5/2.6 integration.
11 * Andras Toth for mknod rdev issue.
12 * Michael Fischer for finding the problem with inode inconsistency.
13 * Some code bodily lifted from JFFS
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
18 */
19
20/*
21 *
22 * This is the file system front-end to YAFFS that hooks it up to
23 * the VFS.
24 *
25 * Special notes:
26 * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
27 * this superblock
28 * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
29 * superblock
30 * >> inode->u.generic_ip points to the associated struct yaffs_obj.
31 */
32
33/*
34 * There are two variants of the VFS glue code. This variant should compile
35 * for any version of Linux.
36 */
37#include <linux/version.h>
38#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0))
39#include <linux/iversion.h>
40#endif
41
42#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
43#define YAFFS_COMPILE_BACKGROUND
44#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23))
45#define YAFFS_COMPILE_FREEZER
46#endif
47#endif
48
49#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
50#define YAFFS_COMPILE_EXPORTFS
51#endif
52
53#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
54#define YAFFS_USE_SETATTR_COPY
55#define YAFFS_USE_TRUNCATE_SETSIZE
56#endif
57#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
58#define YAFFS_HAS_EVICT_INODE
59#endif
60
61#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13)) && \
62 (LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0))
63#define YAFFS_NEW_FOLLOW_LINK 1
64#else
65#define YAFFS_NEW_FOLLOW_LINK 0
66#endif
67
68#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
69#define YAFFS_HAS_WRITE_SUPER
70#endif
71
72
73#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
74#include <linux/config.h>
75#endif
76
77#include <linux/kernel.h>
78#include <linux/module.h>
79#include <linux/slab.h>
80#include <linux/init.h>
81#include <linux/fs.h>
82#include <linux/proc_fs.h>
83#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
84#include <linux/smp_lock.h>
85#endif
86#include <linux/pagemap.h>
87#include <linux/mtd/mtd.h>
88#include <linux/interrupt.h>
89#include <linux/string.h>
90#include <linux/ctype.h>
91
92#if (YAFFS_NEW_FOLLOW_LINK == 1)
93#include <linux/namei.h>
94#endif
95
96#ifdef YAFFS_COMPILE_EXPORTFS
97#include <linux/exportfs.h>
98#endif
99
100#ifdef YAFFS_COMPILE_BACKGROUND
101#include <linux/kthread.h>
102#include <linux/delay.h>
103#endif
104#ifdef YAFFS_COMPILE_FREEZER
105#include <linux/freezer.h>
106#endif
107
108#include <asm/div64.h>
109
110#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
111
112#include <linux/statfs.h>
113
114#define UnlockPage(p) unlock_page(p)
115#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
116
117/* FIXME: use sb->s_id instead ? */
118#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
119
120#else
121
122#include <linux/locks.h>
123#define BDEVNAME_SIZE 0
124#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
125
126#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
127/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
128#define __user
129#endif
130
131#endif
132
133#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
134#define YPROC_ROOT (&proc_root)
135#else
136#define YPROC_ROOT NULL
137#endif
138
139#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
140#define Y_INIT_TIMER(a) init_timer(a)
141#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
142#define Y_INIT_TIMER(a) init_timer_on_stack(a)
143#else
144#define Y_INIT_TIMER(a,cb) timer_setup_on_stack(a,cb,0)
145#endif
146
147#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
148#define YAFFS_USE_WRITE_BEGIN_END 1
149#else
150#define YAFFS_USE_WRITE_BEGIN_END 0
151#endif
152
153#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
154#define YAFFS_SUPER_HAS_DIRTY
155#endif
156
157
158#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
159#define set_nlink(inode, count) do { (inode)->i_nlink = (count); } while(0)
160#endif
161
162#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
163static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
164{
165 uint64_t result = partition_size;
166 do_div(result, block_size);
167 return (uint32_t) result;
168}
169#else
170#define YCALCBLOCKS(s, b) ((s)/(b))
171#endif
172
173#include <linux/uaccess.h>
174#include <linux/mtd/mtd.h>
175
176#include "yportenv.h"
177#include "yaffs_trace.h"
178#include "yaffs_guts.h"
179#include "yaffs_attribs.h"
180#include "yaffs_linux.h"
181#include "yaffs_mtdif.h"
182#include "yaffs_packedtags2.h"
183#include "yaffs_getblockinfo.h"
184
185unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS | 0;
186unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
187unsigned int yaffs_auto_checkpoint = 1;
188unsigned int yaffs_gc_control = 1;
189unsigned int yaffs_bg_enable = 1;
190unsigned int yaffs_auto_select = 1;
191/* Module Parameters */
192#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
193module_param(yaffs_trace_mask, uint, 0644);
194module_param(yaffs_wr_attempts, uint, 0644);
195module_param(yaffs_auto_checkpoint, uint, 0644);
196module_param(yaffs_gc_control, uint, 0644);
197module_param(yaffs_bg_enable, uint, 0644);
198#else
199MODULE_PARM(yaffs_trace_mask, "i");
200MODULE_PARM(yaffs_wr_attempts, "i");
201MODULE_PARM(yaffs_auto_checkpoint, "i");
202MODULE_PARM(yaffs_gc_control, "i");
203#endif
204
205#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
206/* use iget and read_inode */
207#define Y_IGET(sb, inum) iget((sb), (inum))
208
209#else
210/* Call local equivalent */
211#define YAFFS_USE_OWN_IGET
212#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
213
214#endif
215
216#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
217#define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
218#else
219#define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
220#endif
221
222#define yaffs_inode_to_obj(iptr) \
223 ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
224#define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
225
226#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
227#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
228#else
229#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
230#endif
231
232#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
233#define Y_CLEAR_INODE(i) clear_inode(i)
234#else
235#define Y_CLEAR_INODE(i) end_writeback(i)
236#endif
237
238#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
239#define YAFFS_USE_DIR_ITERATE
240#endif
241
242#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0))
243#define YAFFS_USE_XATTR
244#endif
245
246#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,12,0))
247#define YAFFS_NEW_PROCFS
248#include <linux/seq_file.h>
249#endif
250
251#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
252#define PAGE_CACHE_SIZE PAGE_SIZE
253#define PAGE_CACHE_SHIFT PAGE_SHIFT
254#define Y_GET_DENTRY(f) ((f)->f_path.dentry)
255#define YAFFS_NEW_XATTR 1
256#define YAFFS_NEW_GET_LINK 1
257#else
258#define Y_GET_DENTRY(f) ((f)->f_dentry)
259#define YAFFS_NEW_XATTR 0
260#define YAFFS_NEW_GET_LINK 0
261#endif
262
263#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0))
264#define page_cache_release put_page
265#endif
266
267#if (LINUX_VERSION_CODE < KERNEL_VERSION(4,12,0))
268#define update_dir_time(dir) do {\
269 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
270 } while (0)
271#elif (LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0))
272#define update_dir_time(dir) do {\
273 (dir)->i_ctime = (dir)->i_mtime = current_kernel_time(); \
274 } while (0)
275#else
276#define update_dir_time(dir) do {\
277 (dir)->i_ctime = (dir)->i_mtime = current_kernel_time64(); \
278 } while (0)
279#endif
280
281#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0))
282static inline int setattr_prepare(struct dentry *dentry, struct iattr *attr)
283{
284 return inode_change_ok(dentry->d_inode, attr);
285}
286#endif
287
288static void yaffs_fill_inode_from_obj(struct inode *inode,
289 struct yaffs_obj *obj);
290
291
292static void yaffs_gross_lock(struct yaffs_dev *dev)
293{
294 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
295 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
296 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
297}
298
299static void yaffs_gross_unlock(struct yaffs_dev *dev)
300{
301 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
302 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
303}
304
305
306static int yaffs_readpage_nolock(struct file *f, struct page *pg)
307{
308 /* Lifted from jffs2 */
309
310 struct yaffs_obj *obj;
311 unsigned char *pg_buf;
312 int ret;
313 loff_t pos = ((loff_t) pg->index) << PAGE_SHIFT;
314 struct yaffs_dev *dev;
315
316 yaffs_trace(YAFFS_TRACE_OS,
317 "yaffs_readpage_nolock at %lld, size %08x",
318 (long long)pos,
319 (unsigned)PAGE_SIZE);
320
321 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
322
323 dev = obj->my_dev;
324
325#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
326 BUG_ON(!PageLocked(pg));
327#else
328 if (!PageLocked(pg))
329 PAGE_BUG(pg);
330#endif
331
332 pg_buf = kmap(pg);
333 /* FIXME: Can kmap fail? */
334
335 yaffs_gross_lock(dev);
336
337 ret = yaffs_file_rd(obj, pg_buf, pos, PAGE_CACHE_SIZE);
338
339 yaffs_gross_unlock(dev);
340
341 if (ret >= 0)
342 ret = 0;
343
344 if (ret) {
345 ClearPageUptodate(pg);
346 SetPageError(pg);
347 } else {
348 SetPageUptodate(pg);
349 ClearPageError(pg);
350 }
351
352 flush_dcache_page(pg);
353 kunmap(pg);
354
355 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
356 return ret;
357}
358
359static int yaffs_readpage_unlock(struct file *f, struct page *pg)
360{
361 int ret = yaffs_readpage_nolock(f, pg);
362 UnlockPage(pg);
363 return ret;
364}
365
366static int yaffs_readpage(struct file *f, struct page *pg)
367{
368 int ret;
369
370 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
371 ret = yaffs_readpage_unlock(f, pg);
372 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
373 return ret;
374}
375
376
377static void yaffs_set_super_dirty_val(struct yaffs_dev *dev, int val)
378{
379 struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
380
381 if (lc)
382 lc->dirty = val;
383
384# ifdef YAFFS_SUPER_HAS_DIRTY
385 {
386 struct super_block *sb = lc->super;
387
388 if (sb)
389 sb->s_dirt = val;
390 }
391#endif
392
393}
394
395static void yaffs_set_super_dirty(struct yaffs_dev *dev)
396{
397 yaffs_set_super_dirty_val(dev, 1);
398}
399
400static void yaffs_clear_super_dirty(struct yaffs_dev *dev)
401{
402 yaffs_set_super_dirty_val(dev, 0);
403}
404
405static int yaffs_check_super_dirty(struct yaffs_dev *dev)
406{
407 struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
408
409 if (lc && lc->dirty)
410 return 1;
411
412# ifdef YAFFS_SUPER_HAS_DIRTY
413 {
414 struct super_block *sb = lc->super;
415
416 if (sb && sb->s_dirt)
417 return 1;
418 }
419#endif
420 return 0;
421
422}
423
424#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
425static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
426#else
427static int yaffs_writepage(struct page *page)
428#endif
429{
430 struct yaffs_dev *dev;
431 struct address_space *mapping = page->mapping;
432 struct inode *inode;
433 unsigned long end_index;
434 char *buffer;
435 struct yaffs_obj *obj;
436 int n_written = 0;
437 unsigned n_bytes;
438 loff_t i_size;
439
440 if (!mapping)
441 BUG();
442 inode = mapping->host;
443 if (!inode)
444 BUG();
445 i_size = i_size_read(inode);
446
447 end_index = i_size >> PAGE_CACHE_SHIFT;
448
449 if (page->index < end_index)
450 n_bytes = PAGE_CACHE_SIZE;
451 else {
452 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
453
454 if (page->index > end_index || !n_bytes) {
455 yaffs_trace(YAFFS_TRACE_OS,
456 "yaffs_writepage at %lld, inode size = %lld!!",
457 ((loff_t)page->index) << PAGE_CACHE_SHIFT,
458 inode->i_size);
459 yaffs_trace(YAFFS_TRACE_OS,
460 " -> don't care!!");
461
462 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
463 set_page_writeback(page);
464 unlock_page(page);
465 end_page_writeback(page);
466 return 0;
467 }
468 }
469
470 if (n_bytes != PAGE_CACHE_SIZE)
471 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
472
473 get_page(page);
474
475 buffer = kmap(page);
476
477 obj = yaffs_inode_to_obj(inode);
478 dev = obj->my_dev;
479 yaffs_gross_lock(dev);
480
481 yaffs_trace(YAFFS_TRACE_OS,
482 "yaffs_writepage at %lld, size %08x",
483 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes);
484 yaffs_trace(YAFFS_TRACE_OS,
485 "writepag0: obj = %lld, ino = %lld",
486 obj->variant.file_variant.file_size, inode->i_size);
487
488 n_written = yaffs_wr_file(obj, buffer,
489 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes, 0);
490
491 yaffs_set_super_dirty(dev);
492
493 yaffs_trace(YAFFS_TRACE_OS,
494 "writepag1: obj = %lld, ino = %lld",
495 obj->variant.file_variant.file_size, inode->i_size);
496
497 yaffs_gross_unlock(dev);
498
499 kunmap(page);
500 set_page_writeback(page);
501 unlock_page(page);
502 end_page_writeback(page);
503 put_page(page);
504
505 return (n_written == n_bytes) ? 0 : -ENOSPC;
506}
507
508/* Space holding and freeing is done to ensure we have space available for write_begin/end */
509/* For now we just assume few parallel writes and check against a small number. */
510/* Todo: need to do this with a counter to handle parallel reads better */
511
512static ssize_t yaffs_hold_space(struct file *f)
513{
514 struct yaffs_obj *obj;
515 struct yaffs_dev *dev;
516
517 int n_free_chunks;
518
519 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
520
521 dev = obj->my_dev;
522
523 yaffs_gross_lock(dev);
524
525 n_free_chunks = yaffs_get_n_free_chunks(dev);
526
527 yaffs_gross_unlock(dev);
528
529 return (n_free_chunks > 20) ? 1 : 0;
530}
531
532static void yaffs_release_space(struct file *f)
533{
534 struct yaffs_obj *obj;
535 struct yaffs_dev *dev;
536
537 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
538
539 dev = obj->my_dev;
540
541 yaffs_gross_lock(dev);
542
543 yaffs_gross_unlock(dev);
544}
545
546#if (YAFFS_USE_WRITE_BEGIN_END > 0)
547static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
548 loff_t pos, unsigned len, unsigned flags,
549 struct page **pagep, void **fsdata)
550{
551 struct page *pg = NULL;
552 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
553
554 int ret = 0;
555 int space_held = 0;
556
557 /* Get a page */
558#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
559 pg = grab_cache_page_write_begin(mapping, index, flags);
560#else
561 pg = __grab_cache_page(mapping, index);
562#endif
563
564 *pagep = pg;
565 if (!pg) {
566 ret = -ENOMEM;
567 goto out;
568 }
569 yaffs_trace(YAFFS_TRACE_OS,
570 "start yaffs_write_begin index %d(%x) uptodate %d",
571 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
572
573 /* Get fs space */
574 space_held = yaffs_hold_space(filp);
575
576 if (!space_held) {
577 ret = -ENOSPC;
578 goto out;
579 }
580
581 /* Update page if required */
582
583 if (!Page_Uptodate(pg))
584 ret = yaffs_readpage_nolock(filp, pg);
585
586 if (ret)
587 goto out;
588
589 /* Happy path return */
590 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
591
592 return 0;
593
594out:
595 yaffs_trace(YAFFS_TRACE_OS,
596 "end yaffs_write_begin fail returning %d", ret);
597 if (space_held)
598 yaffs_release_space(filp);
599 if (pg) {
600 unlock_page(pg);
601 page_cache_release(pg);
602 }
603 return ret;
604}
605
606#else
607
608static int yaffs_prepare_write(struct file *f, struct page *pg,
609 unsigned offset, unsigned to)
610{
611 yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
612
613 if (!Page_Uptodate(pg))
614 return yaffs_readpage_nolock(f, pg);
615 return 0;
616}
617#endif
618
619
620static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
621 loff_t * pos)
622{
623 struct yaffs_obj *obj;
624 int n_written;
625 loff_t ipos;
626 struct inode *inode;
627 struct yaffs_dev *dev;
628
629 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
630
631 if (!obj) {
632 yaffs_trace(YAFFS_TRACE_OS,
633 "yaffs_file_write: hey obj is null!");
634 return -EINVAL;
635 }
636
637 dev = obj->my_dev;
638
639 yaffs_gross_lock(dev);
640
641 inode = Y_GET_DENTRY(f)->d_inode;
642
643 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
644 ipos = inode->i_size;
645 else
646 ipos = *pos;
647
648 yaffs_trace(YAFFS_TRACE_OS,
649 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %lld",
650 (unsigned)n, (unsigned)n, obj->obj_id, ipos);
651
652 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
653
654 yaffs_set_super_dirty(dev);
655
656 yaffs_trace(YAFFS_TRACE_OS,
657 "yaffs_file_write: %d(%x) bytes written",
658 (unsigned)n, (unsigned)n);
659
660 if (n_written > 0) {
661 ipos += n_written;
662 *pos = ipos;
663 if (ipos > inode->i_size) {
664 inode->i_size = ipos;
665 inode->i_blocks = (ipos + 511) >> 9;
666
667 yaffs_trace(YAFFS_TRACE_OS,
668 "yaffs_file_write size updated to %lld bytes, %d blocks",
669 ipos, (int)(inode->i_blocks));
670 }
671
672 }
673 yaffs_gross_unlock(dev);
674 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
675}
676
677
678#if (YAFFS_USE_WRITE_BEGIN_END > 0)
679static int yaffs_write_end(struct file *filp, struct address_space *mapping,
680 loff_t pos, unsigned len, unsigned copied,
681 struct page *pg, void *fsdadata)
682{
683 int ret = 0;
684 void *addr, *kva;
685 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
686
687 kva = kmap(pg);
688 addr = kva + offset_into_page;
689
690 yaffs_trace(YAFFS_TRACE_OS,
691 "yaffs_write_end addr %p pos %lld n_bytes %d",
692 addr, pos, copied);
693
694 ret = yaffs_file_write(filp, addr, copied, &pos);
695
696 if (ret != copied) {
697 yaffs_trace(YAFFS_TRACE_OS,
698 "yaffs_write_end not same size ret %d copied %d",
699 ret, copied);
700 SetPageError(pg);
701 }
702
703 kunmap(pg);
704
705 yaffs_release_space(filp);
706 unlock_page(pg);
707 page_cache_release(pg);
708 return ret;
709}
710#else
711
712static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
713 unsigned to)
714{
715 void *addr, *kva;
716
717 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
718 int n_bytes = to - offset;
719 int n_written;
720
721 kva = kmap(pg);
722 addr = kva + offset;
723
724 yaffs_trace(YAFFS_TRACE_OS,
725 "yaffs_commit_write addr %p pos %lld n_bytes %d",
726 addr, pos, n_bytes);
727
728 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
729
730 if (n_written != n_bytes) {
731 yaffs_trace(YAFFS_TRACE_OS,
732 "yaffs_commit_write not same size n_written %d n_bytes %d",
733 n_written, n_bytes);
734 SetPageError(pg);
735 }
736 kunmap(pg);
737
738 yaffs_trace(YAFFS_TRACE_OS,
739 "yaffs_commit_write returning %d",
740 n_written == n_bytes ? 0 : n_written);
741
742 return n_written == n_bytes ? 0 : n_written;
743}
744#endif
745
746static struct address_space_operations yaffs_file_address_operations = {
747 .readpage = yaffs_readpage,
748 .writepage = yaffs_writepage,
749#if (YAFFS_USE_WRITE_BEGIN_END > 0)
750 .write_begin = yaffs_write_begin,
751 .write_end = yaffs_write_end,
752#else
753 .prepare_write = yaffs_prepare_write,
754 .commit_write = yaffs_commit_write,
755#endif
756};
757
758
759#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
760static int yaffs_file_flush(struct file *file, fl_owner_t id)
761#else
762static int yaffs_file_flush(struct file *file)
763#endif
764{
765 struct yaffs_obj *obj = yaffs_dentry_to_obj(Y_GET_DENTRY(file));
766
767 struct yaffs_dev *dev = obj->my_dev;
768
769 yaffs_trace(YAFFS_TRACE_OS,
770 "yaffs_file_flush object %d (%s)",
771 obj->obj_id,
772 obj->dirty ? "dirty" : "clean");
773
774 yaffs_gross_lock(dev);
775
776 yaffs_flush_file(obj, 1, 0, 0);
777
778 yaffs_gross_unlock(dev);
779
780 return 0;
781}
782
783
784#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
785static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync)
786#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
787static int yaffs_sync_object(struct file *file, int datasync)
788#else
789static int yaffs_sync_object(struct file *file, struct dentry *dentry,
790 int datasync)
791#endif
792{
793 struct yaffs_obj *obj;
794 struct yaffs_dev *dev;
795#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
796 struct dentry *dentry = file->f_path.dentry;
797#endif
798
799 obj = yaffs_dentry_to_obj(dentry);
800
801 dev = obj->my_dev;
802
803 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
804 "yaffs_sync_object");
805 yaffs_gross_lock(dev);
806 yaffs_flush_file(obj, 1, datasync, 0);
807 yaffs_gross_unlock(dev);
808 return 0;
809}
810
811
812#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
813static const struct file_operations yaffs_file_operations = {
814#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
815#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
816 .read = new_sync_read,
817 .write = new_sync_write,
818#endif
819 .read_iter = generic_file_read_iter,
820 .write_iter = generic_file_write_iter,
821#else
822 .read = do_sync_read,
823 .write = do_sync_write,
824 .aio_read = generic_file_aio_read,
825 .aio_write = generic_file_aio_write,
826#endif
827 .mmap = generic_file_mmap,
828 .flush = yaffs_file_flush,
829 .fsync = yaffs_sync_object,
830 .splice_read = generic_file_splice_read,
831#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
832 .splice_write = iter_file_splice_write,
833#else
834 .splice_write = generic_file_splice_write,
835#endif
836 .llseek = generic_file_llseek,
837};
838
839#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
840
841static const struct file_operations yaffs_file_operations = {
842 .read = do_sync_read,
843 .write = do_sync_write,
844 .aio_read = generic_file_aio_read,
845 .aio_write = generic_file_aio_write,
846 .mmap = generic_file_mmap,
847 .flush = yaffs_file_flush,
848 .fsync = yaffs_sync_object,
849 .sendfile = generic_file_sendfile,
850};
851
852#else
853
854static const struct file_operations yaffs_file_operations = {
855 .read = generic_file_read,
856 .write = generic_file_write,
857 .mmap = generic_file_mmap,
858 .flush = yaffs_file_flush,
859 .fsync = yaffs_sync_object,
860#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
861 .sendfile = generic_file_sendfile,
862#endif
863};
864#endif
865
866
867
868
869#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
870static void zero_user_segment(struct page *page, unsigned start, unsigned end)
871{
872 void *kaddr = kmap_atomic(page, KM_USER0);
873 memset(kaddr + start, 0, end - start);
874 kunmap_atomic(kaddr, KM_USER0);
875 flush_dcache_page(page);
876}
877#endif
878
879
880static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
881{
882#ifdef YAFFS_USE_TRUNCATE_SETSIZE
883 truncate_setsize(inode, newsize);
884 return 0;
885#else
886 truncate_inode_pages(&inode->i_data, newsize);
887 return 0;
888#endif
889
890}
891
892
893static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
894{
895#ifdef YAFFS_USE_SETATTR_COPY
896 setattr_copy(inode, attr);
897 return 0;
898#else
899 return inode_setattr(inode, attr);
900#endif
901
902}
903
904static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
905{
906 struct inode *inode = dentry->d_inode;
907 int error = 0;
908 struct yaffs_dev *dev;
909
910 yaffs_trace(YAFFS_TRACE_OS,
911 "yaffs_setattr of object %d",
912 yaffs_inode_to_obj(inode)->obj_id);
913#if 0
914 /* Fail if a requested resize >= 2GB */
915 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
916 error = -EINVAL;
917#endif
918
919 if (error == 0)
920 error = setattr_prepare(dentry, attr);
921 if (error == 0) {
922 int result;
923 if (!error) {
924 error = yaffs_vfs_setattr(inode, attr);
925 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
926 if (attr->ia_valid & ATTR_SIZE) {
927 yaffs_vfs_setsize(inode, attr->ia_size);
928 inode->i_blocks = (inode->i_size + 511) >> 9;
929 }
930 }
931 dev = yaffs_inode_to_obj(inode)->my_dev;
932 if (attr->ia_valid & ATTR_SIZE) {
933 yaffs_trace(YAFFS_TRACE_OS,
934 "resize to %d(%x)",
935 (int)(attr->ia_size),
936 (int)(attr->ia_size));
937 }
938 yaffs_gross_lock(dev);
939 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
940 if (result == YAFFS_OK) {
941 error = 0;
942 } else {
943 error = -EPERM;
944 }
945 yaffs_gross_unlock(dev);
946
947 }
948
949 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
950
951 return error;
952}
953
954#ifdef YAFFS_USE_XATTR
955#if (YAFFS_NEW_XATTR > 0)
956static int yaffs_setxattr(struct dentry *dentry, struct inode *inode,
957 const char *name, const void *value, size_t size, int flags)
958{
959#else
960static int yaffs_setxattr(struct dentry *dentry, const char *name,
961 const void *value, size_t size, int flags)
962{
963 struct inode *inode = dentry->d_inode;
964#endif
965 int error = 0;
966 struct yaffs_dev *dev;
967 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
968
969 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
970
971 if (error == 0) {
972 int result;
973 dev = obj->my_dev;
974 yaffs_gross_lock(dev);
975 result = yaffs_set_xattrib(obj, name, value, size, flags);
976 if (result == YAFFS_OK)
977 error = 0;
978 else if (result < 0)
979 error = result;
980 yaffs_gross_unlock(dev);
981
982 }
983 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
984
985 return error;
986}
987
988#if (YAFFS_NEW_XATTR > 0)
989static ssize_t yaffs_getxattr(struct dentry * dentry, struct inode *inode,
990 const char *name, void *buff, size_t size)
991{
992#else
993static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
994 void *buff, size_t size)
995{
996 struct inode *inode = dentry->d_inode;
997#endif
998 int error = 0;
999 struct yaffs_dev *dev;
1000 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1001
1002 yaffs_trace(YAFFS_TRACE_OS,
1003 "yaffs_getxattr \"%s\" from object %d",
1004 name, obj->obj_id);
1005
1006 if (error == 0) {
1007 dev = obj->my_dev;
1008 yaffs_gross_lock(dev);
1009 error = yaffs_get_xattrib(obj, name, buff, size);
1010 yaffs_gross_unlock(dev);
1011
1012 }
1013 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
1014
1015 return error;
1016}
1017
1018static int yaffs_removexattr(struct dentry *dentry, const char *name)
1019{
1020 struct inode *inode = dentry->d_inode;
1021 int error = 0;
1022 struct yaffs_dev *dev;
1023 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1024
1025 yaffs_trace(YAFFS_TRACE_OS,
1026 "yaffs_removexattr of object %d", obj->obj_id);
1027
1028 if (error == 0) {
1029 int result;
1030 dev = obj->my_dev;
1031 yaffs_gross_lock(dev);
1032 result = yaffs_remove_xattrib(obj, name);
1033 if (result == YAFFS_OK)
1034 error = 0;
1035 else if (result < 0)
1036 error = result;
1037 yaffs_gross_unlock(dev);
1038
1039 }
1040 yaffs_trace(YAFFS_TRACE_OS,
1041 "yaffs_removexattr done returning %d", error);
1042
1043 return error;
1044}
1045#endif
1046
1047static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
1048{
1049 struct inode *inode = dentry->d_inode;
1050 int error = 0;
1051 struct yaffs_dev *dev;
1052 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1053
1054 yaffs_trace(YAFFS_TRACE_OS,
1055 "yaffs_listxattr of object %d", obj->obj_id);
1056
1057 if (error == 0) {
1058 dev = obj->my_dev;
1059 yaffs_gross_lock(dev);
1060 error = yaffs_list_xattrib(obj, buff, size);
1061 yaffs_gross_unlock(dev);
1062
1063 }
1064 yaffs_trace(YAFFS_TRACE_OS,
1065 "yaffs_listxattr done returning %d", error);
1066
1067 return error;
1068}
1069
1070
1071static const struct inode_operations yaffs_file_inode_operations = {
1072 .setattr = yaffs_setattr,
1073#ifdef YAFFS_USE_XATTR
1074 .setxattr = yaffs_setxattr,
1075 .getxattr = yaffs_getxattr,
1076 .removexattr = yaffs_removexattr,
1077#endif
1078 .listxattr = yaffs_listxattr,
1079};
1080
1081
1082static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1083 int buflen)
1084{
1085 unsigned char *alias;
1086 int ret;
1087
1088 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1089
1090 yaffs_gross_lock(dev);
1091
1092 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1093
1094 yaffs_gross_unlock(dev);
1095
1096 if (!alias)
1097 return -ENOMEM;
1098
1099#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0)
1100 ret = vfs_readlink(dentry, buffer, buflen, alias);
1101#else
1102 ret = readlink_copy(buffer, buflen, alias);
1103#endif
1104 kfree(alias);
1105 return ret;
1106}
1107
1108#if (YAFFS_NEW_GET_LINK == 0)
1109#if (YAFFS_NEW_FOLLOW_LINK == 1)
1110static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1111{
1112 void *ret;
1113#else
1114static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1115{
1116 int ret
1117#endif
1118 unsigned char *alias;
1119 int ret_int = 0;
1120 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1121
1122 yaffs_gross_lock(dev);
1123
1124 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1125 yaffs_gross_unlock(dev);
1126
1127 if (!alias) {
1128 ret_int = -ENOMEM;
1129 goto out;
1130 }
1131#if (YAFFS_NEW_FOLLOW_LINK == 1)
1132 nd_set_link(nd, alias);
1133 ret = alias;
1134out:
1135 if (ret_int)
1136 ret = ERR_PTR(ret_int);
1137 return ret;
1138#else
1139 ret = vfs_follow_link(nd, alias);
1140 kfree(alias);
1141out:
1142 if (ret_int)
1143 ret = ret_int;
1144 return ret;
1145#endif
1146}
1147#else
1148static const char *yaffs_get_link(struct dentry *dentry, struct inode *inode, struct delayed_call *done)
1149{
1150 unsigned char *alias;
1151 struct yaffs_dev *dev;
1152
1153 if (!dentry)
1154 return ERR_PTR(-ECHILD);
1155
1156 dev = yaffs_dentry_to_obj(dentry)->my_dev;
1157
1158 yaffs_gross_lock(dev);
1159
1160 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1161 yaffs_gross_unlock(dev);
1162
1163 if (!alias)
1164 return ERR_PTR(-ENOMEM);
1165 set_delayed_call(done, kfree_link, alias);
1166 return alias;
1167}
1168#endif
1169
1170#ifdef YAFFS_HAS_PUT_INODE
1171
1172/* For now put inode is just for debugging
1173 * Put inode is called when the inode **structure** is put.
1174 */
1175static void yaffs_put_inode(struct inode *inode)
1176{
1177 yaffs_trace(YAFFS_TRACE_OS,
1178 "yaffs_put_inode: ino %d, count %d"),
1179 (int)inode->i_ino, atomic_read(&inode->i_count);
1180
1181}
1182#endif
1183
1184#if (YAFFS_NEW_FOLLOW_LINK == 1)
1185void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
1186{
1187 kfree(alias);
1188}
1189#endif
1190
1191static const struct inode_operations yaffs_symlink_inode_operations = {
1192 .readlink = yaffs_readlink,
1193#if (YAFFS_NEW_GET_LINK == 1)
1194 .get_link = yaffs_get_link,
1195#else
1196 .follow_link = yaffs_follow_link,
1197#endif
1198#if (YAFFS_NEW_FOLLOW_LINK == 1)
1199 .put_link = yaffs_put_link,
1200#endif
1201 .setattr = yaffs_setattr,
1202#ifdef YAFFS_USE_XATTR
1203 .setxattr = yaffs_setxattr,
1204 .getxattr = yaffs_getxattr,
1205 .removexattr = yaffs_removexattr,
1206#endif
1207 .listxattr = yaffs_listxattr,
1208};
1209
1210#ifdef YAFFS_USE_OWN_IGET
1211
1212static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1213{
1214 struct inode *inode;
1215 struct yaffs_obj *obj;
1216 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1217
1218 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
1219
1220 inode = iget_locked(sb, ino);
1221 if (!inode)
1222 return ERR_PTR(-ENOMEM);
1223 if (!(inode->i_state & I_NEW))
1224 return inode;
1225
1226 /* NB This is called as a side effect of other functions, but
1227 * we had to release the lock to prevent deadlocks, so
1228 * need to lock again.
1229 */
1230
1231 yaffs_gross_lock(dev);
1232
1233 obj = yaffs_find_by_number(dev, inode->i_ino);
1234
1235 yaffs_fill_inode_from_obj(inode, obj);
1236
1237 yaffs_gross_unlock(dev);
1238
1239 unlock_new_inode(inode);
1240 return inode;
1241}
1242
1243#else
1244
1245static void yaffs_read_inode(struct inode *inode)
1246{
1247 /* NB This is called as a side effect of other functions, but
1248 * we had to release the lock to prevent deadlocks, so
1249 * need to lock again.
1250 */
1251
1252 struct yaffs_obj *obj;
1253 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
1254
1255 yaffs_trace(YAFFS_TRACE_OS,
1256 "yaffs_read_inode for %d", (int)inode->i_ino);
1257
1258 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1259 yaffs_gross_lock(dev);
1260
1261 obj = yaffs_find_by_number(dev, inode->i_ino);
1262
1263 yaffs_fill_inode_from_obj(inode, obj);
1264
1265 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1266 yaffs_gross_unlock(dev);
1267}
1268
1269#endif
1270
1271
1272
1273struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1274 struct yaffs_obj *obj)
1275{
1276 struct inode *inode;
1277
1278 if (!sb) {
1279 yaffs_trace(YAFFS_TRACE_OS,
1280 "yaffs_get_inode for NULL super_block!!");
1281 return NULL;
1282
1283 }
1284
1285 if (!obj) {
1286 yaffs_trace(YAFFS_TRACE_OS,
1287 "yaffs_get_inode for NULL object!!");
1288 return NULL;
1289
1290 }
1291
1292 yaffs_trace(YAFFS_TRACE_OS,
1293 "yaffs_get_inode for object %d", obj->obj_id);
1294
1295 inode = Y_IGET(sb, obj->obj_id);
1296 if (IS_ERR(inode))
1297 return NULL;
1298
1299 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1300 /* iget also increments the inode's i_count */
1301 /* NB You can't be holding gross_lock or deadlock will happen! */
1302
1303 return inode;
1304}
1305
1306
1307
1308#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1309#define YCRED(x) x
1310#else
1311#define YCRED(x) (x->cred)
1312#endif
1313
1314#if LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0)
1315#define YPROC_uid(p) (YCRED(p)->fsuid)
1316#define YPROC_gid(p) (YCRED(p)->fsgid)
1317#define EXTRACT_gid(x) x
1318#define EXTRACT_uid(x) x
1319#define MAKE_gid(x) x
1320#define MAKE_uid(x) x
1321#else
1322#define YPROC_uid(p) from_kuid(&init_user_ns, YCRED(p)->fsuid)
1323#define YPROC_gid(p) from_kgid(&init_user_ns, YCRED(p)->fsgid)
1324#define EXTRACT_gid(x) from_kgid(&init_user_ns, x)
1325#define EXTRACT_uid(x) from_kuid(&init_user_ns, x)
1326#define MAKE_gid(x) make_kgid(&init_user_ns, x)
1327#define MAKE_uid(x) make_kuid(&init_user_ns, x)
1328#endif
1329
1330
1331#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1332static int yaffs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1333 dev_t rdev)
1334#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1335static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1336 dev_t rdev)
1337#else
1338static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1339 int rdev)
1340#endif
1341{
1342 struct inode *inode;
1343
1344 struct yaffs_obj *obj = NULL;
1345 struct yaffs_dev *dev;
1346
1347 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1348
1349 int error = -ENOSPC;
1350 uid_t uid = YPROC_uid(current);
1351 gid_t gid =
1352 (dir->i_mode & S_ISGID) ? EXTRACT_gid(dir->i_gid) : YPROC_gid(current);
1353
1354 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1355 mode |= S_ISGID;
1356
1357 if (parent) {
1358 yaffs_trace(YAFFS_TRACE_OS,
1359 "yaffs_mknod: parent object %d type %d",
1360 parent->obj_id, parent->variant_type);
1361 } else {
1362 yaffs_trace(YAFFS_TRACE_OS,
1363 "yaffs_mknod: could not get parent object");
1364 return -EPERM;
1365 }
1366
1367 yaffs_trace(YAFFS_TRACE_OS,
1368 "yaffs_mknod: making oject for %s, mode %x dev %x",
1369 dentry->d_name.name, mode, rdev);
1370
1371 dev = parent->my_dev;
1372
1373 yaffs_gross_lock(dev);
1374
1375 switch (mode & S_IFMT) {
1376 default:
1377 /* Special (socket, fifo, device...) */
1378 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
1379#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1380 obj =
1381 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1382 gid, old_encode_dev(rdev));
1383#else
1384 obj =
1385 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1386 gid, rdev);
1387#endif
1388 break;
1389 case S_IFREG: /* file */
1390 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
1391 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1392 gid);
1393 break;
1394 case S_IFDIR: /* directory */
1395 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
1396 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1397 uid, gid);
1398 break;
1399 case S_IFLNK: /* symlink */
1400 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
1401 obj = NULL; /* Do we ever get here? */
1402 break;
1403 }
1404
1405 /* Can not call yaffs_get_inode() with gross lock held */
1406 yaffs_gross_unlock(dev);
1407
1408 if (obj) {
1409 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1410 d_instantiate(dentry, inode);
1411 update_dir_time(dir);
1412 yaffs_trace(YAFFS_TRACE_OS,
1413 "yaffs_mknod created object %d count = %d",
1414 obj->obj_id, atomic_read(&inode->i_count));
1415 error = 0;
1416 yaffs_fill_inode_from_obj(dir, parent);
1417 } else {
1418 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
1419 error = -ENOMEM;
1420 }
1421
1422 return error;
1423}
1424
1425#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1426static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1427#else
1428static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1429#endif
1430{
1431 int ret_val;
1432 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
1433 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1434 return ret_val;
1435}
1436
1437
1438#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
1439static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1440 bool dummy)
1441#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1442static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1443 struct nameidata *n)
1444#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1445static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1446 struct nameidata *n)
1447#else
1448static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1449#endif
1450{
1451 yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
1452 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1453}
1454
1455#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
1456static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1457 unsigned int dummy)
1458#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1459static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1460 struct nameidata *n)
1461#else
1462static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1463#endif
1464{
1465 struct yaffs_obj *obj;
1466 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1467
1468 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
1469
1470 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1471 yaffs_gross_lock(dev);
1472
1473 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
1474 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
1475
1476 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
1477
1478 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
1479
1480 /* Can't hold gross lock when calling yaffs_get_inode() */
1481 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1482 yaffs_gross_unlock(dev);
1483
1484 if (obj) {
1485 yaffs_trace(YAFFS_TRACE_OS,
1486 "yaffs_lookup found %d", obj->obj_id);
1487
1488 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1489 } else {
1490 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
1491
1492 }
1493
1494/* added NCB for 2.5/6 compatability - forces add even if inode is
1495 * NULL which creates dentry hash */
1496 d_add(dentry, inode);
1497
1498 return NULL;
1499}
1500
1501/*
1502 * Create a link...
1503 */
1504static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1505 struct dentry *dentry)
1506{
1507 struct inode *inode = old_dentry->d_inode;
1508 struct yaffs_obj *obj = NULL;
1509 struct yaffs_obj *link = NULL;
1510 struct yaffs_dev *dev;
1511
1512 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
1513
1514 obj = yaffs_inode_to_obj(inode);
1515 dev = obj->my_dev;
1516
1517 yaffs_gross_lock(dev);
1518
1519 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1520 link =
1521 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1522 obj);
1523
1524 if (link) {
1525 set_nlink(old_dentry->d_inode, yaffs_get_obj_link_count(obj));
1526 d_instantiate(dentry, old_dentry->d_inode);
1527 atomic_inc(&old_dentry->d_inode->i_count);
1528 yaffs_trace(YAFFS_TRACE_OS,
1529 "yaffs_link link count %d i_count %d",
1530 old_dentry->d_inode->i_nlink,
1531 atomic_read(&old_dentry->d_inode->i_count));
1532 }
1533
1534 yaffs_gross_unlock(dev);
1535
1536 if (link) {
1537 update_dir_time(dir);
1538 return 0;
1539 }
1540
1541 return -EPERM;
1542}
1543
1544static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1545 const char *symname)
1546{
1547 struct yaffs_obj *obj;
1548 struct yaffs_dev *dev;
1549 uid_t uid = YPROC_uid(current);
1550 gid_t gid =
1551 (dir->i_mode & S_ISGID) ? EXTRACT_gid(dir->i_gid) : YPROC_gid(current);
1552
1553 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
1554
1555 if (strnlen(dentry->d_name.name, YAFFS_MAX_NAME_LENGTH + 1) >
1556 YAFFS_MAX_NAME_LENGTH)
1557 return -ENAMETOOLONG;
1558
1559 if (strnlen(symname, YAFFS_MAX_ALIAS_LENGTH + 1) >
1560 YAFFS_MAX_ALIAS_LENGTH)
1561 return -ENAMETOOLONG;
1562
1563 dev = yaffs_inode_to_obj(dir)->my_dev;
1564 yaffs_gross_lock(dev);
1565 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1566 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1567 yaffs_gross_unlock(dev);
1568
1569 if (obj) {
1570 struct inode *inode;
1571
1572 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1573 d_instantiate(dentry, inode);
1574 update_dir_time(dir);
1575 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
1576 return 0;
1577 } else {
1578 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
1579 }
1580
1581 return -ENOMEM;
1582}
1583
1584/*
1585 * The VFS layer already does all the dentry stuff for rename.
1586 *
1587 * NB: POSIX says you can rename an object over an old object of the same name
1588 */
1589#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0))
1590static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1591 struct inode *new_dir, struct dentry *new_dentry, unsigned int unused)
1592#else
1593static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1594 struct inode *new_dir, struct dentry *new_dentry)
1595#endif
1596{
1597 struct yaffs_dev *dev;
1598 int ret_val = YAFFS_FAIL;
1599 struct yaffs_obj *target;
1600
1601 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
1602 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1603
1604 yaffs_gross_lock(dev);
1605
1606 /* Check if the target is an existing directory that is not empty. */
1607 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1608 new_dentry->d_name.name);
1609
1610 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1611 !list_empty(&target->variant.dir_variant.children)) {
1612
1613 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
1614
1615 ret_val = YAFFS_FAIL;
1616 } else {
1617 /* Now does unlinking internally using shadowing mechanism */
1618 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
1619
1620 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1621 old_dentry->d_name.name,
1622 yaffs_inode_to_obj(new_dir),
1623 new_dentry->d_name.name);
1624 }
1625 yaffs_gross_unlock(dev);
1626
1627 if (ret_val == YAFFS_OK) {
1628 if (target)
1629 inode_dec_link_count(new_dentry->d_inode);
1630
1631 update_dir_time(old_dir);
1632 if (old_dir != new_dir)
1633 update_dir_time(new_dir);
1634 return 0;
1635 } else {
1636 return -ENOTEMPTY;
1637 }
1638}
1639
1640
1641
1642
1643static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1644{
1645 int ret_val;
1646
1647 struct yaffs_dev *dev;
1648 struct yaffs_obj *obj;
1649
1650 yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
1651 (int)(dir->i_ino), dentry->d_name.name);
1652 obj = yaffs_inode_to_obj(dir);
1653 dev = obj->my_dev;
1654
1655 yaffs_gross_lock(dev);
1656
1657 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1658
1659 if (ret_val == YAFFS_OK) {
1660 inode_dec_link_count(dentry->d_inode);
1661#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0))
1662 inode_inc_iversion(dir);
1663#else
1664 dir->i_version++;
1665#endif
1666 yaffs_gross_unlock(dev);
1667 update_dir_time(dir);
1668 return 0;
1669 }
1670 yaffs_gross_unlock(dev);
1671 return -ENOTEMPTY;
1672}
1673
1674
1675
1676static const struct inode_operations yaffs_dir_inode_operations = {
1677 .create = yaffs_create,
1678 .lookup = yaffs_lookup,
1679 .link = yaffs_link,
1680 .unlink = yaffs_unlink,
1681 .symlink = yaffs_symlink,
1682 .mkdir = yaffs_mkdir,
1683 .rmdir = yaffs_unlink,
1684 .mknod = yaffs_mknod,
1685 .rename = yaffs_rename,
1686 .setattr = yaffs_setattr,
1687 .listxattr = yaffs_listxattr,
1688#ifdef YAFFS_USE_XATTR
1689 .setxattr = yaffs_setxattr,
1690 .getxattr = yaffs_getxattr,
1691 .removexattr = yaffs_removexattr,
1692#endif
1693};
1694
1695/*-----------------------------------------------------------------*/
1696/* Directory search context allows us to unlock access to yaffs during
1697 * filldir without causing problems with the directory being modified.
1698 * This is similar to the tried and tested mechanism used in yaffs direct.
1699 *
1700 * A search context iterates along a doubly linked list of siblings in the
1701 * directory. If the iterating object is deleted then this would corrupt
1702 * the list iteration, likely causing a crash. The search context avoids
1703 * this by using the remove_obj_fn to move the search context to the
1704 * next object before the object is deleted.
1705 *
1706 * Many readdirs (and thus seach conexts) may be alive simulateously so
1707 * each struct yaffs_dev has a list of these.
1708 *
1709 * A seach context lives for the duration of a readdir.
1710 *
1711 * All these functions must be called while yaffs is locked.
1712 */
1713
1714struct yaffs_search_context {
1715 struct yaffs_dev *dev;
1716 struct yaffs_obj *dir_obj;
1717 struct yaffs_obj *next_return;
1718 struct list_head others;
1719};
1720
1721/*
1722 * yaffs_new_search() creates a new search context, initialises it and
1723 * adds it to the device's search context list.
1724 *
1725 * Called at start of readdir.
1726 */
1727static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
1728{
1729 struct yaffs_dev *dev = dir->my_dev;
1730 struct yaffs_search_context *sc =
1731 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
1732 if (sc) {
1733 sc->dir_obj = dir;
1734 sc->dev = dev;
1735 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
1736 sc->next_return = NULL;
1737 else
1738 sc->next_return =
1739 list_entry(dir->variant.dir_variant.children.next,
1740 struct yaffs_obj, siblings);
1741 INIT_LIST_HEAD(&sc->others);
1742 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
1743 }
1744 return sc;
1745}
1746
1747/*
1748 * yaffs_search_end() disposes of a search context and cleans up.
1749 */
1750static void yaffs_search_end(struct yaffs_search_context *sc)
1751{
1752 if (sc) {
1753 list_del(&sc->others);
1754 kfree(sc);
1755 }
1756}
1757
1758/*
1759 * yaffs_search_advance() moves a search context to the next object.
1760 * Called when the search iterates or when an object removal causes
1761 * the search context to be moved to the next object.
1762 */
1763static void yaffs_search_advance(struct yaffs_search_context *sc)
1764{
1765 if (!sc)
1766 return;
1767
1768 if (sc->next_return == NULL ||
1769 list_empty(&sc->dir_obj->variant.dir_variant.children))
1770 sc->next_return = NULL;
1771 else {
1772 struct list_head *next = sc->next_return->siblings.next;
1773
1774 if (next == &sc->dir_obj->variant.dir_variant.children)
1775 sc->next_return = NULL; /* end of list */
1776 else
1777 sc->next_return =
1778 list_entry(next, struct yaffs_obj, siblings);
1779 }
1780}
1781
1782/*
1783 * yaffs_remove_obj_callback() is called when an object is unlinked.
1784 * We check open search contexts and advance any which are currently
1785 * on the object being iterated.
1786 */
1787static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
1788{
1789
1790 struct list_head *i;
1791 struct yaffs_search_context *sc;
1792 struct list_head *search_contexts =
1793 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
1794
1795 /* Iterate through the directory search contexts.
1796 * If any are currently on the object being removed, then advance
1797 * the search context to the next object to prevent a hanging pointer.
1798 */
1799 list_for_each(i, search_contexts) {
1800 sc = list_entry(i, struct yaffs_search_context, others);
1801 if (sc->next_return == obj)
1802 yaffs_search_advance(sc);
1803 }
1804
1805}
1806
1807
1808/*-----------------------------------------------------------------*/
1809
1810#ifdef YAFFS_USE_DIR_ITERATE
1811static int yaffs_iterate(struct file *f, struct dir_context *dc)
1812{
1813 struct yaffs_obj *obj;
1814 struct yaffs_dev *dev;
1815 struct yaffs_search_context *sc;
1816 unsigned long curoffs;
1817 struct yaffs_obj *l;
1818 int ret_val = 0;
1819
1820 char name[YAFFS_MAX_NAME_LENGTH + 1];
1821
1822 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
1823 dev = obj->my_dev;
1824
1825 yaffs_gross_lock(dev);
1826
1827 yaffs_dev_to_lc(dev)->readdir_process = current;
1828
1829 sc = yaffs_new_search(obj);
1830 if (!sc) {
1831 ret_val = -ENOMEM;
1832 goto out;
1833 }
1834
1835 if (!dir_emit_dots(f, dc)) {
1836 yaffs_gross_unlock(dev);
1837 return 0;
1838 }
1839
1840 curoffs = 1;
1841
1842 while (sc->next_return) {
1843 curoffs++;
1844 l = sc->next_return;
1845 if (curoffs >= dc->pos) {
1846 int this_inode = yaffs_get_obj_inode(l);
1847 int this_type = yaffs_get_obj_type(l);
1848
1849 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1850 yaffs_trace(YAFFS_TRACE_OS,
1851 "yaffs_readdir: %s inode %d",
1852 name, yaffs_get_obj_inode(l));
1853
1854 yaffs_gross_unlock(dev);
1855
1856 if (!dir_emit(dc,
1857 name,
1858 strlen(name),
1859 this_inode,
1860 this_type)) {
1861 yaffs_gross_lock(dev);
1862 goto out;
1863 }
1864
1865 yaffs_gross_lock(dev);
1866
1867 dc->pos++;
1868 f->f_pos++;
1869 }
1870 yaffs_search_advance(sc);
1871 }
1872
1873out:
1874 yaffs_search_end(sc);
1875 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1876 yaffs_gross_unlock(dev);
1877
1878 return ret_val;
1879}
1880
1881#else
1882
1883static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1884{
1885 struct yaffs_obj *obj;
1886 struct yaffs_dev *dev;
1887 struct yaffs_search_context *sc;
1888 struct inode *inode = Y_GET_DENTRY(f)->d_inode;
1889 unsigned long offset, curoffs;
1890 struct yaffs_obj *l;
1891 int ret_val = 0;
1892
1893 char name[YAFFS_MAX_NAME_LENGTH + 1];
1894
1895 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
1896 dev = obj->my_dev;
1897
1898 yaffs_gross_lock(dev);
1899
1900 yaffs_dev_to_lc(dev)->readdir_process = current;
1901
1902 offset = f->f_pos;
1903
1904 sc = yaffs_new_search(obj);
1905 if (!sc) {
1906 ret_val = -ENOMEM;
1907 goto out;
1908 }
1909
1910 yaffs_trace(YAFFS_TRACE_OS,
1911 "yaffs_readdir: starting at %d", (int)offset);
1912
1913 if (offset == 0) {
1914 yaffs_trace(YAFFS_TRACE_OS,
1915 "yaffs_readdir: entry . ino %d",
1916 (int)inode->i_ino);
1917 yaffs_gross_unlock(dev);
1918 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1919 yaffs_gross_lock(dev);
1920 goto out;
1921 }
1922 yaffs_gross_lock(dev);
1923 offset++;
1924 f->f_pos++;
1925 }
1926 if (offset == 1) {
1927 yaffs_trace(YAFFS_TRACE_OS,
1928 "yaffs_readdir: entry .. ino %d",
1929 (int)f->f_dentry->d_parent->d_inode->i_ino);
1930 yaffs_gross_unlock(dev);
1931 if (filldir(dirent, "..", 2, offset,
1932 f->f_dentry->d_parent->d_inode->i_ino,
1933 DT_DIR) < 0) {
1934 yaffs_gross_lock(dev);
1935 goto out;
1936 }
1937 yaffs_gross_lock(dev);
1938 offset++;
1939 f->f_pos++;
1940 }
1941
1942 curoffs = 1;
1943
1944 /* If the directory has changed since the open or last call to
1945 readdir, rewind to after the 2 canned entries. */
1946 if (f->f_version != inode->i_version) {
1947 offset = 2;
1948 f->f_pos = offset;
1949 f->f_version = inode->i_version;
1950 }
1951
1952 while (sc->next_return) {
1953 curoffs++;
1954 l = sc->next_return;
1955 if (curoffs >= offset) {
1956 int this_inode = yaffs_get_obj_inode(l);
1957 int this_type = yaffs_get_obj_type(l);
1958
1959 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1960 yaffs_trace(YAFFS_TRACE_OS,
1961 "yaffs_readdir: %s inode %d",
1962 name, yaffs_get_obj_inode(l));
1963
1964 yaffs_gross_unlock(dev);
1965
1966 if (filldir(dirent,
1967 name,
1968 strlen(name),
1969 offset, this_inode, this_type) < 0) {
1970 yaffs_gross_lock(dev);
1971 goto out;
1972 }
1973
1974 yaffs_gross_lock(dev);
1975
1976 offset++;
1977 f->f_pos++;
1978 }
1979 yaffs_search_advance(sc);
1980 }
1981
1982out:
1983 yaffs_search_end(sc);
1984 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1985 yaffs_gross_unlock(dev);
1986
1987 return ret_val;
1988}
1989
1990#endif
1991
1992static const struct file_operations yaffs_dir_operations = {
1993 .read = generic_read_dir,
1994#ifdef YAFFS_USE_DIR_ITERATE
1995 .iterate = yaffs_iterate,
1996#else
1997 .readdir = yaffs_readdir,
1998#endif
1999 .fsync = yaffs_sync_object,
2000 .llseek = generic_file_llseek,
2001};
2002
2003static void yaffs_fill_inode_from_obj(struct inode *inode,
2004 struct yaffs_obj *obj)
2005{
2006 if (inode && obj) {
2007
2008 /* Check mode against the variant type and attempt to repair if broken. */
2009 u32 mode = obj->yst_mode;
2010 switch (obj->variant_type) {
2011 case YAFFS_OBJECT_TYPE_FILE:
2012 if (!S_ISREG(mode)) {
2013 obj->yst_mode &= ~S_IFMT;
2014 obj->yst_mode |= S_IFREG;
2015 }
2016
2017 break;
2018 case YAFFS_OBJECT_TYPE_SYMLINK:
2019 if (!S_ISLNK(mode)) {
2020 obj->yst_mode &= ~S_IFMT;
2021 obj->yst_mode |= S_IFLNK;
2022 }
2023
2024 break;
2025 case YAFFS_OBJECT_TYPE_DIRECTORY:
2026 if (!S_ISDIR(mode)) {
2027 obj->yst_mode &= ~S_IFMT;
2028 obj->yst_mode |= S_IFDIR;
2029 }
2030
2031 break;
2032 case YAFFS_OBJECT_TYPE_UNKNOWN:
2033 case YAFFS_OBJECT_TYPE_HARDLINK:
2034 case YAFFS_OBJECT_TYPE_SPECIAL:
2035 default:
2036 /* TODO? */
2037 break;
2038 }
2039
2040 inode->i_flags |= S_NOATIME;
2041
2042 inode->i_ino = obj->obj_id;
2043 inode->i_mode = obj->yst_mode;
2044 inode->i_uid = MAKE_uid(obj->yst_uid);
2045 inode->i_gid = MAKE_gid(obj->yst_gid);
2046#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
2047 inode->i_blksize = inode->i_sb->s_blocksize;
2048#endif
2049#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2050
2051 inode->i_rdev = old_decode_dev(obj->yst_rdev);
2052 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2053 inode->i_atime.tv_nsec = 0;
2054 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2055 inode->i_mtime.tv_nsec = 0;
2056 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2057 inode->i_ctime.tv_nsec = 0;
2058#else
2059 inode->i_rdev = obj->yst_rdev;
2060 inode->i_atime = obj->yst_atime;
2061 inode->i_mtime = obj->yst_mtime;
2062 inode->i_ctime = obj->yst_ctime;
2063#endif
2064 inode->i_size = yaffs_get_obj_length(obj);
2065 inode->i_blocks = (inode->i_size + 511) >> 9;
2066
2067 set_nlink(inode, yaffs_get_obj_link_count(obj));
2068
2069 yaffs_trace(YAFFS_TRACE_OS,
2070 "yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
2071 inode->i_mode, obj->yst_uid, obj->yst_gid,
2072 inode->i_size, atomic_read(&inode->i_count));
2073
2074 switch (obj->yst_mode & S_IFMT) {
2075 default: /* fifo, device or socket */
2076#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2077 init_special_inode(inode, obj->yst_mode,
2078 old_decode_dev(obj->yst_rdev));
2079#else
2080 init_special_inode(inode, obj->yst_mode,
2081 (dev_t) (obj->yst_rdev));
2082#endif
2083 break;
2084 case S_IFREG: /* file */
2085 inode->i_op = &yaffs_file_inode_operations;
2086 inode->i_fop = &yaffs_file_operations;
2087 inode->i_mapping->a_ops =
2088 &yaffs_file_address_operations;
2089 break;
2090 case S_IFDIR: /* directory */
2091 inode->i_op = &yaffs_dir_inode_operations;
2092 inode->i_fop = &yaffs_dir_operations;
2093 break;
2094 case S_IFLNK: /* symlink */
2095 inode->i_op = &yaffs_symlink_inode_operations;
2096 break;
2097 }
2098
2099 yaffs_inode_to_obj_lv(inode) = obj;
2100
2101 obj->my_inode = inode;
2102
2103 } else {
2104 yaffs_trace(YAFFS_TRACE_OS,
2105 "yaffs_fill_inode invalid parameters");
2106 }
2107
2108}
2109
2110
2111
2112/*
2113 * yaffs background thread functions .
2114 * yaffs_bg_thread_fn() the thread function
2115 * yaffs_bg_start() launches the background thread.
2116 * yaffs_bg_stop() cleans up the background thread.
2117 *
2118 * NB:
2119 * The thread should only run after the yaffs is initialised
2120 * The thread should be stopped before yaffs is unmounted.
2121 * The thread should not do any writing while the fs is in read only.
2122 */
2123
2124static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2125{
2126 unsigned erased_chunks =
2127 dev->n_erased_blocks * dev->param.chunks_per_block;
2128 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2129 unsigned scattered = 0; /* Free chunks not in an erased block */
2130
2131 if (erased_chunks < dev->n_free_chunks)
2132 scattered = (dev->n_free_chunks - erased_chunks);
2133
2134 if (!context->bg_running)
2135 return 0;
2136 else if (scattered < (dev->param.chunks_per_block * 2))
2137 return 0;
2138 else if (erased_chunks > dev->n_free_chunks / 2)
2139 return 0;
2140 else if (erased_chunks > dev->n_free_chunks / 4)
2141 return 1;
2142 else
2143 return 2;
2144}
2145
2146#ifdef YAFFS_COMPILE_BACKGROUND
2147
2148#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2149struct timer_struct {
2150 struct task_struct *task;
2151 struct timer_list timer;
2152};
2153#endif
2154
2155#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2156static void yaffs_background_waker(struct timer_list *t)
2157{
2158 struct timer_struct *ts = from_timer(ts, t, timer);
2159
2160 wake_up_process(ts->task);
2161}
2162#else
2163void yaffs_background_waker(unsigned long data)
2164{
2165 wake_up_process((struct task_struct *)data);
2166}
2167#endif
2168
2169static int yaffs_bg_thread_fn(void *data)
2170{
2171 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2172 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2173 unsigned long now = jiffies;
2174 unsigned long next_dir_update = now;
2175 unsigned long next_gc = now;
2176 unsigned long expires;
2177 unsigned int urgency;
2178
2179 int gc_result;
2180#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2181 struct timer_struct timer;
2182#else
2183 struct timer_list timer;
2184#endif
2185
2186 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2187 "yaffs_background starting for dev %p", (void *)dev);
2188
2189#ifdef YAFFS_COMPILE_FREEZER
2190 set_freezable();
2191#endif
2192 while (context->bg_running) {
2193 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
2194
2195 if (kthread_should_stop())
2196 break;
2197
2198#ifdef YAFFS_COMPILE_FREEZER
2199 if (try_to_freeze())
2200 continue;
2201#endif
2202 yaffs_gross_lock(dev);
2203
2204 now = jiffies;
2205
2206 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
2207 yaffs_update_dirty_dirs(dev);
2208 next_dir_update = now + HZ;
2209 }
2210
2211 if (time_after(now, next_gc) && yaffs_bg_enable) {
2212 if (!dev->is_checkpointed) {
2213 urgency = yaffs_bg_gc_urgency(dev);
2214 gc_result = yaffs_bg_gc(dev, urgency);
2215 if (urgency > 1)
2216 next_gc = now + HZ / 20 + 1;
2217 else if (urgency > 0)
2218 next_gc = now + HZ / 10 + 1;
2219 else
2220 next_gc = now + HZ * 2;
2221 } else {
2222 /*
2223 * gc not running so set to next_dir_update
2224 * to cut down on wake ups
2225 */
2226 next_gc = next_dir_update;
2227 }
2228 }
2229 yaffs_gross_unlock(dev);
2230 expires = next_dir_update;
2231 if (time_before(next_gc, expires))
2232 expires = next_gc;
2233 if (time_before(expires, now))
2234 expires = now + HZ;
2235
2236#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2237 Y_INIT_TIMER(&timer.timer, yaffs_background_waker);
2238 timer.timer.expires = expires + 1;
2239 timer.task = current;
2240#else
2241 Y_INIT_TIMER(&timer);
2242 timer.expires = expires + 1;
2243 timer.data = (unsigned long)current;
2244 timer.function = yaffs_background_waker;
2245#endif
2246
2247 set_current_state(TASK_INTERRUPTIBLE);
2248#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2249 add_timer(&timer.timer);
2250 schedule();
2251 del_timer_sync(&timer.timer);
2252#else
2253 add_timer(&timer);
2254 schedule();
2255 del_timer_sync(&timer);
2256#endif
2257 }
2258
2259 return 0;
2260}
2261
2262static int yaffs_bg_start(struct yaffs_dev *dev)
2263{
2264 int retval = 0;
2265 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2266
2267 if (dev->read_only)
2268 return -1;
2269
2270 context->bg_running = 1;
2271
2272 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2273 (void *)dev, "yaffs-bg-%d",
2274 context->mount_id);
2275
2276 if (IS_ERR(context->bg_thread)) {
2277 retval = PTR_ERR(context->bg_thread);
2278 context->bg_thread = NULL;
2279 context->bg_running = 0;
2280 }
2281 return retval;
2282}
2283
2284static void yaffs_bg_stop(struct yaffs_dev *dev)
2285{
2286 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2287
2288 ctxt->bg_running = 0;
2289
2290 if (ctxt->bg_thread) {
2291 kthread_stop(ctxt->bg_thread);
2292 ctxt->bg_thread = NULL;
2293 }
2294}
2295#else
2296static int yaffs_bg_thread_fn(void *data)
2297{
2298 return 0;
2299}
2300
2301static int yaffs_bg_start(struct yaffs_dev *dev)
2302{
2303 return 0;
2304}
2305
2306static void yaffs_bg_stop(struct yaffs_dev *dev)
2307{
2308}
2309#endif
2310
2311
2312static void yaffs_flush_inodes(struct super_block *sb)
2313{
2314 struct inode *iptr;
2315 struct yaffs_obj *obj;
2316
2317 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
2318 obj = yaffs_inode_to_obj(iptr);
2319 if (obj) {
2320 yaffs_trace(YAFFS_TRACE_OS,
2321 "flushing obj %d",
2322 obj->obj_id);
2323 yaffs_flush_file(obj, 1, 0, 0);
2324 }
2325 }
2326}
2327
2328static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2329{
2330 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2331 if (!dev)
2332 return;
2333
2334 yaffs_flush_inodes(sb);
2335 yaffs_update_dirty_dirs(dev);
2336 yaffs_flush_whole_cache(dev, 1);
2337 if (do_checkpoint)
2338 yaffs_checkpoint_save(dev);
2339}
2340
2341static LIST_HEAD(yaffs_context_list);
2342struct mutex yaffs_context_lock;
2343
2344static void yaffs_put_super(struct super_block *sb)
2345{
2346 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2347 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
2348
2349 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2350 "yaffs_put_super");
2351
2352 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2353 "Shutting down yaffs background thread");
2354 yaffs_bg_stop(dev);
2355 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2356 "yaffs background thread shut down");
2357
2358 yaffs_gross_lock(dev);
2359
2360 yaffs_flush_super(sb, 1);
2361
2362 yaffs_deinitialise(dev);
2363
2364 yaffs_gross_unlock(dev);
2365
2366 mutex_lock(&yaffs_context_lock);
2367 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2368 mutex_unlock(&yaffs_context_lock);
2369
2370 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2371 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
2372 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2373 }
2374
2375 kfree(dev);
2376
2377 yaffs_put_mtd_device(mtd);
2378
2379 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2380 "yaffs_put_super done");
2381}
2382
2383
2384static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
2385{
2386 return yaffs_gc_control;
2387}
2388
2389
2390#ifdef YAFFS_COMPILE_EXPORTFS
2391
2392static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
2393 uint32_t generation)
2394{
2395 return Y_IGET(sb, ino);
2396}
2397
2398static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
2399 struct fid *fid, int fh_len,
2400 int fh_type)
2401{
2402 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2403 yaffs2_nfs_get_inode);
2404}
2405
2406static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
2407 struct fid *fid, int fh_len,
2408 int fh_type)
2409{
2410 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2411 yaffs2_nfs_get_inode);
2412}
2413
2414struct dentry *yaffs2_get_parent(struct dentry *dentry)
2415{
2416
2417 struct super_block *sb = dentry->d_inode->i_sb;
2418 struct dentry *parent = ERR_PTR(-ENOENT);
2419 struct inode *inode;
2420 unsigned long parent_ino;
2421 struct yaffs_obj *d_obj;
2422 struct yaffs_obj *parent_obj;
2423
2424 d_obj = yaffs_inode_to_obj(dentry->d_inode);
2425
2426 if (d_obj) {
2427 parent_obj = d_obj->parent;
2428 if (parent_obj) {
2429 parent_ino = yaffs_get_obj_inode(parent_obj);
2430 inode = Y_IGET(sb, parent_ino);
2431
2432 if (IS_ERR(inode)) {
2433 parent = ERR_CAST(inode);
2434 } else {
2435 parent = d_obtain_alias(inode);
2436 if (!IS_ERR(parent)) {
2437 parent = ERR_PTR(-ENOMEM);
2438 iput(inode);
2439 }
2440 }
2441 }
2442 }
2443
2444 return parent;
2445}
2446
2447/* Just declare a zero structure as a NULL value implies
2448 * using the default functions of exportfs.
2449 */
2450
2451static struct export_operations yaffs_export_ops = {
2452 .fh_to_dentry = yaffs2_fh_to_dentry,
2453 .fh_to_parent = yaffs2_fh_to_parent,
2454 .get_parent = yaffs2_get_parent,
2455};
2456
2457#endif
2458
2459static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
2460{
2461 /* Clear the association between the inode and
2462 * the struct yaffs_obj.
2463 */
2464 obj->my_inode = NULL;
2465 yaffs_inode_to_obj_lv(inode) = NULL;
2466
2467 /* If the object freeing was deferred, then the real
2468 * free happens now.
2469 * This should fix the inode inconsistency problem.
2470 */
2471 yaffs_handle_defered_free(obj);
2472}
2473
2474#ifdef YAFFS_HAS_EVICT_INODE
2475/* yaffs_evict_inode combines into one operation what was previously done in
2476 * yaffs_clear_inode() and yaffs_delete_inode()
2477 *
2478 */
2479static void yaffs_evict_inode(struct inode *inode)
2480{
2481 struct yaffs_obj *obj;
2482 struct yaffs_dev *dev;
2483 int deleteme = 0;
2484
2485 obj = yaffs_inode_to_obj(inode);
2486
2487 yaffs_trace(YAFFS_TRACE_OS,
2488 "yaffs_evict_inode: ino %d, count %d %s",
2489 (int)inode->i_ino, atomic_read(&inode->i_count),
2490 obj ? "object exists" : "null object");
2491
2492 if (!inode->i_nlink && !is_bad_inode(inode))
2493 deleteme = 1;
2494 truncate_inode_pages(&inode->i_data, 0);
2495 Y_CLEAR_INODE(inode);
2496
2497 if (deleteme && obj) {
2498 dev = obj->my_dev;
2499 yaffs_gross_lock(dev);
2500 yaffs_del_obj(obj);
2501 yaffs_gross_unlock(dev);
2502 }
2503 if (obj) {
2504 dev = obj->my_dev;
2505 yaffs_gross_lock(dev);
2506 yaffs_unstitch_obj(inode, obj);
2507 yaffs_gross_unlock(dev);
2508 }
2509}
2510#else
2511
2512/* clear is called to tell the fs to release any per-inode data it holds.
2513 * The object might still exist on disk and is just being thrown out of the cache
2514 * or else the object has actually been deleted and we're being called via
2515 * the chain
2516 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
2517 */
2518
2519static void yaffs_clear_inode(struct inode *inode)
2520{
2521 struct yaffs_obj *obj;
2522 struct yaffs_dev *dev;
2523
2524 obj = yaffs_inode_to_obj(inode);
2525
2526 yaffs_trace(YAFFS_TRACE_OS,
2527 "yaffs_clear_inode: ino %d, count %d %s",
2528 (int)inode->i_ino, atomic_read(&inode->i_count),
2529 obj ? "object exists" : "null object");
2530
2531 if (obj) {
2532 dev = obj->my_dev;
2533 yaffs_gross_lock(dev);
2534 yaffs_unstitch_obj(inode, obj);
2535 yaffs_gross_unlock(dev);
2536 }
2537
2538}
2539
2540/* delete is called when the link count is zero and the inode
2541 * is put (ie. nobody wants to know about it anymore, time to
2542 * delete the file).
2543 * NB Must call clear_inode()
2544 */
2545static void yaffs_delete_inode(struct inode *inode)
2546{
2547 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2548 struct yaffs_dev *dev;
2549
2550 yaffs_trace(YAFFS_TRACE_OS,
2551 "yaffs_delete_inode: ino %d, count %d %s",
2552 (int)inode->i_ino, atomic_read(&inode->i_count),
2553 obj ? "object exists" : "null object");
2554
2555 if (obj) {
2556 dev = obj->my_dev;
2557 yaffs_gross_lock(dev);
2558 yaffs_del_obj(obj);
2559 yaffs_gross_unlock(dev);
2560 }
2561#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
2562 truncate_inode_pages(&inode->i_data, 0);
2563#endif
2564 clear_inode(inode);
2565}
2566#endif
2567
2568
2569
2570
2571#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2572static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2573{
2574 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2575 struct super_block *sb = dentry->d_sb;
2576#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2577static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2578{
2579 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2580#else
2581static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2582{
2583 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2584#endif
2585
2586 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
2587
2588 yaffs_gross_lock(dev);
2589
2590 buf->f_type = YAFFS_MAGIC;
2591 buf->f_bsize = sb->s_blocksize;
2592 buf->f_namelen = 255;
2593
2594 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2595 /* Do this if chunk size is not a power of 2 */
2596
2597 uint64_t bytes_in_dev;
2598 uint64_t bytes_free;
2599
2600 bytes_in_dev =
2601 ((uint64_t)
2602 ((dev->param.end_block - dev->param.start_block +
2603 1))) * ((uint64_t) (dev->param.chunks_per_block *
2604 dev->data_bytes_per_chunk));
2605
2606 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2607 buf->f_blocks = bytes_in_dev;
2608
2609 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
2610 ((uint64_t) (dev->data_bytes_per_chunk));
2611
2612 do_div(bytes_free, sb->s_blocksize);
2613
2614 buf->f_bfree = bytes_free;
2615
2616 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2617
2618 buf->f_blocks =
2619 (dev->param.end_block - dev->param.start_block + 1) *
2620 dev->param.chunks_per_block /
2621 (sb->s_blocksize / dev->data_bytes_per_chunk);
2622 buf->f_bfree =
2623 yaffs_get_n_free_chunks(dev) /
2624 (sb->s_blocksize / dev->data_bytes_per_chunk);
2625 } else {
2626 buf->f_blocks =
2627 (dev->param.end_block - dev->param.start_block + 1) *
2628 dev->param.chunks_per_block *
2629 (dev->data_bytes_per_chunk / sb->s_blocksize);
2630
2631 buf->f_bfree =
2632 yaffs_get_n_free_chunks(dev) *
2633 (dev->data_bytes_per_chunk / sb->s_blocksize);
2634 }
2635
2636 buf->f_files = 0;
2637 buf->f_ffree = 0;
2638 buf->f_bavail = buf->f_bfree;
2639
2640 yaffs_gross_unlock(dev);
2641 return 0;
2642}
2643
2644
2645
2646static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
2647{
2648
2649 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2650 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2651 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2652 int do_checkpoint;
2653 int dirty = yaffs_check_super_dirty(dev);
2654
2655 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2656 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
2657 gc_urgent,
2658 dirty ? "dirty" : "clean",
2659 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2660 oneshot_checkpoint ? " one-shot" : "");
2661
2662 yaffs_gross_lock(dev);
2663 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2664 oneshot_checkpoint) && !dev->is_checkpointed;
2665
2666 if (dirty || do_checkpoint) {
2667 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2668 yaffs_clear_super_dirty(dev);
2669 if (oneshot_checkpoint)
2670 yaffs_auto_checkpoint &= ~4;
2671 }
2672 yaffs_gross_unlock(dev);
2673
2674 return 0;
2675}
2676
2677
2678#ifdef YAFFS_HAS_WRITE_SUPER
2679#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2680static void yaffs_write_super(struct super_block *sb)
2681#else
2682static int yaffs_write_super(struct super_block *sb)
2683#endif
2684{
2685 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2686
2687 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2688 "yaffs_write_super %s",
2689 request_checkpoint ? " checkpt" : "");
2690
2691 yaffs_do_sync_fs(sb, request_checkpoint);
2692
2693#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2694 return 0;
2695#endif
2696}
2697#endif
2698
2699#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2700static int yaffs_sync_fs(struct super_block *sb, int wait)
2701#else
2702static int yaffs_sync_fs(struct super_block *sb)
2703#endif
2704{
2705 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2706
2707 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2708 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
2709
2710 yaffs_do_sync_fs(sb, request_checkpoint);
2711
2712 return 0;
2713}
2714
2715/* the function only is used to change dev->read_only when this file system
2716 * is remounted.
2717 */
2718static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2719{
2720 int read_only = 0;
2721 struct mtd_info *mtd;
2722 struct yaffs_dev *dev = 0;
2723
2724 /* Get the device */
2725 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2726 if (!mtd) {
2727 yaffs_trace(YAFFS_TRACE_ALWAYS,
2728 "MTD device #%u doesn't appear to exist",
2729 MINOR(sb->s_dev));
2730 return 1;
2731 }
2732
2733 /* Check it's NAND */
2734 if (mtd->type != MTD_NANDFLASH) {
2735 yaffs_trace(YAFFS_TRACE_ALWAYS,
2736 "MTD device is not NAND it's type %d",
2737 mtd->type);
2738 return 1;
2739 }
2740
2741 read_only = ((*flags & MS_RDONLY) != 0);
2742 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2743 read_only = 1;
2744 printk(KERN_INFO
2745 "yaffs: mtd is read only, setting superblock read only");
2746 *flags |= MS_RDONLY;
2747 }
2748
2749 dev = sb->s_fs_info;
2750 dev->read_only = read_only;
2751
2752 return 0;
2753}
2754
2755static const struct super_operations yaffs_super_ops = {
2756 .statfs = yaffs_statfs,
2757
2758#ifndef YAFFS_USE_OWN_IGET
2759 .read_inode = yaffs_read_inode,
2760#endif
2761#ifdef YAFFS_HAS_PUT_INODE
2762 .put_inode = yaffs_put_inode,
2763#endif
2764 .put_super = yaffs_put_super,
2765#ifdef YAFFS_HAS_EVICT_INODE
2766 .evict_inode = yaffs_evict_inode,
2767#else
2768 .delete_inode = yaffs_delete_inode,
2769 .clear_inode = yaffs_clear_inode,
2770#endif
2771 .sync_fs = yaffs_sync_fs,
2772#ifdef YAFFS_HAS_WRITE_SUPER
2773 .write_super = yaffs_write_super,
2774#endif
2775 .remount_fs = yaffs_remount_fs,
2776};
2777
2778struct yaffs_options {
2779 int inband_tags;
2780 int skip_checkpoint_read;
2781 int skip_checkpoint_write;
2782 int no_cache;
2783 int tags_ecc_on;
2784 int tags_ecc_overridden;
2785 int lazy_loading_enabled;
2786 int lazy_loading_overridden;
2787 int empty_lost_and_found;
2788 int empty_lost_and_found_overridden;
2789 int disable_summary;
2790};
2791
2792#define MAX_OPT_LEN 30
2793static int yaffs_parse_options(struct yaffs_options *options,
2794 const char *options_str)
2795{
2796 char cur_opt[MAX_OPT_LEN + 1];
2797 int p;
2798 int error = 0;
2799
2800 /* Parse through the options which is a comma seperated list */
2801
2802 while (options_str && *options_str && !error) {
2803 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2804 p = 0;
2805
2806 while (*options_str == ',')
2807 options_str++;
2808
2809 while (*options_str && *options_str != ',') {
2810 if (p < MAX_OPT_LEN) {
2811 cur_opt[p] = *options_str;
2812 p++;
2813 }
2814 options_str++;
2815 }
2816
2817 if (!strcmp(cur_opt, "inband-tags")) {
2818 options->inband_tags = 1;
2819 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2820 options->tags_ecc_on = 0;
2821 options->tags_ecc_overridden = 1;
2822 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2823 options->tags_ecc_on = 1;
2824 options->tags_ecc_overridden = 1;
2825 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2826 options->lazy_loading_enabled = 0;
2827 options->lazy_loading_overridden = 1;
2828 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2829 options->lazy_loading_enabled = 1;
2830 options->lazy_loading_overridden = 1;
2831 } else if (!strcmp(cur_opt, "disable-summary")) {
2832 options->disable_summary = 1;
2833 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2834 options->empty_lost_and_found = 0;
2835 options->empty_lost_and_found_overridden = 1;
2836 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2837 options->empty_lost_and_found = 1;
2838 options->empty_lost_and_found_overridden = 1;
2839 } else if (!strcmp(cur_opt, "no-cache")) {
2840 options->no_cache = 1;
2841 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2842 options->skip_checkpoint_read = 1;
2843 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2844 options->skip_checkpoint_write = 1;
2845 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2846 options->skip_checkpoint_read = 1;
2847 options->skip_checkpoint_write = 1;
2848 } else {
2849 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2850 cur_opt);
2851 error = 1;
2852 }
2853 }
2854
2855 return error;
2856}
2857
2858
2859static struct dentry *yaffs_make_root(struct inode *inode)
2860{
2861#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
2862 struct dentry *root = d_alloc_root(inode);
2863
2864 if (!root)
2865 iput(inode);
2866
2867 return root;
2868#else
2869 return d_make_root(inode);
2870#endif
2871}
2872
2873
2874
2875
2876static struct super_block *yaffs_internal_read_super(int yaffs_version,
2877 struct super_block *sb,
2878 void *data, int silent)
2879{
2880 int n_blocks;
2881 struct inode *inode = NULL;
2882 struct dentry *root;
2883 struct yaffs_dev *dev = 0;
2884 char devname_buf[BDEVNAME_SIZE + 1];
2885 struct mtd_info *mtd;
2886 int err;
2887 char *data_str = (char *)data;
2888 struct yaffs_linux_context *context = NULL;
2889 struct yaffs_param *param;
2890
2891 int read_only = 0;
2892 int inband_tags = 0;
2893
2894 struct yaffs_options options;
2895
2896 unsigned mount_id;
2897 int found;
2898 struct yaffs_linux_context *context_iterator;
2899 struct list_head *l;
2900
2901 if (!sb) {
2902 printk(KERN_INFO "yaffs: sb is NULL\n");
2903 return NULL;
2904 }
2905
2906 sb->s_magic = YAFFS_MAGIC;
2907 sb->s_op = &yaffs_super_ops;
2908 sb->s_flags |= MS_NOATIME;
2909
2910 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2911
2912#ifdef YAFFS_COMPILE_EXPORTFS
2913 sb->s_export_op = &yaffs_export_ops;
2914#endif
2915
2916 if (!sb->s_dev)
2917 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2918 else if (!yaffs_devname(sb, devname_buf))
2919 printk(KERN_INFO "yaffs: devname is NULL\n");
2920 else
2921 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2922 sb->s_dev,
2923 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2924
2925 if (!data_str)
2926 data_str = "";
2927
2928 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2929
2930 memset(&options, 0, sizeof(options));
2931
2932 if (yaffs_parse_options(&options, data_str)) {
2933 /* Option parsing failed */
2934 return NULL;
2935 }
2936
2937 sb->s_blocksize = PAGE_CACHE_SIZE;
2938 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2939
2940 yaffs_trace(YAFFS_TRACE_OS,
2941 "yaffs_read_super: Using yaffs%d", yaffs_version);
2942 yaffs_trace(YAFFS_TRACE_OS,
2943 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2944
2945 yaffs_trace(YAFFS_TRACE_ALWAYS,
2946 "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
2947 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2948 yaffs_devname(sb, devname_buf));
2949
2950 /* Get the device */
2951 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2952 if (IS_ERR(mtd)) {
2953 yaffs_trace(YAFFS_TRACE_ALWAYS,
2954 "yaffs: MTD device %u either not valid or unavailable",
2955 MINOR(sb->s_dev));
2956 return NULL;
2957 }
2958
2959 if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2960 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2961 yaffs_version = 2;
2962 }
2963
2964 /* Added NCB 26/5/2006 for completeness */
2965 if (yaffs_version == 2 && !options.inband_tags
2966 && WRITE_SIZE(mtd) == 512) {
2967 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2968 yaffs_version = 1;
2969 }
2970
2971 if (mtd->oobavail < sizeof(struct yaffs_packed_tags2) ||
2972 options.inband_tags)
2973 inband_tags = 1;
2974
2975 if(yaffs_verify_mtd(mtd, yaffs_version, inband_tags) < 0)
2976 return NULL;
2977
2978 /* OK, so if we got here, we have an MTD that's NAND and looks
2979 * like it has the right capabilities
2980 * Set the struct yaffs_dev up for mtd
2981 */
2982
2983 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2984 read_only = 1;
2985 printk(KERN_INFO
2986 "yaffs: mtd is read only, setting superblock read only\n"
2987 );
2988 sb->s_flags |= MS_RDONLY;
2989 }
2990
2991 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2992 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2993
2994 if (!dev || !context) {
2995 kfree(dev);
2996 kfree(context);
2997 dev = NULL;
2998 context = NULL;
2999
3000 /* Deep shit could not allocate device structure */
3001 yaffs_trace(YAFFS_TRACE_ALWAYS,
3002 "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
3003 );
3004 return NULL;
3005 }
3006 memset(dev, 0, sizeof(struct yaffs_dev));
3007 param = &(dev->param);
3008
3009 memset(context, 0, sizeof(struct yaffs_linux_context));
3010 dev->os_context = context;
3011 INIT_LIST_HEAD(&(context->context_list));
3012 context->dev = dev;
3013 context->super = sb;
3014
3015 dev->read_only = read_only;
3016
3017#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3018 sb->s_fs_info = dev;
3019#else
3020 sb->u.generic_sbp = dev;
3021#endif
3022
3023
3024 dev->driver_context = mtd;
3025 param->name = mtd->name;
3026
3027 /* Set up the memory size parameters.... */
3028
3029
3030 param->n_reserved_blocks = 5;
3031 param->n_caches = (options.no_cache) ? 0 : 10;
3032 param->inband_tags = inband_tags;
3033
3034 param->enable_xattr = 1;
3035 if (options.lazy_loading_overridden)
3036 param->disable_lazy_load = !options.lazy_loading_enabled;
3037
3038 param->defered_dir_update = 1;
3039
3040 if (options.tags_ecc_overridden)
3041 param->no_tags_ecc = !options.tags_ecc_on;
3042
3043 param->empty_lost_n_found = 1;
3044 param->refresh_period = 0;
3045 param->disable_summary = options.disable_summary;
3046
3047
3048#ifdef CONFIG_YAFFS_DISABLE_BAD_BLOCK_MARKING
3049 param->disable_bad_block_marking = 1;
3050#endif
3051 if (options.empty_lost_and_found_overridden)
3052 param->empty_lost_n_found = options.empty_lost_and_found;
3053
3054 /* ... and the functions. */
3055 if (yaffs_version == 2) {
3056 param->is_yaffs2 = 1;
3057#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3058 param->total_bytes_per_chunk = mtd->writesize;
3059 param->chunks_per_block = mtd->erasesize / mtd->writesize;
3060#else
3061 param->total_bytes_per_chunk = mtd->oobblock;
3062 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
3063#endif
3064 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
3065
3066 param->start_block = 0;
3067 param->end_block = n_blocks - 1;
3068 } else {
3069 param->is_yaffs2 = 0;
3070 n_blocks = YCALCBLOCKS(mtd->size,
3071 YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
3072
3073 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
3074 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
3075 }
3076
3077 param->start_block = 0;
3078 param->end_block = n_blocks - 1;
3079
3080 yaffs_mtd_drv_install(dev);
3081
3082 param->sb_dirty_fn = yaffs_set_super_dirty;
3083 param->gc_control_fn = yaffs_gc_control_callback;
3084
3085 yaffs_dev_to_lc(dev)->super = sb;
3086
3087 param->use_nand_ecc = 1;
3088
3089 param->skip_checkpt_rd = options.skip_checkpoint_read;
3090 param->skip_checkpt_wr = options.skip_checkpoint_write;
3091
3092 mutex_lock(&yaffs_context_lock);
3093 /* Get a mount id */
3094 found = 0;
3095 for (mount_id = 0; !found; mount_id++) {
3096 found = 1;
3097 list_for_each(l, &yaffs_context_list) {
3098 context_iterator =
3099 list_entry(l, struct yaffs_linux_context,
3100 context_list);
3101 if (context_iterator->mount_id == mount_id)
3102 found = 0;
3103 }
3104 }
3105 context->mount_id = mount_id;
3106
3107 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
3108 &yaffs_context_list);
3109 mutex_unlock(&yaffs_context_lock);
3110
3111 /* Directory search handling... */
3112 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
3113 param->remove_obj_fn = yaffs_remove_obj_callback;
3114
3115 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
3116
3117 yaffs_gross_lock(dev);
3118
3119 err = yaffs_guts_initialise(dev);
3120
3121 yaffs_trace(YAFFS_TRACE_OS,
3122 "yaffs_read_super: guts initialised %s",
3123 (err == YAFFS_OK) ? "OK" : "FAILED");
3124
3125 if (err == YAFFS_OK)
3126 yaffs_bg_start(dev);
3127
3128 if (!context->bg_thread)
3129 param->defered_dir_update = 0;
3130
3131 sb->s_maxbytes = yaffs_max_file_size(dev);
3132
3133 /* Release lock before yaffs_get_inode() */
3134 yaffs_gross_unlock(dev);
3135
3136 /* Create root inode */
3137 if (err == YAFFS_OK)
3138 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
3139
3140 if (!inode)
3141 return NULL;
3142
3143 inode->i_op = &yaffs_dir_inode_operations;
3144 inode->i_fop = &yaffs_dir_operations;
3145
3146 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
3147
3148 root = yaffs_make_root(inode);
3149
3150 if (!root)
3151 return NULL;
3152
3153 sb->s_root = root;
3154 if(!dev->is_checkpointed)
3155 yaffs_set_super_dirty(dev);
3156
3157 yaffs_trace(YAFFS_TRACE_ALWAYS,
3158 "yaffs_read_super: is_checkpointed %d",
3159 dev->is_checkpointed);
3160
3161 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
3162 return sb;
3163}
3164
3165#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3166static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3167 int silent)
3168{
3169 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3170}
3171
3172#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3173static struct dentry *yaffs_mount(struct file_system_type *fs_type, int flags,
3174 const char *dev_name, void *data)
3175{
3176 return mount_bdev(fs_type, flags, dev_name, data, yaffs_internal_read_super_mtd);
3177}
3178#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3179static int yaffs_read_super(struct file_system_type *fs,
3180 int flags, const char *dev_name,
3181 void *data, struct vfsmount *mnt)
3182{
3183
3184 return get_sb_bdev(fs, flags, dev_name, data,
3185 yaffs_internal_read_super_mtd, mnt);
3186}
3187#else
3188static struct super_block *yaffs_read_super(struct file_system_type *fs,
3189 int flags, const char *dev_name,
3190 void *data)
3191{
3192
3193 return get_sb_bdev(fs, flags, dev_name, data,
3194 yaffs_internal_read_super_mtd);
3195}
3196#endif
3197
3198static struct file_system_type yaffs_fs_type = {
3199 .owner = THIS_MODULE,
3200 .name = "yaffs",
3201#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3202 .mount = yaffs_mount,
3203#else
3204 .get_sb = yaffs_read_super,
3205#endif
3206 .kill_sb = kill_block_super,
3207 .fs_flags = FS_REQUIRES_DEV,
3208};
3209#else
3210static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3211 int silent)
3212{
3213 return yaffs_internal_read_super(1, sb, data, silent);
3214}
3215
3216static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3217 FS_REQUIRES_DEV);
3218#endif
3219
3220
3221#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3222static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3223 int silent)
3224{
3225 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3226}
3227
3228#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3229static struct dentry *yaffs2_mount(struct file_system_type *fs_type, int flags,
3230 const char *dev_name, void *data)
3231{
3232 return mount_bdev(fs_type, flags, dev_name, data, yaffs2_internal_read_super_mtd);
3233}
3234#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3235static int yaffs2_read_super(struct file_system_type *fs,
3236 int flags, const char *dev_name, void *data,
3237 struct vfsmount *mnt)
3238{
3239 return get_sb_bdev(fs, flags, dev_name, data,
3240 yaffs2_internal_read_super_mtd, mnt);
3241}
3242#else
3243static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3244 int flags, const char *dev_name,
3245 void *data)
3246{
3247
3248 return get_sb_bdev(fs, flags, dev_name, data,
3249 yaffs2_internal_read_super_mtd);
3250}
3251#endif
3252
3253static struct file_system_type yaffs2_fs_type = {
3254 .owner = THIS_MODULE,
3255 .name = "yaffs2",
3256#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3257 .mount = yaffs2_mount,
3258#else
3259 .get_sb = yaffs2_read_super,
3260#endif
3261 .kill_sb = kill_block_super,
3262 .fs_flags = FS_REQUIRES_DEV,
3263};
3264#else
3265static struct super_block *yaffs2_read_super(struct super_block *sb,
3266 void *data, int silent)
3267{
3268 return yaffs_internal_read_super(2, sb, data, silent);
3269}
3270
3271static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3272 FS_REQUIRES_DEV);
3273#endif
3274
3275
3276static struct proc_dir_entry *my_proc_entry;
3277
3278static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3279{
3280 struct yaffs_param *param = &dev->param;
3281 int bs[10];
3282
3283 yaffs_count_blocks_by_state(dev,bs);
3284
3285 buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
3286 buf += sprintf(buf, "end_block............ %d\n", param->end_block);
3287 buf += sprintf(buf, "total_bytes_per_chunk %d\n",
3288 param->total_bytes_per_chunk);
3289 buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
3290 buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
3291 buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
3292 buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
3293 buf += sprintf(buf, "empty_lost_n_found... %d\n",
3294 param->empty_lost_n_found);
3295 buf += sprintf(buf, "disable_lazy_load.... %d\n",
3296 param->disable_lazy_load);
3297 buf += sprintf(buf, "disable_bad_block_mrk %d\n",
3298 param->disable_bad_block_marking);
3299 buf += sprintf(buf, "refresh_period....... %d\n",
3300 param->refresh_period);
3301 buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
3302 buf += sprintf(buf, "n_reserved_blocks.... %d\n",
3303 param->n_reserved_blocks);
3304 buf += sprintf(buf, "always_check_erased.. %d\n",
3305 param->always_check_erased);
3306 buf += sprintf(buf, "\n");
3307 buf += sprintf(buf, "block count by state\n");
3308 buf += sprintf(buf, "0:%d 1:%d 2:%d 3:%d 4:%d\n",
3309 bs[0], bs[1], bs[2], bs[3], bs[4]);
3310 buf += sprintf(buf, "5:%d 6:%d 7:%d 8:%d 9:%d\n",
3311 bs[5], bs[6], bs[7], bs[8], bs[9]);
3312
3313 return buf;
3314}
3315
3316static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3317{
3318 buf += sprintf(buf, "max file size....... %lld\n",
3319 (long long) yaffs_max_file_size(dev));
3320 buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
3321 dev->data_bytes_per_chunk);
3322 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3323 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3324 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3325 buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
3326 dev->blocks_in_checkpt);
3327 buf += sprintf(buf, "\n");
3328 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3329 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3330 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3331 buf += sprintf(buf, "\n");
3332 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3333 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3334 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3335 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3336 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3337 buf += sprintf(buf, "passive_gc_count..... %u\n",
3338 dev->passive_gc_count);
3339 buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
3340 dev->oldest_dirty_gc_count);
3341 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3342 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3343 buf += sprintf(buf, "n_retried_writes..... %u\n",
3344 dev->n_retried_writes);
3345 buf += sprintf(buf, "n_retired_blocks..... %u\n",
3346 dev->n_retired_blocks);
3347 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3348 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3349 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
3350 dev->n_tags_ecc_fixed);
3351 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
3352 dev->n_tags_ecc_unfixed);
3353 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3354 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3355 buf += sprintf(buf, "n_unlinked_files..... %u\n",
3356 dev->n_unlinked_files);
3357 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3358 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3359 buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
3360 buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
3361
3362 return buf;
3363}
3364
3365static int yaffs_proc_read(char *page,
3366 char **start,
3367 off_t offset, int count, int *eof, void *data)
3368{
3369 struct list_head *item;
3370 char *buf = page;
3371 int step = offset;
3372 int n = 0;
3373
3374 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3375 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3376 * This also assumes the user has posted a read buffer large
3377 * enough to hold the complete output; but that's life in /proc.
3378 */
3379
3380 *(int *)start = 1;
3381
3382 /* Print header first */
3383 if (step == 0)
3384 buf +=
3385 sprintf(buf, "Multi-version YAFFS\n");
3386 else if (step == 1)
3387 buf += sprintf(buf, "\n");
3388 else {
3389 step -= 2;
3390
3391 mutex_lock(&yaffs_context_lock);
3392
3393 /* Locate and print the Nth entry. Order N-squared but N is small. */
3394 list_for_each(item, &yaffs_context_list) {
3395 struct yaffs_linux_context *dc =
3396 list_entry(item, struct yaffs_linux_context,
3397 context_list);
3398 struct yaffs_dev *dev = dc->dev;
3399
3400 if (n < (step & ~1)) {
3401 n += 2;
3402 continue;
3403 }
3404 if ((step & 1) == 0) {
3405 buf +=
3406 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3407 dev->param.name);
3408 buf = yaffs_dump_dev_part0(buf, dev);
3409 } else {
3410 buf = yaffs_dump_dev_part1(buf, dev);
3411 }
3412
3413 break;
3414 }
3415 mutex_unlock(&yaffs_context_lock);
3416 }
3417
3418 return buf - page < count ? buf - page : count;
3419}
3420
3421/**
3422 * Set the verbosity of the warnings and error messages.
3423 *
3424 * Note that the names can only be a..z or _ with the current code.
3425 */
3426
3427static struct {
3428 char *mask_name;
3429 unsigned mask_bitfield;
3430} mask_flags[] = {
3431 {"allocate", YAFFS_TRACE_ALLOCATE},
3432 {"always", YAFFS_TRACE_ALWAYS},
3433 {"background", YAFFS_TRACE_BACKGROUND},
3434 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3435 {"buffers", YAFFS_TRACE_BUFFERS},
3436 {"bug", YAFFS_TRACE_BUG},
3437 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3438 {"deletion", YAFFS_TRACE_DELETION},
3439 {"erase", YAFFS_TRACE_ERASE},
3440 {"error", YAFFS_TRACE_ERROR},
3441 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3442 {"gc", YAFFS_TRACE_GC},
3443 {"lock", YAFFS_TRACE_LOCK},
3444 {"mtd", YAFFS_TRACE_MTD},
3445 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3446 {"os", YAFFS_TRACE_OS},
3447 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3448 {"scan", YAFFS_TRACE_SCAN},
3449 {"mount", YAFFS_TRACE_MOUNT},
3450 {"tracing", YAFFS_TRACE_TRACING},
3451 {"sync", YAFFS_TRACE_SYNC},
3452 {"write", YAFFS_TRACE_WRITE},
3453 {"verify", YAFFS_TRACE_VERIFY},
3454 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3455 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3456 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3457 {"all", 0xffffffff},
3458 {"none", 0},
3459 {NULL, 0},
3460};
3461
3462#define MAX_MASK_NAME_LENGTH 40
3463static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3464 unsigned long count)
3465{
3466 unsigned rg = 0, mask_bitfield;
3467 char *end;
3468 char *mask_name;
3469 const char *x;
3470 char substring[MAX_MASK_NAME_LENGTH + 1];
3471 int i;
3472 int done = 0;
3473 int add, len = 0;
3474 int pos = 0;
3475
3476 rg = yaffs_trace_mask;
3477
3478 while (!done && (pos < count)) {
3479 done = 1;
3480 while ((pos < count) && isspace(buf[pos]))
3481 pos++;
3482
3483 switch (buf[pos]) {
3484 case '+':
3485 case '-':
3486 case '=':
3487 add = buf[pos];
3488 pos++;
3489 break;
3490
3491 default:
3492 add = ' ';
3493 break;
3494 }
3495 mask_name = NULL;
3496
3497 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3498
3499 if (end > buf + pos) {
3500 mask_name = "numeral";
3501 len = end - (buf + pos);
3502 pos += len;
3503 done = 0;
3504 } else {
3505 for (x = buf + pos, i = 0;
3506 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3507 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3508 substring[i] = *x;
3509 substring[i] = '\0';
3510
3511 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3512 if (strcmp(substring, mask_flags[i].mask_name)
3513 == 0) {
3514 mask_name = mask_flags[i].mask_name;
3515 mask_bitfield =
3516 mask_flags[i].mask_bitfield;
3517 done = 0;
3518 break;
3519 }
3520 }
3521 }
3522
3523 if (mask_name != NULL) {
3524 done = 0;
3525 switch (add) {
3526 case '-':
3527 rg &= ~mask_bitfield;
3528 break;
3529 case '+':
3530 rg |= mask_bitfield;
3531 break;
3532 case '=':
3533 rg = mask_bitfield;
3534 break;
3535 default:
3536 rg |= mask_bitfield;
3537 break;
3538 }
3539 }
3540 }
3541
3542 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3543
3544 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3545
3546 if (rg & YAFFS_TRACE_ALWAYS) {
3547 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3548 char flag;
3549 flag = ((rg & mask_flags[i].mask_bitfield) ==
3550 mask_flags[i].mask_bitfield) ? '+' : '-';
3551 printk(KERN_DEBUG "%c%s\n", flag,
3552 mask_flags[i].mask_name);
3553 }
3554 }
3555
3556 return count;
3557}
3558
3559/* Debug strings are of the form:
3560 * .bnnn print info on block n
3561 * .cobjn,chunkn print nand chunk id for objn:chunkn
3562 */
3563
3564static int yaffs_proc_debug_write(struct file *file, const char *buf,
3565 unsigned long count)
3566{
3567
3568 char str[100];
3569 char *p0;
3570 char *p1;
3571 long p1_val;
3572 long p0_val;
3573 char cmd;
3574 struct list_head *item;
3575
3576 memset(str, 0, sizeof(str));
3577 memcpy(str, buf, min((size_t)count, sizeof(str) -1));
3578
3579 cmd = str[1];
3580
3581 p0 = str + 2;
3582
3583 p1 = p0;
3584
3585 while (*p1 && *p1 != ',') {
3586 p1++;
3587 }
3588 *p1 = '\0';
3589 p1++;
3590
3591 p0_val = simple_strtol(p0, NULL, 0);
3592 p1_val = simple_strtol(p1, NULL, 0);
3593
3594
3595 mutex_lock(&yaffs_context_lock);
3596
3597 /* Locate and print the Nth entry. Order N-squared but N is small. */
3598 list_for_each(item, &yaffs_context_list) {
3599 struct yaffs_linux_context *dc =
3600 list_entry(item, struct yaffs_linux_context,
3601 context_list);
3602 struct yaffs_dev *dev = dc->dev;
3603
3604 if (cmd == 'b') {
3605 struct yaffs_block_info *bi;
3606
3607 bi = yaffs_get_block_info(dev,p0_val);
3608
3609 if(bi) {
3610 printk("Block %d: state %d, retire %d, use %d, seq %d\n",
3611 (int)p0_val, bi->block_state,
3612 bi->needs_retiring, bi->pages_in_use,
3613 bi->seq_number);
3614 }
3615 } else if (cmd == 'c') {
3616 struct yaffs_obj *obj;
3617 int nand_chunk;
3618
3619 obj = yaffs_find_by_number(dev, p0_val);
3620 if (!obj)
3621 printk("No obj %d\n", (int)p0_val);
3622 else {
3623 if(p1_val == 0)
3624 nand_chunk = obj->hdr_chunk;
3625 else
3626 nand_chunk =
3627 yaffs_find_chunk_in_file(obj,
3628 p1_val, NULL);
3629 printk("Nand chunk for %d:%d is %d\n",
3630 (int)p0_val, (int)p1_val, nand_chunk);
3631 }
3632 }
3633 }
3634
3635 mutex_unlock(&yaffs_context_lock);
3636
3637 return count;
3638}
3639
3640
3641#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0))
3642static int yaffs_proc_write(struct file *file, const char *buf,
3643 unsigned long count, void *ppos)
3644#else
3645static ssize_t yaffs_proc_write(struct file *file, const char __user *buf,
3646 size_t count, loff_t *ppos)
3647#endif
3648{
3649 if (buf[0] == '.')
3650 return yaffs_proc_debug_write(file, buf, count);
3651 return yaffs_proc_write_trace_options(file, buf, count);
3652}
3653
3654/* Stuff to handle installation of file systems */
3655struct file_system_to_install {
3656 struct file_system_type *fst;
3657 int installed;
3658};
3659
3660static struct file_system_to_install fs_to_install[] = {
3661 {&yaffs_fs_type, 0},
3662 {&yaffs2_fs_type, 0},
3663 {NULL, 0}
3664};
3665
3666
3667#ifdef YAFFS_NEW_PROCFS
3668static int yaffs_proc_show(struct seq_file *m, void *v)
3669{
3670 /* FIXME: Unify in a better way? */
3671 char buffer[512];
3672 char *start;
3673 int len;
3674
3675 len = yaffs_proc_read(buffer, &start, 0, sizeof(buffer), NULL, NULL);
3676 seq_puts(m, buffer);
3677 return 0;
3678}
3679
3680static int yaffs_proc_open(struct inode *inode, struct file *file)
3681{
3682 return single_open(file, yaffs_proc_show, NULL);
3683}
3684
3685static struct file_operations procfs_ops = {
3686 .owner = THIS_MODULE,
3687 .open = yaffs_proc_open,
3688 .read = seq_read,
3689 .write = yaffs_proc_write,
3690 .release = single_release,
3691};
3692
3693static int yaffs_procfs_init(void)
3694{
3695 /* Install the proc_fs entries */
3696 my_proc_entry = proc_create("yaffs",
3697 S_IRUGO | S_IFREG,
3698 YPROC_ROOT,
3699 &procfs_ops);
3700
3701 if (my_proc_entry) {
3702 return 0;
3703 } else {
3704 return -ENOMEM;
3705 }
3706}
3707
3708#else
3709
3710
3711static int yaffs_procfs_init(void)
3712{
3713 /* Install the proc_fs entries */
3714 my_proc_entry = create_proc_entry("yaffs",
3715 S_IRUGO | S_IFREG, YPROC_ROOT);
3716
3717 if (my_proc_entry) {
3718 my_proc_entry->write_proc = yaffs_proc_write;
3719 my_proc_entry->read_proc = yaffs_proc_read;
3720 my_proc_entry->data = NULL;
3721 return 0;
3722 } else {
3723 return -ENOMEM;
3724 }
3725}
3726
3727#endif
3728
3729
3730static int __init init_yaffs_fs(void)
3731{
3732 int error = 0;
3733 struct file_system_to_install *fsinst;
3734
3735 yaffs_trace(YAFFS_TRACE_ALWAYS,
3736 "yaffs Installing.");
3737
3738 mutex_init(&yaffs_context_lock);
3739
3740 error = yaffs_procfs_init();
3741 if (error)
3742 return error;
3743
3744 /* Now add the file system entries */
3745
3746 fsinst = fs_to_install;
3747
3748 while (fsinst->fst && !error) {
3749 error = register_filesystem(fsinst->fst);
3750 if (!error)
3751 fsinst->installed = 1;
3752 fsinst++;
3753 }
3754
3755 /* Any errors? uninstall */
3756 if (error) {
3757 fsinst = fs_to_install;
3758
3759 while (fsinst->fst) {
3760 if (fsinst->installed) {
3761 unregister_filesystem(fsinst->fst);
3762 fsinst->installed = 0;
3763 }
3764 fsinst++;
3765 }
3766 }
3767
3768 return error;
3769}
3770
3771static void __exit exit_yaffs_fs(void)
3772{
3773
3774 struct file_system_to_install *fsinst;
3775
3776 yaffs_trace(YAFFS_TRACE_ALWAYS,
3777 "yaffs removing.");
3778
3779 remove_proc_entry("yaffs", YPROC_ROOT);
3780
3781 fsinst = fs_to_install;
3782
3783 while (fsinst->fst) {
3784 if (fsinst->installed) {
3785 unregister_filesystem(fsinst->fst);
3786 fsinst->installed = 0;
3787 }
3788 fsinst++;
3789 }
3790}
3791
3792module_init(init_yaffs_fs)
3793module_exit(exit_yaffs_fs)
3794
3795MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3796MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
3797MODULE_LICENSE("GPL");