- /* find free FAT entry */
- lock_fat(sb);
-
- if (sbi->free_clusters == 0) {
- unlock_fat(sb);
- return -ENOSPC;
- }
-
- limit = sbi->max_cluster;
- new_dclus = sbi->prev_free + 1;
- for (count = FAT_START_ENT; count < limit; count++, new_dclus++) {
- new_dclus = new_dclus % limit;
- if (new_dclus < FAT_START_ENT)
- new_dclus = FAT_START_ENT;
-
- ret = fat_access(sb, new_dclus, -1);
- if (ret < 0) {
- unlock_fat(sb);
- return ret;
- } else if (ret == FAT_ENT_FREE)
- break;
- }
- if (count >= limit) {
- sbi->free_clusters = 0;
- unlock_fat(sb);
- return -ENOSPC;
- }
-
- ret = fat_access(sb, new_dclus, FAT_ENT_EOF);
- if (ret < 0) {
- unlock_fat(sb);
- return ret;
- }
-
- sbi->prev_free = new_dclus;
- if (sbi->free_clusters != -1)
- sbi->free_clusters--;
- fat_clusters_flush(sb);
-
- unlock_fat(sb);
-