Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / ntfs-3g / libntfs-3g / device.c
blob274abacb4244c2a58c0c079959a37948bcb37905
1 /**
2 * device.c - Low level device io functions. Originated from the Linux-NTFS project.
4 * Copyright (c) 2004-2006 Anton Altaparmakov
5 * Copyright (c) 2004-2006 Szabolcs Szakacsits
6 * Copyright (c) 2010 Jean-Pierre Andre
8 * This program/include file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program/include file is distributed in the hope that it will be
14 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program (in the main directory of the NTFS-3G
20 * distribution in the file COPYING); if not, write to the Free Software
21 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37 #ifdef HAVE_ERRNO_H
38 #include <errno.h>
39 #endif
40 #ifdef HAVE_STDIO_H
41 #include <stdio.h>
42 #endif
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
45 #endif
46 #ifdef HAVE_SYS_STAT_H
47 #include <sys/stat.h>
48 #endif
49 #ifdef HAVE_FCNTL_H
50 #include <fcntl.h>
51 #endif
52 #ifdef HAVE_SYS_IOCTL_H
53 #include <sys/ioctl.h>
54 #endif
55 #ifdef HAVE_SYS_PARAM_H
56 #include <sys/param.h>
57 #endif
58 #ifdef HAVE_SYS_MOUNT_H
59 #include <sys/mount.h>
60 #endif
61 #ifdef HAVE_SYS_DISK_H
62 #include <sys/disk.h>
63 #endif
64 #ifdef HAVE_LINUX_FD_H
65 #include <linux/fd.h>
66 #endif
67 #ifdef HAVE_LINUX_HDREG_H
68 #include <linux/hdreg.h>
69 #endif
71 #include "types.h"
72 #include "mst.h"
73 #include "debug.h"
74 #include "device.h"
75 #include "logging.h"
76 #include "misc.h"
78 #if defined(linux) && defined(_IO) && !defined(BLKGETSIZE)
79 #define BLKGETSIZE _IO(0x12,96) /* Get device size in 512-byte blocks. */
80 #endif
81 #if defined(linux) && defined(_IOR) && !defined(BLKGETSIZE64)
82 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* Get device size in bytes. */
83 #endif
84 #if defined(linux) && !defined(HDIO_GETGEO)
85 #define HDIO_GETGEO 0x0301 /* Get device geometry. */
86 #endif
87 #if defined(linux) && defined(_IO) && !defined(BLKSSZGET)
88 # define BLKSSZGET _IO(0x12,104) /* Get device sector size in bytes. */
89 #endif
90 #if defined(linux) && defined(_IO) && !defined(BLKBSZSET)
91 # define BLKBSZSET _IOW(0x12,113,size_t) /* Set device block size in bytes. */
92 #endif
94 /**
95 * ntfs_device_alloc - allocate an ntfs device structure and pre-initialize it
96 * @name: name of the device (must be present)
97 * @state: initial device state (usually zero)
98 * @dops: ntfs device operations to use with the device (must be present)
99 * @priv_data: pointer to private data (optional)
101 * Allocate an ntfs device structure and pre-initialize it with the user-
102 * specified device operations @dops, device state @state, device name @name,
103 * and optional private data @priv_data.
105 * Note, @name is copied and can hence be freed after this functions returns.
107 * On success return a pointer to the allocated ntfs device structure and on
108 * error return NULL with errno set to the error code returned by ntfs_malloc().
110 struct ntfs_device *ntfs_device_alloc(const char *name, const long state,
111 struct ntfs_device_operations *dops, void *priv_data)
113 struct ntfs_device *dev;
115 if (!name) {
116 errno = EINVAL;
117 return NULL;
120 dev = ntfs_malloc(sizeof(struct ntfs_device));
121 if (dev) {
122 if (!(dev->d_name = strdup(name))) {
123 int eo = errno;
124 free(dev);
125 errno = eo;
126 return NULL;
128 dev->d_ops = dops;
129 dev->d_state = state;
130 dev->d_private = priv_data;
132 return dev;
136 * ntfs_device_free - free an ntfs device structure
137 * @dev: ntfs device structure to free
139 * Free the ntfs device structure @dev.
141 * Return 0 on success or -1 on error with errno set to the error code. The
142 * following error codes are defined:
143 * EINVAL Invalid pointer @dev.
144 * EBUSY Device is still open. Close it before freeing it!
146 int ntfs_device_free(struct ntfs_device *dev)
148 if (!dev) {
149 errno = EINVAL;
150 return -1;
152 if (NDevOpen(dev)) {
153 errno = EBUSY;
154 return -1;
156 free(dev->d_name);
157 free(dev);
158 return 0;
162 * Sync the device
164 * returns zero if successful.
167 int ntfs_device_sync(struct ntfs_device *dev)
169 int ret;
170 struct ntfs_device_operations *dops;
172 if (NDevDirty(dev)) {
173 dops = dev->d_ops;
174 ret = dops->sync(dev);
175 } else
176 ret = 0;
177 return ret;
181 * ntfs_pread - positioned read from disk
182 * @dev: device to read from
183 * @pos: position in device to read from
184 * @count: number of bytes to read
185 * @b: output data buffer
187 * This function will read @count bytes from device @dev at position @pos into
188 * the data buffer @b.
190 * On success, return the number of successfully read bytes. If this number is
191 * lower than @count this means that we have either reached end of file or
192 * encountered an error during the read so that the read is partial. 0 means
193 * end of file or nothing to read (@count is 0).
195 * On error and nothing has been read, return -1 with errno set appropriately
196 * to the return code of either seek, read, or set to EINVAL in case of
197 * invalid arguments.
199 s64 ntfs_pread(struct ntfs_device *dev, const s64 pos, s64 count, void *b)
201 s64 br, total;
202 struct ntfs_device_operations *dops;
204 ntfs_log_trace("pos %lld, count %lld\n",(long long)pos,(long long)count);
206 if (!b || count < 0 || pos < 0) {
207 errno = EINVAL;
208 return -1;
210 if (!count)
211 return 0;
213 dops = dev->d_ops;
215 for (total = 0; count; count -= br, total += br) {
216 br = dops->pread(dev, (char*)b + total, count, pos + total);
217 /* If everything ok, continue. */
218 if (br > 0)
219 continue;
220 /* If EOF or error return number of bytes read. */
221 if (!br || total)
222 return total;
223 /* Nothing read and error, return error status. */
224 return br;
226 /* Finally, return the number of bytes read. */
227 return total;
231 * ntfs_pwrite - positioned write to disk
232 * @dev: device to write to
233 * @pos: position in file descriptor to write to
234 * @count: number of bytes to write
235 * @b: data buffer to write to disk
237 * This function will write @count bytes from data buffer @b to the device @dev
238 * at position @pos.
240 * On success, return the number of successfully written bytes. If this number
241 * is lower than @count this means that the write has been interrupted in
242 * flight or that an error was encountered during the write so that the write
243 * is partial. 0 means nothing was written (also return 0 when @count is 0).
245 * On error and nothing has been written, return -1 with errno set
246 * appropriately to the return code of either seek, write, or set
247 * to EINVAL in case of invalid arguments.
249 s64 ntfs_pwrite(struct ntfs_device *dev, const s64 pos, s64 count,
250 const void *b)
252 s64 written, total, ret = -1;
253 struct ntfs_device_operations *dops;
255 ntfs_log_trace("pos %lld, count %lld\n",(long long)pos,(long long)count);
257 if (!b || count < 0 || pos < 0) {
258 errno = EINVAL;
259 goto out;
261 if (!count)
262 return 0;
263 if (NDevReadOnly(dev)) {
264 errno = EROFS;
265 goto out;
268 dops = dev->d_ops;
270 NDevSetDirty(dev);
271 for (total = 0; count; count -= written, total += written) {
272 written = dops->pwrite(dev, (const char*)b + total, count,
273 pos + total);
274 /* If everything ok, continue. */
275 if (written > 0)
276 continue;
278 * If nothing written or error return number of bytes written.
280 if (!written || total)
281 break;
282 /* Nothing written and error, return error status. */
283 total = written;
284 break;
286 if (NDevSync(dev) && total && dops->sync(dev)) {
287 total--; /* on sync error, return partially written */
289 ret = total;
290 out:
291 return ret;
295 * ntfs_mst_pread - multi sector transfer (mst) positioned read
296 * @dev: device to read from
297 * @pos: position in file descriptor to read from
298 * @count: number of blocks to read
299 * @bksize: size of each block that needs mst deprotecting
300 * @b: output data buffer
302 * Multi sector transfer (mst) positioned read. This function will read @count
303 * blocks of size @bksize bytes each from device @dev at position @pos into the
304 * the data buffer @b.
306 * On success, return the number of successfully read blocks. If this number is
307 * lower than @count this means that we have reached end of file, that the read
308 * was interrupted, or that an error was encountered during the read so that
309 * the read is partial. 0 means end of file or nothing was read (also return 0
310 * when @count or @bksize are 0).
312 * On error and nothing was read, return -1 with errno set appropriately to the
313 * return code of either seek, read, or set to EINVAL in case of invalid
314 * arguments.
316 * NOTE: If an incomplete multi sector transfer has been detected the magic
317 * will have been changed to magic_BAAD but no error will be returned. Thus it
318 * is possible that we return count blocks as being read but that any number
319 * (between zero and count!) of these blocks is actually subject to a multi
320 * sector transfer error. This should be detected by the caller by checking for
321 * the magic being "BAAD".
323 s64 ntfs_mst_pread(struct ntfs_device *dev, const s64 pos, s64 count,
324 const u32 bksize, void *b)
326 s64 br, i;
328 if (bksize & (bksize - 1) || bksize % NTFS_BLOCK_SIZE) {
329 errno = EINVAL;
330 return -1;
332 /* Do the read. */
333 br = ntfs_pread(dev, pos, count * bksize, b);
334 if (br < 0)
335 return br;
337 * Apply fixups to successfully read data, disregarding any errors
338 * returned from the MST fixup function. This is because we want to
339 * fixup everything possible and we rely on the fact that the "BAAD"
340 * magic will be detected later on.
342 count = br / bksize;
343 for (i = 0; i < count; ++i)
344 ntfs_mst_post_read_fixup((NTFS_RECORD*)
345 ((u8*)b + i * bksize), bksize);
346 /* Finally, return the number of complete blocks read. */
347 return count;
351 * ntfs_mst_pwrite - multi sector transfer (mst) positioned write
352 * @dev: device to write to
353 * @pos: position in file descriptor to write to
354 * @count: number of blocks to write
355 * @bksize: size of each block that needs mst protecting
356 * @b: data buffer to write to disk
358 * Multi sector transfer (mst) positioned write. This function will write
359 * @count blocks of size @bksize bytes each from data buffer @b to the device
360 * @dev at position @pos.
362 * On success, return the number of successfully written blocks. If this number
363 * is lower than @count this means that the write has been interrupted or that
364 * an error was encountered during the write so that the write is partial. 0
365 * means nothing was written (also return 0 when @count or @bksize are 0).
367 * On error and nothing has been written, return -1 with errno set
368 * appropriately to the return code of either seek, write, or set
369 * to EINVAL in case of invalid arguments.
371 * NOTE: We mst protect the data, write it, then mst deprotect it using a quick
372 * deprotect algorithm (no checking). This saves us from making a copy before
373 * the write and at the same time causes the usn to be incremented in the
374 * buffer. This conceptually fits in better with the idea that cached data is
375 * always deprotected and protection is performed when the data is actually
376 * going to hit the disk and the cache is immediately deprotected again
377 * simulating an mst read on the written data. This way cache coherency is
378 * achieved.
380 s64 ntfs_mst_pwrite(struct ntfs_device *dev, const s64 pos, s64 count,
381 const u32 bksize, void *b)
383 s64 written, i;
385 if (count < 0 || bksize % NTFS_BLOCK_SIZE) {
386 errno = EINVAL;
387 return -1;
389 if (!count)
390 return 0;
391 /* Prepare data for writing. */
392 for (i = 0; i < count; ++i) {
393 int err;
395 err = ntfs_mst_pre_write_fixup((NTFS_RECORD*)
396 ((u8*)b + i * bksize), bksize);
397 if (err < 0) {
398 /* Abort write at this position. */
399 if (!i)
400 return err;
401 count = i;
402 break;
405 /* Write the prepared data. */
406 written = ntfs_pwrite(dev, pos, count * bksize, b);
407 /* Quickly deprotect the data again. */
408 for (i = 0; i < count; ++i)
409 ntfs_mst_post_write_fixup((NTFS_RECORD*)((u8*)b + i * bksize));
410 if (written <= 0)
411 return written;
412 /* Finally, return the number of complete blocks written. */
413 return written / bksize;
417 * ntfs_cluster_read - read ntfs clusters
418 * @vol: volume to read from
419 * @lcn: starting logical cluster number
420 * @count: number of clusters to read
421 * @b: output data buffer
423 * Read @count ntfs clusters starting at logical cluster number @lcn from
424 * volume @vol into buffer @b. Return number of clusters read or -1 on error,
425 * with errno set to the error code.
427 s64 ntfs_cluster_read(const ntfs_volume *vol, const s64 lcn, const s64 count,
428 void *b)
430 s64 br;
432 if (!vol || lcn < 0 || count < 0) {
433 errno = EINVAL;
434 return -1;
436 if (vol->nr_clusters < lcn + count) {
437 errno = ESPIPE;
438 ntfs_log_perror("Trying to read outside of volume "
439 "(%lld < %lld)", (long long)vol->nr_clusters,
440 (long long)lcn + count);
441 return -1;
443 br = ntfs_pread(vol->dev, lcn << vol->cluster_size_bits,
444 count << vol->cluster_size_bits, b);
445 if (br < 0) {
446 ntfs_log_perror("Error reading cluster(s)");
447 return br;
449 return br >> vol->cluster_size_bits;
453 * ntfs_cluster_write - write ntfs clusters
454 * @vol: volume to write to
455 * @lcn: starting logical cluster number
456 * @count: number of clusters to write
457 * @b: data buffer to write to disk
459 * Write @count ntfs clusters starting at logical cluster number @lcn from
460 * buffer @b to volume @vol. Return the number of clusters written or -1 on
461 * error, with errno set to the error code.
463 s64 ntfs_cluster_write(const ntfs_volume *vol, const s64 lcn,
464 const s64 count, const void *b)
466 s64 bw;
468 if (!vol || lcn < 0 || count < 0) {
469 errno = EINVAL;
470 return -1;
472 if (vol->nr_clusters < lcn + count) {
473 errno = ESPIPE;
474 ntfs_log_perror("Trying to write outside of volume "
475 "(%lld < %lld)", (long long)vol->nr_clusters,
476 (long long)lcn + count);
477 return -1;
479 if (!NVolReadOnly(vol))
480 bw = ntfs_pwrite(vol->dev, lcn << vol->cluster_size_bits,
481 count << vol->cluster_size_bits, b);
482 else
483 bw = count << vol->cluster_size_bits;
484 if (bw < 0) {
485 ntfs_log_perror("Error writing cluster(s)");
486 return bw;
488 return bw >> vol->cluster_size_bits;
492 * ntfs_device_offset_valid - test if a device offset is valid
493 * @dev: open device
494 * @ofs: offset to test for validity
496 * Test if the offset @ofs is an existing location on the device described
497 * by the open device structure @dev.
499 * Return 0 if it is valid and -1 if it is not valid.
501 static int ntfs_device_offset_valid(struct ntfs_device *dev, s64 ofs)
503 char ch;
505 if (dev->d_ops->seek(dev, ofs, SEEK_SET) >= 0 &&
506 dev->d_ops->read(dev, &ch, 1) == 1)
507 return 0;
508 return -1;
512 * ntfs_device_size_get - return the size of a device in blocks
513 * @dev: open device
514 * @block_size: block size in bytes in which to return the result
516 * Return the number of @block_size sized blocks in the device described by the
517 * open device @dev.
519 * Adapted from e2fsutils-1.19, Copyright (C) 1995 Theodore Ts'o.
521 * On error return -1 with errno set to the error code.
523 s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size)
525 s64 high, low;
527 if (!dev || block_size <= 0 || (block_size - 1) & block_size) {
528 errno = EINVAL;
529 return -1;
531 #ifdef BLKGETSIZE64
532 { u64 size;
534 if (dev->d_ops->ioctl(dev, BLKGETSIZE64, &size) >= 0) {
535 ntfs_log_debug("BLKGETSIZE64 nr bytes = %llu (0x%llx)\n",
536 (unsigned long long)size,
537 (unsigned long long)size);
538 return (s64)size / block_size;
541 #endif
542 #ifdef BLKGETSIZE
543 { unsigned long size;
545 if (dev->d_ops->ioctl(dev, BLKGETSIZE, &size) >= 0) {
546 ntfs_log_debug("BLKGETSIZE nr 512 byte blocks = %lu (0x%lx)\n",
547 size, size);
548 return (s64)size * 512 / block_size;
551 #endif
552 #ifdef FDGETPRM
553 { struct floppy_struct this_floppy;
555 if (dev->d_ops->ioctl(dev, FDGETPRM, &this_floppy) >= 0) {
556 ntfs_log_debug("FDGETPRM nr 512 byte blocks = %lu (0x%lx)\n",
557 (unsigned long)this_floppy.size,
558 (unsigned long)this_floppy.size);
559 return (s64)this_floppy.size * 512 / block_size;
562 #endif
563 #ifdef DIOCGMEDIASIZE
565 /* FreeBSD */
566 off_t size;
568 if (dev->d_ops->ioctl(dev, DIOCGMEDIASIZE, &size) >= 0) {
569 ntfs_log_debug("DIOCGMEDIASIZE nr bytes = %llu (0x%llx)\n",
570 (unsigned long long)size,
571 (unsigned long long)size);
572 return (s64)size / block_size;
575 #endif
576 #ifdef DKIOCGETBLOCKCOUNT
578 /* Mac OS X */
579 uint64_t blocks;
580 int sector_size;
582 sector_size = ntfs_device_sector_size_get(dev);
583 if (sector_size >= 0 && dev->d_ops->ioctl(dev,
584 DKIOCGETBLOCKCOUNT, &blocks) >= 0)
586 ntfs_log_debug("DKIOCGETBLOCKCOUNT nr blocks = %llu (0x%llx)\n",
587 (unsigned long long) blocks,
588 (unsigned long long) blocks);
589 return blocks * sector_size / block_size;
592 #endif
594 * We couldn't figure it out by using a specialized ioctl,
595 * so do binary search to find the size of the device.
597 low = 0LL;
598 for (high = 1024LL; !ntfs_device_offset_valid(dev, high); high <<= 1)
599 low = high;
600 while (low < high - 1LL) {
601 const s64 mid = (low + high) / 2;
603 if (!ntfs_device_offset_valid(dev, mid))
604 low = mid;
605 else
606 high = mid;
608 dev->d_ops->seek(dev, 0LL, SEEK_SET);
609 return (low + 1LL) / block_size;
613 * ntfs_device_partition_start_sector_get - get starting sector of a partition
614 * @dev: open device
616 * On success, return the starting sector of the partition @dev in the parent
617 * block device of @dev. On error return -1 with errno set to the error code.
619 * The following error codes are defined:
620 * EINVAL Input parameter error
621 * EOPNOTSUPP System does not support HDIO_GETGEO ioctl
622 * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO
624 s64 ntfs_device_partition_start_sector_get(struct ntfs_device *dev)
626 if (!dev) {
627 errno = EINVAL;
628 return -1;
630 #ifdef HDIO_GETGEO
631 { struct hd_geometry geo;
633 if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) {
634 ntfs_log_debug("HDIO_GETGEO start_sect = %lu (0x%lx)\n",
635 geo.start, geo.start);
636 return geo.start;
639 #else
640 errno = EOPNOTSUPP;
641 #endif
642 return -1;
646 * ntfs_device_heads_get - get number of heads of device
647 * @dev: open device
649 * On success, return the number of heads on the device @dev. On error return
650 * -1 with errno set to the error code.
652 * The following error codes are defined:
653 * EINVAL Input parameter error
654 * EOPNOTSUPP System does not support HDIO_GETGEO ioctl
655 * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO
657 int ntfs_device_heads_get(struct ntfs_device *dev)
659 if (!dev) {
660 errno = EINVAL;
661 return -1;
663 #ifdef HDIO_GETGEO
664 { struct hd_geometry geo;
666 if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) {
667 ntfs_log_debug("HDIO_GETGEO heads = %u (0x%x)\n",
668 (unsigned)geo.heads,
669 (unsigned)geo.heads);
670 return geo.heads;
673 #else
674 errno = EOPNOTSUPP;
675 #endif
676 return -1;
680 * ntfs_device_sectors_per_track_get - get number of sectors per track of device
681 * @dev: open device
683 * On success, return the number of sectors per track on the device @dev. On
684 * error return -1 with errno set to the error code.
686 * The following error codes are defined:
687 * EINVAL Input parameter error
688 * EOPNOTSUPP System does not support HDIO_GETGEO ioctl
689 * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO
691 int ntfs_device_sectors_per_track_get(struct ntfs_device *dev)
693 if (!dev) {
694 errno = EINVAL;
695 return -1;
697 #ifdef HDIO_GETGEO
698 { struct hd_geometry geo;
700 if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) {
701 ntfs_log_debug("HDIO_GETGEO sectors_per_track = %u (0x%x)\n",
702 (unsigned)geo.sectors,
703 (unsigned)geo.sectors);
704 return geo.sectors;
707 #else
708 errno = EOPNOTSUPP;
709 #endif
710 return -1;
714 * ntfs_device_sector_size_get - get sector size of a device
715 * @dev: open device
717 * On success, return the sector size in bytes of the device @dev.
718 * On error return -1 with errno set to the error code.
720 * The following error codes are defined:
721 * EINVAL Input parameter error
722 * EOPNOTSUPP System does not support BLKSSZGET ioctl
723 * ENOTTY @dev is a file or a device not supporting BLKSSZGET
725 int ntfs_device_sector_size_get(struct ntfs_device *dev)
727 if (!dev) {
728 errno = EINVAL;
729 return -1;
731 #ifdef BLKSSZGET
733 int sect_size = 0;
735 if (!dev->d_ops->ioctl(dev, BLKSSZGET, &sect_size)) {
736 ntfs_log_debug("BLKSSZGET sector size = %d bytes\n",
737 sect_size);
738 return sect_size;
741 #elif defined(DIOCGSECTORSIZE)
743 /* FreeBSD */
744 size_t sect_size = 0;
746 if (!dev->d_ops->ioctl(dev, DIOCGSECTORSIZE, &sect_size)) {
747 ntfs_log_debug("DIOCGSECTORSIZE sector size = %d bytes\n",
748 (int) sect_size);
749 return sect_size;
752 #elif defined(DKIOCGETBLOCKSIZE)
754 /* Mac OS X */
755 uint32_t sect_size = 0;
757 if (!dev->d_ops->ioctl(dev, DKIOCGETBLOCKSIZE, &sect_size)) {
758 ntfs_log_debug("DKIOCGETBLOCKSIZE sector size = %d bytes\n",
759 (int) sect_size);
760 return sect_size;
763 #else
764 errno = EOPNOTSUPP;
765 #endif
766 return -1;
770 * ntfs_device_block_size_set - set block size of a device
771 * @dev: open device
772 * @block_size: block size to set @dev to
774 * On success, return 0.
775 * On error return -1 with errno set to the error code.
777 * The following error codes are defined:
778 * EINVAL Input parameter error
779 * EOPNOTSUPP System does not support BLKBSZSET ioctl
780 * ENOTTY @dev is a file or a device not supporting BLKBSZSET
782 int ntfs_device_block_size_set(struct ntfs_device *dev,
783 int block_size __attribute__((unused)))
785 if (!dev) {
786 errno = EINVAL;
787 return -1;
789 #ifdef BLKBSZSET
791 size_t s_block_size = block_size;
792 if (!dev->d_ops->ioctl(dev, BLKBSZSET, &s_block_size)) {
793 ntfs_log_debug("Used BLKBSZSET to set block size to "
794 "%d bytes.\n", block_size);
795 return 0;
797 /* If not a block device, pretend it was successful. */
798 if (!NDevBlock(dev))
799 return 0;
801 #else
802 /* If not a block device, pretend it was successful. */
803 if (!NDevBlock(dev))
804 return 0;
805 errno = EOPNOTSUPP;
806 #endif
807 return -1;