-
Notifications
You must be signed in to change notification settings - Fork 11
/
yaffs_vfs_multi.c
3443 lines (2829 loc) · 85.9 KB
/
yaffs_vfs_multi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
*
* Copyright (C) 2002-2011 Aleph One Ltd.
* for Toby Churchill Ltd and Brightstar Engineering
*
* Created by Charles Manning <[email protected]>
* Acknowledgements:
* Luc van OostenRyck for numerous patches.
* Nick Bane for numerous patches.
* Nick Bane for 2.5/2.6 integration.
* Andras Toth for mknod rdev issue.
* Michael Fischer for finding the problem with inode inconsistency.
* Some code bodily lifted from JFFS
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
/*
*
* This is the file system front-end to YAFFS that hooks it up to
* the VFS.
*
* Special notes:
* >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
* this superblock
* >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
* superblock
* >> inode->u.generic_ip points to the associated struct yaffs_obj.
*/
/*
* There are two variants of the VFS glue code. This variant should compile
* for any version of Linux.
*/
#include <linux/version.h>
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
#define YAFFS_COMPILE_BACKGROUND
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23))
#define YAFFS_COMPILE_FREEZER
#endif
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
#define YAFFS_COMPILE_EXPORTFS
#endif
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
#define YAFFS_USE_SETATTR_COPY
#define YAFFS_USE_TRUNCATE_SETSIZE
#endif
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
#define YAFFS_HAS_EVICT_INODE
#endif
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
#define YAFFS_NEW_FOLLOW_LINK 1
#else
#define YAFFS_NEW_FOLLOW_LINK 0
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
#include <linux/config.h>
#endif
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
#include <linux/smp_lock.h>
#endif
#include <linux/pagemap.h>
#include <linux/mtd/mtd.h>
#include <linux/interrupt.h>
#include <linux/string.h>
#include <linux/ctype.h>
#if (YAFFS_NEW_FOLLOW_LINK == 1)
#include <linux/namei.h>
#endif
#ifdef YAFFS_COMPILE_EXPORTFS
#include <linux/exportfs.h>
#endif
#ifdef YAFFS_COMPILE_BACKGROUND
#include <linux/kthread.h>
#include <linux/delay.h>
#endif
#ifdef YAFFS_COMPILE_FREEZER
#include <linux/freezer.h>
#endif
#include <asm/div64.h>
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
#include <linux/statfs.h>
#define UnlockPage(p) unlock_page(p)
#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
/* FIXME: use sb->s_id instead ? */
#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
#else
#include <linux/locks.h>
#define BDEVNAME_SIZE 0
#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
#define __user
#endif
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
#define YPROC_ROOT (&proc_root)
#else
#define YPROC_ROOT NULL
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
#define Y_INIT_TIMER(a) init_timer(a)
#else
#define Y_INIT_TIMER(a) init_timer_on_stack(a)
#endif
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
#define WRITE_SIZE_STR "writesize"
#define WRITE_SIZE(mtd) ((mtd)->writesize)
#else
#define WRITE_SIZE_STR "oobblock"
#define WRITE_SIZE(mtd) ((mtd)->oobblock)
#endif
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
#define YAFFS_USE_WRITE_BEGIN_END 1
#else
#define YAFFS_USE_WRITE_BEGIN_END 0
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
#define set_nlink(inode, count) do { (inode)->i_nlink = (count); } while(0)
#endif
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
{
uint64_t result = partition_size;
do_div(result, block_size);
return (uint32_t) result;
}
#else
#define YCALCBLOCKS(s, b) ((s)/(b))
#endif
#include <linux/uaccess.h>
#include <linux/mtd/mtd.h>
#include "yportenv.h"
#include "yaffs_trace.h"
#include "yaffs_guts.h"
#include "yaffs_attribs.h"
#include "yaffs_linux.h"
#include "yaffs_mtdif.h"
unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
unsigned int yaffs_auto_checkpoint = 1;
unsigned int yaffs_gc_control = 1;
unsigned int yaffs_bg_enable = 1;
unsigned int yaffs_auto_select = 1;
/* Module Parameters */
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
module_param(yaffs_trace_mask, uint, 0644);
module_param(yaffs_wr_attempts, uint, 0644);
module_param(yaffs_auto_checkpoint, uint, 0644);
module_param(yaffs_gc_control, uint, 0644);
module_param(yaffs_bg_enable, uint, 0644);
#else
MODULE_PARM(yaffs_trace_mask, "i");
MODULE_PARM(yaffs_wr_attempts, "i");
MODULE_PARM(yaffs_auto_checkpoint, "i");
MODULE_PARM(yaffs_gc_control, "i");
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
/* use iget and read_inode */
#define Y_IGET(sb, inum) iget((sb), (inum))
static void yaffs_read_inode(struct inode *inode);
#else
/* Call local equivalent */
#define YAFFS_USE_OWN_IGET
#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
#endif
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
#define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
#else
#define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
#endif
#define yaffs_inode_to_obj(iptr) \
((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
#define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
#else
#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
#endif
#define update_dir_time(dir) do {\
(dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
} while (0)
static void yaffs_put_super(struct super_block *sb);
static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
loff_t *pos);
static ssize_t yaffs_hold_space(struct file *f);
static void yaffs_release_space(struct file *f);
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
static int yaffs_file_flush(struct file *file, fl_owner_t id);
#else
static int yaffs_file_flush(struct file *file);
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync);
#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
static int yaffs_sync_object(struct file *file, int datasync);
#else
static int yaffs_sync_object(struct file *file, struct dentry *dentry,
int datasync);
#endif
static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
struct nameidata *n);
static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
struct nameidata *n);
#else
static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
#endif
static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
struct dentry *dentry);
static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
const char *symname);
static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
dev_t dev);
#else
static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
int dev);
#endif
static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry);
static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
static int yaffs_sync_fs(struct super_block *sb, int wait);
static void yaffs_write_super(struct super_block *sb);
#else
static int yaffs_sync_fs(struct super_block *sb);
static int yaffs_write_super(struct super_block *sb);
#endif
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
#else
static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
#endif
#ifdef YAFFS_HAS_PUT_INODE
static void yaffs_put_inode(struct inode *inode);
#endif
#ifdef YAFFS_HAS_EVICT_INODE
static void yaffs_evict_inode(struct inode *);
#else
static void yaffs_delete_inode(struct inode *);
static void yaffs_clear_inode(struct inode *);
#endif
static int yaffs_readpage(struct file *file, struct page *page);
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
#else
static int yaffs_writepage(struct page *page);
#endif
static int yaffs_setxattr(struct dentry *dentry, const char *name,
const void *value, size_t size, int flags);
static ssize_t yaffs_getxattr(struct dentry *dentry, const char *name,
void *buff, size_t size);
static int yaffs_removexattr(struct dentry *dentry, const char *name);
static ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
#if (YAFFS_USE_WRITE_BEGIN_END != 0)
static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
loff_t pos, unsigned len, unsigned flags,
struct page **pagep, void **fsdata);
static int yaffs_write_end(struct file *filp, struct address_space *mapping,
loff_t pos, unsigned len, unsigned copied,
struct page *pg, void *fsdadata);
#else
static int yaffs_prepare_write(struct file *f, struct page *pg,
unsigned offset, unsigned to);
static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
unsigned to);
#endif
static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
int buflen);
#if (YAFFS_NEW_FOLLOW_LINK == 1)
void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
#else
static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
#endif
static void yaffs_touch_super(struct yaffs_dev *dev);
static int yaffs_vfs_setattr(struct inode *, struct iattr *);
static struct address_space_operations yaffs_file_address_operations = {
.readpage = yaffs_readpage,
.writepage = yaffs_writepage,
#if (YAFFS_USE_WRITE_BEGIN_END > 0)
.write_begin = yaffs_write_begin,
.write_end = yaffs_write_end,
#else
.prepare_write = yaffs_prepare_write,
.commit_write = yaffs_commit_write,
#endif
};
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
static const struct file_operations yaffs_file_operations = {
.read = do_sync_read,
.write = do_sync_write,
.aio_read = generic_file_aio_read,
.aio_write = generic_file_aio_write,
.mmap = generic_file_mmap,
.flush = yaffs_file_flush,
.fsync = yaffs_sync_object,
.splice_read = generic_file_splice_read,
.splice_write = generic_file_splice_write,
.llseek = generic_file_llseek,
};
#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
static const struct file_operations yaffs_file_operations = {
.read = do_sync_read,
.write = do_sync_write,
.aio_read = generic_file_aio_read,
.aio_write = generic_file_aio_write,
.mmap = generic_file_mmap,
.flush = yaffs_file_flush,
.fsync = yaffs_sync_object,
.sendfile = generic_file_sendfile,
};
#else
static const struct file_operations yaffs_file_operations = {
.read = generic_file_read,
.write = generic_file_write,
.mmap = generic_file_mmap,
.flush = yaffs_file_flush,
.fsync = yaffs_sync_object,
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
.sendfile = generic_file_sendfile,
#endif
};
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
static void zero_user_segment(struct page *page, unsigned start, unsigned end)
{
void *kaddr = kmap_atomic(page, KM_USER0);
memset(kaddr + start, 0, end - start);
kunmap_atomic(kaddr, KM_USER0);
flush_dcache_page(page);
}
#endif
static const struct inode_operations yaffs_file_inode_operations = {
.setattr = yaffs_setattr,
.setxattr = yaffs_setxattr,
.getxattr = yaffs_getxattr,
.listxattr = yaffs_listxattr,
.removexattr = yaffs_removexattr,
};
static const struct inode_operations yaffs_symlink_inode_operations = {
.readlink = yaffs_readlink,
.follow_link = yaffs_follow_link,
#if (YAFFS_NEW_FOLLOW_LINK == 1)
.put_link = yaffs_put_link,
#endif
.setattr = yaffs_setattr,
.setxattr = yaffs_setxattr,
.getxattr = yaffs_getxattr,
.listxattr = yaffs_listxattr,
.removexattr = yaffs_removexattr,
};
static const struct inode_operations yaffs_dir_inode_operations = {
.create = yaffs_create,
.lookup = yaffs_lookup,
.link = yaffs_link,
.unlink = yaffs_unlink,
.symlink = yaffs_symlink,
.mkdir = yaffs_mkdir,
.rmdir = yaffs_unlink,
.mknod = yaffs_mknod,
.rename = yaffs_rename,
.setattr = yaffs_setattr,
.setxattr = yaffs_setxattr,
.getxattr = yaffs_getxattr,
.listxattr = yaffs_listxattr,
.removexattr = yaffs_removexattr,
};
static const struct file_operations yaffs_dir_operations = {
.read = generic_read_dir,
.readdir = yaffs_readdir,
.fsync = yaffs_sync_object,
.llseek = generic_file_llseek,
};
static const struct super_operations yaffs_super_ops = {
.statfs = yaffs_statfs,
#ifndef YAFFS_USE_OWN_IGET
.read_inode = yaffs_read_inode,
#endif
#ifdef YAFFS_HAS_PUT_INODE
.put_inode = yaffs_put_inode,
#endif
.put_super = yaffs_put_super,
#ifdef YAFFS_HAS_EVICT_INODE
.evict_inode = yaffs_evict_inode,
#else
.delete_inode = yaffs_delete_inode,
.clear_inode = yaffs_clear_inode,
#endif
.sync_fs = yaffs_sync_fs,
.write_super = yaffs_write_super,
};
static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
{
#ifdef YAFFS_USE_SETATTR_COPY
setattr_copy(inode, attr);
return 0;
#else
return inode_setattr(inode, attr);
#endif
}
static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
{
#ifdef YAFFS_USE_TRUNCATE_SETSIZE
truncate_setsize(inode, newsize);
return 0;
#else
truncate_inode_pages(&inode->i_data, newsize);
return 0;
#endif
}
static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
{
return yaffs_gc_control;
}
static void yaffs_gross_lock(struct yaffs_dev *dev)
{
yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
}
static void yaffs_gross_unlock(struct yaffs_dev *dev)
{
yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
}
#ifdef YAFFS_COMPILE_EXPORTFS
static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
uint32_t generation)
{
return Y_IGET(sb, ino);
}
static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
struct fid *fid, int fh_len,
int fh_type)
{
return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
yaffs2_nfs_get_inode);
}
static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
struct fid *fid, int fh_len,
int fh_type)
{
return generic_fh_to_parent(sb, fid, fh_len, fh_type,
yaffs2_nfs_get_inode);
}
struct dentry *yaffs2_get_parent(struct dentry *dentry)
{
struct super_block *sb = dentry->d_inode->i_sb;
struct dentry *parent = ERR_PTR(-ENOENT);
struct inode *inode;
unsigned long parent_ino;
struct yaffs_obj *d_obj;
struct yaffs_obj *parent_obj;
d_obj = yaffs_inode_to_obj(dentry->d_inode);
if (d_obj) {
parent_obj = d_obj->parent;
if (parent_obj) {
parent_ino = yaffs_get_obj_inode(parent_obj);
inode = Y_IGET(sb, parent_ino);
if (IS_ERR(inode)) {
parent = ERR_CAST(inode);
} else {
parent = d_obtain_alias(inode);
if (!IS_ERR(parent)) {
parent = ERR_PTR(-ENOMEM);
iput(inode);
}
}
}
}
return parent;
}
/* Just declare a zero structure as a NULL value implies
* using the default functions of exportfs.
*/
static struct export_operations yaffs_export_ops = {
.fh_to_dentry = yaffs2_fh_to_dentry,
.fh_to_parent = yaffs2_fh_to_parent,
.get_parent = yaffs2_get_parent,
};
#endif
/*-----------------------------------------------------------------*/
/* Directory search context allows us to unlock access to yaffs during
* filldir without causing problems with the directory being modified.
* This is similar to the tried and tested mechanism used in yaffs direct.
*
* A search context iterates along a doubly linked list of siblings in the
* directory. If the iterating object is deleted then this would corrupt
* the list iteration, likely causing a crash. The search context avoids
* this by using the remove_obj_fn to move the search context to the
* next object before the object is deleted.
*
* Many readdirs (and thus seach conexts) may be alive simulateously so
* each struct yaffs_dev has a list of these.
*
* A seach context lives for the duration of a readdir.
*
* All these functions must be called while yaffs is locked.
*/
struct yaffs_search_context {
struct yaffs_dev *dev;
struct yaffs_obj *dir_obj;
struct yaffs_obj *next_return;
struct list_head others;
};
/*
* yaffs_new_search() creates a new search context, initialises it and
* adds it to the device's search context list.
*
* Called at start of readdir.
*/
static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
{
struct yaffs_dev *dev = dir->my_dev;
struct yaffs_search_context *sc =
kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
if (sc) {
sc->dir_obj = dir;
sc->dev = dev;
if (list_empty(&sc->dir_obj->variant.dir_variant.children))
sc->next_return = NULL;
else
sc->next_return =
list_entry(dir->variant.dir_variant.children.next,
struct yaffs_obj, siblings);
INIT_LIST_HEAD(&sc->others);
list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
}
return sc;
}
/*
* yaffs_search_end() disposes of a search context and cleans up.
*/
static void yaffs_search_end(struct yaffs_search_context *sc)
{
if (sc) {
list_del(&sc->others);
kfree(sc);
}
}
/*
* yaffs_search_advance() moves a search context to the next object.
* Called when the search iterates or when an object removal causes
* the search context to be moved to the next object.
*/
static void yaffs_search_advance(struct yaffs_search_context *sc)
{
if (!sc)
return;
if (sc->next_return == NULL ||
list_empty(&sc->dir_obj->variant.dir_variant.children))
sc->next_return = NULL;
else {
struct list_head *next = sc->next_return->siblings.next;
if (next == &sc->dir_obj->variant.dir_variant.children)
sc->next_return = NULL; /* end of list */
else
sc->next_return =
list_entry(next, struct yaffs_obj, siblings);
}
}
/*
* yaffs_remove_obj_callback() is called when an object is unlinked.
* We check open search contexts and advance any which are currently
* on the object being iterated.
*/
static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
{
struct list_head *i;
struct yaffs_search_context *sc;
struct list_head *search_contexts =
&(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
/* Iterate through the directory search contexts.
* If any are currently on the object being removed, then advance
* the search context to the next object to prevent a hanging pointer.
*/
list_for_each(i, search_contexts) {
sc = list_entry(i, struct yaffs_search_context, others);
if (sc->next_return == obj)
yaffs_search_advance(sc);
}
}
/*-----------------------------------------------------------------*/
static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
int buflen)
{
unsigned char *alias;
int ret;
struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
yaffs_gross_lock(dev);
alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
yaffs_gross_unlock(dev);
if (!alias)
return -ENOMEM;
ret = vfs_readlink(dentry, buffer, buflen, alias);
kfree(alias);
return ret;
}
#if (YAFFS_NEW_FOLLOW_LINK == 1)
static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
{
void *ret;
#else
static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
{
int ret
#endif
unsigned char *alias;
int ret_int = 0;
struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
yaffs_gross_lock(dev);
alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
yaffs_gross_unlock(dev);
if (!alias) {
ret_int = -ENOMEM;
goto out;
}
#if (YAFFS_NEW_FOLLOW_LINK == 1)
nd_set_link(nd, alias);
ret = alias;
out:
if (ret_int)
ret = ERR_PTR(ret_int);
return ret;
#else
ret = vfs_follow_link(nd, alias);
kfree(alias);
out:
if (ret_int)
ret = ret_int;
return ret;
#endif
}
#if (YAFFS_NEW_FOLLOW_LINK == 1)
void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
{
kfree(alias);
}
#endif
struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
struct yaffs_obj *obj);
/*
* Lookup is used to find objects in the fs
*/
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
struct nameidata *n)
#else
static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
#endif
{
struct yaffs_obj *obj;
struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
if (current != yaffs_dev_to_lc(dev)->readdir_process)
yaffs_gross_lock(dev);
yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
/* Can't hold gross lock when calling yaffs_get_inode() */
if (current != yaffs_dev_to_lc(dev)->readdir_process)
yaffs_gross_unlock(dev);
if (obj) {
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_lookup found %d", obj->obj_id);
inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
} else {
yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
}
/* added NCB for 2.5/6 compatability - forces add even if inode is
* NULL which creates dentry hash */
d_add(dentry, inode);
return NULL;
}
#ifdef YAFFS_HAS_PUT_INODE
/* For now put inode is just for debugging
* Put inode is called when the inode **structure** is put.
*/
static void yaffs_put_inode(struct inode *inode)
{
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_put_inode: ino %d, count %d"),
(int)inode->i_ino, atomic_read(&inode->i_count);
}
#endif
static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
{
/* Clear the association between the inode and
* the struct yaffs_obj.
*/
obj->my_inode = NULL;
yaffs_inode_to_obj_lv(inode) = NULL;
/* If the object freeing was deferred, then the real
* free happens now.
* This should fix the inode inconsistency problem.
*/
yaffs_handle_defered_free(obj);
}
#ifdef YAFFS_HAS_EVICT_INODE
/* yaffs_evict_inode combines into one operation what was previously done in
* yaffs_clear_inode() and yaffs_delete_inode()
*
*/
static void yaffs_evict_inode(struct inode *inode)
{
struct yaffs_obj *obj;
struct yaffs_dev *dev;
int deleteme = 0;
obj = yaffs_inode_to_obj(inode);
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_evict_inode: ino %d, count %d %s",
(int)inode->i_ino, atomic_read(&inode->i_count),
obj ? "object exists" : "null object");
if (!inode->i_nlink && !is_bad_inode(inode))
deleteme = 1;
truncate_inode_pages(&inode->i_data, 0);
end_writeback(inode);
if (deleteme && obj) {
dev = obj->my_dev;
yaffs_gross_lock(dev);
yaffs_del_obj(obj);
yaffs_gross_unlock(dev);
}
if (obj) {
dev = obj->my_dev;
yaffs_gross_lock(dev);
yaffs_unstitch_obj(inode, obj);
yaffs_gross_unlock(dev);
}
}
#else
/* clear is called to tell the fs to release any per-inode data it holds.
* The object might still exist on disk and is just being thrown out of the cache
* or else the object has actually been deleted and we're being called via
* the chain
* yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
*/
static void yaffs_clear_inode(struct inode *inode)
{
struct yaffs_obj *obj;
struct yaffs_dev *dev;
obj = yaffs_inode_to_obj(inode);
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_clear_inode: ino %d, count %d %s",
(int)inode->i_ino, atomic_read(&inode->i_count),
obj ? "object exists" : "null object");
if (obj) {
dev = obj->my_dev;
yaffs_gross_lock(dev);
yaffs_unstitch_obj(inode, obj);
yaffs_gross_unlock(dev);
}
}
/* delete is called when the link count is zero and the inode
* is put (ie. nobody wants to know about it anymore, time to
* delete the file).
* NB Must call clear_inode()
*/
static void yaffs_delete_inode(struct inode *inode)
{
struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
struct yaffs_dev *dev;
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_delete_inode: ino %d, count %d %s",
(int)inode->i_ino, atomic_read(&inode->i_count),
obj ? "object exists" : "null object");
if (obj) {
dev = obj->my_dev;
yaffs_gross_lock(dev);
yaffs_del_obj(obj);
yaffs_gross_unlock(dev);
}
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
truncate_inode_pages(&inode->i_data, 0);
#endif
clear_inode(inode);
}
#endif
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
static int yaffs_file_flush(struct file *file, fl_owner_t id)
#else
static int yaffs_file_flush(struct file *file)
#endif
{
struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
struct yaffs_dev *dev = obj->my_dev;
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_file_flush object %d (%s)",
obj->obj_id,
obj->dirty ? "dirty" : "clean");
yaffs_gross_lock(dev);
yaffs_flush_file(obj, 1, 0);
yaffs_gross_unlock(dev);
return 0;
}
static int yaffs_readpage_nolock(struct file *f, struct page *pg)
{
/* Lifted from jffs2 */
struct yaffs_obj *obj;
unsigned char *pg_buf;
int ret;
loff_t pos = ((loff_t) pg->index) << PAGE_CACHE_SHIFT;
struct yaffs_dev *dev;
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_readpage_nolock at %lld, size %08x",
(long long)pos,
(unsigned)PAGE_CACHE_SIZE);
obj = yaffs_dentry_to_obj(f->f_dentry);
dev = obj->my_dev;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
BUG_ON(!PageLocked(pg));
#else
if (!PageLocked(pg))
PAGE_BUG(pg);
#endif
pg_buf = kmap(pg);
/* FIXME: Can kmap fail? */
yaffs_gross_lock(dev);