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