summaryrefslogtreecommitdiff
path: root/package/aufs2-util/src/rdu.c
blob: ac958f08425402dd22d56c3d66bbc9d30785b7bb (plain)
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
/*
 * Copyright (C) 2009 Junjiro Okajima
 *
 * This program, aufs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#define _ATFILE_SOURCE
#define _GNU_SOURCE
#define _REENTRANT

#include <linux/aufs_type.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfs.h>    /* or <sys/statfs.h> */
#include <assert.h>
#include <dirent.h>
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "compat.h"

/* ---------------------------------------------------------------------- */

struct rdu {
#ifdef AuRDU_REENTRANT
	pthread_rwlock_t lock;
#else
	struct dirent de;
#endif

	int fd;

	unsigned long npos, idx;
	struct au_rdu_ent **pos;

	unsigned long nent, sz;
	struct au_rdu_ent *ent;

	int shwh;
	struct au_rdu_ent *real, *wh;
};

static struct rdu **rdu;
#define RDU_STEP 8
static int rdu_cur, rdu_lim = RDU_STEP;

/* ---------------------------------------------------------------------- */

/* #define RduLocalTest */
#ifdef RduLocalTest
static int rdu_test_data(struct rdu *p, int err)
{
	struct au_rdu_ent *e = p->ent;
	static int i;

	if (!i++) {
		err = 3;
		e->ino = e->type = e->nlen = 1;
		strcpy(e->name, ".");
		e += au_rdu_len(e->nlen);
		e->ino = e->type = e->nlen = 2;
		strcpy(e->name, "..");
		e += au_rdu_len(e->nlen);
		e->ino = e->type = e->nlen = 3;
		strcpy(e->name, "foo");
	} else
		err = 0;

	return err;
}
#else
static int rdu_test_data(struct rdu *p, int err)
{
	return err;
}
#endif

/* #define RduDebug */
#ifdef RduDebug
#define DPri(fmt, args...)	fprintf(stderr, "%s:%d: " fmt, \
					__func__, __LINE__, ##args)
#else
#define DPri(fmt, args...)	do {} while (0)
#endif

/* ---------------------------------------------------------------------- */

#ifdef AuRDU_REENTRANT
static void rdu_rwlock_init(struct rdu *p)
{
	pthread_rwlock_init(&p->lock);
}

static void rdu_read_lock(struct rdu *p)
{
	pthread_rwlock_rdlock(&p->lock);
}

static void rdu_write_lock(struct rdu *p)
{
	pthread_rwlock_wrlock(&p->lock);
}

static void rdu_unlock(struct rdu *p)
{
	pthread_rwlock_unlock(&p->lock);
}

static pthread_mutex_t rdu_lib_mtx = PTHREAD_MUTEX_INITIALIZER;
#define rdu_lib_lock()		pthread_mutex_lock(&rdu_lib_mtx)
#define rdu_lib_unlock()	pthread_mutex_unlock(&rdu_lib_mtx)
#define rdu_lib_must_lock()	assert(pthread_mutex_trylock(&rdu_lib_mtx))
#else
static void rdu_rwlock_init(struct rdu *p)
{
	/* empty */
}

static void rdu_read_lock(struct rdu *p)
{
	/* empty */
}

static void rdu_write_lock(struct rdu *p)
{
	/* empty */
}

static void rdu_unlock(struct rdu *p)
{
	/* empty */
}

#define rdu_lib_lock()		do {} while(0)
#define rdu_lib_unlock()	do {} while(0)
#define rdu_lib_must_lock()	do {} while(0)
#endif

/*
 * initialize this library, particularly global variables.
 */
static int rdu_lib_init(void)
{
	int err;

	err = 0;
	if (rdu)
		goto out;

	rdu_lib_lock();
	if (!rdu) {
		rdu = calloc(rdu_lim, sizeof(*rdu));
		err = !rdu;
	}
	rdu_lib_unlock();

 out:
	return err;
}

static int rdu_append(struct rdu *p)
{
	int err, i;
	void *t;

	rdu_lib_must_lock();

	err = 0;
	if (rdu_cur < rdu_lim - 1)
		rdu[rdu_cur++] = p;
	else {
		t = realloc(rdu, rdu_lim + RDU_STEP * sizeof(*rdu));
		if (t) {
			rdu = t;
			rdu_lim += RDU_STEP;
			rdu[rdu_cur++] = p;
			for (i = 0; i < RDU_STEP - 1; i++)
				rdu[rdu_cur + i] = NULL;
		} else
			err = -1;
	}

	return err;
}

/* ---------------------------------------------------------------------- */

static struct rdu *rdu_new(int fd)
{
	struct rdu *p;
	int err;

	p = malloc(sizeof(*p));
	if (p) {
		rdu_rwlock_init(p);
		p->fd = fd;
		p->sz = BUFSIZ;
		p->ent = malloc(BUFSIZ);
		if (p->ent) {
			err = rdu_append(p);
			if (!err)
				goto out; /* success */
		}
	}
	free(p);
	p = NULL;

 out:
	return p;
}

static struct rdu *rdu_buf_lock(int fd)
{
	struct rdu *p;
	int i;

	assert(rdu);
	assert(fd >= 0);

	p = NULL;
	rdu_lib_lock();
	for (i = 0; i < rdu_cur; i++)
		if (rdu[i] && rdu[i]->fd == fd) {
			p = rdu[i];
			goto out;
		}

	for (i = 0; i < rdu_cur; i++)
		if (rdu[i] && rdu[i]->fd == -1) {
			p = rdu[i];
			p->fd = fd;
			goto out;
		}
	if (!p)
		p = rdu_new(fd);

 out:
	if (p)
		rdu_write_lock(p);
	rdu_lib_unlock();

	return p;
}

static void rdu_free(int fd)
{
	struct rdu *p;

	p = rdu_buf_lock(fd);
	if (p) {
		free(p->ent);
		free(p->pos);
		p->fd = -1;
		p->ent = NULL;
		p->pos = NULL;
		rdu_unlock(p);
	}
}

/* ---------------------------------------------------------------------- */

static int rdu_do_store(int dirfd, struct au_rdu_ent *ent,
			struct au_rdu_ent **pos, struct rdu *p)
{
	int err;
	unsigned char c;
	struct stat st;

	c = ent->name[ent->nlen];
	ent->name[ent->nlen] = 0;
	DPri("%s\n", ent->name);
	err = fstatat(dirfd, ent->name, &st, AT_SYMLINK_NOFOLLOW);
	ent->name[ent->nlen] = c;
	if (!err) {
		ent->ino = st.st_ino;
		pos[p->idx++] = ent;
	} else {
		DPri("err %d\n", err);
		if (errno == ENOENT)
			err = 0;
	}

	return err;
}

struct rdu_thread_arg {
	int pipefd;
	struct rdu *p;
};

static void *rdu_thread(void *_arg)
{
	int err, pipefd, dirfd;
	ssize_t ssz;
	struct rdu_thread_arg *arg = _arg;
	struct au_rdu_ent *ent, **pos;
	struct rdu *p;

	pipefd = arg->pipefd;
	p = arg->p;
	dirfd = p->fd;
	pos = p->pos;
	while (1) {
		DPri("read\n");
		ssz = read(pipefd, &ent, sizeof(ent));
		DPri("ssz %zd\n", ssz);
		if (ssz != sizeof(ent) || !ent) {
			//perror("read");
			break;
		}

		//DPri("%p\n", ent);
		err = rdu_do_store(dirfd, ent, pos, p);
	}

	DPri("here\n");
	return NULL;
}

static int rdu_store(struct rdu *p, struct au_rdu_ent *ent, int pipefd)
{
#ifdef RduLocalTest
	if (ent)
		return rdu_do_store(p->fd, ent, p->pos, p);
	return 0;
#else
	ssize_t ssz;

	//DPri("%p\n", ent);
	ssz = write(pipefd, &ent, sizeof(ent));
	DPri("ssz %zd\n", ssz);
	//sleep(1);
	return ssz != sizeof(ent);
#endif
}

/* ---------------------------------------------------------------------- */
/* the heart of this library */

static void rdu_tfree(void *node)
{
	/* empty */
}

static int rdu_ent_compar(const void *_a, const void *_b)
{
	int ret;
	const struct au_rdu_ent *a = _a, *b = _b;

	ret = (int)a->nlen - b->nlen;
	if (!ret)
		ret = memcmp(a->name, b->name, a->nlen);
	return ret;
}

static int rdu_ent_compar_wh(const void *_a, const void *_b)
{
	int ret;
	const struct au_rdu_ent *real = _a, *wh = _b;

	if (real->nlen >= AUFS_WH_PFX_LEN
	    && !memcmp(real->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
		wh = _a;
		real = _b;
	}

	ret = (int)wh->nlen - AUFS_WH_PFX_LEN - real->nlen;
	if (!ret)
		ret = memcmp(wh->name + AUFS_WH_PFX_LEN, real->name,
			     real->nlen);
	return ret;
}

/* tsearch(3) may not be thread-safe */
static int rdu_ent_append(struct rdu *p, struct au_rdu_ent *ent, int pipefd)
{
	int err;
	struct au_rdu_ent *e;

	err = 0;
	e = tfind(ent, (void *)&p->wh, rdu_ent_compar_wh);
	if (e)
		goto out;

	e = tsearch(ent, (void *)&p->real, rdu_ent_compar);
	if (e)
		err = rdu_store(p, ent, pipefd);
	else
		err = -1;

 out:
	return err;
}

static int rdu_ent_append_wh(struct rdu *p, struct au_rdu_ent *ent, int pipefd)
{
	int err;
	struct au_rdu_ent *e;

	err = 0;
	e = tfind(ent, (void *)&p->wh, rdu_ent_compar);
	if (e)
		goto out;

	e = tsearch(ent, (void *)&p->wh, rdu_ent_compar);
	if (e) {
		if (p->shwh)
			err = rdu_store(p, ent, pipefd);
	} else
		err = -1;

 out:
	return err;
}

static int rdu_merge(struct rdu *p)
{
	int err;
	unsigned long ul;
	pthread_t th;
	int fds[2];
	struct rdu_thread_arg arg;
	struct au_rdu_ent *ent;
	void *t;

	err = -1;
	p->pos = malloc(sizeof(*p->pos) * p->npos);
	if (!p->pos)
		goto out;

	/* pipe(2) may not be scheduled well in linux-2.6.23 and earlier */
	err = pipe(fds);
	if (err)
		goto out_free;

	arg.pipefd = fds[0];
	arg.p = p;
#ifndef RduLocalTest
	err = pthread_create(&th, NULL, rdu_thread, &arg);
#endif
	if (err)
		goto out_close;

	p->real = NULL;
	p->wh = NULL;
	ent = p->ent;
	for (ul = 0; !err && ul < p->npos; ul++) {
		if (ent->nlen <= AUFS_WH_PFX_LEN
		    || strncmp(ent->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
			err = rdu_ent_append(p, ent, fds[1]);
		else
			err = rdu_ent_append_wh(p, ent, fds[1]);
		ent += au_rdu_len(ent->nlen);
	}
	rdu_store(p, /*ent*/NULL, fds[1]); /* terminate the thread */
	tdestroy(p->real, rdu_tfree);
	tdestroy(p->wh, rdu_tfree);

#ifndef RduLocalTest
	pthread_join(th, NULL);
#endif
	p->npos = p->idx;
	t = realloc(p->pos, sizeof(*p->pos) * p->npos);
	if (t)
		p->pos = t;
	/* t == NULL is not an error */

 out_close:
	close(fds[1]);
	close(fds[0]);
	if (!err)
		goto out; /* success */
 out_free:
	free(p->pos);
	p->pos = NULL;
 out:
	return err;
}

static int rdu_init(struct rdu *p)
{
	int err;
	struct aufs_rdu param;
	char *t;

	memset(&param, 0, sizeof(param));
	param.ent = p->ent;
	param.sz = p->sz;
	t = getenv("AUFS_RDU_BLK");
	if (t)
		param.blk = strtoul(t + sizeof("AUFS_RDU_BLK"), NULL, 0);

	do {
		err = ioctl(p->fd, AUFS_CTL_RDU, &param);
		err = rdu_test_data(p, err);
		if (err > 0) {
			p->npos += err;
			if (!param.full)
				continue;

			assert(param.blk);
			t = realloc(p->ent, p->sz + param.blk);
			if (t) {
				param.sz = param.blk;
				param.ent = (void *)(t + p->sz);
				p->ent = (void *)t;
				p->sz += param.blk;
			} else
				err = -1;
		}
	} while (err > 0);
	p->shwh = param.shwh;
	if (!err)
		err = rdu_merge(p);

	if (err) {
		free(p->ent);
		p->ent = NULL;
	}

	return err;
}

static int rdu_pos(struct dirent *de, struct rdu *p, long pos)
{
	int err;
	struct au_rdu_ent *ent;

	err = -1;
	if (pos <= p->npos) {
		ent = p->pos[pos];
		de->d_ino = ent->ino;
		de->d_off = pos;
		de->d_reclen = sizeof(*ent) + ent->nlen;
		de->d_type = ent->type;
		memcpy(de->d_name, ent->name, ent->nlen);
		de->d_name[ent->nlen] = 0;
		err = 0;
	}
	return err;
}

/* ---------------------------------------------------------------------- */

static struct dirent *(*real_readdir)(DIR *dir);
static int (*real_readdir_r)(DIR *dir, struct dirent *de, struct dirent **rde);
static int (*real_closedir)(DIR *dir);

static int rdu_dl(void **real, char *sym)
{
	char *p;

	if (*real)
		return 0;

	dlerror(); /* clear */
	*real = dlsym(RTLD_NEXT, sym);
	p = dlerror();
	if (p)
		fprintf(stderr, "%s\n", p);
	return !!p;
}

#define RduDlFunc(sym) \
static int rdu_dl_##sym(void) \
{ \
	return rdu_dl((void *)&real_##sym, #sym); \
}

RduDlFunc(readdir);
RduDlFunc(closedir);

#ifdef AuRDU_REENTRANT
RduDlFunc(readdir_r);
#else
#define rdu_dl_readdir_r()	1
#endif

/* ---------------------------------------------------------------------- */

static int rdu_readdir(DIR *dir, struct dirent *de, struct dirent **rde)
{
	int err, fd;
	struct rdu *p;
	long pos;
	struct statfs stfs;

	if (rde)
		*rde = NULL;

	errno = EBADF;
	fd = dirfd(dir);
	err = fd;
	if (fd < 0)
		goto out;

	err = fstatfs(fd, &stfs);
	if (err)
		goto out;

	if (
#ifdef RduLocalTest
		1 ||
#endif
		stfs.f_type == AUFS_SUPER_MAGIC) {
		err = rdu_lib_init();
		if (err)
			goto out;

		p = rdu_buf_lock(fd);
		if (!p)
			goto out;

		pos = telldir(dir);
		if (!pos || !p->npos) {
			err = rdu_init(p);
			rdu_unlock(p);
		}
		if (err)
			goto out;

		rdu_read_lock(p);
		if (!de)
			de = &p->de;
		err = rdu_pos(de, p, pos);
		rdu_unlock(p);
		if (!err) {
			*rde = de;
			seekdir(dir, pos + 1);
		}
	} else if (!de) {
		if (!rdu_dl_readdir()) {
			err = 0;
			*rde = real_readdir(dir);
			if (!*rde)
				err = -1;
		}
	} else {
		if (!rdu_dl_readdir_r())
			err = real_readdir_r(dir, de, rde);
	}
 out:
	return err;
}

struct dirent *readdir(DIR *dir)
{
	struct dirent *de;
	int err;

	err = rdu_readdir(dir, NULL, &de);
	DPri("err %d\n", err);
	return de;
}

#ifdef AuRDU_REENTRANT
int readdir_r(DIR *dirp, struct dirent *de, struct dirent **rde)
{
	return rdu_readdir(dir, de, rde);
}
#endif

int closedir(DIR *dir)
{
	int err, fd;
	struct statfs stfs;

	errno = EBADF;
	fd = dirfd(dir);
	if (fd < 0)
		goto out;
	err = fstatfs(fd, &stfs);
	if (err)
		goto out;

	if (stfs.f_type == AUFS_SUPER_MAGIC)
		rdu_free(fd);
	if (!rdu_dl_closedir())
		err = real_closedir(dir);

 out:
	return err;
}

#if 0
extern DIR *opendir (__const char *__name) __nonnull ((1));
extern int closedir (DIR *__dirp) __nonnull ((1));
extern struct dirent *__REDIRECT (readdir, (DIR *__dirp), readdir64)
     __nonnull ((1));
extern struct dirent64 *readdir64 (DIR *__dirp) __nonnull ((1));
extern int readdir_r (DIR *__restrict __dirp,
		      struct dirent *__restrict __entry,
		      struct dirent **__restrict __result)
     __nonnull ((1, 2, 3));
extern int readdir64_r (DIR *__restrict __dirp,
			struct dirent64 *__restrict __entry,
			struct dirent64 **__restrict __result)
     __nonnull ((1, 2, 3));
extern void rewinddir (DIR *__dirp) __THROW __nonnull ((1));
extern void seekdir (DIR *__dirp, long int __pos) __THROW __nonnull ((1));
extern long int telldir (DIR *__dirp) __THROW __nonnull ((1));
extern int dirfd (DIR *__dirp) __THROW __nonnull ((1));
extern int scandir (__const char *__restrict __dir,
		    struct dirent ***__restrict __namelist,
		    int (*__selector) (__const struct dirent *),
		    int (*__cmp) (__const void *, __const void *))
     __nonnull ((1, 2));
extern int scandir64 (__const char *__restrict __dir,
		      struct dirent64 ***__restrict __namelist,
		      int (*__selector) (__const struct dirent64 *),
		      int (*__cmp) (__const void *, __const void *))
     __nonnull ((1, 2));
extern int alphasort (__const void *__e1, __const void *__e2)
     __THROW __attribute_pure__ __nonnull ((1, 2));
extern int alphasort64 (__const void *__e1, __const void *__e2)
     __THROW __attribute_pure__ __nonnull ((1, 2));
extern int versionsort (__const void *__e1, __const void *__e2)
     __THROW __attribute_pure__ __nonnull ((1, 2));
extern int versionsort64 (__const void *__e1, __const void *__e2)
     __THROW __attribute_pure__ __nonnull ((1, 2));
extern __ssize_t getdirentries (int __fd, char *__restrict __buf,
				size_t __nbytes,
				__off_t *__restrict __basep)
     __THROW __nonnull ((2, 4));
extern __ssize_t getdirentries64 (int __fd, char *__restrict __buf,
				  size_t __nbytes,
				  __off64_t *__restrict __basep)
     __THROW __nonnull ((2, 4));
#endif