-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathyaffs_vfs_multi.c
3626 lines (2923 loc) · 86 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(3, 6, 0))
#define YAFFS_HAS_WRITE_SUPER
#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, 27))
#define YAFFS_USE_WRITE_BEGIN_END 1
#else
#define YAFFS_USE_WRITE_BEGIN_END 0
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
#define YAFFS_SUPER_HAS_DIRTY
#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"
#include "yaffs_packedtags2.h"
#include "yaffs_getblockinfo.h"
unsigned int yaffs_trace_mask =
YAFFS_TRACE_BAD_BLOCKS |
YAFFS_TRACE_ALWAYS |
0;
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))
#else
/* Call local equivalent */
#define YAFFS_USE_OWN_IGET
#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
#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
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
#define Y_CLEAR_INODE(i) clear_inode(i)
#else
#define Y_CLEAR_INODE(i) end_writeback(i)
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
#define YAFFS_USE_DIR_ITERATE
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,12,0))
#define YAFFS_NEW_PROCFS
#include <linux/seq_file.h>
#endif
#define update_dir_time(dir) do {\
(dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
} while (0)
static void yaffs_fill_inode_from_obj(struct inode *inode,
struct yaffs_obj *obj);
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));
}
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);
ret = yaffs_file_rd(obj, pg_buf, pos, PAGE_CACHE_SIZE);
yaffs_gross_unlock(dev);
if (ret >= 0)
ret = 0;
if (ret) {
ClearPageUptodate(pg);
SetPageError(pg);
} else {
SetPageUptodate(pg);
ClearPageError(pg);
}
flush_dcache_page(pg);
kunmap(pg);
yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
return ret;
}
static int yaffs_readpage_unlock(struct file *f, struct page *pg)
{
int ret = yaffs_readpage_nolock(f, pg);
UnlockPage(pg);
return ret;
}
static int yaffs_readpage(struct file *f, struct page *pg)
{
int ret;
yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
ret = yaffs_readpage_unlock(f, pg);
yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
return ret;
}
static void yaffs_set_super_dirty_val(struct yaffs_dev *dev, int val)
{
struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
if (lc)
lc->dirty = val;
# ifdef YAFFS_SUPER_HAS_DIRTY
{
struct super_block *sb = lc->super;
if (sb)
sb->s_dirt = val;
}
#endif
}
static void yaffs_set_super_dirty(struct yaffs_dev *dev)
{
yaffs_set_super_dirty_val(dev, 1);
}
static void yaffs_clear_super_dirty(struct yaffs_dev *dev)
{
yaffs_set_super_dirty_val(dev, 0);
}
static int yaffs_check_super_dirty(struct yaffs_dev *dev)
{
struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
if (lc && lc->dirty)
return 1;
# ifdef YAFFS_SUPER_HAS_DIRTY
{
struct super_block *sb = lc->super;
if (sb && sb->s_dirt)
return 1;
}
#endif
return 0;
}
#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
{
struct yaffs_dev *dev;
struct address_space *mapping = page->mapping;
struct inode *inode;
unsigned long end_index;
char *buffer;
struct yaffs_obj *obj;
int n_written = 0;
unsigned n_bytes;
loff_t i_size;
if (!mapping)
BUG();
inode = mapping->host;
if (!inode)
BUG();
i_size = i_size_read(inode);
end_index = i_size >> PAGE_CACHE_SHIFT;
if (page->index < end_index)
n_bytes = PAGE_CACHE_SIZE;
else {
n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
if (page->index > end_index || !n_bytes) {
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_writepage at %lld, inode size = %lld!!",
((loff_t)page->index) << PAGE_CACHE_SHIFT,
inode->i_size);
yaffs_trace(YAFFS_TRACE_OS,
" -> don't care!!");
zero_user_segment(page, 0, PAGE_CACHE_SIZE);
set_page_writeback(page);
unlock_page(page);
end_page_writeback(page);
return 0;
}
}
if (n_bytes != PAGE_CACHE_SIZE)
zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
get_page(page);
buffer = kmap(page);
obj = yaffs_inode_to_obj(inode);
dev = obj->my_dev;
yaffs_gross_lock(dev);
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_writepage at %lld, size %08x",
((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes);
yaffs_trace(YAFFS_TRACE_OS,
"writepag0: obj = %lld, ino = %lld",
obj->variant.file_variant.file_size, inode->i_size);
n_written = yaffs_wr_file(obj, buffer,
((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes, 0);
yaffs_set_super_dirty(dev);
yaffs_trace(YAFFS_TRACE_OS,
"writepag1: obj = %lld, ino = %lld",
obj->variant.file_variant.file_size, inode->i_size);
yaffs_gross_unlock(dev);
kunmap(page);
set_page_writeback(page);
unlock_page(page);
end_page_writeback(page);
put_page(page);
return (n_written == n_bytes) ? 0 : -ENOSPC;
}
/* Space holding and freeing is done to ensure we have space available for write_begin/end */
/* For now we just assume few parallel writes and check against a small number. */
/* Todo: need to do this with a counter to handle parallel reads better */
static ssize_t yaffs_hold_space(struct file *f)
{
struct yaffs_obj *obj;
struct yaffs_dev *dev;
int n_free_chunks;
obj = yaffs_dentry_to_obj(f->f_dentry);
dev = obj->my_dev;
yaffs_gross_lock(dev);
n_free_chunks = yaffs_get_n_free_chunks(dev);
yaffs_gross_unlock(dev);
return (n_free_chunks > 20) ? 1 : 0;
}
static void yaffs_release_space(struct file *f)
{
struct yaffs_obj *obj;
struct yaffs_dev *dev;
obj = yaffs_dentry_to_obj(f->f_dentry);
dev = obj->my_dev;
yaffs_gross_lock(dev);
yaffs_gross_unlock(dev);
}
#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)
{
struct page *pg = NULL;
pgoff_t index = pos >> PAGE_CACHE_SHIFT;
int ret = 0;
int space_held = 0;
/* Get a page */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
pg = grab_cache_page_write_begin(mapping, index, flags);
#else
pg = __grab_cache_page(mapping, index);
#endif
*pagep = pg;
if (!pg) {
ret = -ENOMEM;
goto out;
}
yaffs_trace(YAFFS_TRACE_OS,
"start yaffs_write_begin index %d(%x) uptodate %d",
(int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
/* Get fs space */
space_held = yaffs_hold_space(filp);
if (!space_held) {
ret = -ENOSPC;
goto out;
}
/* Update page if required */
if (!Page_Uptodate(pg))
ret = yaffs_readpage_nolock(filp, pg);
if (ret)
goto out;
/* Happy path return */
yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
return 0;
out:
yaffs_trace(YAFFS_TRACE_OS,
"end yaffs_write_begin fail returning %d", ret);
if (space_held)
yaffs_release_space(filp);
if (pg) {
unlock_page(pg);
page_cache_release(pg);
}
return ret;
}
#else
static int yaffs_prepare_write(struct file *f, struct page *pg,
unsigned offset, unsigned to)
{
yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
if (!Page_Uptodate(pg))
return yaffs_readpage_nolock(f, pg);
return 0;
}
#endif
static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
loff_t * pos)
{
struct yaffs_obj *obj;
int n_written;
loff_t ipos;
struct inode *inode;
struct yaffs_dev *dev;
obj = yaffs_dentry_to_obj(f->f_dentry);
if (!obj) {
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_file_write: hey obj is null!");
return -EINVAL;
}
dev = obj->my_dev;
yaffs_gross_lock(dev);
inode = f->f_dentry->d_inode;
if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
ipos = inode->i_size;
else
ipos = *pos;
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_file_write about to write writing %u(%x) bytes to object %d at %lld",
(unsigned)n, (unsigned)n, obj->obj_id, ipos);
n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
yaffs_set_super_dirty(dev);
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_file_write: %d(%x) bytes written",
(unsigned)n, (unsigned)n);
if (n_written > 0) {
ipos += n_written;
*pos = ipos;
if (ipos > inode->i_size) {
inode->i_size = ipos;
inode->i_blocks = (ipos + 511) >> 9;
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_file_write size updated to %lld bytes, %d blocks",
ipos, (int)(inode->i_blocks));
}
}
yaffs_gross_unlock(dev);
return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
}
#if (YAFFS_USE_WRITE_BEGIN_END > 0)
static int yaffs_write_end(struct file *filp, struct address_space *mapping,
loff_t pos, unsigned len, unsigned copied,
struct page *pg, void *fsdadata)
{
int ret = 0;
void *addr, *kva;
uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
kva = kmap(pg);
addr = kva + offset_into_page;
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_write_end addr %p pos %lld n_bytes %d",
addr, pos, copied);
ret = yaffs_file_write(filp, addr, copied, &pos);
if (ret != copied) {
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_write_end not same size ret %d copied %d",
ret, copied);
SetPageError(pg);
}
kunmap(pg);
yaffs_release_space(filp);
unlock_page(pg);
page_cache_release(pg);
return ret;
}
#else
static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
unsigned to)
{
void *addr, *kva;
loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
int n_bytes = to - offset;
int n_written;
kva = kmap(pg);
addr = kva + offset;
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_commit_write addr %p pos %lld n_bytes %d",
addr, pos, n_bytes);
n_written = yaffs_file_write(f, addr, n_bytes, &pos);
if (n_written != n_bytes) {
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_commit_write not same size n_written %d n_bytes %d",
n_written, n_bytes);
SetPageError(pg);
}
kunmap(pg);
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_commit_write returning %d",
n_written == n_bytes ? 0 : n_written);
return n_written == n_bytes ? 0 : n_written;
}
#endif
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, 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;
}
#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
{
struct yaffs_obj *obj;
struct yaffs_dev *dev;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
struct dentry *dentry = file->f_path.dentry;
#endif
obj = yaffs_dentry_to_obj(dentry);
dev = obj->my_dev;
yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
"yaffs_sync_object");
yaffs_gross_lock(dev);
yaffs_flush_file(obj, 1, datasync);
yaffs_gross_unlock(dev);
return 0;
}
#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 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 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_setattr(struct dentry *dentry, struct iattr *attr)
{
struct inode *inode = dentry->d_inode;
int error = 0;
struct yaffs_dev *dev;
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_setattr of object %d",
yaffs_inode_to_obj(inode)->obj_id);
#if 0
/* Fail if a requested resize >= 2GB */
if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
error = -EINVAL;
#endif
if (error == 0)
error = inode_change_ok(inode, attr);
if (error == 0) {
int result;
if (!error) {
error = yaffs_vfs_setattr(inode, attr);
yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
if (attr->ia_valid & ATTR_SIZE) {
yaffs_vfs_setsize(inode, attr->ia_size);
inode->i_blocks = (inode->i_size + 511) >> 9;
}
}
dev = yaffs_inode_to_obj(inode)->my_dev;
if (attr->ia_valid & ATTR_SIZE) {
yaffs_trace(YAFFS_TRACE_OS,
"resize to %d(%x)",
(int)(attr->ia_size),
(int)(attr->ia_size));
}
yaffs_gross_lock(dev);
result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
if (result == YAFFS_OK) {
error = 0;
} else {
error = -EPERM;
}
yaffs_gross_unlock(dev);
}
yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
return error;
}
static int yaffs_setxattr(struct dentry *dentry, const char *name,
const void *value, size_t size, int flags)
{
struct inode *inode = dentry->d_inode;
int error = 0;
struct yaffs_dev *dev;
struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
if (error == 0) {
int result;
dev = obj->my_dev;
yaffs_gross_lock(dev);
result = yaffs_set_xattrib(obj, name, value, size, flags);
if (result == YAFFS_OK)
error = 0;
else if (result < 0)
error = result;
yaffs_gross_unlock(dev);
}
yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
return error;
}
static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
void *buff, size_t size)
{
struct inode *inode = dentry->d_inode;
int error = 0;
struct yaffs_dev *dev;
struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_getxattr \"%s\" from object %d",
name, obj->obj_id);
if (error == 0) {
dev = obj->my_dev;
yaffs_gross_lock(dev);
error = yaffs_get_xattrib(obj, name, buff, size);
yaffs_gross_unlock(dev);
}
yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
return error;
}
static int yaffs_removexattr(struct dentry *dentry, const char *name)
{
struct inode *inode = dentry->d_inode;
int error = 0;
struct yaffs_dev *dev;
struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_removexattr of object %d", obj->obj_id);
if (error == 0) {
int result;
dev = obj->my_dev;
yaffs_gross_lock(dev);
result = yaffs_remove_xattrib(obj, name);
if (result == YAFFS_OK)
error = 0;
else if (result < 0)
error = result;
yaffs_gross_unlock(dev);
}
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_removexattr done returning %d", error);
return error;
}
static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
{
struct inode *inode = dentry->d_inode;
int error = 0;
struct yaffs_dev *dev;
struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
yaffs_trace(YAFFS_TRACE_OS,
"yaffs_listxattr of object %d", obj->obj_id);
if (error == 0) {
dev = obj->my_dev;
yaffs_gross_lock(dev);
error = yaffs_list_xattrib(obj, buff, size);
yaffs_gross_unlock(dev);