source: trunk/libtransmission/fdlimit.c @ 7798

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

(trunk libT) add mac, linux, and win32 support for os-level hints that local data will be read in random order, to disable readahead buffering.

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