source: trunk/libtransmission/fdlimit.c @ 8726

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

(trunk libT) #2228: transmission should learn to truncate files on updating torrents

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