Rev 3501 | Rev 3505 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3501 | Rev 3503 | ||
---|---|---|---|
Line 147... | Line 147... | ||
147 | /* FIXME */ |
147 | /* FIXME */ |
148 | free(block->data); |
148 | free(block->data); |
149 | free(block); |
149 | free(block); |
150 | } |
150 | } |
151 | 151 | ||
- | 152 | #define FAT1 0 |
|
- | 153 | ||
152 | #define FAT_BS(b) ((fat_bs_t *)((b)->data)) |
154 | #define FAT_BS(b) ((fat_bs_t *)((b)->data)) |
153 | 155 | ||
154 | #define FAT_CLST_RES0 0x0000 |
156 | #define FAT_CLST_RES0 0x0000 |
155 | #define FAT_CLST_RES1 0x0001 |
157 | #define FAT_CLST_RES1 0x0001 |
156 | #define FAT_CLST_FIRST 0x0002 |
158 | #define FAT_CLST_FIRST 0x0002 |
Line 888... | Line 890... | ||
888 | /* zero out the initial part of the new cluster chain */ |
890 | /* zero out the initial part of the new cluster chain */ |
889 | for (o = boundary; o < pos; o += bps) { |
891 | for (o = boundary; o < pos; o += bps) { |
890 | b = _fat_block_get(nodep->idx->dev_handle, mcl, |
892 | b = _fat_block_get(nodep->idx->dev_handle, mcl, |
891 | (o - boundary) / bps); |
893 | (o - boundary) / bps); |
892 | memset(b->data, 0, min(bps, pos - o)); |
894 | memset(b->data, 0, min(bps, pos - o)); |
893 | b->dirty = true; |
895 | b->dirty = true; /* need to sync node */ |
894 | block_put(b); |
896 | block_put(b); |
895 | } |
897 | } |
896 | } |
898 | } |
897 | 899 | ||
- | 900 | static void |
|
- | 901 | fat_mark_cluster(dev_handle_t dev_handle, unsigned fatno, fat_cluster_t clst, |
|
- | 902 | fat_cluster_t value) |
|
- | 903 | { |
|
- | 904 | /* TODO */ |
|
- | 905 | } |
|
- | 906 | ||
- | 907 | static void |
|
- | 908 | fat_alloc_shadow_clusters(dev_handle_t dev_handle, fat_cluster_t *lifo, |
|
- | 909 | unsigned nclsts) |
|
- | 910 | { |
|
- | 911 | /* TODO */ |
|
- | 912 | } |
|
- | 913 | ||
898 | static int |
914 | static int |
899 | fat_alloc_clusters(unsigned nclsts, fat_cluster_t *mcl, fat_cluster_t *lcl) |
915 | fat_alloc_clusters(dev_handle_t dev_handle, unsigned nclsts, fat_cluster_t *mcl, |
- | 916 | fat_cluster_t *lcl) |
|
900 | { |
917 | { |
- | 918 | uint16_t bps; |
|
- | 919 | uint16_t rscnt; |
|
- | 920 | uint16_t sf; |
|
- | 921 | block_t *bb, *blk; |
|
- | 922 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */ |
|
- | 923 | unsigned found = 0; /* top of the free cluster number stack */ |
|
- | 924 | unsigned b, c, cl; |
|
- | 925 | ||
- | 926 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t)); |
|
- | 927 | if (lifo) |
|
- | 928 | return ENOMEM; |
|
- | 929 | ||
- | 930 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
|
- | 931 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
|
- | 932 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
|
- | 933 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
|
- | 934 | block_put(bb); |
|
- | 935 | ||
- | 936 | /* |
|
- | 937 | * Search FAT1 for unused clusters. |
|
- | 938 | */ |
|
- | 939 | for (b = 0, cl = 0; b < sf; blk++) { |
|
- | 940 | blk = block_get(dev_handle, rscnt + b, bps); |
|
- | 941 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
|
- | 942 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
|
- | 943 | if (*clst == FAT_CLST_RES0) { |
|
- | 944 | /* |
|
- | 945 | * The cluster is free. Put it into our stack |
|
- | 946 | * of found clusters and mark it as non-free. |
|
- | 947 | */ |
|
- | 948 | lifo[found] = cl; |
|
- | 949 | if (found == 0) |
|
- | 950 | *clst = FAT_CLST_LAST1; |
|
- | 951 | else |
|
- | 952 | *clst = lifo[found - 1]; |
|
- | 953 | blk->dirty = true; /* need to sync block */ |
|
- | 954 | if (++found == nclsts) { |
|
- | 955 | /* we are almost done */ |
|
- | 956 | block_put(blk); |
|
- | 957 | /* update the shadow copies of FAT */ |
|
- | 958 | fat_alloc_shadow_clusters(dev_handle, |
|
- | 959 | lifo, nclsts); |
|
- | 960 | *mcl = lifo[found - 1]; |
|
- | 961 | *lcl = lifo[0]; |
|
- | 962 | free(lifo); |
|
- | 963 | return EOK; |
|
- | 964 | } |
|
- | 965 | } |
|
- | 966 | } |
|
- | 967 | block_put(blk); |
|
- | 968 | } |
|
- | 969 | ||
- | 970 | /* |
|
- | 971 | * We could not find enough clusters. Now we need to free the clusters |
|
- | 972 | * we have allocated so far. |
|
- | 973 | */ |
|
- | 974 | while (found--) |
|
- | 975 | fat_mark_cluster(dev_handle, FAT1, lifo[found], FAT_CLST_RES0); |
|
- | 976 | ||
- | 977 | free(lifo); |
|
901 | return ENOTSUP; /* TODO */ |
978 | return ENOSPC; |
902 | } |
979 | } |
903 | 980 | ||
904 | static void |
981 | static void |
905 | fat_append_clusters(fat_node_t *nodep, fat_cluster_t mcl) |
982 | fat_append_clusters(fat_node_t *nodep, fat_cluster_t mcl) |
906 | { |
983 | { |
Line 984... | Line 1061... | ||
984 | fat_cluster_t mcl, lcl; |
1061 | fat_cluster_t mcl, lcl; |
985 | 1062 | ||
986 | nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) / |
1063 | nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) / |
987 | bps * spc; |
1064 | bps * spc; |
988 | /* create an independent chain of nclsts clusters in all FATs */ |
1065 | /* create an independent chain of nclsts clusters in all FATs */ |
989 | status = fat_alloc_clusters(nclsts, &mcl, &lcl); |
1066 | status = fat_alloc_clusters(dev_handle, nclsts, &mcl, &lcl); |
990 | if (status != EOK) { |
1067 | if (status != EOK) { |
991 | /* could not allocate a chain of nclsts clusters */ |
1068 | /* could not allocate a chain of nclsts clusters */ |
992 | fat_node_put(nodep); |
1069 | fat_node_put(nodep); |
993 | ipc_answer_0(callid, status); |
1070 | ipc_answer_0(callid, status); |
994 | ipc_answer_0(rid, status); |
1071 | ipc_answer_0(rid, status); |