1 /** 2 * device.c - Low level device io functions. Originated from the Linux-NTFS project. 3 * 4 * Copyright (c) 2004-2006 Anton Altaparmakov 5 * Copyright (c) 2004-2006 Szabolcs Szakacsits 6 * 7 * This program/include file is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as published 9 * by the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program/include file is distributed in the hope that it will be 13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program (in the main directory of the NTFS-3G 19 * distribution in the file COPYING); if not, write to the Free Software 20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include "config.h" 25 #endif 26 27 #ifdef HAVE_UNISTD_H 28 #include <unistd.h> 29 #endif 30 #ifdef HAVE_STDLIB_H 31 #include <stdlib.h> 32 #endif 33 #ifdef HAVE_STRING_H 34 #include <string.h> 35 #endif 36 #ifdef HAVE_ERRNO_H 37 #include <errno.h> 38 #endif 39 #ifdef HAVE_STDIO_H 40 #include <stdio.h> 41 #endif 42 #ifdef HAVE_SYS_TYPES_H 43 #include <sys/types.h> 44 #endif 45 #ifdef HAVE_SYS_STAT_H 46 #include <sys/stat.h> 47 #endif 48 #ifdef HAVE_FCNTL_H 49 #include <fcntl.h> 50 #endif 51 #ifdef HAVE_SYS_IOCTL_H 52 #include <sys/ioctl.h> 53 #endif 54 #ifdef HAVE_SYS_PARAM_H 55 #include <sys/param.h> 56 #endif 57 #ifdef HAVE_SYS_MOUNT_H 58 #include <sys/mount.h> 59 #endif 60 #ifdef HAVE_LINUX_FD_H 61 #include <linux/fd.h> 62 #endif 63 #ifdef HAVE_LINUX_HDREG_H 64 #include <linux/hdreg.h> 65 #endif 66 67 #include "types.h" 68 #include "mst.h" 69 #include "debug.h" 70 #include "device.h" 71 #include "logging.h" 72 #include "misc.h" 73 74 #if defined(linux) && defined(_IO) && !defined(BLKGETSIZE) 75 #define BLKGETSIZE _IO(0x12,96) /* Get device size in 512-byte blocks. */ 76 #endif 77 #if defined(linux) && defined(_IOR) && !defined(BLKGETSIZE64) 78 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* Get device size in bytes. */ 79 #endif 80 #if defined(linux) && !defined(HDIO_GETGEO) 81 #define HDIO_GETGEO 0x0301 /* Get device geometry. */ 82 #endif 83 #if defined(linux) && defined(_IO) && !defined(BLKSSZGET) 84 # define BLKSSZGET _IO(0x12,104) /* Get device sector size in bytes. */ 85 #endif 86 #if defined(linux) && defined(_IO) && !defined(BLKBSZSET) 87 # define BLKBSZSET _IOW(0x12,113,size_t) /* Set device block size in bytes. */ 88 #endif 89 90 /** 91 * ntfs_device_alloc - allocate an ntfs device structure and pre-initialize it 92 * @name: name of the device (must be present) 93 * @state: initial device state (usually zero) 94 * @dops: ntfs device operations to use with the device (must be present) 95 * @priv_data: pointer to private data (optional) 96 * 97 * Allocate an ntfs device structure and pre-initialize it with the user- 98 * specified device operations @dops, device state @state, device name @name, 99 * and optional private data @priv_data. 100 * 101 * Note, @name is copied and can hence be freed after this functions returns. 102 * 103 * On success return a pointer to the allocated ntfs device structure and on 104 * error return NULL with errno set to the error code returned by ntfs_malloc(). 105 */ 106 struct ntfs_device *ntfs_device_alloc(const char *name, const long state, 107 struct ntfs_device_operations *dops, void *priv_data) 108 { 109 struct ntfs_device *dev; 110 111 if (!name) { 112 errno = EINVAL; 113 return NULL; 114 } 115 116 dev = ntfs_malloc(sizeof(struct ntfs_device)); 117 if (dev) { 118 if (!(dev->d_name = strdup(name))) { 119 int eo = errno; 120 free(dev); 121 errno = eo; 122 return NULL; 123 } 124 dev->d_ops = dops; 125 dev->d_state = state; 126 dev->d_private = priv_data; 127 } 128 return dev; 129 } 130 131 /** 132 * ntfs_device_free - free an ntfs device structure 133 * @dev: ntfs device structure to free 134 * 135 * Free the ntfs device structure @dev. 136 * 137 * Return 0 on success or -1 on error with errno set to the error code. The 138 * following error codes are defined: 139 * EINVAL Invalid pointer @dev. 140 * EBUSY Device is still open. Close it before freeing it! 141 */ 142 int ntfs_device_free(struct ntfs_device *dev) 143 { 144 if (!dev) { 145 errno = EINVAL; 146 return -1; 147 } 148 if (NDevOpen(dev)) { 149 errno = EBUSY; 150 return -1; 151 } 152 free(dev->d_name); 153 free(dev); 154 return 0; 155 } 156 157 /** 158 * ntfs_pread - positioned read from disk 159 * @dev: device to read from 160 * @pos: position in device to read from 161 * @count: number of bytes to read 162 * @b: output data buffer 163 * 164 * This function will read @count bytes from device @dev at position @pos into 165 * the data buffer @b. 166 * 167 * On success, return the number of successfully read bytes. If this number is 168 * lower than @count this means that we have either reached end of file or 169 * encountered an error during the read so that the read is partial. 0 means 170 * end of file or nothing to read (@count is 0). 171 * 172 * On error and nothing has been read, return -1 with errno set appropriately 173 * to the return code of either seek, read, or set to EINVAL in case of 174 * invalid arguments. 175 */ 176 s64 ntfs_pread(struct ntfs_device *dev, const s64 pos, s64 count, void *b) 177 { 178 s64 br, total; 179 struct ntfs_device_operations *dops; 180 181 ntfs_log_trace("Entering for pos 0x%llx, count 0x%llx.\n", pos, count); 182 183 if (!b || count < 0 || pos < 0) { 184 errno = EINVAL; 185 return -1; 186 } 187 if (!count) 188 return 0; 189 190 dops = dev->d_ops; 191 192 for (total = 0; count; count -= br, total += br) { 193 br = dops->pread(dev, (char*)b + total, count, pos + total); 194 /* If everything ok, continue. */ 195 if (br > 0) 196 continue; 197 /* If EOF or error return number of bytes read. */ 198 if (!br || total) 199 return total; 200 /* Nothing read and error, return error status. */ 201 return br; 202 } 203 /* Finally, return the number of bytes read. */ 204 return total; 205 } 206 207 /** 208 * ntfs_pwrite - positioned write to disk 209 * @dev: device to write to 210 * @pos: position in file descriptor to write to 211 * @count: number of bytes to write 212 * @b: data buffer to write to disk 213 * 214 * This function will write @count bytes from data buffer @b to the device @dev 215 * at position @pos. 216 * 217 * On success, return the number of successfully written bytes. If this number 218 * is lower than @count this means that the write has been interrupted in 219 * flight or that an error was encountered during the write so that the write 220 * is partial. 0 means nothing was written (also return 0 when @count is 0). 221 * 222 * On error and nothing has been written, return -1 with errno set 223 * appropriately to the return code of either seek, write, or set 224 * to EINVAL in case of invalid arguments. 225 */ 226 s64 ntfs_pwrite(struct ntfs_device *dev, const s64 pos, s64 count, 227 const void *b) 228 { 229 s64 written, total, ret = -1; 230 struct ntfs_device_operations *dops; 231 232 ntfs_log_trace("Entering for pos 0x%llx, count 0x%llx.\n", pos, count); 233 if (!b || count < 0 || pos < 0) { 234 errno = EINVAL; 235 goto out; 236 } 237 if (!count) 238 return 0; 239 if (NDevReadOnly(dev)) { 240 errno = EROFS; 241 goto out; 242 } 243 244 dops = dev->d_ops; 245 246 NDevSetDirty(dev); 247 for (total = 0; count; count -= written, total += written) { 248 written = dops->pwrite(dev, (const char*)b + total, count, 249 pos + total); 250 /* If everything ok, continue. */ 251 if (written > 0) 252 continue; 253 /* 254 * If nothing written or error return number of bytes written. 255 */ 256 if (!written || total) 257 break; 258 /* Nothing written and error, return error status. */ 259 total = written; 260 break; 261 } 262 ret = total; 263 out: 264 return ret; 265 } 266 267 /** 268 * ntfs_mst_pread - multi sector transfer (mst) positioned read 269 * @dev: device to read from 270 * @pos: position in file descriptor to read from 271 * @count: number of blocks to read 272 * @bksize: size of each block that needs mst deprotecting 273 * @b: output data buffer 274 * 275 * Multi sector transfer (mst) positioned read. This function will read @count 276 * blocks of size @bksize bytes each from device @dev at position @pos into the 277 * the data buffer @b. 278 * 279 * On success, return the number of successfully read blocks. If this number is 280 * lower than @count this means that we have reached end of file, that the read 281 * was interrupted, or that an error was encountered during the read so that 282 * the read is partial. 0 means end of file or nothing was read (also return 0 283 * when @count or @bksize are 0). 284 * 285 * On error and nothing was read, return -1 with errno set appropriately to the 286 * return code of either seek, read, or set to EINVAL in case of invalid 287 * arguments. 288 * 289 * NOTE: If an incomplete multi sector transfer has been detected the magic 290 * will have been changed to magic_BAAD but no error will be returned. Thus it 291 * is possible that we return count blocks as being read but that any number 292 * (between zero and count!) of these blocks is actually subject to a multi 293 * sector transfer error. This should be detected by the caller by checking for 294 * the magic being "BAAD". 295 */ 296 s64 ntfs_mst_pread(struct ntfs_device *dev, const s64 pos, s64 count, 297 const u32 bksize, void *b) 298 { 299 s64 br, i; 300 301 if (bksize & (bksize - 1) || bksize % NTFS_BLOCK_SIZE) { 302 errno = EINVAL; 303 return -1; 304 } 305 /* Do the read. */ 306 br = ntfs_pread(dev, pos, count * bksize, b); 307 if (br < 0) 308 return br; 309 /* 310 * Apply fixups to successfully read data, disregarding any errors 311 * returned from the MST fixup function. This is because we want to 312 * fixup everything possible and we rely on the fact that the "BAAD" 313 * magic will be detected later on. 314 */ 315 count = br / bksize; 316 for (i = 0; i < count; ++i) 317 ntfs_mst_post_read_fixup((NTFS_RECORD*) 318 ((u8*)b + i * bksize), bksize); 319 /* Finally, return the number of complete blocks read. */ 320 return count; 321 } 322 323 /** 324 * ntfs_mst_pwrite - multi sector transfer (mst) positioned write 325 * @dev: device to write to 326 * @pos: position in file descriptor to write to 327 * @count: number of blocks to write 328 * @bksize: size of each block that needs mst protecting 329 * @b: data buffer to write to disk 330 * 331 * Multi sector transfer (mst) positioned write. This function will write 332 * @count blocks of size @bksize bytes each from data buffer @b to the device 333 * @dev at position @pos. 334 * 335 * On success, return the number of successfully written blocks. If this number 336 * is lower than @count this means that the write has been interrupted or that 337 * an error was encountered during the write so that the write is partial. 0 338 * means nothing was written (also return 0 when @count or @bksize are 0). 339 * 340 * On error and nothing has been written, return -1 with errno set 341 * appropriately to the return code of either seek, write, or set 342 * to EINVAL in case of invalid arguments. 343 * 344 * NOTE: We mst protect the data, write it, then mst deprotect it using a quick 345 * deprotect algorithm (no checking). This saves us from making a copy before 346 * the write and at the same time causes the usn to be incremented in the 347 * buffer. This conceptually fits in better with the idea that cached data is 348 * always deprotected and protection is performed when the data is actually 349 * going to hit the disk and the cache is immediately deprotected again 350 * simulating an mst read on the written data. This way cache coherency is 351 * achieved. 352 */ 353 s64 ntfs_mst_pwrite(struct ntfs_device *dev, const s64 pos, s64 count, 354 const u32 bksize, void *b) 355 { 356 s64 written, i; 357 358 if (count < 0 || bksize % NTFS_BLOCK_SIZE) { 359 errno = EINVAL; 360 return -1; 361 } 362 if (!count) 363 return 0; 364 /* Prepare data for writing. */ 365 for (i = 0; i < count; ++i) { 366 int err; 367 368 err = ntfs_mst_pre_write_fixup((NTFS_RECORD*) 369 ((u8*)b + i * bksize), bksize); 370 if (err < 0) { 371 /* Abort write at this position. */ 372 if (!i) 373 return err; 374 count = i; 375 break; 376 } 377 } 378 /* Write the prepared data. */ 379 written = ntfs_pwrite(dev, pos, count * bksize, b); 380 /* Quickly deprotect the data again. */ 381 for (i = 0; i < count; ++i) 382 ntfs_mst_post_write_fixup((NTFS_RECORD*)((u8*)b + i * bksize)); 383 if (written <= 0) 384 return written; 385 /* Finally, return the number of complete blocks written. */ 386 return written / bksize; 387 } 388 389 /** 390 * ntfs_cluster_read - read ntfs clusters 391 * @vol: volume to read from 392 * @lcn: starting logical cluster number 393 * @count: number of clusters to read 394 * @b: output data buffer 395 * 396 * Read @count ntfs clusters starting at logical cluster number @lcn from 397 * volume @vol into buffer @b. Return number of clusters read or -1 on error, 398 * with errno set to the error code. 399 */ 400 s64 ntfs_cluster_read(const ntfs_volume *vol, const s64 lcn, const s64 count, 401 void *b) 402 { 403 s64 br; 404 405 if (!vol || lcn < 0 || count < 0) { 406 errno = EINVAL; 407 return -1; 408 } 409 if (vol->nr_clusters < lcn + count) { 410 errno = ESPIPE; 411 ntfs_log_perror("Trying to read outside of volume " 412 "(%lld < %lld)", (long long)vol->nr_clusters, 413 (long long)lcn + count); 414 return -1; 415 } 416 br = ntfs_pread(vol->dev, lcn << vol->cluster_size_bits, 417 count << vol->cluster_size_bits, b); 418 if (br < 0) { 419 ntfs_log_perror("Error reading cluster(s)"); 420 return br; 421 } 422 return br >> vol->cluster_size_bits; 423 } 424 425 /** 426 * ntfs_cluster_write - write ntfs clusters 427 * @vol: volume to write to 428 * @lcn: starting logical cluster number 429 * @count: number of clusters to write 430 * @b: data buffer to write to disk 431 * 432 * Write @count ntfs clusters starting at logical cluster number @lcn from 433 * buffer @b to volume @vol. Return the number of clusters written or -1 on 434 * error, with errno set to the error code. 435 */ 436 s64 ntfs_cluster_write(const ntfs_volume *vol, const s64 lcn, 437 const s64 count, const void *b) 438 { 439 s64 bw; 440 441 if (!vol || lcn < 0 || count < 0) { 442 errno = EINVAL; 443 return -1; 444 } 445 if (vol->nr_clusters < lcn + count) { 446 errno = ESPIPE; 447 ntfs_log_perror("Trying to write outside of volume " 448 "(%lld < %lld)", (long long)vol->nr_clusters, 449 (long long)lcn + count); 450 return -1; 451 } 452 if (!NVolReadOnly(vol)) 453 bw = ntfs_pwrite(vol->dev, lcn << vol->cluster_size_bits, 454 count << vol->cluster_size_bits, b); 455 else 456 bw = count << vol->cluster_size_bits; 457 if (bw < 0) { 458 ntfs_log_perror("Error writing cluster(s)"); 459 return bw; 460 } 461 return bw >> vol->cluster_size_bits; 462 } 463 464 /** 465 * ntfs_device_offset_valid - test if a device offset is valid 466 * @dev: open device 467 * @ofs: offset to test for validity 468 * 469 * Test if the offset @ofs is an existing location on the device described 470 * by the open device structure @dev. 471 * 472 * Return 0 if it is valid and -1 if it is not valid. 473 */ 474 static int ntfs_device_offset_valid(struct ntfs_device *dev, s64 ofs) 475 { 476 char ch; 477 478 if (dev->d_ops->seek(dev, ofs, SEEK_SET) >= 0 && 479 dev->d_ops->read(dev, &ch, 1) == 1) 480 return 0; 481 return -1; 482 } 483 484 /** 485 * ntfs_device_size_get - return the size of a device in blocks 486 * @dev: open device 487 * @block_size: block size in bytes in which to return the result 488 * 489 * Return the number of @block_size sized blocks in the device described by the 490 * open device @dev. 491 * 492 * Adapted from e2fsutils-1.19, Copyright (C) 1995 Theodore Ts'o. 493 * 494 * On error return -1 with errno set to the error code. 495 */ 496 s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size) 497 { 498 s64 high, low; 499 500 if (!dev || block_size <= 0 || (block_size - 1) & block_size) { 501 errno = EINVAL; 502 return -1; 503 } 504 #ifdef BLKGETSIZE64 505 { u64 size; 506 507 if (dev->d_ops->ioctl(dev, BLKGETSIZE64, &size) >= 0) { 508 ntfs_log_debug("BLKGETSIZE64 nr bytes = %llu (0x%llx)\n", 509 (unsigned long long)size, 510 (unsigned long long)size); 511 return (s64)size / block_size; 512 } 513 } 514 #endif 515 #ifdef BLKGETSIZE 516 { unsigned long size; 517 518 if (dev->d_ops->ioctl(dev, BLKGETSIZE, &size) >= 0) { 519 ntfs_log_debug("BLKGETSIZE nr 512 byte blocks = %lu (0x%lx)\n", 520 size, size); 521 return (s64)size * 512 / block_size; 522 } 523 } 524 #endif 525 #ifdef FDGETPRM 526 { struct floppy_struct this_floppy; 527 528 if (dev->d_ops->ioctl(dev, FDGETPRM, &this_floppy) >= 0) { 529 ntfs_log_debug("FDGETPRM nr 512 byte blocks = %lu (0x%lx)\n", 530 (unsigned long)this_floppy.size, 531 (unsigned long)this_floppy.size); 532 return (s64)this_floppy.size * 512 / block_size; 533 } 534 } 535 #endif 536 /* 537 * We couldn't figure it out by using a specialized ioctl, 538 * so do binary search to find the size of the device. 539 */ 540 low = 0LL; 541 for (high = 1024LL; !ntfs_device_offset_valid(dev, high); high <<= 1) 542 low = high; 543 while (low < high - 1LL) { 544 const s64 mid = (low + high) / 2; 545 546 if (!ntfs_device_offset_valid(dev, mid)) 547 low = mid; 548 else 549 high = mid; 550 } 551 dev->d_ops->seek(dev, 0LL, SEEK_SET); 552 return (low + 1LL) / block_size; 553 } 554 555 /** 556 * ntfs_device_partition_start_sector_get - get starting sector of a partition 557 * @dev: open device 558 * 559 * On success, return the starting sector of the partition @dev in the parent 560 * block device of @dev. On error return -1 with errno set to the error code. 561 * 562 * The following error codes are defined: 563 * EINVAL Input parameter error 564 * EOPNOTSUPP System does not support HDIO_GETGEO ioctl 565 * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO 566 */ 567 s64 ntfs_device_partition_start_sector_get(struct ntfs_device *dev) 568 { 569 if (!dev) { 570 errno = EINVAL; 571 return -1; 572 } 573 #ifdef HDIO_GETGEO 574 { struct hd_geometry geo; 575 576 if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) { 577 ntfs_log_debug("HDIO_GETGEO start_sect = %lu (0x%lx)\n", 578 geo.start, geo.start); 579 return geo.start; 580 } 581 } 582 #else 583 errno = EOPNOTSUPP; 584 #endif 585 return -1; 586 } 587 588 /** 589 * ntfs_device_heads_get - get number of heads of device 590 * @dev: open device 591 * 592 * On success, return the number of heads on the device @dev. On error return 593 * -1 with errno set to the error code. 594 * 595 * The following error codes are defined: 596 * EINVAL Input parameter error 597 * EOPNOTSUPP System does not support HDIO_GETGEO ioctl 598 * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO 599 */ 600 int ntfs_device_heads_get(struct ntfs_device *dev) 601 { 602 if (!dev) { 603 errno = EINVAL; 604 return -1; 605 } 606 #ifdef HDIO_GETGEO 607 { struct hd_geometry geo; 608 609 if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) { 610 ntfs_log_debug("HDIO_GETGEO heads = %u (0x%x)\n", 611 (unsigned)geo.heads, 612 (unsigned)geo.heads); 613 return geo.heads; 614 } 615 } 616 #else 617 errno = EOPNOTSUPP; 618 #endif 619 return -1; 620 } 621 622 /** 623 * ntfs_device_sectors_per_track_get - get number of sectors per track of device 624 * @dev: open device 625 * 626 * On success, return the number of sectors per track on the device @dev. On 627 * error return -1 with errno set to the error code. 628 * 629 * The following error codes are defined: 630 * EINVAL Input parameter error 631 * EOPNOTSUPP System does not support HDIO_GETGEO ioctl 632 * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO 633 */ 634 int ntfs_device_sectors_per_track_get(struct ntfs_device *dev) 635 { 636 if (!dev) { 637 errno = EINVAL; 638 return -1; 639 } 640 #ifdef HDIO_GETGEO 641 { struct hd_geometry geo; 642 643 if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) { 644 ntfs_log_debug("HDIO_GETGEO sectors_per_track = %u (0x%x)\n", 645 (unsigned)geo.sectors, 646 (unsigned)geo.sectors); 647 return geo.sectors; 648 } 649 } 650 #else 651 errno = EOPNOTSUPP; 652 #endif 653 return -1; 654 } 655 656 /** 657 * ntfs_device_sector_size_get - get sector size of a device 658 * @dev: open device 659 * 660 * On success, return the sector size in bytes of the device @dev. 661 * On error return -1 with errno set to the error code. 662 * 663 * The following error codes are defined: 664 * EINVAL Input parameter error 665 * EOPNOTSUPP System does not support BLKSSZGET ioctl 666 * ENOTTY @dev is a file or a device not supporting BLKSSZGET 667 */ 668 int ntfs_device_sector_size_get(struct ntfs_device *dev) 669 { 670 if (!dev) { 671 errno = EINVAL; 672 return -1; 673 } 674 #ifdef BLKSSZGET 675 { 676 int sect_size = 0; 677 678 if (!dev->d_ops->ioctl(dev, BLKSSZGET, §_size)) { 679 ntfs_log_debug("BLKSSZGET sector size = %d bytes\n", 680 sect_size); 681 return sect_size; 682 } 683 } 684 #else 685 errno = EOPNOTSUPP; 686 #endif 687 return -1; 688 } 689 690 /** 691 * ntfs_device_block_size_set - set block size of a device 692 * @dev: open device 693 * @block_size: block size to set @dev to 694 * 695 * On success, return 0. 696 * On error return -1 with errno set to the error code. 697 * 698 * The following error codes are defined: 699 * EINVAL Input parameter error 700 * EOPNOTSUPP System does not support BLKBSZSET ioctl 701 * ENOTTY @dev is a file or a device not supporting BLKBSZSET 702 */ 703 int ntfs_device_block_size_set(struct ntfs_device *dev, 704 int block_size __attribute__((unused))) 705 { 706 if (!dev) { 707 errno = EINVAL; 708 return -1; 709 } 710 #ifdef BLKBSZSET 711 { 712 size_t s_block_size = block_size; 713 if (!dev->d_ops->ioctl(dev, BLKBSZSET, &s_block_size)) { 714 ntfs_log_debug("Used BLKBSZSET to set block size to " 715 "%d bytes.\n", block_size); 716 return 0; 717 } 718 /* If not a block device, pretend it was successful. */ 719 if (!NDevBlock(dev)) 720 return 0; 721 } 722 #else 723 /* If not a block device, pretend it was successful. */ 724 if (!NDevBlock(dev)) 725 return 0; 726 errno = EOPNOTSUPP; 727 #endif 728 return -1; 729 } 730