staging: rtl8723bs: os_dep: change return type of rtw_suspend_ap_wow
[linux/fpc-iii.git] / fs / sysv / balloc.c
blob0e69dbdf7277feccbb8542823ff5641e68028b3a
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/sysv/balloc.c
5 * minix/bitmap.c
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * ext/freelists.c
9 * Copyright (C) 1992 Remy Card (card@masi.ibp.fr)
11 * xenix/alloc.c
12 * Copyright (C) 1992 Doug Evans
14 * coh/alloc.c
15 * Copyright (C) 1993 Pascal Haible, Bruno Haible
17 * sysv/balloc.c
18 * Copyright (C) 1993 Bruno Haible
20 * This file contains code for allocating/freeing blocks.
23 #include <linux/buffer_head.h>
24 #include <linux/string.h>
25 #include "sysv.h"
27 /* We don't trust the value of
28 sb->sv_sbd2->s_tfree = *sb->sv_free_blocks
29 but we nevertheless keep it up to date. */
31 static inline sysv_zone_t *get_chunk(struct super_block *sb, struct buffer_head *bh)
33 char *bh_data = bh->b_data;
35 if (SYSV_SB(sb)->s_type == FSTYPE_SYSV4)
36 return (sysv_zone_t*)(bh_data+4);
37 else
38 return (sysv_zone_t*)(bh_data+2);
41 /* NOTE NOTE NOTE: nr is a block number _as_ _stored_ _on_ _disk_ */
43 void sysv_free_block(struct super_block * sb, sysv_zone_t nr)
45 struct sysv_sb_info * sbi = SYSV_SB(sb);
46 struct buffer_head * bh;
47 sysv_zone_t *blocks = sbi->s_bcache;
48 unsigned count;
49 unsigned block = fs32_to_cpu(sbi, nr);
52 * This code does not work at all for AFS (it has a bitmap
53 * free list). As AFS is supposed to be read-only no one
54 * should call this for an AFS filesystem anyway...
56 if (sbi->s_type == FSTYPE_AFS)
57 return;
59 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
60 printk("sysv_free_block: trying to free block not in datazone\n");
61 return;
64 mutex_lock(&sbi->s_lock);
65 count = fs16_to_cpu(sbi, *sbi->s_bcache_count);
67 if (count > sbi->s_flc_size) {
68 printk("sysv_free_block: flc_count > flc_size\n");
69 mutex_unlock(&sbi->s_lock);
70 return;
72 /* If the free list head in super-block is full, it is copied
73 * into this block being freed, ditto if it's completely empty
74 * (applies only on Coherent).
76 if (count == sbi->s_flc_size || count == 0) {
77 block += sbi->s_block_base;
78 bh = sb_getblk(sb, block);
79 if (!bh) {
80 printk("sysv_free_block: getblk() failed\n");
81 mutex_unlock(&sbi->s_lock);
82 return;
84 memset(bh->b_data, 0, sb->s_blocksize);
85 *(__fs16*)bh->b_data = cpu_to_fs16(sbi, count);
86 memcpy(get_chunk(sb,bh), blocks, count * sizeof(sysv_zone_t));
87 mark_buffer_dirty(bh);
88 set_buffer_uptodate(bh);
89 brelse(bh);
90 count = 0;
92 sbi->s_bcache[count++] = nr;
94 *sbi->s_bcache_count = cpu_to_fs16(sbi, count);
95 fs32_add(sbi, sbi->s_free_blocks, 1);
96 dirty_sb(sb);
97 mutex_unlock(&sbi->s_lock);
100 sysv_zone_t sysv_new_block(struct super_block * sb)
102 struct sysv_sb_info *sbi = SYSV_SB(sb);
103 unsigned int block;
104 sysv_zone_t nr;
105 struct buffer_head * bh;
106 unsigned count;
108 mutex_lock(&sbi->s_lock);
109 count = fs16_to_cpu(sbi, *sbi->s_bcache_count);
111 if (count == 0) /* Applies only to Coherent FS */
112 goto Enospc;
113 nr = sbi->s_bcache[--count];
114 if (nr == 0) /* Applies only to Xenix FS, SystemV FS */
115 goto Enospc;
117 block = fs32_to_cpu(sbi, nr);
119 *sbi->s_bcache_count = cpu_to_fs16(sbi, count);
121 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
122 printk("sysv_new_block: new block %d is not in data zone\n",
123 block);
124 goto Enospc;
127 if (count == 0) { /* the last block continues the free list */
128 unsigned count;
130 block += sbi->s_block_base;
131 if (!(bh = sb_bread(sb, block))) {
132 printk("sysv_new_block: cannot read free-list block\n");
133 /* retry this same block next time */
134 *sbi->s_bcache_count = cpu_to_fs16(sbi, 1);
135 goto Enospc;
137 count = fs16_to_cpu(sbi, *(__fs16*)bh->b_data);
138 if (count > sbi->s_flc_size) {
139 printk("sysv_new_block: free-list block with >flc_size entries\n");
140 brelse(bh);
141 goto Enospc;
143 *sbi->s_bcache_count = cpu_to_fs16(sbi, count);
144 memcpy(sbi->s_bcache, get_chunk(sb, bh),
145 count * sizeof(sysv_zone_t));
146 brelse(bh);
148 /* Now the free list head in the superblock is valid again. */
149 fs32_add(sbi, sbi->s_free_blocks, -1);
150 dirty_sb(sb);
151 mutex_unlock(&sbi->s_lock);
152 return nr;
154 Enospc:
155 mutex_unlock(&sbi->s_lock);
156 return 0;
159 unsigned long sysv_count_free_blocks(struct super_block * sb)
161 struct sysv_sb_info * sbi = SYSV_SB(sb);
162 int sb_count;
163 int count;
164 struct buffer_head * bh = NULL;
165 sysv_zone_t *blocks;
166 unsigned block;
167 int n;
170 * This code does not work at all for AFS (it has a bitmap
171 * free list). As AFS is supposed to be read-only we just
172 * lie and say it has no free block at all.
174 if (sbi->s_type == FSTYPE_AFS)
175 return 0;
177 mutex_lock(&sbi->s_lock);
178 sb_count = fs32_to_cpu(sbi, *sbi->s_free_blocks);
180 if (0)
181 goto trust_sb;
183 /* this causes a lot of disk traffic ... */
184 count = 0;
185 n = fs16_to_cpu(sbi, *sbi->s_bcache_count);
186 blocks = sbi->s_bcache;
187 while (1) {
188 sysv_zone_t zone;
189 if (n > sbi->s_flc_size)
190 goto E2big;
191 zone = 0;
192 while (n && (zone = blocks[--n]) != 0)
193 count++;
194 if (zone == 0)
195 break;
197 block = fs32_to_cpu(sbi, zone);
198 if (bh)
199 brelse(bh);
201 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones)
202 goto Einval;
203 block += sbi->s_block_base;
204 bh = sb_bread(sb, block);
205 if (!bh)
206 goto Eio;
207 n = fs16_to_cpu(sbi, *(__fs16*)bh->b_data);
208 blocks = get_chunk(sb, bh);
210 if (bh)
211 brelse(bh);
212 if (count != sb_count)
213 goto Ecount;
214 done:
215 mutex_unlock(&sbi->s_lock);
216 return count;
218 Einval:
219 printk("sysv_count_free_blocks: new block %d is not in data zone\n",
220 block);
221 goto trust_sb;
222 Eio:
223 printk("sysv_count_free_blocks: cannot read free-list block\n");
224 goto trust_sb;
225 E2big:
226 printk("sysv_count_free_blocks: >flc_size entries in free-list block\n");
227 if (bh)
228 brelse(bh);
229 trust_sb:
230 count = sb_count;
231 goto done;
232 Ecount:
233 printk("sysv_count_free_blocks: free block count was %d, "
234 "correcting to %d\n", sb_count, count);
235 if (!sb_rdonly(sb)) {
236 *sbi->s_free_blocks = cpu_to_fs32(sbi, count);
237 dirty_sb(sb);
239 goto done;