source: trunk/libtransmission/fdlimit.c @ 7800

Last change on this file since 7800 was 7800, checked in by charles, 13 years ago

(trunk libT) try again to make hudson-t happy wrt posix_fadvise()

  • Property svn:keywords set to Date Rev Author Id
File size: 15.4 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 7800 2009-01-25 23:47:07Z charles $
3 *
4 * Copyright (c) 2005-2008 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#ifndef WIN32
26 #define HAVE_GETRLIMIT
27#endif
28
29#ifdef HAVE_POSIX_FADVISE
30 #ifdef _XOPEN_SOURCE
31  #undef _XOPEN_SOURCE
32 #endif
33 #define _XOPEN_SOURCE 600
34#endif
35
36#include <assert.h>
37#include <errno.h>
38#include <inttypes.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#ifdef SYS_DARWIN
43 #include <fcntl.h>
44#endif
45
46#ifdef HAVE_FALLOCATE
47 #include <linux/falloc.h>
48#endif
49
50#include <sys/types.h>
51#include <sys/stat.h>
52#ifdef HAVE_GETRLIMIT
53 #include <sys/time.h> /* getrlimit */
54 #include <sys/resource.h> /* getrlimit */
55#endif
56#include <unistd.h>
57#include <fcntl.h> /* O_LARGEFILE posix_fadvise */
58
59#include <evutil.h>
60
61#include "transmission.h"
62#include "fdlimit.h"
63#include "list.h"
64#include "net.h"
65#include "platform.h" /* tr_lock */
66#include "utils.h"
67
68#define dbgmsg( ... ) \
69    do { \
70        if( tr_deepLoggingIsActive( ) ) \
71            tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
72    } while( 0 )
73
74/**
75***
76**/
77
78enum
79{
80    NOFILE_BUFFER = 512, /* the process' number of open files is
81                            globalMaxPeers + NOFILE_BUFFER */
82};
83
84struct tr_openfile
85{
86    tr_bool    isCheckedOut;
87    tr_bool    isWritable;
88    tr_bool    closeWhenDone;
89    char       filename[MAX_PATH_LENGTH];
90    int        fd;
91    uint64_t   date;
92};
93
94struct tr_fd_s
95{
96    int                   socketCount;
97    int                   socketLimit;
98
99    struct tr_openfile  * openFiles;
100    int                   openFileLimit;
101
102    tr_lock             * lock;
103};
104
105static struct tr_fd_s * gFd = NULL;
106
107/***
108****
109****  Local Files
110****
111***/
112
113#ifndef O_LARGEFILE
114 #define O_LARGEFILE 0
115#endif
116
117static tr_bool
118preallocateFileSparse( int fd, uint64_t length )
119{
120    const char zero = '\0';
121
122    if( length == 0 )
123        return TRUE;
124
125    if( lseek( fd, length-1, SEEK_SET ) == -1 )
126        return FALSE;
127    if( write( fd, &zero, 1 ) == -1 )
128        return FALSE;
129    if( ftruncate( fd, length ) == -1 )
130        return FALSE;
131
132    return TRUE;
133}
134
135static tr_bool
136preallocateFileFull( const char * filename, uint64_t length )
137{
138    tr_bool success = 0;
139
140#ifdef WIN32
141
142    HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 );
143    if( hFile != INVALID_HANDLE_VALUE )
144    {
145        LARGE_INTEGER li;
146        li.QuadPart = length;
147        success = SetFilePointerEx( hFile, li, NULL, FILE_BEGIN ) && SetEndOfFile( hFile );
148        CloseHandle( hFile );
149    }
150
151#else
152
153    int flags = O_RDWR | O_CREAT | O_LARGEFILE;
154    int fd = open( filename, flags, 0666 );
155    if( fd >= 0 )
156    {
157       
158# ifdef HAVE_FALLOCATE
159
160        success = !fallocate( fd, FALLOC_FL_KEEP_SIZE, 0, length );
161
162# elif defined(HAVE_POSIX_FALLOCATE)
163
164        success = !posix_fallocate( fd, 0, length );
165
166# elif defined(SYS_DARWIN)
167
168        fstore_t fst;
169        fst.fst_flags = F_ALLOCATECONTIG;
170        fst.fst_posmode = F_PEOFPOSMODE;
171        fst.fst_offset = 0;
172        fst.fst_length = length;
173        fst.fst_bytesalloc = 0;
174        success = !fcntl( fd, F_PREALLOCATE, &fst );
175
176# else
177
178        #warning no known method to preallocate files on this platform
179        success = 0;
180
181# endif
182
183        close( fd );
184    }
185
186#endif
187
188    return success;
189}
190
191/**
192 * returns 0 on success, or an errno value on failure.
193 * errno values include ENOENT if the parent folder doesn't exist,
194 * plus the errno values set by tr_mkdirp() and open().
195 */
196static int
197TrOpenFile( int                      i,
198            const char             * folder,
199            const char             * torrentFile,
200            tr_bool                  doWrite,
201            tr_preallocation_mode    preallocationMode,
202            uint64_t                 desiredFileSize )
203{
204    struct tr_openfile * file = &gFd->openFiles[i];
205    int                  flags;
206    char               * filename;
207    struct stat          sb;
208    int                  alreadyExisted;
209
210    /* confirm the parent folder exists */
211    if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
212    {
213        tr_err( _( "Couldn't create \"%1$s\": parent folder \"%2$s\" does not exist" ), torrentFile, folder );
214        return ENOENT;
215    }
216
217    /* create subfolders, if any */
218    filename = tr_buildPath( folder, torrentFile, NULL );
219    if( doWrite )
220    {
221        char * tmp = tr_dirname( filename );
222        const int err = tr_mkdirp( tmp, 0777 ) ? errno : 0;
223        if( err ) {
224            tr_err( _( "Couldn't create \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
225            tr_free( tmp );
226            tr_free( filename );
227            return err;
228        }
229        tr_free( tmp );
230    }
231
232    alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
233
234    if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_FULL ) )
235        if( preallocateFileFull( filename, desiredFileSize ) )
236            tr_inf( _( "Preallocated file \"%s\"" ), filename );
237   
238    /* open the file */
239    flags = doWrite ? ( O_RDWR | O_CREAT ) : O_RDONLY;
240#ifdef O_RANDOM
241    flags |= O_RANDOM
242#endif
243#ifdef O_LARGEFILE
244    flags |= O_LARGEFILE;
245#endif
246#ifdef WIN32
247    flags |= O_BINARY;
248#endif
249    file->fd = open( filename, flags, 0666 );
250    if( file->fd == -1 )
251    {
252        const int err = errno;
253        tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
254        tr_free( filename );
255        return err;
256    }
257
258    if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_SPARSE ) )
259        preallocateFileSparse( file->fd, desiredFileSize );
260
261#if defined( SYS_DARWIN )
262    fcntl( file->fd, F_NOCACHE, 1 );
263    fcntl( file->fd, F_RDAHEAD, 0 );
264#elif defined( HAVE_POSIX_FADVISE )
265    posix_fadvise( file->fd, 0, 0, POSIX_FADV_RANDOM );
266#endif
267
268    tr_free( filename );
269    return 0;
270}
271
272static int
273fileIsOpen( const struct tr_openfile * o )
274{
275    return o->fd >= 0;
276}
277
278static void
279TrCloseFile( int i )
280{
281    struct tr_openfile * o = &gFd->openFiles[i];
282
283    assert( i >= 0 );
284    assert( i < gFd->openFileLimit );
285    assert( fileIsOpen( o ) );
286
287    close( o->fd );
288    o->fd = -1;
289    o->isCheckedOut = 0;
290}
291
292static int
293fileIsCheckedOut( const struct tr_openfile * o )
294{
295    return fileIsOpen( o ) && o->isCheckedOut;
296}
297
298/* returns an fd on success, or a -1 on failure and sets errno */
299int
300tr_fdFileCheckout( const char             * folder,
301                   const char             * torrentFile,
302                   tr_bool                  doWrite,
303                   tr_preallocation_mode    preallocationMode,
304                   uint64_t                 desiredFileSize )
305{
306    int i, winner = -1;
307    struct tr_openfile * o;
308    char filename[MAX_PATH_LENGTH];
309
310    assert( folder && *folder );
311    assert( torrentFile && *torrentFile );
312    assert( doWrite == 0 || doWrite == 1 );
313
314    tr_snprintf( filename, sizeof( filename ), "%s%c%s", folder, TR_PATH_DELIMITER, torrentFile );
315    dbgmsg( "looking for file '%s', writable %c", filename, doWrite ? 'y' : 'n' );
316
317    tr_lockLock( gFd->lock );
318
319    /* Is it already open? */
320    for( i = 0; i < gFd->openFileLimit; ++i )
321    {
322        o = &gFd->openFiles[i];
323
324        if( !fileIsOpen( o ) )
325            continue;
326
327        if( strcmp( filename, o->filename ) )
328            continue;
329
330        if( fileIsCheckedOut( o ) )
331        {
332            dbgmsg( "found it!  it's open, but checked out.  waiting..." );
333            tr_lockUnlock( gFd->lock );
334            tr_wait( 200 );
335            tr_lockLock( gFd->lock );
336            i = -1; /* reloop */
337            continue;
338        }
339
340        if( doWrite && !o->isWritable )
341        {
342            dbgmsg(
343                "found it!  it's open and available, but isn't writable. closing..." );
344            TrCloseFile( i );
345            break;
346        }
347
348        dbgmsg( "found it!  it's ready for use!" );
349        winner = i;
350        break;
351    }
352
353    dbgmsg(
354        "it's not already open.  looking for an open slot or an old file." );
355    while( winner < 0 )
356    {
357        uint64_t date = tr_date( ) + 1;
358
359        /* look for the file that's been open longest */
360        for( i = 0; i < gFd->openFileLimit; ++i )
361        {
362            o = &gFd->openFiles[i];
363
364            if( !fileIsOpen( o ) )
365            {
366                winner = i;
367                dbgmsg( "found an empty slot in %d", winner );
368                break;
369            }
370
371            if( date > o->date )
372            {
373                date = o->date;
374                winner = i;
375            }
376        }
377
378        if( winner >= 0 )
379        {
380            if( fileIsOpen( &gFd->openFiles[winner] ) )
381            {
382                dbgmsg( "closing file '%s', slot #%d",
383                        gFd->openFiles[winner].filename,
384                        winner );
385                TrCloseFile( winner );
386            }
387        }
388        else
389        {
390            dbgmsg(
391                "everything's full!  waiting for someone else to finish something" );
392            tr_lockUnlock( gFd->lock );
393            tr_wait( 200 );
394            tr_lockLock( gFd->lock );
395        }
396    }
397
398    assert( winner >= 0 );
399    o = &gFd->openFiles[winner];
400    if( !fileIsOpen( o ) )
401    {
402        const int err = TrOpenFile( winner, folder, torrentFile, doWrite, preallocationMode, desiredFileSize );
403        if( err ) {
404            tr_lockUnlock( gFd->lock );
405            errno = err;
406            return -1;
407        }
408
409        dbgmsg( "opened '%s' in slot %d, doWrite %c", filename, winner,
410                doWrite ? 'y' : 'n' );
411        tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
412        o->isWritable = doWrite;
413    }
414
415    dbgmsg( "checking out '%s' in slot %d", filename, winner );
416    o->isCheckedOut = 1;
417    o->closeWhenDone = 0;
418    o->date = tr_date( );
419    tr_lockUnlock( gFd->lock );
420    return o->fd;
421}
422
423void
424tr_fdFileReturn( int fd )
425{
426    int i;
427
428    tr_lockLock( gFd->lock );
429
430    for( i = 0; i < gFd->openFileLimit; ++i )
431    {
432        struct tr_openfile * o = &gFd->openFiles[i];
433        if( o->fd != fd )
434            continue;
435
436        dbgmsg( "releasing file '%s' in slot #%d", o->filename, i );
437        o->isCheckedOut = 0;
438        if( o->closeWhenDone )
439            TrCloseFile( i );
440
441        break;
442    }
443
444    tr_lockUnlock( gFd->lock );
445}
446
447void
448tr_fdFileClose( const char * filename )
449{
450    int i;
451
452    tr_lockLock( gFd->lock );
453
454    for( i = 0; i < gFd->openFileLimit; ++i )
455    {
456        struct tr_openfile * o = &gFd->openFiles[i];
457        if( !fileIsOpen( o ) || strcmp( filename, o->filename ) )
458            continue;
459
460        dbgmsg( "tr_fdFileClose closing '%s'", filename );
461
462        if( !o->isCheckedOut )
463        {
464            dbgmsg( "not checked out, so closing it now... '%s'", filename );
465            TrCloseFile( i );
466        }
467        else
468        {
469            dbgmsg(
470                "flagging file '%s', slot #%d to be closed when checked in",
471                gFd->openFiles[i].filename, i );
472            o->closeWhenDone = 1;
473        }
474    }
475
476    tr_lockUnlock( gFd->lock );
477}
478
479/***
480****
481****  Sockets
482****
483***/
484
485static int
486getSocketMax( struct tr_fd_s * gFd )
487{
488    return gFd->socketLimit;
489}
490
491int
492tr_fdSocketCreate( int domain, int type )
493{
494    int s = -1;
495
496    tr_lockLock( gFd->lock );
497
498    if( gFd->socketCount < getSocketMax( gFd ) )
499        if( ( s = socket( domain, type, 0 ) ) < 0 )
500        {
501#ifdef SYS_DARWIN
502            if( sockerrno != EAFNOSUPPORT )
503#endif
504            tr_err( _( "Couldn't create socket: %s" ),
505                   tr_strerror( sockerrno ) );
506            s = -sockerrno;
507        }
508
509    if( s > -1 )
510        ++gFd->socketCount;
511
512    assert( gFd->socketCount >= 0 );
513
514    tr_lockUnlock( gFd->lock );
515    return s;
516}
517
518int
519tr_fdSocketAccept( int           b,
520                   tr_address  * addr,
521                   tr_port     * port )
522{
523    int                s = -1;
524    unsigned int       len;
525    struct sockaddr_storage sock;
526
527    assert( addr );
528    assert( port );
529
530    tr_lockLock( gFd->lock );
531    if( gFd->socketCount < getSocketMax( gFd ) )
532    {
533        len = sizeof( struct sockaddr_storage );
534        s = accept( b, (struct sockaddr *) &sock, &len );
535    }
536    if( s > -1 )
537    {
538        /* "The ss_family field of the sockaddr_storage structure will always
539         * align with the family field of any protocol-specific structure." */ 
540        if( sock.ss_family == AF_INET ) 
541        { 
542            struct sockaddr_in * sock4 = (struct sockaddr_in *)&sock; 
543            addr->type = TR_AF_INET; 
544            addr->addr.addr4.s_addr = sock4->sin_addr.s_addr; 
545            *port = sock4->sin_port; 
546        } 
547        else 
548        { 
549            struct sockaddr_in6 * sock6 = (struct sockaddr_in6 *)&sock; 
550            addr->type = TR_AF_INET6; 
551            addr->addr.addr6 = sock6->sin6_addr;
552            *port = sock6->sin6_port; 
553        } 
554        ++gFd->socketCount;
555    }
556    tr_lockUnlock( gFd->lock );
557
558    return s;
559}
560
561static void
562socketClose( int fd )
563{
564    EVUTIL_CLOSESOCKET( fd );
565}
566
567void
568tr_fdSocketClose( int s )
569{
570    tr_lockLock( gFd->lock );
571
572    if( s >= 0 )
573    {
574        socketClose( s );
575        --gFd->socketCount;
576    }
577
578    assert( gFd->socketCount >= 0 );
579
580    tr_lockUnlock( gFd->lock );
581}
582
583/***
584****
585****  Startup / Shutdown
586****
587***/
588
589void
590tr_fdInit( size_t openFileLimit, size_t socketLimit )
591{
592    int i;
593
594    assert( gFd == NULL );
595    gFd = tr_new0( struct tr_fd_s, 1 );
596    gFd->openFiles = tr_new0( struct tr_openfile, openFileLimit );
597    gFd->openFileLimit = openFileLimit;
598    gFd->lock = tr_lockNew( );
599
600#ifdef HAVE_GETRLIMIT
601    {
602        struct rlimit rlim;
603        getrlimit( RLIMIT_NOFILE, &rlim );
604        rlim.rlim_cur = MIN( rlim.rlim_max,
605                            (rlim_t)( socketLimit + NOFILE_BUFFER ) );
606        setrlimit( RLIMIT_NOFILE, &rlim );
607        gFd->socketLimit = rlim.rlim_cur - NOFILE_BUFFER;
608        tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
609    }
610#else
611    gFd->socketLimit = socketLimit;
612#endif
613    tr_dbg( "%zu usable file descriptors", socketLimit );
614
615    for( i = 0; i < gFd->openFileLimit; ++i )
616        gFd->openFiles[i].fd = -1;
617}
618
619void
620tr_fdClose( void )
621{
622    int i = 0;
623
624    for( i = 0; i < gFd->openFileLimit; ++i )
625        if( fileIsOpen( &gFd->openFiles[i] ) )
626            TrCloseFile( i );
627
628    tr_lockFree( gFd->lock );
629
630    tr_free( gFd->openFiles );
631    tr_free( gFd );
632    gFd = NULL;
633}
634
635void
636tr_fdSetPeerLimit( uint16_t n )
637{
638    assert( gFd != NULL && "tr_fdInit() must be called first!" );
639    gFd->socketLimit = n;
640}
641
642uint16_t
643tr_fdGetPeerLimit( void )
644{
645    return gFd ? gFd->socketLimit : -1;
646}
647
Note: See TracBrowser for help on using the repository browser.