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