summaryrefslogtreecommitdiffstats
path: root/xlators/performance/read-ahead/src/read-ahead.c
blob: 067c5b29a310675577ecda2822ed02e85307f96d (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
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
/*
  Copyright (c) 2006-2009 Gluster, Inc. <http://www.gluster.com>
  This file is part of GlusterFS.

  GlusterFS 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 3 of the License,
  or (at your option) any later version.

  GlusterFS 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, see
  <http://www.gnu.org/licenses/>.
*/

/* 
   TODO:
   - handle O_DIRECT
   - maintain offset, flush on lseek
   - ensure efficient memory managment in case of random seek
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include "glusterfs.h"
#include "logging.h"
#include "dict.h"
#include "xlator.h"
#include "read-ahead.h"
#include "statedump.h"
#include <assert.h>
#include <sys/time.h>

static void
read_ahead (call_frame_t *frame, ra_file_t *file);


int
ra_open_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
             int32_t op_ret, int32_t op_errno, fd_t *fd)
{
	ra_conf_t  *conf = NULL;
	ra_file_t  *file = NULL;
	int         ret = 0;
        long        wbflags = 0;

	conf  = this->private;

	if (op_ret == -1) {
		goto unwind;
	}

        wbflags = (long)frame->local;

	file = GF_CALLOC (1, sizeof (*file), gf_ra_mt_ra_file_t);
	if (!file) {
                op_ret = -1;
                op_errno = ENOMEM;
		gf_log (this->name, GF_LOG_ERROR,
			"out of memory");
		goto unwind;
	}

	/* If O_DIRECT open, we disable caching on it */

	if ((fd->flags & O_DIRECT) || ((fd->flags & O_ACCMODE) == O_WRONLY))
		file->disabled = 1;

        if (wbflags & GF_OPEN_NOWB) {
                file->disabled = 1;
        }

	file->offset = (unsigned long long) 0;
	file->conf = conf;
	file->pages.next = &file->pages;
	file->pages.prev = &file->pages;
	file->pages.offset = (unsigned long long) 0;
	file->pages.file = file;

	ra_conf_lock (conf);
	{
		file->next = conf->files.next;
		conf->files.next = file;
		file->next->prev = file;
		file->prev = &conf->files;
	}
	ra_conf_unlock (conf);

	file->fd = fd;
	file->page_count = conf->page_count;
	file->page_size = conf->page_size;
	pthread_mutex_init (&file->file_lock, NULL);

	if (!file->disabled) {
		file->page_count = 1;
	}

	ret = fd_ctx_set (fd, this, (uint64_t)(long)file);
        if (ret == -1) {
                ra_file_destroy (file);
                op_ret = -1;
                op_errno = ENOMEM;
        }
        
unwind:
        frame->local = NULL;

	STACK_UNWIND_STRICT (open, frame, op_ret, op_errno, fd);

	return 0;
}


int
ra_create_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
               int32_t op_ret, int32_t op_errno, fd_t *fd, inode_t *inode,
               struct iatt *buf, struct iatt *preparent,
               struct iatt *postparent)
{
	ra_conf_t  *conf = NULL;
	ra_file_t  *file = NULL;
	int         ret = 0;

	conf  = this->private;

	if (op_ret == -1) {
		goto unwind;
	}

	file = GF_CALLOC (1, sizeof (*file), gf_ra_mt_ra_file_t);
	if (!file) {
                op_ret = -1;
                op_errno = ENOMEM;
		gf_log (this->name, GF_LOG_ERROR,
			"out of memory");
		goto unwind;
	}

	/* If O_DIRECT open, we disable caching on it */

	if ((fd->flags & O_DIRECT) || ((fd->flags & O_ACCMODE) == O_WRONLY))
			file->disabled = 1;

	file->offset = (unsigned long long) 0;
	//file->size = fd->inode->buf.ia_size;
	file->conf = conf;
	file->pages.next = &file->pages;
	file->pages.prev = &file->pages;
	file->pages.offset = (unsigned long long) 0;
	file->pages.file = file;

	ra_conf_lock (conf);
	{
		file->next = conf->files.next;
		conf->files.next = file;
		file->next->prev = file;
		file->prev = &conf->files;
	}
	ra_conf_unlock (conf);

	file->fd = fd;
	file->page_count = conf->page_count;
	file->page_size = conf->page_size;
	pthread_mutex_init (&file->file_lock, NULL);

	ret = fd_ctx_set (fd, this, (uint64_t)(long)file);
        if (ret == -1) {
                ra_file_destroy (file);
                op_ret = -1;
                op_errno = ENOMEM;
        }

unwind:
	STACK_UNWIND_STRICT (create, frame, op_ret, op_errno, fd, inode, buf,
                             preparent, postparent);

	return 0;
}


int
ra_open (call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
         fd_t *fd, int32_t wbflags)
{
        frame->local = (void *)(long)wbflags;

	STACK_WIND (frame, ra_open_cbk,
		    FIRST_CHILD (this),
		    FIRST_CHILD (this)->fops->open,
		    loc, flags, fd, wbflags);

	return 0;
}

int
ra_create (call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
           mode_t mode, fd_t *fd, dict_t *params)
{
	STACK_WIND (frame, ra_create_cbk,
		    FIRST_CHILD(this),
		    FIRST_CHILD(this)->fops->create,
		    loc, flags, mode, fd, params);

	return 0;
}

/* free cache pages between offset and offset+size,
   does not touch pages with frames waiting on it
*/

static void
flush_region (call_frame_t *frame, ra_file_t *file, off_t offset, off_t size)
{
	ra_page_t *trav = NULL;
	ra_page_t *next = NULL;

	ra_file_lock (file);
	{
		trav = file->pages.next;
		while (trav != &file->pages
		       && trav->offset < (offset + size)) {

			next = trav->next;
			if (trav->offset >= offset && !trav->waitq) {
				ra_page_purge (trav);
			}
			trav = next;
		}
	}
	ra_file_unlock (file);
}


int
ra_release (xlator_t *this, fd_t *fd)
{
	uint64_t tmp_file = 0;
	int      ret = 0;

	ret = fd_ctx_del (fd, this, &tmp_file);
	
	if (!ret) {
		ra_file_destroy ((ra_file_t *)(long)tmp_file);
	}

	return 0;
}


void
read_ahead (call_frame_t *frame, ra_file_t *file)
{
	off_t      ra_offset = 0;
	size_t     ra_size = 0;
	off_t      trav_offset = 0;
	ra_page_t  *trav = NULL;
	off_t      cap = 0;
	char       fault = 0;

	if (!file->page_count)
		return;

	ra_size   = file->page_size * file->page_count;
	ra_offset = floor (file->offset, file->page_size);
	cap       = file->size ? file->size : file->offset + ra_size;

	while (ra_offset < min (file->offset + ra_size, cap)) {

		ra_file_lock (file);
		{
			trav = ra_page_get (file, ra_offset);
		}
		ra_file_unlock (file);

		if (!trav)
			break;

		ra_offset += file->page_size;
	}

	if (trav)
		/* comfortable enough */
		return;

	trav_offset = ra_offset;

	cap  = file->size ? file->size : ra_offset + ra_size;

	while (trav_offset < min(ra_offset + ra_size, cap)) {
		fault = 0;
		ra_file_lock (file);
		{
			trav = ra_page_get (file, trav_offset);
			if (!trav) {
				fault = 1;
				trav = ra_page_create (file, trav_offset);
				if (trav) 
					trav->dirty = 1;
			}
		}
		ra_file_unlock (file);

		if (!trav) {
			/* OUT OF MEMORY */
			break;
		}

		if (fault) {
			gf_log (frame->this->name, GF_LOG_TRACE,
				"RA at offset=%"PRId64, trav_offset);
			ra_page_fault (file, frame, trav_offset);
		}
		trav_offset += file->page_size;
	}

	return;
}


int
ra_need_atime_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                   int32_t op_ret, int32_t op_errno, struct iovec *vector,
                   int32_t count, struct iatt *stbuf, struct iobref *iobref)
{
	STACK_DESTROY (frame->root);
	return 0;
}


static void
dispatch_requests (call_frame_t *frame, ra_file_t *file)
{
	ra_local_t    *local = NULL;
	ra_conf_t     *conf = NULL;
	off_t         rounded_offset = 0;
	off_t         rounded_end = 0;
	off_t         trav_offset = 0;
	ra_page_t     *trav = NULL;
	call_frame_t  *ra_frame = NULL;
	char          need_atime_update = 1;
	char          fault = 0;

	local = frame->local;
	conf  = file->conf;

	rounded_offset = floor (local->offset, file->page_size);
	rounded_end    = roof (local->offset + local->size, file->page_size);

	trav_offset = rounded_offset;

	while (trav_offset < rounded_end) {
		fault = 0;

		ra_file_lock (file);
		{
			trav = ra_page_get (file, trav_offset);
			if (!trav) {
				trav = ra_page_create (file, trav_offset);
				fault = 1;
				need_atime_update = 0;
			}

			if (!trav) {
                                local->op_ret = -1;
                                local->op_errno = ENOMEM;
 				goto unlock;
                        }

			if (trav->ready) {
				gf_log (frame->this->name, GF_LOG_TRACE,
					"HIT at offset=%"PRId64".",
					trav_offset);
				ra_frame_fill (trav, frame);
			} else {
				gf_log (frame->this->name, GF_LOG_TRACE,
					"IN-TRANSIT at offset=%"PRId64".",
					trav_offset);
				ra_wait_on_page (trav, frame);
				need_atime_update = 0;
			}
		}
	unlock:
		ra_file_unlock (file);

		if (fault) {
			gf_log (frame->this->name, GF_LOG_TRACE,
				"MISS at offset=%"PRId64".",
				trav_offset);
			ra_page_fault (file, frame, trav_offset);
		}

		trav_offset += file->page_size;
	}

	if (need_atime_update && conf->force_atime_update) {
		/* TODO: use untimens() since readv() can confuse underlying
		   io-cache and others */
		ra_frame = copy_frame (frame);
                if (ra_frame == NULL) {
                        goto out;
                }

		STACK_WIND (ra_frame, ra_need_atime_cbk,
			    FIRST_CHILD (frame->this), 
			    FIRST_CHILD (frame->this)->fops->readv,
			    file->fd, 1, 1);
	}

out:
	return ;
}


int
ra_readv_disabled_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                       int32_t op_ret, int32_t op_errno, struct iovec *vector,
                       int32_t count, struct iatt *stbuf, struct iobref *iobref)
{
	STACK_UNWIND_STRICT (readv, frame, op_ret, op_errno, vector, count,
                             stbuf, iobref);

	return 0;
}


int
ra_readv (call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
          off_t offset)
{
	ra_file_t    *file = NULL;
	ra_local_t   *local = NULL;
	ra_conf_t    *conf = NULL;
	int          op_errno = 0;
	char         expected_offset = 1;
	uint64_t     tmp_file = 0;

	conf = this->private;

	gf_log (this->name, GF_LOG_TRACE,
		"NEW REQ at offset=%"PRId64" for size=%"GF_PRI_SIZET"",
		offset, size);

	fd_ctx_get (fd, this, &tmp_file);
	file = (ra_file_t *)(long)tmp_file;

        if (file == NULL) {
                op_errno = EBADF;
                gf_log (this->name, GF_LOG_DEBUG, "readv received on fd with no"
                        " file set in its context");
                goto unwind;
        }

	if (file->offset != offset) {
		gf_log (this->name, GF_LOG_DEBUG,
			"unexpected offset (%"PRId64" != %"PRId64") resetting",
			file->offset, offset);

		expected_offset = file->expected = file->page_count = 0;
	} else {
		gf_log (this->name, GF_LOG_TRACE,
			"expected offset (%"PRId64") when page_count=%d",
			offset, file->page_count);

		if (file->expected < (conf->page_size * conf->page_count)) {
			file->expected += size;
			file->page_count = min ((file->expected / file->page_size),
						conf->page_count);
		}
	}

	if (!expected_offset) {
		flush_region (frame, file, 0, file->pages.prev->offset + 1);
	}

	if (file->disabled) {
		STACK_WIND (frame, ra_readv_disabled_cbk,
			    FIRST_CHILD (frame->this), 
			    FIRST_CHILD (frame->this)->fops->readv,
			    file->fd, size, offset);
		return 0;
	}

	local = (void *) GF_CALLOC (1, sizeof (*local),
                                    gf_ra_mt_ra_local_t);
	if (!local) {
		gf_log (this->name, GF_LOG_ERROR,
			"out of memory");
		op_errno = ENOMEM;
		goto unwind;
	}

	local->fd         = fd;
	local->offset     = offset;
	local->size       = size;
	local->wait_count = 1;

	local->fill.next  = &local->fill;
	local->fill.prev  = &local->fill;

	pthread_mutex_init (&local->local_lock, NULL);

	frame->local = local;

	dispatch_requests (frame, file);

	flush_region (frame, file, 0, floor (offset, file->page_size));

        read_ahead (frame, file);

	ra_frame_return (frame);

	file->offset = offset + size;

	return 0;

unwind:
	STACK_UNWIND_STRICT (readv, frame, -1, op_errno, NULL, 0, NULL, NULL);

	return 0;
}


int
ra_flush_cbk (call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
              int32_t op_errno)
{
	STACK_UNWIND_STRICT (flush, frame, op_ret, op_errno);
	return 0;
}



int
ra_fsync_cbk (call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
              int32_t op_errno, struct iatt *prebuf, struct iatt *postbuf)
{
	STACK_UNWIND_STRICT (fsync, frame, op_ret, op_errno, prebuf, postbuf);
	return 0;
}


int
ra_flush (call_frame_t *frame, xlator_t *this, fd_t *fd)
{
	ra_file_t *file = NULL;
	uint64_t  tmp_file = 0;
        int32_t   op_errno = 0;

	fd_ctx_get (fd, this, &tmp_file);
	file = (ra_file_t *)(long)tmp_file;
        if (file == NULL) {
                op_errno = EBADF;
                gf_log (this->name, GF_LOG_DEBUG, "flush received on fd with no"
                        " file set in its context");
                goto unwind;
        }

        flush_region (frame, file, 0, file->pages.prev->offset+1);

	STACK_WIND (frame, ra_flush_cbk,
		    FIRST_CHILD (this),
		    FIRST_CHILD (this)->fops->flush,
		    fd);
	return 0;

unwind:
	STACK_UNWIND_STRICT (flush, frame, -1, op_errno);
        return 0;
}


int
ra_fsync (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync)
{
	ra_file_t *file = NULL;
	uint64_t  tmp_file = 0;
        int32_t   op_errno = 0;

	fd_ctx_get (fd, this, &tmp_file);
	file = (ra_file_t *)(long)tmp_file;
        if (file == NULL) {
                op_errno = EBADF;
                gf_log (this->name, GF_LOG_DEBUG, "fsync received on fd with no"
                        " file set in its context");
                goto unwind;
        }

	if (file) {
		flush_region (frame, file, 0, file->pages.prev->offset+1);
	}

	STACK_WIND (frame, ra_fsync_cbk,
		    FIRST_CHILD (this),
		    FIRST_CHILD (this)->fops->fsync,
		    fd, datasync);
	return 0;

unwind:
	STACK_UNWIND_STRICT (fsync, frame, -1, op_errno, NULL, NULL);
        return 0;
}


int
ra_writev_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
               int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
               struct iatt *postbuf)
{
	fd_t      *fd = NULL;
	ra_file_t *file = NULL;
	uint64_t  tmp_file = 0;

	fd = frame->local;

	fd_ctx_get (fd, this, &tmp_file);
	file = (ra_file_t *)(long)tmp_file;

        flush_region (frame, file, 0, file->pages.prev->offset+1);

	frame->local = NULL;
	STACK_UNWIND_STRICT (writev, frame, op_ret, op_errno, prebuf, postbuf);
	return 0;
}


int
ra_writev (call_frame_t *frame, xlator_t *this, fd_t *fd, struct iovec *vector,
           int32_t count, off_t offset, struct iobref *iobref)
{
	ra_file_t *file = NULL;
	uint64_t  tmp_file = 0;
        int32_t   op_errno = 0;

	fd_ctx_get (fd, this, &tmp_file);
	file = (ra_file_t *)(long)tmp_file;
        if (file == NULL) {
                op_errno = EBADF;
                gf_log (this->name, GF_LOG_DEBUG, "writev received on fd with"
                        "no file set in its context");
                goto unwind;
        }

        flush_region (frame, file, 0, file->pages.prev->offset+1);

        /* reset the read-ahead counters too */
        file->expected = file->page_count = 0;

	frame->local = fd;

	STACK_WIND (frame, ra_writev_cbk,
		    FIRST_CHILD(this),
		    FIRST_CHILD(this)->fops->writev,
		    fd, vector, count, offset, iobref);

	return 0;

unwind:
	STACK_UNWIND_STRICT (writev, frame, -1, op_errno, NULL, NULL);
        return 0;
}


int
ra_truncate_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
                 struct iatt *postbuf)
{
	STACK_UNWIND_STRICT (truncate, frame, op_ret, op_errno, prebuf,
                             postbuf);
	return 0;
}


int
ra_attr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
	     int32_t op_ret, int32_t op_errno, struct iatt *buf)
{
	STACK_UNWIND_STRICT (stat, frame, op_ret, op_errno, buf);
	return 0;
}


int
ra_truncate (call_frame_t *frame, xlator_t *this, loc_t *loc, off_t offset)
{
	ra_file_t *file = NULL;
	fd_t      *iter_fd = NULL;
	inode_t   *inode = NULL;
	uint64_t  tmp_file = 0;

	inode = loc->inode;

	LOCK (&inode->lock);
	{
		list_for_each_entry (iter_fd, &inode->fd_list, inode_list) {
			fd_ctx_get (iter_fd, this, &tmp_file);
			file = (ra_file_t *)(long)tmp_file;

			if (!file)
				continue;
			flush_region (frame, file, 0,
				      file->pages.prev->offset + 1);
		}
	}
	UNLOCK (&inode->lock);

	STACK_WIND (frame, ra_truncate_cbk,
		    FIRST_CHILD (this),
		    FIRST_CHILD (this)->fops->truncate,
		    loc, offset);
	return 0;
}


int
ra_fstat (call_frame_t *frame, xlator_t *this, fd_t *fd)
{
	ra_file_t *file = NULL;
	fd_t      *iter_fd = NULL;
	inode_t   *inode = NULL;
	uint64_t  tmp_file = 0;

	inode = fd->inode;

	LOCK (&inode->lock);
	{
		list_for_each_entry (iter_fd, &inode->fd_list, inode_list) {
			fd_ctx_get (iter_fd, this, &tmp_file);
			file = (ra_file_t *)(long)tmp_file;

			if (!file)
				continue;
			flush_region (frame, file, 0,
				      file->pages.prev->offset + 1);
		}
	}
	UNLOCK (&inode->lock);

	STACK_WIND (frame, ra_attr_cbk,
		    FIRST_CHILD (this),
		    FIRST_CHILD (this)->fops->fstat,
		    fd);
	return 0;
}


int
ra_ftruncate (call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset)
{
	ra_file_t *file = NULL;
	fd_t      *iter_fd = NULL;
	inode_t   *inode = NULL;
	uint64_t  tmp_file = 0;

	inode = fd->inode;

	LOCK (&inode->lock);
	{
		list_for_each_entry (iter_fd, &inode->fd_list, inode_list) {
			fd_ctx_get (iter_fd, this, &tmp_file);
			file = (ra_file_t *)(long)tmp_file;
			if (!file)
				continue;
			flush_region (frame, file, 0,
				      file->pages.prev->offset + 1);
		}
	}
	UNLOCK (&inode->lock);

	STACK_WIND (frame, ra_truncate_cbk,
		    FIRST_CHILD (this),
		    FIRST_CHILD (this)->fops->ftruncate,
		    fd, offset);
	return 0;
}

int
ra_priv_dump (xlator_t *this)
{
        ra_conf_t       *conf = NULL;
        int             ret = -1;
        char            key[GF_DUMP_MAX_BUF_LEN];
        char            key_prefix[GF_DUMP_MAX_BUF_LEN];

        if (!this)
                return -1;

        conf = this->private;
        if (!conf) {
                gf_log (this->name, GF_LOG_WARNING,
                        "conf null in xlator");
                return -1;
        }

        ret = pthread_mutex_trylock (&conf->conf_lock);
        if (ret) {
                gf_log ("", GF_LOG_WARNING, "Unable to lock client %s"
                        " errno: %d", this->name, errno);
                return -1;
        }


        gf_proc_dump_build_key (key_prefix,
                                "xlator.performance.read-ahead",
                                "priv");

        gf_proc_dump_add_section (key_prefix);
        gf_proc_dump_build_key (key, key_prefix, "page_size");
        gf_proc_dump_write (key, "%d", conf->page_size);
        gf_proc_dump_build_key (key, key_prefix, "page_count");
        gf_proc_dump_write (key, "%d", conf->page_count);
        gf_proc_dump_build_key (key, key_prefix, "force_atime_update");
        gf_proc_dump_write (key, "%d", conf->force_atime_update);

        pthread_mutex_unlock (&conf->conf_lock);

        return 0;
}

int32_t
mem_acct_init (xlator_t *this)
{
        int     ret = -1;

        if (!this)
                return ret;

        ret = xlator_mem_acct_init (this, gf_ra_mt_end + 1);
        
        if (ret != 0) {
                gf_log (this->name, GF_LOG_ERROR, "Memory accounting init"
                                "failed");
                return ret;
        }

        return ret;
}

int
init (xlator_t *this)
{
	ra_conf_t *conf = NULL;
	dict_t    *options = this->options;
	char      *page_count_string = NULL;
        int32_t   ret = -1;

	if (!this->children || this->children->next) {
		gf_log (this->name,  GF_LOG_ERROR,
			"FATAL: read-ahead not configured with exactly one"
                        " child");
                goto out;
	}

	if (!this->parents) {
		gf_log (this->name, GF_LOG_WARNING,
			"dangling volume. check volfile ");
	}
 
	conf = (void *) GF_CALLOC (1, sizeof (*conf),
                                   gf_ra_mt_ra_conf_t);
        if (conf == NULL) {
                gf_log (this->name, GF_LOG_ERROR,
                        "FATAL: Out of memory");
                goto out;
        }

	conf->page_size = this->ctx->page_size;
	conf->page_count = 4;

	if (dict_get (options, "page-count"))
		page_count_string = data_to_str (dict_get (options, 
							   "page-count"));
	if (page_count_string)
	{
		if (gf_string2uint_base10 (page_count_string, &conf->page_count)
                    != 0)
		{
			gf_log ("read-ahead", 
				GF_LOG_ERROR, 
				"invalid number format \"%s\" of \"option "
                                "page-count\"", 
				page_count_string);
                        goto out;
		}
		gf_log (this->name, GF_LOG_DEBUG, "Using conf->page_count = %u",
			conf->page_count);
	}
  
	if (dict_get (options, "force-atime-update")) {
		char *force_atime_update_str = data_to_str (dict_get (options,
								      "force-atime-update"));
		if (gf_string2boolean (force_atime_update_str,
                                       &conf->force_atime_update) == -1) {
			gf_log (this->name, GF_LOG_ERROR,
				"'force-atime-update' takes only boolean "
                                "options");
                        goto out;
		}
		if (conf->force_atime_update)
			gf_log (this->name, GF_LOG_DEBUG, "Forcing atime "
                                "updates on cache hit");
	}

	conf->files.next = &conf->files;
	conf->files.prev = &conf->files;

	pthread_mutex_init (&conf->conf_lock, NULL);
	this->private = conf;
        ret = 0;

out:
        if (ret == -1) {
                if (conf != NULL) {
                        GF_FREE (conf);
                }
        }

        return ret;
}

void
fini (xlator_t *this)
{
	ra_conf_t *conf = this->private;

        if (conf == NULL)
                return;

	pthread_mutex_destroy (&conf->conf_lock);
	GF_FREE (conf);

	this->private = NULL;
	return;
}

struct xlator_fops fops = {
	.open        = ra_open,
	.create      = ra_create,
	.readv       = ra_readv,
	.writev      = ra_writev,
	.flush       = ra_flush,
	.fsync       = ra_fsync,
	.truncate    = ra_truncate,
	.ftruncate   = ra_ftruncate,
	.fstat       = ra_fstat,
};

struct xlator_cbks cbks = {
	.release       = ra_release,
};

struct xlator_dumpops dumpops = {
        .priv      =  ra_priv_dump,
};

struct volume_options options[] = {
	{ .key  = {"force-atime-update"}, 
	  .type = GF_OPTION_TYPE_BOOL 
	},
	{ .key  = {"page-count"}, 
	  .type = GF_OPTION_TYPE_INT, 
	  .min  = 1, 
	  .max  = 16 
	},
	{ .key = {NULL} },
};
> v3.7.14commit 04fa81f1c4...Kaushal M9 years v3.8.1commit 754e2658ba...Niels de Vos10 years v3.7.13commit 4dce874a77...Kaushal M10 years v3.7.12commit 5cf8e28988...Kaushal M10 years v3.8.0commit c6d9e23b54...Niels de Vos10 years v3.7.12rc1commit bac2781a2d...Vijay Bellur10 years v3.8rc2commit d53bfc6f04...Niels de Vos10 years v3.8rc1commit 74ab2d6f10...Niels de Vos10 years v3.8rc0commit 0fadd46c2c...Niels de Vos10 years v3.9devcommit 60e340481a...Niels de Vos10 years v3.7.11commit b0bfc70048...Kaushal M10 years v3.7.10commit 889e92c722...Kaushal M10 years v3.7.9commit 1eece065f1...Vijay Bellur10 years v3.5.9commit ab52c1e696...Niels de Vos10 years v3.6.9commit 52f6c664a2...Raghavendra Bhat10 years v3.5.8commit 721b15a74d...Niels de Vos10 years v3.7.8commit 3db831788c...Pranith Kumar K10 years v3.7.7commit ccdfa49c90...Pranith Kumar K10 years v3.6.8commit b19a1d0798...Kaushal M10 years v3.5.7commit 3efd1b8997...Niels de Vos10 years v3.6.7commit 5d264dbcb7...Raghavendra Bhat10 years v3.7.6commit a3289b1d06...Raghavendra Talur10 years v3.7.5commit 25e581d42e...Pranith Kumar K10 years v3.6.6commit 69b5471d13...Raghavendra Bhat10 years v3.5.6commit 6a0faa1ff1...Niels de Vos10 years v3.7.4commit 300a69669a...Kaushal M10 years v3.6.5commit dfa2bfb289...Raghavendra Bhat10 years v3.7.3commit 918e430294...Kaushal M10 years v3.6.4commit 50b0c7baad...Raghavendra Bhat10 years v3.5.5commit 191725abf5...Niels de Vos11 years v3.7.2commit 568d7f4dda...Atin Mukherjee11 years v3.6.4beta2commit 83778a592f...Raghavendra Bhat11 years v3.5.4commit 55bd875f0b...Niels de Vos11 years v3.7.1commit f266dc6be5...Krishnan Parthasarathi11 years v3.6.4beta1commit 24b3db0768...Raghavendra Bhat11 years v3.7.0commit 5538051a64...Vijay Bellur11 years v3.7.0beta2commit a492b22f57...Niels de Vos11 years v3.7.0beta1commit 7e2baf9acd...Vijay Bellur11 years v3.6.3commit e7640557a6...Raghavendra Bhat11 years v3.8devcommit 08a1041ca9...Vijay Bellur11 years v3.7.0alpha0commit 8b987be340...Vijay Bellur11 years v3.4.7commit bcb906a9e2...Kaleb S. KEITHLEY11 years v3.5.4beta1commit 077185afe3...Niels de Vos11 years v3.6.3beta2commit 3d76e803e7...Raghavendra Bhat11 years v3.4.7beta4commit bcb906a9e2...Kaleb S. KEITHLEY11 years v3.4.7beta3commit fcd46d8e8f...Kaleb S. KEITHLEY11 years v3.4.7beta2commit 982f0fac53...Kaleb S. KEITHLEY11 years v3.4.7beta1commit b037cfe399...Kaleb S. KEITHLEY11 years v3.6.3beta1commit e312b0807b...Raghavendra Bhat11 years v3.6.2commit 709d471294...Raghavendra Bhat11 years v3.6.2beta2commit 443cda365c...Raghavendra Bhat11 years v3.6.2beta1commit 6e423ca790...Raghavendra Bhat11 years v3.5.3commit be560c6f41...Niels de Vos11 years v3.4.6commit ceaba0fc43...Kaleb S. KEITHLEY11 years v3.6.1commit 1ffdf112f7...Vijay Bellur11 years v3.6.0commit 3867bdb496...Vijay Bellur11 years v3.4.6beta2commit ceaba0fc43...Kaleb S. KEITHLEY11 years v3.5.3beta2commit a970fc0cd7...Niels de Vos11 years v3.5.3beta1commit db7d578da0...Niels de Vos11 years v3.6.0beta3commit 912eec63f4...Vijay Bellur11 years v3.6.0beta2commit dd80d06145...Vijay Bellur11 years v3.6.0beta1commit 936cf82d93...Vijay Bellur11 years v3.4.6beta1commit 1d4ef0b891...Kaleb S. KEITHLEY11 years v3.5.2commit 9bb7cb9ff1...Niels de Vos11 years v3.4.5commit 7b564b8800...Kaleb S. KEITHLEY11 years v3.5.2beta1commit d5f72dc496...Niels de Vos11 years v3.6.0alpha1commit 6aafb3a64a...Vijay Bellur11 years v3.7devcommit 52da727e75...Vijay Bellur11 years v3.4.5beta2commit e311014d94...Kaleb S. KEITHLEY12 years v3.4.5beta1commit ecc21940ea...Kaleb S. KEITHLEY12 years v3.5.1commit b8f6798c17...Niels de Vos12 years v3.5.1beta2commit b167a0ca7f...Niels de Vos12 years v3.4.4commit 8005d56f08...Kaleb S. KEITHLEY12 years v3.4.4beta1commit 8005d56f08...Kaleb S. KEITHLEY12 years v3.5.1betacommit d74024b470...Niels de Vos12 years v3.5.0commit 2e767af207...Vijay Bellur12 years v3.5.0beta5commit 4e0659a779...Vijay Bellur12 years v3.4.3commit b0d6d20ab2...Kaleb S. KEITHLEY12 years v3.4.3beta2commit 33cc417e64...Kaleb S. KEITHLEY12 years v3.4.3beta1commit 010a9a7867...Kaleb S. KEITHLEY12 years v3.5.0beta4commit e779cc8c32...Vijay Bellur12 years v3.4.3alpha1commit 945c6de4e6...Kaleb S. KEITHLEY12 years v3.5.0beta3commit b319f01ecd...Vijay Bellur12 years v3.5.0beta2commit a338c4fbc4...Vijay Bellur12 years v3.5beta1commit 1350c7193e...Vijay Bellur12 years v3.4.2commit 098fd71353...Vijay Bellur12 years v3.4.2qa5commit b2ee85b3e4...Vijay Bellur12 years v3.4.2qa4commit 1832dbf0ba...Vijay Bellur12 years v3.4.2qa3commit 790c2813ef...Vijay Bellur12 years v3.4.2qa2commit 1e40a57d16...Vijay Bellur12 years v3.5.0qa3commit b58810f5df...Anand Avati12 years v3.5qa2commit a25d321bad...Vijay Bellur12 years v3.4.2qa1commit 88dc9d8899...Anand Avati12 years v3.5.0qa1commit f21cefed29...Vijay Bellur12 years v3.4.1commit 56769c4db9...Vijay Bellur12 years v3.4.1rc1commit 56769c4db9...Anand Avati12 years v3.4.1qa3commit 04163fc4ba...Anand Avati12 years v3.4.1qa2commit 536eccde0b...Anand Avati12 years v3.4.1qa1commit 8565d383a1...Vijay Bellur12 years v3.3.2commit 34bca063cd...Vijay Bellur12 years v3.4.0commit b92b98ef9a...Vijay Bellur12 years v3.3.2qa4commit 34bca063cd...Vijay Bellur13 years v3.4.0beta4commit 505f57e07c...Vijay Bellur13 years v3.4.0beta3commit 2fda7a9de2...Vijay Bellur13 years v3.4.0beta2commit df83bc05ff...Vijay Bellur13 years v3.3.2qa3commit 1a7e6053d3...Vijay Bellur13 years v3.3.2qa2commit 0ab16bb29a...Vijay Bellur13 years v3.4.0beta1commit 5ac55756cd...Anand Avati13 years v3.4.0alpha3commit 92729add67...Vijay Bellur13 years v3.3.2qa1commit d836002fce...Vijay Bellur13 years v3.4.0alpha2commit c37546cf11...Anand Avati13 years v3.4.0alphacommit 765fdd0809...Vijay Bellur13 years v3.4.0qa8commit 315ee9c4e0...Vijay Bellur13 years v3.4.0qa7commit 6fd654dc94...Vijay Bellur13 years v3.3.1commit e7f14ad073...Vijay Bellur13 years v3.4.0qa6commit e8c75fd929...Vijay Bellur13 years v3.3.0.5rhs-40commit 6e3efac008...Vijay Bellur13 years v3.3.0.5rhs-39commit 6e3efac008...Vijay Bellur13 years v3.4.0qa5commit fef94c2acf...Vijay Bellur13 years v3.4.0qa4commit 48d749dda3...Vijay Bellur13 years v3.4.0qa3commit c85a3eee54...Vijay Bellur13 years v3.3.1qa3commit 517a9d2450...Vijay Bellur13 years v3.3.1qa2commit ace4cae71c...Vijay Bellur13 years v3.3.1qa1commit 753f8c1324...Vijay Bellur13 years v3.2.7commit 092dc2676b...Vijay Bellur14 years v3.2.7qa2commit 2533d2b56b...Vijay Bellur14 years v3.3.0commit 1b79849119...Vijay Bellur14 years v3.3.0qa45commit 493ef71222...Anand Avati14 years v3.3.0qa44commit 647f561f6a...Vijay Bellur14 years v3.3.0qa43commit 9d4c8b3909...Vijay Bellur14 years v3.3.0qa42commit d54d9e9412...Vijay Bellur14 years v3.3.0beta4commit bdd240eca1...Vijay Bellur14 years v3.3.0qa41commit 8852f95869...Vijay Bellur14 years v3.3.0qa40commit 9189ff9739...Vijay Bellur14 years v3.3.0qa39commit 81df001b3e...Vijay Bellur14 years v3.3.0qa38commit fdcbf065a9...Vijay Bellur14 years v3.3.0qa37commit 66fddb979d...Vijay Bellur14 years v3.3.0qa36