Transmission: update to 2.82
[tomato.git] / release / src / router / transmission / libtransmission / fdlimit.c
blob389f804dbbd4fe0e01c61b5c90516a0040e8863a
1 /*
2 * This file Copyright (C) Mnemosyne LLC
4 * This file is licensed by the GPL version 2. Works owned by the
5 * Transmission project are granted a special exemption to clause 2 (b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
10 * $Id: fdlimit.c 14147 2013-07-27 16:18:12Z jordan $
13 #ifdef HAVE_POSIX_FADVISE
14 #ifdef _XOPEN_SOURCE
15 #undef _XOPEN_SOURCE
16 #endif
17 #define _XOPEN_SOURCE 600
18 #endif
20 #include <assert.h>
21 #include <errno.h>
22 #include <inttypes.h>
23 #include <string.h>
24 #ifdef SYS_DARWIN
25 #include <fcntl.h>
26 #endif
28 #ifdef HAVE_FALLOCATE64
29 /* FIXME can't find the right #include voodoo to pick up the declaration.. */
30 extern int fallocate64 (int fd, int mode, uint64_t offset, uint64_t len);
31 #endif
33 #ifdef HAVE_XFS_XFS_H
34 #include <xfs/xfs.h>
35 #endif
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/time.h> /* getrlimit */
40 #include <sys/resource.h> /* getrlimit */
41 #include <fcntl.h> /* O_LARGEFILE posix_fadvise */
42 #include <unistd.h> /* lseek (), write (), ftruncate (), pread (), pwrite (), etc */
44 #include "transmission.h"
45 #include "fdlimit.h"
46 #include "log.h"
47 #include "session.h"
48 #include "torrent.h" /* tr_isTorrent () */
50 #define dbgmsg(...) \
51 do \
52 { \
53 if (tr_logGetDeepEnabled ()) \
54 tr_logAddDeep (__FILE__, __LINE__, NULL, __VA_ARGS__); \
55 } \
56 while (0)
58 /***
59 ****
60 **** Local Files
61 ****
62 ***/
64 #ifndef O_LARGEFILE
65 #define O_LARGEFILE 0
66 #endif
68 #ifndef O_BINARY
69 #define O_BINARY 0
70 #endif
72 #ifndef O_SEQUENTIAL
73 #define O_SEQUENTIAL 0
74 #endif
77 static bool
78 preallocate_file_sparse (int fd, uint64_t length)
80 const char zero = '\0';
81 bool success = 0;
83 if (!length)
84 success = true;
86 #ifdef HAVE_FALLOCATE64
87 if (!success) /* fallocate64 is always preferred, so try it first */
88 success = !fallocate64 (fd, 0, 0, length);
89 #endif
91 if (!success) /* fallback: the old-style seek-and-write */
92 success = (lseek (fd, length-1, SEEK_SET) != -1)
93 && (write (fd, &zero, 1) != -1)
94 && (ftruncate (fd, length) != -1);
96 return success;
99 static bool
100 preallocate_file_full (const char * filename, uint64_t length)
102 bool success = 0;
104 #ifdef WIN32
106 HANDLE hFile = CreateFile (filename, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
107 if (hFile != INVALID_HANDLE_VALUE)
109 LARGE_INTEGER li;
110 li.QuadPart = length;
111 success = SetFilePointerEx (hFile, li, NULL, FILE_BEGIN) && SetEndOfFile (hFile);
112 CloseHandle (hFile);
115 #else
117 int flags = O_RDWR | O_CREAT | O_LARGEFILE;
118 int fd = open (filename, flags, 0666);
119 if (fd >= 0)
121 # ifdef HAVE_FALLOCATE64
122 if (!success)
123 success = !fallocate64 (fd, 0, 0, length);
124 # endif
125 # ifdef HAVE_XFS_XFS_H
126 if (!success && platform_test_xfs_fd (fd))
128 xfs_flock64_t fl;
129 fl.l_whence = 0;
130 fl.l_start = 0;
131 fl.l_len = length;
132 success = !xfsctl (NULL, fd, XFS_IOC_RESVSP64, &fl);
134 # endif
135 # ifdef SYS_DARWIN
136 if (!success)
138 fstore_t fst;
139 fst.fst_flags = F_ALLOCATECONTIG;
140 fst.fst_posmode = F_PEOFPOSMODE;
141 fst.fst_offset = 0;
142 fst.fst_length = length;
143 fst.fst_bytesalloc = 0;
144 success = !fcntl (fd, F_PREALLOCATE, &fst);
146 # endif
147 # ifdef HAVE_POSIX_FALLOCATE
148 if (!success)
149 success = !posix_fallocate (fd, 0, length);
150 # endif
152 if (!success) /* if nothing else works, do it the old-fashioned way */
154 uint8_t buf[ 4096 ];
155 memset (buf, 0, sizeof (buf));
156 success = true;
157 while (success && (length > 0))
159 const int thisPass = MIN (length, sizeof (buf));
160 success = write (fd, buf, thisPass) == thisPass;
161 length -= thisPass;
165 close (fd);
168 #endif
170 return success;
174 /* portability wrapper for fsync (). */
176 tr_fsync (int fd)
178 #ifdef WIN32
179 return _commit (fd);
180 #else
181 return fsync (fd);
182 #endif
186 /* Like pread and pwrite, except that the position is undefined afterwards.
187 And of course they are not thread-safe. */
189 /* don't use pread/pwrite on old versions of uClibc because they're buggy.
190 * https://trac.transmissionbt.com/ticket/3826 */
191 #ifdef __UCLIBC__
192 #define TR_UCLIBC_CHECK_VERSION(major,minor,micro) \
193 (__UCLIBC_MAJOR__ > (major) || \
194 (__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ > (minor)) || \
195 (__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ == (minor) && \
196 __UCLIBC_SUBLEVEL__ >= (micro)))
197 #if !TR_UCLIBC_CHECK_VERSION (0,9,28)
198 #undef HAVE_PREAD
199 #undef HAVE_PWRITE
200 #endif
201 #endif
203 #ifdef SYS_DARWIN
204 #define HAVE_PREAD
205 #define HAVE_PWRITE
206 #endif
208 ssize_t
209 tr_pread (int fd, void *buf, size_t count, off_t offset)
211 #ifdef HAVE_PREAD
212 return pread (fd, buf, count, offset);
213 #else
214 const off_t lrc = lseek (fd, offset, SEEK_SET);
215 if (lrc < 0)
216 return -1;
217 return read (fd, buf, count);
218 #endif
221 ssize_t
222 tr_pwrite (int fd, const void *buf, size_t count, off_t offset)
224 #ifdef HAVE_PWRITE
225 return pwrite (fd, buf, count, offset);
226 #else
227 const off_t lrc = lseek (fd, offset, SEEK_SET);
228 if (lrc < 0)
229 return -1;
230 return write (fd, buf, count);
231 #endif
235 tr_prefetch (int fd UNUSED, off_t offset UNUSED, size_t count UNUSED)
237 #ifdef HAVE_POSIX_FADVISE
238 return posix_fadvise (fd, offset, count, POSIX_FADV_WILLNEED);
239 #elif defined (SYS_DARWIN)
240 struct radvisory radv;
241 radv.ra_offset = offset;
242 radv.ra_count = count;
243 return fcntl (fd, F_RDADVISE, &radv);
244 #else
245 return 0;
246 #endif
249 void
250 tr_set_file_for_single_pass (int fd)
252 if (fd >= 0)
254 /* Set hints about the lookahead buffer and caching. It's okay
255 for these to fail silently, so don't let them affect errno */
256 const int err = errno;
257 #ifdef HAVE_POSIX_FADVISE
258 posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
259 #endif
260 #ifdef SYS_DARWIN
261 fcntl (fd, F_RDAHEAD, 1);
262 fcntl (fd, F_NOCACHE, 1);
263 #endif
264 errno = err;
268 static int
269 open_local_file (const char * filename, int flags)
271 const int fd = open (filename, flags, 0666);
272 tr_set_file_for_single_pass (fd);
273 return fd;
276 tr_open_file_for_writing (const char * filename)
278 return open_local_file (filename, O_LARGEFILE|O_BINARY|O_CREAT|O_WRONLY);
281 tr_open_file_for_scanning (const char * filename)
283 return open_local_file (filename, O_LARGEFILE|O_BINARY|O_SEQUENTIAL|O_RDONLY);
286 void
287 tr_close_file (int fd)
289 close (fd);
292 /*****
293 ******
294 ******
295 ******
296 *****/
298 struct tr_cached_file
300 bool is_writable;
301 int fd;
302 int torrent_id;
303 tr_file_index_t file_index;
304 time_t used_at;
307 static inline bool
308 cached_file_is_open (const struct tr_cached_file * o)
310 assert (o != NULL);
312 return o->fd >= 0;
315 static void
316 cached_file_close (struct tr_cached_file * o)
318 assert (cached_file_is_open (o));
320 tr_close_file (o->fd);
321 o->fd = -1;
325 * returns 0 on success, or an errno value on failure.
326 * errno values include ENOENT if the parent folder doesn't exist,
327 * plus the errno values set by tr_mkdirp () and open ().
329 static int
330 cached_file_open (struct tr_cached_file * o,
331 const char * filename,
332 bool writable,
333 tr_preallocation_mode allocation,
334 uint64_t file_size)
336 int flags;
337 struct stat sb;
338 bool already_existed;
339 bool resize_needed;
341 /* create subfolders, if any */
342 if (writable)
344 char * dir = tr_dirname (filename);
345 const int err = tr_mkdirp (dir, 0777) ? errno : 0;
346 if (err)
348 tr_logAddError (_("Couldn't create \"%1$s\": %2$s"), dir, tr_strerror (err));
349 tr_free (dir);
350 return err;
352 tr_free (dir);
355 already_existed = !stat (filename, &sb) && S_ISREG (sb.st_mode);
357 if (writable && !already_existed && (allocation == TR_PREALLOCATE_FULL))
358 if (preallocate_file_full (filename, file_size))
359 tr_logAddDebug ("Preallocated file \"%s\"", filename);
361 /* we can't resize the file w/o write permissions */
362 resize_needed = already_existed && (file_size < (uint64_t)sb.st_size);
363 writable |= resize_needed;
365 /* open the file */
366 flags = writable ? (O_RDWR | O_CREAT) : O_RDONLY;
367 flags |= O_LARGEFILE | O_BINARY | O_SEQUENTIAL;
368 o->fd = open (filename, flags, 0666);
370 if (o->fd == -1)
372 const int err = errno;
373 tr_logAddError (_("Couldn't open \"%1$s\": %2$s"), filename, tr_strerror (err));
374 return err;
377 /* If the file already exists and it's too large, truncate it.
378 * This is a fringe case that happens if a torrent's been updated
379 * and one of the updated torrent's files is smaller.
380 * http://trac.transmissionbt.com/ticket/2228
381 * https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/318249
383 if (resize_needed && (ftruncate (o->fd, file_size) == -1))
385 const int err = errno;
386 tr_logAddError (_("Couldn't truncate \"%1$s\": %2$s"), filename, tr_strerror (err));
387 return err;
390 if (writable && !already_existed && (allocation == TR_PREALLOCATE_SPARSE))
391 preallocate_file_sparse (o->fd, file_size);
393 /* Many (most?) clients request blocks in ascending order,
394 * so increase the readahead buffer.
395 * Also, disable OS-level caching because "inactive memory" angers users. */
396 tr_set_file_for_single_pass (o->fd);
398 return 0;
401 /***
402 ****
403 ***/
405 struct tr_fileset
407 struct tr_cached_file * begin;
408 const struct tr_cached_file * end;
411 static void
412 fileset_construct (struct tr_fileset * set, int n)
414 struct tr_cached_file * o;
415 const struct tr_cached_file TR_CACHED_FILE_INIT = { 0, -1, 0, 0, 0 };
417 set->begin = tr_new (struct tr_cached_file, n);
418 set->end = set->begin + n;
420 for (o=set->begin; o!=set->end; ++o)
421 *o = TR_CACHED_FILE_INIT;
424 static void
425 fileset_close_all (struct tr_fileset * set)
427 struct tr_cached_file * o;
429 if (set != NULL)
430 for (o=set->begin; o!=set->end; ++o)
431 if (cached_file_is_open (o))
432 cached_file_close (o);
435 static void
436 fileset_destruct (struct tr_fileset * set)
438 fileset_close_all (set);
439 tr_free (set->begin);
440 set->end = set->begin = NULL;
443 static void
444 fileset_close_torrent (struct tr_fileset * set, int torrent_id)
446 struct tr_cached_file * o;
448 if (set != NULL)
449 for (o=set->begin; o!=set->end; ++o)
450 if ((o->torrent_id == torrent_id) && cached_file_is_open (o))
451 cached_file_close (o);
454 static struct tr_cached_file *
455 fileset_lookup (struct tr_fileset * set, int torrent_id, tr_file_index_t i)
457 struct tr_cached_file * o;
459 if (set != NULL)
460 for (o=set->begin; o!=set->end; ++o)
461 if ((torrent_id == o->torrent_id) && (i == o->file_index) && cached_file_is_open (o))
462 return o;
464 return NULL;
467 static struct tr_cached_file *
468 fileset_get_empty_slot (struct tr_fileset * set)
470 struct tr_cached_file * cull = NULL;
472 if (set->begin != NULL)
474 struct tr_cached_file * o;
476 /* try to find an unused slot */
477 for (o=set->begin; o!=set->end; ++o)
478 if (!cached_file_is_open (o))
479 return o;
481 /* all slots are full... recycle the least recently used */
482 for (cull=NULL, o=set->begin; o!=set->end; ++o)
483 if (!cull || o->used_at < cull->used_at)
484 cull = o;
486 cached_file_close (cull);
489 return cull;
492 /***
493 ****
494 **** Startup / Shutdown
495 ****
496 ***/
498 struct tr_fdInfo
500 int peerCount;
501 struct tr_fileset fileset;
504 static void
505 ensureSessionFdInfoExists (tr_session * session)
507 assert (tr_isSession (session));
509 if (session->fdInfo == NULL)
511 struct rlimit limit;
512 struct tr_fdInfo * i;
513 const int FILE_CACHE_SIZE = 32;
515 /* Create the local file cache */
516 i = tr_new0 (struct tr_fdInfo, 1);
517 fileset_construct (&i->fileset, FILE_CACHE_SIZE);
518 session->fdInfo = i;
520 /* set the open-file limit to the largest safe size wrt FD_SETSIZE */
521 if (!getrlimit (RLIMIT_NOFILE, &limit))
523 const int old_limit = (int) limit.rlim_cur;
524 const int new_limit = MIN (limit.rlim_max, FD_SETSIZE);
525 if (new_limit != old_limit)
527 limit.rlim_cur = new_limit;
528 setrlimit (RLIMIT_NOFILE, &limit);
529 getrlimit (RLIMIT_NOFILE, &limit);
530 tr_logAddInfo ("Changed open file limit from %d to %d", old_limit, (int)limit.rlim_cur);
536 void
537 tr_fdClose (tr_session * session)
539 if (session && session->fdInfo)
541 struct tr_fdInfo * i = session->fdInfo;
542 fileset_destruct (&i->fileset);
543 tr_free (i);
544 session->fdInfo = NULL;
548 /***
549 ****
550 ***/
552 static struct tr_fileset*
553 get_fileset (tr_session * session)
555 if (!session)
556 return NULL;
558 ensureSessionFdInfoExists (session);
559 return &session->fdInfo->fileset;
562 void
563 tr_fdFileClose (tr_session * s, const tr_torrent * tor, tr_file_index_t i)
565 struct tr_cached_file * o;
567 if ((o = fileset_lookup (get_fileset (s), tr_torrentId (tor), i)))
569 /* flush writable files so that their mtimes will be
570 * up-to-date when this function returns to the caller... */
571 if (o->is_writable)
572 tr_fsync (o->fd);
574 cached_file_close (o);
579 tr_fdFileGetCached (tr_session * s, int torrent_id, tr_file_index_t i, bool writable)
581 struct tr_cached_file * o = fileset_lookup (get_fileset (s), torrent_id, i);
583 if (!o || (writable && !o->is_writable))
584 return -1;
586 o->used_at = tr_time ();
587 return o->fd;
590 #ifdef SYS_DARWIN
591 #define TR_STAT_MTIME(sb)((sb).st_mtimespec.tv_sec)
592 #else
593 #define TR_STAT_MTIME(sb)((sb).st_mtime)
594 #endif
596 bool
597 tr_fdFileGetCachedMTime (tr_session * s, int torrent_id, tr_file_index_t i, time_t * mtime)
599 bool success;
600 struct stat sb;
601 struct tr_cached_file * o = fileset_lookup (get_fileset (s), torrent_id, i);
603 if ((success = (o != NULL) && !fstat (o->fd, &sb)))
604 *mtime = TR_STAT_MTIME (sb);
606 return success;
609 void
610 tr_fdTorrentClose (tr_session * session, int torrent_id)
612 assert (tr_sessionIsLocked (session));
614 fileset_close_torrent (get_fileset (session), torrent_id);
617 /* returns an fd on success, or a -1 on failure and sets errno */
619 tr_fdFileCheckout (tr_session * session,
620 int torrent_id,
621 tr_file_index_t i,
622 const char * filename,
623 bool writable,
624 tr_preallocation_mode allocation,
625 uint64_t file_size)
627 struct tr_fileset * set = get_fileset (session);
628 struct tr_cached_file * o = fileset_lookup (set, torrent_id, i);
630 if (o && writable && !o->is_writable)
631 cached_file_close (o); /* close it so we can reopen in rw mode */
632 else if (!o)
633 o = fileset_get_empty_slot (set);
635 if (!cached_file_is_open (o))
637 const int err = cached_file_open (o, filename, writable, allocation, file_size);
638 if (err)
640 errno = err;
641 return -1;
644 dbgmsg ("opened '%s' writable %c", filename, writable?'y':'n');
645 o->is_writable = writable;
648 dbgmsg ("checking out '%s'", filename);
649 o->torrent_id = torrent_id;
650 o->file_index = i;
651 o->used_at = tr_time ();
652 return o->fd;
655 /***
656 ****
657 **** Sockets
658 ****
659 ***/
662 tr_fdSocketCreate (tr_session * session, int domain, int type)
664 int s = -1;
665 struct tr_fdInfo * gFd;
666 assert (tr_isSession (session));
668 ensureSessionFdInfoExists (session);
669 gFd = session->fdInfo;
671 if (gFd->peerCount < session->peerLimit)
672 if ((s = socket (domain, type, 0)) < 0)
673 if (sockerrno != EAFNOSUPPORT)
674 tr_logAddError (_("Couldn't create socket: %s"), tr_strerror (sockerrno));
676 if (s > -1)
677 ++gFd->peerCount;
679 assert (gFd->peerCount >= 0);
681 if (s >= 0)
683 static bool buf_logged = false;
684 if (!buf_logged)
686 int i;
687 socklen_t size = sizeof (int);
688 buf_logged = true;
689 getsockopt (s, SOL_SOCKET, SO_SNDBUF, &i, &size);
690 tr_logAddDebug ("SO_SNDBUF size is %d", i);
691 getsockopt (s, SOL_SOCKET, SO_RCVBUF, &i, &size);
692 tr_logAddDebug ("SO_RCVBUF size is %d", i);
696 return s;
700 tr_fdSocketAccept (tr_session * s, int sockfd, tr_address * addr, tr_port * port)
702 int fd;
703 unsigned int len;
704 struct tr_fdInfo * gFd;
705 struct sockaddr_storage sock;
707 assert (tr_isSession (s));
708 assert (addr);
709 assert (port);
711 ensureSessionFdInfoExists (s);
712 gFd = s->fdInfo;
714 len = sizeof (struct sockaddr_storage);
715 fd = accept (sockfd, (struct sockaddr *) &sock, &len);
717 if (fd >= 0)
719 if ((gFd->peerCount < s->peerLimit)
720 && tr_address_from_sockaddr_storage (addr, port, &sock))
722 ++gFd->peerCount;
724 else
726 tr_netCloseSocket (fd);
727 fd = -1;
731 return fd;
734 void
735 tr_fdSocketClose (tr_session * session, int fd)
737 assert (tr_isSession (session));
739 if (session->fdInfo != NULL)
741 struct tr_fdInfo * gFd = session->fdInfo;
743 if (fd >= 0)
745 tr_netCloseSocket (fd);
746 --gFd->peerCount;
749 assert (gFd->peerCount >= 0);