source: trunk/libtransmission/fdlimit.c @ 8654

Last change on this file since 8654 was 8654, checked in by charles, 12 years ago

(trunk libT) fix copy/paste error in tr_fdAccept()

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