2 fuse module to perform the vfs part of read-only remote caching for NFS
4 Copyright Ronnie Sahlberg 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #define FUSE_USE_VERSION 26
22 /* we use a local lock file to limit how many files we are migrating
25 #define FILE_MIGRATE_MAX_CONCURRENT 100
26 #define FILE_MIGRATE_LIMITER_DIR "/var/lib/remote-cache"
27 #define FILE_MIGRATE_LIMITER "/var/lib/remote-cache/lockfile"
36 #include <sys/types.h>
39 #include <sys/statvfs.h>
50 #include "../lib/tdb/include/tdb.h"
51 #include "../lib/talloc/talloc.h"
52 #include "../lib/util/debug.h"
55 #define discard_const(ptr) ((void *)((intptr_t)(ptr)))
57 /* this is the directory where we cache data */
58 static const char *cache = NULL;
60 /* this is where the remote nfs server is mounted */
61 static const char *remote = NULL;
63 /* this is the directory where we present the cached image and which
64 we re-export through our local nfs server to the clients */
65 static const char *export = NULL;
68 static int debug_level = 0;
70 /* the maximum time in seconds we cache a directory.
71 defaults to 0 which means 'forever'
73 static int max_dir_cache;
75 /* minimum number of inodes in the cache filesystem before we will stop
76 caching more directory structures.
78 static int dir_min_inodes = 100;
80 /* minimum number of blocks in the cache filesystem before we will stop
81 caching more directory structures.
83 static int dir_min_blocks = 1000;
85 /* minimum number of inodes in the cache filesystem before we will stop
88 static int file_min_inodes = 100;
90 /* minimum number of blocks in the cache filesystem before we will stop
93 static int file_min_blocks = 10000;
95 /* maximum size in MB of files to be copied to the local cache.
96 0 means there is no size limit.
98 static int file_max_size = 0;
100 /* if 0, we check the mtime of the parent directory instead of the
101 file itself. This speeds up things when we have an environment
102 where files are created/deleted but never modified.
104 static int file_check_parent_mtime = 0;
106 /* Dont migrate files that were modified less than this many
109 static int file_min_mtime_age = 120;
111 /* Should we allow read/write semantics ? */
112 static int read_write = 0;
114 static int sigchild_initialized = 0;
115 static int debug_initialized = 0;
116 static int log_fd = -1;
118 struct tdb_context *groups_db = NULL;
120 extern int this_log_level;
131 #define DEBUG(lvl, x) \
133 if ((lvl) <= debug_level) {\
134 this_log_level = (lvl);\
135 if (!debug_initialized) {\
137 debug_initialized=1;\
143 static int rate_limit_is_ok(void)
145 /* only print one message every 60 seconds */
146 static time_t last_time = 0;
147 static time_t this_time = 0;
149 this_time = time(NULL);
151 if (this_time < last_time) {
152 /* a job for s hawkins */
153 last_time = this_time;
157 if (this_time > (last_time+60)) {
158 last_time = this_time;
165 static void unlink_object(const char *path)
167 TALLOC_CTX *mem_ctx = talloc_new(NULL);
172 bzero(&st, sizeof(st));
173 ret = lstat(path, &st);
174 if ((ret == -1) && (errno == ENOENT)) {
176 talloc_free(mem_ctx);
180 if ( (st.st_mode & S_IFMT) == S_IFREG) {
182 talloc_free(mem_ctx);
186 cmd = talloc_asprintf(mem_ctx, "rm -rf %s", path);
189 DEBUG(DEBUG_DEBUG,("%s failed with ret:%d\n", cmd, ret));
191 talloc_free(mem_ctx);
195 #define MAX_GROUPS 256
199 gid_t groups[MAX_GROUPS];
203 switch_to_real_user(void)
206 uid_t uid = fuse_get_context()->uid;
207 struct group_list gl;
208 TDB_DATA data = {NULL,0};
210 gl.num_groups = MAX_GROUPS;
212 /* make sure we have a group mapping database */
213 if (groups_db == NULL) {
214 DEBUG(DEBUG_ERR,("Initializing group mapping cache database\n"));
215 groups_db = tdb_open("groups", 10000, TDB_INTERNAL, O_RDWR|O_CREAT, 0);
216 if (groups_db == NULL) {
217 DEBUG(DEBUG_ERR,("Failed to initialize group mapping database.\n"));
222 TDB_DATA key, newdata;
225 key.dptr = (uint8_t *)&uid;
226 key.dsize= sizeof(uid);
228 tdb_chainlock(groups_db, key);
229 data = tdb_fetch(groups_db, key);
231 /* we already have a record for thus uid, check if it is still
234 if (data.dptr != NULL) {
235 if (data.dsize != sizeof(gl)) {
236 DEBUG(DEBUG_ERR,("Wrong size of gl structure was:%d expected:%d\n", (int)data.dsize, (int)sizeof(gl)));
239 tdb_chainunlock(groups_db, key);
240 goto finished_groups;
243 memcpy(&gl, data.dptr, sizeof(gl));
247 if (gl.num_groups >= MAX_GROUPS) {
248 gl.num_groups = MAX_GROUPS;
249 DEBUG(DEBUG_ERR,("Invalid number of groups in gl structure (%d)\n", gl.num_groups));
252 if (gl.ts > time(NULL)-36000) {
253 tdb_chainunlock(groups_db, key);
254 goto finished_groups;
258 /* we need to re-read the list of groups from the system and
259 store it in the cache
263 tdb_chainunlock(groups_db, key);
264 goto finished_groups;
267 getgrouplist(pw->pw_name, pw->pw_gid, gl.groups, &gl.num_groups);
270 newdata.dptr = (uint8_t *)≷
271 newdata.dsize = sizeof(gl);
272 if (tdb_store(groups_db, key, newdata, TDB_REPLACE)) {
273 DEBUG(DEBUG_ERR,("Failed to write gl structure to cahce tdb\n"));
274 tdb_chainunlock(groups_db, key);
275 goto finished_groups;
278 tdb_chainunlock(groups_db, key);
282 ret = setgid(fuse_get_context()->gid);
284 DEBUG(DEBUG_ERR, (__location__ " failed to setegid\n"));
289 if (gl.num_groups != MAX_GROUPS) {
290 ret = setgroups(gl.num_groups, gl.groups);
292 DEBUG(DEBUG_ERR, (__location__ " failed to setgroups\n"));
300 DEBUG(DEBUG_ERR, (__location__ " failed to seteuid\n"));
310 switch_back_to_root(void )
312 /* switch back to root */
320 static int XXXremote_cache_getattr(const char *path, struct stat *st)
322 TALLOC_CTX *mem_ctx = talloc_new(NULL);
327 int old_ret, new_ret;
329 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
330 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
332 bzero(&new_st, sizeof(new_st));
333 new_ret = lstat(new_path, &new_st);
334 bzero(&old_st, sizeof(old_st));
335 old_ret = lstat(old_path, &old_st);
337 DEBUG(DEBUG_DEBUG, ("GETATTR: %s (%s:%d:%d -> %s:%d:%d)\n", path, new_path, (int)new_st.st_mtime, (int)new_st.st_size, old_path, (int)old_st.st_mtime, (int)old_st.st_size));
339 if ( (old_ret !=0) || (new_ret!=0)
340 || (new_st.st_mtime!=old_st.st_mtime)
341 || (new_st.st_size!=old_st.st_size) ){
342 DEBUG(DEBUG_DEBUG, (__location__ " Need to migrate %s\n", new_path));
344 new_ret = lstat(new_path, &new_st);
347 memcpy(st, &new_st, sizeof(new_st));
349 talloc_free(mem_ctx);
354 #define MAX_NAME_LEN 255
355 struct cached_dir_entry {
356 char filename[MAX_NAME_LEN+1];
360 #define CACHE_DIR_NAME ". DIRCACHE"
362 int open_dir_cache_ro(TALLOC_CTX *mem_ctx, const char *path, struct stat *old_st
367 char *cache_path = NULL;
368 struct stat cache_st;
370 cache_path = talloc_asprintf(mem_ctx, "%s/%s/%s", cache, path, CACHE_DIR_NAME);
372 /* get size and timestamps of the cache file if it exists */
373 bzero(&cache_st, sizeof(cache_st));
374 ret = lstat(cache_path, &cache_st);
376 DEBUG(DEBUG_ERR, (__location__ " Failed lstat(%s) %s(%u)\n", cache_path, strerror(errno), errno));
379 if (cache_st.st_mtime != old_st->st_mtime) {
380 /* dont use the cache, its too old */
384 cache_fd = open(cache_path, O_RDONLY);
391 int open_parent_cache_ro(TALLOC_CTX *mem_ctx, const char *path)
396 struct stat parent_st;
400 ppath = talloc_strdup(mem_ctx, path);
401 sptr = rindex(ppath, '/');
410 parent_path = talloc_asprintf(mem_ctx, "%s/%s", remote, ppath);
411 ret = lstat(parent_path, &parent_st);
417 cache_fd = open_dir_cache_ro(mem_ctx, ppath, &parent_st);
421 int unlink_parent_dir_cache(TALLOC_CTX *mem_ctx, const char *path)
428 ppath = talloc_strdup(mem_ctx, path);
429 sptr = rindex(ppath, '/');
438 cache_path = talloc_asprintf(mem_ctx, "%s/%s", ppath, CACHE_DIR_NAME);
440 ret = unlink(cache_path);
441 DEBUG(DEBUG_DEBUG,("unlink_parent_dir_cache(%s) == %d\n", cache_path, ret));
446 int lookup_attribute_in_parent(TALLOC_CTX *mem_ctx, const char *path, struct stat *st)
449 struct cached_dir_entry cache_dirent;
453 sptr = talloc_strdup(mem_ctx, path);
459 sptr = rindex(sptr, '/');
471 cache_fd = open_parent_cache_ro(mem_ctx, path);
472 if (cache_fd == -1) {
477 while(read(cache_fd, &cache_dirent, sizeof(cache_dirent)) == sizeof(cache_dirent)) {
478 if (!strcmp(cache_dirent.filename, sptr)) {
479 memcpy(st, &cache_dirent.st, sizeof(cache_dirent.st));
494 /* GETATTR, always return the data for the old object on the original share
495 let the linux NFS clients cache the metadata using normal attribute caching
496 unless file_check_parent_mtime is set in which case we just verify
497 the parents mtime and then return the cached attributes for the object.
499 static int remote_cache_getattr(const char *path, struct stat *st)
501 TALLOC_CTX *mem_ctx = talloc_new(NULL);
506 struct stat cache_st;
509 if (file_check_parent_mtime) {
510 /* try to get the attributes from the parents cache
511 (if the parents cache is "current"
513 ret = lookup_attribute_in_parent(mem_ctx, path, &old_st);
515 DEBUG(DEBUG_DEBUG, (__location__ " GETATTR:[%s] from parent cache\n", path));
522 * read the attributes from the remote site
524 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
525 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
527 bzero(&old_st, sizeof(old_st));
528 ret = lstat(old_path, &old_st);
531 if (errno == ENOENT) {
532 DEBUG(DEBUG_INFO,(__location__ " File '%s' is removed from the remote site. Remove local cache copy (if any)\n", old_path));
533 unlink_object(new_path);
537 DEBUG(DEBUG_DEBUG, (__location__ " GETATTR: from remote site %s (%s:%d:%d) ret:%d\n", path, old_path, (int)old_st.st_mtime, (int)old_st.st_size, ret));
540 * if it was a file, then we also check the cached copy if it has
543 if ( (old_st.st_mode & S_IFMT) != S_IFREG) {
548 * read the attributes from the local cache directory
550 cache_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
552 bzero(&cache_st, sizeof(cache_st));
553 if (lstat(cache_path, &cache_st) != 0) {
558 * check that the locally cached copy is valid
560 if ((cache_st.st_mtime != old_st.st_mtime)
561 || (cache_st.st_size != old_st.st_size)) {
562 DEBUG(DEBUG_INFO, (__location__ " locally cached file %s has expired (time:%u:%u size:%u:%u)\n", cache_path, (unsigned int)cache_st.st_mtime, (unsigned int)old_st.st_mtime, (unsigned int)cache_st.st_size, (unsigned int)old_st.st_size));
563 unlink_object(cache_path);
568 memcpy(st, &old_st, sizeof(old_st));
570 talloc_free(mem_ctx);
574 static int remote_cache_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
576 TALLOC_CTX *mem_ctx = talloc_new(NULL);
578 char *new_path = NULL;
579 char *old_path = NULL;
580 char *obj_path = NULL;
581 char *cache_path = NULL;
584 struct stat cache_st;
585 struct dirent *dirent = NULL;
587 struct cached_dir_entry cache_dirent;
591 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
592 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
593 cache_path = talloc_asprintf(mem_ctx, "%s/%s/%s", cache, path, CACHE_DIR_NAME);
595 DEBUG(DEBUG_DEBUG, (__location__ " READDIR %s\n", path));
599 /* check the age of the directory cache.
600 purge all cached directories older than max_dir_cache
602 if (max_dir_cache != 0) {
606 ret = lstat(cache_path, &cache_st);
608 DEBUG(DEBUG_ERR, (__location__ " remote_cache_readdir: failed to stat cache file %s ret %d\n", cache_path, ret));
610 goto read_remote_build_cache;
612 if (ltime < cache_st.st_ctime) {
613 DEBUG(DEBUG_ERR, (__location__ " dircache %s has ctime (%u) in the future (ltime:%u)\n", cache_path, (int)cache_st.st_ctime, (int)ltime));
616 if (ltime > (cache_st.st_ctime + max_dir_cache)) {
617 DEBUG(DEBUG_ERR, ("Cache has expired for directory %s. Flushing cache\n", new_path));
623 * try to open the directory cache file and read from it
625 ret = lstat(cache_path, &cache_st);
627 DEBUG(DEBUG_ERR, (__location__ " remote_cache_readdir: failed to stat cache file %s %s\n", cache_path, strerror(errno)));
628 goto read_remote_build_cache;
630 ret = lstat(old_path, &old_st);
632 DEBUG(DEBUG_ERR, (__location__ " remote_cache_readdir: failed to stat cache file %s %s\n", cache_path, strerror(errno)));
633 goto read_remote_build_cache;
635 if (cache_st.st_mtime != old_st.st_mtime) {
636 /* dont use the cache, its too old */
637 DEBUG(DEBUG_ERR, (__location__ " Directory %s has expired from the cache.\n", old_path));
639 goto read_remote_build_cache;
642 ret = switch_to_real_user();
644 DEBUG(DEBUG_ERR, (__location__ "SETEUID failed\n"));
645 switch_back_to_root();
648 cache_fd = open(cache_path, O_RDONLY);
649 if ((cache_fd == -1) && (errno == EACCES)) {
651 switch_back_to_root();
654 if (cache_fd != -1) {
655 while(read(cache_fd, &cache_dirent, sizeof(cache_dirent)) == sizeof(cache_dirent)) {
656 filler(buf, &cache_dirent.filename[0], &cache_dirent.st, 0);
658 switch_back_to_root();
661 switch_back_to_root();
666 * as root, read the directory from the remote site and store it
669 read_remote_build_cache:
670 /* if we have run out of space, we cant build a local cache */
671 ret = statvfs(cache, &vfs);
673 DEBUG(DEBUG_ERR, (__location__ " statfs(%s) failed\n", cache));
677 if (vfs.f_bfree < dir_min_blocks) {
678 if( rate_limit_is_ok() ){
679 DEBUG(DEBUG_CRIT, ("Cache is full. Only %d blocks left in %s. Can not build local cache of directory %s/%s\n", (int)vfs.f_bfree, cache, export, path));
684 if (vfs.f_ffree < dir_min_inodes) {
685 if( rate_limit_is_ok() ){
686 DEBUG(DEBUG_CRIT, ("Cache is full. Only %d inodes left in %s. Can not build local cache of directory %s/%s\n", (int)vfs.f_ffree, cache, export, path));
696 * stat the remote directory and create it in the cache
698 ret = lstat(old_path, &old_st);
700 DEBUG(DEBUG_ERR, (__location__ " remote_cache_readdir: failed to stat cache file %s %s\n", cache_path, strerror(errno)));
705 if (chown(new_path, old_st.st_uid, old_st.st_gid) == -1) {
706 DEBUG(DEBUG_ERR, ("CHOWN for new directory %s failed with %s\n", new_path, strerror(errno)));
710 if (chmod(new_path, old_st.st_mode) == -1) {
711 DEBUG(DEBUG_ERR, ("CHMOD for new directory %s failed with %s\n", new_path, strerror(errno)));
718 * create the dir cache file
720 cache_fd = open(cache_path, O_RDWR|O_CREAT);
721 if (cache_fd == -1) {
722 DEBUG(DEBUG_ERR, (__location__ " Failed to create/open directory cache\n"));
725 if (fchown(cache_fd, old_st.st_uid, old_st.st_gid) == -1) {
726 DEBUG(DEBUG_ERR, ("CHOWN for dir cache %s failed with errno:%u\n", cache_path, errno));
730 if (fchmod(cache_fd, old_st.st_mode) == -1) {
731 DEBUG(DEBUG_ERR, ("CHMOD for dir cache %s failed with errno:%u\n", cache_path, errno));
736 lock.l_type = F_WRLCK;
737 lock.l_whence = SEEK_SET;
740 lock.l_pid = getpid();
741 if (fcntl(cache_fd, F_SETLK, &lock) != 0) {
742 DEBUG(DEBUG_ERR, (__location__ " Failed to get lock on dircache file %s try again later\n", new_path));
749 dir = opendir(old_path);
751 DEBUG(DEBUG_ERR, (__location__ " remote_cache_readdir: failed to open old directory %s\n", old_path));
758 while ((dirent = readdir(dir)) != NULL) {
759 talloc_free(obj_path);
762 if (strlen(dirent->d_name) > MAX_NAME_LEN) {
763 DEBUG(DEBUG_ERR, (__location__ " Too long name : %s. skipping entry for %s\n",
764 dirent->d_name, cache_path));
767 memcpy(&cache_dirent.filename[0],dirent->d_name, strlen(dirent->d_name)+1);
770 obj_path = talloc_asprintf(mem_ctx, "%s/%s", old_path, dirent->d_name);
772 ret = lstat(obj_path, &cache_dirent.st);
774 DEBUG(DEBUG_ERR, (__location__ " remote_cache_readdir: failed to stat %s ret %d\n", obj_path, ret));
780 if (cache_fd != -1) {
782 r = write(cache_fd, &cache_dirent, sizeof(cache_dirent));
783 if (r != sizeof(cache_dirent)) {
784 DEBUG(DEBUG_ERR, (__location__ " write of cached dirent failed for %s\n", cache_path));
793 DEBUG(DEBUG_ERR, (__location__ " remote_cache_readdir: readdir returned NULL and set errno to :%d for %s\n", errno, cache_path));
798 /* if all went well, update the timestamps of our cache to match the
799 directory on the remote server
801 if (cache_fd != -1) {
802 struct utimbuf times;
807 times.actime = old_st.st_atime;
808 times.modtime = old_st.st_mtime;
809 r = utime(cache_path, ×);
811 DEBUG(DEBUG_ERR, (__location__ " update cache time failed for %s\n", cache_path));
816 /* and finally, try it again and hopefully read it from the cache
817 * as the real user this time.
823 if (cache_fd != -1) {
830 talloc_free(mem_ctx);
834 static int remote_cache_statfs(const char *path, struct statvfs *buf)
836 TALLOC_CTX *mem_ctx = talloc_new(NULL);
837 char *old_path = NULL;
840 //qqq DEBUG(DEBUG_DEBUG, (__location__ " STATFS called\n"));
841 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
842 ret = statvfs(old_path, buf);
849 talloc_free(mem_ctx);
853 static int remote_cache_readlink(const char *path, char *buf, size_t len)
855 TALLOC_CTX *mem_ctx = talloc_new(NULL);
856 char *old_path = NULL;
860 DEBUG(DEBUG_DEBUG, (__location__ " READLINK %s\n", path));
861 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
862 lsize = readlink(old_path, buf, len);
869 talloc_free(mem_ctx);
873 static int remote_cache_access(const char *path, int mask)
876 TALLOC_CTX *mem_ctx = talloc_new(NULL);
877 char *old_path = NULL;
885 DEBUG(DEBUG_DEBUG, (__location__ " ACCESS %s mask 0x%08x\n", path, mask));
889 DEBUG(DEBUG_DEBUG, (__location__ " Access failed W was not allowed\n"));
895 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
897 bzero(&old_st, sizeof(old_st));
898 ret = lstat(old_path, &old_st);
900 DEBUG(DEBUG_ERR, (__location__ " Failed lstat(%s) %s(%u)\n", old_path, strerror(errno), errno));
905 my_uid = fuse_get_context()->uid;
906 my_gid = fuse_get_context()->gid;
908 /* check the r bit */
911 if (old_st.st_mode & S_IROTH) {
914 if ((old_st.st_mode & S_IRGRP)
915 && (old_st.st_gid == my_gid) ) {
918 if ((old_st.st_mode & S_IRUSR)
919 && (old_st.st_uid == my_uid) ) {
929 /* check the w bit */
932 if (old_st.st_mode & S_IWOTH) {
935 if ((old_st.st_mode & S_IWGRP)
936 && (old_st.st_gid == my_gid) ) {
939 if ((old_st.st_mode & S_IWUSR)
940 && (old_st.st_uid == my_uid) ) {
951 DEBUG(DEBUG_DEBUG, (__location__ " Access failed W was not allowed\n"));
956 /* check the x bit */
959 if (old_st.st_mode & S_IXOTH) {
962 if ((old_st.st_mode & S_IXGRP)
963 && (old_st.st_gid == my_gid) ) {
966 if ((old_st.st_mode & S_IXUSR)
967 && (old_st.st_uid == my_uid) ) {
978 DEBUG(DEBUG_DEBUG, (__location__ " Access failed X was not allowed\n"));
984 talloc_free(mem_ctx);
989 void child_migrate_file(TALLOC_CTX *mem_ctx, char *old_path, char *new_path)
995 char *migrate_cmd = NULL;
998 DEBUG(DEBUG_INFO, (__location__ " Migration child %d for file %s->%s\n", getpid(), old_path, new_path));
999 fd = open(new_path, O_RDWR|O_CREAT, 0600);
1001 DEBUG(DEBUG_ERR, (__location__ " Failed to open/create file to migrate on local cache\n"));
1005 lock.l_type = F_WRLCK;
1006 lock.l_whence = SEEK_SET;
1009 lock.l_pid = getpid();
1010 if (fcntl(fd, F_SETLK, &lock) != 0) {
1011 DEBUG(DEBUG_ERR, (__location__ " Failed to get lock on file in migrate child %s\n", new_path));
1016 lfd = open(FILE_MIGRATE_LIMITER, O_RDWR);
1018 DEBUG(DEBUG_ERR, (__location__ " Failed to open migration limiter lock file\n"));
1023 llock.l_type = F_WRLCK;
1024 llock.l_whence = SEEK_SET;
1025 llock.l_start = random()%FILE_MIGRATE_MAX_CONCURRENT;
1027 llock.l_pid = getpid();
1028 if (fcntl(lfd, F_SETLK, &llock) != 0) {
1029 DEBUG(DEBUG_ERR, (__location__ " Failed to get migration lock. Too many concurrent migrations.\n"));
1037 migrate_cmd = talloc_asprintf(mem_ctx, "rsync -ptgo \"%s\" \"%s\"", old_path, new_path);
1038 ret = system(migrate_cmd);
1040 DEBUG(DEBUG_ERR, (__location__ " migration %s -> %s failed\n", old_path, new_path));
1043 lock.l_type = F_UNLCK;
1044 if (fcntl(fd, F_SETLK, &lock) != 0) {
1045 DEBUG(DEBUG_ERR, (__location__ " Failed to unlock file in migrate child %s\n", new_path));
1051 llock.l_type = F_UNLCK;
1052 if (fcntl(lfd, F_SETLK, &llock) != 0) {
1053 DEBUG(DEBUG_ERR, (__location__ " Failed to unlock file in migrate child %s\n", new_path));
1059 DEBUG(DEBUG_INFO, (__location__ " migration of %s -> %s complete\n", old_path, new_path));
1066 sigchld_handler (int signum)
1070 /* reap all children */
1071 while( wait3(&wait_stat, WNOHANG, NULL) != -1);
1075 int try_migrate_file(TALLOC_CTX *mem_ctx, char *old_path, char *new_path)
1078 struct sigaction sa;
1081 DEBUG(DEBUG_INFO, (__location__ " TRY to migrate %s -> %s\n", old_path, new_path));
1083 if (!sigchild_initialized) {
1084 sa.sa_handler = sigchld_handler;
1085 sigfillset(&sa.sa_mask);
1086 sa.sa_flags = SA_RESTART;
1087 ret = sigaction( SIGCHLD, &sa, NULL);
1089 printf("failed to set up CIGCHLD handler\n");
1092 sigchild_initialized = 1;
1097 DEBUG(DEBUG_ERR, (__location__ " failed to fork migration child\n"));
1105 child_migrate_file(mem_ctx, old_path, new_path);
1114 static int remote_cache_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
1116 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1117 char *new_path = NULL;
1118 char *old_path = NULL;
1119 struct stat cache_st;
1127 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1128 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1131 DEBUG(DEBUG_DEBUG, (__location__ " READ: %s offset:%d length:%d\n", path, (int)offset, (int)size));
1135 * If we should check the mtime of the file itself
1136 * instead of using the trick to check the mtime of the parent
1137 * directory instead. Note, that trick only works if files are
1138 * created and deleted but never modified.
1140 if (file_check_parent_mtime == 0) {
1141 /* read attributes from local cache */
1142 bzero(&new_st, sizeof(new_st));
1143 ret = lstat(new_path, &new_st);
1145 DEBUG(DEBUG_DEBUG, (__location__ " Failed to lstat(%s) %s\n", new_path, strerror(errno)));
1146 goto read_from_remote_site;
1149 /* read attributes from remote site */
1150 bzero(&old_st, sizeof(old_st));
1151 ret = lstat(old_path, &old_st);
1153 DEBUG(DEBUG_ERR, (__location__ " Failed to lstat(%s) %s\n", old_path, strerror(errno)));
1158 if ((new_st.st_mtime != old_st.st_mtime)
1159 || (new_st.st_size != old_st.st_size)) {
1160 DEBUG(DEBUG_ERR, (__location__ " locally cached file %s has expired (time:%u:%u size:%u:%u)\n", new_path, (unsigned int)new_st.st_mtime, (unsigned int)old_st.st_mtime, (unsigned int)new_st.st_size, (unsigned int)old_st.st_size));
1161 unlink_object(new_path);
1162 goto read_from_remote_site;
1165 goto read_from_local_cache;
1169 /* we use the trick to check the mtime of the parent directory
1170 * instead of the file itself. This reduses the pressure on the
1171 * attribute cache and speeds things up quite a lot.
1172 * This can only be used when files are created/deleted but never
1175 ret = lookup_attribute_in_parent(mem_ctx, path, &cache_st);
1177 DEBUG(DEBUG_ERR, (__location__ " READ: %s not found in parent cache\n", path));
1178 goto read_from_remote_site;
1181 bzero(&new_st, sizeof(new_st));
1182 ret = lstat(new_path, &new_st);
1184 DEBUG(DEBUG_ERR, (__location__ " Failed to lstat(%s) %s\n", new_path, strerror(errno)));
1185 goto read_from_remote_site;
1187 if ((new_st.st_mtime != cache_st.st_mtime)
1188 || (new_st.st_size != cache_st.st_size)) {
1189 DEBUG(DEBUG_ERR, (__location__ " can not read from local cached file\n"));
1190 goto read_from_remote_site;
1194 read_from_local_cache:
1196 * read from local cache
1199 DEBUG(DEBUG_DEBUG, (__location__ " READ: %s from local cache\n", path));
1200 ret = switch_to_real_user();
1202 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1206 fd = open(new_path, O_RDONLY);
1208 if (errno != EACCES) {
1209 DEBUG(DEBUG_ERR, (__location__ " READ: failed to open %s %s(%u)\n", new_path, strerror(errno), errno));
1214 ret = pread(fd, buf, size, offset);
1216 DEBUG(DEBUG_ERR, (__location__ " remote_cache_read: local pread returned %d\n", ret));
1224 * read from remote site
1225 * start a background job to migrate the entire file
1227 read_from_remote_site:
1228 ret = switch_to_real_user();
1230 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1235 DEBUG(DEBUG_DEBUG, (__location__ " READ: %s from remote site\n", path));
1236 fd = open(old_path, O_RDONLY);
1238 DEBUG(DEBUG_ERR, (__location__ " READ: failed to open remote file %s %s\n", old_path, strerror(errno)));
1242 /* should forcibly invalidate parents cache here ? */
1244 ret = pread(fd, buf, size, offset);
1246 DEBUG(DEBUG_ERR, (__location__ " remote_cache_read: remote pread returned %d\n", ret));
1251 switch_back_to_root();
1253 if (lstat(old_path, &cache_st) == -1) {
1254 DEBUG(DEBUG_ERR, (__location__ " Failed to stat remote file %s when trying to migrate\n", old_path));
1258 if ( (file_max_size > 0)
1259 && (cache_st.st_size>>20) > file_max_size) {
1260 if( rate_limit_is_ok() ){
1261 DEBUG(DEBUG_ERR,("file %s is too big for the cache %u MB\n", old_path, (int)(cache_st.st_size>>20)));
1266 /* if we have run out of space, we cant build a local cache */
1267 if (statvfs(cache, &vfs) != 0) {
1268 DEBUG(DEBUG_ERR, (__location__ " statfs(%s) failed\n", cache));
1271 if (vfs.f_bfree < file_min_blocks) {
1272 if( rate_limit_is_ok() ){
1273 DEBUG(DEBUG_CRIT, ("Cache is full. Only %d blocks left in %s. Can not add file %s to the cache\n", (int)vfs.f_bfree, cache, old_path));
1278 if (vfs.f_ffree < file_min_inodes) {
1279 if( rate_limit_is_ok() ){
1280 DEBUG(DEBUG_CRIT, ("Cache is full. Only %d inodes left in %s. Can not add file %s to the cache\n", (int)vfs.f_ffree, cache, old_path));
1286 /* only migrate the file if it was last modified
1287 * file_min_mtime_age seconds ago
1289 bzero(&old_st, sizeof(old_st));
1290 if (lstat(old_path, &old_st) == -1) {
1291 DEBUG(DEBUG_ERR, (__location__ " Failed to lstat(%s) %s\n", old_path, strerror(errno)));
1296 if (ltime < (old_st.st_mtime + file_min_mtime_age)) {
1297 DEBUG(DEBUG_INFO, (__location__ " file '%s' has changed too recently (%d seconds ago, limit is %d seconds). Skipping migration\n", old_path, (int)(ltime-old_st.st_mtime), file_min_mtime_age));
1302 try_migrate_file(mem_ctx, old_path, new_path);
1306 switch_back_to_root();
1312 talloc_free(mem_ctx);
1316 static int remote_cache_mknod(const char *path, mode_t mode, dev_t rdev)
1318 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1319 char *new_path = NULL;
1320 char *old_path = NULL;
1324 talloc_free(mem_ctx);
1328 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1329 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1332 DEBUG(DEBUG_DEBUG, (__location__ " MKNOD:%s mode:%o rdev:%x\n", path, (int)mode, (int)rdev));
1334 ret = switch_to_real_user();
1336 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1341 ret = mknod(old_path, mode, rdev);
1343 DEBUG(DEBUG_DEBUG,(__location__ " MKNOD %s mode:%o rdev:%x failed with errno:%u\n", old_path, mode, (int)rdev, errno));
1348 /* if the mknod was successful we try to delete any local cached
1349 entries for this name
1351 switch_back_to_root();
1352 unlink_object(new_path);
1353 unlink_parent_dir_cache(mem_ctx, new_path);
1356 switch_back_to_root();
1357 talloc_free(mem_ctx);
1363 static int remote_cache_mkdir(const char *path, mode_t mode)
1365 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1366 char *new_path = NULL;
1367 char *old_path = NULL;
1371 talloc_free(mem_ctx);
1375 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1376 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1379 DEBUG(DEBUG_DEBUG, (__location__ " MKDIR:%s mode:%o\n", path, (int)mode));
1381 ret = switch_to_real_user();
1383 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1388 ret = mkdir(old_path, mode);
1390 DEBUG(DEBUG_DEBUG,(__location__ " MKDIR %s mode:%o failed with errno:%u\n", old_path, mode, errno));
1395 /* if the mkdir was successful we try to delete any local cached
1396 entries for this name
1398 switch_back_to_root();
1399 unlink_object(new_path);
1400 unlink_parent_dir_cache(mem_ctx, new_path);
1403 switch_back_to_root();
1404 talloc_free(mem_ctx);
1409 static int remote_cache_rmdir(const char *path)
1411 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1412 char *new_path = NULL;
1413 char *old_path = NULL;
1417 talloc_free(mem_ctx);
1421 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1422 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1425 DEBUG(DEBUG_DEBUG, (__location__ " RMDIR:%s\n", path));
1427 ret = switch_to_real_user();
1429 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1434 ret = rmdir(old_path);
1436 DEBUG(DEBUG_DEBUG,(__location__ " RMDIR %s failed with errno:%u\n", old_path, errno));
1441 /* if the rmdir was successful we try to delete any local cached
1442 entries for this name
1444 switch_back_to_root();
1445 unlink_object(new_path);
1446 unlink_parent_dir_cache(mem_ctx, new_path);
1449 switch_back_to_root();
1450 talloc_free(mem_ctx);
1456 static int remote_cache_unlink(const char *path)
1458 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1459 char *new_path = NULL;
1460 char *old_path = NULL;
1464 talloc_free(mem_ctx);
1468 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1469 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1472 DEBUG(DEBUG_DEBUG, (__location__ " UNLINK:%s\n", path));
1474 ret = switch_to_real_user();
1476 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1480 /* unlink the file on the remote site */
1481 ret = unlink(old_path);
1483 DEBUG(DEBUG_DEBUG,(__location__ " UNLINK %s failed with errno:%u\n", old_path, errno));
1488 /* if the unlink was successful we try to delete any local cached
1489 entries for this name and remove the cache for this directory
1491 switch_back_to_root();
1492 unlink_object(new_path);
1493 unlink_parent_dir_cache(mem_ctx, new_path);
1496 switch_back_to_root();
1497 talloc_free(mem_ctx);
1501 static int remote_cache_chmod(const char *path, mode_t mode)
1503 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1504 char *new_path = NULL;
1505 char *old_path = NULL;
1509 talloc_free(mem_ctx);
1513 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1514 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1517 DEBUG(DEBUG_DEBUG, (__location__ " CHMOD:%s mode:%o\n", path, mode));
1519 ret = switch_to_real_user();
1521 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1526 ret = chmod(old_path, mode);
1528 DEBUG(DEBUG_DEBUG,(__location__ " CHMOD %s mode %o failed with errno:%u\n", old_path, mode, errno));
1533 /* if the chmod was successful we try to delete any local cached
1534 entries for this name
1536 switch_back_to_root();
1537 unlink_object(new_path);
1538 unlink_parent_dir_cache(mem_ctx, new_path);
1541 switch_back_to_root();
1542 talloc_free(mem_ctx);
1546 static int remote_cache_chown(const char *path, uid_t uid, gid_t gid)
1548 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1549 char *new_path = NULL;
1550 char *old_path = NULL;
1554 talloc_free(mem_ctx);
1558 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1559 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1562 DEBUG(DEBUG_DEBUG, (__location__ " CHOWN:%s uid %d gid %d\n", path, uid, gid));
1564 ret = switch_to_real_user();
1566 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1571 ret = chown(old_path, uid, gid);
1573 DEBUG(DEBUG_DEBUG,(__location__ " CHOWN %s uid %d gid %d failed with errno:%u\n", old_path, uid, gid, errno));
1578 /* if the chown was successful we try to delete any local cached
1579 entries for this name
1581 switch_back_to_root();
1582 unlink_object(new_path);
1583 unlink_parent_dir_cache(mem_ctx, new_path);
1586 switch_back_to_root();
1587 talloc_free(mem_ctx);
1591 static int remote_cache_create(const char *path, mode_t mode, struct fuse_file_info *fi)
1593 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1594 char *new_path = NULL;
1595 char *old_path = NULL;
1600 talloc_free(mem_ctx);
1604 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1605 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1608 DEBUG(DEBUG_DEBUG, (__location__ " CREATE %s mode %o\n", path, mode));
1610 ret = switch_to_real_user();
1612 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1617 fd = creat(old_path, mode);
1619 DEBUG(DEBUG_DEBUG,(__location__ " CREATE %s mode %o failed with errno:%u\n", old_path, mode, errno));
1625 /* if the creat was successful we try to delete any local cached
1626 entries for this name
1629 switch_back_to_root();
1630 unlink_object(new_path);
1631 unlink_parent_dir_cache(mem_ctx, new_path);
1634 switch_back_to_root();
1635 talloc_free(mem_ctx);
1639 static int remote_cache_utime(const char *path, struct utimbuf *times)
1641 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1642 char *new_path = NULL;
1643 char *old_path = NULL;
1647 talloc_free(mem_ctx);
1651 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1652 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1655 DEBUG(DEBUG_DEBUG, (__location__ " UTIME %s\n", path));
1657 ret = switch_to_real_user();
1659 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1664 ret = utime(old_path, times);
1666 DEBUG(DEBUG_DEBUG,(__location__ " UTIME %s failed with errno:%u\n", old_path, errno));
1671 /* if the utime was successful we try to delete any local cached
1672 entries for this name
1674 switch_back_to_root();
1675 unlink_object(new_path);
1676 unlink_parent_dir_cache(mem_ctx, new_path);
1679 switch_back_to_root();
1680 talloc_free(mem_ctx);
1684 static int remote_cache_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
1686 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1687 char *new_path = NULL;
1688 char *old_path = NULL;
1693 talloc_free(mem_ctx);
1697 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1698 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1701 DEBUG(DEBUG_DEBUG, (__location__ " WRITE %s offset %d len %d\n", path, (int)offset, (int)size));
1703 ret = switch_to_real_user();
1705 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1710 fd = open(old_path, O_RDWR);
1712 if (errno != EACCES) {
1713 DEBUG(DEBUG_ERR, (__location__ " WRITE: failed to open %s %s(%u)\n", new_path, strerror(errno), errno));
1718 ret = pwrite(fd, buf, size, offset);
1720 DEBUG(DEBUG_ERR, (__location__ " remote_cache_write: local pwrite returned %d\n", ret));
1726 /* if the pwrite was successful we try to delete any local cached
1727 entries for this name
1729 switch_back_to_root();
1730 unlink_object(new_path);
1731 unlink_parent_dir_cache(mem_ctx, new_path);
1734 switch_back_to_root();
1739 talloc_free(mem_ctx);
1743 static int remote_cache_truncate(const char *path, off_t offset)
1745 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1746 char *new_path = NULL;
1747 char *old_path = NULL;
1751 talloc_free(mem_ctx);
1755 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1756 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1759 DEBUG(DEBUG_DEBUG, (__location__ " TRUNCATE %s offset %d\n", path, (int)offset));
1761 ret = switch_to_real_user();
1763 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1768 ret = truncate(old_path, offset);
1770 DEBUG(DEBUG_DEBUG,(__location__ " TRUNCATE %s offset %d failed with errno:%u\n", old_path, (int)offset, errno));
1775 /* if the truncate was successful we try to delete any local cached
1776 entries for this name
1778 switch_back_to_root();
1779 unlink_object(new_path);
1780 unlink_parent_dir_cache(mem_ctx, new_path);
1783 switch_back_to_root();
1784 talloc_free(mem_ctx);
1788 static int remote_cache_ftruncate(const char *path, off_t offset, struct fuse_file_info *fi)
1790 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1791 char *new_path = NULL;
1792 char *old_path = NULL;
1796 talloc_free(mem_ctx);
1800 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1801 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1804 DEBUG(DEBUG_DEBUG, (__location__ " FTRUNCATE %s offset %d\n", path, (int)offset));
1806 ret = switch_to_real_user();
1808 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1813 ret = truncate(old_path, offset);
1815 DEBUG(DEBUG_DEBUG,(__location__ " FTRUNCATE %s offset %d failed with errno:%u\n", old_path, (int)offset, errno));
1820 /* if the ftruncate was successful we try to delete any local cached
1821 entries for this name
1823 switch_back_to_root();
1824 unlink_object(new_path);
1825 unlink_parent_dir_cache(mem_ctx, new_path);
1828 switch_back_to_root();
1829 talloc_free(mem_ctx);
1833 static int remote_cache_link(const char *path, const char *newpath)
1835 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1836 char *new_path = NULL;
1837 char *old_path = NULL;
1838 char *new_newpath = NULL;
1839 char *old_newpath = NULL;
1843 talloc_free(mem_ctx);
1847 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1848 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1849 new_newpath= talloc_asprintf(mem_ctx, "%s/%s", cache, newpath);
1850 old_newpath= talloc_asprintf(mem_ctx, "%s/%s", remote, newpath);
1853 DEBUG(DEBUG_DEBUG, (__location__ " LINK %s -> %s\n", path, newpath));
1855 ret = switch_to_real_user();
1857 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1861 ret = link(old_path, old_newpath);
1863 DEBUG(DEBUG_DEBUG,(__location__ " LINK %s -> %s failed with errno:%u\n", old_path, old_newpath, errno));
1868 /* if the link was successful we try to delete any local cached
1869 entries for this name
1871 switch_back_to_root();
1872 unlink_object(new_newpath);
1873 unlink_parent_dir_cache(mem_ctx, new_newpath);
1876 switch_back_to_root();
1877 talloc_free(mem_ctx);
1882 static int remote_cache_symlink(const char *linkname, const char *path)
1884 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1885 char *new_newpath = NULL;
1886 char *old_newpath = NULL;
1890 talloc_free(mem_ctx);
1894 new_newpath= talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1895 old_newpath= talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1898 DEBUG(DEBUG_DEBUG, (__location__ " SYMLINK %s -> %s\n", linkname, path));
1900 ret = switch_to_real_user();
1902 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1906 ret = symlink(linkname, old_newpath);
1908 DEBUG(DEBUG_DEBUG,(__location__ " SYMLINK %s -> %s failed with errno:%u\n", linkname, old_newpath, errno));
1913 /* if the symlink was successful we try to delete any local cached
1914 entries for this name
1916 switch_back_to_root();
1917 unlink_object(new_newpath);
1918 unlink_parent_dir_cache(mem_ctx, new_newpath);
1921 switch_back_to_root();
1922 talloc_free(mem_ctx);
1926 static int remote_cache_rename(const char *path, const char *newpath)
1928 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1929 char *new_path = NULL;
1930 char *old_path = NULL;
1931 char *new_newpath = NULL;
1932 char *old_newpath = NULL;
1936 talloc_free(mem_ctx);
1940 new_path = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
1941 old_path = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
1942 new_newpath= talloc_asprintf(mem_ctx, "%s/%s", cache, newpath);
1943 old_newpath= talloc_asprintf(mem_ctx, "%s/%s", remote, newpath);
1946 DEBUG(DEBUG_DEBUG, (__location__ " RENAME %s -> %s\n", path, newpath));
1948 ret = switch_to_real_user();
1950 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1954 ret = rename(old_path, old_newpath);
1956 DEBUG(DEBUG_DEBUG,(__location__ " RENAME %s -> %s failed with errno:%u\n", old_path, old_newpath, errno));
1961 /* if the link was successful we try to delete any local cached
1962 entries for this name
1964 switch_back_to_root();
1965 unlink_object(new_path);
1966 unlink_object(new_newpath);
1967 unlink_parent_dir_cache(mem_ctx, new_path);
1970 switch_back_to_root();
1971 talloc_free(mem_ctx);
1976 static int remote_cache_lock(const char *path, struct fuse_file_info *ffi, int cmd, struct flock *fl)
1987 DEBUG(DEBUG_DEBUG, ("LOCK %s cmd:%s(%d)\n", path, cmd==F_GETLK?"F_GETLK":cmd==F_SETLKW?"F_SETLKW":cmd==F_SETLK?"F_SETLK":"unknown", cmd));
1989 ret = switch_to_real_user();
1991 DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
1995 fd = *((int *)(&ffi->fh));
1997 DEBUG(DEBUG_ERR,(__location__ " ERROR file to lock is not open %s\n", path));
2002 ret = fcntl(fd, cmd, fl);
2004 DEBUG(DEBUG_ERR,(__location__ " FCNTL lock failed with errno:%d for fd:%d for file %s\n", errno, fd, path));
2010 switch_back_to_root();
2016 static struct fuse_operations remote_cache_ops = {
2017 .getattr = remote_cache_getattr,
2018 .readdir = remote_cache_readdir,
2019 .statfs = remote_cache_statfs,
2020 .readlink = remote_cache_readlink,
2021 .access = remote_cache_access,
2022 .read = remote_cache_read,
2025 .unlink = remote_cache_unlink,
2026 .create = remote_cache_create,
2027 .mknod = remote_cache_mknod,
2028 .mkdir = remote_cache_mkdir,
2029 .rmdir = remote_cache_rmdir,
2030 .rename = remote_cache_rename,
2031 .chmod = remote_cache_chmod,
2032 .chown = remote_cache_chown,
2033 .utime = remote_cache_utime,
2034 .write = remote_cache_write,
2035 .truncate = remote_cache_truncate,
2036 .ftruncate = remote_cache_ftruncate,
2037 .symlink = remote_cache_symlink,
2038 .link = remote_cache_link,
2042 .lock = remote_cache_lock,
2043 // .open = remote_cache_open,
2044 // .release = remote_cache_release,
2048 // .flush = migrate_flush,
2049 // .fsync = migrate_fsync,
2050 // .setxattr = migrate_setxattr,
2051 // .getxattr = migrate_getxattr,
2052 // .listxattr = migrate_listxattr,
2053 // .removexattr = migrate_removexattr,
2054 // .opendir = migrate_opendir,
2055 // .releasedir = migrate_releasedir,
2056 // .fsyncdir = migrate_fsyncdir,
2057 // .fgetattr = migrate_fgetattr,
2058 // .utimens = migrate_utimens,
2062 static void usage(void)
2064 fprintf(stderr, "remote-cache [OPTIONS]\n");
2065 fprintf(stderr, " --export=directory : the directory to re-export the data through\n");
2066 fprintf(stderr, " --cache=directory : the local directory to cache data in\n");
2067 fprintf(stderr, " --remote=directory : the remote doirectory to cache\n");
2072 int main(int argc, const char *argv[])
2075 const char *fuse_argv[] = {
2079 "-odefault_permissions",
2081 "-omax_write=32768",
2086 struct poptOption popt_options[] = {
2088 { "export", 'e', POPT_ARG_STRING, &export, 0, "export directory", "directory" },
2089 { "cache", 'c', POPT_ARG_STRING, &cache, 0, "directory to store the cached data", "directory" },
2090 { "remote", 'r', POPT_ARG_STRING, &remote, 0, "directory where the remote nfs data is mounted", "directory" },
2091 { "max-dir-cache", 0, POPT_ARG_INT, &max_dir_cache, 0, "maximum number of seconds to cache a directory.", "integer" },
2092 { "dir-min-inodes", 0, POPT_ARG_INT, &dir_min_inodes, 0, "minimum free inodes to cache directory (default 100)", "integer" },
2093 { "dir-min-blocks", 0, POPT_ARG_INT, &dir_min_blocks, 0, "minimum free blocks to cache directory (default 1000)", "integer" },
2094 { "file-min-inodes", 0, POPT_ARG_INT, &file_min_inodes, 0, "minimum free inodes to cache more files (default 100)", "integer" },
2095 { "file-min-blocks", 0, POPT_ARG_INT, &file_min_blocks, 0, "minimum free blocks to cache more files (default 10000)", "integer" },
2096 { "file-max-size", 0, POPT_ARG_INT, &file_max_size, 0, "maximum size in MB of files to cache", "integer" },
2097 { "file-min-mtime-age", 0, POPT_ARG_INT, &file_min_mtime_age, 0, "minimum age in seconds since last change for migration", "integer" },
2098 { "debug", 'd', POPT_ARG_INT, &debug_level, 0, "debug level", "integer" },
2099 { "file-check-parent-mtime", 0, POPT_ARG_NONE, &file_check_parent_mtime, 0, "check mtime of remote file instead of parent directory mtime", NULL },
2100 { "read-write", 0, POPT_ARG_NONE, &read_write, 0, "read/write cache", NULL },
2106 const char **extra_argv;
2110 #ifdef REMOTE_CACHE_VERS
2112 #define XSTR(x) STR(x)
2113 printf("remote-cache version %s\n", XSTR(REMOTE_CACHE_VERS));
2116 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
2117 while ((opt = poptGetNextOpt(pc)) != -1) {
2120 fprintf(stderr, "Invalid option %s: %s\n",
2121 poptBadOption(pc, 0), poptStrerror(opt));
2126 /* setup the remaining options for the main program to use */
2127 extra_argv = poptGetArgs(pc);
2130 while (extra_argv[extra_argc]) extra_argc++;
2133 if (export == NULL) {
2134 fprintf(stderr, "You must specify --export\n");
2137 bzero(&st, sizeof(st));
2138 ret = lstat(export, &st);
2140 fprintf(stderr, "Could not stat() --export %s\n", export);
2142 fprintf(stderr, "Aborting\n");
2145 if ((st.st_mode & S_IFMT) != S_IFDIR) {
2146 fprintf(stderr, "--export %s is not a directory\n", export);
2147 fprintf(stderr, "Aborting\n");
2152 if (cache == NULL) {
2153 fprintf(stderr, "You must specify --cache\n");
2156 bzero(&st, sizeof(st));
2157 ret = lstat(cache, &st);
2159 fprintf(stderr, "Could not stat() --cache %s\n", cache);
2161 fprintf(stderr, "Aborting\n");
2164 if ((st.st_mode & S_IFMT) != S_IFDIR) {
2165 fprintf(stderr, "--cache %s is not a directory\n", cache);
2166 fprintf(stderr, "Aborting\n");
2170 if (remote == NULL) {
2171 fprintf(stderr, "You must specify --remote\n");
2174 bzero(&st, sizeof(st));
2175 ret = lstat(remote, &st);
2177 fprintf(stderr, "Could not stat() --remote %s\n", remote);
2179 fprintf(stderr, "Aborting\n");
2182 if ((st.st_mode & S_IFMT) != S_IFDIR) {
2183 fprintf(stderr, "--remote %s is not a directory\n", remote);
2184 fprintf(stderr, "Aborting\n");
2189 ret = mkdir(FILE_MIGRATE_LIMITER_DIR, 0777);
2190 if ((ret != 0) && (errno != EEXIST)) {
2191 printf("ERROR: could not create lock file directory %s\n", FILE_MIGRATE_LIMITER_DIR);
2196 ret = creat(FILE_MIGRATE_LIMITER, 0777);
2198 printf("ERROR: could not create lock file %s\n", FILE_MIGRATE_LIMITER);
2202 chmod(FILE_MIGRATE_LIMITER, 0777);
2204 if (dir_min_inodes < 1) {
2205 printf("ERROR: --dir-min-inodes must be > 0\n");
2209 if (dir_min_blocks < 1) {
2210 printf("ERROR: --dir-min-blocks must be > 0\n");
2214 if (file_min_inodes < 1) {
2215 printf("ERROR: --file-min-inodes must be > 0\n");
2219 if (file_min_blocks < 1) {
2220 printf("ERROR: --file-min-blocks must be > 0\n");
2224 fuse_argv[1] = export;
2226 printf("Remote caching of %s to temp storage %s exporting through %s\n", remote, cache, export);
2228 printf("here we go ...\n");
2230 log_fd = creat("/var/log/log.remote-cache", 0777);
2232 fprintf(stderr, "Could not open logfile. Aborting\n");
2236 return fuse_main(8, discard_const(fuse_argv), &remote_cache_ops, NULL);