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