udf_allocation.c revision 1.5 1 1.5 reinoud /* $NetBSD: udf_allocation.c,v 1.5 2008/06/25 15:28:29 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.5 reinoud __KERNEL_RCSID(0, "$NetBSD: udf_allocation.c,v 1.5 2008/06/25 15:28:29 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_quota.h"
37 1.1 reinoud #include "opt_compat_netbsd.h"
38 1.1 reinoud #endif
39 1.1 reinoud
40 1.1 reinoud /* TODO strip */
41 1.1 reinoud #include <sys/param.h>
42 1.1 reinoud #include <sys/systm.h>
43 1.1 reinoud #include <sys/sysctl.h>
44 1.1 reinoud #include <sys/namei.h>
45 1.1 reinoud #include <sys/proc.h>
46 1.1 reinoud #include <sys/kernel.h>
47 1.1 reinoud #include <sys/vnode.h>
48 1.1 reinoud #include <miscfs/genfs/genfs_node.h>
49 1.1 reinoud #include <sys/mount.h>
50 1.1 reinoud #include <sys/buf.h>
51 1.1 reinoud #include <sys/file.h>
52 1.1 reinoud #include <sys/device.h>
53 1.1 reinoud #include <sys/disklabel.h>
54 1.1 reinoud #include <sys/ioctl.h>
55 1.1 reinoud #include <sys/malloc.h>
56 1.1 reinoud #include <sys/dirent.h>
57 1.1 reinoud #include <sys/stat.h>
58 1.1 reinoud #include <sys/conf.h>
59 1.1 reinoud #include <sys/kauth.h>
60 1.1 reinoud #include <sys/kthread.h>
61 1.1 reinoud #include <dev/clock_subr.h>
62 1.1 reinoud
63 1.1 reinoud #include <fs/udf/ecma167-udf.h>
64 1.1 reinoud #include <fs/udf/udf_mount.h>
65 1.1 reinoud
66 1.1 reinoud #if defined(_KERNEL_OPT)
67 1.1 reinoud #include "opt_udf.h"
68 1.1 reinoud #endif
69 1.1 reinoud
70 1.1 reinoud #include "udf.h"
71 1.1 reinoud #include "udf_subr.h"
72 1.1 reinoud #include "udf_bswap.h"
73 1.1 reinoud
74 1.1 reinoud
75 1.1 reinoud #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
76 1.1 reinoud
77 1.1 reinoud static void udf_record_allocation_in_node(struct udf_mount *ump,
78 1.1 reinoud struct buf *buf, uint16_t vpart_num, uint64_t *mapping,
79 1.1 reinoud struct long_ad *node_ad_cpy);
80 1.1 reinoud
81 1.1 reinoud /*
82 1.1 reinoud * IDEA/BUSY: Each udf_node gets its own extentwalker state for all operations;
83 1.1 reinoud * this will hopefully/likely reduce O(nlog(n)) to O(1) for most functionality
84 1.1 reinoud * since actions are most likely sequencial and thus seeking doesn't need
85 1.1 reinoud * searching for the same or adjacent position again.
86 1.1 reinoud */
87 1.1 reinoud
88 1.1 reinoud /* --------------------------------------------------------------------- */
89 1.1 reinoud //#ifdef DEBUG
90 1.1 reinoud #if 1
91 1.1 reinoud #if 1
92 1.1 reinoud static void
93 1.1 reinoud udf_node_dump(struct udf_node *udf_node) {
94 1.1 reinoud struct file_entry *fe;
95 1.1 reinoud struct extfile_entry *efe;
96 1.1 reinoud struct icb_tag *icbtag;
97 1.1 reinoud struct short_ad *short_ad;
98 1.1 reinoud struct long_ad *long_ad;
99 1.1 reinoud uint64_t inflen;
100 1.1 reinoud uint32_t icbflags, addr_type, max_l_ad;
101 1.1 reinoud uint32_t len, lb_num;
102 1.1 reinoud uint8_t *data_pos;
103 1.1 reinoud int part_num;
104 1.1 reinoud int adlen, ad_off, dscr_size, l_ea, l_ad, lb_size, flags;
105 1.1 reinoud
106 1.1 reinoud if ((udf_verbose & UDF_DEBUG_ADWLK) == 0)
107 1.1 reinoud return;
108 1.1 reinoud
109 1.1 reinoud lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
110 1.1 reinoud
111 1.1 reinoud fe = udf_node->fe;
112 1.1 reinoud efe = udf_node->efe;
113 1.1 reinoud if (fe) {
114 1.1 reinoud icbtag = &fe->icbtag;
115 1.1 reinoud inflen = udf_rw64(fe->inf_len);
116 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
117 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
118 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
119 1.1 reinoud data_pos = (uint8_t *) fe + dscr_size + l_ea;
120 1.1 reinoud } else {
121 1.1 reinoud icbtag = &efe->icbtag;
122 1.1 reinoud inflen = udf_rw64(efe->inf_len);
123 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
124 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
125 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
126 1.1 reinoud data_pos = (uint8_t *) efe + dscr_size + l_ea;
127 1.1 reinoud }
128 1.1 reinoud max_l_ad = lb_size - dscr_size - l_ea;
129 1.1 reinoud
130 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
131 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
132 1.1 reinoud
133 1.1 reinoud printf("udf_node_dump:\n");
134 1.1 reinoud printf("\tudf_node %p\n", udf_node);
135 1.1 reinoud
136 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
137 1.1 reinoud printf("\t\tIntern alloc, len = %"PRIu64"\n", inflen);
138 1.1 reinoud return;
139 1.1 reinoud }
140 1.1 reinoud
141 1.1 reinoud printf("\t\tInflen = %"PRIu64"\n", inflen);
142 1.1 reinoud printf("\t\tl_ad = %d\n", l_ad);
143 1.1 reinoud
144 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
145 1.1 reinoud adlen = sizeof(struct short_ad);
146 1.1 reinoud } else {
147 1.1 reinoud adlen = sizeof(struct long_ad);
148 1.1 reinoud }
149 1.1 reinoud
150 1.1 reinoud printf("\t\t");
151 1.1 reinoud for (ad_off = 0; ad_off < max_l_ad-adlen; ad_off += adlen) {
152 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
153 1.1 reinoud short_ad = (struct short_ad *) (data_pos + ad_off);
154 1.1 reinoud len = udf_rw32(short_ad->len);
155 1.1 reinoud lb_num = udf_rw32(short_ad->lb_num);
156 1.1 reinoud part_num = -1;
157 1.1 reinoud flags = UDF_EXT_FLAGS(len);
158 1.1 reinoud len = UDF_EXT_LEN(len);
159 1.1 reinoud } else {
160 1.1 reinoud long_ad = (struct long_ad *) (data_pos + ad_off);
161 1.1 reinoud len = udf_rw32(long_ad->len);
162 1.1 reinoud lb_num = udf_rw32(long_ad->loc.lb_num);
163 1.1 reinoud part_num = udf_rw16(long_ad->loc.part_num);
164 1.1 reinoud flags = UDF_EXT_FLAGS(len);
165 1.1 reinoud len = UDF_EXT_LEN(len);
166 1.1 reinoud }
167 1.1 reinoud printf("[");
168 1.1 reinoud if (part_num >= 0)
169 1.1 reinoud printf("part %d, ", part_num);
170 1.1 reinoud printf("lb_num %d, len %d", lb_num, len);
171 1.1 reinoud if (flags)
172 1.1 reinoud printf(", flags %d", flags);
173 1.1 reinoud printf("] ");
174 1.1 reinoud if (ad_off + adlen == l_ad)
175 1.1 reinoud printf("\n\t\tl_ad END\n\t\t");
176 1.1 reinoud }
177 1.1 reinoud printf("\n");
178 1.1 reinoud }
179 1.1 reinoud #else
180 1.1 reinoud #define udf_node_dump(a)
181 1.1 reinoud #endif
182 1.1 reinoud
183 1.1 reinoud static void
184 1.1 reinoud udf_node_sanity_check(struct udf_node *udf_node,
185 1.1 reinoud uint64_t *cnt_inflen, uint64_t *cnt_logblksrec) {
186 1.1 reinoud struct file_entry *fe;
187 1.1 reinoud struct extfile_entry *efe;
188 1.1 reinoud struct icb_tag *icbtag;
189 1.1 reinoud struct short_ad *short_ad;
190 1.1 reinoud struct long_ad *long_ad;
191 1.1 reinoud uint64_t inflen, logblksrec;
192 1.1 reinoud uint32_t icbflags, addr_type, max_l_ad;
193 1.1 reinoud uint32_t len, lb_num;
194 1.1 reinoud uint8_t *data_pos;
195 1.1 reinoud int part_num;
196 1.1 reinoud int adlen, ad_off, dscr_size, l_ea, l_ad, lb_size, flags, whole_lb;
197 1.1 reinoud
198 1.1 reinoud /* only lock mutex; we're not changing and its a debug checking func */
199 1.1 reinoud mutex_enter(&udf_node->node_mutex);
200 1.1 reinoud
201 1.1 reinoud lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
202 1.1 reinoud
203 1.1 reinoud fe = udf_node->fe;
204 1.1 reinoud efe = udf_node->efe;
205 1.1 reinoud if (fe) {
206 1.1 reinoud icbtag = &fe->icbtag;
207 1.1 reinoud inflen = udf_rw64(fe->inf_len);
208 1.1 reinoud logblksrec = udf_rw64(fe->logblks_rec);
209 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
210 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
211 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
212 1.1 reinoud data_pos = (uint8_t *) fe + dscr_size + l_ea;
213 1.1 reinoud } else {
214 1.1 reinoud icbtag = &efe->icbtag;
215 1.1 reinoud inflen = udf_rw64(efe->inf_len);
216 1.1 reinoud logblksrec = udf_rw64(efe->logblks_rec);
217 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
218 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
219 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
220 1.1 reinoud data_pos = (uint8_t *) efe + dscr_size + l_ea;
221 1.1 reinoud }
222 1.1 reinoud max_l_ad = lb_size - dscr_size - l_ea;
223 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
224 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
225 1.1 reinoud
226 1.1 reinoud /* reset counters */
227 1.1 reinoud *cnt_inflen = 0;
228 1.1 reinoud *cnt_logblksrec = 0;
229 1.1 reinoud
230 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
231 1.1 reinoud KASSERT(l_ad <= max_l_ad);
232 1.1 reinoud KASSERT(l_ad == inflen);
233 1.1 reinoud *cnt_inflen = inflen;
234 1.1 reinoud mutex_exit(&udf_node->node_mutex);
235 1.1 reinoud return;
236 1.1 reinoud }
237 1.1 reinoud
238 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
239 1.1 reinoud adlen = sizeof(struct short_ad);
240 1.1 reinoud } else {
241 1.1 reinoud adlen = sizeof(struct long_ad);
242 1.1 reinoud }
243 1.1 reinoud
244 1.1 reinoud /* start counting */
245 1.1 reinoud whole_lb = 1;
246 1.1 reinoud for (ad_off = 0; ad_off < l_ad; ad_off += adlen) {
247 1.1 reinoud KASSERT(whole_lb == 1);
248 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
249 1.1 reinoud short_ad = (struct short_ad *) (data_pos + ad_off);
250 1.1 reinoud len = udf_rw32(short_ad->len);
251 1.1 reinoud lb_num = udf_rw32(short_ad->lb_num);
252 1.1 reinoud part_num = -1;
253 1.1 reinoud flags = UDF_EXT_FLAGS(len);
254 1.1 reinoud len = UDF_EXT_LEN(len);
255 1.1 reinoud } else {
256 1.1 reinoud long_ad = (struct long_ad *) (data_pos + ad_off);
257 1.1 reinoud len = udf_rw32(long_ad->len);
258 1.1 reinoud lb_num = udf_rw32(long_ad->loc.lb_num);
259 1.1 reinoud part_num = udf_rw16(long_ad->loc.part_num);
260 1.1 reinoud flags = UDF_EXT_FLAGS(len);
261 1.1 reinoud len = UDF_EXT_LEN(len);
262 1.1 reinoud }
263 1.1 reinoud KASSERT(flags != UDF_EXT_REDIRECT); /* not implemented yet */
264 1.1 reinoud *cnt_inflen += len;
265 1.1 reinoud if (flags == UDF_EXT_ALLOCATED) {
266 1.1 reinoud *cnt_logblksrec += (len + lb_size -1) / lb_size;
267 1.1 reinoud }
268 1.1 reinoud whole_lb = ((len % lb_size) == 0);
269 1.1 reinoud }
270 1.1 reinoud /* rest should be zero (ad_off > l_ad < max_l_ad - adlen) */
271 1.1 reinoud
272 1.1 reinoud KASSERT(*cnt_inflen == inflen);
273 1.1 reinoud KASSERT(*cnt_logblksrec == logblksrec);
274 1.1 reinoud
275 1.1 reinoud mutex_exit(&udf_node->node_mutex);
276 1.1 reinoud if (0)
277 1.1 reinoud udf_node_dump(udf_node);
278 1.1 reinoud }
279 1.1 reinoud #else
280 1.1 reinoud #define udf_node_sanity_check(a, b, c)
281 1.1 reinoud #endif
282 1.1 reinoud
283 1.1 reinoud /* --------------------------------------------------------------------- */
284 1.1 reinoud
285 1.1 reinoud int
286 1.1 reinoud udf_translate_vtop(struct udf_mount *ump, struct long_ad *icb_loc,
287 1.1 reinoud uint32_t *lb_numres, uint32_t *extres)
288 1.1 reinoud {
289 1.1 reinoud struct part_desc *pdesc;
290 1.1 reinoud struct spare_map_entry *sme;
291 1.1 reinoud struct long_ad s_icb_loc;
292 1.1 reinoud uint64_t foffset, end_foffset;
293 1.1 reinoud uint32_t lb_size, len;
294 1.1 reinoud uint32_t lb_num, lb_rel, lb_packet;
295 1.1 reinoud uint32_t udf_rw32_lbmap, ext_offset;
296 1.1 reinoud uint16_t vpart;
297 1.1 reinoud int rel, part, error, eof, slot, flags;
298 1.1 reinoud
299 1.1 reinoud assert(ump && icb_loc && lb_numres);
300 1.1 reinoud
301 1.1 reinoud vpart = udf_rw16(icb_loc->loc.part_num);
302 1.1 reinoud lb_num = udf_rw32(icb_loc->loc.lb_num);
303 1.1 reinoud if (vpart > UDF_VTOP_RAWPART)
304 1.1 reinoud return EINVAL;
305 1.1 reinoud
306 1.1 reinoud translate_again:
307 1.1 reinoud part = ump->vtop[vpart];
308 1.1 reinoud pdesc = ump->partitions[part];
309 1.1 reinoud
310 1.1 reinoud switch (ump->vtop_tp[vpart]) {
311 1.1 reinoud case UDF_VTOP_TYPE_RAW :
312 1.1 reinoud /* 1:1 to the end of the device */
313 1.1 reinoud *lb_numres = lb_num;
314 1.1 reinoud *extres = INT_MAX;
315 1.1 reinoud return 0;
316 1.1 reinoud case UDF_VTOP_TYPE_PHYS :
317 1.1 reinoud /* transform into its disc logical block */
318 1.1 reinoud if (lb_num > udf_rw32(pdesc->part_len))
319 1.1 reinoud return EINVAL;
320 1.1 reinoud *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
321 1.1 reinoud
322 1.1 reinoud /* extent from here to the end of the partition */
323 1.1 reinoud *extres = udf_rw32(pdesc->part_len) - lb_num;
324 1.1 reinoud return 0;
325 1.1 reinoud case UDF_VTOP_TYPE_VIRT :
326 1.1 reinoud /* only maps one logical block, lookup in VAT */
327 1.1 reinoud if (lb_num >= ump->vat_entries) /* XXX > or >= ? */
328 1.1 reinoud return EINVAL;
329 1.1 reinoud
330 1.1 reinoud /* lookup in virtual allocation table file */
331 1.1 reinoud mutex_enter(&ump->allocate_mutex);
332 1.1 reinoud error = udf_vat_read(ump->vat_node,
333 1.1 reinoud (uint8_t *) &udf_rw32_lbmap, 4,
334 1.1 reinoud ump->vat_offset + lb_num * 4);
335 1.1 reinoud mutex_exit(&ump->allocate_mutex);
336 1.1 reinoud
337 1.1 reinoud if (error)
338 1.1 reinoud return error;
339 1.1 reinoud
340 1.1 reinoud lb_num = udf_rw32(udf_rw32_lbmap);
341 1.1 reinoud
342 1.1 reinoud /* transform into its disc logical block */
343 1.1 reinoud if (lb_num > udf_rw32(pdesc->part_len))
344 1.1 reinoud return EINVAL;
345 1.1 reinoud *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
346 1.1 reinoud
347 1.1 reinoud /* just one logical block */
348 1.1 reinoud *extres = 1;
349 1.1 reinoud return 0;
350 1.1 reinoud case UDF_VTOP_TYPE_SPARABLE :
351 1.1 reinoud /* check if the packet containing the lb_num is remapped */
352 1.1 reinoud lb_packet = lb_num / ump->sparable_packet_size;
353 1.1 reinoud lb_rel = lb_num % ump->sparable_packet_size;
354 1.1 reinoud
355 1.1 reinoud for (rel = 0; rel < udf_rw16(ump->sparing_table->rt_l); rel++) {
356 1.1 reinoud sme = &ump->sparing_table->entries[rel];
357 1.1 reinoud if (lb_packet == udf_rw32(sme->org)) {
358 1.1 reinoud /* NOTE maps to absolute disc logical block! */
359 1.1 reinoud *lb_numres = udf_rw32(sme->map) + lb_rel;
360 1.1 reinoud *extres = ump->sparable_packet_size - lb_rel;
361 1.1 reinoud return 0;
362 1.1 reinoud }
363 1.1 reinoud }
364 1.1 reinoud
365 1.1 reinoud /* transform into its disc logical block */
366 1.1 reinoud if (lb_num > udf_rw32(pdesc->part_len))
367 1.1 reinoud return EINVAL;
368 1.1 reinoud *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
369 1.1 reinoud
370 1.1 reinoud /* rest of block */
371 1.1 reinoud *extres = ump->sparable_packet_size - lb_rel;
372 1.1 reinoud return 0;
373 1.1 reinoud case UDF_VTOP_TYPE_META :
374 1.1 reinoud /* we have to look into the file's allocation descriptors */
375 1.1 reinoud
376 1.1 reinoud /* use metadatafile allocation mutex */
377 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
378 1.1 reinoud
379 1.1 reinoud UDF_LOCK_NODE(ump->metadata_node, 0);
380 1.1 reinoud
381 1.1 reinoud /* get first overlapping extent */
382 1.1 reinoud foffset = 0;
383 1.1 reinoud slot = 0;
384 1.1 reinoud for (;;) {
385 1.1 reinoud udf_get_adslot(ump->metadata_node,
386 1.1 reinoud slot, &s_icb_loc, &eof);
387 1.5 reinoud DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, "
388 1.5 reinoud "len = %d, lb_num = %d, part = %d\n",
389 1.5 reinoud slot, eof,
390 1.5 reinoud UDF_EXT_FLAGS(udf_rw32(s_icb_loc.len)),
391 1.5 reinoud UDF_EXT_LEN(udf_rw32(s_icb_loc.len)),
392 1.5 reinoud udf_rw32(s_icb_loc.loc.lb_num),
393 1.5 reinoud udf_rw16(s_icb_loc.loc.part_num)));
394 1.1 reinoud if (eof) {
395 1.1 reinoud DPRINTF(TRANSLATE,
396 1.1 reinoud ("Meta partition translation "
397 1.1 reinoud "failed: can't seek location\n"));
398 1.1 reinoud UDF_UNLOCK_NODE(ump->metadata_node, 0);
399 1.1 reinoud return EINVAL;
400 1.1 reinoud }
401 1.1 reinoud len = udf_rw32(s_icb_loc.len);
402 1.1 reinoud flags = UDF_EXT_FLAGS(len);
403 1.1 reinoud len = UDF_EXT_LEN(len);
404 1.1 reinoud
405 1.5 reinoud if (flags == UDF_EXT_REDIRECT) {
406 1.5 reinoud slot++;
407 1.5 reinoud continue;
408 1.5 reinoud }
409 1.5 reinoud
410 1.1 reinoud end_foffset = foffset + len;
411 1.1 reinoud
412 1.1 reinoud if (end_foffset > lb_num * lb_size)
413 1.1 reinoud break; /* found */
414 1.5 reinoud foffset = end_foffset;
415 1.1 reinoud slot++;
416 1.1 reinoud }
417 1.1 reinoud /* found overlapping slot */
418 1.1 reinoud ext_offset = lb_num * lb_size - foffset;
419 1.1 reinoud
420 1.1 reinoud /* process extent offset */
421 1.1 reinoud lb_num = udf_rw32(s_icb_loc.loc.lb_num);
422 1.1 reinoud vpart = udf_rw16(s_icb_loc.loc.part_num);
423 1.1 reinoud lb_num += (ext_offset + lb_size -1) / lb_size;
424 1.1 reinoud len -= ext_offset;
425 1.1 reinoud ext_offset = 0;
426 1.1 reinoud
427 1.1 reinoud flags = UDF_EXT_FLAGS(s_icb_loc.len);
428 1.1 reinoud
429 1.1 reinoud UDF_UNLOCK_NODE(ump->metadata_node, 0);
430 1.1 reinoud if (flags != UDF_EXT_ALLOCATED) {
431 1.1 reinoud DPRINTF(TRANSLATE, ("Metadata partition translation "
432 1.1 reinoud "failed: not allocated\n"));
433 1.1 reinoud return EINVAL;
434 1.1 reinoud }
435 1.1 reinoud
436 1.1 reinoud /*
437 1.1 reinoud * vpart and lb_num are updated, translate again since we
438 1.1 reinoud * might be mapped on sparable media
439 1.1 reinoud */
440 1.1 reinoud goto translate_again;
441 1.1 reinoud default:
442 1.1 reinoud printf("UDF vtop translation scheme %d unimplemented yet\n",
443 1.1 reinoud ump->vtop_tp[vpart]);
444 1.1 reinoud }
445 1.1 reinoud
446 1.1 reinoud return EINVAL;
447 1.1 reinoud }
448 1.1 reinoud
449 1.1 reinoud /* --------------------------------------------------------------------- */
450 1.1 reinoud
451 1.1 reinoud /*
452 1.1 reinoud * Translate an extent (in logical_blocks) into logical block numbers; used
453 1.1 reinoud * for read and write operations. DOESNT't check extents.
454 1.1 reinoud */
455 1.1 reinoud
456 1.1 reinoud int
457 1.1 reinoud udf_translate_file_extent(struct udf_node *udf_node,
458 1.1 reinoud uint32_t from, uint32_t num_lb,
459 1.1 reinoud uint64_t *map)
460 1.1 reinoud {
461 1.1 reinoud struct udf_mount *ump;
462 1.1 reinoud struct icb_tag *icbtag;
463 1.1 reinoud struct long_ad t_ad, s_ad;
464 1.1 reinoud uint64_t transsec;
465 1.1 reinoud uint64_t foffset, end_foffset;
466 1.1 reinoud uint32_t transsec32;
467 1.1 reinoud uint32_t lb_size;
468 1.1 reinoud uint32_t ext_offset;
469 1.1 reinoud uint32_t lb_num, len;
470 1.1 reinoud uint32_t overlap, translen;
471 1.1 reinoud uint16_t vpart_num;
472 1.1 reinoud int eof, error, flags;
473 1.1 reinoud int slot, addr_type, icbflags;
474 1.1 reinoud
475 1.1 reinoud if (!udf_node)
476 1.1 reinoud return ENOENT;
477 1.1 reinoud
478 1.1 reinoud KASSERT(num_lb > 0);
479 1.1 reinoud
480 1.1 reinoud UDF_LOCK_NODE(udf_node, 0);
481 1.1 reinoud
482 1.1 reinoud /* initialise derivative vars */
483 1.1 reinoud ump = udf_node->ump;
484 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
485 1.1 reinoud
486 1.1 reinoud if (udf_node->fe) {
487 1.1 reinoud icbtag = &udf_node->fe->icbtag;
488 1.1 reinoud } else {
489 1.1 reinoud icbtag = &udf_node->efe->icbtag;
490 1.1 reinoud }
491 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
492 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
493 1.1 reinoud
494 1.1 reinoud /* do the work */
495 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
496 1.1 reinoud *map = UDF_TRANS_INTERN;
497 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
498 1.1 reinoud return 0;
499 1.1 reinoud }
500 1.1 reinoud
501 1.1 reinoud /* find first overlapping extent */
502 1.1 reinoud foffset = 0;
503 1.1 reinoud slot = 0;
504 1.1 reinoud for (;;) {
505 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
506 1.4 reinoud DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
507 1.4 reinoud "lb_num = %d, part = %d\n", slot, eof,
508 1.4 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)),
509 1.4 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
510 1.4 reinoud udf_rw32(s_ad.loc.lb_num),
511 1.4 reinoud udf_rw16(s_ad.loc.part_num)));
512 1.1 reinoud if (eof) {
513 1.1 reinoud DPRINTF(TRANSLATE,
514 1.1 reinoud ("Translate file extent "
515 1.1 reinoud "failed: can't seek location\n"));
516 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
517 1.1 reinoud return EINVAL;
518 1.1 reinoud }
519 1.1 reinoud len = udf_rw32(s_ad.len);
520 1.1 reinoud flags = UDF_EXT_FLAGS(len);
521 1.1 reinoud len = UDF_EXT_LEN(len);
522 1.1 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
523 1.1 reinoud
524 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
525 1.1 reinoud slot++;
526 1.1 reinoud continue;
527 1.1 reinoud }
528 1.1 reinoud
529 1.1 reinoud end_foffset = foffset + len;
530 1.1 reinoud
531 1.1 reinoud if (end_foffset > from * lb_size)
532 1.1 reinoud break; /* found */
533 1.1 reinoud foffset = end_foffset;
534 1.1 reinoud slot++;
535 1.1 reinoud }
536 1.1 reinoud /* found overlapping slot */
537 1.1 reinoud ext_offset = from * lb_size - foffset;
538 1.1 reinoud
539 1.1 reinoud for (;;) {
540 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
541 1.4 reinoud DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
542 1.4 reinoud "lb_num = %d, part = %d\n", slot, eof,
543 1.4 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)),
544 1.4 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
545 1.4 reinoud udf_rw32(s_ad.loc.lb_num),
546 1.4 reinoud udf_rw16(s_ad.loc.part_num)));
547 1.1 reinoud if (eof) {
548 1.1 reinoud DPRINTF(TRANSLATE,
549 1.1 reinoud ("Translate file extent "
550 1.1 reinoud "failed: past eof\n"));
551 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
552 1.1 reinoud return EINVAL;
553 1.1 reinoud }
554 1.1 reinoud
555 1.1 reinoud len = udf_rw32(s_ad.len);
556 1.1 reinoud flags = UDF_EXT_FLAGS(len);
557 1.1 reinoud len = UDF_EXT_LEN(len);
558 1.1 reinoud
559 1.1 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
560 1.1 reinoud vpart_num = udf_rw16(s_ad.loc.part_num);
561 1.1 reinoud
562 1.1 reinoud end_foffset = foffset + len;
563 1.1 reinoud
564 1.1 reinoud /* process extent, don't forget to advance on ext_offset! */
565 1.1 reinoud lb_num += (ext_offset + lb_size -1) / lb_size;
566 1.1 reinoud overlap = (len - ext_offset + lb_size -1) / lb_size;
567 1.1 reinoud ext_offset = 0;
568 1.1 reinoud
569 1.1 reinoud /*
570 1.1 reinoud * note that the while(){} is nessisary for the extent that
571 1.1 reinoud * the udf_translate_vtop() returns doens't have to span the
572 1.1 reinoud * whole extent.
573 1.1 reinoud */
574 1.1 reinoud
575 1.1 reinoud overlap = MIN(overlap, num_lb);
576 1.4 reinoud while (overlap && (flags != UDF_EXT_REDIRECT)) {
577 1.1 reinoud switch (flags) {
578 1.1 reinoud case UDF_EXT_FREE :
579 1.1 reinoud case UDF_EXT_ALLOCATED_BUT_NOT_USED :
580 1.1 reinoud transsec = UDF_TRANS_ZERO;
581 1.1 reinoud translen = overlap;
582 1.1 reinoud while (overlap && num_lb && translen) {
583 1.1 reinoud *map++ = transsec;
584 1.1 reinoud lb_num++;
585 1.1 reinoud overlap--; num_lb--; translen--;
586 1.1 reinoud }
587 1.1 reinoud break;
588 1.1 reinoud case UDF_EXT_ALLOCATED :
589 1.1 reinoud t_ad.loc.lb_num = udf_rw32(lb_num);
590 1.1 reinoud t_ad.loc.part_num = udf_rw16(vpart_num);
591 1.1 reinoud error = udf_translate_vtop(ump,
592 1.1 reinoud &t_ad, &transsec32, &translen);
593 1.1 reinoud transsec = transsec32;
594 1.1 reinoud if (error) {
595 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
596 1.1 reinoud return error;
597 1.1 reinoud }
598 1.1 reinoud while (overlap && num_lb && translen) {
599 1.1 reinoud *map++ = transsec;
600 1.1 reinoud lb_num++; transsec++;
601 1.1 reinoud overlap--; num_lb--; translen--;
602 1.1 reinoud }
603 1.1 reinoud break;
604 1.4 reinoud default:
605 1.4 reinoud DPRINTF(TRANSLATE,
606 1.4 reinoud ("Translate file extent "
607 1.4 reinoud "failed: bad flags %x\n", flags));
608 1.4 reinoud UDF_UNLOCK_NODE(udf_node, 0);
609 1.4 reinoud return EINVAL;
610 1.1 reinoud }
611 1.1 reinoud }
612 1.1 reinoud if (num_lb == 0)
613 1.1 reinoud break;
614 1.1 reinoud
615 1.1 reinoud if (flags != UDF_EXT_REDIRECT)
616 1.1 reinoud foffset = end_foffset;
617 1.1 reinoud slot++;
618 1.1 reinoud }
619 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
620 1.1 reinoud
621 1.1 reinoud return 0;
622 1.1 reinoud }
623 1.1 reinoud
624 1.1 reinoud /* --------------------------------------------------------------------- */
625 1.1 reinoud
626 1.1 reinoud static int
627 1.1 reinoud udf_search_free_vatloc(struct udf_mount *ump, uint32_t *lbnumres)
628 1.1 reinoud {
629 1.1 reinoud uint32_t lb_size, lb_num, lb_map, udf_rw32_lbmap;
630 1.1 reinoud uint8_t *blob;
631 1.1 reinoud int entry, chunk, found, error;
632 1.1 reinoud
633 1.1 reinoud KASSERT(ump);
634 1.1 reinoud KASSERT(ump->logical_vol);
635 1.1 reinoud
636 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
637 1.1 reinoud blob = malloc(lb_size, M_UDFTEMP, M_WAITOK);
638 1.1 reinoud
639 1.1 reinoud /* TODO static allocation of search chunk */
640 1.1 reinoud
641 1.1 reinoud lb_num = MIN(ump->vat_entries, ump->vat_last_free_lb);
642 1.1 reinoud found = 0;
643 1.1 reinoud error = 0;
644 1.1 reinoud entry = 0;
645 1.1 reinoud do {
646 1.1 reinoud chunk = MIN(lb_size, (ump->vat_entries - lb_num) * 4);
647 1.1 reinoud if (chunk <= 0)
648 1.1 reinoud break;
649 1.1 reinoud /* load in chunk */
650 1.1 reinoud error = udf_vat_read(ump->vat_node, blob, chunk,
651 1.1 reinoud ump->vat_offset + lb_num * 4);
652 1.1 reinoud
653 1.1 reinoud if (error)
654 1.1 reinoud break;
655 1.1 reinoud
656 1.1 reinoud /* search this chunk */
657 1.1 reinoud for (entry=0; entry < chunk /4; entry++, lb_num++) {
658 1.1 reinoud udf_rw32_lbmap = *((uint32_t *) (blob + entry * 4));
659 1.1 reinoud lb_map = udf_rw32(udf_rw32_lbmap);
660 1.1 reinoud if (lb_map == 0xffffffff) {
661 1.1 reinoud found = 1;
662 1.1 reinoud break;
663 1.1 reinoud }
664 1.1 reinoud }
665 1.1 reinoud } while (!found);
666 1.1 reinoud if (error) {
667 1.1 reinoud printf("udf_search_free_vatloc: error reading in vat chunk "
668 1.1 reinoud "(lb %d, size %d)\n", lb_num, chunk);
669 1.1 reinoud }
670 1.1 reinoud
671 1.1 reinoud if (!found) {
672 1.1 reinoud /* extend VAT */
673 1.1 reinoud DPRINTF(WRITE, ("udf_search_free_vatloc: extending\n"));
674 1.1 reinoud lb_num = ump->vat_entries;
675 1.1 reinoud ump->vat_entries++;
676 1.1 reinoud }
677 1.1 reinoud
678 1.1 reinoud /* mark entry with initialiser just in case */
679 1.1 reinoud lb_map = udf_rw32(0xfffffffe);
680 1.1 reinoud udf_vat_write(ump->vat_node, (uint8_t *) &lb_map, 4,
681 1.1 reinoud ump->vat_offset + lb_num *4);
682 1.1 reinoud ump->vat_last_free_lb = lb_num;
683 1.1 reinoud
684 1.1 reinoud free(blob, M_UDFTEMP);
685 1.1 reinoud *lbnumres = lb_num;
686 1.1 reinoud return 0;
687 1.1 reinoud }
688 1.1 reinoud
689 1.1 reinoud
690 1.1 reinoud static void
691 1.1 reinoud udf_bitmap_allocate(struct udf_bitmap *bitmap, int ismetadata,
692 1.1 reinoud uint32_t ptov, uint32_t *num_lb, uint64_t *pmappos, uint64_t *lmappos)
693 1.1 reinoud {
694 1.1 reinoud uint32_t offset, lb_num, bit;
695 1.1 reinoud int32_t diff;
696 1.1 reinoud uint8_t *bpos;
697 1.1 reinoud int pass;
698 1.1 reinoud
699 1.1 reinoud if (!ismetadata) {
700 1.1 reinoud /* heuristic to keep the two pointers not too close */
701 1.1 reinoud diff = bitmap->data_pos - bitmap->metadata_pos;
702 1.1 reinoud if ((diff >= 0) && (diff < 1024))
703 1.1 reinoud bitmap->data_pos = bitmap->metadata_pos + 1024;
704 1.1 reinoud }
705 1.1 reinoud offset = ismetadata ? bitmap->metadata_pos : bitmap->data_pos;
706 1.1 reinoud offset &= ~7;
707 1.1 reinoud for (pass = 0; pass < 2; pass++) {
708 1.1 reinoud if (offset >= bitmap->max_offset)
709 1.1 reinoud offset = 0;
710 1.1 reinoud
711 1.1 reinoud while (offset < bitmap->max_offset) {
712 1.1 reinoud if (*num_lb == 0)
713 1.1 reinoud break;
714 1.1 reinoud
715 1.1 reinoud /* use first bit not set */
716 1.1 reinoud bpos = bitmap->bits + offset/8;
717 1.1 reinoud bit = ffs(*bpos);
718 1.1 reinoud if (bit == 0) {
719 1.1 reinoud offset += 8;
720 1.1 reinoud continue;
721 1.1 reinoud }
722 1.1 reinoud *bpos &= ~(1 << (bit-1));
723 1.1 reinoud lb_num = offset + bit-1;
724 1.1 reinoud *lmappos++ = lb_num;
725 1.1 reinoud *pmappos++ = lb_num + ptov;
726 1.1 reinoud *num_lb = *num_lb - 1;
727 1.1 reinoud // offset = (offset & ~7);
728 1.1 reinoud }
729 1.1 reinoud }
730 1.1 reinoud
731 1.1 reinoud if (ismetadata) {
732 1.1 reinoud bitmap->metadata_pos = offset;
733 1.1 reinoud } else {
734 1.1 reinoud bitmap->data_pos = offset;
735 1.1 reinoud }
736 1.1 reinoud }
737 1.1 reinoud
738 1.1 reinoud
739 1.1 reinoud static void
740 1.1 reinoud udf_bitmap_free(struct udf_bitmap *bitmap, uint32_t lb_num, uint32_t num_lb)
741 1.1 reinoud {
742 1.1 reinoud uint32_t offset;
743 1.1 reinoud uint32_t bit, bitval;
744 1.1 reinoud uint8_t *bpos;
745 1.1 reinoud
746 1.1 reinoud offset = lb_num;
747 1.1 reinoud
748 1.1 reinoud /* starter bits */
749 1.1 reinoud bpos = bitmap->bits + offset/8;
750 1.1 reinoud bit = offset % 8;
751 1.1 reinoud while ((bit != 0) && (num_lb > 0)) {
752 1.1 reinoud bitval = (1 << bit);
753 1.1 reinoud KASSERT((*bpos & bitval) == 0);
754 1.1 reinoud *bpos |= bitval;
755 1.1 reinoud offset++; num_lb--;
756 1.1 reinoud bit = (bit + 1) % 8;
757 1.1 reinoud }
758 1.1 reinoud if (num_lb == 0)
759 1.1 reinoud return;
760 1.1 reinoud
761 1.1 reinoud /* whole bytes */
762 1.1 reinoud KASSERT(bit == 0);
763 1.1 reinoud bpos = bitmap->bits + offset / 8;
764 1.1 reinoud while (num_lb >= 8) {
765 1.1 reinoud KASSERT((*bpos == 0));
766 1.1 reinoud *bpos = 255;
767 1.1 reinoud offset += 8; num_lb -= 8;
768 1.1 reinoud bpos++;
769 1.1 reinoud }
770 1.1 reinoud
771 1.1 reinoud /* stop bits */
772 1.1 reinoud KASSERT(num_lb < 8);
773 1.1 reinoud bit = 0;
774 1.1 reinoud while (num_lb > 0) {
775 1.1 reinoud bitval = (1 << bit);
776 1.1 reinoud KASSERT((*bpos & bitval) == 0);
777 1.1 reinoud *bpos |= bitval;
778 1.1 reinoud offset++; num_lb--;
779 1.1 reinoud bit = (bit + 1) % 8;
780 1.1 reinoud }
781 1.1 reinoud }
782 1.1 reinoud
783 1.1 reinoud
784 1.1 reinoud /* allocate a contiguous sequence of sectornumbers */
785 1.1 reinoud static int
786 1.1 reinoud udf_allocate_space(struct udf_mount *ump, int ismetadata, int alloc_type,
787 1.1 reinoud int num_lb, uint16_t *alloc_partp,
788 1.1 reinoud uint64_t *lmapping, uint64_t *pmapping)
789 1.1 reinoud {
790 1.1 reinoud struct mmc_trackinfo *alloc_track, *other_track;
791 1.1 reinoud struct udf_bitmap *bitmap;
792 1.1 reinoud struct part_desc *pdesc;
793 1.1 reinoud struct logvol_int_desc *lvid;
794 1.1 reinoud uint64_t *lmappos, *pmappos;
795 1.1 reinoud uint32_t ptov, lb_num, *freepos, free_lbs;
796 1.1 reinoud int lb_size, alloc_num_lb;
797 1.1 reinoud int alloc_part;
798 1.1 reinoud int error;
799 1.1 reinoud
800 1.1 reinoud mutex_enter(&ump->allocate_mutex);
801 1.1 reinoud
802 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
803 1.1 reinoud KASSERT(lb_size == ump->discinfo.sector_size);
804 1.1 reinoud
805 1.1 reinoud if (ismetadata) {
806 1.1 reinoud alloc_part = ump->metadata_part;
807 1.1 reinoud alloc_track = &ump->metadata_track;
808 1.1 reinoud other_track = &ump->data_track;
809 1.1 reinoud } else {
810 1.1 reinoud alloc_part = ump->data_part;
811 1.1 reinoud alloc_track = &ump->data_track;
812 1.1 reinoud other_track = &ump->metadata_track;
813 1.1 reinoud }
814 1.1 reinoud
815 1.1 reinoud *alloc_partp = alloc_part;
816 1.1 reinoud
817 1.1 reinoud error = 0;
818 1.1 reinoud /* XXX check disc space */
819 1.1 reinoud
820 1.1 reinoud pdesc = ump->partitions[ump->vtop[alloc_part]];
821 1.1 reinoud lmappos = lmapping;
822 1.1 reinoud pmappos = pmapping;
823 1.1 reinoud
824 1.1 reinoud switch (alloc_type) {
825 1.1 reinoud case UDF_ALLOC_VAT :
826 1.1 reinoud /* search empty slot in VAT file */
827 1.1 reinoud KASSERT(num_lb == 1);
828 1.1 reinoud error = udf_search_free_vatloc(ump, &lb_num);
829 1.1 reinoud if (!error) {
830 1.1 reinoud *lmappos = lb_num;
831 1.1 reinoud *pmappos = 0; /* will get late-allocated */
832 1.1 reinoud }
833 1.1 reinoud break;
834 1.1 reinoud case UDF_ALLOC_SEQUENTIAL :
835 1.1 reinoud /* sequential allocation on recordable media */
836 1.1 reinoud /* calculate offset from physical base partition */
837 1.1 reinoud ptov = udf_rw32(pdesc->start_loc);
838 1.1 reinoud
839 1.1 reinoud for (lb_num = 0; lb_num < num_lb; lb_num++) {
840 1.1 reinoud *pmappos++ = alloc_track->next_writable;
841 1.1 reinoud *lmappos++ = alloc_track->next_writable - ptov;
842 1.1 reinoud alloc_track->next_writable++;
843 1.1 reinoud alloc_track->free_blocks--;
844 1.1 reinoud }
845 1.1 reinoud if (alloc_track->tracknr == other_track->tracknr)
846 1.1 reinoud memcpy(other_track, alloc_track,
847 1.1 reinoud sizeof(struct mmc_trackinfo));
848 1.1 reinoud break;
849 1.1 reinoud case UDF_ALLOC_SPACEMAP :
850 1.1 reinoud ptov = udf_rw32(pdesc->start_loc);
851 1.1 reinoud
852 1.1 reinoud /* allocate on unallocated bits page */
853 1.1 reinoud alloc_num_lb = num_lb;
854 1.1 reinoud bitmap = &ump->part_unalloc_bits[alloc_part];
855 1.1 reinoud udf_bitmap_allocate(bitmap, ismetadata, ptov, &alloc_num_lb,
856 1.1 reinoud pmappos, lmappos);
857 1.1 reinoud ump->lvclose |= UDF_WRITE_PART_BITMAPS;
858 1.1 reinoud if (alloc_num_lb) {
859 1.1 reinoud /* TODO convert freed to unalloc and try again */
860 1.1 reinoud /* free allocated piece for now */
861 1.1 reinoud lmappos = lmapping;
862 1.1 reinoud for (lb_num=0; lb_num < num_lb-alloc_num_lb; lb_num++) {
863 1.1 reinoud udf_bitmap_free(bitmap, *lmappos++, 1);
864 1.1 reinoud }
865 1.1 reinoud error = ENOSPC;
866 1.1 reinoud }
867 1.1 reinoud if (!error) {
868 1.1 reinoud /* adjust freecount */
869 1.1 reinoud lvid = ump->logvol_integrity;
870 1.1 reinoud freepos = &lvid->tables[0] + alloc_part;
871 1.1 reinoud free_lbs = udf_rw32(*freepos);
872 1.1 reinoud *freepos = udf_rw32(free_lbs - num_lb);
873 1.1 reinoud }
874 1.1 reinoud break;
875 1.1 reinoud case UDF_ALLOC_METABITMAP :
876 1.1 reinoud case UDF_ALLOC_METASEQUENTIAL :
877 1.1 reinoud case UDF_ALLOC_RELAXEDSEQUENTIAL :
878 1.1 reinoud printf("ALERT: udf_allocate_space : allocation %d "
879 1.1 reinoud "not implemented yet!\n", alloc_type);
880 1.1 reinoud /* TODO implement, doesn't have to be contiguous */
881 1.1 reinoud error = ENOSPC;
882 1.1 reinoud break;
883 1.1 reinoud }
884 1.1 reinoud
885 1.1 reinoud #ifdef DEBUG
886 1.1 reinoud if (udf_verbose & UDF_DEBUG_ALLOC) {
887 1.1 reinoud lmappos = lmapping;
888 1.1 reinoud pmappos = pmapping;
889 1.1 reinoud printf("udf_allocate_space, mapping l->p:\n");
890 1.1 reinoud for (lb_num = 0; lb_num < num_lb; lb_num++) {
891 1.1 reinoud printf("\t%"PRIu64" -> %"PRIu64"\n",
892 1.1 reinoud *lmappos++, *pmappos++);
893 1.1 reinoud }
894 1.1 reinoud }
895 1.1 reinoud #endif
896 1.1 reinoud mutex_exit(&ump->allocate_mutex);
897 1.1 reinoud
898 1.1 reinoud return error;
899 1.1 reinoud }
900 1.1 reinoud
901 1.1 reinoud /* --------------------------------------------------------------------- */
902 1.1 reinoud
903 1.1 reinoud void
904 1.1 reinoud udf_free_allocated_space(struct udf_mount *ump, uint32_t lb_num,
905 1.1 reinoud uint16_t vpart_num, uint32_t num_lb)
906 1.1 reinoud {
907 1.1 reinoud struct udf_bitmap *bitmap;
908 1.1 reinoud struct part_desc *pdesc;
909 1.1 reinoud struct logvol_int_desc *lvid;
910 1.1 reinoud uint32_t ptov, lb_map, udf_rw32_lbmap;
911 1.1 reinoud uint32_t *freepos, free_lbs;
912 1.1 reinoud int phys_part;
913 1.1 reinoud int error;
914 1.1 reinoud
915 1.1 reinoud DPRINTF(ALLOC, ("udf_free_allocated_space: freeing virt lbnum %d "
916 1.1 reinoud "part %d + %d sect\n", lb_num, vpart_num, num_lb));
917 1.1 reinoud
918 1.1 reinoud mutex_enter(&ump->allocate_mutex);
919 1.1 reinoud
920 1.1 reinoud /* get partition backing up this vpart_num */
921 1.1 reinoud pdesc = ump->partitions[ump->vtop[vpart_num]];
922 1.1 reinoud
923 1.1 reinoud switch (ump->vtop_tp[vpart_num]) {
924 1.1 reinoud case UDF_VTOP_TYPE_PHYS :
925 1.1 reinoud case UDF_VTOP_TYPE_SPARABLE :
926 1.1 reinoud /* free space to freed or unallocated space bitmap */
927 1.1 reinoud ptov = udf_rw32(pdesc->start_loc);
928 1.1 reinoud phys_part = ump->vtop[vpart_num];
929 1.1 reinoud
930 1.1 reinoud /* first try freed space bitmap */
931 1.1 reinoud bitmap = &ump->part_freed_bits[phys_part];
932 1.1 reinoud
933 1.1 reinoud /* if not defined, use unallocated bitmap */
934 1.1 reinoud if (bitmap->bits == NULL)
935 1.1 reinoud bitmap = &ump->part_unalloc_bits[phys_part];
936 1.1 reinoud
937 1.1 reinoud /* if no bitmaps are defined, bail out */
938 1.1 reinoud if (bitmap->bits == NULL)
939 1.1 reinoud break;
940 1.1 reinoud
941 1.1 reinoud /* free bits if its defined */
942 1.1 reinoud KASSERT(bitmap->bits);
943 1.1 reinoud ump->lvclose |= UDF_WRITE_PART_BITMAPS;
944 1.1 reinoud udf_bitmap_free(bitmap, lb_num, num_lb);
945 1.1 reinoud
946 1.1 reinoud /* adjust freecount */
947 1.1 reinoud lvid = ump->logvol_integrity;
948 1.1 reinoud freepos = &lvid->tables[0] + vpart_num;
949 1.1 reinoud free_lbs = udf_rw32(*freepos);
950 1.1 reinoud *freepos = udf_rw32(free_lbs + num_lb);
951 1.1 reinoud break;
952 1.1 reinoud case UDF_VTOP_TYPE_VIRT :
953 1.1 reinoud /* free this VAT entry */
954 1.1 reinoud KASSERT(num_lb == 1);
955 1.1 reinoud
956 1.1 reinoud lb_map = 0xffffffff;
957 1.1 reinoud udf_rw32_lbmap = udf_rw32(lb_map);
958 1.1 reinoud error = udf_vat_write(ump->vat_node,
959 1.1 reinoud (uint8_t *) &udf_rw32_lbmap, 4,
960 1.1 reinoud ump->vat_offset + lb_num * 4);
961 1.1 reinoud KASSERT(error == 0);
962 1.1 reinoud ump->vat_last_free_lb = MIN(ump->vat_last_free_lb, lb_num);
963 1.1 reinoud break;
964 1.1 reinoud case UDF_VTOP_TYPE_META :
965 1.1 reinoud /* free space in the metadata bitmap */
966 1.1 reinoud default:
967 1.1 reinoud printf("ALERT: udf_free_allocated_space : allocation %d "
968 1.1 reinoud "not implemented yet!\n", ump->vtop_tp[vpart_num]);
969 1.1 reinoud break;
970 1.1 reinoud }
971 1.1 reinoud
972 1.1 reinoud mutex_exit(&ump->allocate_mutex);
973 1.1 reinoud }
974 1.1 reinoud
975 1.1 reinoud /* --------------------------------------------------------------------- */
976 1.1 reinoud
977 1.1 reinoud int
978 1.1 reinoud udf_pre_allocate_space(struct udf_mount *ump, int udf_c_type, int num_lb,
979 1.1 reinoud uint16_t *alloc_partp, uint64_t *lmapping, uint64_t *pmapping)
980 1.1 reinoud {
981 1.1 reinoud int ismetadata, alloc_type;
982 1.1 reinoud
983 1.1 reinoud ismetadata = (udf_c_type == UDF_C_NODE);
984 1.1 reinoud alloc_type = ismetadata? ump->meta_alloc : ump->data_alloc;
985 1.1 reinoud
986 1.1 reinoud #ifdef DIAGNOSTIC
987 1.1 reinoud if ((alloc_type == UDF_ALLOC_VAT) && (udf_c_type != UDF_C_NODE)) {
988 1.1 reinoud panic("udf_pre_allocate_space: bad c_type on VAT!\n");
989 1.1 reinoud }
990 1.1 reinoud #endif
991 1.1 reinoud
992 1.1 reinoud /* reserve size for VAT allocated data */
993 1.1 reinoud if (alloc_type == UDF_ALLOC_VAT) {
994 1.1 reinoud mutex_enter(&ump->allocate_mutex);
995 1.1 reinoud ump->uncomitted_lb += num_lb;
996 1.1 reinoud mutex_exit(&ump->allocate_mutex);
997 1.1 reinoud }
998 1.1 reinoud
999 1.1 reinoud return udf_allocate_space(ump, ismetadata, alloc_type,
1000 1.1 reinoud num_lb, alloc_partp, lmapping, pmapping);
1001 1.1 reinoud }
1002 1.1 reinoud
1003 1.1 reinoud /* --------------------------------------------------------------------- */
1004 1.1 reinoud
1005 1.1 reinoud /*
1006 1.1 reinoud * Allocate a buf on disc for direct write out. The space doesn't have to be
1007 1.1 reinoud * contiguous as the caller takes care of this.
1008 1.1 reinoud */
1009 1.1 reinoud
1010 1.1 reinoud void
1011 1.1 reinoud udf_late_allocate_buf(struct udf_mount *ump, struct buf *buf,
1012 1.1 reinoud uint64_t *lmapping, uint64_t *pmapping, struct long_ad *node_ad_cpy)
1013 1.1 reinoud {
1014 1.1 reinoud struct udf_node *udf_node = VTOI(buf->b_vp);
1015 1.1 reinoud uint16_t vpart_num;
1016 1.1 reinoud int lb_size, blks, udf_c_type;
1017 1.1 reinoud int ismetadata, alloc_type;
1018 1.1 reinoud int num_lb;
1019 1.1 reinoud int error, s;
1020 1.1 reinoud
1021 1.1 reinoud /*
1022 1.1 reinoud * for each sector in the buf, allocate a sector on disc and record
1023 1.1 reinoud * its position in the provided mapping array.
1024 1.1 reinoud *
1025 1.1 reinoud * If its userdata or FIDs, record its location in its node.
1026 1.1 reinoud */
1027 1.1 reinoud
1028 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
1029 1.1 reinoud num_lb = (buf->b_bcount + lb_size -1) / lb_size;
1030 1.1 reinoud blks = lb_size / DEV_BSIZE;
1031 1.1 reinoud udf_c_type = buf->b_udf_c_type;
1032 1.1 reinoud
1033 1.1 reinoud KASSERT(lb_size == ump->discinfo.sector_size);
1034 1.1 reinoud
1035 1.1 reinoud ismetadata = (udf_c_type == UDF_C_NODE);
1036 1.1 reinoud alloc_type = ismetadata? ump->meta_alloc : ump->data_alloc;
1037 1.1 reinoud
1038 1.1 reinoud #ifdef DIAGNOSTIC
1039 1.1 reinoud if ((alloc_type == UDF_ALLOC_VAT) && (udf_c_type != UDF_C_NODE)) {
1040 1.1 reinoud panic("udf_late_allocate_buf: bad c_type on VAT!\n");
1041 1.1 reinoud }
1042 1.1 reinoud #endif
1043 1.1 reinoud
1044 1.1 reinoud if (udf_c_type == UDF_C_NODE) {
1045 1.1 reinoud /* if not VAT, its allready allocated */
1046 1.1 reinoud if (alloc_type != UDF_ALLOC_VAT)
1047 1.1 reinoud return;
1048 1.1 reinoud
1049 1.1 reinoud /* allocate sequential */
1050 1.1 reinoud alloc_type = UDF_ALLOC_SEQUENTIAL;
1051 1.1 reinoud }
1052 1.1 reinoud
1053 1.1 reinoud error = udf_allocate_space(ump, ismetadata, alloc_type,
1054 1.1 reinoud num_lb, &vpart_num, lmapping, pmapping);
1055 1.1 reinoud if (error) {
1056 1.1 reinoud /* ARGH! we've not done our accounting right! */
1057 1.1 reinoud panic("UDF disc allocation accounting gone wrong");
1058 1.1 reinoud }
1059 1.1 reinoud
1060 1.1 reinoud /* commit our sector count */
1061 1.1 reinoud mutex_enter(&ump->allocate_mutex);
1062 1.1 reinoud if (num_lb > ump->uncomitted_lb) {
1063 1.1 reinoud ump->uncomitted_lb = 0;
1064 1.1 reinoud } else {
1065 1.1 reinoud ump->uncomitted_lb -= num_lb;
1066 1.1 reinoud }
1067 1.1 reinoud mutex_exit(&ump->allocate_mutex);
1068 1.1 reinoud
1069 1.1 reinoud buf->b_blkno = (*pmapping) * blks;
1070 1.1 reinoud
1071 1.1 reinoud /* If its userdata or FIDs, record its allocation in its node. */
1072 1.1 reinoud if ((udf_c_type == UDF_C_USERDATA) || (udf_c_type == UDF_C_FIDS)) {
1073 1.1 reinoud udf_record_allocation_in_node(ump, buf, vpart_num, lmapping,
1074 1.1 reinoud node_ad_cpy);
1075 1.1 reinoud /* decrement our outstanding bufs counter */
1076 1.1 reinoud s = splbio();
1077 1.1 reinoud udf_node->outstanding_bufs--;
1078 1.1 reinoud splx(s);
1079 1.1 reinoud }
1080 1.1 reinoud }
1081 1.1 reinoud
1082 1.1 reinoud /* --------------------------------------------------------------------- */
1083 1.1 reinoud
1084 1.1 reinoud /*
1085 1.1 reinoud * Try to merge a1 with the new piece a2. udf_ads_merge returns error when not
1086 1.1 reinoud * possible (anymore); a2 returns the rest piece.
1087 1.1 reinoud */
1088 1.1 reinoud
1089 1.1 reinoud static int
1090 1.1 reinoud udf_ads_merge(uint32_t lb_size, struct long_ad *a1, struct long_ad *a2)
1091 1.1 reinoud {
1092 1.1 reinoud uint32_t max_len, merge_len;
1093 1.1 reinoud uint32_t a1_len, a2_len;
1094 1.1 reinoud uint32_t a1_flags, a2_flags;
1095 1.1 reinoud uint32_t a1_lbnum, a2_lbnum;
1096 1.1 reinoud uint16_t a1_part, a2_part;
1097 1.1 reinoud
1098 1.1 reinoud max_len = ((UDF_EXT_MAXLEN / lb_size) * lb_size);
1099 1.1 reinoud
1100 1.1 reinoud a1_flags = UDF_EXT_FLAGS(udf_rw32(a1->len));
1101 1.1 reinoud a1_len = UDF_EXT_LEN(udf_rw32(a1->len));
1102 1.1 reinoud a1_lbnum = udf_rw32(a1->loc.lb_num);
1103 1.1 reinoud a1_part = udf_rw16(a1->loc.part_num);
1104 1.1 reinoud
1105 1.1 reinoud a2_flags = UDF_EXT_FLAGS(udf_rw32(a2->len));
1106 1.1 reinoud a2_len = UDF_EXT_LEN(udf_rw32(a2->len));
1107 1.1 reinoud a2_lbnum = udf_rw32(a2->loc.lb_num);
1108 1.1 reinoud a2_part = udf_rw16(a2->loc.part_num);
1109 1.1 reinoud
1110 1.1 reinoud /* defines same space */
1111 1.1 reinoud if (a1_flags != a2_flags)
1112 1.1 reinoud return 1;
1113 1.1 reinoud
1114 1.1 reinoud if (a1_flags != UDF_EXT_FREE) {
1115 1.1 reinoud /* the same partition */
1116 1.1 reinoud if (a1_part != a2_part)
1117 1.1 reinoud return 1;
1118 1.1 reinoud
1119 1.1 reinoud /* a2 is successor of a1 */
1120 1.1 reinoud if (a1_lbnum * lb_size + a1_len != a2_lbnum * lb_size)
1121 1.1 reinoud return 1;
1122 1.1 reinoud }
1123 1.1 reinoud
1124 1.1 reinoud /* merge as most from a2 if possible */
1125 1.1 reinoud merge_len = MIN(a2_len, max_len - a1_len);
1126 1.1 reinoud a1_len += merge_len;
1127 1.1 reinoud a2_len -= merge_len;
1128 1.1 reinoud a2_lbnum += merge_len/lb_size;
1129 1.1 reinoud
1130 1.1 reinoud a1->len = udf_rw32(a1_len | a1_flags);
1131 1.1 reinoud a2->len = udf_rw32(a2_len | a2_flags);
1132 1.1 reinoud a2->loc.lb_num = udf_rw32(a2_lbnum);
1133 1.1 reinoud
1134 1.1 reinoud if (a2_len > 0)
1135 1.1 reinoud return 1;
1136 1.1 reinoud
1137 1.1 reinoud /* there is space over to merge */
1138 1.1 reinoud return 0;
1139 1.1 reinoud }
1140 1.1 reinoud
1141 1.1 reinoud /* --------------------------------------------------------------------- */
1142 1.1 reinoud
1143 1.1 reinoud static void
1144 1.1 reinoud udf_wipe_adslots(struct udf_node *udf_node)
1145 1.1 reinoud {
1146 1.1 reinoud struct file_entry *fe;
1147 1.1 reinoud struct extfile_entry *efe;
1148 1.1 reinoud struct alloc_ext_entry *ext;
1149 1.1 reinoud uint64_t inflen, objsize;
1150 1.1 reinoud uint32_t lb_size, dscr_size, l_ea, l_ad, max_l_ad, crclen;
1151 1.1 reinoud uint8_t *data_pos;
1152 1.1 reinoud int extnr;
1153 1.1 reinoud
1154 1.1 reinoud lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
1155 1.1 reinoud
1156 1.1 reinoud fe = udf_node->fe;
1157 1.1 reinoud efe = udf_node->efe;
1158 1.1 reinoud if (fe) {
1159 1.1 reinoud inflen = udf_rw64(fe->inf_len);
1160 1.1 reinoud objsize = inflen;
1161 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
1162 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
1163 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
1164 1.1 reinoud data_pos = (uint8_t *) fe + dscr_size + l_ea;
1165 1.1 reinoud } else {
1166 1.1 reinoud inflen = udf_rw64(efe->inf_len);
1167 1.1 reinoud objsize = udf_rw64(efe->obj_size);
1168 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
1169 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
1170 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
1171 1.1 reinoud data_pos = (uint8_t *) efe + dscr_size + l_ea;
1172 1.1 reinoud }
1173 1.1 reinoud max_l_ad = lb_size - dscr_size - l_ea;
1174 1.1 reinoud
1175 1.1 reinoud /* wipe fe/efe */
1176 1.1 reinoud memset(data_pos, 0, max_l_ad);
1177 1.1 reinoud crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea;
1178 1.1 reinoud if (fe) {
1179 1.1 reinoud fe->l_ad = udf_rw32(0);
1180 1.1 reinoud fe->logblks_rec = udf_rw64(0);
1181 1.1 reinoud fe->tag.desc_crc_len = udf_rw32(crclen);
1182 1.1 reinoud } else {
1183 1.1 reinoud efe->l_ad = udf_rw32(0);
1184 1.1 reinoud efe->logblks_rec = udf_rw64(0);
1185 1.1 reinoud efe->tag.desc_crc_len = udf_rw32(crclen);
1186 1.1 reinoud }
1187 1.1 reinoud
1188 1.1 reinoud /* wipe all allocation extent entries */
1189 1.1 reinoud for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
1190 1.1 reinoud ext = udf_node->ext[extnr];
1191 1.1 reinoud dscr_size = sizeof(struct alloc_ext_entry) -1;
1192 1.1 reinoud max_l_ad = lb_size - dscr_size;
1193 1.1 reinoud memset(data_pos, 0, max_l_ad);
1194 1.1 reinoud ext->l_ad = udf_rw32(0);
1195 1.1 reinoud
1196 1.1 reinoud crclen = dscr_size - UDF_DESC_TAG_LENGTH;
1197 1.1 reinoud ext->tag.desc_crc_len = udf_rw32(crclen);
1198 1.1 reinoud }
1199 1.1 reinoud }
1200 1.1 reinoud
1201 1.1 reinoud /* --------------------------------------------------------------------- */
1202 1.1 reinoud
1203 1.1 reinoud void
1204 1.1 reinoud udf_get_adslot(struct udf_node *udf_node, int slot, struct long_ad *icb,
1205 1.1 reinoud int *eof) {
1206 1.1 reinoud struct file_entry *fe;
1207 1.1 reinoud struct extfile_entry *efe;
1208 1.1 reinoud struct alloc_ext_entry *ext;
1209 1.1 reinoud struct icb_tag *icbtag;
1210 1.1 reinoud struct short_ad *short_ad;
1211 1.1 reinoud struct long_ad *long_ad;
1212 1.1 reinoud uint32_t offset;
1213 1.1 reinoud uint32_t lb_size, dscr_size, l_ea, l_ad, max_l_ad;
1214 1.1 reinoud uint8_t *data_pos;
1215 1.1 reinoud int icbflags, addr_type, adlen, extnr;
1216 1.1 reinoud
1217 1.1 reinoud /* determine what descriptor we are in */
1218 1.1 reinoud lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
1219 1.1 reinoud
1220 1.1 reinoud fe = udf_node->fe;
1221 1.1 reinoud efe = udf_node->efe;
1222 1.1 reinoud if (fe) {
1223 1.1 reinoud icbtag = &fe->icbtag;
1224 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
1225 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
1226 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
1227 1.1 reinoud data_pos = (uint8_t *) fe + dscr_size + l_ea;
1228 1.1 reinoud } else {
1229 1.1 reinoud icbtag = &efe->icbtag;
1230 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
1231 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
1232 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
1233 1.1 reinoud data_pos = (uint8_t *) efe + dscr_size + l_ea;
1234 1.1 reinoud }
1235 1.1 reinoud max_l_ad = lb_size - dscr_size - l_ea;
1236 1.1 reinoud
1237 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
1238 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
1239 1.1 reinoud
1240 1.1 reinoud /* just in case we're called on an intern, its EOF */
1241 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
1242 1.1 reinoud memset(icb, 0, sizeof(struct long_ad));
1243 1.1 reinoud *eof = 1;
1244 1.1 reinoud return;
1245 1.1 reinoud }
1246 1.1 reinoud
1247 1.1 reinoud adlen = 0;
1248 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
1249 1.1 reinoud adlen = sizeof(struct short_ad);
1250 1.1 reinoud } else if (addr_type == UDF_ICB_LONG_ALLOC) {
1251 1.1 reinoud adlen = sizeof(struct long_ad);
1252 1.1 reinoud }
1253 1.1 reinoud
1254 1.1 reinoud /* if offset too big, we go to the allocation extensions */
1255 1.1 reinoud offset = slot * adlen;
1256 1.3 reinoud extnr = -1;
1257 1.3 reinoud while (offset >= max_l_ad) {
1258 1.3 reinoud extnr++;
1259 1.1 reinoud offset -= max_l_ad;
1260 1.1 reinoud ext = udf_node->ext[extnr];
1261 1.1 reinoud dscr_size = sizeof(struct alloc_ext_entry) -1;
1262 1.1 reinoud l_ad = udf_rw32(ext->l_ad);
1263 1.1 reinoud max_l_ad = lb_size - dscr_size;
1264 1.3 reinoud data_pos = (uint8_t *) ext + dscr_size;
1265 1.1 reinoud if (extnr > udf_node->num_extensions) {
1266 1.1 reinoud l_ad = 0; /* force EOF */
1267 1.1 reinoud break;
1268 1.1 reinoud }
1269 1.1 reinoud }
1270 1.1 reinoud
1271 1.1 reinoud *eof = (offset >= l_ad) || (l_ad == 0);
1272 1.1 reinoud if (*eof) {
1273 1.1 reinoud memset(icb, 0, sizeof(struct long_ad));
1274 1.1 reinoud return;
1275 1.1 reinoud }
1276 1.1 reinoud
1277 1.1 reinoud /* get the element */
1278 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
1279 1.1 reinoud short_ad = (struct short_ad *) (data_pos + offset);
1280 1.1 reinoud icb->len = short_ad->len;
1281 1.5 reinoud icb->loc.part_num = udf_node->loc.loc.part_num;
1282 1.1 reinoud icb->loc.lb_num = short_ad->lb_num;
1283 1.1 reinoud } else if (addr_type == UDF_ICB_LONG_ALLOC) {
1284 1.1 reinoud long_ad = (struct long_ad *) (data_pos + offset);
1285 1.1 reinoud *icb = *long_ad;
1286 1.1 reinoud }
1287 1.1 reinoud }
1288 1.1 reinoud
1289 1.1 reinoud /* --------------------------------------------------------------------- */
1290 1.1 reinoud
1291 1.1 reinoud int
1292 1.1 reinoud udf_append_adslot(struct udf_node *udf_node, int slot, struct long_ad *icb) {
1293 1.1 reinoud union dscrptr *dscr;
1294 1.1 reinoud struct file_entry *fe;
1295 1.1 reinoud struct extfile_entry *efe;
1296 1.1 reinoud struct alloc_ext_entry *ext;
1297 1.1 reinoud struct icb_tag *icbtag;
1298 1.1 reinoud struct short_ad *short_ad;
1299 1.1 reinoud struct long_ad *long_ad, o_icb;
1300 1.1 reinoud uint64_t logblks_rec, *logblks_rec_p;
1301 1.1 reinoud uint32_t offset, rest, len;
1302 1.1 reinoud uint32_t lb_size, dscr_size, l_ea, l_ad, *l_ad_p, max_l_ad, crclen;
1303 1.1 reinoud uint8_t *data_pos;
1304 1.1 reinoud int icbflags, addr_type, adlen, extnr;
1305 1.1 reinoud
1306 1.1 reinoud /* determine what descriptor we are in */
1307 1.1 reinoud lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
1308 1.1 reinoud
1309 1.1 reinoud fe = udf_node->fe;
1310 1.1 reinoud efe = udf_node->efe;
1311 1.1 reinoud if (fe) {
1312 1.1 reinoud icbtag = &fe->icbtag;
1313 1.1 reinoud dscr = (union dscrptr *) fe;
1314 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
1315 1.1 reinoud
1316 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
1317 1.1 reinoud l_ad_p = &fe->l_ad;
1318 1.1 reinoud logblks_rec_p = &fe->logblks_rec;
1319 1.1 reinoud } else {
1320 1.1 reinoud icbtag = &efe->icbtag;
1321 1.1 reinoud dscr = (union dscrptr *) efe;
1322 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
1323 1.1 reinoud
1324 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
1325 1.1 reinoud l_ad_p = &efe->l_ad;
1326 1.1 reinoud logblks_rec_p = &efe->logblks_rec;
1327 1.1 reinoud }
1328 1.1 reinoud data_pos = (uint8_t *) dscr + dscr_size + l_ea;
1329 1.1 reinoud max_l_ad = lb_size - dscr_size - l_ea;
1330 1.1 reinoud
1331 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
1332 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
1333 1.1 reinoud
1334 1.1 reinoud /* just in case we're called on an intern, its EOF */
1335 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
1336 1.1 reinoud panic("udf_append_adslot on UDF_ICB_INTERN_ALLOC\n");
1337 1.1 reinoud }
1338 1.1 reinoud
1339 1.1 reinoud adlen = 0;
1340 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
1341 1.1 reinoud adlen = sizeof(struct short_ad);
1342 1.1 reinoud } else if (addr_type == UDF_ICB_LONG_ALLOC) {
1343 1.1 reinoud adlen = sizeof(struct long_ad);
1344 1.1 reinoud }
1345 1.1 reinoud
1346 1.1 reinoud /* if offset too big, we go to the allocation extensions */
1347 1.1 reinoud offset = slot * adlen;
1348 1.1 reinoud extnr = 0;
1349 1.1 reinoud while (offset > max_l_ad) {
1350 1.1 reinoud offset -= max_l_ad;
1351 1.1 reinoud ext = udf_node->ext[extnr];
1352 1.1 reinoud dscr = (union dscrptr *) ext;
1353 1.1 reinoud dscr_size = sizeof(struct alloc_ext_entry) -1;
1354 1.1 reinoud
1355 1.1 reinoud KASSERT(ext != NULL);
1356 1.1 reinoud l_ad_p = &ext->l_ad;
1357 1.1 reinoud max_l_ad = lb_size - dscr_size;
1358 1.1 reinoud data_pos = (uint8_t *) dscr + dscr_size;
1359 1.1 reinoud
1360 1.1 reinoud extnr++;
1361 1.1 reinoud }
1362 1.1 reinoud /* offset is offset within the current (E)FE/AED */
1363 1.1 reinoud l_ad = udf_rw32(*l_ad_p);
1364 1.1 reinoud crclen = udf_rw32(dscr->tag.desc_crc_len);
1365 1.1 reinoud logblks_rec = udf_rw64(*logblks_rec_p);
1366 1.1 reinoud
1367 1.1 reinoud if (extnr > udf_node->num_extensions)
1368 1.1 reinoud return EFBIG; /* too fragmented */
1369 1.1 reinoud
1370 1.1 reinoud /* overwriting old piece? */
1371 1.1 reinoud if (offset < l_ad) {
1372 1.1 reinoud /* overwrite entry; compensate for the old element */
1373 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
1374 1.1 reinoud short_ad = (struct short_ad *) (data_pos + offset);
1375 1.1 reinoud o_icb.len = short_ad->len;
1376 1.1 reinoud o_icb.loc.part_num = udf_rw16(0); /* ignore */
1377 1.1 reinoud o_icb.loc.lb_num = short_ad->lb_num;
1378 1.1 reinoud } else if (addr_type == UDF_ICB_LONG_ALLOC) {
1379 1.1 reinoud long_ad = (struct long_ad *) (data_pos + offset);
1380 1.1 reinoud o_icb = *long_ad;
1381 1.1 reinoud } else {
1382 1.1 reinoud panic("Invalid address type in udf_append_adslot\n");
1383 1.1 reinoud }
1384 1.1 reinoud
1385 1.1 reinoud len = udf_rw32(o_icb.len);
1386 1.1 reinoud if (UDF_EXT_FLAGS(len) == UDF_EXT_ALLOCATED) {
1387 1.1 reinoud /* adjust counts */
1388 1.1 reinoud len = UDF_EXT_LEN(len);
1389 1.1 reinoud logblks_rec -= (len + lb_size -1) / lb_size;
1390 1.1 reinoud }
1391 1.1 reinoud }
1392 1.1 reinoud
1393 1.1 reinoud /* calculate rest space in this descriptor */
1394 1.1 reinoud rest = max_l_ad - offset;
1395 1.1 reinoud if (rest <= adlen) {
1396 1.1 reinoud /* create redirect and link new allocation extension */
1397 1.1 reinoud printf("udf_append_to_adslot: can't create allocation extention yet\n");
1398 1.1 reinoud return EFBIG;
1399 1.1 reinoud }
1400 1.1 reinoud
1401 1.1 reinoud /* write out the element */
1402 1.1 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
1403 1.1 reinoud short_ad = (struct short_ad *) (data_pos + offset);
1404 1.1 reinoud short_ad->len = icb->len;
1405 1.1 reinoud short_ad->lb_num = icb->loc.lb_num;
1406 1.1 reinoud } else if (addr_type == UDF_ICB_LONG_ALLOC) {
1407 1.1 reinoud long_ad = (struct long_ad *) (data_pos + offset);
1408 1.1 reinoud *long_ad = *icb;
1409 1.1 reinoud }
1410 1.1 reinoud
1411 1.1 reinoud /* adjust logblks recorded count */
1412 1.1 reinoud if (UDF_EXT_FLAGS(icb->len) == UDF_EXT_ALLOCATED)
1413 1.1 reinoud logblks_rec += (UDF_EXT_LEN(icb->len) + lb_size -1) / lb_size;
1414 1.1 reinoud *logblks_rec_p = udf_rw64(logblks_rec);
1415 1.1 reinoud
1416 1.1 reinoud /* adjust l_ad and crclen when needed */
1417 1.1 reinoud if (offset >= l_ad) {
1418 1.1 reinoud l_ad += adlen;
1419 1.1 reinoud crclen += adlen;
1420 1.1 reinoud dscr->tag.desc_crc_len = udf_rw32(crclen);
1421 1.1 reinoud *l_ad_p = udf_rw32(l_ad);
1422 1.1 reinoud }
1423 1.1 reinoud
1424 1.1 reinoud return 0;
1425 1.1 reinoud }
1426 1.1 reinoud
1427 1.1 reinoud /* --------------------------------------------------------------------- */
1428 1.1 reinoud
1429 1.1 reinoud /*
1430 1.1 reinoud * Adjust the node's allocation descriptors to reflect the new mapping; do
1431 1.1 reinoud * take note that we might glue to existing allocation descriptors.
1432 1.1 reinoud *
1433 1.1 reinoud * XXX Note there can only be one allocation being recorded/mount; maybe
1434 1.1 reinoud * explicit allocation in shedule thread?
1435 1.1 reinoud */
1436 1.1 reinoud
1437 1.1 reinoud static void
1438 1.1 reinoud udf_record_allocation_in_node(struct udf_mount *ump, struct buf *buf,
1439 1.1 reinoud uint16_t vpart_num, uint64_t *mapping, struct long_ad *node_ad_cpy)
1440 1.1 reinoud {
1441 1.1 reinoud struct vnode *vp = buf->b_vp;
1442 1.1 reinoud struct udf_node *udf_node = VTOI(vp);
1443 1.1 reinoud struct file_entry *fe;
1444 1.1 reinoud struct extfile_entry *efe;
1445 1.1 reinoud struct icb_tag *icbtag;
1446 1.1 reinoud struct long_ad s_ad, c_ad;
1447 1.1 reinoud uint64_t inflen, from, till;
1448 1.1 reinoud uint64_t foffset, end_foffset, restart_foffset;
1449 1.1 reinoud uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
1450 1.1 reinoud uint32_t num_lb, len, flags, lb_num;
1451 1.1 reinoud uint32_t run_start;
1452 1.1 reinoud uint32_t slot_offset;
1453 1.1 reinoud uint32_t skip_len, skipped;
1454 1.1 reinoud int addr_type, icbflags;
1455 1.1 reinoud int udf_c_type = buf->b_udf_c_type;
1456 1.1 reinoud int lb_size, run_length, eof;
1457 1.1 reinoud int slot, cpy_slot, cpy_slots, restart_slot;
1458 1.1 reinoud int error;
1459 1.1 reinoud
1460 1.1 reinoud DPRINTF(ALLOC, ("udf_record_allocation_in_node\n"));
1461 1.1 reinoud udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
1462 1.1 reinoud
1463 1.1 reinoud /* sanity check ... should be panic ? */
1464 1.1 reinoud if ((udf_c_type != UDF_C_USERDATA) && (udf_c_type != UDF_C_FIDS))
1465 1.1 reinoud return;
1466 1.1 reinoud
1467 1.1 reinoud lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
1468 1.1 reinoud
1469 1.1 reinoud /* do the job */
1470 1.1 reinoud UDF_LOCK_NODE(udf_node, 0); /* XXX can deadlock ? */
1471 1.1 reinoud
1472 1.1 reinoud fe = udf_node->fe;
1473 1.1 reinoud efe = udf_node->efe;
1474 1.1 reinoud if (fe) {
1475 1.1 reinoud icbtag = &fe->icbtag;
1476 1.1 reinoud inflen = udf_rw64(fe->inf_len);
1477 1.1 reinoud } else {
1478 1.1 reinoud icbtag = &efe->icbtag;
1479 1.1 reinoud inflen = udf_rw64(efe->inf_len);
1480 1.1 reinoud }
1481 1.1 reinoud
1482 1.1 reinoud /* do check if `till' is not past file information length */
1483 1.1 reinoud from = buf->b_lblkno * lb_size;
1484 1.1 reinoud till = MIN(inflen, from + buf->b_resid);
1485 1.1 reinoud
1486 1.1 reinoud num_lb = (till - from + lb_size -1) / lb_size;
1487 1.1 reinoud
1488 1.1 reinoud DPRINTF(ALLOC, ("record allocation from = %"PRIu64" + %d\n", from, buf->b_bcount));
1489 1.1 reinoud
1490 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
1491 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
1492 1.1 reinoud
1493 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
1494 1.1 reinoud /* nothing to do */
1495 1.1 reinoud /* XXX clean up rest of node? just in case? */
1496 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
1497 1.1 reinoud return;
1498 1.1 reinoud }
1499 1.1 reinoud
1500 1.1 reinoud slot = 0;
1501 1.1 reinoud cpy_slot = 0;
1502 1.1 reinoud foffset = 0;
1503 1.1 reinoud
1504 1.1 reinoud /* 1) copy till first overlap piece to the rewrite buffer */
1505 1.1 reinoud for (;;) {
1506 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
1507 1.1 reinoud if (eof) {
1508 1.1 reinoud DPRINTF(WRITE,
1509 1.1 reinoud ("Record allocation in node "
1510 1.1 reinoud "failed: encountered EOF\n"));
1511 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
1512 1.1 reinoud buf->b_error = EINVAL;
1513 1.1 reinoud return;
1514 1.1 reinoud }
1515 1.1 reinoud len = udf_rw32(s_ad.len);
1516 1.1 reinoud flags = UDF_EXT_FLAGS(len);
1517 1.1 reinoud len = UDF_EXT_LEN(len);
1518 1.1 reinoud
1519 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
1520 1.1 reinoud slot++;
1521 1.1 reinoud continue;
1522 1.1 reinoud }
1523 1.1 reinoud
1524 1.1 reinoud end_foffset = foffset + len;
1525 1.1 reinoud if (end_foffset > from)
1526 1.1 reinoud break; /* found */
1527 1.1 reinoud
1528 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
1529 1.1 reinoud
1530 1.1 reinoud DPRINTF(ALLOC, ("\t1: vp %d, lb %d, len %d, flags %d "
1531 1.1 reinoud "-> stack\n",
1532 1.1 reinoud udf_rw16(s_ad.loc.part_num),
1533 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
1534 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
1535 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
1536 1.1 reinoud
1537 1.1 reinoud foffset = end_foffset;
1538 1.1 reinoud slot++;
1539 1.1 reinoud }
1540 1.1 reinoud restart_slot = slot;
1541 1.1 reinoud restart_foffset = foffset;
1542 1.1 reinoud
1543 1.1 reinoud /* 2) trunc overlapping slot at overlap and copy it */
1544 1.1 reinoud slot_offset = from - foffset;
1545 1.1 reinoud if (slot_offset > 0) {
1546 1.1 reinoud DPRINTF(ALLOC, ("\tslot_offset = %d, flags = %d (%d)\n",
1547 1.1 reinoud slot_offset, flags >> 30, flags));
1548 1.1 reinoud
1549 1.1 reinoud s_ad.len = udf_rw32(slot_offset | flags);
1550 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
1551 1.1 reinoud
1552 1.1 reinoud DPRINTF(ALLOC, ("\t2: vp %d, lb %d, len %d, flags %d "
1553 1.1 reinoud "-> stack\n",
1554 1.1 reinoud udf_rw16(s_ad.loc.part_num),
1555 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
1556 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
1557 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
1558 1.1 reinoud }
1559 1.1 reinoud foffset += slot_offset;
1560 1.1 reinoud
1561 1.1 reinoud /* 3) insert new mappings */
1562 1.1 reinoud memset(&s_ad, 0, sizeof(struct long_ad));
1563 1.1 reinoud lb_num = 0;
1564 1.1 reinoud for (lb_num = 0; lb_num < num_lb; lb_num++) {
1565 1.1 reinoud run_start = mapping[lb_num];
1566 1.1 reinoud run_length = 1;
1567 1.1 reinoud while (lb_num < num_lb-1) {
1568 1.1 reinoud if (mapping[lb_num+1] != mapping[lb_num]+1)
1569 1.1 reinoud if (mapping[lb_num+1] != mapping[lb_num])
1570 1.1 reinoud break;
1571 1.1 reinoud run_length++;
1572 1.1 reinoud lb_num++;
1573 1.1 reinoud }
1574 1.1 reinoud /* insert slot for this mapping */
1575 1.1 reinoud len = run_length * lb_size;
1576 1.1 reinoud
1577 1.1 reinoud /* bounds checking */
1578 1.1 reinoud if (foffset + len > till)
1579 1.1 reinoud len = till - foffset;
1580 1.1 reinoud KASSERT(foffset + len <= inflen);
1581 1.1 reinoud
1582 1.1 reinoud s_ad.len = udf_rw32(len | UDF_EXT_ALLOCATED);
1583 1.1 reinoud s_ad.loc.part_num = udf_rw16(vpart_num);
1584 1.1 reinoud s_ad.loc.lb_num = udf_rw32(run_start);
1585 1.1 reinoud
1586 1.1 reinoud foffset += len;
1587 1.1 reinoud
1588 1.1 reinoud /* paranoia */
1589 1.1 reinoud if (len == 0) {
1590 1.1 reinoud DPRINTF(WRITE,
1591 1.1 reinoud ("Record allocation in node "
1592 1.1 reinoud "failed: insert failed\n"));
1593 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
1594 1.1 reinoud buf->b_error = EINVAL;
1595 1.1 reinoud return;
1596 1.1 reinoud }
1597 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
1598 1.1 reinoud
1599 1.1 reinoud DPRINTF(ALLOC, ("\t3: insert new mapping vp %d lb %d, len %d, "
1600 1.1 reinoud "flags %d -> stack\n",
1601 1.1 reinoud udf_rw16(s_ad.loc.part_num), udf_rw32(s_ad.loc.lb_num),
1602 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
1603 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
1604 1.1 reinoud }
1605 1.1 reinoud
1606 1.1 reinoud /* 4) pop replaced length */
1607 1.1 reinoud slot = restart_slot;
1608 1.1 reinoud foffset = restart_foffset;
1609 1.1 reinoud
1610 1.1 reinoud skip_len = till - foffset; /* relative to start of slot */
1611 1.1 reinoud slot_offset = from - foffset;
1612 1.1 reinoud for (;;) {
1613 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
1614 1.1 reinoud if (eof)
1615 1.1 reinoud break;
1616 1.1 reinoud
1617 1.1 reinoud len = udf_rw32(s_ad.len);
1618 1.1 reinoud flags = UDF_EXT_FLAGS(len);
1619 1.1 reinoud len = UDF_EXT_LEN(len);
1620 1.1 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
1621 1.1 reinoud
1622 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
1623 1.1 reinoud slot++;
1624 1.1 reinoud continue;
1625 1.1 reinoud }
1626 1.1 reinoud
1627 1.1 reinoud DPRINTF(ALLOC, ("\t4i: got slot %d, skip_len %d, vp %d, "
1628 1.1 reinoud "lb %d, len %d, flags %d\n",
1629 1.1 reinoud slot, skip_len, udf_rw16(s_ad.loc.part_num),
1630 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
1631 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
1632 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
1633 1.1 reinoud
1634 1.1 reinoud skipped = MIN(len, skip_len);
1635 1.1 reinoud if (flags != UDF_EXT_FREE) {
1636 1.1 reinoud if (slot_offset) {
1637 1.1 reinoud /* skip these blocks first */
1638 1.1 reinoud num_lb = (slot_offset + lb_size-1) / lb_size;
1639 1.1 reinoud len -= slot_offset;
1640 1.1 reinoud skip_len -= slot_offset;
1641 1.1 reinoud foffset += slot_offset;
1642 1.1 reinoud lb_num += num_lb;
1643 1.1 reinoud skipped -= slot_offset;
1644 1.1 reinoud slot_offset = 0;
1645 1.1 reinoud }
1646 1.1 reinoud /* free space from current position till `skipped' */
1647 1.1 reinoud num_lb = (skipped + lb_size-1) / lb_size;
1648 1.1 reinoud udf_free_allocated_space(ump, lb_num,
1649 1.1 reinoud udf_rw16(s_ad.loc.part_num), num_lb);
1650 1.1 reinoud lb_num += num_lb;
1651 1.1 reinoud }
1652 1.1 reinoud len -= skipped;
1653 1.1 reinoud skip_len -= skipped;
1654 1.1 reinoud foffset += skipped;
1655 1.1 reinoud
1656 1.1 reinoud if (len) {
1657 1.1 reinoud KASSERT(skipped % lb_size == 0);
1658 1.1 reinoud
1659 1.1 reinoud /* we arrived at our point, push remainder */
1660 1.1 reinoud s_ad.len = udf_rw32(len | flags);
1661 1.1 reinoud s_ad.loc.lb_num = udf_rw32(lb_num);
1662 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
1663 1.1 reinoud foffset += len;
1664 1.1 reinoud slot++;
1665 1.1 reinoud
1666 1.1 reinoud DPRINTF(ALLOC, ("\t4: vp %d, lb %d, len %d, flags %d "
1667 1.1 reinoud "-> stack\n",
1668 1.1 reinoud udf_rw16(s_ad.loc.part_num),
1669 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
1670 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
1671 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
1672 1.1 reinoud break;
1673 1.1 reinoud }
1674 1.1 reinoud slot++;
1675 1.1 reinoud }
1676 1.1 reinoud
1677 1.1 reinoud /* 5) copy remainder */
1678 1.1 reinoud for (;;) {
1679 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
1680 1.1 reinoud if (eof)
1681 1.1 reinoud break;
1682 1.1 reinoud
1683 1.1 reinoud len = udf_rw32(s_ad.len);
1684 1.1 reinoud flags = UDF_EXT_FLAGS(len);
1685 1.1 reinoud len = UDF_EXT_LEN(len);
1686 1.1 reinoud
1687 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
1688 1.1 reinoud slot++;
1689 1.1 reinoud continue;
1690 1.1 reinoud }
1691 1.1 reinoud
1692 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
1693 1.1 reinoud
1694 1.1 reinoud DPRINTF(ALLOC, ("\t5: insert new mapping "
1695 1.1 reinoud "vp %d lb %d, len %d, flags %d "
1696 1.1 reinoud "-> stack\n",
1697 1.1 reinoud udf_rw16(s_ad.loc.part_num),
1698 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
1699 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
1700 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
1701 1.1 reinoud
1702 1.1 reinoud slot++;
1703 1.1 reinoud }
1704 1.1 reinoud
1705 1.1 reinoud /* 6) reset node descriptors */
1706 1.1 reinoud udf_wipe_adslots(udf_node);
1707 1.1 reinoud
1708 1.1 reinoud /* 7) copy back extents; merge when possible. Recounting on the fly */
1709 1.1 reinoud cpy_slots = cpy_slot;
1710 1.1 reinoud
1711 1.1 reinoud c_ad = node_ad_cpy[0];
1712 1.1 reinoud slot = 0;
1713 1.1 reinoud DPRINTF(ALLOC, ("\t7s: stack -> got mapping vp %d "
1714 1.1 reinoud "lb %d, len %d, flags %d\n",
1715 1.1 reinoud udf_rw16(c_ad.loc.part_num),
1716 1.1 reinoud udf_rw32(c_ad.loc.lb_num),
1717 1.1 reinoud UDF_EXT_LEN(udf_rw32(c_ad.len)),
1718 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
1719 1.1 reinoud
1720 1.1 reinoud for (cpy_slot = 1; cpy_slot < cpy_slots; cpy_slot++) {
1721 1.1 reinoud s_ad = node_ad_cpy[cpy_slot];
1722 1.1 reinoud
1723 1.1 reinoud DPRINTF(ALLOC, ("\t7i: stack -> got mapping vp %d "
1724 1.1 reinoud "lb %d, len %d, flags %d\n",
1725 1.1 reinoud udf_rw16(s_ad.loc.part_num),
1726 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
1727 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
1728 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
1729 1.1 reinoud
1730 1.1 reinoud /* see if we can merge */
1731 1.1 reinoud if (udf_ads_merge(lb_size, &c_ad, &s_ad)) {
1732 1.1 reinoud /* not mergable (anymore) */
1733 1.1 reinoud DPRINTF(ALLOC, ("\t7: appending vp %d lb %d, "
1734 1.1 reinoud "len %d, flags %d\n",
1735 1.1 reinoud udf_rw16(c_ad.loc.part_num),
1736 1.1 reinoud udf_rw32(c_ad.loc.lb_num),
1737 1.1 reinoud UDF_EXT_LEN(udf_rw32(c_ad.len)),
1738 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
1739 1.1 reinoud
1740 1.1 reinoud error = udf_append_adslot(udf_node, slot, &c_ad);
1741 1.1 reinoud if (error) {
1742 1.1 reinoud buf->b_error = error;
1743 1.1 reinoud goto out;
1744 1.1 reinoud }
1745 1.1 reinoud c_ad = s_ad;
1746 1.1 reinoud slot++;
1747 1.1 reinoud }
1748 1.1 reinoud }
1749 1.1 reinoud
1750 1.1 reinoud /* 8) push rest slot (if any) */
1751 1.1 reinoud if (UDF_EXT_LEN(c_ad.len) > 0) {
1752 1.1 reinoud DPRINTF(ALLOC, ("\t8: last append vp %d lb %d, "
1753 1.1 reinoud "len %d, flags %d\n",
1754 1.1 reinoud udf_rw16(c_ad.loc.part_num),
1755 1.1 reinoud udf_rw32(c_ad.loc.lb_num),
1756 1.1 reinoud UDF_EXT_LEN(udf_rw32(c_ad.len)),
1757 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
1758 1.1 reinoud
1759 1.1 reinoud error = udf_append_adslot(udf_node, slot, &c_ad);
1760 1.1 reinoud if (error) {
1761 1.1 reinoud buf->b_error = error;
1762 1.1 reinoud goto out;
1763 1.1 reinoud }
1764 1.1 reinoud }
1765 1.1 reinoud
1766 1.1 reinoud out:
1767 1.1 reinoud /* the node's descriptors should now be sane */
1768 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
1769 1.1 reinoud
1770 1.1 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
1771 1.1 reinoud
1772 1.1 reinoud KASSERT(orig_inflen == new_inflen);
1773 1.1 reinoud KASSERT(new_lbrec >= orig_lbrec);
1774 1.1 reinoud
1775 1.1 reinoud return;
1776 1.1 reinoud }
1777 1.1 reinoud
1778 1.1 reinoud /* --------------------------------------------------------------------- */
1779 1.1 reinoud
1780 1.1 reinoud int
1781 1.1 reinoud udf_grow_node(struct udf_node *udf_node, uint64_t new_size)
1782 1.1 reinoud {
1783 1.1 reinoud union dscrptr *dscr;
1784 1.1 reinoud struct vnode *vp = udf_node->vnode;
1785 1.1 reinoud struct udf_mount *ump = udf_node->ump;
1786 1.1 reinoud struct file_entry *fe;
1787 1.1 reinoud struct extfile_entry *efe;
1788 1.1 reinoud struct icb_tag *icbtag;
1789 1.1 reinoud struct long_ad c_ad, s_ad;
1790 1.1 reinoud uint64_t size_diff, old_size, inflen, objsize, chunk, append_len;
1791 1.1 reinoud uint64_t foffset, end_foffset;
1792 1.1 reinoud uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
1793 1.1 reinoud uint32_t lb_size, dscr_size, crclen, lastblock_grow;
1794 1.1 reinoud uint32_t len, flags, max_len;
1795 1.1 reinoud uint32_t max_l_ad, l_ad, l_ea;
1796 1.1 reinoud uint8_t *data_pos, *evacuated_data;
1797 1.1 reinoud int icbflags, addr_type;
1798 1.1 reinoud int slot, cpy_slot;
1799 1.1 reinoud int eof, error;
1800 1.1 reinoud
1801 1.1 reinoud DPRINTF(ALLOC, ("udf_grow_node\n"));
1802 1.1 reinoud udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
1803 1.1 reinoud
1804 1.1 reinoud UDF_LOCK_NODE(udf_node, 0);
1805 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
1806 1.1 reinoud max_len = ((UDF_EXT_MAXLEN / lb_size) * lb_size);
1807 1.1 reinoud
1808 1.1 reinoud fe = udf_node->fe;
1809 1.1 reinoud efe = udf_node->efe;
1810 1.1 reinoud if (fe) {
1811 1.1 reinoud dscr = (union dscrptr *) fe;
1812 1.1 reinoud icbtag = &fe->icbtag;
1813 1.1 reinoud inflen = udf_rw64(fe->inf_len);
1814 1.1 reinoud objsize = inflen;
1815 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
1816 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
1817 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
1818 1.1 reinoud } else {
1819 1.1 reinoud dscr = (union dscrptr *) efe;
1820 1.1 reinoud icbtag = &efe->icbtag;
1821 1.1 reinoud inflen = udf_rw64(efe->inf_len);
1822 1.1 reinoud objsize = udf_rw64(efe->obj_size);
1823 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
1824 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
1825 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
1826 1.1 reinoud }
1827 1.1 reinoud data_pos = (uint8_t *) dscr + dscr_size + l_ea;
1828 1.1 reinoud max_l_ad = lb_size - dscr_size - l_ea;
1829 1.1 reinoud
1830 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
1831 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
1832 1.1 reinoud
1833 1.1 reinoud old_size = inflen;
1834 1.1 reinoud size_diff = new_size - old_size;
1835 1.1 reinoud
1836 1.1 reinoud DPRINTF(ALLOC, ("\tfrom %"PRIu64" to %"PRIu64"\n", old_size, new_size));
1837 1.1 reinoud
1838 1.1 reinoud evacuated_data = NULL;
1839 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
1840 1.1 reinoud if (l_ad + size_diff <= max_l_ad) {
1841 1.1 reinoud /* only reflect size change directly in the node */
1842 1.1 reinoud inflen += size_diff;
1843 1.1 reinoud objsize += size_diff;
1844 1.1 reinoud l_ad += size_diff;
1845 1.1 reinoud crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
1846 1.1 reinoud if (fe) {
1847 1.1 reinoud fe->inf_len = udf_rw64(inflen);
1848 1.1 reinoud fe->l_ad = udf_rw32(l_ad);
1849 1.1 reinoud fe->tag.desc_crc_len = udf_rw32(crclen);
1850 1.1 reinoud } else {
1851 1.1 reinoud efe->inf_len = udf_rw64(inflen);
1852 1.1 reinoud efe->obj_size = udf_rw64(objsize);
1853 1.1 reinoud efe->l_ad = udf_rw32(l_ad);
1854 1.1 reinoud efe->tag.desc_crc_len = udf_rw32(crclen);
1855 1.1 reinoud }
1856 1.1 reinoud error = 0;
1857 1.1 reinoud
1858 1.1 reinoud /* set new size for uvm */
1859 1.1 reinoud uvm_vnp_setsize(vp, old_size);
1860 1.1 reinoud uvm_vnp_setwritesize(vp, new_size);
1861 1.1 reinoud
1862 1.1 reinoud #if 0
1863 1.1 reinoud /* zero append space in buffer */
1864 1.1 reinoud uvm_vnp_zerorange(vp, old_size, new_size - old_size);
1865 1.1 reinoud #endif
1866 1.1 reinoud
1867 1.1 reinoud /* unlock */
1868 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
1869 1.1 reinoud
1870 1.1 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
1871 1.1 reinoud KASSERT(new_inflen == orig_inflen + size_diff);
1872 1.1 reinoud KASSERT(new_lbrec == orig_lbrec);
1873 1.1 reinoud KASSERT(new_lbrec == 0);
1874 1.1 reinoud return 0;
1875 1.1 reinoud }
1876 1.1 reinoud
1877 1.1 reinoud DPRINTF(ALLOC, ("\tCONVERT from internal\n"));
1878 1.1 reinoud
1879 1.1 reinoud if (old_size > 0) {
1880 1.1 reinoud /* allocate some space and copy in the stuff to keep */
1881 1.1 reinoud evacuated_data = malloc(lb_size, M_UDFTEMP, M_WAITOK);
1882 1.1 reinoud memset(evacuated_data, 0, lb_size);
1883 1.1 reinoud
1884 1.1 reinoud /* node is locked, so safe to exit mutex */
1885 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
1886 1.1 reinoud
1887 1.1 reinoud /* read in using the `normal' vn_rdwr() */
1888 1.1 reinoud error = vn_rdwr(UIO_READ, udf_node->vnode,
1889 1.1 reinoud evacuated_data, old_size, 0,
1890 1.1 reinoud UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
1891 1.1 reinoud FSCRED, NULL, NULL);
1892 1.1 reinoud
1893 1.1 reinoud /* enter again */
1894 1.1 reinoud UDF_LOCK_NODE(udf_node, 0);
1895 1.1 reinoud }
1896 1.1 reinoud
1897 1.1 reinoud /* convert to a normal alloc */
1898 1.1 reinoud /* XXX HOWTO selecting allocation method ? */
1899 1.1 reinoud icbflags &= ~UDF_ICB_TAG_FLAGS_ALLOC_MASK;
1900 1.1 reinoud icbflags |= UDF_ICB_LONG_ALLOC; /* XXX or SHORT_ALLOC */
1901 1.1 reinoud icbtag->flags = udf_rw16(icbflags);
1902 1.1 reinoud
1903 1.1 reinoud /* wipe old descriptor space */
1904 1.1 reinoud udf_wipe_adslots(udf_node);
1905 1.1 reinoud
1906 1.1 reinoud memset(&c_ad, 0, sizeof(struct long_ad));
1907 1.1 reinoud c_ad.len = udf_rw32(old_size | UDF_EXT_FREE);
1908 1.1 reinoud c_ad.loc.part_num = udf_rw16(0); /* not relevant */
1909 1.1 reinoud c_ad.loc.lb_num = udf_rw32(0); /* not relevant */
1910 1.1 reinoud
1911 1.1 reinoud slot = 0;
1912 1.1 reinoud } else {
1913 1.1 reinoud /* goto the last entry (if any) */
1914 1.1 reinoud slot = 0;
1915 1.1 reinoud cpy_slot = 0;
1916 1.1 reinoud foffset = 0;
1917 1.1 reinoud memset(&c_ad, 0, sizeof(struct long_ad));
1918 1.1 reinoud for (;;) {
1919 1.1 reinoud udf_get_adslot(udf_node, slot, &c_ad, &eof);
1920 1.1 reinoud if (eof)
1921 1.1 reinoud break;
1922 1.1 reinoud
1923 1.1 reinoud len = udf_rw32(c_ad.len);
1924 1.1 reinoud flags = UDF_EXT_FLAGS(len);
1925 1.1 reinoud len = UDF_EXT_LEN(len);
1926 1.1 reinoud
1927 1.1 reinoud end_foffset = foffset + len;
1928 1.1 reinoud if (flags != UDF_EXT_REDIRECT)
1929 1.1 reinoud foffset = end_foffset;
1930 1.1 reinoud
1931 1.1 reinoud slot++;
1932 1.1 reinoud }
1933 1.1 reinoud /* at end of adslots */
1934 1.1 reinoud
1935 1.1 reinoud /* special case if the old size was zero, then there is no last slot */
1936 1.1 reinoud if (old_size == 0) {
1937 1.1 reinoud c_ad.len = udf_rw32(0 | UDF_EXT_FREE);
1938 1.1 reinoud c_ad.loc.part_num = udf_rw16(0); /* not relevant */
1939 1.1 reinoud c_ad.loc.lb_num = udf_rw32(0); /* not relevant */
1940 1.1 reinoud } else {
1941 1.1 reinoud /* refetch last slot */
1942 1.1 reinoud slot--;
1943 1.1 reinoud udf_get_adslot(udf_node, slot, &c_ad, &eof);
1944 1.1 reinoud }
1945 1.1 reinoud }
1946 1.1 reinoud
1947 1.1 reinoud /*
1948 1.1 reinoud * If the length of the last slot is not a multiple of lb_size, adjust
1949 1.1 reinoud * length so that it is; don't forget to adjust `append_len'! relevant for
1950 1.1 reinoud * extending existing files
1951 1.1 reinoud */
1952 1.1 reinoud len = udf_rw32(c_ad.len);
1953 1.1 reinoud flags = UDF_EXT_FLAGS(len);
1954 1.1 reinoud len = UDF_EXT_LEN(len);
1955 1.1 reinoud
1956 1.1 reinoud lastblock_grow = 0;
1957 1.1 reinoud if (len % lb_size > 0) {
1958 1.1 reinoud lastblock_grow = lb_size - (len % lb_size);
1959 1.1 reinoud lastblock_grow = MIN(size_diff, lastblock_grow);
1960 1.1 reinoud len += lastblock_grow;
1961 1.1 reinoud c_ad.len = udf_rw32(len | flags);
1962 1.1 reinoud
1963 1.1 reinoud /* TODO zero appened space in buffer! */
1964 1.1 reinoud /* using uvm_vnp_zerorange(vp, old_size, new_size - old_size); ? */
1965 1.1 reinoud }
1966 1.1 reinoud memset(&s_ad, 0, sizeof(struct long_ad));
1967 1.1 reinoud
1968 1.1 reinoud /* size_diff can be bigger than allowed, so grow in chunks */
1969 1.1 reinoud append_len = size_diff - lastblock_grow;
1970 1.1 reinoud while (append_len > 0) {
1971 1.1 reinoud chunk = MIN(append_len, max_len);
1972 1.1 reinoud s_ad.len = udf_rw32(chunk | UDF_EXT_FREE);
1973 1.1 reinoud s_ad.loc.part_num = udf_rw16(0);
1974 1.1 reinoud s_ad.loc.lb_num = udf_rw32(0);
1975 1.1 reinoud
1976 1.1 reinoud if (udf_ads_merge(lb_size, &c_ad, &s_ad)) {
1977 1.1 reinoud /* not mergable (anymore) */
1978 1.1 reinoud error = udf_append_adslot(udf_node, slot, &c_ad);
1979 1.1 reinoud if (error)
1980 1.1 reinoud goto errorout;
1981 1.1 reinoud slot++;
1982 1.1 reinoud c_ad = s_ad;
1983 1.1 reinoud memset(&s_ad, 0, sizeof(struct long_ad));
1984 1.1 reinoud }
1985 1.1 reinoud append_len -= chunk;
1986 1.1 reinoud }
1987 1.1 reinoud
1988 1.1 reinoud /* if there is a rest piece in the accumulator, append it */
1989 1.1 reinoud if (UDF_EXT_LEN(c_ad.len) > 0) {
1990 1.1 reinoud error = udf_append_adslot(udf_node, slot, &c_ad);
1991 1.1 reinoud if (error)
1992 1.1 reinoud goto errorout;
1993 1.1 reinoud slot++;
1994 1.1 reinoud }
1995 1.1 reinoud
1996 1.1 reinoud /* if there is a rest piece that didn't fit, append it */
1997 1.1 reinoud if (UDF_EXT_LEN(s_ad.len) > 0) {
1998 1.1 reinoud error = udf_append_adslot(udf_node, slot, &s_ad);
1999 1.1 reinoud if (error)
2000 1.1 reinoud goto errorout;
2001 1.1 reinoud slot++;
2002 1.1 reinoud }
2003 1.1 reinoud
2004 1.1 reinoud inflen += size_diff;
2005 1.1 reinoud objsize += size_diff;
2006 1.1 reinoud if (fe) {
2007 1.1 reinoud fe->inf_len = udf_rw64(inflen);
2008 1.1 reinoud } else {
2009 1.1 reinoud efe->inf_len = udf_rw64(inflen);
2010 1.1 reinoud efe->obj_size = udf_rw64(objsize);
2011 1.1 reinoud }
2012 1.1 reinoud error = 0;
2013 1.1 reinoud
2014 1.1 reinoud if (evacuated_data) {
2015 1.1 reinoud /* set new write size for uvm */
2016 1.1 reinoud uvm_vnp_setwritesize(vp, old_size);
2017 1.1 reinoud
2018 1.1 reinoud /* write out evacuated data */
2019 1.1 reinoud error = vn_rdwr(UIO_WRITE, udf_node->vnode,
2020 1.1 reinoud evacuated_data, old_size, 0,
2021 1.1 reinoud UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
2022 1.1 reinoud FSCRED, NULL, NULL);
2023 1.1 reinoud uvm_vnp_setsize(vp, old_size);
2024 1.1 reinoud }
2025 1.1 reinoud
2026 1.1 reinoud errorout:
2027 1.1 reinoud if (evacuated_data)
2028 1.1 reinoud free(evacuated_data, M_UDFTEMP);
2029 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2030 1.1 reinoud
2031 1.1 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
2032 1.1 reinoud KASSERT(new_inflen == orig_inflen + size_diff);
2033 1.1 reinoud KASSERT(new_lbrec == orig_lbrec);
2034 1.1 reinoud
2035 1.1 reinoud return error;
2036 1.1 reinoud }
2037 1.1 reinoud
2038 1.1 reinoud /* --------------------------------------------------------------------- */
2039 1.1 reinoud
2040 1.1 reinoud int
2041 1.1 reinoud udf_shrink_node(struct udf_node *udf_node, uint64_t new_size)
2042 1.1 reinoud {
2043 1.1 reinoud struct vnode *vp = udf_node->vnode;
2044 1.1 reinoud struct udf_mount *ump = udf_node->ump;
2045 1.1 reinoud struct file_entry *fe;
2046 1.1 reinoud struct extfile_entry *efe;
2047 1.1 reinoud struct icb_tag *icbtag;
2048 1.1 reinoud struct long_ad c_ad, s_ad, *node_ad_cpy;
2049 1.1 reinoud uint64_t size_diff, old_size, inflen, objsize;
2050 1.1 reinoud uint64_t foffset, end_foffset;
2051 1.1 reinoud uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
2052 1.1 reinoud uint32_t lb_size, dscr_size, crclen;
2053 1.1 reinoud uint32_t slot_offset;
2054 1.1 reinoud uint32_t len, flags, max_len;
2055 1.1 reinoud uint32_t num_lb, lb_num;
2056 1.1 reinoud uint32_t max_l_ad, l_ad, l_ea;
2057 1.1 reinoud uint16_t vpart_num;
2058 1.1 reinoud uint8_t *data_pos;
2059 1.1 reinoud int icbflags, addr_type;
2060 1.1 reinoud int slot, cpy_slot, cpy_slots;
2061 1.1 reinoud int eof, error;
2062 1.1 reinoud
2063 1.1 reinoud DPRINTF(ALLOC, ("udf_shrink_node\n"));
2064 1.1 reinoud udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
2065 1.1 reinoud
2066 1.1 reinoud UDF_LOCK_NODE(udf_node, 0);
2067 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
2068 1.1 reinoud max_len = ((UDF_EXT_MAXLEN / lb_size) * lb_size);
2069 1.1 reinoud
2070 1.1 reinoud /* do the work */
2071 1.1 reinoud fe = udf_node->fe;
2072 1.1 reinoud efe = udf_node->efe;
2073 1.1 reinoud if (fe) {
2074 1.1 reinoud icbtag = &fe->icbtag;
2075 1.1 reinoud inflen = udf_rw64(fe->inf_len);
2076 1.1 reinoud objsize = inflen;
2077 1.1 reinoud dscr_size = sizeof(struct file_entry) -1;
2078 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
2079 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
2080 1.1 reinoud data_pos = (uint8_t *) fe + dscr_size + l_ea;
2081 1.1 reinoud } else {
2082 1.1 reinoud icbtag = &efe->icbtag;
2083 1.1 reinoud inflen = udf_rw64(efe->inf_len);
2084 1.1 reinoud objsize = udf_rw64(efe->obj_size);
2085 1.1 reinoud dscr_size = sizeof(struct extfile_entry) -1;
2086 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
2087 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
2088 1.1 reinoud data_pos = (uint8_t *) efe + dscr_size + l_ea;
2089 1.1 reinoud }
2090 1.1 reinoud max_l_ad = lb_size - dscr_size - l_ea;
2091 1.1 reinoud
2092 1.1 reinoud icbflags = udf_rw16(icbtag->flags);
2093 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2094 1.1 reinoud
2095 1.1 reinoud old_size = inflen;
2096 1.1 reinoud size_diff = old_size - new_size;
2097 1.1 reinoud
2098 1.1 reinoud DPRINTF(ALLOC, ("\tfrom %"PRIu64" to %"PRIu64"\n", old_size, new_size));
2099 1.1 reinoud
2100 1.1 reinoud /* shrink the node to its new size */
2101 1.1 reinoud if (addr_type == UDF_ICB_INTERN_ALLOC) {
2102 1.1 reinoud /* only reflect size change directly in the node */
2103 1.1 reinoud KASSERT(new_size <= max_l_ad);
2104 1.1 reinoud inflen -= size_diff;
2105 1.1 reinoud objsize -= size_diff;
2106 1.1 reinoud l_ad -= size_diff;
2107 1.1 reinoud crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
2108 1.1 reinoud if (fe) {
2109 1.1 reinoud fe->inf_len = udf_rw64(inflen);
2110 1.1 reinoud fe->l_ad = udf_rw32(l_ad);
2111 1.1 reinoud fe->tag.desc_crc_len = udf_rw32(crclen);
2112 1.1 reinoud } else {
2113 1.1 reinoud efe->inf_len = udf_rw64(inflen);
2114 1.1 reinoud efe->obj_size = udf_rw64(objsize);
2115 1.1 reinoud efe->l_ad = udf_rw32(l_ad);
2116 1.1 reinoud efe->tag.desc_crc_len = udf_rw32(crclen);
2117 1.1 reinoud }
2118 1.1 reinoud error = 0;
2119 1.1 reinoud /* TODO zero appened space in buffer! */
2120 1.1 reinoud /* using uvm_vnp_zerorange(vp, old_size, old_size - new_size); ? */
2121 1.1 reinoud
2122 1.1 reinoud /* set new size for uvm */
2123 1.1 reinoud uvm_vnp_setsize(vp, new_size);
2124 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2125 1.1 reinoud
2126 1.1 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
2127 1.1 reinoud KASSERT(new_inflen == orig_inflen - size_diff);
2128 1.1 reinoud KASSERT(new_lbrec == orig_lbrec);
2129 1.1 reinoud KASSERT(new_lbrec == 0);
2130 1.1 reinoud
2131 1.1 reinoud return 0;
2132 1.1 reinoud }
2133 1.1 reinoud
2134 1.1 reinoud /* setup node cleanup extents copy space */
2135 1.1 reinoud node_ad_cpy = malloc(lb_size * UDF_MAX_ALLOC_EXTENTS,
2136 1.1 reinoud M_UDFMNT, M_WAITOK);
2137 1.1 reinoud memset(node_ad_cpy, 0, lb_size * UDF_MAX_ALLOC_EXTENTS);
2138 1.1 reinoud
2139 1.1 reinoud /*
2140 1.1 reinoud * Shrink the node by releasing the allocations and truncate the last
2141 1.1 reinoud * allocation to the new size. If the new size fits into the
2142 1.1 reinoud * allocation descriptor itself, transform it into an
2143 1.1 reinoud * UDF_ICB_INTERN_ALLOC.
2144 1.1 reinoud */
2145 1.1 reinoud slot = 0;
2146 1.1 reinoud cpy_slot = 0;
2147 1.1 reinoud foffset = 0;
2148 1.1 reinoud
2149 1.1 reinoud /* 1) copy till first overlap piece to the rewrite buffer */
2150 1.1 reinoud for (;;) {
2151 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
2152 1.1 reinoud if (eof) {
2153 1.1 reinoud DPRINTF(WRITE,
2154 1.1 reinoud ("Shrink node failed: "
2155 1.1 reinoud "encountered EOF\n"));
2156 1.1 reinoud error = EINVAL;
2157 1.1 reinoud goto errorout; /* panic? */
2158 1.1 reinoud }
2159 1.1 reinoud len = udf_rw32(s_ad.len);
2160 1.1 reinoud flags = UDF_EXT_FLAGS(len);
2161 1.1 reinoud len = UDF_EXT_LEN(len);
2162 1.1 reinoud
2163 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
2164 1.1 reinoud slot++;
2165 1.1 reinoud continue;
2166 1.1 reinoud }
2167 1.1 reinoud
2168 1.1 reinoud end_foffset = foffset + len;
2169 1.1 reinoud if (end_foffset > new_size)
2170 1.1 reinoud break; /* found */
2171 1.1 reinoud
2172 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
2173 1.1 reinoud
2174 1.1 reinoud DPRINTF(ALLOC, ("\t1: vp %d, lb %d, len %d, flags %d "
2175 1.1 reinoud "-> stack\n",
2176 1.1 reinoud udf_rw16(s_ad.loc.part_num),
2177 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
2178 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
2179 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
2180 1.1 reinoud
2181 1.1 reinoud foffset = end_foffset;
2182 1.1 reinoud slot++;
2183 1.1 reinoud }
2184 1.1 reinoud slot_offset = new_size - foffset;
2185 1.1 reinoud
2186 1.1 reinoud /* 2) trunc overlapping slot at overlap and copy it */
2187 1.1 reinoud if (slot_offset > 0) {
2188 1.1 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
2189 1.1 reinoud vpart_num = udf_rw16(s_ad.loc.part_num);
2190 1.1 reinoud
2191 1.1 reinoud if (flags == UDF_EXT_ALLOCATED) {
2192 1.1 reinoud lb_num += (slot_offset + lb_size -1) / lb_size;
2193 1.1 reinoud num_lb = (len - slot_offset + lb_size - 1) / lb_size;
2194 1.1 reinoud
2195 1.1 reinoud udf_free_allocated_space(ump, lb_num, vpart_num, num_lb);
2196 1.1 reinoud }
2197 1.1 reinoud
2198 1.1 reinoud s_ad.len = udf_rw32(slot_offset | flags);
2199 1.1 reinoud node_ad_cpy[cpy_slot++] = s_ad;
2200 1.1 reinoud slot++;
2201 1.1 reinoud
2202 1.1 reinoud DPRINTF(ALLOC, ("\t2: vp %d, lb %d, len %d, flags %d "
2203 1.1 reinoud "-> stack\n",
2204 1.1 reinoud udf_rw16(s_ad.loc.part_num),
2205 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
2206 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
2207 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
2208 1.1 reinoud }
2209 1.1 reinoud
2210 1.1 reinoud /* 3) delete remainder */
2211 1.1 reinoud for (;;) {
2212 1.1 reinoud udf_get_adslot(udf_node, slot, &s_ad, &eof);
2213 1.1 reinoud if (eof)
2214 1.1 reinoud break;
2215 1.1 reinoud
2216 1.1 reinoud len = udf_rw32(s_ad.len);
2217 1.1 reinoud flags = UDF_EXT_FLAGS(len);
2218 1.1 reinoud len = UDF_EXT_LEN(len);
2219 1.1 reinoud
2220 1.1 reinoud if (flags == UDF_EXT_REDIRECT) {
2221 1.1 reinoud slot++;
2222 1.1 reinoud continue;
2223 1.1 reinoud }
2224 1.1 reinoud
2225 1.1 reinoud DPRINTF(ALLOC, ("\t3: delete remainder "
2226 1.1 reinoud "vp %d lb %d, len %d, flags %d\n",
2227 1.1 reinoud udf_rw16(s_ad.loc.part_num),
2228 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
2229 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
2230 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
2231 1.1 reinoud
2232 1.1 reinoud if (flags == UDF_EXT_ALLOCATED) {
2233 1.1 reinoud lb_num = udf_rw32(s_ad.loc.lb_num);
2234 1.1 reinoud vpart_num = udf_rw16(s_ad.loc.part_num);
2235 1.1 reinoud num_lb = (len + lb_size - 1) / lb_size;
2236 1.1 reinoud
2237 1.1 reinoud udf_free_allocated_space(ump, lb_num, vpart_num,
2238 1.1 reinoud num_lb);
2239 1.1 reinoud }
2240 1.1 reinoud
2241 1.1 reinoud slot++;
2242 1.1 reinoud }
2243 1.1 reinoud
2244 1.1 reinoud /* 4) if it will fit into the descriptor then convert */
2245 1.1 reinoud if (new_size < max_l_ad) {
2246 1.1 reinoud /*
2247 1.1 reinoud * resque/evacuate old piece by reading it in, and convert it
2248 1.1 reinoud * to internal alloc.
2249 1.1 reinoud */
2250 1.1 reinoud if (new_size == 0) {
2251 1.1 reinoud /* XXX/TODO only for zero sizing now */
2252 1.1 reinoud udf_wipe_adslots(udf_node);
2253 1.1 reinoud
2254 1.1 reinoud icbflags &= ~UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2255 1.1 reinoud icbflags |= UDF_ICB_INTERN_ALLOC;
2256 1.1 reinoud icbtag->flags = udf_rw16(icbflags);
2257 1.1 reinoud
2258 1.1 reinoud inflen -= size_diff; KASSERT(inflen == 0);
2259 1.1 reinoud objsize -= size_diff;
2260 1.1 reinoud l_ad = new_size;
2261 1.1 reinoud crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
2262 1.1 reinoud if (fe) {
2263 1.1 reinoud fe->inf_len = udf_rw64(inflen);
2264 1.1 reinoud fe->l_ad = udf_rw32(l_ad);
2265 1.1 reinoud fe->tag.desc_crc_len = udf_rw32(crclen);
2266 1.1 reinoud } else {
2267 1.1 reinoud efe->inf_len = udf_rw64(inflen);
2268 1.1 reinoud efe->obj_size = udf_rw64(objsize);
2269 1.1 reinoud efe->l_ad = udf_rw32(l_ad);
2270 1.1 reinoud efe->tag.desc_crc_len = udf_rw32(crclen);
2271 1.1 reinoud }
2272 1.1 reinoud /* eventually copy in evacuated piece */
2273 1.1 reinoud /* set new size for uvm */
2274 1.1 reinoud uvm_vnp_setsize(vp, new_size);
2275 1.1 reinoud
2276 1.1 reinoud free(node_ad_cpy, M_UDFMNT);
2277 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2278 1.1 reinoud
2279 1.1 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
2280 1.1 reinoud KASSERT(new_inflen == orig_inflen - size_diff);
2281 1.1 reinoud KASSERT(new_inflen == 0);
2282 1.1 reinoud KASSERT(new_lbrec == 0);
2283 1.1 reinoud
2284 1.1 reinoud return 0;
2285 1.1 reinoud }
2286 1.1 reinoud
2287 1.1 reinoud printf("UDF_SHRINK_NODE: could convert to internal alloc!\n");
2288 1.1 reinoud }
2289 1.1 reinoud
2290 1.1 reinoud /* 5) reset node descriptors */
2291 1.1 reinoud udf_wipe_adslots(udf_node);
2292 1.1 reinoud
2293 1.1 reinoud /* 6) copy back extents; merge when possible. Recounting on the fly */
2294 1.1 reinoud cpy_slots = cpy_slot;
2295 1.1 reinoud
2296 1.1 reinoud c_ad = node_ad_cpy[0];
2297 1.1 reinoud slot = 0;
2298 1.1 reinoud for (cpy_slot = 1; cpy_slot < cpy_slots; cpy_slot++) {
2299 1.1 reinoud s_ad = node_ad_cpy[cpy_slot];
2300 1.1 reinoud
2301 1.1 reinoud DPRINTF(ALLOC, ("\t6: stack -> got mapping vp %d "
2302 1.1 reinoud "lb %d, len %d, flags %d\n",
2303 1.1 reinoud udf_rw16(s_ad.loc.part_num),
2304 1.1 reinoud udf_rw32(s_ad.loc.lb_num),
2305 1.1 reinoud UDF_EXT_LEN(udf_rw32(s_ad.len)),
2306 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
2307 1.1 reinoud
2308 1.1 reinoud /* see if we can merge */
2309 1.1 reinoud if (udf_ads_merge(lb_size, &c_ad, &s_ad)) {
2310 1.1 reinoud /* not mergable (anymore) */
2311 1.1 reinoud DPRINTF(ALLOC, ("\t6: appending vp %d lb %d, "
2312 1.1 reinoud "len %d, flags %d\n",
2313 1.1 reinoud udf_rw16(c_ad.loc.part_num),
2314 1.1 reinoud udf_rw32(c_ad.loc.lb_num),
2315 1.1 reinoud UDF_EXT_LEN(udf_rw32(c_ad.len)),
2316 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
2317 1.1 reinoud
2318 1.1 reinoud error = udf_append_adslot(udf_node, slot, &c_ad);
2319 1.1 reinoud if (error)
2320 1.1 reinoud goto errorout; /* panic? */
2321 1.1 reinoud c_ad = s_ad;
2322 1.1 reinoud slot++;
2323 1.1 reinoud }
2324 1.1 reinoud }
2325 1.1 reinoud
2326 1.1 reinoud /* 7) push rest slot (if any) */
2327 1.1 reinoud if (UDF_EXT_LEN(c_ad.len) > 0) {
2328 1.1 reinoud DPRINTF(ALLOC, ("\t7: last append vp %d lb %d, "
2329 1.1 reinoud "len %d, flags %d\n",
2330 1.1 reinoud udf_rw16(c_ad.loc.part_num),
2331 1.1 reinoud udf_rw32(c_ad.loc.lb_num),
2332 1.1 reinoud UDF_EXT_LEN(udf_rw32(c_ad.len)),
2333 1.1 reinoud UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
2334 1.1 reinoud
2335 1.1 reinoud error = udf_append_adslot(udf_node, slot, &c_ad);
2336 1.1 reinoud if (error)
2337 1.1 reinoud goto errorout; /* panic? */
2338 1.1 reinoud ;
2339 1.1 reinoud }
2340 1.1 reinoud
2341 1.1 reinoud inflen -= size_diff;
2342 1.1 reinoud objsize -= size_diff;
2343 1.1 reinoud if (fe) {
2344 1.1 reinoud fe->inf_len = udf_rw64(inflen);
2345 1.1 reinoud } else {
2346 1.1 reinoud efe->inf_len = udf_rw64(inflen);
2347 1.1 reinoud efe->obj_size = udf_rw64(objsize);
2348 1.1 reinoud }
2349 1.1 reinoud error = 0;
2350 1.1 reinoud
2351 1.1 reinoud /* set new size for uvm */
2352 1.1 reinoud uvm_vnp_setsize(vp, new_size);
2353 1.1 reinoud
2354 1.1 reinoud errorout:
2355 1.1 reinoud free(node_ad_cpy, M_UDFMNT);
2356 1.1 reinoud UDF_UNLOCK_NODE(udf_node, 0);
2357 1.1 reinoud
2358 1.1 reinoud udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
2359 1.1 reinoud KASSERT(new_inflen == orig_inflen - size_diff);
2360 1.1 reinoud
2361 1.1 reinoud return error;
2362 1.1 reinoud }
2363 1.1 reinoud
2364