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