This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
/
yaffs_fs.c
2126 lines (1724 loc) · 51.8 KB
/
yaffs_fs.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 FFS. A NAND-flash specific file system.
* yaffs_fs.c
*
* Copyright (C) 2002 Aleph One Ltd.
* for Toby Churchill Ltd and Brightstar Engineering
*
* Created by Charles Manning <charles@aleph1.co.uk>
*
* 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 yaffs_Device associated with
* this superblock
* >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
* superblock
* >> inode->u.generic_ip points to the associated yaffs_Object.
*
* 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 JFFS2.
*/
const char *yaffs_fs_c_version =
"$Id: yaffs_fs.c,v 1.53 2006/10/03 10:13:03 charles Exp $";
extern const char *yaffs_guts_c_version;
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/smp_lock.h>
#include <linux/pagemap.h>
#include <linux/mtd/mtd.h>
#include <linux/interrupt.h>
#include <linux/string.h>
#include <linux/ctype.h>
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
#include <linux/statfs.h> /* Added NCB 15-8-2003 */
#include <asm/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
#include <asm/uaccess.h>
#include "yportenv.h"
#include "yaffs_guts.h"
unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS |
YAFFS_TRACE_BAD_BLOCKS |
YAFFS_TRACE_CHECKPOINT
/* | 0xFFFFFFFF */;
#include <linux/mtd/mtd.h>
#include "yaffs_mtdif.h"
#include "yaffs_mtdif2.h"
/*#define T(x) printk x */
#define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
#else
#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
#endif
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);
#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
static int yaffs_sync_object(struct file *file, struct dentry *dentry,
int datasync);
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
static void yaffs_read_inode(struct inode *inode);
static void yaffs_put_inode(struct inode *inode);
static void yaffs_delete_inode(struct inode *);
static void yaffs_clear_inode(struct inode *);
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_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);
static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
int buflen);
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
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 struct address_space_operations yaffs_file_address_operations = {
.readpage = yaffs_readpage,
.writepage = yaffs_writepage,
.prepare_write = yaffs_prepare_write,
.commit_write = yaffs_commit_write,
};
static 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
};
static struct inode_operations yaffs_file_inode_operations = {
.setattr = yaffs_setattr,
};
static struct inode_operations yaffs_symlink_inode_operations = {
.readlink = yaffs_readlink,
.follow_link = yaffs_follow_link,
.setattr = yaffs_setattr,
};
static 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,
};
static struct file_operations yaffs_dir_operations = {
.read = generic_read_dir,
.readdir = yaffs_readdir,
.fsync = yaffs_sync_object,
};
static struct super_operations yaffs_super_ops = {
.statfs = yaffs_statfs,
.read_inode = yaffs_read_inode,
.put_inode = yaffs_put_inode,
.put_super = yaffs_put_super,
.delete_inode = yaffs_delete_inode,
.clear_inode = yaffs_clear_inode,
.sync_fs = yaffs_sync_fs,
.write_super = yaffs_write_super,
};
static void yaffs_GrossLock(yaffs_Device * dev)
{
T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
down(&dev->grossLock);
}
static void yaffs_GrossUnlock(yaffs_Device * dev)
{
T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
up(&dev->grossLock);
}
static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
int buflen)
{
unsigned char *alias;
int ret;
yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
yaffs_GrossLock(dev);
alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
yaffs_GrossUnlock(dev);
if (!alias)
return -ENOMEM;
ret = vfs_readlink(dentry, buffer, buflen, alias);
kfree(alias);
return ret;
}
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
#else
static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
#endif
{
unsigned char *alias;
int ret;
yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
yaffs_GrossLock(dev);
alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
yaffs_GrossUnlock(dev);
if (!alias)
{
ret = -ENOMEM;
goto out;
}
ret = vfs_follow_link(nd, alias);
kfree(alias);
out:
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
return ERR_PTR (ret);
#else
return ret;
#endif
}
struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
yaffs_Object * 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
{
yaffs_Object *obj;
struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
yaffs_GrossLock(dev);
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_lookup for %d:%s\n",
yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
obj =
yaffs_FindObjectByName(yaffs_InodeToObject(dir),
dentry->d_name.name);
obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
/* Can't hold gross lock when calling yaffs_get_inode() */
yaffs_GrossUnlock(dev);
if (obj) {
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
if (inode) {
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_loookup dentry \n"));
/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
* d_add even if NULL inode */
#if 0
/*dget(dentry); // try to solve directory bug */
d_add(dentry, inode);
/* return dentry; */
return NULL;
#endif
}
} else {
T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
}
/* added NCB for 2.5/6 compatability - forces add even if inode is
* NULL which creates dentry hash */
d_add(dentry, inode);
return NULL;
/* return (ERR_PTR(-EIO)); */
}
/* 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)
{
T(YAFFS_TRACE_OS,
("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
atomic_read(&inode->i_count)));
}
/* clear is called to tell the fs to release any per-inode data it holds */
static void yaffs_clear_inode(struct inode *inode)
{
yaffs_Object *obj;
yaffs_Device *dev;
obj = yaffs_InodeToObject(inode);
T(YAFFS_TRACE_OS,
("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
atomic_read(&inode->i_count),
obj ? "object exists" : "null object"));
if (obj) {
dev = obj->myDev;
yaffs_GrossLock(dev);
/* Clear the association between the inode and
* the yaffs_Object.
*/
obj->myInode = NULL;
inode->u.generic_ip = NULL;
/* If the object freeing was deferred, then the real
* free happens now.
* This should fix the inode inconsistency problem.
*/
yaffs_HandleDeferedFree(obj);
yaffs_GrossUnlock(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)
{
yaffs_Object *obj = yaffs_InodeToObject(inode);
yaffs_Device *dev;
T(YAFFS_TRACE_OS,
("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
atomic_read(&inode->i_count),
obj ? "object exists" : "null object"));
if (obj) {
dev = obj->myDev;
yaffs_GrossLock(dev);
yaffs_DeleteFile(obj);
yaffs_GrossUnlock(dev);
}
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
truncate_inode_pages (&inode->i_data, 0);
#endif
clear_inode(inode);
}
#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
{
yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
yaffs_Device *dev = obj->myDev;
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
obj->dirty ? "dirty" : "clean"));
yaffs_GrossLock(dev);
yaffs_FlushFile(obj, 1);
yaffs_GrossUnlock(dev);
return 0;
}
static int yaffs_readpage_nolock(struct file *f, struct page *pg)
{
/* Lifted from jffs2 */
yaffs_Object *obj;
unsigned char *pg_buf;
int ret;
yaffs_Device *dev;
T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
(unsigned)(pg->index << PAGE_CACHE_SHIFT),
(unsigned)PAGE_CACHE_SIZE));
obj = yaffs_DentryToObject(f->f_dentry);
dev = obj->myDev;
#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_GrossLock(dev);
ret =
yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
PAGE_CACHE_SIZE);
yaffs_GrossUnlock(dev);
if (ret >= 0)
ret = 0;
if (ret) {
ClearPageUptodate(pg);
SetPageError(pg);
} else {
SetPageUptodate(pg);
ClearPageError(pg);
}
flush_dcache_page(pg);
kunmap(pg);
T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
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)
{
return yaffs_readpage_unlock(f, pg);
}
/* writepage inspired by/stolen from smbfs */
#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 address_space *mapping = page->mapping;
loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
struct inode *inode;
unsigned long end_index;
char *buffer;
yaffs_Object *obj;
int nWritten = 0;
unsigned nBytes;
if (!mapping)
BUG();
inode = mapping->host;
if (!inode)
BUG();
if (offset > inode->i_size) {
T(YAFFS_TRACE_OS,
(KERN_DEBUG
"yaffs_writepage at %08x, inode size = %08x!!!\n",
(unsigned)(page->index << PAGE_CACHE_SHIFT),
(unsigned)inode->i_size));
T(YAFFS_TRACE_OS,
(KERN_DEBUG " -> don't care!!\n"));
unlock_page(page);
return 0;
}
end_index = inode->i_size >> PAGE_CACHE_SHIFT;
/* easy case */
if (page->index < end_index) {
nBytes = PAGE_CACHE_SIZE;
} else {
nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
}
get_page(page);
buffer = kmap(page);
obj = yaffs_InodeToObject(inode);
yaffs_GrossLock(obj->myDev);
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
(unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
T(YAFFS_TRACE_OS,
(KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
(int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
nWritten =
yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
nBytes, 0);
T(YAFFS_TRACE_OS,
(KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
(int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
yaffs_GrossUnlock(obj->myDev);
kunmap(page);
SetPageUptodate(page);
UnlockPage(page);
put_page(page);
return (nWritten == nBytes) ? 0 : -ENOSPC;
}
static int yaffs_prepare_write(struct file *f, struct page *pg,
unsigned offset, unsigned to)
{
T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
return yaffs_readpage_nolock(f, pg);
return 0;
}
static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
unsigned to)
{
void *addr = page_address(pg) + offset;
loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
int nBytes = to - offset;
int nWritten;
unsigned spos = pos;
unsigned saddr = (unsigned)addr;
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
spos, nBytes));
nWritten = yaffs_file_write(f, addr, nBytes, &pos);
if (nWritten != nBytes) {
T(YAFFS_TRACE_OS,
(KERN_DEBUG
"yaffs_commit_write not same size nWritten %d nBytes %d\n",
nWritten, nBytes));
SetPageError(pg);
ClearPageUptodate(pg);
} else {
SetPageUptodate(pg);
}
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_commit_write returning %d\n",
nWritten == nBytes ? 0 : nWritten));
return nWritten == nBytes ? 0 : nWritten;
}
static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
{
if (inode && obj) {
/* Check mode against the variant type and attempt to repair if broken. */
__u32 mode = obj->yst_mode;
switch( obj->variantType ){
case YAFFS_OBJECT_TYPE_FILE :
if( ! S_ISREG(mode) ){
obj->yst_mode &= ~S_IFMT;
obj->yst_mode |= S_IFREG;
}
break;
case YAFFS_OBJECT_TYPE_SYMLINK :
if( ! S_ISLNK(mode) ){
obj->yst_mode &= ~S_IFMT;
obj->yst_mode |= S_IFLNK;
}
break;
case YAFFS_OBJECT_TYPE_DIRECTORY :
if( ! S_ISDIR(mode) ){
obj->yst_mode &= ~S_IFMT;
obj->yst_mode |= S_IFDIR;
}
break;
case YAFFS_OBJECT_TYPE_UNKNOWN :
case YAFFS_OBJECT_TYPE_HARDLINK :
case YAFFS_OBJECT_TYPE_SPECIAL :
default:
/* TODO? */
break;
}
inode->i_ino = obj->objectId;
inode->i_mode = obj->yst_mode;
inode->i_uid = obj->yst_uid;
inode->i_gid = obj->yst_gid;
inode->i_blksize = inode->i_sb->s_blocksize;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
inode->i_rdev = old_decode_dev(obj->yst_rdev);
inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
inode->i_atime.tv_nsec = 0;
inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
inode->i_mtime.tv_nsec = 0;
inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
inode->i_ctime.tv_nsec = 0;
#else
inode->i_rdev = obj->yst_rdev;
inode->i_atime = obj->yst_atime;
inode->i_mtime = obj->yst_mtime;
inode->i_ctime = obj->yst_ctime;
#endif
inode->i_size = yaffs_GetObjectFileLength(obj);
inode->i_blocks = (inode->i_size + 511) >> 9;
inode->i_nlink = yaffs_GetObjectLinkCount(obj);
T(YAFFS_TRACE_OS,
(KERN_DEBUG
"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
inode->i_mode, inode->i_uid, inode->i_gid,
(int)inode->i_size, atomic_read(&inode->i_count)));
switch (obj->yst_mode & S_IFMT) {
default: /* fifo, device or socket */
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
init_special_inode(inode, obj->yst_mode,
old_decode_dev(obj->yst_rdev));
#else
init_special_inode(inode, obj->yst_mode,
(dev_t) (obj->yst_rdev));
#endif
break;
case S_IFREG: /* file */
inode->i_op = &yaffs_file_inode_operations;
inode->i_fop = &yaffs_file_operations;
inode->i_mapping->a_ops =
&yaffs_file_address_operations;
break;
case S_IFDIR: /* directory */
inode->i_op = &yaffs_dir_inode_operations;
inode->i_fop = &yaffs_dir_operations;
break;
case S_IFLNK: /* symlink */
inode->i_op = &yaffs_symlink_inode_operations;
break;
}
inode->u.generic_ip = obj;
obj->myInode = inode;
} else {
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
}
}
struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
yaffs_Object * obj)
{
struct inode *inode;
if (!sb) {
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
return NULL;
}
if (!obj) {
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
return NULL;
}
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
inode = iget(sb, obj->objectId);
/* NB Side effect: iget calls back to yaffs_read_inode(). */
/* iget also increments the inode's i_count */
/* NB You can't be holding grossLock or deadlock will happen! */
return inode;
}
static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
loff_t * pos)
{
yaffs_Object *obj;
int nWritten, ipos;
struct inode *inode;
yaffs_Device *dev;
obj = yaffs_DentryToObject(f->f_dentry);
dev = obj->myDev;
yaffs_GrossLock(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;
}
if (!obj) {
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
} else {
T(YAFFS_TRACE_OS,
(KERN_DEBUG
"yaffs_file_write about to write writing %d bytes"
"to object %d at %d\n",
n, obj->objectId, ipos));
}
nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
n, nWritten, ipos));
if (nWritten > 0) {
ipos += nWritten;
*pos = ipos;
if (ipos > inode->i_size) {
inode->i_size = ipos;
inode->i_blocks = (ipos + 511) >> 9;
T(YAFFS_TRACE_OS,
(KERN_DEBUG
"yaffs_file_write size updated to %d bytes, "
"%d blocks\n",
ipos, (int)(inode->i_blocks)));
}
}
yaffs_GrossUnlock(dev);
return nWritten == 0 ? -ENOSPC : nWritten;
}
static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
{
yaffs_Object *obj;
yaffs_Device *dev;
struct inode *inode = f->f_dentry->d_inode;
unsigned long offset, curoffs;
struct list_head *i;
yaffs_Object *l;
char name[YAFFS_MAX_NAME_LENGTH + 1];
obj = yaffs_DentryToObject(f->f_dentry);
dev = obj->myDev;
yaffs_GrossLock(dev);
offset = f->f_pos;
T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
if (offset == 0) {
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
(int)inode->i_ino));
if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
< 0) {
goto out;
}
offset++;
f->f_pos++;
}
if (offset == 1) {
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
(int)f->f_dentry->d_parent->d_inode->i_ino));
if (filldir
(dirent, "..", 2, offset,
f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
goto out;
}
offset++;
f->f_pos++;
}
curoffs = 1;
/* If the directory has changed since the open or last call to
readdir, rewind to after the 2 canned entries. */
if (f->f_version != inode->i_version) {
offset = 2;
f->f_pos = offset;
f->f_version = inode->i_version;
}
list_for_each(i, &obj->variant.directoryVariant.children) {
curoffs++;
if (curoffs >= offset) {
l = list_entry(i, yaffs_Object, siblings);
yaffs_GetObjectName(l, name,
YAFFS_MAX_NAME_LENGTH + 1);
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
yaffs_GetObjectInode(l)));
if (filldir(dirent,
name,
strlen(name),
offset,
yaffs_GetObjectInode(l),
yaffs_GetObjectType(l))
< 0) {
goto up_and_out;
}
offset++;
f->f_pos++;
}
}
up_and_out:
out:
yaffs_GrossUnlock(dev);
return 0;
}
/*
* File creation. Allocate an inode, and we're done..
*/
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
dev_t rdev)
#else
static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
int rdev)
#endif
{
struct inode *inode;
yaffs_Object *obj = NULL;
yaffs_Device *dev;
yaffs_Object *parent = yaffs_InodeToObject(dir);
int error = -ENOSPC;
uid_t uid = current->fsuid;
gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
mode |= S_ISGID;
if (parent) {
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
parent->objectId, parent->variantType));
} else {
T(YAFFS_TRACE_OS,
(KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
return -EPERM;
}
T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
"mode %x dev %x\n",
dentry->d_name.name, mode, rdev));
dev = parent->myDev;
yaffs_GrossLock(dev);
switch (mode & S_IFMT) {
default:
/* Special (socket, fifo, device...) */
T(YAFFS_TRACE_OS, (KERN_DEBUG
"yaffs_mknod: making special\n"));
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
obj =
yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
gid, old_encode_dev(rdev));
#else
obj =
yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,