source: trunk/libtransmission/fdlimit.c @ 9339

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

(trunk libT) #1483: fix bug introduced in r9328

  • Property svn:keywords set to Date Rev Author Id
File size: 17.7 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 9339 2009-10-20 20:19:41Z 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_XFS_XFS_H
47 #include <xfs/xfs.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" /* MAX_PATH_LENGTH, TR_PATH_DELIMITER */
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          isWritable;
87    int              torrentId;
88    tr_file_index_t  fileNum;
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    int                   openFileLimit;
99    struct tr_openfile  * openFiles;
100};
101
102static struct tr_fd_s * gFd = NULL;
103
104/***
105****
106****  Local Files
107****
108***/
109
110#ifndef O_LARGEFILE
111 #define O_LARGEFILE 0
112#endif
113
114static tr_bool
115preallocateFileSparse( int fd, uint64_t length )
116{
117    const char zero = '\0';
118
119    if( length == 0 )
120        return TRUE;
121
122    if( lseek( fd, length-1, SEEK_SET ) == -1 )
123        return FALSE;
124    if( write( fd, &zero, 1 ) == -1 )
125        return FALSE;
126    if( ftruncate( fd, length ) == -1 )
127        return FALSE;
128
129    return TRUE;
130}
131
132static tr_bool
133preallocateFileFull( const char * filename, uint64_t length )
134{
135    tr_bool success = 0;
136
137#ifdef WIN32
138
139    HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 );
140    if( hFile != INVALID_HANDLE_VALUE )
141    {
142        LARGE_INTEGER li;
143        li.QuadPart = length;
144        success = SetFilePointerEx( hFile, li, NULL, FILE_BEGIN ) && SetEndOfFile( hFile );
145        CloseHandle( hFile );
146    }
147
148#else
149
150    int flags = O_RDWR | O_CREAT | O_LARGEFILE;
151    int fd = open( filename, flags, 0666 );
152    if( fd >= 0 )
153    {
154# ifdef HAVE_XFS_XFS_H
155        if( !success && platform_test_xfs_fd( fd ) )
156        {
157            xfs_flock64_t fl;
158            fl.l_whence = 0;
159            fl.l_start = 0;
160            fl.l_len = length;
161            success = !xfsctl( NULL, fd, XFS_IOC_RESVSP64, &fl );
162        }
163# endif
164# ifdef SYS_DARWIN
165        if( !success )
166        {
167            fstore_t fst;
168            fst.fst_flags = F_ALLOCATECONTIG;
169            fst.fst_posmode = F_PEOFPOSMODE;
170            fst.fst_offset = 0;
171            fst.fst_length = length;
172            fst.fst_bytesalloc = 0;
173            success = !fcntl( fd, F_PREALLOCATE, &fst );
174        }
175# endif
176# ifdef HAVE_POSIX_FALLOCATE
177        if( !success )
178        {
179            success = !posix_fallocate( fd, 0, length );
180        }
181# endif
182
183        if( !success ) /* if nothing else works, do it the old-fashioned way */
184        {
185            uint8_t buf[ 4096 ];
186            memset( buf, 0, sizeof( buf ) );
187            success = TRUE;
188            while ( success && ( length > 0 ) )
189            {
190                const int thisPass = MIN( length, sizeof( buf ) );
191                success = write( fd, buf, thisPass ) == thisPass;
192                length -= thisPass;
193            }
194        }
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    tr_bool              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\": \"%2$s\" is not a folder" ), 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 the file already exists and it's too large, truncate it.
342     * This is a fringe case that happens if a torrent's been updated
343     * and one of the updated torrent's files is smaller.
344     * http://trac.transmissionbt.com/ticket/2228
345     * https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/318249
346     */
347    if( alreadyExisted && ( desiredFileSize < (uint64_t)sb.st_size ) )
348        ftruncate( file->fd, desiredFileSize );
349
350    if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_SPARSE ) )
351        preallocateFileSparse( file->fd, desiredFileSize );
352
353#ifdef HAVE_POSIX_FADVISE
354    /* this doubles the OS level readahead buffer, which in practice
355     * turns out to be a good thing, because many (most?) clients request
356     * chunks of blocks in order.
357     * It's okay for this to fail silently, so don't let it affect errno */
358    {
359        const int err = errno;
360        posix_fadvise( file->fd, 0, 0, POSIX_FADV_SEQUENTIAL );
361        errno = err;
362    }
363#endif
364
365    tr_free( filename );
366    return 0;
367}
368
369static TR_INLINE tr_bool
370fileIsOpen( const struct tr_openfile * o )
371{
372    return o->fd >= 0;
373}
374
375static void
376TrCloseFile( struct tr_openfile * o )
377{
378    assert( o != NULL );
379    assert( fileIsOpen( o ) );
380
381    tr_close_file( o->fd );
382    o->fd = -1;
383}
384
385int
386tr_fdFileGetCached( int              torrentId,
387                    tr_file_index_t  fileNum,
388                    tr_bool          doWrite )
389{
390    struct tr_openfile * match = NULL;
391
392    assert( torrentId > 0 );
393    assert( tr_isBool( doWrite ) );
394
395    /* is it already open? */
396    {
397        int i;
398        struct tr_openfile * o;
399        for( i=0; i<gFd->openFileLimit; ++i )
400        {
401            o = &gFd->openFiles[i];
402
403            if( torrentId != o->torrentId )
404                continue;
405            if( fileNum != o->fileNum )
406                continue;
407            if( !fileIsOpen( o ) )
408                continue;
409
410            match = o;
411            break;
412        }
413    }
414
415    if( ( match != NULL ) && ( !doWrite || match->isWritable ) )
416    {
417        match->date = tr_date( );
418        return match->fd;
419    }
420
421    return -1;
422}
423
424/* returns an fd on success, or a -1 on failure and sets errno */
425int
426tr_fdFileCheckout( int                      torrentId,
427                   tr_file_index_t          fileNum,
428                   const char             * folder,
429                   const char             * torrentFile,
430                   tr_bool                  doWrite,
431                   tr_preallocation_mode    preallocationMode,
432                   uint64_t                 desiredFileSize )
433{
434    int i, winner = -1;
435    struct tr_openfile * o;
436    char filename[MAX_PATH_LENGTH];
437
438    assert( torrentId > 0 );
439    assert( folder && *folder );
440    assert( torrentFile && *torrentFile );
441    assert( tr_isBool( doWrite ) );
442
443    tr_snprintf( filename, sizeof( filename ), "%s%c%s", folder, TR_PATH_DELIMITER, torrentFile );
444    dbgmsg( "looking for file '%s', writable %c", filename, doWrite ? 'y' : 'n' );
445
446    /* is it already open? */
447    for( i=0; i<gFd->openFileLimit; ++i )
448    {
449        o = &gFd->openFiles[i];
450
451        if( torrentId != o->torrentId )
452            continue;
453        if( fileNum != o->fileNum )
454            continue;
455        if( !fileIsOpen( o ) )
456            continue;
457
458        if( doWrite && !o->isWritable )
459        {
460            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
461            TrCloseFile( o );
462            break;
463        }
464
465        dbgmsg( "found it!  it's ready for use!" );
466        winner = i;
467        break;
468    }
469
470    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
471    while( winner < 0 )
472    {
473        uint64_t date = tr_date( ) + 1;
474
475        /* look for the file that's been open longest */
476        for( i=0; i<gFd->openFileLimit; ++i )
477        {
478            o = &gFd->openFiles[i];
479
480            if( !fileIsOpen( o ) )
481            {
482                winner = i;
483                dbgmsg( "found an empty slot in %d", winner );
484                break;
485            }
486
487            if( date > o->date )
488            {
489                date = o->date;
490                winner = i;
491            }
492        }
493
494        assert( winner >= 0 );
495
496        if( fileIsOpen( &gFd->openFiles[winner] ) )
497        {
498            dbgmsg( "closing file \"%s\"", gFd->openFiles[winner].filename );
499            TrCloseFile( &gFd->openFiles[winner] );
500        }
501    }
502
503    assert( winner >= 0 );
504    o = &gFd->openFiles[winner];
505    if( !fileIsOpen( o ) )
506    {
507        const int err = TrOpenFile( winner, folder, torrentFile, doWrite,
508                                    preallocationMode, desiredFileSize );
509        if( err ) {
510            errno = err;
511            return -1;
512        }
513
514        dbgmsg( "opened '%s' in slot %d, doWrite %c", filename, winner,
515                doWrite ? 'y' : 'n' );
516        tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
517        o->isWritable = doWrite;
518    }
519
520    dbgmsg( "checking out '%s' in slot %d", filename, winner );
521    o->torrentId = torrentId;
522    o->fileNum = fileNum;
523    o->date = tr_date( );
524    return o->fd;
525}
526
527void
528tr_fdFileClose( const tr_torrent * tor, tr_file_index_t fileNum )
529{
530    struct tr_openfile * o;
531    const struct tr_openfile * end;
532    const int torrentId = tr_torrentId( tor );
533    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
534    {
535        if( torrentId != o->torrentId )
536            continue;
537        if( fileNum != o->fileNum )
538            continue;
539        if( !fileIsOpen( o ) )
540            continue;
541
542        dbgmsg( "tr_fdFileClose closing \"%s\"", o->filename );
543        TrCloseFile( o );
544    }
545}
546
547void
548tr_fdTorrentClose( int torrentId )
549{
550    struct tr_openfile * o;
551    const struct tr_openfile * end;
552
553    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
554        if( fileIsOpen( o ) && ( o->torrentId == torrentId ) )
555            TrCloseFile( o );
556}
557
558/***
559****
560****  Sockets
561****
562***/
563
564static TR_INLINE 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    if( gFd->socketCount < getSocketMax( gFd ) )
576        if( ( s = socket( domain, type, 0 ) ) < 0 )
577        {
578            if( sockerrno != EAFNOSUPPORT )
579                tr_err( _( "Couldn't create socket: %s" ),
580                        tr_strerror( sockerrno ) );
581        }
582
583    if( s > -1 )
584        ++gFd->socketCount;
585
586    assert( gFd->socketCount >= 0 );
587
588    return s;
589}
590
591int
592tr_fdSocketAccept( int           b,
593                   tr_address  * addr,
594                   tr_port     * port )
595{
596    int s;
597    unsigned int len;
598    struct sockaddr_storage sock;
599
600    assert( addr );
601    assert( port );
602
603    len = sizeof( struct sockaddr_storage );
604    s = accept( b, (struct sockaddr *) &sock, &len );
605
606    if( ( s >= 0 ) && gFd->socketCount > getSocketMax( gFd ) )
607    {
608        EVUTIL_CLOSESOCKET( s );
609        s = -1;
610    }
611
612    if( s >= 0 )
613    {
614        /* "The ss_family field of the sockaddr_storage structure will always
615         * align with the family field of any protocol-specific structure." */
616        if( sock.ss_family == AF_INET )
617        {
618            struct sockaddr_in *si;
619            union { struct sockaddr_storage dummy; struct sockaddr_in si; } s;
620            s.dummy = sock;
621            si = &s.si;
622            addr->type = TR_AF_INET;
623            addr->addr.addr4.s_addr = si->sin_addr.s_addr;
624            *port = si->sin_port;
625        }
626        else
627        {
628            struct sockaddr_in6 *si;
629            union { struct sockaddr_storage dummy; struct sockaddr_in6 si; } s;
630            s.dummy = sock;
631            si = &s.si;
632            addr->type = TR_AF_INET6;
633            addr->addr.addr6 = si->sin6_addr;
634            *port = si->sin6_port;
635        }
636        ++gFd->socketCount;
637    }
638
639    return s;
640}
641
642void
643tr_fdSocketClose( int fd )
644{
645    if( fd >= 0 )
646    {
647        EVUTIL_CLOSESOCKET( fd );
648        --gFd->socketCount;
649    }
650
651    assert( gFd->socketCount >= 0 );
652}
653
654/***
655****
656****  Startup / Shutdown
657****
658***/
659
660void
661tr_fdInit( size_t openFileLimit, size_t socketLimit )
662{
663    int i;
664
665    assert( gFd == NULL );
666    gFd = tr_new0( struct tr_fd_s, 1 );
667    gFd->openFiles = tr_new0( struct tr_openfile, openFileLimit );
668    gFd->openFileLimit = openFileLimit;
669
670#ifdef HAVE_GETRLIMIT
671    {
672        struct rlimit rlim;
673        getrlimit( RLIMIT_NOFILE, &rlim );
674        rlim.rlim_cur = MIN( rlim.rlim_max,
675                            (rlim_t)( socketLimit + NOFILE_BUFFER ) );
676        setrlimit( RLIMIT_NOFILE, &rlim );
677        gFd->socketLimit = rlim.rlim_cur - NOFILE_BUFFER;
678        tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
679    }
680#else
681    gFd->socketLimit = socketLimit;
682#endif
683    tr_dbg( "%zu usable file descriptors", socketLimit );
684
685    for( i = 0; i < gFd->openFileLimit; ++i )
686        gFd->openFiles[i].fd = -1;
687}
688
689void
690tr_fdClose( void )
691{
692    struct tr_openfile * o;
693    const struct tr_openfile * end;
694
695    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
696        if( fileIsOpen( o ) )
697            TrCloseFile( o );
698
699    tr_free( gFd->openFiles );
700    tr_free( gFd );
701    gFd = NULL;
702}
703
704void
705tr_fdSetPeerLimit( uint16_t n )
706{
707    assert( gFd != NULL && "tr_fdInit() must be called first!" );
708    gFd->socketLimit = n;
709}
710
711uint16_t
712tr_fdGetPeerLimit( void )
713{
714    return gFd ? gFd->socketLimit : -1;
715}
Note: See TracBrowser for help on using the repository browser.