forked from dhinesh77/Team-Win-Recovery-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopdev.c
1559 lines (1297 loc) · 32.7 KB
/
loopdev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* No copyright is claimed. This code is in the public domain; do with
* it what you wish.
*
* Written by Karel Zak <[email protected]>
*
* -- based on mount/losetup.c
*
* Simple library for work with loop devices.
*
* - requires kernel 2.6.x
* - reads info from /sys/block/loop<N>/loop/<attr> (new kernels)
* - reads info by ioctl
* - supports *unlimited* number of loop devices
* - supports /dev/loop<N> as well as /dev/loop/<N>
* - minimize overhead (fd, loopinfo, ... are shared for all operations)
* - setup (associate device and backing file)
* - delete (dis-associate file)
* - old LOOP_{SET,GET}_STATUS (32bit) ioctls are unsupported
* - extendible
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/sysmacros.h>
#include <inttypes.h>
#include <dirent.h>
#include <linux/posix_types.h>
#include "linux_version.h"
#include "c.h"
#include "sysfs.h"
#include "pathnames.h"
#include "loopdev.h"
#include "canonicalize.h"
#include "at.h"
#define CONFIG_LOOPDEV_DEBUG
#ifdef CONFIG_LOOPDEV_DEBUG
# include <stdarg.h>
# define DBG(l,x) do { \
if ((l)->debug) {\
fprintf(stderr, "loopdev: [%p]: ", (l)); \
x; \
} \
} while(0)
static inline void __attribute__ ((__format__ (__printf__, 1, 2)))
loopdev_debug(const char *mesg, ...)
{
va_list ap;
va_start(ap, mesg);
vfprintf(stderr, mesg, ap);
va_end(ap);
fputc('\n', stderr);
}
#else /* !CONFIG_LOOPDEV_DEBUG */
# define DBG(m,x) do { ; } while(0)
#endif
/*
* see loopcxt_init()
*/
#define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL))
#define loopcxt_sysfs_available(_lc) (!((_lc)->flags & LOOPDEV_FL_NOSYSFS)) \
&& !loopcxt_ioctl_enabled(_lc)
/*
* @lc: context
* @device: device name, absolute device path or NULL to reset the current setting
*
* Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device
* names ("loop<N>") are converted to the path (/dev/loop<N> or to
* /dev/loop/<N>)
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_set_device(struct loopdev_cxt *lc, const char *device)
{
if (!lc)
return -EINVAL;
if (lc->fd >= 0) {
close(lc->fd);
DBG(lc, loopdev_debug("closing old open fd"));
}
lc->fd = -1;
lc->mode = 0;
lc->has_info = 0;
lc->info_failed = 0;
*lc->device = '\0';
memset(&lc->info, 0, sizeof(lc->info));
/* set new */
if (device) {
if (*device != '/') {
const char *dir = _PATH_DEV;
/* compose device name for /dev/loop<n> or /dev/loop/<n> */
if (lc->flags & LOOPDEV_FL_DEVSUBDIR) {
if (strlen(device) < 5)
return -1;
device += 4;
dir = _PATH_DEV_LOOP "/"; /* _PATH_DEV uses tailing slash */
}
snprintf(lc->device, sizeof(lc->device), "%s%s",
dir, device);
} else {
strncpy(lc->device, device, sizeof(lc->device));
lc->device[sizeof(lc->device) - 1] = '\0';
}
DBG(lc, loopdev_debug("%s successfully assigned", device));
}
sysfs_deinit(&lc->sysfs);
return 0;
}
int loopcxt_has_device(struct loopdev_cxt *lc)
{
return lc && *lc->device;
}
/*
* @lc: context
* @flags: LOOPDEV_FL_* flags
*
* Initilize loop handler.
*
* We have two sets of the flags:
*
* * LOOPDEV_FL_* flags control loopcxt_* API behavior
*
* * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
*
* Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2)
* syscall to open loop device. By default is the device open read-only.
*
* The expection is loopcxt_setup_device(), where the device is open read-write
* if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()).
*
* Returns: <0 on error, 0 on success.
*/
int loopcxt_init(struct loopdev_cxt *lc, int flags)
{
int rc;
struct stat st;
struct loopdev_cxt dummy = UL_LOOPDEVCXT_EMPTY;
if (!lc)
return -EINVAL;
memcpy(lc, &dummy, sizeof(dummy));
lc->flags = flags;
if (getenv("LOOPDEV_DEBUG"))
loopcxt_enable_debug(lc, TRUE);
rc = loopcxt_set_device(lc, NULL);
if (rc)
return rc;
if (stat(_PATH_SYS_BLOCK, &st) || !S_ISDIR(st.st_mode)) {
lc->flags |= LOOPDEV_FL_NOSYSFS;
lc->flags &= ~LOOPDEV_FL_NOIOCTL;
DBG(lc, loopdev_debug("init: disable /sys usage"));
}
if (!(lc->flags & LOOPDEV_FL_NOSYSFS) &&
get_linux_version() >= KERNEL_VERSION(2,6,37)) {
/*
* Use only sysfs for basic information about loop devices
*/
lc->flags |= LOOPDEV_FL_NOIOCTL;
DBG(lc, loopdev_debug("init: ignore ioctls"));
}
if (!(lc->flags & LOOPDEV_FL_CONTROL) && !stat(_PATH_DEV_LOOPCTL, &st)) {
lc->flags |= LOOPDEV_FL_CONTROL;
DBG(lc, loopdev_debug("init: loop-control detected "));
}
return 0;
}
/*
* @lc: context
*
* Deinitialize loop context
*/
void loopcxt_deinit(struct loopdev_cxt *lc)
{
int errsv = errno;
if (!lc)
return;
DBG(lc, loopdev_debug("de-initialize"));
free(lc->filename);
lc->filename = NULL;
ignore_result( loopcxt_set_device(lc, NULL) );
loopcxt_deinit_iterator(lc);
errno = errsv;
}
/*
* @lc: context
* @enable: TRUE/FALSE
*
* Enabled/disables debug messages
*/
void loopcxt_enable_debug(struct loopdev_cxt *lc, int enable)
{
if (lc)
lc->debug = enable ? 1 : 0;
}
/*
* @lc: context
*
* Returns newly allocated device path.
*/
char *loopcxt_strdup_device(struct loopdev_cxt *lc)
{
if (!lc || !lc->device || !*lc->device)
return NULL;
return strdup(lc->device);
}
/*
* @lc: context
*
* Returns pointer device name in the @lc struct.
*/
const char *loopcxt_get_device(struct loopdev_cxt *lc)
{
return lc && lc->device && *lc->device ? lc->device : NULL;
}
/*
* @lc: context
*
* Returns pointer to the sysfs context (see lib/sysfs.c)
*/
struct sysfs_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)
{
if (!lc || !*lc->device || (lc->flags & LOOPDEV_FL_NOSYSFS))
return NULL;
if (!lc->sysfs.devno) {
dev_t devno = sysfs_devname_to_devno(lc->device, NULL);
if (!devno) {
DBG(lc, loopdev_debug("sysfs: failed devname to devno"));
return NULL;
}
if (sysfs_init(&lc->sysfs, devno, NULL)) {
DBG(lc, loopdev_debug("sysfs: init failed"));
return NULL;
}
}
return &lc->sysfs;
}
/*
* @lc: context
*
* Returns: file descriptor to the open loop device or <0 on error. The mode
* depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is
* read-only.
*/
int loopcxt_get_fd(struct loopdev_cxt *lc)
{
if (!lc || !*lc->device)
return -EINVAL;
if (lc->fd < 0) {
lc->mode = lc->flags & LOOPDEV_FL_RDWR ? O_RDWR : O_RDONLY;
lc->fd = open(lc->device, lc->mode);
DBG(lc, loopdev_debug("open %s [%s]: %s", lc->device,
lc->flags & LOOPDEV_FL_RDWR ? "rw" : "ro",
lc->fd < 0 ? "failed" : "ok"));
}
return lc->fd;
}
int loopcxt_set_fd(struct loopdev_cxt *lc, int fd, int mode)
{
if (!lc)
return -EINVAL;
lc->fd = fd;
lc->mode = mode;
return 0;
}
/*
* @lc: context
* @flags: LOOPITER_FL_* flags
*
* Iterator allows to scan list of the free or used loop devices.
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags)
{
struct loopdev_iter *iter;
struct stat st;
if (!lc)
return -EINVAL;
DBG(lc, loopdev_debug("iter: initialize"));
iter = &lc->iter;
/* always zeroize
*/
memset(iter, 0, sizeof(*iter));
iter->ncur = -1;
iter->flags = flags;
iter->default_check = 1;
if (!lc->extra_check) {
/*
* Check for /dev/loop/<N> subdirectory
*/
if (!(lc->flags & LOOPDEV_FL_DEVSUBDIR) &&
stat(_PATH_DEV_LOOP, &st) == 0 && S_ISDIR(st.st_mode))
lc->flags |= LOOPDEV_FL_DEVSUBDIR;
lc->extra_check = 1;
}
return 0;
}
/*
* @lc: context
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_deinit_iterator(struct loopdev_cxt *lc)
{
struct loopdev_iter *iter;
if (!lc)
return -EINVAL;
DBG(lc, loopdev_debug("iter: de-initialize"));
iter = &lc->iter;
free(iter->minors);
if (iter->proc)
fclose(iter->proc);
if (iter->sysblock)
closedir(iter->sysblock);
iter->minors = NULL;
iter->proc = NULL;
iter->sysblock = NULL;
iter->done = 1;
return 0;
}
/*
* Same as loopcxt_set_device, but also checks if the device is
* associeted with any file.
*
* Returns: <0 on error, 0 on success, 1 device does not match with
* LOOPITER_FL_{USED,FREE} flags.
*/
static int loopiter_set_device(struct loopdev_cxt *lc, const char *device)
{
int rc = loopcxt_set_device(lc, device);
int used;
if (rc)
return rc;
if (!(lc->iter.flags & LOOPITER_FL_USED) &&
!(lc->iter.flags & LOOPITER_FL_FREE))
return 0; /* caller does not care about device status */
used = loopcxt_get_offset(lc, NULL) == 0;
if ((lc->iter.flags & LOOPITER_FL_USED) && used)
return 0;
if ((lc->iter.flags & LOOPITER_FL_FREE) && !used)
return 0;
DBG(lc, loopdev_debug("iter: unset device"));
ignore_result( loopcxt_set_device(lc, NULL) );
return 1;
}
static int cmpnum(const void *p1, const void *p2)
{
return (((* (int *) p1) > (* (int *) p2)) -
((* (int *) p1) < (* (int *) p2)));
}
/*
* The classic scandir() is more expensive and less portable.
* We needn't full loop device names -- loop numbers (loop<N>)
* are enough.
*/
static int loop_scandir(const char *dirname, int **ary, int hasprefix)
{
DIR *dir;
struct dirent *d;
unsigned int n, count = 0, arylen = 0;
if (!dirname || !ary)
return 0;
dir = opendir(dirname);
if (!dir)
return 0;
free(*ary);
*ary = NULL;
while((d = readdir(dir))) {
#ifdef _DIRENT_HAVE_D_TYPE
if (d->d_type != DT_BLK && d->d_type != DT_UNKNOWN &&
d->d_type != DT_LNK)
continue;
#endif
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
if (hasprefix) {
/* /dev/loop<N> */
if (sscanf(d->d_name, "loop%u", &n) != 1)
continue;
} else {
/* /dev/loop/<N> */
char *end = NULL;
n = strtol(d->d_name, &end, 10);
if (d->d_name == end || (end && *end) || errno)
continue;
}
if (n < LOOPDEV_DEFAULT_NNODES)
continue; /* ignore loop<0..7> */
if (count + 1 > arylen) {
int *tmp;
arylen += 1;
tmp = realloc(*ary, arylen * sizeof(int));
if (!tmp) {
free(*ary);
closedir(dir);
return -1;
}
*ary = tmp;
}
if (*ary)
(*ary)[count++] = n;
}
if (count && *ary)
qsort(*ary, count, sizeof(int), cmpnum);
closedir(dir);
return count;
}
/*
* Set the next *used* loop device according to /proc/partitions.
*
* Loop devices smaller than 512 bytes are invisible for this function.
*/
static int loopcxt_next_from_proc(struct loopdev_cxt *lc)
{
struct loopdev_iter *iter = &lc->iter;
char buf[BUFSIZ];
DBG(lc, loopdev_debug("iter: scan /proc/partitions"));
if (!iter->proc)
iter->proc = fopen(_PATH_PROC_PARTITIONS, "r");
if (!iter->proc)
return 1;
while (fgets(buf, sizeof(buf), iter->proc)) {
unsigned int m;
char name[128 + 1];
if (sscanf(buf, " %u %*s %*s %128[^\n ]",
&m, name) != 2 || m != LOOPDEV_MAJOR)
continue;
DBG(lc, loopdev_debug("iter: check %s", name));
if (loopiter_set_device(lc, name) == 0)
return 0;
}
return 1;
}
/*
* Set the next *used* loop device according to
* /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
*
* This is preferred method.
*/
static int loopcxt_next_from_sysfs(struct loopdev_cxt *lc)
{
struct loopdev_iter *iter = &lc->iter;
struct dirent *d;
int fd;
DBG(lc, loopdev_debug("iter: scan /sys/block"));
if (!iter->sysblock)
iter->sysblock = opendir(_PATH_SYS_BLOCK);
if (!iter->sysblock)
return 1;
fd = dirfd(iter->sysblock);
while ((d = readdir(iter->sysblock))) {
char name[256];
struct stat st;
DBG(lc, loopdev_debug("iter: check %s", d->d_name));
if (strcmp(d->d_name, ".") == 0
|| strcmp(d->d_name, "..") == 0
|| strncmp(d->d_name, "loop", 4) != 0)
continue;
snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name);
if (fstat_at(fd, _PATH_SYS_BLOCK, name, &st, 0) != 0)
continue;
if (loopiter_set_device(lc, d->d_name) == 0)
return 0;
}
return 1;
}
/*
* @lc: context, has to initialized by loopcxt_init_iterator()
*
* Returns: 0 on success, -1 on error, 1 at the end of scanning. The details
* about the current loop device are available by
* loopcxt_get_{fd,backing_file,device,offset, ...} functions.
*/
int loopcxt_next(struct loopdev_cxt *lc)
{
struct loopdev_iter *iter;
if (!lc)
return -EINVAL;
DBG(lc, loopdev_debug("iter: next"));
iter = &lc->iter;
if (iter->done)
return 1;
/* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
*/
if (iter->flags & LOOPITER_FL_USED) {
int rc;
if (loopcxt_sysfs_available(lc))
rc = loopcxt_next_from_sysfs(lc);
else
rc = loopcxt_next_from_proc(lc);
if (rc == 0)
return 0;
goto done;
}
/* B) Classic way, try first eight loop devices (default number
* of loop devices). This is enough for 99% of all cases.
*/
if (iter->default_check) {
DBG(lc, loopdev_debug("iter: next: default check"));
for (++iter->ncur; iter->ncur < LOOPDEV_DEFAULT_NNODES;
iter->ncur++) {
char name[16];
snprintf(name, sizeof(name), "loop%d", iter->ncur);
if (loopiter_set_device(lc, name) == 0)
return 0;
}
iter->default_check = 0;
}
/* C) the worst possibility, scan whole /dev or /dev/loop/<N>
*/
if (!iter->minors) {
DBG(lc, loopdev_debug("iter: next: scan /dev"));
iter->nminors = (lc->flags & LOOPDEV_FL_DEVSUBDIR) ?
loop_scandir(_PATH_DEV_LOOP, &iter->minors, 0) :
loop_scandir(_PATH_DEV, &iter->minors, 1);
iter->ncur = -1;
}
for (++iter->ncur; iter->ncur < iter->nminors; iter->ncur++) {
char name[16];
snprintf(name, sizeof(name), "loop%d", iter->minors[iter->ncur]);
if (loopiter_set_device(lc, name) == 0)
return 0;
}
done:
loopcxt_deinit_iterator(lc);
return 1;
}
/*
* @device: path to device
*/
int is_loopdev(const char *device)
{
struct stat st;
if (!device)
return 0;
return (stat(device, &st) == 0 &&
S_ISBLK(st.st_mode) &&
major(st.st_rdev) == LOOPDEV_MAJOR);
}
/*
* @lc: context
*
* Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
*/
struct loop_info64 *loopcxt_get_info(struct loopdev_cxt *lc)
{
int fd;
if (!lc || lc->info_failed)
return NULL;
if (lc->has_info)
return &lc->info;
fd = loopcxt_get_fd(lc);
if (fd < 0)
return NULL;
if (ioctl(fd, LOOP_GET_STATUS64, &lc->info) == 0) {
lc->has_info = 1;
lc->info_failed = 0;
DBG(lc, loopdev_debug("reading loop_info64 OK"));
return &lc->info;
} else {
lc->info_failed = 1;
DBG(lc, loopdev_debug("reading loop_info64 FAILED"));
}
return NULL;
}
/*
* @lc: context
*
* Returns (allocated) string with path to the file assicieted
* with the current loop device.
*/
char *loopcxt_get_backing_file(struct loopdev_cxt *lc)
{
struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
char *res = NULL;
if (sysfs)
/*
* This is always preffered, the loop_info64
* has too small buffer for the filename.
*/
res = sysfs_strdup(sysfs, "loop/backing_file");
if (!res && loopcxt_ioctl_enabled(lc)) {
struct loop_info64 *lo = loopcxt_get_info(lc);
if (lo) {
lo->lo_file_name[LO_NAME_SIZE - 2] = '*';
lo->lo_file_name[LO_NAME_SIZE - 1] = '\0';
res = strdup((char *) lo->lo_file_name);
}
}
DBG(lc, loopdev_debug("get_backing_file [%s]", res));
return res;
}
/*
* @lc: context
* @offset: returns offset number for the given device
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset)
{
struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
int rc = -EINVAL;
if (sysfs)
rc = sysfs_read_u64(sysfs, "loop/offset", offset);
if (rc && loopcxt_ioctl_enabled(lc)) {
struct loop_info64 *lo = loopcxt_get_info(lc);
if (lo) {
if (offset)
*offset = lo->lo_offset;
rc = 0;
}
}
DBG(lc, loopdev_debug("get_offset [rc=%d]", rc));
return rc;
}
/*
* @lc: context
* @sizelimit: returns size limit for the given device
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size)
{
struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
int rc = -EINVAL;
if (sysfs)
rc = sysfs_read_u64(sysfs, "loop/sizelimit", size);
if (rc && loopcxt_ioctl_enabled(lc)) {
struct loop_info64 *lo = loopcxt_get_info(lc);
if (lo) {
if (size)
*size = lo->lo_sizelimit;
rc = 0;
}
}
DBG(lc, loopdev_debug("get_sizelimit [rc=%d]", rc));
return rc;
}
/*
* @lc: context
* @devno: returns encryption type
*
* Cryptoloop is DEPRECATED!
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_get_encrypt_type(struct loopdev_cxt *lc, uint32_t *type)
{
struct loop_info64 *lo = loopcxt_get_info(lc);
int rc = -EINVAL;
if (lo) {
if (type)
*type = lo->lo_encrypt_type;
rc = 0;
}
DBG(lc, loopdev_debug("get_encrypt_type [rc=%d]", rc));
return rc;
}
/*
* @lc: context
* @devno: returns crypt name
*
* Cryptoloop is DEPRECATED!
*
* Returns: <0 on error, 0 on success
*/
const char *loopcxt_get_crypt_name(struct loopdev_cxt *lc)
{
struct loop_info64 *lo = loopcxt_get_info(lc);
if (lo)
return (char *) lo->lo_crypt_name;
DBG(lc, loopdev_debug("get_crypt_name failed"));
return NULL;
}
/*
* @lc: context
* @devno: returns backing file devno
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_get_backing_devno(struct loopdev_cxt *lc, dev_t *devno)
{
struct loop_info64 *lo = loopcxt_get_info(lc);
int rc = -EINVAL;
if (lo) {
if (devno)
*devno = lo->lo_device;
rc = 0;
}
DBG(lc, loopdev_debug("get_backing_devno [rc=%d]", rc));
return rc;
}
/*
* @lc: context
* @ino: returns backing file inode
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_get_backing_inode(struct loopdev_cxt *lc, ino_t *ino)
{
struct loop_info64 *lo = loopcxt_get_info(lc);
int rc = -EINVAL;
if (lo) {
if (ino)
*ino = lo->lo_inode;
rc = 0;
}
DBG(lc, loopdev_debug("get_backing_inode [rc=%d]", rc));
return rc;
}
/*
* Check if the kernel supports partitioned loop devices.
*
* Notes:
* - kernels < 3.2 support partitioned loop devices and PT scanning
* only if max_part= module paremeter is non-zero
*
* - kernels >= 3.2 always support partitioned loop devices
*
* - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
*
* - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
* LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
* by default.
*
* See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
*/
int loopmod_supports_partscan(void)
{
int rc, ret = 0;
FILE *f;
if (get_linux_version() >= KERNEL_VERSION(3,2,0))
return 1;
f = fopen("/sys/module/loop/parameters/max_part", "r");
if (!f)
return 0;
rc = fscanf(f, "%d", &ret);
fclose(f);
return rc == 1 ? ret : 0;
}
/*
* @lc: context
*
* Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
* scannig is enabled for all loop devices.
*/
int loopcxt_is_partscan(struct loopdev_cxt *lc)
{
struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
if (sysfs) {
/* kernel >= 3.2 */
int fl;
if (sysfs_read_int(sysfs, "loop/partscan", &fl) == 0)
return fl;
}
/* old kernels (including kernels without loopN/loop/<flags> directory */
return loopmod_supports_partscan();
}
/*
* @lc: context
*
* Returns: 1 if the autoclear flags is set.
*/
int loopcxt_is_autoclear(struct loopdev_cxt *lc)
{
struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
if (sysfs) {
int fl;
if (sysfs_read_int(sysfs, "loop/autoclear", &fl) == 0)
return fl;
}
if (loopcxt_ioctl_enabled(lc)) {
struct loop_info64 *lo = loopcxt_get_info(lc);
if (lo)
return lo->lo_flags & LO_FLAGS_AUTOCLEAR;
}
return 0;
}
/*
* @lc: context
*
* Returns: 1 if the readonly flags is set.
*/
int loopcxt_is_readonly(struct loopdev_cxt *lc)
{
struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
if (sysfs) {
int fl;
if (sysfs_read_int(sysfs, "ro", &fl) == 0)
return fl;
}
if (loopcxt_ioctl_enabled(lc)) {
struct loop_info64 *lo = loopcxt_get_info(lc);
if (lo)
return lo->lo_flags & LO_FLAGS_READ_ONLY;
}
return 0;
}
/*
* @lc: context
* @st: backing file stat or NULL
* @backing_file: filename
* @offset: offset
* @flags: LOOPDEV_FL_OFFSET if @offset should not be ignored
*
* Returns 1 if the current @lc loopdev is associated with the given backing
* file. Note that the preferred way is to use devno and inode number rather
* than filename. The @backing_file filename is poor solution usable in case
* that you don't have rights to call stat().
*
* Don't forget that old kernels provide very restricted (in size) backing
* filename by LOOP_GET_STAT64 ioctl only.
*/
int loopcxt_is_used(struct loopdev_cxt *lc,
struct stat *st,
const char *backing_file,
uint64_t offset,
int flags)
{
ino_t ino;
dev_t dev;
if (!lc)
return 0;
DBG(lc, loopdev_debug("checking %s vs. %s",
loopcxt_get_device(lc),
backing_file));
if (st && loopcxt_get_backing_inode(lc, &ino) == 0 &&
loopcxt_get_backing_devno(lc, &dev) == 0) {
if (ino == st->st_ino && dev == st->st_dev)
goto found;
/* don't use filename if we have devno and inode */
return 0;
}
/* poor man's solution */
if (backing_file) {
char *name = loopcxt_get_backing_file(lc);
int rc = name && strcmp(name, backing_file) == 0;
free(name);
if (rc)
goto found;
}
return 0;
found:
if (flags & LOOPDEV_FL_OFFSET) {
uint64_t off;