udf_allocation.c revision 1.37 1 1.37 reinoud /* $NetBSD: udf_allocation.c,v 1.37 2014/12/03 21:34:55 reinoud Exp $ */
2 1.1 reinoud
3 1.1 reinoud /*
4 1.1 reinoud * Copyright (c) 2006, 2008 Reinoud Zandijk
5 1.1 reinoud * All rights reserved.
6 1.1 reinoud *
7 1.1 reinoud * Redistribution and use in source and binary forms, with or without
8 1.1 reinoud * modification, are permitted provided that the following conditions
9 1.1 reinoud * are met:
10 1.1 reinoud * 1. Redistributions of source code must retain the above copyright
11 1.1 reinoud * notice, this list of conditions and the following disclaimer.
12 1.1 reinoud * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 reinoud * notice, this list of conditions and the following disclaimer in the
14 1.1 reinoud * documentation and/or other materials provided with the distribution.
15 1.1 reinoud *
16 1.1 reinoud * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 reinoud * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 reinoud * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 reinoud * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 reinoud * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 reinoud * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 reinoud * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 reinoud * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 reinoud * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 reinoud * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 reinoud *
27 1.1 reinoud */
28 1.1 reinoud
29 1.1 reinoud #include <sys/cdefs.h>
30 1.1 reinoud #ifndef lint
31 1.37 reinoud __KERNEL_RCSID(0, "$NetBSD: udf_allocation.c,v 1.37 2014/12/03 21:34:55 reinoud Exp $");
32 1.1 reinoud #endif /* not lint */
33 1.1 reinoud
34 1.1 reinoud
35 1.1 reinoud #if defined(_KERNEL_OPT)
36 1.1 reinoud #include "opt_compat_netbsd.h"
37 1.1 reinoud #endif
38 1.1 reinoud
39 1.1 reinoud /* TODO strip */
40 1.1 reinoud #include <sys/param.h>
41 1.1 reinoud #include <sys/systm.h>
42 1.1 reinoud #include <sys/sysctl.h>
43 1.1 reinoud #include <sys/namei.h>
44 1.1 reinoud #include <sys/proc.h>
45 1.1 reinoud #include <sys/kernel.h>
46 1.1 reinoud #include <sys/vnode.h>
47 1.1 reinoud #include <miscfs/genfs/genfs_node.h>
48 1.1 reinoud #include <sys/mount.h>
49 1.1 reinoud #include <sys/buf.h>
50 1.1 reinoud #include <sys/file.h>
51 1.1 reinoud #include <sys/device.h>
52 1.1 reinoud #include <sys/disklabel.h>
53 1.1 reinoud #include <sys/ioctl.h>
54 1.1 reinoud #include <sys/malloc.h>
55 1.1 reinoud #include <sys/dirent.h>
56 1.1 reinoud #include <sys/stat.h>
57 1.1 reinoud #include <sys/conf.h>
58 1.1 reinoud #include <sys/kauth.h>
59 1.1 reinoud #include <sys/kthread.h>
60 1.1 reinoud #include <dev/clock_subr.h>
61 1.1 reinoud
62 1.1 reinoud #include <fs/udf/ecma167-udf.h>
63 1.1 reinoud #include <fs/udf/udf_mount.h>
64 1.1 reinoud
65 1.1 reinoud #include "udf.h"
66 1.1 reinoud #include "udf_subr.h"
67 1.1 reinoud #include "udf_bswap.h"
68 1.1 reinoud
69 1.1 reinoud
70 1.1 reinoud #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
71 1.1 reinoud
72 1.1 reinoud static void udf_record_allocation_in_node(struct udf_mount *ump,
73 1.1 reinoud struct buf *buf, uint16_t vpart_num, uint64_t *mapping,
74 1.1 reinoud struct long_ad *node_ad_cpy);
75 1.1 reinoud
76 1.28 reinoud static void udf_collect_free_space_for_vpart(struct udf_mount *ump,
77 1.28 reinoud uint16_t vpart_num, uint32_t num_lb);
78 1.28 reinoud
79 1.31 reinoud static int udf_ads_merge(uint32_t max_len, uint32_t lb_size, struct long_ad *a1, struct long_ad *a2);
80 1.28 reinoud static void udf_wipe_adslots(struct udf_node *udf_node);
81 1.28 reinoud static void udf_count_alloc_exts(struct udf_node *udf_node);
82 1.28 reinoud
83 1.1 reinoud
84 1.1 reinoud /* --------------------------------------------------------------------- */
85 1.11 reinoud
86 1.12 reinoud #if 0
87 1.1 reinoud #if 1
88 1.1 reinoud static void
89 1.1 reinoud udf_node_dump(struct udf_node *udf_node) {
90 1.1 reinoud struct file_entry *fe;
91 1.1 reinoud struct extfile_entry *efe;
92 1.1 reinoud struct icb_tag *icbtag;
93 1.11 reinoud struct long_ad s_ad;
94 1.1 reinoud uint64_t inflen;
95 1.11 reinoud uint32_t icbflags, addr_type;
96 1.1 reinoud uint32_t len, lb_num;
97 1.11 reinoud uint32_t flags;
98 1.1 reinoud int part_num;
99 1.11 reinoud int lb_size, eof, slot;
100 1.1 reinoud
101 1.10 reinoud if ((udf_verbose & UDF_DEBUG_NODEDUMP) == 0)
102 1.1 reinoud return;
103 1.1 reinoud
104 1.1 reinoud lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
105 1.1 reinoud
106 1.1 reinoud fe = udf_node->fe;
107 1.1 reinoud efe = udf_node->efe;
108 1.1 reinoud if (fe) {
109 1.1 reinoud icbtag = &fe->icbtag;
110 1.1 reinoud inflen = udf_rw64(fe->inf_len);
111 1.1 reinoud } else {
112 1.1 reinoud icbtag = &efe->icbtag;
113 1.1 reinoud inflen = udf_rw64(efe->inf_len);
114 1.1 reinoud }
115 1.1 reinoud
116 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
117 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
118 1.1 reinoud
119 1.11 reinoud printf("udf_node_dump %p :\n", udf_node);
120 1.1 reinoud
121 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
122 1.11 reinoud printf("\tIntern alloc, len = %"PRIu64"\n", inflen);
123 1.1 reinoud return;
124 1.1 reinoud }
125 1.1 reinoud
126 1.11 reinoud printf("\tInflen = %"PRIu64"\n", inflen);
127 1.11 reinoud printf("\t\t");
128 1.1 reinoud
129 1.11 reinoud slot = 0;
130 1.11 reinoud for (;;) {
131 1.11 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
132 1.11 reinoud if (eof)
133 1.11 reinoud break;
134 1.11 reinoud part_num = udf_rw16(s_ad.loc.part_num);
135 1.11 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
136 1.11 reinoud len = udf_rw32(s_ad.len);
137 1.11 reinoud flags = UDF_EXT_FLAGS(len);
138 1.11 reinoud len = UDF_EXT_LEN(len);
139 1.1 reinoud
140 1.1 reinoud printf("[");
141 1.1 reinoud if (part_num >= 0)
142 1.1 reinoud printf("part %d, ", part_num);
143 1.1 reinoud printf("lb_num %d, len %d", lb_num, len);
144 1.1 reinoud if (flags)
145 1.10 reinoud printf(", flags %d", flags>>30);
146 1.1 reinoud printf("] ");
147 1.11 reinoud
148 1.11 reinoud if (flags == UDF_EXT_REDIRECT) {
149 1.11 reinoud printf("\n\textent END\n\tallocation extent\n\t\t");
150 1.11 reinoud }
151 1.11 reinoud
152 1.11 reinoud slot++;
153 1.1 reinoud }
154 1.11 reinoud printf("\n\tl_ad END\n\n");
155 1.1 reinoud }
156 1.1 reinoud #else
157 1.1 reinoud #define udf_node_dump(a)
158 1.1 reinoud #endif
159 1.1 reinoud
160 1.9 reinoud
161 1.9 reinoud static void
162 1.9 reinoud udf_assert_allocated(struct udf_mount *ump, uint16_t vpart_num,
163 1.9 reinoud uint32_t lb_num, uint32_t num_lb)
164 1.9 reinoud {
165 1.9 reinoud struct udf_bitmap *bitmap;
166 1.9 reinoud struct part_desc *pdesc;
167 1.9 reinoud uint32_t ptov;
168 1.9 reinoud uint32_t bitval;
169 1.9 reinoud uint8_t *bpos;
170 1.9 reinoud int bit;
171 1.9 reinoud int phys_part;
172 1.9 reinoud int ok;
173 1.9 reinoud
174 1.10 reinoud DPRINTF(PARANOIA, ("udf_assert_allocated: check virt lbnum %d "
175 1.9 reinoud "part %d + %d sect\n", lb_num, vpart_num, num_lb));
176 1.9 reinoud
177 1.9 reinoud /* get partition backing up this vpart_num */
178 1.9 reinoud pdesc = ump->partitions[ump->vtop[vpart_num]];
179 1.9 reinoud
180 1.9 reinoud switch (ump->vtop_tp[vpart_num]) {
181 1.9 reinoud case UDF_VTOP_TYPE_PHYS :
182 1.9 reinoud case UDF_VTOP_TYPE_SPARABLE :
183 1.9 reinoud /* free space to freed or unallocated space bitmap */
184 1.9 reinoud ptov = udf_rw32(pdesc->start_loc);
185 1.9 reinoud phys_part = ump->vtop[vpart_num];
186 1.9 reinoud
187 1.9 reinoud /* use unallocated bitmap */
188 1.9 reinoud bitmap = &ump->part_unalloc_bits[phys_part];
189 1.9 reinoud
190 1.9 reinoud /* if no bitmaps are defined, bail out */
191 1.9 reinoud if (bitmap->bits == NULL)
192 1.9 reinoud break;
193 1.9 reinoud
194 1.9 reinoud /* check bits */
195 1.9 reinoud KASSERT(bitmap->bits);
196 1.9 reinoud ok = 1;
197 1.9 reinoud bpos = bitmap->bits + lb_num/8;
198 1.9 reinoud bit = lb_num % 8;
199 1.9 reinoud while (num_lb > 0) {
200 1.9 reinoud bitval = (1 << bit);
201 1.9 reinoud DPRINTF(PARANOIA, ("XXX : check %d, %p, bit %d\n",
202 1.9 reinoud lb_num, bpos, bit));
203 1.9 reinoud KASSERT(bitmap->bits + lb_num/8 == bpos);
204 1.9 reinoud if (*bpos & bitval) {
205 1.9 reinoud printf("\tlb_num %d is NOT marked busy\n",
206 1.9 reinoud lb_num);
207 1.9 reinoud ok = 0;
208 1.9 reinoud }
209 1.9 reinoud lb_num++; num_lb--;
210 1.9 reinoud bit = (bit + 1) % 8;
211 1.9 reinoud if (bit == 0)
212 1.9 reinoud bpos++;
213 1.9 reinoud }
214 1.9 reinoud if (!ok) {
215 1.9 reinoud /* KASSERT(0); */
216 1.9 reinoud }
217 1.9 reinoud
218 1.9 reinoud break;
219 1.9 reinoud case UDF_VTOP_TYPE_VIRT :
220 1.9 reinoud /* TODO check space */
221 1.9 reinoud KASSERT(num_lb == 1);
222 1.9 reinoud break;
223 1.9 reinoud case UDF_VTOP_TYPE_META :
224 1.9 reinoud /* TODO check space in the metadata bitmap */
225 1.9 reinoud default:
226 1.9 reinoud /* not implemented */
227 1.9 reinoud break;
228 1.9 reinoud }
229 1.9 reinoud }
230 1.9 reinoud
231 1.9 reinoud
232 1.1 reinoud static void
233 1.1 reinoud udf_node_sanity_check(struct udf_node *udf_node,
234 1.17 reinoud uint64_t *cnt_inflen, uint64_t *cnt_logblksrec)
235 1.17 reinoud {
236 1.17 reinoud union dscrptr *dscr;
237 1.1 reinoud struct file_entry *fe;
238 1.1 reinoud struct extfile_entry *efe;
239 1.1 reinoud struct icb_tag *icbtag;
240 1.11 reinoud struct long_ad s_ad;
241 1.1 reinoud uint64_t inflen, logblksrec;
242 1.11 reinoud uint32_t icbflags, addr_type;
243 1.11 reinoud uint32_t len, lb_num, l_ea, l_ad, max_l_ad;
244 1.9 reinoud uint16_t part_num;
245 1.17 reinoud uint8_t *data_pos;
246 1.11 reinoud int dscr_size, lb_size, flags, whole_lb;
247 1.17 reinoud int i, slot, eof;
248 1.1 reinoud
249 1.9 reinoud // KASSERT(mutex_owned(&udf_node->ump->allocate_mutex));
250 1.1 reinoud
251 1.10 reinoud if (1)
252 1.10 reinoud udf_node_dump(udf_node);
253 1.10 reinoud
254 1.1 reinoud lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
255 1.1 reinoud
256 1.1 reinoud fe = udf_node->fe;
257 1.1 reinoud efe = udf_node->efe;
258 1.1 reinoud if (fe) {
259 1.17 reinoud dscr = (union dscrptr *) fe;
260 1.17 reinoud icbtag = &fe->icbtag;
261 1.17 reinoud inflen = udf_rw64(fe->inf_len);
262 1.11 reinoud dscr_size = sizeof(struct file_entry) -1;
263 1.1 reinoud logblksrec = udf_rw64(fe->logblks_rec);
264 1.11 reinoud l_ad = udf_rw32(fe->l_ad);
265 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
266 1.1 reinoud } else {
267 1.17 reinoud dscr = (union dscrptr *) efe;
268 1.17 reinoud icbtag = &efe->icbtag;
269 1.17 reinoud inflen = udf_rw64(efe->inf_len);
270 1.11 reinoud dscr_size = sizeof(struct extfile_entry) -1;
271 1.1 reinoud logblksrec = udf_rw64(efe->logblks_rec);
272 1.11 reinoud l_ad = udf_rw32(efe->l_ad);
273 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
274 1.1 reinoud }
275 1.17 reinoud data_pos = (uint8_t *) dscr + dscr_size + l_ea;
276 1.11 reinoud max_l_ad = lb_size - dscr_size - l_ea;
277 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
278 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
279 1.1 reinoud
280 1.17 reinoud /* check if tail is zero */
281 1.17 reinoud DPRINTF(PARANOIA, ("Sanity check blank tail\n"));
282 1.17 reinoud for (i = l_ad; i < max_l_ad; i++) {
283 1.17 reinoud if (data_pos[i] != 0)
284 1.17 reinoud printf( "sanity_check: violation: node byte %d "
285 1.17 reinoud "has value %d\n", i, data_pos[i]);
286 1.17 reinoud }
287 1.17 reinoud
288 1.1 reinoud /* reset counters */
289 1.1 reinoud *cnt_inflen = 0;
290 1.1 reinoud *cnt_logblksrec = 0;
291 1.1 reinoud
292 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
293 1.1 reinoud KASSERT(l_ad <= max_l_ad);
294 1.1 reinoud KASSERT(l_ad == inflen);
295 1.1 reinoud *cnt_inflen = inflen;
296 1.1 reinoud return;
297 1.1 reinoud }
298 1.1 reinoud
299 1.1 reinoud /* start counting */
300 1.1 reinoud whole_lb = 1;
301 1.11 reinoud slot = 0;
302 1.11 reinoud for (;;) {
303 1.11 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
304 1.11 reinoud if (eof)
305 1.11 reinoud break;
306 1.1 reinoud KASSERT(whole_lb == 1);
307 1.11 reinoud
308 1.11 reinoud part_num = udf_rw16(s_ad.loc.part_num);
309 1.11 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
310 1.11 reinoud len = udf_rw32(s_ad.len);
311 1.11 reinoud flags = UDF_EXT_FLAGS(len);
312 1.11 reinoud len = UDF_EXT_LEN(len);
313 1.11 reinoud
314 1.6 reinoud if (flags != UDF_EXT_REDIRECT) {
315 1.6 reinoud *cnt_inflen += len;
316 1.6 reinoud if (flags == UDF_EXT_ALLOCATED) {
317 1.6 reinoud *cnt_logblksrec += (len + lb_size -1) / lb_size;
318 1.6 reinoud }
319 1.6 reinoud } else {
320 1.6 reinoud KASSERT(len == lb_size);
321 1.1 reinoud }
322 1.9 reinoud /* check allocation */
323 1.9 reinoud if (flags == UDF_EXT_ALLOCATED)
324 1.9 reinoud udf_assert_allocated(udf_node->ump, part_num, lb_num,
325 1.9 reinoud (len + lb_size - 1) / lb_size);
326 1.8 reinoud
327 1.8 reinoud /* check whole lb */
328 1.1 reinoud whole_lb = ((len % lb_size) == 0);
329 1.11 reinoud
330 1.11 reinoud slot++;
331 1.1 reinoud }
332 1.1 reinoud /* rest should be zero (ad_off > l_ad < max_l_ad - adlen) */
333 1.1 reinoud
334 1.1 reinoud KASSERT(*cnt_inflen == inflen);
335 1.1 reinoud KASSERT(*cnt_logblksrec == logblksrec);
336 1.1 reinoud
337 1.9 reinoud // KASSERT(mutex_owned(&udf_node->ump->allocate_mutex));
338 1.1 reinoud }
339 1.1 reinoud #else
340 1.12 reinoud static void
341 1.12 reinoud udf_node_sanity_check(struct udf_node *udf_node,
342 1.12 reinoud uint64_t *cnt_inflen, uint64_t *cnt_logblksrec) {
343 1.12 reinoud struct file_entry *fe;
344 1.12 reinoud struct extfile_entry *efe;
345 1.12 reinoud uint64_t inflen, logblksrec;
346 1.12 reinoud
347 1.12 reinoud fe = udf_node->fe;
348 1.12 reinoud efe = udf_node->efe;
349 1.12 reinoud if (fe) {
350 1.12 reinoud inflen = udf_rw64(fe->inf_len);
351 1.12 reinoud logblksrec = udf_rw64(fe->logblks_rec);
352 1.12 reinoud } else {
353 1.12 reinoud inflen = udf_rw64(efe->inf_len);
354 1.12 reinoud logblksrec = udf_rw64(efe->logblks_rec);
355 1.12 reinoud }
356 1.12 reinoud *cnt_logblksrec = logblksrec;
357 1.12 reinoud *cnt_inflen = inflen;
358 1.12 reinoud }
359 1.1 reinoud #endif
360 1.1 reinoud
361 1.1 reinoud /* --------------------------------------------------------------------- */
362 1.1 reinoud
363 1.26 reinoud void
364 1.26 reinoud udf_calc_freespace(struct udf_mount *ump, uint64_t *sizeblks, uint64_t *freeblks)
365 1.26 reinoud {
366 1.26 reinoud struct logvol_int_desc *lvid;
367 1.26 reinoud uint32_t *pos1, *pos2;
368 1.26 reinoud int vpart, num_vpart;
369 1.26 reinoud
370 1.26 reinoud lvid = ump->logvol_integrity;
371 1.26 reinoud *freeblks = *sizeblks = 0;
372 1.26 reinoud
373 1.26 reinoud /*
374 1.26 reinoud * Sequentials media report free space directly (CD/DVD/BD-R), for the
375 1.26 reinoud * other media we need the logical volume integrity.
376 1.26 reinoud *
377 1.26 reinoud * We sum all free space up here regardless of type.
378 1.26 reinoud */
379 1.26 reinoud
380 1.26 reinoud KASSERT(lvid);
381 1.26 reinoud num_vpart = udf_rw32(lvid->num_part);
382 1.26 reinoud
383 1.26 reinoud if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
384 1.26 reinoud /* use track info directly summing if there are 2 open */
385 1.26 reinoud /* XXX assumption at most two tracks open */
386 1.26 reinoud *freeblks = ump->data_track.free_blocks;
387 1.26 reinoud if (ump->data_track.tracknr != ump->metadata_track.tracknr)
388 1.26 reinoud *freeblks += ump->metadata_track.free_blocks;
389 1.26 reinoud *sizeblks = ump->discinfo.last_possible_lba;
390 1.26 reinoud } else {
391 1.26 reinoud /* free and used space for mountpoint based on logvol integrity */
392 1.26 reinoud for (vpart = 0; vpart < num_vpart; vpart++) {
393 1.26 reinoud pos1 = &lvid->tables[0] + vpart;
394 1.26 reinoud pos2 = &lvid->tables[0] + num_vpart + vpart;
395 1.26 reinoud if (udf_rw32(*pos1) != (uint32_t) -1) {
396 1.26 reinoud *freeblks += udf_rw32(*pos1);
397 1.26 reinoud *sizeblks += udf_rw32(*pos2);
398 1.26 reinoud }
399 1.26 reinoud }
400 1.26 reinoud }
401 1.26 reinoud /* adjust for accounted uncommitted blocks */
402 1.26 reinoud for (vpart = 0; vpart < num_vpart; vpart++)
403 1.26 reinoud *freeblks -= ump->uncommitted_lbs[vpart];
404 1.26 reinoud
405 1.26 reinoud if (*freeblks > UDF_DISC_SLACK) {
406 1.26 reinoud *freeblks -= UDF_DISC_SLACK;
407 1.26 reinoud } else {
408 1.26 reinoud *freeblks = 0;
409 1.26 reinoud }
410 1.26 reinoud }
411 1.26 reinoud
412 1.26 reinoud
413 1.26 reinoud static void
414 1.26 reinoud udf_calc_vpart_freespace(struct udf_mount *ump, uint16_t vpart_num, uint64_t *freeblks)
415 1.26 reinoud {
416 1.26 reinoud struct logvol_int_desc *lvid;
417 1.26 reinoud uint32_t *pos1;
418 1.26 reinoud
419 1.26 reinoud lvid = ump->logvol_integrity;
420 1.26 reinoud *freeblks = 0;
421 1.26 reinoud
422 1.26 reinoud /*
423 1.26 reinoud * Sequentials media report free space directly (CD/DVD/BD-R), for the
424 1.26 reinoud * other media we need the logical volume integrity.
425 1.26 reinoud *
426 1.26 reinoud * We sum all free space up here regardless of type.
427 1.26 reinoud */
428 1.26 reinoud
429 1.26 reinoud KASSERT(lvid);
430 1.26 reinoud if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
431 1.26 reinoud /* XXX assumption at most two tracks open */
432 1.26 reinoud if (vpart_num == ump->data_part) {
433 1.26 reinoud *freeblks = ump->data_track.free_blocks;
434 1.26 reinoud } else {
435 1.26 reinoud *freeblks = ump->metadata_track.free_blocks;
436 1.26 reinoud }
437 1.26 reinoud } else {
438 1.26 reinoud /* free and used space for mountpoint based on logvol integrity */
439 1.26 reinoud pos1 = &lvid->tables[0] + vpart_num;
440 1.26 reinoud if (udf_rw32(*pos1) != (uint32_t) -1)
441 1.26 reinoud *freeblks += udf_rw32(*pos1);
442 1.26 reinoud }
443 1.26 reinoud
444 1.26 reinoud /* adjust for accounted uncommitted blocks */
445 1.28 reinoud if (*freeblks > ump->uncommitted_lbs[vpart_num]) {
446 1.28 reinoud *freeblks -= ump->uncommitted_lbs[vpart_num];
447 1.28 reinoud } else {
448 1.28 reinoud *freeblks = 0;
449 1.28 reinoud }
450 1.26 reinoud }
451 1.26 reinoud
452 1.26 reinoud /* --------------------------------------------------------------------- */
453 1.26 reinoud
454 1.1 reinoud int
455 1.1 reinoud udf_translate_vtop(struct udf_mount *ump, struct long_ad *icb_loc,
456 1.1 reinoud uint32_t *lb_numres, uint32_t *extres)
457 1.1 reinoud {
458 1.1 reinoud struct part_desc *pdesc;
459 1.1 reinoud struct spare_map_entry *sme;
460 1.1 reinoud struct long_ad s_icb_loc;
461 1.1 reinoud uint64_t foffset, end_foffset;
462 1.1 reinoud uint32_t lb_size, len;
463 1.1 reinoud uint32_t lb_num, lb_rel, lb_packet;
464 1.1 reinoud uint32_t udf_rw32_lbmap, ext_offset;
465 1.1 reinoud uint16_t vpart;
466 1.1 reinoud int rel, part, error, eof, slot, flags;
467 1.1 reinoud
468 1.1 reinoud assert(ump && icb_loc && lb_numres);
469 1.1 reinoud
470 1.1 reinoud vpart = udf_rw16(icb_loc->loc.part_num);
471 1.1 reinoud lb_num = udf_rw32(icb_loc->loc.lb_num);
472 1.1 reinoud if (vpart > UDF_VTOP_RAWPART)
473 1.1 reinoud return EINVAL;
474 1.1 reinoud
475 1.1 reinoud translate_again:
476 1.1 reinoud part = ump->vtop[vpart];
477 1.1 reinoud pdesc = ump->partitions[part];
478 1.1 reinoud
479 1.1 reinoud switch (ump->vtop_tp[vpart]) {
480 1.1 reinoud case UDF_VTOP_TYPE_RAW :
481 1.1 reinoud /* 1:1 to the end of the device */
482 1.1 reinoud *lb_numres = lb_num;
483 1.1 reinoud *extres = INT_MAX;
484 1.1 reinoud return 0;
485 1.1 reinoud case UDF_VTOP_TYPE_PHYS :
486 1.1 reinoud /* transform into its disc logical block */
487 1.1 reinoud if (lb_num > udf_rw32(pdesc->part_len))
488 1.1 reinoud return EINVAL;
489 1.1 reinoud *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
490 1.1 reinoud
491 1.1 reinoud /* extent from here to the end of the partition */
492 1.1 reinoud *extres = udf_rw32(pdesc->part_len) - lb_num;
493 1.1 reinoud return 0;
494 1.1 reinoud case UDF_VTOP_TYPE_VIRT :
495 1.1 reinoud /* only maps one logical block, lookup in VAT */
496 1.1 reinoud if (lb_num >= ump->vat_entries) /* XXX > or >= ? */
497 1.1 reinoud return EINVAL;
498 1.1 reinoud
499 1.1 reinoud /* lookup in virtual allocation table file */
500 1.1 reinoud mutex_enter(&ump->allocate_mutex);
501 1.1 reinoud error = udf_vat_read(ump->vat_node,
502 1.1 reinoud (uint8_t *) &udf_rw32_lbmap, 4,
503 1.1 reinoud ump->vat_offset + lb_num * 4);
504 1.1 reinoud mutex_exit(&ump->allocate_mutex);
505 1.1 reinoud
506 1.1 reinoud if (error)
507 1.1 reinoud return error;
508 1.1 reinoud
509 1.1 reinoud lb_num = udf_rw32(udf_rw32_lbmap);
510 1.1 reinoud
511 1.1 reinoud /* transform into its disc logical block */
512 1.1 reinoud if (lb_num > udf_rw32(pdesc->part_len))
513 1.1 reinoud return EINVAL;
514 1.1 reinoud *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
515 1.1 reinoud
516 1.1 reinoud /* just one logical block */
517 1.1 reinoud *extres = 1;
518 1.1 reinoud return 0;
519 1.1 reinoud case UDF_VTOP_TYPE_SPARABLE :
520 1.1 reinoud /* check if the packet containing the lb_num is remapped */
521 1.1 reinoud lb_packet = lb_num / ump->sparable_packet_size;
522 1.1 reinoud lb_rel = lb_num % ump->sparable_packet_size;
523 1.1 reinoud
524 1.1 reinoud for (rel = 0; rel < udf_rw16(ump->sparing_table->rt_l); rel++) {
525 1.1 reinoud sme = &ump->sparing_table->entries[rel];
526 1.1 reinoud if (lb_packet == udf_rw32(sme->org)) {
527 1.1 reinoud /* NOTE maps to absolute disc logical block! */
528 1.1 reinoud *lb_numres = udf_rw32(sme->map) + lb_rel;
529 1.1 reinoud *extres = ump->sparable_packet_size - lb_rel;
530 1.1 reinoud return 0;
531 1.1 reinoud }
532 1.1 reinoud }
533 1.1 reinoud
534 1.1 reinoud /* transform into its disc logical block */
535 1.1 reinoud if (lb_num > udf_rw32(pdesc->part_len))
536 1.1 reinoud return EINVAL;
537 1.1 reinoud *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
538 1.1 reinoud
539 1.1 reinoud /* rest of block */
540 1.1 reinoud *extres = ump->sparable_packet_size - lb_rel;
541 1.1 reinoud return 0;
542 1.1 reinoud case UDF_VTOP_TYPE_META :
543 1.1 reinoud /* we have to look into the file's allocation descriptors */
544 1.1 reinoud
545 1.1 reinoud /* use metadatafile allocation mutex */
546 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
547 1.1 reinoud
548 1.1 reinoud UDF_LOCK_NODE(ump->metadata_node, 0);
549 1.1 reinoud
550 1.1 reinoud /* get first overlapping extent */
551 1.1 reinoud foffset = 0;
552 1.1 reinoud slot = 0;
553 1.1 reinoud for (;;) {
554 1.1 reinoud udf_get_adslot(ump->metadata_node,
555 1.1 reinoud slot, &s_icb_loc, &eof);
556 1.5 reinoud DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, "
557 1.5 reinoud "len = %d, lb_num = %d, part = %d\n",
558 1.5 reinoud slot, eof,
559 1.5 reinoud UDF_EXT_FLAGS(udf_rw32(s_icb_loc.len)),
560 1.5 reinoud UDF_EXT_LEN(udf_rw32(s_icb_loc.len)),
561 1.5 reinoud udf_rw32(s_icb_loc.loc.lb_num),
562 1.5 reinoud udf_rw16(s_icb_loc.loc.part_num)));
563 1.1 reinoud if (eof) {
564 1.1 reinoud DPRINTF(TRANSLATE,
565 1.1 reinoud ("Meta partition translation "
566 1.1 reinoud "failed: can't seek location\n"));
567 1.1 reinoud UDF_UNLOCK_NODE(ump->metadata_node, 0);
568 1.1 reinoud return EINVAL;
569 1.1 reinoud }
570 1.1 reinoud len = udf_rw32(s_icb_loc.len);
571 1.1 reinoud flags = UDF_EXT_FLAGS(len);
572 1.1 reinoud len = UDF_EXT_LEN(len);
573 1.1 reinoud
574 1.5 reinoud if (flags == UDF_EXT_REDIRECT) {
575 1.5 reinoud slot++;
576 1.5 reinoud continue;
577 1.5 reinoud }
578 1.5 reinoud
579 1.1 reinoud end_foffset = foffset + len;
580 1.1 reinoud
581 1.34 reinoud if (end_foffset > (uint64_t) lb_num * lb_size)
582 1.1 reinoud break; /* found */
583 1.5 reinoud foffset = end_foffset;
584 1.1 reinoud slot++;
585 1.1 reinoud }
586 1.1 reinoud /* found overlapping slot */
587 1.1 reinoud ext_offset = lb_num * lb_size - foffset;
588 1.1 reinoud
589 1.1 reinoud /* process extent offset */
590 1.1 reinoud lb_num = udf_rw32(s_icb_loc.loc.lb_num);
591 1.1 reinoud vpart = udf_rw16(s_icb_loc.loc.part_num);
592 1.1 reinoud lb_num += (ext_offset + lb_size -1) / lb_size;
593 1.1 reinoud ext_offset = 0;
594 1.1 reinoud
595 1.1 reinoud UDF_UNLOCK_NODE(ump->metadata_node, 0);
596 1.1 reinoud if (flags != UDF_EXT_ALLOCATED) {
597 1.1 reinoud DPRINTF(TRANSLATE, ("Metadata partition translation "
598 1.1 reinoud "failed: not allocated\n"));
599 1.1 reinoud return EINVAL;
600 1.1 reinoud }
601 1.1 reinoud
602 1.1 reinoud /*
603 1.1 reinoud * vpart and lb_num are updated, translate again since we
604 1.1 reinoud * might be mapped on sparable media
605 1.1 reinoud */
606 1.1 reinoud goto translate_again;
607 1.1 reinoud default:
608 1.1 reinoud printf("UDF vtop translation scheme %d unimplemented yet\n",
609 1.1 reinoud ump->vtop_tp[vpart]);
610 1.1 reinoud }
611 1.1 reinoud
612 1.1 reinoud return EINVAL;
613 1.1 reinoud }
614 1.1 reinoud
615 1.16 reinoud
616 1.16 reinoud /* XXX provisional primitive braindead version */
617 1.16 reinoud /* TODO use ext_res */
618 1.16 reinoud void
619 1.16 reinoud udf_translate_vtop_list(struct udf_mount *ump, uint32_t sectors,
620 1.16 reinoud uint16_t vpart_num, uint64_t *lmapping, uint64_t *pmapping)
621 1.16 reinoud {
622 1.16 reinoud struct long_ad loc;
623 1.16 reinoud uint32_t lb_numres, ext_res;
624 1.16 reinoud int sector;
625 1.16 reinoud
626 1.16 reinoud for (sector = 0; sector < sectors; sector++) {
627 1.16 reinoud memset(&loc, 0, sizeof(struct long_ad));
628 1.16 reinoud loc.loc.part_num = udf_rw16(vpart_num);
629 1.16 reinoud loc.loc.lb_num = udf_rw32(*lmapping);
630 1.16 reinoud udf_translate_vtop(ump, &loc, &lb_numres, &ext_res);
631 1.16 reinoud *pmapping = lb_numres;
632 1.16 reinoud lmapping++; pmapping++;
633 1.16 reinoud }
634 1.16 reinoud }
635 1.16 reinoud
636 1.16 reinoud
637 1.1 reinoud /* --------------------------------------------------------------------- */
638 1.1 reinoud
639 1.1 reinoud /*
640 1.1 reinoud * Translate an extent (in logical_blocks) into logical block numbers; used
641 1.1 reinoud * for read and write operations. DOESNT't check extents.
642 1.1 reinoud */
643 1.1 reinoud
644 1.1 reinoud int
645 1.1 reinoud udf_translate_file_extent(struct udf_node *udf_node,
646 1.1 reinoud uint32_t from, uint32_t num_lb,
647 1.1 reinoud uint64_t *map)
648 1.1 reinoud {
649 1.1 reinoud struct udf_mount *ump;
650 1.1 reinoud struct icb_tag *icbtag;
651 1.1 reinoud struct long_ad t_ad, s_ad;
652 1.1 reinoud uint64_t transsec;
653 1.1 reinoud uint64_t foffset, end_foffset;
654 1.1 reinoud uint32_t transsec32;
655 1.1 reinoud uint32_t lb_size;
656 1.1 reinoud uint32_t ext_offset;
657 1.1 reinoud uint32_t lb_num, len;
658 1.1 reinoud uint32_t overlap, translen;
659 1.1 reinoud uint16_t vpart_num;
660 1.1 reinoud int eof, error, flags;
661 1.1 reinoud int slot, addr_type, icbflags;
662 1.1 reinoud
663 1.1 reinoud if (!udf_node)
664 1.1 reinoud return ENOENT;
665 1.1 reinoud
666 1.1 reinoud KASSERT(num_lb > 0);
667 1.1 reinoud
668 1.1 reinoud UDF_LOCK_NODE(udf_node, 0);
669 1.1 reinoud
670 1.1 reinoud /* initialise derivative vars */
671 1.1 reinoud ump = udf_node->ump;
672 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
673 1.1 reinoud
674 1.1 reinoud if (udf_node->fe) {
675 1.1 reinoud icbtag = &udf_node->fe->icbtag;
676 1.1 reinoud } else {
677 1.1 reinoud icbtag = &udf_node->efe->icbtag;
678 1.1 reinoud }
679 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
680 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
681 1.1 reinoud
682 1.1 reinoud /* do the work */
683 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
684 1.1 reinoud *map = UDF_TRANS_INTERN;
685 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
686 1.1 reinoud return 0;
687 1.1 reinoud }
688 1.1 reinoud
689 1.1 reinoud /* find first overlapping extent */
690 1.1 reinoud foffset = 0;
691 1.1 reinoud slot = 0;
692 1.1 reinoud for (;;) {
693 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
694 1.4 reinoud DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
695 1.4 reinoud "lb_num = %d, part = %d\n", slot, eof,
696 1.4 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)),
697 1.4 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
698 1.4 reinoud udf_rw32(s_ad.loc.lb_num),
699 1.4 reinoud udf_rw16(s_ad.loc.part_num)));
700 1.1 reinoud if (eof) {
701 1.1 reinoud DPRINTF(TRANSLATE,
702 1.1 reinoud ("Translate file extent "
703 1.1 reinoud "failed: can't seek location\n"));
704 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
705 1.1 reinoud return EINVAL;
706 1.1 reinoud }
707 1.1 reinoud len = udf_rw32(s_ad.len);
708 1.1 reinoud flags = UDF_EXT_FLAGS(len);
709 1.1 reinoud len = UDF_EXT_LEN(len);
710 1.1 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
711 1.1 reinoud
712 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
713 1.1 reinoud slot++;
714 1.1 reinoud continue;
715 1.1 reinoud }
716 1.1 reinoud
717 1.1 reinoud end_foffset = foffset + len;
718 1.1 reinoud
719 1.34 reinoud if (end_foffset > (uint64_t) from * lb_size)
720 1.1 reinoud break; /* found */
721 1.1 reinoud foffset = end_foffset;
722 1.1 reinoud slot++;
723 1.1 reinoud }
724 1.1 reinoud /* found overlapping slot */
725 1.34 reinoud ext_offset = (uint64_t) from * lb_size - foffset;
726 1.1 reinoud
727 1.1 reinoud for (;;) {
728 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
729 1.4 reinoud DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
730 1.4 reinoud "lb_num = %d, part = %d\n", slot, eof,
731 1.4 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)),
732 1.4 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
733 1.4 reinoud udf_rw32(s_ad.loc.lb_num),
734 1.4 reinoud udf_rw16(s_ad.loc.part_num)));
735 1.1 reinoud if (eof) {
736 1.1 reinoud DPRINTF(TRANSLATE,
737 1.1 reinoud ("Translate file extent "
738 1.1 reinoud "failed: past eof\n"));
739 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
740 1.1 reinoud return EINVAL;
741 1.1 reinoud }
742 1.1 reinoud
743 1.1 reinoud len = udf_rw32(s_ad.len);
744 1.1 reinoud flags = UDF_EXT_FLAGS(len);
745 1.1 reinoud len = UDF_EXT_LEN(len);
746 1.1 reinoud
747 1.1 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
748 1.1 reinoud vpart_num = udf_rw16(s_ad.loc.part_num);
749 1.1 reinoud
750 1.1 reinoud end_foffset = foffset + len;
751 1.1 reinoud
752 1.1 reinoud /* process extent, don't forget to advance on ext_offset! */
753 1.1 reinoud lb_num += (ext_offset + lb_size -1) / lb_size;
754 1.1 reinoud overlap = (len - ext_offset + lb_size -1) / lb_size;
755 1.1 reinoud ext_offset = 0;
756 1.1 reinoud
757 1.1 reinoud /*
758 1.1 reinoud * note that the while(){} is nessisary for the extent that
759 1.1 reinoud * the udf_translate_vtop() returns doens't have to span the
760 1.1 reinoud * whole extent.
761 1.1 reinoud */
762 1.1 reinoud
763 1.1 reinoud overlap = MIN(overlap, num_lb);
764 1.4 reinoud while (overlap && (flags != UDF_EXT_REDIRECT)) {
765 1.1 reinoud switch (flags) {
766 1.1 reinoud case UDF_EXT_FREE :
767 1.1 reinoud case UDF_EXT_ALLOCATED_BUT_NOT_USED :
768 1.1 reinoud transsec = UDF_TRANS_ZERO;
769 1.1 reinoud translen = overlap;
770 1.1 reinoud while (overlap && num_lb && translen) {
771 1.1 reinoud *map++ = transsec;
772 1.1 reinoud lb_num++;
773 1.1 reinoud overlap--; num_lb--; translen--;
774 1.1 reinoud }
775 1.1 reinoud break;
776 1.1 reinoud case UDF_EXT_ALLOCATED :
777 1.1 reinoud t_ad.loc.lb_num = udf_rw32(lb_num);
778 1.1 reinoud t_ad.loc.part_num = udf_rw16(vpart_num);
779 1.1 reinoud error = udf_translate_vtop(ump,
780 1.1 reinoud &t_ad, &transsec32, &translen);
781 1.1 reinoud transsec = transsec32;
782 1.1 reinoud if (error) {
783 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
784 1.1 reinoud return error;
785 1.1 reinoud }
786 1.1 reinoud while (overlap && num_lb && translen) {
787 1.1 reinoud *map++ = transsec;
788 1.1 reinoud lb_num++; transsec++;
789 1.1 reinoud overlap--; num_lb--; translen--;
790 1.1 reinoud }
791 1.1 reinoud break;
792 1.4 reinoud default:
793 1.4 reinoud DPRINTF(TRANSLATE,
794 1.4 reinoud ("Translate file extent "
795 1.4 reinoud "failed: bad flags %x\n", flags));
796 1.4 reinoud UDF_UNLOCK_NODE(udf_node, 0);
797 1.4 reinoud return EINVAL;
798 1.1 reinoud }
799 1.1 reinoud }
800 1.1 reinoud if (num_lb == 0)
801 1.1 reinoud break;
802 1.1 reinoud
803 1.1 reinoud if (flags != UDF_EXT_REDIRECT)
804 1.1 reinoud foffset = end_foffset;
805 1.1 reinoud slot++;
806 1.1 reinoud }
807 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
808 1.1 reinoud
809 1.1 reinoud return 0;
810 1.1 reinoud }
811 1.1 reinoud
812 1.1 reinoud /* --------------------------------------------------------------------- */
813 1.1 reinoud
814 1.1 reinoud static int
815 1.1 reinoud udf_search_free_vatloc(struct udf_mount *ump, uint32_t *lbnumres)
816 1.1 reinoud {
817 1.1 reinoud uint32_t lb_size, lb_num, lb_map, udf_rw32_lbmap;
818 1.1 reinoud uint8_t *blob;
819 1.1 reinoud int entry, chunk, found, error;
820 1.1 reinoud
821 1.1 reinoud KASSERT(ump);
822 1.1 reinoud KASSERT(ump->logical_vol);
823 1.1 reinoud
824 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
825 1.1 reinoud blob = malloc(lb_size, M_UDFTEMP, M_WAITOK);
826 1.1 reinoud
827 1.1 reinoud /* TODO static allocation of search chunk */
828 1.1 reinoud
829 1.1 reinoud lb_num = MIN(ump->vat_entries, ump->vat_last_free_lb);
830 1.1 reinoud found = 0;
831 1.1 reinoud error = 0;
832 1.1 reinoud entry = 0;
833 1.1 reinoud do {
834 1.1 reinoud chunk = MIN(lb_size, (ump->vat_entries - lb_num) * 4);
835 1.1 reinoud if (chunk <= 0)
836 1.1 reinoud break;
837 1.1 reinoud /* load in chunk */
838 1.1 reinoud error = udf_vat_read(ump->vat_node, blob, chunk,
839 1.1 reinoud ump->vat_offset + lb_num * 4);
840 1.1 reinoud
841 1.1 reinoud if (error)
842 1.1 reinoud break;
843 1.1 reinoud
844 1.1 reinoud /* search this chunk */
845 1.1 reinoud for (entry=0; entry < chunk /4; entry++, lb_num++) {
846 1.1 reinoud udf_rw32_lbmap = *((uint32_t *) (blob + entry * 4));
847 1.1 reinoud lb_map = udf_rw32(udf_rw32_lbmap);
848 1.1 reinoud if (lb_map == 0xffffffff) {
849 1.1 reinoud found = 1;
850 1.1 reinoud break;
851 1.1 reinoud }
852 1.1 reinoud }
853 1.1 reinoud } while (!found);
854 1.1 reinoud if (error) {
855 1.1 reinoud printf("udf_search_free_vatloc: error reading in vat chunk "
856 1.1 reinoud "(lb %d, size %d)\n", lb_num, chunk);
857 1.1 reinoud }
858 1.1 reinoud
859 1.1 reinoud if (!found) {
860 1.1 reinoud /* extend VAT */
861 1.1 reinoud DPRINTF(WRITE, ("udf_search_free_vatloc: extending\n"));
862 1.1 reinoud lb_num = ump->vat_entries;
863 1.1 reinoud ump->vat_entries++;
864 1.1 reinoud }
865 1.1 reinoud
866 1.1 reinoud /* mark entry with initialiser just in case */
867 1.1 reinoud lb_map = udf_rw32(0xfffffffe);
868 1.1 reinoud udf_vat_write(ump->vat_node, (uint8_t *) &lb_map, 4,
869 1.1 reinoud ump->vat_offset + lb_num *4);
870 1.1 reinoud ump->vat_last_free_lb = lb_num;
871 1.1 reinoud
872 1.1 reinoud free(blob, M_UDFTEMP);
873 1.1 reinoud *lbnumres = lb_num;
874 1.1 reinoud return 0;
875 1.1 reinoud }
876 1.1 reinoud
877 1.1 reinoud
878 1.1 reinoud static void
879 1.1 reinoud udf_bitmap_allocate(struct udf_bitmap *bitmap, int ismetadata,
880 1.16 reinoud uint32_t *num_lb, uint64_t *lmappos)
881 1.1 reinoud {
882 1.1 reinoud uint32_t offset, lb_num, bit;
883 1.1 reinoud int32_t diff;
884 1.1 reinoud uint8_t *bpos;
885 1.1 reinoud int pass;
886 1.1 reinoud
887 1.1 reinoud if (!ismetadata) {
888 1.1 reinoud /* heuristic to keep the two pointers not too close */
889 1.1 reinoud diff = bitmap->data_pos - bitmap->metadata_pos;
890 1.1 reinoud if ((diff >= 0) && (diff < 1024))
891 1.1 reinoud bitmap->data_pos = bitmap->metadata_pos + 1024;
892 1.1 reinoud }
893 1.1 reinoud offset = ismetadata ? bitmap->metadata_pos : bitmap->data_pos;
894 1.1 reinoud offset &= ~7;
895 1.1 reinoud for (pass = 0; pass < 2; pass++) {
896 1.1 reinoud if (offset >= bitmap->max_offset)
897 1.1 reinoud offset = 0;
898 1.1 reinoud
899 1.1 reinoud while (offset < bitmap->max_offset) {
900 1.1 reinoud if (*num_lb == 0)
901 1.1 reinoud break;
902 1.1 reinoud
903 1.1 reinoud /* use first bit not set */
904 1.1 reinoud bpos = bitmap->bits + offset/8;
905 1.9 reinoud bit = ffs(*bpos); /* returns 0 or 1..8 */
906 1.1 reinoud if (bit == 0) {
907 1.1 reinoud offset += 8;
908 1.1 reinoud continue;
909 1.1 reinoud }
910 1.20 reinoud
911 1.20 reinoud /* check for ffs overshoot */
912 1.20 reinoud if (offset + bit-1 >= bitmap->max_offset) {
913 1.20 reinoud offset = bitmap->max_offset;
914 1.20 reinoud break;
915 1.20 reinoud }
916 1.20 reinoud
917 1.9 reinoud DPRINTF(PARANOIA, ("XXX : allocate %d, %p, bit %d\n",
918 1.9 reinoud offset + bit -1, bpos, bit-1));
919 1.1 reinoud *bpos &= ~(1 << (bit-1));
920 1.1 reinoud lb_num = offset + bit-1;
921 1.1 reinoud *lmappos++ = lb_num;
922 1.1 reinoud *num_lb = *num_lb - 1;
923 1.1 reinoud // offset = (offset & ~7);
924 1.1 reinoud }
925 1.1 reinoud }
926 1.1 reinoud
927 1.1 reinoud if (ismetadata) {
928 1.1 reinoud bitmap->metadata_pos = offset;
929 1.1 reinoud } else {
930 1.1 reinoud bitmap->data_pos = offset;
931 1.1 reinoud }
932 1.1 reinoud }
933 1.1 reinoud
934 1.1 reinoud
935 1.1 reinoud static void
936 1.1 reinoud udf_bitmap_free(struct udf_bitmap *bitmap, uint32_t lb_num, uint32_t num_lb)
937 1.1 reinoud {
938 1.1 reinoud uint32_t offset;
939 1.1 reinoud uint32_t bit, bitval;
940 1.1 reinoud uint8_t *bpos;
941 1.1 reinoud
942 1.1 reinoud offset = lb_num;
943 1.1 reinoud
944 1.1 reinoud /* starter bits */
945 1.1 reinoud bpos = bitmap->bits + offset/8;
946 1.1 reinoud bit = offset % 8;
947 1.1 reinoud while ((bit != 0) && (num_lb > 0)) {
948 1.1 reinoud bitval = (1 << bit);
949 1.1 reinoud KASSERT((*bpos & bitval) == 0);
950 1.9 reinoud DPRINTF(PARANOIA, ("XXX : free %d, %p, %d\n",
951 1.9 reinoud offset, bpos, bit));
952 1.1 reinoud *bpos |= bitval;
953 1.1 reinoud offset++; num_lb--;
954 1.1 reinoud bit = (bit + 1) % 8;
955 1.1 reinoud }
956 1.1 reinoud if (num_lb == 0)
957 1.1 reinoud return;
958 1.1 reinoud
959 1.1 reinoud /* whole bytes */
960 1.1 reinoud KASSERT(bit == 0);
961 1.1 reinoud bpos = bitmap->bits + offset / 8;
962 1.1 reinoud while (num_lb >= 8) {
963 1.1 reinoud KASSERT((*bpos == 0));
964 1.9 reinoud DPRINTF(PARANOIA, ("XXX : free %d + 8, %p\n", offset, bpos));
965 1.1 reinoud *bpos = 255;
966 1.1 reinoud offset += 8; num_lb -= 8;
967 1.1 reinoud bpos++;
968 1.1 reinoud }
969 1.1 reinoud
970 1.1 reinoud /* stop bits */
971 1.1 reinoud KASSERT(num_lb < 8);
972 1.1 reinoud bit = 0;
973 1.1 reinoud while (num_lb > 0) {
974 1.1 reinoud bitval = (1 << bit);
975 1.1 reinoud KASSERT((*bpos & bitval) == 0);
976 1.9 reinoud DPRINTF(PARANOIA, ("XXX : free %d, %p, %d\n",
977 1.9 reinoud offset, bpos, bit));
978 1.1 reinoud *bpos |= bitval;
979 1.1 reinoud offset++; num_lb--;
980 1.1 reinoud bit = (bit + 1) % 8;
981 1.1 reinoud }
982 1.1 reinoud }
983 1.1 reinoud
984 1.28 reinoud
985 1.28 reinoud static uint32_t
986 1.28 reinoud udf_bitmap_check_trunc_free(struct udf_bitmap *bitmap, uint32_t to_trunc)
987 1.28 reinoud {
988 1.28 reinoud uint32_t seq_free, offset;
989 1.28 reinoud uint8_t *bpos;
990 1.28 reinoud uint8_t bit, bitval;
991 1.28 reinoud
992 1.28 reinoud DPRINTF(RESERVE, ("\ttrying to trunc %d bits from bitmap\n", to_trunc));
993 1.28 reinoud offset = bitmap->max_offset - to_trunc;
994 1.28 reinoud
995 1.28 reinoud /* starter bits (if any) */
996 1.28 reinoud bpos = bitmap->bits + offset/8;
997 1.28 reinoud bit = offset % 8;
998 1.28 reinoud seq_free = 0;
999 1.28 reinoud while (to_trunc > 0) {
1000 1.28 reinoud seq_free++;
1001 1.28 reinoud bitval = (1 << bit);
1002 1.28 reinoud if (!(*bpos & bitval))
1003 1.28 reinoud seq_free = 0;
1004 1.28 reinoud offset++; to_trunc--;
1005 1.28 reinoud bit++;
1006 1.28 reinoud if (bit == 8) {
1007 1.28 reinoud bpos++;
1008 1.28 reinoud bit = 0;
1009 1.28 reinoud }
1010 1.28 reinoud }
1011 1.28 reinoud
1012 1.28 reinoud DPRINTF(RESERVE, ("\tfound %d sequential free bits in bitmap\n", seq_free));
1013 1.28 reinoud return seq_free;
1014 1.28 reinoud }
1015 1.28 reinoud
1016 1.26 reinoud /* --------------------------------------------------------------------- */
1017 1.26 reinoud
1018 1.26 reinoud /*
1019 1.26 reinoud * We check for overall disc space with a margin to prevent critical
1020 1.26 reinoud * conditions. If disc space is low we try to force a sync() to improve our
1021 1.26 reinoud * estimates. When confronted with meta-data partition size shortage we know
1022 1.26 reinoud * we have to check if it can be extended and we need to extend it when
1023 1.26 reinoud * needed.
1024 1.26 reinoud *
1025 1.26 reinoud * A 2nd strategy we could use when disc space is getting low on a disc
1026 1.26 reinoud * formatted with a meta-data partition is to see if there are sparse areas in
1027 1.26 reinoud * the meta-data partition and free blocks there for extra data.
1028 1.26 reinoud */
1029 1.26 reinoud
1030 1.26 reinoud void
1031 1.26 reinoud udf_do_reserve_space(struct udf_mount *ump, struct udf_node *udf_node,
1032 1.26 reinoud uint16_t vpart_num, uint32_t num_lb)
1033 1.26 reinoud {
1034 1.26 reinoud ump->uncommitted_lbs[vpart_num] += num_lb;
1035 1.26 reinoud if (udf_node)
1036 1.26 reinoud udf_node->uncommitted_lbs += num_lb;
1037 1.26 reinoud }
1038 1.26 reinoud
1039 1.26 reinoud
1040 1.26 reinoud void
1041 1.26 reinoud udf_do_unreserve_space(struct udf_mount *ump, struct udf_node *udf_node,
1042 1.26 reinoud uint16_t vpart_num, uint32_t num_lb)
1043 1.26 reinoud {
1044 1.26 reinoud ump->uncommitted_lbs[vpart_num] -= num_lb;
1045 1.26 reinoud if (ump->uncommitted_lbs[vpart_num] < 0) {
1046 1.26 reinoud DPRINTF(RESERVE, ("UDF: underflow on partition reservation, "
1047 1.26 reinoud "part %d: %d\n", vpart_num,
1048 1.26 reinoud ump->uncommitted_lbs[vpart_num]));
1049 1.26 reinoud ump->uncommitted_lbs[vpart_num] = 0;
1050 1.26 reinoud }
1051 1.26 reinoud if (udf_node) {
1052 1.26 reinoud udf_node->uncommitted_lbs -= num_lb;
1053 1.26 reinoud if (udf_node->uncommitted_lbs < 0) {
1054 1.26 reinoud DPRINTF(RESERVE, ("UDF: underflow of node "
1055 1.26 reinoud "reservation : %d\n",
1056 1.26 reinoud udf_node->uncommitted_lbs));
1057 1.26 reinoud udf_node->uncommitted_lbs = 0;
1058 1.26 reinoud }
1059 1.26 reinoud }
1060 1.26 reinoud }
1061 1.26 reinoud
1062 1.26 reinoud
1063 1.26 reinoud int
1064 1.26 reinoud udf_reserve_space(struct udf_mount *ump, struct udf_node *udf_node,
1065 1.26 reinoud int udf_c_type, uint16_t vpart_num, uint32_t num_lb, int can_fail)
1066 1.26 reinoud {
1067 1.26 reinoud uint64_t freeblks;
1068 1.26 reinoud uint64_t slack;
1069 1.26 reinoud int i, error;
1070 1.26 reinoud
1071 1.26 reinoud slack = 0;
1072 1.26 reinoud if (can_fail)
1073 1.26 reinoud slack = UDF_DISC_SLACK;
1074 1.26 reinoud
1075 1.26 reinoud error = 0;
1076 1.26 reinoud mutex_enter(&ump->allocate_mutex);
1077 1.26 reinoud
1078 1.26 reinoud /* check if there is enough space available */
1079 1.28 reinoud for (i = 0; i < 3; i++) { /* XXX arbitrary number */
1080 1.26 reinoud udf_calc_vpart_freespace(ump, vpart_num, &freeblks);
1081 1.26 reinoud if (num_lb + slack < freeblks)
1082 1.26 reinoud break;
1083 1.26 reinoud /* issue SYNC */
1084 1.26 reinoud DPRINTF(RESERVE, ("udf_reserve_space: issuing sync\n"));
1085 1.26 reinoud mutex_exit(&ump->allocate_mutex);
1086 1.26 reinoud udf_do_sync(ump, FSCRED, 0);
1087 1.26 reinoud mutex_enter(&mntvnode_lock);
1088 1.28 reinoud /* 1/8 second wait */
1089 1.26 reinoud cv_timedwait(&ump->dirtynodes_cv, &mntvnode_lock,
1090 1.28 reinoud hz/8);
1091 1.26 reinoud mutex_exit(&mntvnode_lock);
1092 1.26 reinoud mutex_enter(&ump->allocate_mutex);
1093 1.26 reinoud }
1094 1.26 reinoud
1095 1.26 reinoud /* check if there is enough space available now */
1096 1.26 reinoud udf_calc_vpart_freespace(ump, vpart_num, &freeblks);
1097 1.26 reinoud if (num_lb + slack >= freeblks) {
1098 1.28 reinoud DPRINTF(RESERVE, ("udf_reserve_space: try to redistribute "
1099 1.28 reinoud "partition space\n"));
1100 1.28 reinoud DPRINTF(RESERVE, ("\tvpart %d, type %d is full\n",
1101 1.28 reinoud vpart_num, ump->vtop_alloc[vpart_num]));
1102 1.28 reinoud /* Try to redistribute space if possible */
1103 1.28 reinoud udf_collect_free_space_for_vpart(ump, vpart_num, num_lb + slack);
1104 1.26 reinoud }
1105 1.26 reinoud
1106 1.26 reinoud /* check if there is enough space available now */
1107 1.26 reinoud udf_calc_vpart_freespace(ump, vpart_num, &freeblks);
1108 1.26 reinoud if (num_lb + slack <= freeblks) {
1109 1.26 reinoud udf_do_reserve_space(ump, udf_node, vpart_num, num_lb);
1110 1.26 reinoud } else {
1111 1.26 reinoud DPRINTF(RESERVE, ("udf_reserve_space: out of disc space\n"));
1112 1.26 reinoud error = ENOSPC;
1113 1.26 reinoud }
1114 1.26 reinoud
1115 1.26 reinoud mutex_exit(&ump->allocate_mutex);
1116 1.26 reinoud return error;
1117 1.26 reinoud }
1118 1.26 reinoud
1119 1.26 reinoud
1120 1.26 reinoud void
1121 1.26 reinoud udf_cleanup_reservation(struct udf_node *udf_node)
1122 1.26 reinoud {
1123 1.26 reinoud struct udf_mount *ump = udf_node->ump;
1124 1.26 reinoud int vpart_num;
1125 1.26 reinoud
1126 1.26 reinoud mutex_enter(&ump->allocate_mutex);
1127 1.26 reinoud
1128 1.26 reinoud /* compensate for overlapping blocks */
1129 1.26 reinoud DPRINTF(RESERVE, ("UDF: overlapped %d blocks in count\n", udf_node->uncommitted_lbs));
1130 1.26 reinoud
1131 1.26 reinoud vpart_num = udf_get_record_vpart(ump, udf_get_c_type(udf_node));
1132 1.26 reinoud udf_do_unreserve_space(ump, udf_node, vpart_num, udf_node->uncommitted_lbs);
1133 1.26 reinoud
1134 1.26 reinoud DPRINTF(RESERVE, ("\ttotal now %d\n", ump->uncommitted_lbs[vpart_num]));
1135 1.26 reinoud
1136 1.26 reinoud /* sanity */
1137 1.26 reinoud if (ump->uncommitted_lbs[vpart_num] < 0)
1138 1.26 reinoud ump->uncommitted_lbs[vpart_num] = 0;
1139 1.26 reinoud
1140 1.26 reinoud mutex_exit(&ump->allocate_mutex);
1141 1.26 reinoud }
1142 1.26 reinoud
1143 1.26 reinoud /* --------------------------------------------------------------------- */
1144 1.26 reinoud
1145 1.26 reinoud /*
1146 1.26 reinoud * Allocate an extent of given length on given virt. partition. It doesn't
1147 1.26 reinoud * have to be one stretch.
1148 1.26 reinoud */
1149 1.1 reinoud
1150 1.26 reinoud int
1151 1.26 reinoud udf_allocate_space(struct udf_mount *ump, struct udf_node *udf_node,
1152 1.26 reinoud int udf_c_type, uint16_t vpart_num, uint32_t num_lb, uint64_t *lmapping)
1153 1.1 reinoud {
1154 1.1 reinoud struct mmc_trackinfo *alloc_track, *other_track;
1155 1.1 reinoud struct udf_bitmap *bitmap;
1156 1.1 reinoud struct part_desc *pdesc;
1157 1.1 reinoud struct logvol_int_desc *lvid;
1158 1.16 reinoud uint64_t *lmappos;
1159 1.1 reinoud uint32_t ptov, lb_num, *freepos, free_lbs;
1160 1.36 mrg int lb_size __diagused, alloc_num_lb;
1161 1.17 reinoud int alloc_type, error;
1162 1.17 reinoud int is_node;
1163 1.1 reinoud
1164 1.17 reinoud DPRINTF(CALL, ("udf_allocate_space(ctype %d, vpart %d, num_lb %d\n",
1165 1.17 reinoud udf_c_type, vpart_num, num_lb));
1166 1.1 reinoud mutex_enter(&ump->allocate_mutex);
1167 1.16 reinoud
1168 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
1169 1.1 reinoud KASSERT(lb_size == ump->discinfo.sector_size);
1170 1.1 reinoud
1171 1.17 reinoud alloc_type = ump->vtop_alloc[vpart_num];
1172 1.17 reinoud is_node = (udf_c_type == UDF_C_NODE);
1173 1.1 reinoud
1174 1.1 reinoud lmappos = lmapping;
1175 1.16 reinoud error = 0;
1176 1.1 reinoud switch (alloc_type) {
1177 1.1 reinoud case UDF_ALLOC_VAT :
1178 1.1 reinoud /* search empty slot in VAT file */
1179 1.1 reinoud KASSERT(num_lb == 1);
1180 1.1 reinoud error = udf_search_free_vatloc(ump, &lb_num);
1181 1.26 reinoud if (!error) {
1182 1.1 reinoud *lmappos = lb_num;
1183 1.26 reinoud
1184 1.26 reinoud /* reserve on the backing sequential partition since
1185 1.26 reinoud * that partition is credited back later */
1186 1.26 reinoud udf_do_reserve_space(ump, udf_node,
1187 1.26 reinoud ump->vtop[vpart_num], num_lb);
1188 1.26 reinoud }
1189 1.1 reinoud break;
1190 1.1 reinoud case UDF_ALLOC_SEQUENTIAL :
1191 1.1 reinoud /* sequential allocation on recordable media */
1192 1.17 reinoud /* get partition backing up this vpart_num_num */
1193 1.17 reinoud pdesc = ump->partitions[ump->vtop[vpart_num]];
1194 1.16 reinoud
1195 1.1 reinoud /* calculate offset from physical base partition */
1196 1.1 reinoud ptov = udf_rw32(pdesc->start_loc);
1197 1.1 reinoud
1198 1.17 reinoud /* get our track descriptors */
1199 1.17 reinoud if (vpart_num == ump->node_part) {
1200 1.17 reinoud alloc_track = &ump->metadata_track;
1201 1.17 reinoud other_track = &ump->data_track;
1202 1.17 reinoud } else {
1203 1.17 reinoud alloc_track = &ump->data_track;
1204 1.17 reinoud other_track = &ump->metadata_track;
1205 1.17 reinoud }
1206 1.17 reinoud
1207 1.17 reinoud /* allocate */
1208 1.1 reinoud for (lb_num = 0; lb_num < num_lb; lb_num++) {
1209 1.1 reinoud *lmappos++ = alloc_track->next_writable - ptov;
1210 1.1 reinoud alloc_track->next_writable++;
1211 1.1 reinoud alloc_track->free_blocks--;
1212 1.1 reinoud }
1213 1.17 reinoud
1214 1.17 reinoud /* keep other track up-to-date */
1215 1.1 reinoud if (alloc_track->tracknr == other_track->tracknr)
1216 1.1 reinoud memcpy(other_track, alloc_track,
1217 1.1 reinoud sizeof(struct mmc_trackinfo));
1218 1.1 reinoud break;
1219 1.1 reinoud case UDF_ALLOC_SPACEMAP :
1220 1.16 reinoud /* try to allocate on unallocated bits */
1221 1.1 reinoud alloc_num_lb = num_lb;
1222 1.17 reinoud bitmap = &ump->part_unalloc_bits[vpart_num];
1223 1.17 reinoud udf_bitmap_allocate(bitmap, is_node, &alloc_num_lb, lmappos);
1224 1.1 reinoud ump->lvclose |= UDF_WRITE_PART_BITMAPS;
1225 1.16 reinoud
1226 1.16 reinoud /* have we allocated all? */
1227 1.1 reinoud if (alloc_num_lb) {
1228 1.1 reinoud /* TODO convert freed to unalloc and try again */
1229 1.1 reinoud /* free allocated piece for now */
1230 1.1 reinoud lmappos = lmapping;
1231 1.1 reinoud for (lb_num=0; lb_num < num_lb-alloc_num_lb; lb_num++) {
1232 1.1 reinoud udf_bitmap_free(bitmap, *lmappos++, 1);
1233 1.1 reinoud }
1234 1.1 reinoud error = ENOSPC;
1235 1.1 reinoud }
1236 1.1 reinoud if (!error) {
1237 1.1 reinoud /* adjust freecount */
1238 1.1 reinoud lvid = ump->logvol_integrity;
1239 1.17 reinoud freepos = &lvid->tables[0] + vpart_num;
1240 1.1 reinoud free_lbs = udf_rw32(*freepos);
1241 1.1 reinoud *freepos = udf_rw32(free_lbs - num_lb);
1242 1.1 reinoud }
1243 1.1 reinoud break;
1244 1.15 reinoud case UDF_ALLOC_METABITMAP : /* UDF 2.50, 2.60 BluRay-RE */
1245 1.16 reinoud /* allocate on metadata unallocated bits */
1246 1.16 reinoud alloc_num_lb = num_lb;
1247 1.16 reinoud bitmap = &ump->metadata_unalloc_bits;
1248 1.17 reinoud udf_bitmap_allocate(bitmap, is_node, &alloc_num_lb, lmappos);
1249 1.16 reinoud ump->lvclose |= UDF_WRITE_PART_BITMAPS;
1250 1.16 reinoud
1251 1.16 reinoud /* have we allocated all? */
1252 1.17 reinoud if (alloc_num_lb) {
1253 1.16 reinoud /* YIKES! TODO we need to extend the metadata partition */
1254 1.16 reinoud /* free allocated piece for now */
1255 1.16 reinoud lmappos = lmapping;
1256 1.16 reinoud for (lb_num=0; lb_num < num_lb-alloc_num_lb; lb_num++) {
1257 1.16 reinoud udf_bitmap_free(bitmap, *lmappos++, 1);
1258 1.16 reinoud }
1259 1.16 reinoud error = ENOSPC;
1260 1.16 reinoud }
1261 1.16 reinoud if (!error) {
1262 1.16 reinoud /* adjust freecount */
1263 1.16 reinoud lvid = ump->logvol_integrity;
1264 1.17 reinoud freepos = &lvid->tables[0] + vpart_num;
1265 1.16 reinoud free_lbs = udf_rw32(*freepos);
1266 1.16 reinoud *freepos = udf_rw32(free_lbs - num_lb);
1267 1.16 reinoud }
1268 1.16 reinoud break;
1269 1.15 reinoud case UDF_ALLOC_METASEQUENTIAL : /* UDF 2.60 BluRay-R */
1270 1.15 reinoud case UDF_ALLOC_RELAXEDSEQUENTIAL : /* UDF 2.50/~meta BluRay-R */
1271 1.1 reinoud printf("ALERT: udf_allocate_space : allocation %d "
1272 1.1 reinoud "not implemented yet!\n", alloc_type);
1273 1.1 reinoud /* TODO implement, doesn't have to be contiguous */
1274 1.1 reinoud error = ENOSPC;
1275 1.1 reinoud break;
1276 1.1 reinoud }
1277 1.1 reinoud
1278 1.26 reinoud if (!error) {
1279 1.26 reinoud /* credit our partition since we have committed the space */
1280 1.26 reinoud udf_do_unreserve_space(ump, udf_node, vpart_num, num_lb);
1281 1.26 reinoud }
1282 1.26 reinoud
1283 1.1 reinoud #ifdef DEBUG
1284 1.1 reinoud if (udf_verbose & UDF_DEBUG_ALLOC) {
1285 1.1 reinoud lmappos = lmapping;
1286 1.16 reinoud printf("udf_allocate_space, allocated logical lba :\n");
1287 1.1 reinoud for (lb_num = 0; lb_num < num_lb; lb_num++) {
1288 1.25 reinoud printf("%s %"PRIu64, (lb_num > 0)?",":"",
1289 1.16 reinoud *lmappos++);
1290 1.1 reinoud }
1291 1.16 reinoud printf("\n");
1292 1.1 reinoud }
1293 1.1 reinoud #endif
1294 1.1 reinoud mutex_exit(&ump->allocate_mutex);
1295 1.1 reinoud
1296 1.1 reinoud return error;
1297 1.1 reinoud }
1298 1.1 reinoud
1299 1.1 reinoud /* --------------------------------------------------------------------- */
1300 1.1 reinoud
1301 1.1 reinoud void
1302 1.1 reinoud udf_free_allocated_space(struct udf_mount *ump, uint32_t lb_num,
1303 1.1 reinoud uint16_t vpart_num, uint32_t num_lb)
1304 1.1 reinoud {
1305 1.1 reinoud struct udf_bitmap *bitmap;
1306 1.1 reinoud struct logvol_int_desc *lvid;
1307 1.35 christos uint32_t lb_map, udf_rw32_lbmap;
1308 1.1 reinoud uint32_t *freepos, free_lbs;
1309 1.1 reinoud int phys_part;
1310 1.36 mrg int error __diagused;
1311 1.1 reinoud
1312 1.1 reinoud DPRINTF(ALLOC, ("udf_free_allocated_space: freeing virt lbnum %d "
1313 1.1 reinoud "part %d + %d sect\n", lb_num, vpart_num, num_lb));
1314 1.1 reinoud
1315 1.10 reinoud /* no use freeing zero length */
1316 1.10 reinoud if (num_lb == 0)
1317 1.10 reinoud return;
1318 1.10 reinoud
1319 1.1 reinoud mutex_enter(&ump->allocate_mutex);
1320 1.1 reinoud
1321 1.1 reinoud switch (ump->vtop_tp[vpart_num]) {
1322 1.1 reinoud case UDF_VTOP_TYPE_PHYS :
1323 1.1 reinoud case UDF_VTOP_TYPE_SPARABLE :
1324 1.1 reinoud /* free space to freed or unallocated space bitmap */
1325 1.1 reinoud phys_part = ump->vtop[vpart_num];
1326 1.1 reinoud
1327 1.1 reinoud /* first try freed space bitmap */
1328 1.1 reinoud bitmap = &ump->part_freed_bits[phys_part];
1329 1.1 reinoud
1330 1.1 reinoud /* if not defined, use unallocated bitmap */
1331 1.1 reinoud if (bitmap->bits == NULL)
1332 1.1 reinoud bitmap = &ump->part_unalloc_bits[phys_part];
1333 1.1 reinoud
1334 1.17 reinoud /* if no bitmaps are defined, bail out; XXX OK? */
1335 1.1 reinoud if (bitmap->bits == NULL)
1336 1.1 reinoud break;
1337 1.1 reinoud
1338 1.1 reinoud /* free bits if its defined */
1339 1.1 reinoud KASSERT(bitmap->bits);
1340 1.1 reinoud ump->lvclose |= UDF_WRITE_PART_BITMAPS;
1341 1.1 reinoud udf_bitmap_free(bitmap, lb_num, num_lb);
1342 1.1 reinoud
1343 1.1 reinoud /* adjust freecount */
1344 1.1 reinoud lvid = ump->logvol_integrity;
1345 1.1 reinoud freepos = &lvid->tables[0] + vpart_num;
1346 1.1 reinoud free_lbs = udf_rw32(*freepos);
1347 1.1 reinoud *freepos = udf_rw32(free_lbs + num_lb);
1348 1.1 reinoud break;
1349 1.1 reinoud case UDF_VTOP_TYPE_VIRT :
1350 1.1 reinoud /* free this VAT entry */
1351 1.1 reinoud KASSERT(num_lb == 1);
1352 1.1 reinoud
1353 1.1 reinoud lb_map = 0xffffffff;
1354 1.1 reinoud udf_rw32_lbmap = udf_rw32(lb_map);
1355 1.1 reinoud error = udf_vat_write(ump->vat_node,
1356 1.1 reinoud (uint8_t *) &udf_rw32_lbmap, 4,
1357 1.1 reinoud ump->vat_offset + lb_num * 4);
1358 1.1 reinoud KASSERT(error == 0);
1359 1.1 reinoud ump->vat_last_free_lb = MIN(ump->vat_last_free_lb, lb_num);
1360 1.1 reinoud break;
1361 1.1 reinoud case UDF_VTOP_TYPE_META :
1362 1.1 reinoud /* free space in the metadata bitmap */
1363 1.17 reinoud bitmap = &ump->metadata_unalloc_bits;
1364 1.17 reinoud KASSERT(bitmap->bits);
1365 1.17 reinoud
1366 1.17 reinoud ump->lvclose |= UDF_WRITE_PART_BITMAPS;
1367 1.17 reinoud udf_bitmap_free(bitmap, lb_num, num_lb);
1368 1.17 reinoud
1369 1.17 reinoud /* adjust freecount */
1370 1.17 reinoud lvid = ump->logvol_integrity;
1371 1.17 reinoud freepos = &lvid->tables[0] + vpart_num;
1372 1.17 reinoud free_lbs = udf_rw32(*freepos);
1373 1.17 reinoud *freepos = udf_rw32(free_lbs + num_lb);
1374 1.17 reinoud break;
1375 1.1 reinoud default:
1376 1.1 reinoud printf("ALERT: udf_free_allocated_space : allocation %d "
1377 1.1 reinoud "not implemented yet!\n", ump->vtop_tp[vpart_num]);
1378 1.1 reinoud break;
1379 1.1 reinoud }
1380 1.1 reinoud
1381 1.1 reinoud mutex_exit(&ump->allocate_mutex);
1382 1.1 reinoud }
1383 1.1 reinoud
1384 1.1 reinoud /* --------------------------------------------------------------------- */
1385 1.1 reinoud
1386 1.1 reinoud /*
1387 1.28 reinoud * Special function to synchronise the metadatamirror file when they change on
1388 1.28 reinoud * resizing. When the metadatafile is actually duplicated, this action is a
1389 1.28 reinoud * no-op since they describe different extents on the disc.
1390 1.28 reinoud */
1391 1.28 reinoud
1392 1.31 reinoud void
1393 1.31 reinoud udf_synchronise_metadatamirror_node(struct udf_mount *ump)
1394 1.28 reinoud {
1395 1.28 reinoud struct udf_node *meta_node, *metamirror_node;
1396 1.28 reinoud struct long_ad s_ad;
1397 1.31 reinoud uint32_t len, flags;
1398 1.28 reinoud int slot, cpy_slot;
1399 1.28 reinoud int error, eof;
1400 1.28 reinoud
1401 1.28 reinoud if (ump->metadata_flags & METADATA_DUPLICATED)
1402 1.28 reinoud return;
1403 1.28 reinoud
1404 1.28 reinoud meta_node = ump->metadata_node;
1405 1.28 reinoud metamirror_node = ump->metadatamirror_node;
1406 1.28 reinoud
1407 1.28 reinoud /* 1) wipe mirror node */
1408 1.28 reinoud udf_wipe_adslots(metamirror_node);
1409 1.28 reinoud
1410 1.28 reinoud /* 2) copy all node descriptors from the meta_node */
1411 1.28 reinoud slot = 0;
1412 1.28 reinoud cpy_slot = 0;
1413 1.28 reinoud for (;;) {
1414 1.28 reinoud udf_get_adslot(meta_node, slot, &s_ad, &eof);
1415 1.28 reinoud if (eof)
1416 1.28 reinoud break;
1417 1.31 reinoud len = udf_rw32(s_ad.len);
1418 1.31 reinoud flags = UDF_EXT_FLAGS(len);
1419 1.31 reinoud len = UDF_EXT_LEN(len);
1420 1.31 reinoud
1421 1.31 reinoud if (flags == UDF_EXT_REDIRECT) {
1422 1.31 reinoud slot++;
1423 1.31 reinoud continue;
1424 1.31 reinoud }
1425 1.31 reinoud
1426 1.28 reinoud error = udf_append_adslot(metamirror_node, &cpy_slot, &s_ad);
1427 1.28 reinoud if (error) {
1428 1.28 reinoud /* WTF, this shouldn't happen, what to do now? */
1429 1.28 reinoud panic("udf_synchronise_metadatamirror_node failed!");
1430 1.28 reinoud }
1431 1.31 reinoud cpy_slot++;
1432 1.28 reinoud slot++;
1433 1.28 reinoud }
1434 1.28 reinoud
1435 1.28 reinoud /* 3) adjust metamirror_node size */
1436 1.28 reinoud if (meta_node->fe) {
1437 1.28 reinoud KASSERT(metamirror_node->fe);
1438 1.28 reinoud metamirror_node->fe->inf_len = meta_node->fe->inf_len;
1439 1.28 reinoud } else {
1440 1.28 reinoud KASSERT(meta_node->efe);
1441 1.28 reinoud KASSERT(metamirror_node->efe);
1442 1.28 reinoud metamirror_node->efe->inf_len = meta_node->efe->inf_len;
1443 1.28 reinoud metamirror_node->efe->obj_size = meta_node->efe->obj_size;
1444 1.28 reinoud }
1445 1.28 reinoud
1446 1.28 reinoud /* for sanity */
1447 1.28 reinoud udf_count_alloc_exts(metamirror_node);
1448 1.28 reinoud }
1449 1.28 reinoud
1450 1.28 reinoud /* --------------------------------------------------------------------- */
1451 1.28 reinoud
1452 1.28 reinoud /*
1453 1.28 reinoud * When faced with an out of space but there is still space available on other
1454 1.28 reinoud * partitions, try to redistribute the space. This is only defined for media
1455 1.28 reinoud * using Metadata partitions.
1456 1.28 reinoud *
1457 1.28 reinoud * There are two formats to deal with. Either its a `normal' metadata
1458 1.28 reinoud * partition and we can move blocks between a metadata bitmap and its
1459 1.28 reinoud * companion data spacemap OR its a UDF 2.60 formatted BluRay-R disc with POW
1460 1.28 reinoud * and a metadata partition.
1461 1.28 reinoud */
1462 1.28 reinoud
1463 1.31 reinoud /* implementation limit: ump->datapart is the companion partition */
1464 1.28 reinoud static uint32_t
1465 1.28 reinoud udf_trunc_metadatapart(struct udf_mount *ump, uint32_t num_lb)
1466 1.28 reinoud {
1467 1.28 reinoud struct udf_node *bitmap_node;
1468 1.28 reinoud struct udf_bitmap *bitmap;
1469 1.28 reinoud struct space_bitmap_desc *sbd, *new_sbd;
1470 1.28 reinoud struct logvol_int_desc *lvid;
1471 1.28 reinoud uint64_t inf_len;
1472 1.31 reinoud uint64_t meta_free_lbs, data_free_lbs, to_trunc;
1473 1.28 reinoud uint32_t *freepos, *sizepos;
1474 1.31 reinoud uint32_t unit, lb_size;
1475 1.28 reinoud uint16_t meta_vpart_num, data_vpart_num, num_vpart;
1476 1.36 mrg int err __diagused;
1477 1.28 reinoud
1478 1.28 reinoud unit = ump->metadata_alloc_unit_size;
1479 1.28 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
1480 1.28 reinoud lvid = ump->logvol_integrity;
1481 1.28 reinoud
1482 1.31 reinoud /* XXX
1483 1.31 reinoud *
1484 1.31 reinoud * the following checks will fail for BD-R UDF 2.60! but they are
1485 1.31 reinoud * read-only for now anyway! Its even doubtfull if it is to be allowed
1486 1.31 reinoud * for these discs.
1487 1.31 reinoud */
1488 1.31 reinoud
1489 1.28 reinoud /* lookup vpart for metadata partition */
1490 1.28 reinoud meta_vpart_num = ump->node_part;
1491 1.28 reinoud KASSERT(ump->vtop_alloc[meta_vpart_num] == UDF_ALLOC_METABITMAP);
1492 1.28 reinoud
1493 1.28 reinoud /* lookup vpart for data partition */
1494 1.28 reinoud data_vpart_num = ump->data_part;
1495 1.28 reinoud KASSERT(ump->vtop_alloc[data_vpart_num] == UDF_ALLOC_SPACEMAP);
1496 1.28 reinoud
1497 1.28 reinoud udf_calc_vpart_freespace(ump, data_vpart_num, &data_free_lbs);
1498 1.28 reinoud udf_calc_vpart_freespace(ump, meta_vpart_num, &meta_free_lbs);
1499 1.28 reinoud
1500 1.28 reinoud DPRINTF(RESERVE, ("\tfree space on data partition %"PRIu64" blks\n", data_free_lbs));
1501 1.28 reinoud DPRINTF(RESERVE, ("\tfree space on metadata partition %"PRIu64" blks\n", meta_free_lbs));
1502 1.28 reinoud
1503 1.28 reinoud /* give away some of the free meta space, in unit block sizes */
1504 1.29 reinoud to_trunc = meta_free_lbs/4; /* give out a quarter */
1505 1.28 reinoud to_trunc = MAX(to_trunc, num_lb);
1506 1.28 reinoud to_trunc = unit * ((to_trunc + unit-1) / unit); /* round up */
1507 1.28 reinoud
1508 1.28 reinoud /* scale down if needed and bail out when out of space */
1509 1.28 reinoud if (to_trunc >= meta_free_lbs)
1510 1.28 reinoud return num_lb;
1511 1.28 reinoud
1512 1.28 reinoud /* check extent of bits marked free at the end of the map */
1513 1.28 reinoud bitmap = &ump->metadata_unalloc_bits;
1514 1.28 reinoud to_trunc = udf_bitmap_check_trunc_free(bitmap, to_trunc);
1515 1.28 reinoud to_trunc = unit * (to_trunc / unit); /* round down again */
1516 1.28 reinoud if (to_trunc == 0)
1517 1.28 reinoud return num_lb;
1518 1.28 reinoud
1519 1.31 reinoud DPRINTF(RESERVE, ("\ttruncating %"PRIu64" lbs from the metadata bitmap\n",
1520 1.28 reinoud to_trunc));
1521 1.28 reinoud
1522 1.28 reinoud /* get length of the metadata bitmap node file */
1523 1.28 reinoud bitmap_node = ump->metadatabitmap_node;
1524 1.28 reinoud if (bitmap_node->fe) {
1525 1.28 reinoud inf_len = udf_rw64(bitmap_node->fe->inf_len);
1526 1.28 reinoud } else {
1527 1.28 reinoud KASSERT(bitmap_node->efe);
1528 1.28 reinoud inf_len = udf_rw64(bitmap_node->efe->inf_len);
1529 1.28 reinoud }
1530 1.28 reinoud inf_len -= to_trunc/8;
1531 1.28 reinoud
1532 1.28 reinoud /* as per [UDF 2.60/2.2.13.6] : */
1533 1.28 reinoud /* 1) update the SBD in the metadata bitmap file */
1534 1.28 reinoud sbd = (struct space_bitmap_desc *) bitmap->blob;
1535 1.31 reinoud sbd->num_bits = udf_rw32(udf_rw32(sbd->num_bits) - to_trunc);
1536 1.31 reinoud sbd->num_bytes = udf_rw32(udf_rw32(sbd->num_bytes) - to_trunc/8);
1537 1.28 reinoud bitmap->max_offset = udf_rw32(sbd->num_bits);
1538 1.28 reinoud
1539 1.28 reinoud num_vpart = udf_rw32(lvid->num_part);
1540 1.28 reinoud freepos = &lvid->tables[0] + meta_vpart_num;
1541 1.28 reinoud sizepos = &lvid->tables[0] + num_vpart + meta_vpart_num;
1542 1.28 reinoud *freepos = udf_rw32(*freepos) - to_trunc;
1543 1.28 reinoud *sizepos = udf_rw32(*sizepos) - to_trunc;
1544 1.28 reinoud
1545 1.28 reinoud /* realloc bitmap for better memory usage */
1546 1.28 reinoud new_sbd = realloc(sbd, inf_len, M_UDFVOLD,
1547 1.28 reinoud M_CANFAIL | M_WAITOK);
1548 1.28 reinoud if (new_sbd) {
1549 1.28 reinoud /* update pointers */
1550 1.28 reinoud ump->metadata_unalloc_dscr = new_sbd;
1551 1.28 reinoud bitmap->blob = (uint8_t *) new_sbd;
1552 1.28 reinoud }
1553 1.28 reinoud ump->lvclose |= UDF_WRITE_PART_BITMAPS;
1554 1.28 reinoud
1555 1.28 reinoud /*
1556 1.31 reinoud * The truncated space is secured now and can't be allocated anymore.
1557 1.31 reinoud * Release the allocate mutex so we can shrink the nodes the normal
1558 1.31 reinoud * way.
1559 1.28 reinoud */
1560 1.28 reinoud mutex_exit(&ump->allocate_mutex);
1561 1.28 reinoud
1562 1.28 reinoud /* 2) trunc the metadata bitmap information file, freeing blocks */
1563 1.28 reinoud err = udf_shrink_node(bitmap_node, inf_len);
1564 1.28 reinoud KASSERT(err == 0);
1565 1.28 reinoud
1566 1.28 reinoud /* 3) trunc the metadata file and mirror file, freeing blocks */
1567 1.31 reinoud inf_len = (uint64_t) udf_rw32(sbd->num_bits) * lb_size; /* [4/14.12.4] */
1568 1.28 reinoud err = udf_shrink_node(ump->metadata_node, inf_len);
1569 1.28 reinoud KASSERT(err == 0);
1570 1.31 reinoud if (ump->metadatamirror_node) {
1571 1.31 reinoud if (ump->metadata_flags & METADATA_DUPLICATED) {
1572 1.31 reinoud err = udf_shrink_node(ump->metadatamirror_node, inf_len);
1573 1.31 reinoud } else {
1574 1.31 reinoud /* extents will be copied on writeout */
1575 1.31 reinoud }
1576 1.28 reinoud KASSERT(err == 0);
1577 1.28 reinoud }
1578 1.28 reinoud ump->lvclose |= UDF_WRITE_METAPART_NODES;
1579 1.28 reinoud
1580 1.28 reinoud /* relock before exit */
1581 1.28 reinoud mutex_enter(&ump->allocate_mutex);
1582 1.28 reinoud
1583 1.28 reinoud if (to_trunc > num_lb)
1584 1.28 reinoud return 0;
1585 1.28 reinoud return num_lb - to_trunc;
1586 1.28 reinoud }
1587 1.28 reinoud
1588 1.28 reinoud
1589 1.28 reinoud static void
1590 1.28 reinoud udf_sparsify_metadatapart(struct udf_mount *ump, uint32_t num_lb)
1591 1.28 reinoud {
1592 1.28 reinoud /* NOT IMPLEMENTED, fail */
1593 1.28 reinoud }
1594 1.28 reinoud
1595 1.28 reinoud
1596 1.28 reinoud static void
1597 1.28 reinoud udf_collect_free_space_for_vpart(struct udf_mount *ump,
1598 1.28 reinoud uint16_t vpart_num, uint32_t num_lb)
1599 1.28 reinoud {
1600 1.28 reinoud /* allocate mutex is helt */
1601 1.28 reinoud
1602 1.28 reinoud /* only defined for metadata partitions */
1603 1.28 reinoud if (ump->vtop_tp[ump->node_part] != UDF_VTOP_TYPE_META) {
1604 1.28 reinoud DPRINTF(RESERVE, ("\tcan't grow/shrink; no metadata partitioning\n"));
1605 1.28 reinoud return;
1606 1.28 reinoud }
1607 1.28 reinoud
1608 1.28 reinoud /* UDF 2.60 BD-R+POW? */
1609 1.28 reinoud if (ump->vtop_alloc[ump->node_part] == UDF_ALLOC_METASEQUENTIAL) {
1610 1.28 reinoud DPRINTF(RESERVE, ("\tUDF 2.60 BD-R+POW track grow not implemented yet\n"));
1611 1.28 reinoud return;
1612 1.28 reinoud }
1613 1.28 reinoud
1614 1.28 reinoud if (ump->vtop_tp[vpart_num] == UDF_VTOP_TYPE_META) {
1615 1.28 reinoud /* try to grow the meta partition */
1616 1.28 reinoud DPRINTF(RESERVE, ("\ttrying to grow the meta partition\n"));
1617 1.28 reinoud /* as per [UDF 2.60/2.2.13.5] : extend bitmap and metadata file(s) */
1618 1.30 reinoud DPRINTF(NOTIMPL, ("\tgrowing meta partition not implemented yet\n"));
1619 1.28 reinoud } else {
1620 1.28 reinoud /* try to shrink the metadata partition */
1621 1.28 reinoud DPRINTF(RESERVE, ("\ttrying to shrink the meta partition\n"));
1622 1.28 reinoud /* as per [UDF 2.60/2.2.13.6] : either trunc or make sparse */
1623 1.28 reinoud num_lb = udf_trunc_metadatapart(ump, num_lb);
1624 1.28 reinoud if (num_lb)
1625 1.28 reinoud udf_sparsify_metadatapart(ump, num_lb);
1626 1.28 reinoud }
1627 1.28 reinoud
1628 1.28 reinoud /* allocate mutex should still be helt */
1629 1.28 reinoud }
1630 1.28 reinoud
1631 1.28 reinoud /* --------------------------------------------------------------------- */
1632 1.28 reinoud
1633 1.28 reinoud /*
1634 1.1 reinoud * Allocate a buf on disc for direct write out. The space doesn't have to be
1635 1.1 reinoud * contiguous as the caller takes care of this.
1636 1.1 reinoud */
1637 1.1 reinoud
1638 1.1 reinoud void
1639 1.1 reinoud udf_late_allocate_buf(struct udf_mount *ump, struct buf *buf,
1640 1.17 reinoud uint64_t *lmapping, struct long_ad *node_ad_cpy, uint16_t *vpart_nump)
1641 1.1 reinoud {
1642 1.1 reinoud struct udf_node *udf_node = VTOI(buf->b_vp);
1643 1.35 christos int lb_size, udf_c_type;
1644 1.17 reinoud int vpart_num, num_lb;
1645 1.1 reinoud int error, s;
1646 1.1 reinoud
1647 1.1 reinoud /*
1648 1.1 reinoud * for each sector in the buf, allocate a sector on disc and record
1649 1.1 reinoud * its position in the provided mapping array.
1650 1.1 reinoud *
1651 1.1 reinoud * If its userdata or FIDs, record its location in its node.
1652 1.1 reinoud */
1653 1.1 reinoud
1654 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
1655 1.1 reinoud num_lb = (buf->b_bcount + lb_size -1) / lb_size;
1656 1.1 reinoud udf_c_type = buf->b_udf_c_type;
1657 1.1 reinoud
1658 1.1 reinoud KASSERT(lb_size == ump->discinfo.sector_size);
1659 1.1 reinoud
1660 1.17 reinoud /* select partition to record the buffer on */
1661 1.26 reinoud vpart_num = *vpart_nump = udf_get_record_vpart(ump, udf_c_type);
1662 1.1 reinoud
1663 1.1 reinoud if (udf_c_type == UDF_C_NODE) {
1664 1.1 reinoud /* if not VAT, its allready allocated */
1665 1.17 reinoud if (ump->vtop_alloc[ump->node_part] != UDF_ALLOC_VAT)
1666 1.1 reinoud return;
1667 1.1 reinoud
1668 1.17 reinoud /* allocate on its backing sequential partition */
1669 1.17 reinoud vpart_num = ump->data_part;
1670 1.1 reinoud }
1671 1.1 reinoud
1672 1.26 reinoud /* XXX can this still happen? */
1673 1.17 reinoud /* do allocation on the selected partition */
1674 1.26 reinoud error = udf_allocate_space(ump, udf_node, udf_c_type,
1675 1.17 reinoud vpart_num, num_lb, lmapping);
1676 1.1 reinoud if (error) {
1677 1.26 reinoud /*
1678 1.26 reinoud * ARGH! we haven't done our accounting right! it should
1679 1.26 reinoud * allways succeed.
1680 1.26 reinoud */
1681 1.1 reinoud panic("UDF disc allocation accounting gone wrong");
1682 1.1 reinoud }
1683 1.1 reinoud
1684 1.1 reinoud /* If its userdata or FIDs, record its allocation in its node. */
1685 1.16 reinoud if ((udf_c_type == UDF_C_USERDATA) ||
1686 1.16 reinoud (udf_c_type == UDF_C_FIDS) ||
1687 1.16 reinoud (udf_c_type == UDF_C_METADATA_SBM))
1688 1.16 reinoud {
1689 1.17 reinoud udf_record_allocation_in_node(ump, buf, vpart_num, lmapping,
1690 1.1 reinoud node_ad_cpy);
1691 1.1 reinoud /* decrement our outstanding bufs counter */
1692 1.1 reinoud s = splbio();
1693 1.1 reinoud udf_node->outstanding_bufs--;
1694 1.1 reinoud splx(s);
1695 1.1 reinoud }
1696 1.1 reinoud }
1697 1.1 reinoud
1698 1.1 reinoud /* --------------------------------------------------------------------- */
1699 1.1 reinoud
1700 1.1 reinoud /*
1701 1.1 reinoud * Try to merge a1 with the new piece a2. udf_ads_merge returns error when not
1702 1.1 reinoud * possible (anymore); a2 returns the rest piece.
1703 1.1 reinoud */
1704 1.1 reinoud
1705 1.1 reinoud static int
1706 1.31 reinoud udf_ads_merge(uint32_t max_len, uint32_t lb_size, struct long_ad *a1, struct long_ad *a2)
1707 1.1 reinoud {
1708 1.31 reinoud uint32_t merge_len;
1709 1.1 reinoud uint32_t a1_len, a2_len;
1710 1.1 reinoud uint32_t a1_flags, a2_flags;
1711 1.1 reinoud uint32_t a1_lbnum, a2_lbnum;
1712 1.1 reinoud uint16_t a1_part, a2_part;
1713 1.1 reinoud
1714 1.1 reinoud a1_flags = UDF_EXT_FLAGS(udf_rw32(a1->len));
1715 1.1 reinoud a1_len = UDF_EXT_LEN(udf_rw32(a1->len));
1716 1.1 reinoud a1_lbnum = udf_rw32(a1->loc.lb_num);
1717 1.1 reinoud a1_part = udf_rw16(a1->loc.part_num);
1718 1.1 reinoud
1719 1.1 reinoud a2_flags = UDF_EXT_FLAGS(udf_rw32(a2->len));
1720 1.1 reinoud a2_len = UDF_EXT_LEN(udf_rw32(a2->len));
1721 1.1 reinoud a2_lbnum = udf_rw32(a2->loc.lb_num);
1722 1.1 reinoud a2_part = udf_rw16(a2->loc.part_num);
1723 1.1 reinoud
1724 1.1 reinoud /* defines same space */
1725 1.1 reinoud if (a1_flags != a2_flags)
1726 1.1 reinoud return 1;
1727 1.1 reinoud
1728 1.1 reinoud if (a1_flags != UDF_EXT_FREE) {
1729 1.1 reinoud /* the same partition */
1730 1.1 reinoud if (a1_part != a2_part)
1731 1.1 reinoud return 1;
1732 1.1 reinoud
1733 1.1 reinoud /* a2 is successor of a1 */
1734 1.1 reinoud if (a1_lbnum * lb_size + a1_len != a2_lbnum * lb_size)
1735 1.1 reinoud return 1;
1736 1.1 reinoud }
1737 1.9 reinoud
1738 1.1 reinoud /* merge as most from a2 if possible */
1739 1.1 reinoud merge_len = MIN(a2_len, max_len - a1_len);
1740 1.1 reinoud a1_len += merge_len;
1741 1.1 reinoud a2_len -= merge_len;
1742 1.1 reinoud a2_lbnum += merge_len/lb_size;
1743 1.1 reinoud
1744 1.1 reinoud a1->len = udf_rw32(a1_len | a1_flags);
1745 1.1 reinoud a2->len = udf_rw32(a2_len | a2_flags);
1746 1.1 reinoud a2->loc.lb_num = udf_rw32(a2_lbnum);
1747 1.1 reinoud
1748 1.1 reinoud if (a2_len > 0)
1749 1.1 reinoud return 1;
1750 1.1 reinoud
1751 1.1 reinoud /* there is space over to merge */
1752 1.1 reinoud return 0;
1753 1.1 reinoud }
1754 1.1 reinoud
1755 1.1 reinoud /* --------------------------------------------------------------------- */
1756 1.1 reinoud
1757 1.1 reinoud static void
1758 1.1 reinoud udf_wipe_adslots(struct udf_node *udf_node)
1759 1.1 reinoud {
1760 1.1 reinoud struct file_entry *fe;
1761 1.1 reinoud struct extfile_entry *efe;
1762 1.1 reinoud struct alloc_ext_entry *ext;
1763 1.35 christos uint32_t lb_size, dscr_size, l_ea, max_l_ad, crclen;
1764 1.1 reinoud uint8_t *data_pos;
1765 1.1 reinoud int extnr;
1766 1.1 reinoud
1767 1.1 reinoud lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
1768 1.1 reinoud
1769 1.1 reinoud fe = udf_node->fe;
1770 1.1 reinoud efe = udf_node->efe;
1771 1.1 reinoud if (fe) {
1772 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
1773 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
1774 1.1 reinoud data_pos = (uint8_t *) fe + dscr_size + l_ea;
1775 1.1 reinoud } else {
1776 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
1777 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
1778 1.1 reinoud data_pos = (uint8_t *) efe + dscr_size + l_ea;
1779 1.1 reinoud }
1780 1.1 reinoud max_l_ad = lb_size - dscr_size - l_ea;
1781 1.1 reinoud
1782 1.1 reinoud /* wipe fe/efe */
1783 1.1 reinoud memset(data_pos, 0, max_l_ad);
1784 1.1 reinoud crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea;
1785 1.1 reinoud if (fe) {
1786 1.1 reinoud fe->l_ad = udf_rw32(0);
1787 1.1 reinoud fe->logblks_rec = udf_rw64(0);
1788 1.22 reinoud fe->tag.desc_crc_len = udf_rw16(crclen);
1789 1.1 reinoud } else {
1790 1.1 reinoud efe->l_ad = udf_rw32(0);
1791 1.1 reinoud efe->logblks_rec = udf_rw64(0);
1792 1.22 reinoud efe->tag.desc_crc_len = udf_rw16(crclen);
1793 1.1 reinoud }
1794 1.1 reinoud
1795 1.1 reinoud /* wipe all allocation extent entries */
1796 1.1 reinoud for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
1797 1.1 reinoud ext = udf_node->ext[extnr];
1798 1.1 reinoud dscr_size = sizeof(struct alloc_ext_entry) -1;
1799 1.11 reinoud data_pos = (uint8_t *) ext->data;
1800 1.1 reinoud max_l_ad = lb_size - dscr_size;
1801 1.1 reinoud memset(data_pos, 0, max_l_ad);
1802 1.1 reinoud ext->l_ad = udf_rw32(0);
1803 1.1 reinoud
1804 1.1 reinoud crclen = dscr_size - UDF_DESC_TAG_LENGTH;
1805 1.22 reinoud ext->tag.desc_crc_len = udf_rw16(crclen);
1806 1.1 reinoud }
1807 1.11 reinoud udf_node->i_flags |= IN_NODE_REBUILD;
1808 1.1 reinoud }
1809 1.1 reinoud
1810 1.1 reinoud /* --------------------------------------------------------------------- */
1811 1.1 reinoud
1812 1.1 reinoud void
1813 1.1 reinoud udf_get_adslot(struct udf_node *udf_node, int slot, struct long_ad *icb,
1814 1.1 reinoud int *eof) {
1815 1.1 reinoud struct file_entry *fe;
1816 1.1 reinoud struct extfile_entry *efe;
1817 1.1 reinoud struct alloc_ext_entry *ext;
1818 1.1 reinoud struct icb_tag *icbtag;
1819 1.1 reinoud struct short_ad *short_ad;
1820 1.11 reinoud struct long_ad *long_ad, l_icb;
1821 1.1 reinoud uint32_t offset;
1822 1.35 christos uint32_t dscr_size, l_ea, l_ad, flags;
1823 1.1 reinoud uint8_t *data_pos;
1824 1.1 reinoud int icbflags, addr_type, adlen, extnr;
1825 1.1 reinoud
1826 1.1 reinoud fe = udf_node->fe;
1827 1.1 reinoud efe = udf_node->efe;
1828 1.1 reinoud if (fe) {
1829 1.1 reinoud icbtag = &fe->icbtag;
1830 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
1831 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
1832 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
1833 1.1 reinoud data_pos = (uint8_t *) fe + dscr_size + l_ea;
1834 1.1 reinoud } else {
1835 1.1 reinoud icbtag = &efe->icbtag;
1836 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
1837 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
1838 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
1839 1.1 reinoud data_pos = (uint8_t *) efe + dscr_size + l_ea;
1840 1.1 reinoud }
1841 1.1 reinoud
1842 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
1843 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
1844 1.1 reinoud
1845 1.1 reinoud /* just in case we're called on an intern, its EOF */
1846 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
1847 1.1 reinoud memset(icb, 0, sizeof(struct long_ad));
1848 1.1 reinoud *eof = 1;
1849 1.1 reinoud return;
1850 1.1 reinoud }
1851 1.1 reinoud
1852 1.1 reinoud adlen = 0;
1853 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
1854 1.1 reinoud adlen = sizeof(struct short_ad);
1855 1.1 reinoud } else if (addr_type == UDF_ICB_LONG_ALLOC) {
1856 1.1 reinoud adlen = sizeof(struct long_ad);
1857 1.1 reinoud }
1858 1.1 reinoud
1859 1.1 reinoud /* if offset too big, we go to the allocation extensions */
1860 1.1 reinoud offset = slot * adlen;
1861 1.3 reinoud extnr = -1;
1862 1.11 reinoud while (offset >= l_ad) {
1863 1.11 reinoud /* check if our last entry is a redirect */
1864 1.11 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
1865 1.11 reinoud short_ad = (struct short_ad *) (data_pos + l_ad-adlen);
1866 1.11 reinoud l_icb.len = short_ad->len;
1867 1.11 reinoud l_icb.loc.part_num = udf_node->loc.loc.part_num;
1868 1.11 reinoud l_icb.loc.lb_num = short_ad->lb_num;
1869 1.11 reinoud } else {
1870 1.11 reinoud KASSERT(addr_type == UDF_ICB_LONG_ALLOC);
1871 1.11 reinoud long_ad = (struct long_ad *) (data_pos + l_ad-adlen);
1872 1.11 reinoud l_icb = *long_ad;
1873 1.11 reinoud }
1874 1.11 reinoud flags = UDF_EXT_FLAGS(udf_rw32(l_icb.len));
1875 1.11 reinoud if (flags != UDF_EXT_REDIRECT) {
1876 1.11 reinoud l_ad = 0; /* force EOF */
1877 1.11 reinoud break;
1878 1.11 reinoud }
1879 1.11 reinoud
1880 1.11 reinoud /* advance to next extent */
1881 1.3 reinoud extnr++;
1882 1.11 reinoud if (extnr >= udf_node->num_extensions) {
1883 1.11 reinoud l_ad = 0; /* force EOF */
1884 1.11 reinoud break;
1885 1.11 reinoud }
1886 1.11 reinoud offset = offset - l_ad;
1887 1.1 reinoud ext = udf_node->ext[extnr];
1888 1.1 reinoud dscr_size = sizeof(struct alloc_ext_entry) -1;
1889 1.1 reinoud l_ad = udf_rw32(ext->l_ad);
1890 1.3 reinoud data_pos = (uint8_t *) ext + dscr_size;
1891 1.1 reinoud }
1892 1.1 reinoud
1893 1.11 reinoud /* XXX l_ad == 0 should be enough to check */
1894 1.1 reinoud *eof = (offset >= l_ad) || (l_ad == 0);
1895 1.1 reinoud if (*eof) {
1896 1.11 reinoud DPRINTF(PARANOIDADWLK, ("returning EOF, extnr %d, offset %d, "
1897 1.11 reinoud "l_ad %d\n", extnr, offset, l_ad));
1898 1.1 reinoud memset(icb, 0, sizeof(struct long_ad));
1899 1.1 reinoud return;
1900 1.1 reinoud }
1901 1.1 reinoud
1902 1.1 reinoud /* get the element */
1903 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
1904 1.1 reinoud short_ad = (struct short_ad *) (data_pos + offset);
1905 1.1 reinoud icb->len = short_ad->len;
1906 1.5 reinoud icb->loc.part_num = udf_node->loc.loc.part_num;
1907 1.1 reinoud icb->loc.lb_num = short_ad->lb_num;
1908 1.1 reinoud } else if (addr_type == UDF_ICB_LONG_ALLOC) {
1909 1.1 reinoud long_ad = (struct long_ad *) (data_pos + offset);
1910 1.1 reinoud *icb = *long_ad;
1911 1.1 reinoud }
1912 1.11 reinoud DPRINTF(PARANOIDADWLK, ("returning element : v %d, lb %d, len %d, "
1913 1.11 reinoud "flags %d\n", icb->loc.part_num, icb->loc.lb_num,
1914 1.11 reinoud UDF_EXT_LEN(icb->len), UDF_EXT_FLAGS(icb->len)));
1915 1.1 reinoud }
1916 1.1 reinoud
1917 1.1 reinoud /* --------------------------------------------------------------------- */
1918 1.1 reinoud
1919 1.1 reinoud int
1920 1.11 reinoud udf_append_adslot(struct udf_node *udf_node, int *slot, struct long_ad *icb) {
1921 1.11 reinoud struct udf_mount *ump = udf_node->ump;
1922 1.13 reinoud union dscrptr *dscr, *extdscr;
1923 1.1 reinoud struct file_entry *fe;
1924 1.1 reinoud struct extfile_entry *efe;
1925 1.1 reinoud struct alloc_ext_entry *ext;
1926 1.1 reinoud struct icb_tag *icbtag;
1927 1.1 reinoud struct short_ad *short_ad;
1928 1.11 reinoud struct long_ad *long_ad, o_icb, l_icb;
1929 1.1 reinoud uint64_t logblks_rec, *logblks_rec_p;
1930 1.16 reinoud uint64_t lmapping;
1931 1.11 reinoud uint32_t offset, rest, len, lb_num;
1932 1.1 reinoud uint32_t lb_size, dscr_size, l_ea, l_ad, *l_ad_p, max_l_ad, crclen;
1933 1.11 reinoud uint32_t flags;
1934 1.11 reinoud uint16_t vpart_num;
1935 1.1 reinoud uint8_t *data_pos;
1936 1.1 reinoud int icbflags, addr_type, adlen, extnr;
1937 1.11 reinoud int error;
1938 1.1 reinoud
1939 1.11 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
1940 1.17 reinoud vpart_num = udf_rw16(udf_node->loc.loc.part_num);
1941 1.1 reinoud
1942 1.17 reinoud /* determine what descriptor we are in */
1943 1.1 reinoud fe = udf_node->fe;
1944 1.1 reinoud efe = udf_node->efe;
1945 1.1 reinoud if (fe) {
1946 1.1 reinoud icbtag = &fe->icbtag;
1947 1.1 reinoud dscr = (union dscrptr *) fe;
1948 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
1949 1.1 reinoud
1950 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
1951 1.1 reinoud l_ad_p = &fe->l_ad;
1952 1.1 reinoud logblks_rec_p = &fe->logblks_rec;
1953 1.1 reinoud } else {
1954 1.1 reinoud icbtag = &efe->icbtag;
1955 1.1 reinoud dscr = (union dscrptr *) efe;
1956 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
1957 1.1 reinoud
1958 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
1959 1.1 reinoud l_ad_p = &efe->l_ad;
1960 1.1 reinoud logblks_rec_p = &efe->logblks_rec;
1961 1.1 reinoud }
1962 1.1 reinoud data_pos = (uint8_t *) dscr + dscr_size + l_ea;
1963 1.1 reinoud max_l_ad = lb_size - dscr_size - l_ea;
1964 1.1 reinoud
1965 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
1966 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
1967 1.1 reinoud
1968 1.1 reinoud /* just in case we're called on an intern, its EOF */
1969 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
1970 1.1 reinoud panic("udf_append_adslot on UDF_ICB_INTERN_ALLOC\n");
1971 1.1 reinoud }
1972 1.1 reinoud
1973 1.1 reinoud adlen = 0;
1974 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
1975 1.1 reinoud adlen = sizeof(struct short_ad);
1976 1.1 reinoud } else if (addr_type == UDF_ICB_LONG_ALLOC) {
1977 1.1 reinoud adlen = sizeof(struct long_ad);
1978 1.1 reinoud }
1979 1.1 reinoud
1980 1.17 reinoud /* clean up given long_ad since it can be a synthesized one */
1981 1.11 reinoud flags = UDF_EXT_FLAGS(udf_rw32(icb->len));
1982 1.11 reinoud if (flags == UDF_EXT_FREE) {
1983 1.9 reinoud icb->loc.part_num = udf_rw16(0);
1984 1.9 reinoud icb->loc.lb_num = udf_rw32(0);
1985 1.9 reinoud }
1986 1.9 reinoud
1987 1.1 reinoud /* if offset too big, we go to the allocation extensions */
1988 1.11 reinoud l_ad = udf_rw32(*l_ad_p);
1989 1.11 reinoud offset = (*slot) * adlen;
1990 1.11 reinoud extnr = -1;
1991 1.11 reinoud while (offset >= l_ad) {
1992 1.11 reinoud /* check if our last entry is a redirect */
1993 1.11 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
1994 1.11 reinoud short_ad = (struct short_ad *) (data_pos + l_ad-adlen);
1995 1.11 reinoud l_icb.len = short_ad->len;
1996 1.11 reinoud l_icb.loc.part_num = udf_node->loc.loc.part_num;
1997 1.11 reinoud l_icb.loc.lb_num = short_ad->lb_num;
1998 1.11 reinoud } else {
1999 1.11 reinoud KASSERT(addr_type == UDF_ICB_LONG_ALLOC);
2000 1.11 reinoud long_ad = (struct long_ad *) (data_pos + l_ad-adlen);
2001 1.11 reinoud l_icb = *long_ad;
2002 1.11 reinoud }
2003 1.11 reinoud flags = UDF_EXT_FLAGS(udf_rw32(l_icb.len));
2004 1.11 reinoud if (flags != UDF_EXT_REDIRECT) {
2005 1.11 reinoud /* only one past the last one is adressable */
2006 1.11 reinoud break;
2007 1.11 reinoud }
2008 1.11 reinoud
2009 1.11 reinoud /* advance to next extent */
2010 1.11 reinoud extnr++;
2011 1.11 reinoud KASSERT(extnr < udf_node->num_extensions);
2012 1.11 reinoud offset = offset - l_ad;
2013 1.11 reinoud
2014 1.1 reinoud ext = udf_node->ext[extnr];
2015 1.1 reinoud dscr = (union dscrptr *) ext;
2016 1.1 reinoud dscr_size = sizeof(struct alloc_ext_entry) -1;
2017 1.11 reinoud max_l_ad = lb_size - dscr_size;
2018 1.1 reinoud l_ad_p = &ext->l_ad;
2019 1.11 reinoud l_ad = udf_rw32(*l_ad_p);
2020 1.11 reinoud data_pos = (uint8_t *) ext + dscr_size;
2021 1.11 reinoud }
2022 1.11 reinoud DPRINTF(PARANOIDADWLK, ("append, ext %d, offset %d, l_ad %d\n",
2023 1.11 reinoud extnr, offset, udf_rw32(*l_ad_p)));
2024 1.11 reinoud KASSERT(l_ad == udf_rw32(*l_ad_p));
2025 1.1 reinoud
2026 1.1 reinoud /* offset is offset within the current (E)FE/AED */
2027 1.1 reinoud l_ad = udf_rw32(*l_ad_p);
2028 1.22 reinoud crclen = udf_rw16(dscr->tag.desc_crc_len);
2029 1.1 reinoud logblks_rec = udf_rw64(*logblks_rec_p);
2030 1.1 reinoud
2031 1.1 reinoud /* overwriting old piece? */
2032 1.1 reinoud if (offset < l_ad) {
2033 1.1 reinoud /* overwrite entry; compensate for the old element */
2034 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
2035 1.1 reinoud short_ad = (struct short_ad *) (data_pos + offset);
2036 1.1 reinoud o_icb.len = short_ad->len;
2037 1.1 reinoud o_icb.loc.part_num = udf_rw16(0); /* ignore */
2038 1.1 reinoud o_icb.loc.lb_num = short_ad->lb_num;
2039 1.1 reinoud } else if (addr_type == UDF_ICB_LONG_ALLOC) {
2040 1.1 reinoud long_ad = (struct long_ad *) (data_pos + offset);
2041 1.1 reinoud o_icb = *long_ad;
2042 1.1 reinoud } else {
2043 1.1 reinoud panic("Invalid address type in udf_append_adslot\n");
2044 1.1 reinoud }
2045 1.1 reinoud
2046 1.1 reinoud len = udf_rw32(o_icb.len);
2047 1.1 reinoud if (UDF_EXT_FLAGS(len) == UDF_EXT_ALLOCATED) {
2048 1.1 reinoud /* adjust counts */
2049 1.1 reinoud len = UDF_EXT_LEN(len);
2050 1.1 reinoud logblks_rec -= (len + lb_size -1) / lb_size;
2051 1.1 reinoud }
2052 1.1 reinoud }
2053 1.1 reinoud
2054 1.11 reinoud /* check if we're not appending a redirection */
2055 1.11 reinoud flags = UDF_EXT_FLAGS(udf_rw32(icb->len));
2056 1.11 reinoud KASSERT(flags != UDF_EXT_REDIRECT);
2057 1.11 reinoud
2058 1.11 reinoud /* round down available space */
2059 1.11 reinoud rest = adlen * ((max_l_ad - offset) / adlen);
2060 1.1 reinoud if (rest <= adlen) {
2061 1.11 reinoud /* have to append aed, see if we already have a spare one */
2062 1.11 reinoud extnr++;
2063 1.11 reinoud ext = udf_node->ext[extnr];
2064 1.11 reinoud l_icb = udf_node->ext_loc[extnr];
2065 1.11 reinoud if (ext == NULL) {
2066 1.11 reinoud DPRINTF(ALLOC,("adding allocation extent %d\n", extnr));
2067 1.17 reinoud
2068 1.26 reinoud error = udf_reserve_space(ump, NULL, UDF_C_NODE,
2069 1.26 reinoud vpart_num, 1, /* can fail */ false);
2070 1.26 reinoud if (error) {
2071 1.26 reinoud printf("UDF: couldn't reserve space for AED!\n");
2072 1.26 reinoud return error;
2073 1.26 reinoud }
2074 1.26 reinoud error = udf_allocate_space(ump, NULL, UDF_C_NODE,
2075 1.26 reinoud vpart_num, 1, &lmapping);
2076 1.11 reinoud lb_num = lmapping;
2077 1.11 reinoud if (error)
2078 1.26 reinoud panic("UDF: couldn't allocate AED!\n");
2079 1.11 reinoud
2080 1.11 reinoud /* initialise pointer to location */
2081 1.11 reinoud memset(&l_icb, 0, sizeof(struct long_ad));
2082 1.11 reinoud l_icb.len = udf_rw32(lb_size | UDF_EXT_REDIRECT);
2083 1.11 reinoud l_icb.loc.lb_num = udf_rw32(lb_num);
2084 1.11 reinoud l_icb.loc.part_num = udf_rw16(vpart_num);
2085 1.11 reinoud
2086 1.11 reinoud /* create new aed descriptor */
2087 1.13 reinoud udf_create_logvol_dscr(ump, udf_node, &l_icb, &extdscr);
2088 1.13 reinoud ext = &extdscr->aee;
2089 1.11 reinoud
2090 1.11 reinoud udf_inittag(ump, &ext->tag, TAGID_ALLOCEXTENT, lb_num);
2091 1.11 reinoud dscr_size = sizeof(struct alloc_ext_entry) -1;
2092 1.11 reinoud max_l_ad = lb_size - dscr_size;
2093 1.11 reinoud memset(ext->data, 0, max_l_ad);
2094 1.11 reinoud ext->l_ad = udf_rw32(0);
2095 1.11 reinoud ext->tag.desc_crc_len =
2096 1.22 reinoud udf_rw16(dscr_size - UDF_DESC_TAG_LENGTH);
2097 1.11 reinoud
2098 1.11 reinoud /* declare aed */
2099 1.11 reinoud udf_node->num_extensions++;
2100 1.11 reinoud udf_node->ext_loc[extnr] = l_icb;
2101 1.11 reinoud udf_node->ext[extnr] = ext;
2102 1.11 reinoud }
2103 1.11 reinoud /* add redirect and adjust l_ad and crclen for old descr */
2104 1.11 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
2105 1.11 reinoud short_ad = (struct short_ad *) (data_pos + offset);
2106 1.11 reinoud short_ad->len = l_icb.len;
2107 1.11 reinoud short_ad->lb_num = l_icb.loc.lb_num;
2108 1.11 reinoud } else if (addr_type == UDF_ICB_LONG_ALLOC) {
2109 1.11 reinoud long_ad = (struct long_ad *) (data_pos + offset);
2110 1.11 reinoud *long_ad = l_icb;
2111 1.11 reinoud }
2112 1.11 reinoud l_ad += adlen;
2113 1.11 reinoud crclen += adlen;
2114 1.22 reinoud dscr->tag.desc_crc_len = udf_rw16(crclen);
2115 1.11 reinoud *l_ad_p = udf_rw32(l_ad);
2116 1.11 reinoud
2117 1.11 reinoud /* advance to the new extension */
2118 1.11 reinoud KASSERT(ext != NULL);
2119 1.11 reinoud dscr = (union dscrptr *) ext;
2120 1.11 reinoud dscr_size = sizeof(struct alloc_ext_entry) -1;
2121 1.11 reinoud max_l_ad = lb_size - dscr_size;
2122 1.11 reinoud data_pos = (uint8_t *) dscr + dscr_size;
2123 1.11 reinoud
2124 1.11 reinoud l_ad_p = &ext->l_ad;
2125 1.11 reinoud l_ad = udf_rw32(*l_ad_p);
2126 1.22 reinoud crclen = udf_rw16(dscr->tag.desc_crc_len);
2127 1.11 reinoud offset = 0;
2128 1.11 reinoud
2129 1.11 reinoud /* adjust callees slot count for link insert */
2130 1.11 reinoud *slot += 1;
2131 1.1 reinoud }
2132 1.1 reinoud
2133 1.1 reinoud /* write out the element */
2134 1.11 reinoud DPRINTF(PARANOIDADWLK, ("adding element : %p : v %d, lb %d, "
2135 1.11 reinoud "len %d, flags %d\n", data_pos + offset,
2136 1.11 reinoud icb->loc.part_num, icb->loc.lb_num,
2137 1.11 reinoud UDF_EXT_LEN(icb->len), UDF_EXT_FLAGS(icb->len)));
2138 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
2139 1.1 reinoud short_ad = (struct short_ad *) (data_pos + offset);
2140 1.1 reinoud short_ad->len = icb->len;
2141 1.1 reinoud short_ad->lb_num = icb->loc.lb_num;
2142 1.1 reinoud } else if (addr_type == UDF_ICB_LONG_ALLOC) {
2143 1.1 reinoud long_ad = (struct long_ad *) (data_pos + offset);
2144 1.1 reinoud *long_ad = *icb;
2145 1.1 reinoud }
2146 1.1 reinoud
2147 1.1 reinoud /* adjust logblks recorded count */
2148 1.23 reinoud len = udf_rw32(icb->len);
2149 1.23 reinoud flags = UDF_EXT_FLAGS(len);
2150 1.11 reinoud if (flags == UDF_EXT_ALLOCATED)
2151 1.23 reinoud logblks_rec += (UDF_EXT_LEN(len) + lb_size -1) / lb_size;
2152 1.1 reinoud *logblks_rec_p = udf_rw64(logblks_rec);
2153 1.1 reinoud
2154 1.1 reinoud /* adjust l_ad and crclen when needed */
2155 1.1 reinoud if (offset >= l_ad) {
2156 1.1 reinoud l_ad += adlen;
2157 1.1 reinoud crclen += adlen;
2158 1.22 reinoud dscr->tag.desc_crc_len = udf_rw16(crclen);
2159 1.1 reinoud *l_ad_p = udf_rw32(l_ad);
2160 1.1 reinoud }
2161 1.1 reinoud
2162 1.1 reinoud return 0;
2163 1.1 reinoud }
2164 1.1 reinoud
2165 1.1 reinoud /* --------------------------------------------------------------------- */
2166 1.1 reinoud
2167 1.11 reinoud static void
2168 1.11 reinoud udf_count_alloc_exts(struct udf_node *udf_node)
2169 1.11 reinoud {
2170 1.11 reinoud struct long_ad s_ad;
2171 1.11 reinoud uint32_t lb_num, len, flags;
2172 1.11 reinoud uint16_t vpart_num;
2173 1.11 reinoud int slot, eof;
2174 1.11 reinoud int num_extents, extnr;
2175 1.11 reinoud
2176 1.11 reinoud if (udf_node->num_extensions == 0)
2177 1.11 reinoud return;
2178 1.11 reinoud
2179 1.11 reinoud /* count number of allocation extents in use */
2180 1.11 reinoud num_extents = 0;
2181 1.11 reinoud slot = 0;
2182 1.11 reinoud for (;;) {
2183 1.11 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
2184 1.11 reinoud if (eof)
2185 1.11 reinoud break;
2186 1.11 reinoud len = udf_rw32(s_ad.len);
2187 1.11 reinoud flags = UDF_EXT_FLAGS(len);
2188 1.11 reinoud
2189 1.11 reinoud if (flags == UDF_EXT_REDIRECT)
2190 1.11 reinoud num_extents++;
2191 1.11 reinoud
2192 1.11 reinoud slot++;
2193 1.11 reinoud }
2194 1.11 reinoud
2195 1.11 reinoud DPRINTF(ALLOC, ("udf_count_alloc_ext counted %d live extents\n",
2196 1.11 reinoud num_extents));
2197 1.11 reinoud
2198 1.11 reinoud /* XXX choice: we could delay freeing them on node writeout */
2199 1.11 reinoud /* free excess entries */
2200 1.11 reinoud extnr = num_extents;
2201 1.11 reinoud for (;extnr < udf_node->num_extensions; extnr++) {
2202 1.11 reinoud DPRINTF(ALLOC, ("freeing alloc ext %d\n", extnr));
2203 1.11 reinoud /* free dscriptor */
2204 1.11 reinoud s_ad = udf_node->ext_loc[extnr];
2205 1.11 reinoud udf_free_logvol_dscr(udf_node->ump, &s_ad,
2206 1.11 reinoud udf_node->ext[extnr]);
2207 1.11 reinoud udf_node->ext[extnr] = NULL;
2208 1.11 reinoud
2209 1.11 reinoud /* free disc space */
2210 1.11 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
2211 1.11 reinoud vpart_num = udf_rw16(s_ad.loc.part_num);
2212 1.11 reinoud udf_free_allocated_space(udf_node->ump, lb_num, vpart_num, 1);
2213 1.11 reinoud
2214 1.11 reinoud memset(&udf_node->ext_loc[extnr], 0, sizeof(struct long_ad));
2215 1.11 reinoud }
2216 1.11 reinoud
2217 1.11 reinoud /* set our new number of allocation extents */
2218 1.11 reinoud udf_node->num_extensions = num_extents;
2219 1.11 reinoud }
2220 1.11 reinoud
2221 1.11 reinoud
2222 1.11 reinoud /* --------------------------------------------------------------------- */
2223 1.11 reinoud
2224 1.1 reinoud /*
2225 1.1 reinoud * Adjust the node's allocation descriptors to reflect the new mapping; do
2226 1.1 reinoud * take note that we might glue to existing allocation descriptors.
2227 1.1 reinoud *
2228 1.1 reinoud * XXX Note there can only be one allocation being recorded/mount; maybe
2229 1.1 reinoud * explicit allocation in shedule thread?
2230 1.1 reinoud */
2231 1.1 reinoud
2232 1.1 reinoud static void
2233 1.1 reinoud udf_record_allocation_in_node(struct udf_mount *ump, struct buf *buf,
2234 1.1 reinoud uint16_t vpart_num, uint64_t *mapping, struct long_ad *node_ad_cpy)
2235 1.1 reinoud {
2236 1.1 reinoud struct vnode *vp = buf->b_vp;
2237 1.1 reinoud struct udf_node *udf_node = VTOI(vp);
2238 1.1 reinoud struct file_entry *fe;
2239 1.1 reinoud struct extfile_entry *efe;
2240 1.1 reinoud struct icb_tag *icbtag;
2241 1.1 reinoud struct long_ad s_ad, c_ad;
2242 1.1 reinoud uint64_t inflen, from, till;
2243 1.1 reinoud uint64_t foffset, end_foffset, restart_foffset;
2244 1.1 reinoud uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
2245 1.31 reinoud uint32_t max_len;
2246 1.1 reinoud uint32_t num_lb, len, flags, lb_num;
2247 1.1 reinoud uint32_t run_start;
2248 1.9 reinoud uint32_t slot_offset, replace_len, replace;
2249 1.1 reinoud int addr_type, icbflags;
2250 1.16 reinoud // int udf_c_type = buf->b_udf_c_type;
2251 1.1 reinoud int lb_size, run_length, eof;
2252 1.1 reinoud int slot, cpy_slot, cpy_slots, restart_slot;
2253 1.1 reinoud int error;
2254 1.1 reinoud
2255 1.1 reinoud DPRINTF(ALLOC, ("udf_record_allocation_in_node\n"));
2256 1.1 reinoud
2257 1.16 reinoud #if 0
2258 1.16 reinoud /* XXX disable sanity check for now */
2259 1.1 reinoud /* sanity check ... should be panic ? */
2260 1.1 reinoud if ((udf_c_type != UDF_C_USERDATA) && (udf_c_type != UDF_C_FIDS))
2261 1.1 reinoud return;
2262 1.16 reinoud #endif
2263 1.1 reinoud
2264 1.1 reinoud lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
2265 1.31 reinoud max_len = ((UDF_EXT_MAXLEN / lb_size) * lb_size);
2266 1.1 reinoud
2267 1.1 reinoud /* do the job */
2268 1.1 reinoud UDF_LOCK_NODE(udf_node, 0); /* XXX can deadlock ? */
2269 1.9 reinoud udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
2270 1.1 reinoud
2271 1.1 reinoud fe = udf_node->fe;
2272 1.1 reinoud efe = udf_node->efe;
2273 1.1 reinoud if (fe) {
2274 1.1 reinoud icbtag = &fe->icbtag;
2275 1.1 reinoud inflen = udf_rw64(fe->inf_len);
2276 1.1 reinoud } else {
2277 1.1 reinoud icbtag = &efe->icbtag;
2278 1.1 reinoud inflen = udf_rw64(efe->inf_len);
2279 1.1 reinoud }
2280 1.1 reinoud
2281 1.1 reinoud /* do check if `till' is not past file information length */
2282 1.1 reinoud from = buf->b_lblkno * lb_size;
2283 1.1 reinoud till = MIN(inflen, from + buf->b_resid);
2284 1.1 reinoud
2285 1.1 reinoud num_lb = (till - from + lb_size -1) / lb_size;
2286 1.1 reinoud
2287 1.9 reinoud DPRINTF(ALLOC, ("record allocation from %"PRIu64" + %d\n", from, buf->b_bcount));
2288 1.1 reinoud
2289 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
2290 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2291 1.1 reinoud
2292 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
2293 1.1 reinoud /* nothing to do */
2294 1.1 reinoud /* XXX clean up rest of node? just in case? */
2295 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2296 1.1 reinoud return;
2297 1.1 reinoud }
2298 1.1 reinoud
2299 1.1 reinoud slot = 0;
2300 1.1 reinoud cpy_slot = 0;
2301 1.1 reinoud foffset = 0;
2302 1.1 reinoud
2303 1.1 reinoud /* 1) copy till first overlap piece to the rewrite buffer */
2304 1.1 reinoud for (;;) {
2305 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
2306 1.1 reinoud if (eof) {
2307 1.1 reinoud DPRINTF(WRITE,
2308 1.1 reinoud ("Record allocation in node "
2309 1.1 reinoud "failed: encountered EOF\n"));
2310 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2311 1.1 reinoud buf->b_error = EINVAL;
2312 1.1 reinoud return;
2313 1.1 reinoud }
2314 1.1 reinoud len = udf_rw32(s_ad.len);
2315 1.1 reinoud flags = UDF_EXT_FLAGS(len);
2316 1.1 reinoud len = UDF_EXT_LEN(len);
2317 1.1 reinoud
2318 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
2319 1.1 reinoud slot++;
2320 1.1 reinoud continue;
2321 1.1 reinoud }
2322 1.1 reinoud
2323 1.1 reinoud end_foffset = foffset + len;
2324 1.1 reinoud if (end_foffset > from)
2325 1.1 reinoud break; /* found */
2326 1.1 reinoud
2327 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
2328 1.1 reinoud
2329 1.1 reinoud DPRINTF(ALLOC, ("\t1: vp %d, lb %d, len %d, flags %d "
2330 1.1 reinoud "-> stack\n",
2331 1.1 reinoud udf_rw16(s_ad.loc.part_num),
2332 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
2333 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
2334 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
2335 1.1 reinoud
2336 1.1 reinoud foffset = end_foffset;
2337 1.1 reinoud slot++;
2338 1.1 reinoud }
2339 1.1 reinoud restart_slot = slot;
2340 1.1 reinoud restart_foffset = foffset;
2341 1.1 reinoud
2342 1.1 reinoud /* 2) trunc overlapping slot at overlap and copy it */
2343 1.1 reinoud slot_offset = from - foffset;
2344 1.1 reinoud if (slot_offset > 0) {
2345 1.1 reinoud DPRINTF(ALLOC, ("\tslot_offset = %d, flags = %d (%d)\n",
2346 1.1 reinoud slot_offset, flags >> 30, flags));
2347 1.1 reinoud
2348 1.1 reinoud s_ad.len = udf_rw32(slot_offset | flags);
2349 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
2350 1.1 reinoud
2351 1.1 reinoud DPRINTF(ALLOC, ("\t2: vp %d, lb %d, len %d, flags %d "
2352 1.1 reinoud "-> stack\n",
2353 1.1 reinoud udf_rw16(s_ad.loc.part_num),
2354 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
2355 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
2356 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
2357 1.1 reinoud }
2358 1.1 reinoud foffset += slot_offset;
2359 1.1 reinoud
2360 1.1 reinoud /* 3) insert new mappings */
2361 1.1 reinoud memset(&s_ad, 0, sizeof(struct long_ad));
2362 1.1 reinoud lb_num = 0;
2363 1.1 reinoud for (lb_num = 0; lb_num < num_lb; lb_num++) {
2364 1.1 reinoud run_start = mapping[lb_num];
2365 1.1 reinoud run_length = 1;
2366 1.1 reinoud while (lb_num < num_lb-1) {
2367 1.1 reinoud if (mapping[lb_num+1] != mapping[lb_num]+1)
2368 1.1 reinoud if (mapping[lb_num+1] != mapping[lb_num])
2369 1.1 reinoud break;
2370 1.1 reinoud run_length++;
2371 1.1 reinoud lb_num++;
2372 1.1 reinoud }
2373 1.1 reinoud /* insert slot for this mapping */
2374 1.1 reinoud len = run_length * lb_size;
2375 1.1 reinoud
2376 1.1 reinoud /* bounds checking */
2377 1.1 reinoud if (foffset + len > till)
2378 1.1 reinoud len = till - foffset;
2379 1.1 reinoud KASSERT(foffset + len <= inflen);
2380 1.1 reinoud
2381 1.1 reinoud s_ad.len = udf_rw32(len | UDF_EXT_ALLOCATED);
2382 1.1 reinoud s_ad.loc.part_num = udf_rw16(vpart_num);
2383 1.1 reinoud s_ad.loc.lb_num = udf_rw32(run_start);
2384 1.1 reinoud
2385 1.1 reinoud foffset += len;
2386 1.1 reinoud
2387 1.1 reinoud /* paranoia */
2388 1.1 reinoud if (len == 0) {
2389 1.1 reinoud DPRINTF(WRITE,
2390 1.1 reinoud ("Record allocation in node "
2391 1.1 reinoud "failed: insert failed\n"));
2392 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2393 1.1 reinoud buf->b_error = EINVAL;
2394 1.1 reinoud return;
2395 1.1 reinoud }
2396 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
2397 1.1 reinoud
2398 1.1 reinoud DPRINTF(ALLOC, ("\t3: insert new mapping vp %d lb %d, len %d, "
2399 1.1 reinoud "flags %d -> stack\n",
2400 1.1 reinoud udf_rw16(s_ad.loc.part_num), udf_rw32(s_ad.loc.lb_num),
2401 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
2402 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
2403 1.1 reinoud }
2404 1.1 reinoud
2405 1.1 reinoud /* 4) pop replaced length */
2406 1.9 reinoud slot = restart_slot;
2407 1.1 reinoud foffset = restart_foffset;
2408 1.1 reinoud
2409 1.9 reinoud replace_len = till - foffset; /* total amount of bytes to pop */
2410 1.8 reinoud slot_offset = from - foffset; /* offset in first encounted slot */
2411 1.9 reinoud KASSERT((slot_offset % lb_size) == 0);
2412 1.9 reinoud
2413 1.1 reinoud for (;;) {
2414 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
2415 1.1 reinoud if (eof)
2416 1.1 reinoud break;
2417 1.1 reinoud
2418 1.1 reinoud len = udf_rw32(s_ad.len);
2419 1.1 reinoud flags = UDF_EXT_FLAGS(len);
2420 1.1 reinoud len = UDF_EXT_LEN(len);
2421 1.1 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
2422 1.1 reinoud
2423 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
2424 1.1 reinoud slot++;
2425 1.1 reinoud continue;
2426 1.1 reinoud }
2427 1.1 reinoud
2428 1.8 reinoud DPRINTF(ALLOC, ("\t4i: got slot %d, slot_offset %d, "
2429 1.9 reinoud "replace_len %d, "
2430 1.8 reinoud "vp %d, lb %d, len %d, flags %d\n",
2431 1.9 reinoud slot, slot_offset, replace_len,
2432 1.8 reinoud udf_rw16(s_ad.loc.part_num),
2433 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
2434 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
2435 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
2436 1.1 reinoud
2437 1.9 reinoud /* adjust for slot offset */
2438 1.9 reinoud if (slot_offset) {
2439 1.9 reinoud DPRINTF(ALLOC, ("\t4s: skipping %d\n", slot_offset));
2440 1.9 reinoud lb_num += slot_offset / lb_size;
2441 1.9 reinoud len -= slot_offset;
2442 1.9 reinoud foffset += slot_offset;
2443 1.9 reinoud replace_len -= slot_offset;
2444 1.9 reinoud
2445 1.9 reinoud /* mark adjusted */
2446 1.9 reinoud slot_offset = 0;
2447 1.9 reinoud }
2448 1.9 reinoud
2449 1.9 reinoud /* advance for (the rest of) this slot */
2450 1.9 reinoud replace = MIN(len, replace_len);
2451 1.9 reinoud DPRINTF(ALLOC, ("\t4d: replacing %d\n", replace));
2452 1.9 reinoud
2453 1.9 reinoud /* advance for this slot */
2454 1.9 reinoud if (replace) {
2455 1.10 reinoud /* note: dont round DOWN on num_lb since we then
2456 1.10 reinoud * forget the last partial one */
2457 1.9 reinoud num_lb = (replace + lb_size - 1) / lb_size;
2458 1.9 reinoud if (flags != UDF_EXT_FREE) {
2459 1.9 reinoud udf_free_allocated_space(ump, lb_num,
2460 1.9 reinoud udf_rw16(s_ad.loc.part_num), num_lb);
2461 1.9 reinoud }
2462 1.9 reinoud lb_num += num_lb;
2463 1.9 reinoud len -= replace;
2464 1.9 reinoud foffset += replace;
2465 1.9 reinoud replace_len -= replace;
2466 1.9 reinoud }
2467 1.1 reinoud
2468 1.9 reinoud /* do we have a slot tail ? */
2469 1.1 reinoud if (len) {
2470 1.9 reinoud KASSERT(foffset % lb_size == 0);
2471 1.1 reinoud
2472 1.1 reinoud /* we arrived at our point, push remainder */
2473 1.1 reinoud s_ad.len = udf_rw32(len | flags);
2474 1.1 reinoud s_ad.loc.lb_num = udf_rw32(lb_num);
2475 1.9 reinoud if (flags == UDF_EXT_FREE)
2476 1.9 reinoud s_ad.loc.lb_num = udf_rw32(0);
2477 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
2478 1.1 reinoud foffset += len;
2479 1.1 reinoud slot++;
2480 1.1 reinoud
2481 1.1 reinoud DPRINTF(ALLOC, ("\t4: vp %d, lb %d, len %d, flags %d "
2482 1.1 reinoud "-> stack\n",
2483 1.1 reinoud udf_rw16(s_ad.loc.part_num),
2484 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
2485 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
2486 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
2487 1.1 reinoud break;
2488 1.1 reinoud }
2489 1.9 reinoud
2490 1.1 reinoud slot++;
2491 1.1 reinoud }
2492 1.1 reinoud
2493 1.1 reinoud /* 5) copy remainder */
2494 1.1 reinoud for (;;) {
2495 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
2496 1.1 reinoud if (eof)
2497 1.1 reinoud break;
2498 1.1 reinoud
2499 1.1 reinoud len = udf_rw32(s_ad.len);
2500 1.1 reinoud flags = UDF_EXT_FLAGS(len);
2501 1.1 reinoud len = UDF_EXT_LEN(len);
2502 1.1 reinoud
2503 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
2504 1.1 reinoud slot++;
2505 1.1 reinoud continue;
2506 1.1 reinoud }
2507 1.1 reinoud
2508 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
2509 1.1 reinoud
2510 1.1 reinoud DPRINTF(ALLOC, ("\t5: insert new mapping "
2511 1.1 reinoud "vp %d lb %d, len %d, flags %d "
2512 1.1 reinoud "-> stack\n",
2513 1.1 reinoud udf_rw16(s_ad.loc.part_num),
2514 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
2515 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
2516 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
2517 1.1 reinoud
2518 1.1 reinoud slot++;
2519 1.1 reinoud }
2520 1.1 reinoud
2521 1.1 reinoud /* 6) reset node descriptors */
2522 1.1 reinoud udf_wipe_adslots(udf_node);
2523 1.1 reinoud
2524 1.1 reinoud /* 7) copy back extents; merge when possible. Recounting on the fly */
2525 1.1 reinoud cpy_slots = cpy_slot;
2526 1.1 reinoud
2527 1.1 reinoud c_ad = node_ad_cpy[0];
2528 1.1 reinoud slot = 0;
2529 1.1 reinoud DPRINTF(ALLOC, ("\t7s: stack -> got mapping vp %d "
2530 1.1 reinoud "lb %d, len %d, flags %d\n",
2531 1.1 reinoud udf_rw16(c_ad.loc.part_num),
2532 1.1 reinoud udf_rw32(c_ad.loc.lb_num),
2533 1.1 reinoud UDF_EXT_LEN(udf_rw32(c_ad.len)),
2534 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
2535 1.1 reinoud
2536 1.1 reinoud for (cpy_slot = 1; cpy_slot < cpy_slots; cpy_slot++) {
2537 1.1 reinoud s_ad = node_ad_cpy[cpy_slot];
2538 1.1 reinoud
2539 1.1 reinoud DPRINTF(ALLOC, ("\t7i: stack -> got mapping vp %d "
2540 1.1 reinoud "lb %d, len %d, flags %d\n",
2541 1.1 reinoud udf_rw16(s_ad.loc.part_num),
2542 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
2543 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
2544 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
2545 1.1 reinoud
2546 1.1 reinoud /* see if we can merge */
2547 1.31 reinoud if (udf_ads_merge(max_len, lb_size, &c_ad, &s_ad)) {
2548 1.1 reinoud /* not mergable (anymore) */
2549 1.1 reinoud DPRINTF(ALLOC, ("\t7: appending vp %d lb %d, "
2550 1.1 reinoud "len %d, flags %d\n",
2551 1.1 reinoud udf_rw16(c_ad.loc.part_num),
2552 1.1 reinoud udf_rw32(c_ad.loc.lb_num),
2553 1.1 reinoud UDF_EXT_LEN(udf_rw32(c_ad.len)),
2554 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
2555 1.1 reinoud
2556 1.11 reinoud error = udf_append_adslot(udf_node, &slot, &c_ad);
2557 1.1 reinoud if (error) {
2558 1.1 reinoud buf->b_error = error;
2559 1.1 reinoud goto out;
2560 1.1 reinoud }
2561 1.1 reinoud c_ad = s_ad;
2562 1.1 reinoud slot++;
2563 1.1 reinoud }
2564 1.1 reinoud }
2565 1.1 reinoud
2566 1.1 reinoud /* 8) push rest slot (if any) */
2567 1.1 reinoud if (UDF_EXT_LEN(c_ad.len) > 0) {
2568 1.1 reinoud DPRINTF(ALLOC, ("\t8: last append vp %d lb %d, "
2569 1.1 reinoud "len %d, flags %d\n",
2570 1.1 reinoud udf_rw16(c_ad.loc.part_num),
2571 1.1 reinoud udf_rw32(c_ad.loc.lb_num),
2572 1.1 reinoud UDF_EXT_LEN(udf_rw32(c_ad.len)),
2573 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
2574 1.1 reinoud
2575 1.11 reinoud error = udf_append_adslot(udf_node, &slot, &c_ad);
2576 1.1 reinoud if (error) {
2577 1.1 reinoud buf->b_error = error;
2578 1.1 reinoud goto out;
2579 1.1 reinoud }
2580 1.1 reinoud }
2581 1.1 reinoud
2582 1.1 reinoud out:
2583 1.11 reinoud udf_count_alloc_exts(udf_node);
2584 1.11 reinoud
2585 1.1 reinoud /* the node's descriptors should now be sane */
2586 1.9 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
2587 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2588 1.1 reinoud
2589 1.1 reinoud KASSERT(orig_inflen == new_inflen);
2590 1.1 reinoud KASSERT(new_lbrec >= orig_lbrec);
2591 1.1 reinoud
2592 1.1 reinoud return;
2593 1.1 reinoud }
2594 1.1 reinoud
2595 1.1 reinoud /* --------------------------------------------------------------------- */
2596 1.1 reinoud
2597 1.1 reinoud int
2598 1.1 reinoud udf_grow_node(struct udf_node *udf_node, uint64_t new_size)
2599 1.1 reinoud {
2600 1.1 reinoud struct vnode *vp = udf_node->vnode;
2601 1.1 reinoud struct udf_mount *ump = udf_node->ump;
2602 1.1 reinoud struct file_entry *fe;
2603 1.1 reinoud struct extfile_entry *efe;
2604 1.1 reinoud struct icb_tag *icbtag;
2605 1.1 reinoud struct long_ad c_ad, s_ad;
2606 1.1 reinoud uint64_t size_diff, old_size, inflen, objsize, chunk, append_len;
2607 1.1 reinoud uint64_t foffset, end_foffset;
2608 1.1 reinoud uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
2609 1.31 reinoud uint32_t lb_size, unit_size, dscr_size, crclen, lastblock_grow;
2610 1.17 reinoud uint32_t icbflags, len, flags, max_len;
2611 1.1 reinoud uint32_t max_l_ad, l_ad, l_ea;
2612 1.17 reinoud uint16_t my_part, dst_part;
2613 1.35 christos uint8_t *evacuated_data;
2614 1.17 reinoud int addr_type;
2615 1.35 christos int slot;
2616 1.26 reinoud int eof, error;
2617 1.1 reinoud
2618 1.1 reinoud DPRINTF(ALLOC, ("udf_grow_node\n"));
2619 1.9 reinoud
2620 1.9 reinoud UDF_LOCK_NODE(udf_node, 0);
2621 1.1 reinoud udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
2622 1.1 reinoud
2623 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
2624 1.31 reinoud
2625 1.31 reinoud /* max_len in unit's IFF its a metadata node or metadata mirror node */
2626 1.31 reinoud unit_size = lb_size;
2627 1.31 reinoud if ((udf_node == ump->metadata_node) || (udf_node == ump->metadatamirror_node))
2628 1.31 reinoud unit_size = ump->metadata_alloc_unit_size * lb_size;
2629 1.31 reinoud max_len = ((UDF_EXT_MAXLEN / unit_size) * unit_size);
2630 1.1 reinoud
2631 1.1 reinoud fe = udf_node->fe;
2632 1.1 reinoud efe = udf_node->efe;
2633 1.1 reinoud if (fe) {
2634 1.1 reinoud icbtag = &fe->icbtag;
2635 1.1 reinoud inflen = udf_rw64(fe->inf_len);
2636 1.1 reinoud objsize = inflen;
2637 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
2638 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
2639 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
2640 1.1 reinoud } else {
2641 1.1 reinoud icbtag = &efe->icbtag;
2642 1.1 reinoud inflen = udf_rw64(efe->inf_len);
2643 1.1 reinoud objsize = udf_rw64(efe->obj_size);
2644 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
2645 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
2646 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
2647 1.1 reinoud }
2648 1.1 reinoud max_l_ad = lb_size - dscr_size - l_ea;
2649 1.1 reinoud
2650 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
2651 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2652 1.1 reinoud
2653 1.1 reinoud old_size = inflen;
2654 1.1 reinoud size_diff = new_size - old_size;
2655 1.1 reinoud
2656 1.1 reinoud DPRINTF(ALLOC, ("\tfrom %"PRIu64" to %"PRIu64"\n", old_size, new_size));
2657 1.1 reinoud
2658 1.1 reinoud evacuated_data = NULL;
2659 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
2660 1.1 reinoud if (l_ad + size_diff <= max_l_ad) {
2661 1.1 reinoud /* only reflect size change directly in the node */
2662 1.1 reinoud inflen += size_diff;
2663 1.1 reinoud objsize += size_diff;
2664 1.1 reinoud l_ad += size_diff;
2665 1.1 reinoud crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
2666 1.1 reinoud if (fe) {
2667 1.1 reinoud fe->inf_len = udf_rw64(inflen);
2668 1.1 reinoud fe->l_ad = udf_rw32(l_ad);
2669 1.22 reinoud fe->tag.desc_crc_len = udf_rw16(crclen);
2670 1.1 reinoud } else {
2671 1.1 reinoud efe->inf_len = udf_rw64(inflen);
2672 1.1 reinoud efe->obj_size = udf_rw64(objsize);
2673 1.1 reinoud efe->l_ad = udf_rw32(l_ad);
2674 1.22 reinoud efe->tag.desc_crc_len = udf_rw16(crclen);
2675 1.1 reinoud }
2676 1.1 reinoud error = 0;
2677 1.1 reinoud
2678 1.1 reinoud /* set new size for uvm */
2679 1.1 reinoud uvm_vnp_setwritesize(vp, new_size);
2680 1.33 reinoud uvm_vnp_setsize(vp, new_size);
2681 1.1 reinoud
2682 1.1 reinoud #if 0
2683 1.1 reinoud /* zero append space in buffer */
2684 1.32 hannken ubc_zerorange(&vp->v_uobj, old_size,
2685 1.32 hannken new_size - old_size, UBC_UNMAP_FLAG(vp));
2686 1.1 reinoud #endif
2687 1.1 reinoud
2688 1.9 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
2689 1.9 reinoud
2690 1.1 reinoud /* unlock */
2691 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2692 1.1 reinoud
2693 1.1 reinoud KASSERT(new_inflen == orig_inflen + size_diff);
2694 1.1 reinoud KASSERT(new_lbrec == orig_lbrec);
2695 1.1 reinoud KASSERT(new_lbrec == 0);
2696 1.1 reinoud return 0;
2697 1.1 reinoud }
2698 1.1 reinoud
2699 1.1 reinoud DPRINTF(ALLOC, ("\tCONVERT from internal\n"));
2700 1.1 reinoud
2701 1.1 reinoud if (old_size > 0) {
2702 1.1 reinoud /* allocate some space and copy in the stuff to keep */
2703 1.1 reinoud evacuated_data = malloc(lb_size, M_UDFTEMP, M_WAITOK);
2704 1.1 reinoud memset(evacuated_data, 0, lb_size);
2705 1.1 reinoud
2706 1.1 reinoud /* node is locked, so safe to exit mutex */
2707 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2708 1.1 reinoud
2709 1.1 reinoud /* read in using the `normal' vn_rdwr() */
2710 1.1 reinoud error = vn_rdwr(UIO_READ, udf_node->vnode,
2711 1.1 reinoud evacuated_data, old_size, 0,
2712 1.1 reinoud UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
2713 1.1 reinoud FSCRED, NULL, NULL);
2714 1.1 reinoud
2715 1.1 reinoud /* enter again */
2716 1.1 reinoud UDF_LOCK_NODE(udf_node, 0);
2717 1.1 reinoud }
2718 1.1 reinoud
2719 1.14 reinoud /* convert to a normal alloc and select type */
2720 1.17 reinoud my_part = udf_rw16(udf_node->loc.loc.part_num);
2721 1.26 reinoud dst_part = udf_get_record_vpart(ump, udf_get_c_type(udf_node));
2722 1.17 reinoud addr_type = UDF_ICB_SHORT_ALLOC;
2723 1.17 reinoud if (dst_part != my_part)
2724 1.17 reinoud addr_type = UDF_ICB_LONG_ALLOC;
2725 1.14 reinoud
2726 1.1 reinoud icbflags &= ~UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2727 1.17 reinoud icbflags |= addr_type;
2728 1.1 reinoud icbtag->flags = udf_rw16(icbflags);
2729 1.1 reinoud
2730 1.1 reinoud /* wipe old descriptor space */
2731 1.1 reinoud udf_wipe_adslots(udf_node);
2732 1.1 reinoud
2733 1.1 reinoud memset(&c_ad, 0, sizeof(struct long_ad));
2734 1.1 reinoud c_ad.len = udf_rw32(old_size | UDF_EXT_FREE);
2735 1.1 reinoud c_ad.loc.part_num = udf_rw16(0); /* not relevant */
2736 1.1 reinoud c_ad.loc.lb_num = udf_rw32(0); /* not relevant */
2737 1.1 reinoud
2738 1.1 reinoud slot = 0;
2739 1.1 reinoud } else {
2740 1.1 reinoud /* goto the last entry (if any) */
2741 1.1 reinoud slot = 0;
2742 1.1 reinoud foffset = 0;
2743 1.1 reinoud memset(&c_ad, 0, sizeof(struct long_ad));
2744 1.1 reinoud for (;;) {
2745 1.1 reinoud udf_get_adslot(udf_node, slot, &c_ad, &eof);
2746 1.1 reinoud if (eof)
2747 1.1 reinoud break;
2748 1.1 reinoud
2749 1.1 reinoud len = udf_rw32(c_ad.len);
2750 1.1 reinoud flags = UDF_EXT_FLAGS(len);
2751 1.1 reinoud len = UDF_EXT_LEN(len);
2752 1.1 reinoud
2753 1.1 reinoud end_foffset = foffset + len;
2754 1.1 reinoud if (flags != UDF_EXT_REDIRECT)
2755 1.1 reinoud foffset = end_foffset;
2756 1.1 reinoud
2757 1.1 reinoud slot++;
2758 1.1 reinoud }
2759 1.1 reinoud /* at end of adslots */
2760 1.1 reinoud
2761 1.1 reinoud /* special case if the old size was zero, then there is no last slot */
2762 1.1 reinoud if (old_size == 0) {
2763 1.1 reinoud c_ad.len = udf_rw32(0 | UDF_EXT_FREE);
2764 1.1 reinoud c_ad.loc.part_num = udf_rw16(0); /* not relevant */
2765 1.1 reinoud c_ad.loc.lb_num = udf_rw32(0); /* not relevant */
2766 1.1 reinoud } else {
2767 1.1 reinoud /* refetch last slot */
2768 1.1 reinoud slot--;
2769 1.1 reinoud udf_get_adslot(udf_node, slot, &c_ad, &eof);
2770 1.1 reinoud }
2771 1.1 reinoud }
2772 1.1 reinoud
2773 1.1 reinoud /*
2774 1.1 reinoud * If the length of the last slot is not a multiple of lb_size, adjust
2775 1.1 reinoud * length so that it is; don't forget to adjust `append_len'! relevant for
2776 1.1 reinoud * extending existing files
2777 1.1 reinoud */
2778 1.1 reinoud len = udf_rw32(c_ad.len);
2779 1.1 reinoud flags = UDF_EXT_FLAGS(len);
2780 1.1 reinoud len = UDF_EXT_LEN(len);
2781 1.1 reinoud
2782 1.1 reinoud lastblock_grow = 0;
2783 1.1 reinoud if (len % lb_size > 0) {
2784 1.1 reinoud lastblock_grow = lb_size - (len % lb_size);
2785 1.1 reinoud lastblock_grow = MIN(size_diff, lastblock_grow);
2786 1.1 reinoud len += lastblock_grow;
2787 1.1 reinoud c_ad.len = udf_rw32(len | flags);
2788 1.1 reinoud
2789 1.1 reinoud /* TODO zero appened space in buffer! */
2790 1.32 hannken /* using ubc_zerorange(&vp->v_uobj, old_size, */
2791 1.32 hannken /* new_size - old_size, UBC_UNMAP_FLAG(vp)); ? */
2792 1.1 reinoud }
2793 1.1 reinoud memset(&s_ad, 0, sizeof(struct long_ad));
2794 1.1 reinoud
2795 1.1 reinoud /* size_diff can be bigger than allowed, so grow in chunks */
2796 1.1 reinoud append_len = size_diff - lastblock_grow;
2797 1.1 reinoud while (append_len > 0) {
2798 1.1 reinoud chunk = MIN(append_len, max_len);
2799 1.1 reinoud s_ad.len = udf_rw32(chunk | UDF_EXT_FREE);
2800 1.1 reinoud s_ad.loc.part_num = udf_rw16(0);
2801 1.1 reinoud s_ad.loc.lb_num = udf_rw32(0);
2802 1.1 reinoud
2803 1.31 reinoud if (udf_ads_merge(max_len, lb_size, &c_ad, &s_ad)) {
2804 1.1 reinoud /* not mergable (anymore) */
2805 1.11 reinoud error = udf_append_adslot(udf_node, &slot, &c_ad);
2806 1.1 reinoud if (error)
2807 1.1 reinoud goto errorout;
2808 1.1 reinoud slot++;
2809 1.1 reinoud c_ad = s_ad;
2810 1.1 reinoud memset(&s_ad, 0, sizeof(struct long_ad));
2811 1.1 reinoud }
2812 1.1 reinoud append_len -= chunk;
2813 1.1 reinoud }
2814 1.1 reinoud
2815 1.1 reinoud /* if there is a rest piece in the accumulator, append it */
2816 1.8 reinoud if (UDF_EXT_LEN(udf_rw32(c_ad.len)) > 0) {
2817 1.11 reinoud error = udf_append_adslot(udf_node, &slot, &c_ad);
2818 1.1 reinoud if (error)
2819 1.1 reinoud goto errorout;
2820 1.1 reinoud slot++;
2821 1.1 reinoud }
2822 1.1 reinoud
2823 1.1 reinoud /* if there is a rest piece that didn't fit, append it */
2824 1.8 reinoud if (UDF_EXT_LEN(udf_rw32(s_ad.len)) > 0) {
2825 1.11 reinoud error = udf_append_adslot(udf_node, &slot, &s_ad);
2826 1.1 reinoud if (error)
2827 1.1 reinoud goto errorout;
2828 1.1 reinoud slot++;
2829 1.1 reinoud }
2830 1.1 reinoud
2831 1.1 reinoud inflen += size_diff;
2832 1.1 reinoud objsize += size_diff;
2833 1.1 reinoud if (fe) {
2834 1.1 reinoud fe->inf_len = udf_rw64(inflen);
2835 1.1 reinoud } else {
2836 1.1 reinoud efe->inf_len = udf_rw64(inflen);
2837 1.1 reinoud efe->obj_size = udf_rw64(objsize);
2838 1.1 reinoud }
2839 1.1 reinoud error = 0;
2840 1.1 reinoud
2841 1.1 reinoud if (evacuated_data) {
2842 1.1 reinoud /* set new write size for uvm */
2843 1.1 reinoud uvm_vnp_setwritesize(vp, old_size);
2844 1.1 reinoud
2845 1.1 reinoud /* write out evacuated data */
2846 1.1 reinoud error = vn_rdwr(UIO_WRITE, udf_node->vnode,
2847 1.1 reinoud evacuated_data, old_size, 0,
2848 1.1 reinoud UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
2849 1.1 reinoud FSCRED, NULL, NULL);
2850 1.1 reinoud uvm_vnp_setsize(vp, old_size);
2851 1.1 reinoud }
2852 1.1 reinoud
2853 1.1 reinoud errorout:
2854 1.1 reinoud if (evacuated_data)
2855 1.1 reinoud free(evacuated_data, M_UDFTEMP);
2856 1.9 reinoud
2857 1.11 reinoud udf_count_alloc_exts(udf_node);
2858 1.11 reinoud
2859 1.9 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
2860 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2861 1.1 reinoud
2862 1.1 reinoud KASSERT(new_inflen == orig_inflen + size_diff);
2863 1.1 reinoud KASSERT(new_lbrec == orig_lbrec);
2864 1.1 reinoud
2865 1.1 reinoud return error;
2866 1.1 reinoud }
2867 1.1 reinoud
2868 1.1 reinoud /* --------------------------------------------------------------------- */
2869 1.1 reinoud
2870 1.1 reinoud int
2871 1.1 reinoud udf_shrink_node(struct udf_node *udf_node, uint64_t new_size)
2872 1.1 reinoud {
2873 1.1 reinoud struct vnode *vp = udf_node->vnode;
2874 1.1 reinoud struct udf_mount *ump = udf_node->ump;
2875 1.1 reinoud struct file_entry *fe;
2876 1.1 reinoud struct extfile_entry *efe;
2877 1.1 reinoud struct icb_tag *icbtag;
2878 1.1 reinoud struct long_ad c_ad, s_ad, *node_ad_cpy;
2879 1.1 reinoud uint64_t size_diff, old_size, inflen, objsize;
2880 1.1 reinoud uint64_t foffset, end_foffset;
2881 1.1 reinoud uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
2882 1.31 reinoud uint32_t lb_size, unit_size, dscr_size, crclen;
2883 1.27 reinoud uint32_t slot_offset, slot_offset_lb;
2884 1.1 reinoud uint32_t len, flags, max_len;
2885 1.1 reinoud uint32_t num_lb, lb_num;
2886 1.1 reinoud uint32_t max_l_ad, l_ad, l_ea;
2887 1.1 reinoud uint16_t vpart_num;
2888 1.1 reinoud uint8_t *data_pos;
2889 1.1 reinoud int icbflags, addr_type;
2890 1.1 reinoud int slot, cpy_slot, cpy_slots;
2891 1.1 reinoud int eof, error;
2892 1.1 reinoud
2893 1.1 reinoud DPRINTF(ALLOC, ("udf_shrink_node\n"));
2894 1.9 reinoud
2895 1.9 reinoud UDF_LOCK_NODE(udf_node, 0);
2896 1.1 reinoud udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
2897 1.1 reinoud
2898 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
2899 1.31 reinoud
2900 1.31 reinoud /* max_len in unit's IFF its a metadata node or metadata mirror node */
2901 1.31 reinoud unit_size = lb_size;
2902 1.31 reinoud if ((udf_node == ump->metadata_node) || (udf_node == ump->metadatamirror_node))
2903 1.31 reinoud unit_size = ump->metadata_alloc_unit_size * lb_size;
2904 1.31 reinoud max_len = ((UDF_EXT_MAXLEN / unit_size) * unit_size);
2905 1.1 reinoud
2906 1.1 reinoud /* do the work */
2907 1.1 reinoud fe = udf_node->fe;
2908 1.1 reinoud efe = udf_node->efe;
2909 1.1 reinoud if (fe) {
2910 1.1 reinoud icbtag = &fe->icbtag;
2911 1.1 reinoud inflen = udf_rw64(fe->inf_len);
2912 1.1 reinoud objsize = inflen;
2913 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
2914 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
2915 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
2916 1.1 reinoud data_pos = (uint8_t *) fe + dscr_size + l_ea;
2917 1.1 reinoud } else {
2918 1.1 reinoud icbtag = &efe->icbtag;
2919 1.1 reinoud inflen = udf_rw64(efe->inf_len);
2920 1.1 reinoud objsize = udf_rw64(efe->obj_size);
2921 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
2922 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
2923 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
2924 1.1 reinoud data_pos = (uint8_t *) efe + dscr_size + l_ea;
2925 1.1 reinoud }
2926 1.1 reinoud max_l_ad = lb_size - dscr_size - l_ea;
2927 1.1 reinoud
2928 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
2929 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2930 1.1 reinoud
2931 1.1 reinoud old_size = inflen;
2932 1.1 reinoud size_diff = old_size - new_size;
2933 1.1 reinoud
2934 1.1 reinoud DPRINTF(ALLOC, ("\tfrom %"PRIu64" to %"PRIu64"\n", old_size, new_size));
2935 1.1 reinoud
2936 1.1 reinoud /* shrink the node to its new size */
2937 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
2938 1.1 reinoud /* only reflect size change directly in the node */
2939 1.1 reinoud KASSERT(new_size <= max_l_ad);
2940 1.1 reinoud inflen -= size_diff;
2941 1.1 reinoud objsize -= size_diff;
2942 1.1 reinoud l_ad -= size_diff;
2943 1.1 reinoud crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
2944 1.1 reinoud if (fe) {
2945 1.1 reinoud fe->inf_len = udf_rw64(inflen);
2946 1.1 reinoud fe->l_ad = udf_rw32(l_ad);
2947 1.22 reinoud fe->tag.desc_crc_len = udf_rw16(crclen);
2948 1.1 reinoud } else {
2949 1.1 reinoud efe->inf_len = udf_rw64(inflen);
2950 1.1 reinoud efe->obj_size = udf_rw64(objsize);
2951 1.1 reinoud efe->l_ad = udf_rw32(l_ad);
2952 1.22 reinoud efe->tag.desc_crc_len = udf_rw16(crclen);
2953 1.1 reinoud }
2954 1.1 reinoud error = 0;
2955 1.7 reinoud
2956 1.7 reinoud /* clear the space in the descriptor */
2957 1.37 reinoud KASSERT(old_size >= new_size);
2958 1.7 reinoud memset(data_pos + new_size, 0, old_size - new_size);
2959 1.7 reinoud
2960 1.1 reinoud /* TODO zero appened space in buffer! */
2961 1.32 hannken /* using ubc_zerorange(&vp->v_uobj, old_size, */
2962 1.32 hannken /* old_size - new_size, UBC_UNMAP_FLAG(vp)); ? */
2963 1.1 reinoud
2964 1.1 reinoud /* set new size for uvm */
2965 1.1 reinoud uvm_vnp_setsize(vp, new_size);
2966 1.9 reinoud
2967 1.9 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
2968 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2969 1.1 reinoud
2970 1.1 reinoud KASSERT(new_inflen == orig_inflen - size_diff);
2971 1.1 reinoud KASSERT(new_lbrec == orig_lbrec);
2972 1.1 reinoud KASSERT(new_lbrec == 0);
2973 1.1 reinoud
2974 1.1 reinoud return 0;
2975 1.1 reinoud }
2976 1.1 reinoud
2977 1.1 reinoud /* setup node cleanup extents copy space */
2978 1.1 reinoud node_ad_cpy = malloc(lb_size * UDF_MAX_ALLOC_EXTENTS,
2979 1.1 reinoud M_UDFMNT, M_WAITOK);
2980 1.1 reinoud memset(node_ad_cpy, 0, lb_size * UDF_MAX_ALLOC_EXTENTS);
2981 1.1 reinoud
2982 1.1 reinoud /*
2983 1.1 reinoud * Shrink the node by releasing the allocations and truncate the last
2984 1.1 reinoud * allocation to the new size. If the new size fits into the
2985 1.1 reinoud * allocation descriptor itself, transform it into an
2986 1.1 reinoud * UDF_ICB_INTERN_ALLOC.
2987 1.1 reinoud */
2988 1.1 reinoud slot = 0;
2989 1.1 reinoud cpy_slot = 0;
2990 1.1 reinoud foffset = 0;
2991 1.1 reinoud
2992 1.1 reinoud /* 1) copy till first overlap piece to the rewrite buffer */
2993 1.1 reinoud for (;;) {
2994 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
2995 1.1 reinoud if (eof) {
2996 1.1 reinoud DPRINTF(WRITE,
2997 1.1 reinoud ("Shrink node failed: "
2998 1.1 reinoud "encountered EOF\n"));
2999 1.1 reinoud error = EINVAL;
3000 1.1 reinoud goto errorout; /* panic? */
3001 1.1 reinoud }
3002 1.1 reinoud len = udf_rw32(s_ad.len);
3003 1.1 reinoud flags = UDF_EXT_FLAGS(len);
3004 1.1 reinoud len = UDF_EXT_LEN(len);
3005 1.1 reinoud
3006 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
3007 1.1 reinoud slot++;
3008 1.1 reinoud continue;
3009 1.1 reinoud }
3010 1.1 reinoud
3011 1.1 reinoud end_foffset = foffset + len;
3012 1.1 reinoud if (end_foffset > new_size)
3013 1.1 reinoud break; /* found */
3014 1.1 reinoud
3015 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
3016 1.1 reinoud
3017 1.1 reinoud DPRINTF(ALLOC, ("\t1: vp %d, lb %d, len %d, flags %d "
3018 1.1 reinoud "-> stack\n",
3019 1.1 reinoud udf_rw16(s_ad.loc.part_num),
3020 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
3021 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
3022 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
3023 1.1 reinoud
3024 1.1 reinoud foffset = end_foffset;
3025 1.1 reinoud slot++;
3026 1.1 reinoud }
3027 1.1 reinoud slot_offset = new_size - foffset;
3028 1.1 reinoud
3029 1.1 reinoud /* 2) trunc overlapping slot at overlap and copy it */
3030 1.1 reinoud if (slot_offset > 0) {
3031 1.1 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
3032 1.1 reinoud vpart_num = udf_rw16(s_ad.loc.part_num);
3033 1.1 reinoud
3034 1.1 reinoud if (flags == UDF_EXT_ALLOCATED) {
3035 1.27 reinoud /* calculate extent in lb, and offset in lb */
3036 1.27 reinoud num_lb = (len + lb_size -1) / lb_size;
3037 1.27 reinoud slot_offset_lb = (slot_offset + lb_size -1) / lb_size;
3038 1.27 reinoud
3039 1.27 reinoud /* adjust our slot */
3040 1.27 reinoud lb_num += slot_offset_lb;
3041 1.27 reinoud num_lb -= slot_offset_lb;
3042 1.1 reinoud
3043 1.1 reinoud udf_free_allocated_space(ump, lb_num, vpart_num, num_lb);
3044 1.1 reinoud }
3045 1.1 reinoud
3046 1.1 reinoud s_ad.len = udf_rw32(slot_offset | flags);
3047 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
3048 1.1 reinoud slot++;
3049 1.1 reinoud
3050 1.1 reinoud DPRINTF(ALLOC, ("\t2: vp %d, lb %d, len %d, flags %d "
3051 1.1 reinoud "-> stack\n",
3052 1.1 reinoud udf_rw16(s_ad.loc.part_num),
3053 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
3054 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
3055 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
3056 1.1 reinoud }
3057 1.1 reinoud
3058 1.1 reinoud /* 3) delete remainder */
3059 1.1 reinoud for (;;) {
3060 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
3061 1.1 reinoud if (eof)
3062 1.1 reinoud break;
3063 1.1 reinoud
3064 1.1 reinoud len = udf_rw32(s_ad.len);
3065 1.1 reinoud flags = UDF_EXT_FLAGS(len);
3066 1.1 reinoud len = UDF_EXT_LEN(len);
3067 1.1 reinoud
3068 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
3069 1.1 reinoud slot++;
3070 1.1 reinoud continue;
3071 1.1 reinoud }
3072 1.1 reinoud
3073 1.1 reinoud DPRINTF(ALLOC, ("\t3: delete remainder "
3074 1.1 reinoud "vp %d lb %d, len %d, flags %d\n",
3075 1.1 reinoud udf_rw16(s_ad.loc.part_num),
3076 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
3077 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
3078 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
3079 1.1 reinoud
3080 1.1 reinoud if (flags == UDF_EXT_ALLOCATED) {
3081 1.1 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
3082 1.1 reinoud vpart_num = udf_rw16(s_ad.loc.part_num);
3083 1.1 reinoud num_lb = (len + lb_size - 1) / lb_size;
3084 1.1 reinoud
3085 1.1 reinoud udf_free_allocated_space(ump, lb_num, vpart_num,
3086 1.1 reinoud num_lb);
3087 1.1 reinoud }
3088 1.1 reinoud
3089 1.1 reinoud slot++;
3090 1.1 reinoud }
3091 1.1 reinoud
3092 1.1 reinoud /* 4) if it will fit into the descriptor then convert */
3093 1.1 reinoud if (new_size < max_l_ad) {
3094 1.1 reinoud /*
3095 1.1 reinoud * resque/evacuate old piece by reading it in, and convert it
3096 1.1 reinoud * to internal alloc.
3097 1.1 reinoud */
3098 1.1 reinoud if (new_size == 0) {
3099 1.1 reinoud /* XXX/TODO only for zero sizing now */
3100 1.1 reinoud udf_wipe_adslots(udf_node);
3101 1.1 reinoud
3102 1.1 reinoud icbflags &= ~UDF_ICB_TAG_FLAGS_ALLOC_MASK;
3103 1.1 reinoud icbflags |= UDF_ICB_INTERN_ALLOC;
3104 1.1 reinoud icbtag->flags = udf_rw16(icbflags);
3105 1.1 reinoud
3106 1.1 reinoud inflen -= size_diff; KASSERT(inflen == 0);
3107 1.1 reinoud objsize -= size_diff;
3108 1.1 reinoud l_ad = new_size;
3109 1.1 reinoud crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
3110 1.1 reinoud if (fe) {
3111 1.1 reinoud fe->inf_len = udf_rw64(inflen);
3112 1.1 reinoud fe->l_ad = udf_rw32(l_ad);
3113 1.22 reinoud fe->tag.desc_crc_len = udf_rw16(crclen);
3114 1.1 reinoud } else {
3115 1.1 reinoud efe->inf_len = udf_rw64(inflen);
3116 1.1 reinoud efe->obj_size = udf_rw64(objsize);
3117 1.1 reinoud efe->l_ad = udf_rw32(l_ad);
3118 1.22 reinoud efe->tag.desc_crc_len = udf_rw16(crclen);
3119 1.1 reinoud }
3120 1.1 reinoud /* eventually copy in evacuated piece */
3121 1.1 reinoud /* set new size for uvm */
3122 1.1 reinoud uvm_vnp_setsize(vp, new_size);
3123 1.1 reinoud
3124 1.1 reinoud free(node_ad_cpy, M_UDFMNT);
3125 1.9 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
3126 1.9 reinoud
3127 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
3128 1.1 reinoud
3129 1.1 reinoud KASSERT(new_inflen == orig_inflen - size_diff);
3130 1.1 reinoud KASSERT(new_inflen == 0);
3131 1.1 reinoud KASSERT(new_lbrec == 0);
3132 1.1 reinoud
3133 1.1 reinoud return 0;
3134 1.1 reinoud }
3135 1.1 reinoud
3136 1.1 reinoud printf("UDF_SHRINK_NODE: could convert to internal alloc!\n");
3137 1.1 reinoud }
3138 1.1 reinoud
3139 1.1 reinoud /* 5) reset node descriptors */
3140 1.1 reinoud udf_wipe_adslots(udf_node);
3141 1.1 reinoud
3142 1.1 reinoud /* 6) copy back extents; merge when possible. Recounting on the fly */
3143 1.1 reinoud cpy_slots = cpy_slot;
3144 1.1 reinoud
3145 1.1 reinoud c_ad = node_ad_cpy[0];
3146 1.1 reinoud slot = 0;
3147 1.1 reinoud for (cpy_slot = 1; cpy_slot < cpy_slots; cpy_slot++) {
3148 1.1 reinoud s_ad = node_ad_cpy[cpy_slot];
3149 1.1 reinoud
3150 1.1 reinoud DPRINTF(ALLOC, ("\t6: stack -> got mapping vp %d "
3151 1.1 reinoud "lb %d, len %d, flags %d\n",
3152 1.1 reinoud udf_rw16(s_ad.loc.part_num),
3153 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
3154 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
3155 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
3156 1.1 reinoud
3157 1.1 reinoud /* see if we can merge */
3158 1.31 reinoud if (udf_ads_merge(max_len, lb_size, &c_ad, &s_ad)) {
3159 1.1 reinoud /* not mergable (anymore) */
3160 1.1 reinoud DPRINTF(ALLOC, ("\t6: appending vp %d lb %d, "
3161 1.1 reinoud "len %d, flags %d\n",
3162 1.1 reinoud udf_rw16(c_ad.loc.part_num),
3163 1.1 reinoud udf_rw32(c_ad.loc.lb_num),
3164 1.1 reinoud UDF_EXT_LEN(udf_rw32(c_ad.len)),
3165 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
3166 1.1 reinoud
3167 1.11 reinoud error = udf_append_adslot(udf_node, &slot, &c_ad);
3168 1.1 reinoud if (error)
3169 1.1 reinoud goto errorout; /* panic? */
3170 1.1 reinoud c_ad = s_ad;
3171 1.1 reinoud slot++;
3172 1.1 reinoud }
3173 1.1 reinoud }
3174 1.1 reinoud
3175 1.1 reinoud /* 7) push rest slot (if any) */
3176 1.1 reinoud if (UDF_EXT_LEN(c_ad.len) > 0) {
3177 1.1 reinoud DPRINTF(ALLOC, ("\t7: last append vp %d lb %d, "
3178 1.1 reinoud "len %d, flags %d\n",
3179 1.1 reinoud udf_rw16(c_ad.loc.part_num),
3180 1.1 reinoud udf_rw32(c_ad.loc.lb_num),
3181 1.1 reinoud UDF_EXT_LEN(udf_rw32(c_ad.len)),
3182 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
3183 1.1 reinoud
3184 1.11 reinoud error = udf_append_adslot(udf_node, &slot, &c_ad);
3185 1.1 reinoud if (error)
3186 1.1 reinoud goto errorout; /* panic? */
3187 1.1 reinoud ;
3188 1.1 reinoud }
3189 1.1 reinoud
3190 1.1 reinoud inflen -= size_diff;
3191 1.1 reinoud objsize -= size_diff;
3192 1.1 reinoud if (fe) {
3193 1.1 reinoud fe->inf_len = udf_rw64(inflen);
3194 1.1 reinoud } else {
3195 1.1 reinoud efe->inf_len = udf_rw64(inflen);
3196 1.1 reinoud efe->obj_size = udf_rw64(objsize);
3197 1.1 reinoud }
3198 1.1 reinoud error = 0;
3199 1.1 reinoud
3200 1.1 reinoud /* set new size for uvm */
3201 1.1 reinoud uvm_vnp_setsize(vp, new_size);
3202 1.1 reinoud
3203 1.1 reinoud errorout:
3204 1.1 reinoud free(node_ad_cpy, M_UDFMNT);
3205 1.9 reinoud
3206 1.11 reinoud udf_count_alloc_exts(udf_node);
3207 1.11 reinoud
3208 1.9 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
3209 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
3210 1.1 reinoud
3211 1.1 reinoud KASSERT(new_inflen == orig_inflen - size_diff);
3212 1.1 reinoud
3213 1.1 reinoud return error;
3214 1.1 reinoud }
3215 1.1 reinoud
3216