1 /* $NetBSD: udf_strat_direct.c,v 1.3 2008/07/28 19:41:13 reinoud Exp $ */ 2 3 /* 4 * Copyright (c) 2006, 2008 Reinoud Zandijk 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 #ifndef lint 31 __KERNEL_RCSID(0, "$NetBSD: udf_strat_direct.c,v 1.3 2008/07/28 19:41:13 reinoud Exp $"); 32 #endif /* not lint */ 33 34 35 #if defined(_KERNEL_OPT) 36 #include "opt_quota.h" 37 #include "opt_compat_netbsd.h" 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/sysctl.h> 43 #include <sys/namei.h> 44 #include <sys/proc.h> 45 #include <sys/kernel.h> 46 #include <sys/vnode.h> 47 #include <miscfs/genfs/genfs_node.h> 48 #include <sys/mount.h> 49 #include <sys/buf.h> 50 #include <sys/file.h> 51 #include <sys/device.h> 52 #include <sys/disklabel.h> 53 #include <sys/ioctl.h> 54 #include <sys/malloc.h> 55 #include <sys/dirent.h> 56 #include <sys/stat.h> 57 #include <sys/conf.h> 58 #include <sys/kauth.h> 59 #include <sys/kthread.h> 60 #include <dev/clock_subr.h> 61 62 #include <fs/udf/ecma167-udf.h> 63 #include <fs/udf/udf_mount.h> 64 65 #if defined(_KERNEL_OPT) 66 #include "opt_udf.h" 67 #endif 68 69 #include "udf.h" 70 #include "udf_subr.h" 71 #include "udf_bswap.h" 72 73 74 #define VTOI(vnode) ((struct udf_node *) vnode->v_data) 75 #define PRIV(ump) ((struct strat_private *) ump->strategy_private) 76 77 /* --------------------------------------------------------------------- */ 78 79 /* BUFQ's */ 80 #define UDF_SHED_MAX 3 81 82 #define UDF_SHED_READING 0 83 #define UDF_SHED_WRITING 1 84 #define UDF_SHED_SEQWRITING 2 85 86 87 struct strat_private { 88 struct pool desc_pool; /* node descriptors */ 89 }; 90 91 /* --------------------------------------------------------------------- */ 92 93 static void 94 udf_wr_nodedscr_callback(struct buf *buf) 95 { 96 struct udf_node *udf_node; 97 98 KASSERT(buf); 99 KASSERT(buf->b_data); 100 101 /* called when write action is done */ 102 DPRINTF(WRITE, ("udf_wr_nodedscr_callback(): node written out\n")); 103 104 udf_node = VTOI(buf->b_vp); 105 if (udf_node == NULL) { 106 putiobuf(buf); 107 printf("udf_wr_node_callback: NULL node?\n"); 108 return; 109 } 110 111 /* XXX right flags to mark dirty again on error? */ 112 if (buf->b_error) { 113 /* write error on `defect free' media??? how to solve? */ 114 /* XXX lookup UDF standard for unallocatable space */ 115 udf_node->i_flags |= IN_MODIFIED | IN_ACCESSED; 116 } 117 118 /* decrement outstanding_nodedscr */ 119 KASSERT(udf_node->outstanding_nodedscr >= 1); 120 udf_node->outstanding_nodedscr--; 121 if (udf_node->outstanding_nodedscr == 0) { 122 /* unlock the node */ 123 KASSERT(udf_node->i_flags & IN_CALLBACK_ULK); 124 UDF_UNLOCK_NODE(udf_node, IN_CALLBACK_ULK); 125 126 wakeup(&udf_node->outstanding_nodedscr); 127 } 128 /* unreference the vnode so it can be recycled */ 129 holdrele(udf_node->vnode); 130 131 putiobuf(buf); 132 } 133 134 /* --------------------------------------------------------------------- */ 135 136 static int 137 udf_getblank_nodedscr_direct(struct udf_strat_args *args) 138 { 139 union dscrptr **dscrptr = &args->dscr; 140 struct udf_mount *ump = args->ump; 141 struct strat_private *priv = PRIV(ump); 142 uint32_t lb_size; 143 144 lb_size = udf_rw32(ump->logical_vol->lb_size); 145 *dscrptr = pool_get(&priv->desc_pool, PR_WAITOK); 146 memset(*dscrptr, 0, lb_size); 147 148 return 0; 149 } 150 151 152 static void 153 udf_free_nodedscr_direct(struct udf_strat_args *args) 154 { 155 union dscrptr *dscr = args->dscr; 156 struct udf_mount *ump = args->ump; 157 struct strat_private *priv = PRIV(ump); 158 159 pool_put(&priv->desc_pool, dscr); 160 } 161 162 163 static int 164 udf_read_nodedscr_direct(struct udf_strat_args *args) 165 { 166 union dscrptr **dscrptr = &args->dscr; 167 union dscrptr *tmpdscr; 168 struct udf_mount *ump = args->ump; 169 struct long_ad *icb = args->icb; 170 struct strat_private *priv = PRIV(ump); 171 uint32_t lb_size; 172 uint32_t sector, dummy; 173 int error; 174 175 lb_size = udf_rw32(ump->logical_vol->lb_size); 176 177 error = udf_translate_vtop(ump, icb, §or, &dummy); 178 if (error) 179 return error; 180 181 /* try to read in fe/efe */ 182 error = udf_read_phys_dscr(ump, sector, M_UDFTEMP, &tmpdscr); 183 if (error) 184 return error; 185 186 *dscrptr = pool_get(&priv->desc_pool, PR_WAITOK); 187 memcpy(*dscrptr, tmpdscr, lb_size); 188 free(tmpdscr, M_UDFTEMP); 189 190 return 0; 191 } 192 193 194 static int 195 udf_write_nodedscr_direct(struct udf_strat_args *args) 196 { 197 struct udf_mount *ump = args->ump; 198 struct udf_node *udf_node = args->udf_node; 199 union dscrptr *dscr = args->dscr; 200 struct long_ad *icb = args->icb; 201 int waitfor = args->waitfor; 202 uint32_t logsector, sector, dummy; 203 int error, vpart; 204 205 /* 206 * we have to decide if we write it out sequential or at its fixed 207 * position by examining the partition its (to be) written on. 208 */ 209 vpart = udf_rw16(udf_node->loc.loc.part_num); 210 logsector = udf_rw32(icb->loc.lb_num); 211 KASSERT(ump->vtop_tp[vpart] != UDF_VTOP_TYPE_VIRT); 212 213 sector = 0; 214 error = udf_translate_vtop(ump, icb, §or, &dummy); 215 if (error) 216 goto out; 217 218 /* add reference to the vnode to prevent recycling */ 219 vhold(udf_node->vnode); 220 221 if (waitfor) { 222 DPRINTF(WRITE, ("udf_write_nodedscr: sync write\n")); 223 224 error = udf_write_phys_dscr_sync(ump, udf_node, UDF_C_NODE, 225 dscr, sector, logsector); 226 } else { 227 DPRINTF(WRITE, ("udf_write_nodedscr: no wait, async write\n")); 228 229 error = udf_write_phys_dscr_async(ump, udf_node, UDF_C_NODE, 230 dscr, sector, logsector, udf_wr_nodedscr_callback); 231 /* will be UNLOCKED in call back */ 232 return error; 233 } 234 235 holdrele(udf_node->vnode); 236 out: 237 udf_node->outstanding_nodedscr--; 238 if (udf_node->outstanding_nodedscr == 0) { 239 UDF_UNLOCK_NODE(udf_node, 0); 240 wakeup(&udf_node->outstanding_nodedscr); 241 } 242 243 return error; 244 } 245 246 /* --------------------------------------------------------------------- */ 247 248 static void 249 udf_queue_buf_direct(struct udf_strat_args *args) 250 { 251 struct udf_mount *ump = args->ump; 252 struct buf *buf = args->nestbuf; 253 struct buf *nestbuf; 254 struct desc_tag *tag; 255 struct long_ad *node_ad_cpy; 256 uint64_t *lmapping, *pmapping, *lmappos, blknr, run_start; 257 uint32_t our_sectornr, sectornr; 258 uint32_t lb_size, buf_offset, rbuflen, bpos; 259 uint16_t vpart_num; 260 uint8_t *fidblk; 261 off_t rblk; 262 int sector_size = ump->discinfo.sector_size; 263 int blks = sector_size / DEV_BSIZE; 264 int len, buf_len, sector, sectors, run_length; 265 int what, class, queue; 266 267 KASSERT(ump); 268 KASSERT(buf); 269 KASSERT(buf->b_iodone == nestiobuf_iodone); 270 271 what = buf->b_udf_c_type; 272 queue = UDF_SHED_READING; 273 if ((buf->b_flags & B_READ) == 0) { 274 /* writing */ 275 queue = UDF_SHED_SEQWRITING; 276 if (what == UDF_C_DSCR) 277 queue = UDF_SHED_WRITING; 278 if (what == UDF_C_NODE) 279 queue = UDF_SHED_WRITING; 280 } 281 282 /* use disc sheduler */ 283 class = ump->discinfo.mmc_class; 284 KASSERT((class == MMC_CLASS_UNKN) || (class == MMC_CLASS_DISC) || 285 (ump->discinfo.mmc_cur & MMC_CAP_HW_DEFECTFREE)); 286 287 if (queue == UDF_SHED_READING) { 288 DPRINTF(SHEDULE, ("\nudf_issue_buf READ %p : sector %d type %d," 289 "b_resid %d, b_bcount %d, b_bufsize %d\n", 290 buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type, 291 buf->b_resid, buf->b_bcount, buf->b_bufsize)); 292 VOP_STRATEGY(ump->devvp, buf); 293 return; 294 } 295 296 /* (sectorsize == lb_size) for UDF */ 297 lb_size = udf_rw32(ump->logical_vol->lb_size); 298 blknr = buf->b_blkno; 299 our_sectornr = blknr / blks; 300 301 if (queue == UDF_SHED_WRITING) { 302 DPRINTF(SHEDULE, ("\nudf_issue_buf WRITE %p : sector %d " 303 "type %d, b_resid %d, b_bcount %d, b_bufsize %d\n", 304 buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type, 305 buf->b_resid, buf->b_bcount, buf->b_bufsize)); 306 /* if we have FIDs fixup using buffer's sector number(s) */ 307 if (buf->b_udf_c_type == UDF_C_FIDS) { 308 panic("UDF_C_FIDS in SHED_WRITING!\n"); 309 buf_len = buf->b_bcount; 310 sectornr = our_sectornr; 311 bpos = 0; 312 while (buf_len) { 313 len = MIN(buf_len, sector_size); 314 fidblk = (uint8_t *) buf->b_data + bpos; 315 udf_fixup_fid_block(fidblk, sector_size, 316 0, len, sectornr); 317 sectornr++; 318 bpos += len; 319 buf_len -= len; 320 } 321 } 322 udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type); 323 VOP_STRATEGY(ump->devvp, buf); 324 return; 325 } 326 327 /* UDF_SHED_SEQWRITING */ 328 KASSERT(queue == UDF_SHED_SEQWRITING); 329 DPRINTF(SHEDULE, ("\nudf_issue_buf SEQWRITE %p : sector XXXX " 330 "type %d, b_resid %d, b_bcount %d, b_bufsize %d\n", 331 buf, buf->b_udf_c_type, buf->b_resid, buf->b_bcount, 332 buf->b_bufsize)); 333 334 /* 335 * Buffers should not have been allocated to disc addresses yet on 336 * this queue. Note that a buffer can get multiple extents allocated. 337 * 338 * lmapping contains lb_num relative to base partition. 339 */ 340 lmapping = ump->la_lmapping; 341 node_ad_cpy = ump->la_node_ad_cpy; 342 343 /* logically allocate buf and map it in the file */ 344 udf_late_allocate_buf(ump, buf, lmapping, node_ad_cpy, &vpart_num); 345 346 /* if we have FIDs, fixup using the new allocation table */ 347 if (buf->b_udf_c_type == UDF_C_FIDS) { 348 buf_len = buf->b_bcount; 349 bpos = 0; 350 lmappos = lmapping; 351 while (buf_len) { 352 sectornr = *lmappos++; 353 len = MIN(buf_len, sector_size); 354 fidblk = (uint8_t *) buf->b_data + bpos; 355 udf_fixup_fid_block(fidblk, sector_size, 356 0, len, sectornr); 357 bpos += len; 358 buf_len -= len; 359 } 360 } 361 if (buf->b_udf_c_type == UDF_C_METADATA_SBM) { 362 if (buf->b_lblkno == 0) { 363 /* update the tag location inside */ 364 tag = (struct desc_tag *) buf->b_data; 365 tag->tag_loc = udf_rw32(buf->b_blkno); 366 udf_validate_tag_and_crc_sums(buf->b_data); 367 } 368 } 369 udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type); 370 371 /* 372 * Translate new mappings in lmapping to pmappings and try to 373 * conglomerate extents to reduce the number of writes. 374 * 375 * pmapping to contain lb_nums as used for disc adressing. 376 */ 377 pmapping = ump->la_pmapping; 378 sectors = (buf->b_bcount + sector_size -1) / sector_size; 379 udf_translate_vtop_list(ump, sectors, vpart_num, lmapping, pmapping); 380 381 for (sector = 0; sector < sectors; sector++) { 382 buf_offset = sector * sector_size; 383 DPRINTF(WRITE, ("\tprocessing rel sector %d\n", sector)); 384 385 DPRINTF(WRITE, ("\tissue write sector %"PRIu64"\n", 386 pmapping[sector])); 387 388 run_start = pmapping[sector]; 389 run_length = 1; 390 while (sector < sectors-1) { 391 if (pmapping[sector+1] != pmapping[sector]+1) 392 break; 393 run_length++; 394 sector++; 395 } 396 397 /* nest an iobuf for the extent */ 398 rbuflen = run_length * sector_size; 399 rblk = run_start * (sector_size/DEV_BSIZE); 400 401 nestbuf = getiobuf(NULL, true); 402 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen); 403 /* nestbuf is B_ASYNC */ 404 405 /* identify this nestbuf */ 406 nestbuf->b_lblkno = sector; 407 assert(nestbuf->b_vp == buf->b_vp); 408 409 /* CD shedules on raw blkno */ 410 nestbuf->b_blkno = rblk; 411 nestbuf->b_proc = NULL; 412 nestbuf->b_rawblkno = rblk; 413 nestbuf->b_udf_c_type = UDF_C_PROCESSED; 414 415 VOP_STRATEGY(ump->devvp, nestbuf); 416 } 417 } 418 419 420 static void 421 udf_discstrat_init_direct(struct udf_strat_args *args) 422 { 423 struct udf_mount *ump = args->ump; 424 struct strat_private *priv = PRIV(ump); 425 uint32_t lb_size; 426 427 KASSERT(priv == NULL); 428 ump->strategy_private = malloc(sizeof(struct strat_private), 429 M_UDFTEMP, M_WAITOK); 430 priv = ump->strategy_private; 431 memset(priv, 0 , sizeof(struct strat_private)); 432 433 /* 434 * Initialise pool for descriptors associated with nodes. This is done 435 * in lb_size units though currently lb_size is dictated to be 436 * sector_size. 437 */ 438 memset(&priv->desc_pool, 0, sizeof(struct pool)); 439 440 lb_size = udf_rw32(ump->logical_vol->lb_size); 441 pool_init(&priv->desc_pool, lb_size, 0, 0, 0, "udf_desc_pool", NULL, 442 IPL_NONE); 443 } 444 445 446 static void 447 udf_discstrat_finish_direct(struct udf_strat_args *args) 448 { 449 struct udf_mount *ump = args->ump; 450 struct strat_private *priv = PRIV(ump); 451 452 /* destroy our pool */ 453 pool_destroy(&priv->desc_pool); 454 455 /* free our private space */ 456 free(ump->strategy_private, M_UDFTEMP); 457 ump->strategy_private = NULL; 458 } 459 460 /* --------------------------------------------------------------------- */ 461 462 struct udf_strategy udf_strat_direct = 463 { 464 udf_getblank_nodedscr_direct, 465 udf_free_nodedscr_direct, 466 udf_read_nodedscr_direct, 467 udf_write_nodedscr_direct, 468 udf_queue_buf_direct, 469 udf_discstrat_init_direct, 470 udf_discstrat_finish_direct 471 }; 472 473