forked from linux4sam/u-boot-at91
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaffsfs.c
3211 lines (2610 loc) · 64.6 KB
/
yaffsfs.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]>
*
* 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.
*/
#include <div64.h>
#include "yaffsfs.h"
#include "yaffs_guts.h"
#include "yaffscfg.h"
#include "yportenv.h"
#include "yaffs_trace.h"
#define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
#ifndef NULL
#define NULL ((void *)0)
#endif
/* YAFFSFS_RW_SIZE must be a power of 2 */
#define YAFFSFS_RW_SHIFT (13)
#define YAFFSFS_RW_SIZE (1<<YAFFSFS_RW_SHIFT)
/* Some forward references */
static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
const YCHAR *path,
int symDepth, int getEquiv,
struct yaffs_obj **dirOut,
int *notDir, int *loop);
static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
unsigned int yaffs_wr_attempts;
/*
* Handle management.
* There are open inodes in struct yaffsfs_Inode.
* There are open file descriptors in yaffsfs_FileDes.
* There are open handles in yaffsfs_FileDes.
*
* Things are structured this way to be like the Linux VFS model
* so that interactions with the yaffs guts calls are similar.
* That means more common code paths and less special code.
* That means better testing etc.
*
* We have 3 layers because:
* A handle is different than an fd because you can use dup()
* to create a new handle that accesses the *same* fd. The two
* handles will use the same offset (part of the fd). We only close
* down the fd when there are no more handles accessing it.
*
* More than one fd can currently access one file, but each fd
* has its own permsiions and offset.
*/
struct yaffsfs_Inode {
int count; /* Number of handles accessing this inode */
struct yaffs_obj *iObj;
};
struct yaffsfs_FileDes {
u8 reading:1;
u8 writing:1;
u8 append:1;
u8 shareRead:1;
u8 shareWrite:1;
int inodeId:12; /* Index to corresponding yaffsfs_Inode */
int handleCount:10; /* Number of handles for this fd */
loff_t position; /* current position in file */
};
struct yaffsfs_Handle {
short int fdId;
short int useCount;
};
struct yaffsfs_DirSearchContxt {
struct yaffs_dirent de; /* directory entry */
YCHAR name[NAME_MAX + 1]; /* name of directory being searched */
struct yaffs_obj *dirObj; /* ptr to directory being searched */
struct yaffs_obj *nextReturn; /* obj returned by next readddir */
struct list_head others;
int offset:20;
unsigned inUse:1;
};
static struct yaffsfs_DirSearchContxt yaffsfs_dsc[YAFFSFS_N_DSC];
static struct yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
static struct yaffsfs_FileDes yaffsfs_fd[YAFFSFS_N_HANDLES];
static struct yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
static int yaffsfs_handlesInitialised;
unsigned yaffs_set_trace(unsigned tm)
{
yaffs_trace_mask = tm;
return yaffs_trace_mask;
}
unsigned yaffs_get_trace(void)
{
return yaffs_trace_mask;
}
/*
* yaffsfs_InitHandle
* Inilitalise handle management on start-up.
*/
static void yaffsfs_InitHandles(void)
{
int i;
if (yaffsfs_handlesInitialised)
return;
memset(yaffsfs_inode, 0, sizeof(yaffsfs_inode));
memset(yaffsfs_fd, 0, sizeof(yaffsfs_fd));
memset(yaffsfs_handle, 0, sizeof(yaffsfs_handle));
memset(yaffsfs_dsc, 0, sizeof(yaffsfs_dsc));
for (i = 0; i < YAFFSFS_N_HANDLES; i++)
yaffsfs_fd[i].inodeId = -1;
for (i = 0; i < YAFFSFS_N_HANDLES; i++)
yaffsfs_handle[i].fdId = -1;
}
static struct yaffsfs_Handle *yaffsfs_HandleToPointer(int h)
{
if (h >= 0 && h < YAFFSFS_N_HANDLES)
return &yaffsfs_handle[h];
return NULL;
}
static struct yaffsfs_FileDes *yaffsfs_HandleToFileDes(int handle)
{
struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
if (h && h->useCount > 0 && h->fdId >= 0 && h->fdId < YAFFSFS_N_HANDLES)
return &yaffsfs_fd[h->fdId];
return NULL;
}
static struct yaffsfs_Inode *yaffsfs_HandleToInode(int handle)
{
struct yaffsfs_FileDes *fd = yaffsfs_HandleToFileDes(handle);
if (fd && fd->handleCount > 0 &&
fd->inodeId >= 0 && fd->inodeId < YAFFSFS_N_HANDLES)
return &yaffsfs_inode[fd->inodeId];
return NULL;
}
static struct yaffs_obj *yaffsfs_HandleToObject(int handle)
{
struct yaffsfs_Inode *in = yaffsfs_HandleToInode(handle);
if (in)
return in->iObj;
return NULL;
}
/*
* yaffsfs_FindInodeIdForObject
* Find the inode entry for an object, if it exists.
*/
static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
{
int i;
int ret = -1;
if (obj)
obj = yaffs_get_equivalent_obj(obj);
/* Look for it in open inode table */
for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
if (yaffsfs_inode[i].iObj == obj)
ret = i;
}
return ret;
}
/*
* yaffsfs_GetInodeIdForObject
* Grab an inode entry when opening a new inode.
*/
static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
{
int i;
int ret;
struct yaffsfs_Inode *in = NULL;
if (obj)
obj = yaffs_get_equivalent_obj(obj);
ret = yaffsfs_FindInodeIdForObject(obj);
for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
if (!yaffsfs_inode[i].iObj)
ret = i;
}
if (ret >= 0) {
in = &yaffsfs_inode[ret];
if (!in->iObj)
in->count = 0;
in->iObj = obj;
in->count++;
}
return ret;
}
static int yaffsfs_CountHandles(struct yaffs_obj *obj)
{
int i = yaffsfs_FindInodeIdForObject(obj);
if (i >= 0)
return yaffsfs_inode[i].count;
else
return 0;
}
static void yaffsfs_ReleaseInode(struct yaffsfs_Inode *in)
{
struct yaffs_obj *obj;
obj = in->iObj;
if (obj->unlinked)
yaffs_del_obj(obj);
obj->my_inode = NULL;
in->iObj = NULL;
}
static void yaffsfs_PutInode(int inodeId)
{
if (inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES) {
struct yaffsfs_Inode *in = &yaffsfs_inode[inodeId];
in->count--;
if (in->count <= 0) {
yaffsfs_ReleaseInode(in);
in->count = 0;
}
}
}
static int yaffsfs_NewHandle(struct yaffsfs_Handle **hptr)
{
int i;
struct yaffsfs_Handle *h;
for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
h = &yaffsfs_handle[i];
if (h->useCount < 1) {
memset(h, 0, sizeof(struct yaffsfs_Handle));
h->fdId = -1;
h->useCount = 1;
if (hptr)
*hptr = h;
return i;
}
}
return -1;
}
static int yaffsfs_NewHandleAndFileDes(void)
{
int i;
struct yaffsfs_FileDes *fd;
struct yaffsfs_Handle *h = NULL;
int handle = yaffsfs_NewHandle(&h);
if (handle < 0)
return -1;
for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
fd = &yaffsfs_fd[i];
if (fd->handleCount < 1) {
memset(fd, 0, sizeof(struct yaffsfs_FileDes));
fd->inodeId = -1;
fd->handleCount = 1;
h->fdId = i;
return handle;
}
}
/* Dump the handle because we could not get a fd */
h->useCount = 0;
return -1;
}
/*
* yaffs_get_handle
* Increase use of handle when reading/writing a file
* Also gets the file descriptor.
*/
static int yaffsfs_GetHandle(int handle)
{
struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
if (h && h->useCount > 0) {
h->useCount++;
return 0;
}
return -1;
}
/*
* yaffs_put_handle
* Let go of a handle when closing a file or aborting an open or
* ending a read or write.
*/
static int yaffsfs_PutFileDes(int fdId)
{
struct yaffsfs_FileDes *fd;
if (fdId >= 0 && fdId < YAFFSFS_N_HANDLES) {
fd = &yaffsfs_fd[fdId];
fd->handleCount--;
if (fd->handleCount < 1) {
if (fd->inodeId >= 0) {
yaffsfs_PutInode(fd->inodeId);
fd->inodeId = -1;
}
}
}
return 0;
}
static int yaffsfs_PutHandle(int handle)
{
struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
if (h && h->useCount > 0) {
h->useCount--;
if (h->useCount < 1) {
yaffsfs_PutFileDes(h->fdId);
h->fdId = -1;
}
}
return 0;
}
static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
{
struct yaffsfs_FileDes *fd;
struct yaffsfs_Handle *h;
struct yaffs_obj *obj;
int i;
for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
h = yaffsfs_HandleToPointer(i);
fd = yaffsfs_HandleToFileDes(i);
obj = yaffsfs_HandleToObject(i);
if (h && h->useCount > 0) {
h->useCount = 0;
h->fdId = 0;
}
if (fd && fd->handleCount > 0 && obj && obj->my_dev == dev) {
fd->handleCount = 0;
yaffsfs_PutInode(fd->inodeId);
fd->inodeId = -1;
}
}
}
/*
* Stuff to handle names.
*/
#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
static int yaffs_toupper(YCHAR a)
{
if (a >= 'a' && a <= 'z')
return (a - 'a') + 'A';
else
return a;
}
int yaffsfs_Match(YCHAR a, YCHAR b)
{
return (yaffs_toupper(a) == yaffs_toupper(b));
}
#else
int yaffsfs_Match(YCHAR a, YCHAR b)
{
/* case sensitive */
return (a == b);
}
#endif
int yaffsfs_IsPathDivider(YCHAR ch)
{
const YCHAR *str = YAFFS_PATH_DIVIDERS;
while (*str) {
if (*str == ch)
return 1;
str++;
}
return 0;
}
int yaffsfs_CheckNameLength(const char *name)
{
int retVal = 0;
int nameLength = yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH + 1);
if (nameLength == 0) {
yaffsfs_SetError(-ENOENT);
retVal = -1;
} else if (nameLength > YAFFS_MAX_NAME_LENGTH) {
yaffsfs_SetError(-ENAMETOOLONG);
retVal = -1;
}
return retVal;
}
static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
{
YCHAR *alt_path = NULL;
int path_length;
int i;
/*
* We don't have a definition for max path length.
* We will use 3 * max name length instead.
*/
*ret_path = NULL;
path_length = yaffs_strnlen(path, (YAFFS_MAX_NAME_LENGTH + 1) * 3 + 1);
/* If the last character is a path divider, then we need to
* trim it back so that the name look-up works properly.
* eg. /foo/new_dir/ -> /foo/newdir
* Curveball: Need to handle multiple path dividers:
* eg. /foof/sdfse///// -> /foo/sdfse
*/
if (path_length > 0 && yaffsfs_IsPathDivider(path[path_length - 1])) {
alt_path = kmalloc(path_length + 1, 0);
if (!alt_path)
return -1;
yaffs_strcpy(alt_path, path);
for (i = path_length - 1;
i >= 0 && yaffsfs_IsPathDivider(alt_path[i]); i--)
alt_path[i] = (YCHAR) 0;
}
*ret_path = alt_path;
return 0;
}
LIST_HEAD(yaffsfs_deviceList);
/*
* yaffsfs_FindDevice
* yaffsfs_FindRoot
* Scan the configuration list to find the device
* Curveballs: Should match paths that end in '/' too
* Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
*/
static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
YCHAR **restOfPath)
{
struct list_head *cfg;
const YCHAR *leftOver;
const YCHAR *p;
struct yaffs_dev *retval = NULL;
struct yaffs_dev *dev = NULL;
int thisMatchLength;
int longestMatch = -1;
int matching;
/*
* Check all configs, choose the one that:
* 1) Actually matches a prefix (ie /a amd /abc will not match
* 2) Matches the longest.
*/
list_for_each(cfg, &yaffsfs_deviceList) {
dev = list_entry(cfg, struct yaffs_dev, dev_list);
leftOver = path;
p = dev->param.name;
thisMatchLength = 0;
matching = 1;
while (matching && *p && *leftOver) {
/* Skip over any /s */
while (yaffsfs_IsPathDivider(*p))
p++;
/* Skip over any /s */
while (yaffsfs_IsPathDivider(*leftOver))
leftOver++;
/* Now match the text part */
while (matching &&
*p && !yaffsfs_IsPathDivider(*p) &&
*leftOver && !yaffsfs_IsPathDivider(*leftOver)) {
if (yaffsfs_Match(*p, *leftOver)) {
p++;
leftOver++;
thisMatchLength++;
} else {
matching = 0;
}
}
}
/* Skip over any /s in leftOver */
while (yaffsfs_IsPathDivider(*leftOver))
leftOver++;
/*Skip over any /s in p */
while (yaffsfs_IsPathDivider(*p))
p++;
/* p should now be at the end of the string if fully matched */
if (*p)
matching = 0;
if (matching && (thisMatchLength > longestMatch)) {
/* Matched prefix */
*restOfPath = (YCHAR *) leftOver;
retval = dev;
longestMatch = thisMatchLength;
}
}
return retval;
}
static int yaffsfs_CheckPath(const YCHAR *path)
{
int n = 0;
int divs = 0;
while (*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100) {
if (yaffsfs_IsPathDivider(*path)) {
n = 0;
divs++;
} else
n++;
path++;
}
return (*path) ? -1 : 0;
}
/* FindMountPoint only returns a dev entry if the path is a mount point */
static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
{
struct yaffs_dev *dev;
YCHAR *restOfPath = NULL;
dev = yaffsfs_FindDevice(path, &restOfPath);
if (dev && restOfPath && *restOfPath)
dev = NULL;
return dev;
}
static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path,
YCHAR **restOfPath)
{
struct yaffs_dev *dev;
dev = yaffsfs_FindDevice(path, restOfPath);
if (dev && dev->is_mounted)
return dev->root_dir;
return NULL;
}
static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
int symDepth, int *loop)
{
if (obj)
obj = yaffs_get_equivalent_obj(obj);
while (obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
YCHAR *alias = obj->variant.symlink_variant.alias;
if (yaffsfs_IsPathDivider(*alias))
/* Starts with a /, need to scan from root up */
obj = yaffsfs_FindObject(NULL, alias, symDepth++,
1, NULL, NULL, loop);
else
/*
* Relative to here so use the parent of the
* symlink as a start
*/
obj = yaffsfs_FindObject(obj->parent, alias, symDepth++,
1, NULL, NULL, loop);
}
return obj;
}
/*
* yaffsfs_FindDirectory
* Parse a path to determine the directory and the name within the directory.
*
* eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
*/
static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
const YCHAR *path,
YCHAR **name, int symDepth,
int *notDir, int *loop)
{
struct yaffs_obj *dir;
YCHAR *restOfPath;
YCHAR str[YAFFS_MAX_NAME_LENGTH + 1];
int i;
if (symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES) {
if (loop)
*loop = 1;
return NULL;
}
if (startDir) {
dir = startDir;
restOfPath = (YCHAR *) path;
} else
dir = yaffsfs_FindRoot(path, &restOfPath);
while (dir) {
/*
* parse off /.
* curve ball: also throw away surplus '/'
* eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
*/
while (yaffsfs_IsPathDivider(*restOfPath))
restOfPath++; /* get rid of '/' */
*name = restOfPath;
i = 0;
while (*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)) {
if (i < YAFFS_MAX_NAME_LENGTH) {
str[i] = *restOfPath;
str[i + 1] = '\0';
i++;
}
restOfPath++;
}
if (!*restOfPath)
/* got to the end of the string */
return dir;
else {
if (yaffs_strcmp(str, _Y(".")) == 0) {
/* Do nothing */
} else if (yaffs_strcmp(str, _Y("..")) == 0) {
dir = dir->parent;
} else {
dir = yaffs_find_by_name(dir, str);
dir = yaffsfs_FollowLink(dir, symDepth, loop);
if (dir && dir->variant_type !=
YAFFS_OBJECT_TYPE_DIRECTORY) {
if (notDir)
*notDir = 1;
dir = NULL;
}
}
}
}
/* directory did not exist. */
return NULL;
}
static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
const YCHAR *path,
YCHAR **name,
int symDepth,
int *notDir, int *loop)
{
return yaffsfs_DoFindDirectory(relDir, path, name, symDepth, notDir,
loop);
}
/*
* yaffsfs_FindObject turns a path for an existing object into the object
*/
static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
const YCHAR *path, int symDepth,
int getEquiv,
struct yaffs_obj **dirOut,
int *notDir, int *loop)
{
struct yaffs_obj *dir;
struct yaffs_obj *obj;
YCHAR *name;
dir =
yaffsfs_FindDirectory(relDir, path, &name, symDepth, notDir, loop);
if (dirOut)
*dirOut = dir;
if (dir && *name)
obj = yaffs_find_by_name(dir, name);
else
obj = dir;
if (getEquiv)
obj = yaffs_get_equivalent_obj(obj);
return obj;
}
/*************************************************************************
* Start of yaffsfs visible functions.
*************************************************************************/
int yaffs_dup(int handle)
{
int newHandleNumber = -1;
struct yaffsfs_FileDes *existingFD = NULL;
struct yaffsfs_Handle *existingHandle = NULL;
struct yaffsfs_Handle *newHandle = NULL;
yaffsfs_Lock();
existingHandle = yaffsfs_HandleToPointer(handle);
existingFD = yaffsfs_HandleToFileDes(handle);
if (existingFD)
newHandleNumber = yaffsfs_NewHandle(&newHandle);
if (newHandle) {
newHandle->fdId = existingHandle->fdId;
existingFD->handleCount++;
}
yaffsfs_Unlock();
if (!existingFD)
yaffsfs_SetError(-EBADF);
else if (!newHandle)
yaffsfs_SetError(-ENOMEM);
return newHandleNumber;
}
static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
{
int current_objects = dev->n_obj - dev->n_deleted_files;
if (dev->param.max_objects && current_objects > dev->param.max_objects)
return 1;
else
return 0;
}
int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
{
struct yaffs_obj *obj = NULL;
struct yaffs_obj *dir = NULL;
YCHAR *name;
int handle = -1;
struct yaffsfs_FileDes *fd = NULL;
int openDenied = 0;
int symDepth = 0;
int errorReported = 0;
int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
u8 sharedReadAllowed;
u8 sharedWriteAllowed;
u8 alreadyReading;
u8 alreadyWriting;
u8 readRequested;
u8 writeRequested;
int notDir = 0;
int loop = 0;
if (!path) {
yaffsfs_SetError(-EFAULT);
return -1;
}
if (yaffsfs_CheckPath(path) < 0) {
yaffsfs_SetError(-ENAMETOOLONG);
return -1;
}
/* O_EXCL only has meaning if O_CREAT is specified */
if (!(oflag & O_CREAT))
oflag &= ~(O_EXCL);
/* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
if ((oflag & O_CREAT) & (oflag & O_EXCL))
oflag &= ~(O_TRUNC);
/* Todo: Are there any more flag combos to sanitise ? */
/* Figure out if reading or writing is requested */
readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
yaffsfs_Lock();
handle = yaffsfs_NewHandleAndFileDes();
if (handle < 0) {
yaffsfs_SetError(-ENFILE);
errorReported = 1;
} else {
fd = yaffsfs_HandleToFileDes(handle);
/* try to find the exisiting object */
obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
if (obj &&
obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
obj = NULL;
if (obj) {
/* The file already exists or it might be a directory */
/* A directory can't be opened as a file */
if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
openDenied = 1;
yaffsfs_SetError(-EISDIR);
errorReported = 1;
}
/* Open should fail if O_CREAT and O_EXCL are specified
* for a file that exists.
*/
if (!errorReported &&
(oflag & O_EXCL) && (oflag & O_CREAT)) {
openDenied = 1;
yaffsfs_SetError(-EEXIST);
errorReported = 1;
}
/* Check file permissions */
if (readRequested && !(obj->yst_mode & S_IREAD))
openDenied = 1;
if (writeRequested && !(obj->yst_mode & S_IWRITE))
openDenied = 1;
if (!errorReported && writeRequested &&
obj->my_dev->read_only) {
openDenied = 1;
yaffsfs_SetError(-EROFS);
errorReported = 1;
}
if (openDenied && !errorReported) {
yaffsfs_SetError(-EACCES);
errorReported = 1;
}
/* Check sharing of an existing object. */
if (!openDenied) {
struct yaffsfs_FileDes *fdx;
int i;
sharedReadAllowed = 1;
sharedWriteAllowed = 1;
alreadyReading = 0;
alreadyWriting = 0;
for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
fdx = &yaffsfs_fd[i];
if (fdx->handleCount > 0 &&
fdx->inodeId >= 0 &&
yaffsfs_inode[fdx->inodeId].iObj
== obj) {
if (!fdx->shareRead)
sharedReadAllowed = 0;
if (!fdx->shareWrite)
sharedWriteAllowed = 0;
if (fdx->reading)
alreadyReading = 1;
if (fdx->writing)
alreadyWriting = 1;
}
}
if ((!sharedReadAllowed && readRequested) ||
(!shareRead && alreadyReading) ||
(!sharedWriteAllowed && writeRequested) ||
(!shareWrite && alreadyWriting)) {
openDenied = 1;
yaffsfs_SetError(-EBUSY);
errorReported = 1;
}
}
}
/* If we could not open an existing object, then let's see if
* the directory exists. If not, error.
*/
if (!obj && !errorReported) {
dir = yaffsfs_FindDirectory(NULL, path, &name, 0,
¬Dir, &loop);
if (!dir && notDir) {
yaffsfs_SetError(-ENOTDIR);
errorReported = 1;
} else if (loop) {
yaffsfs_SetError(-ELOOP);
errorReported = 1;
} else if (!dir) {
yaffsfs_SetError(-ENOENT);
errorReported = 1;
}
}
if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
/* Let's see if we can create this file */
if (dir->my_dev->read_only) {
yaffsfs_SetError(-EROFS);
errorReported = 1;
} else if (yaffsfs_TooManyObjects(dir->my_dev)) {
yaffsfs_SetError(-ENFILE);
errorReported = 1;
} else
obj = yaffs_create_file(dir, name, mode, 0, 0);
if (!obj && !errorReported) {
yaffsfs_SetError(-ENOSPC);
errorReported = 1;
}
}
if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
yaffsfs_SetError(-ENOENT);
errorReported = 1;
}
if (obj && !openDenied) {
int inodeId = yaffsfs_GetInodeIdForObject(obj);
if (inodeId < 0) {
/*
* Todo: Fix any problem if inodes run out,
* That can't happen if the number of inode
* items >= number of handles.
*/
}
fd->inodeId = inodeId;
fd->reading = readRequested;
fd->writing = writeRequested;
fd->append = (oflag & O_APPEND) ? 1 : 0;
fd->position = 0;
fd->shareRead = shareRead;
fd->shareWrite = shareWrite;
/* Hook inode to object */
obj->my_inode = (void *)&yaffsfs_inode[inodeId];
if ((oflag & O_TRUNC) && fd->writing)
yaffs_resize_file(obj, 0);
} else {
yaffsfs_PutHandle(handle);
if (!errorReported)
yaffsfs_SetError(0); /* Problem */
handle = -1;
}
}
yaffsfs_Unlock();
return handle;
}
int yaffs_open(const YCHAR *path, int oflag, int mode)
{
return yaffs_open_sharing(path, oflag, mode,
YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
}