static void workspace_nfs_open(fuse_req_t req, struct workspace_fh_struct *fh) { struct resource_struct *resource=fh->object->resource; struct net_nfs_export_struct *nfs_export=(struct net_nfs_export_struct *) resource->data; struct nfs_context *nfs_ctx=(struct nfs_context *) nfs_export->data; char *path=fh->pathinfo.path + fh->relpath; struct nfsfh *nfsfh=NULL; int result=0; if (strlen(path)==0) path=(char *) rootpath; logoutput("workspace_nfs_open, path %s", path); pthread_mutex_lock(&nfs_export->mutex); result=nfs_open(nfs_ctx, path, fh->flags, &nfsfh); pthread_mutex_unlock(&nfs_export->mutex); if (result==0) { fh->handle.data=(void *) nfsfh; fuse_reply_open(req, fh->fi); } else { fuse_reply_err(req, abs(result)); } free_path_pathinfo(&fh->pathinfo); }
static struct file_context * open_file(const char *url, int flags) { struct file_context *file_context; file_context = malloc(sizeof(struct file_context)); if (file_context == NULL) { fprintf(stderr, "Failed to malloc file_context\n"); return NULL; } file_context->fd = -1; file_context->nfs = NULL; file_context->nfsfh = NULL; file_context->url = NULL; file_context->nfs = nfs_init_context(); if (file_context->nfs == NULL) { fprintf(stderr, "failed to init context\n"); free_file_context(file_context); return NULL; } file_context->url = nfs_parse_url_full(file_context->nfs, url); if (file_context->url == NULL) { fprintf(stderr, "%s\n", nfs_get_error(file_context->nfs)); free_file_context(file_context); return NULL; } if (nfs_mount(file_context->nfs, file_context->url->server, file_context->url->path) != 0) { fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(file_context->nfs)); free_file_context(file_context); return NULL; } if (flags == O_RDONLY) { if (nfs_open(file_context->nfs, file_context->url->file, flags, &file_context->nfsfh) != 0) { fprintf(stderr, "Failed to open file %s: %s\n", file_context->url->file, nfs_get_error(file_context->nfs)); free_file_context(file_context); return NULL; } } else { if (nfs_creat(file_context->nfs, file_context->url->file, 0660, &file_context->nfsfh) != 0) { fprintf(stderr, "Failed to creat file %s: %s\n", file_context->url->file, nfs_get_error(file_context->nfs)); free_file_context(file_context); return NULL; } } return file_context; }
/* * Open file */ static int nfs_opendir(struct inode *inode, struct file *filp) { int res = 0; lock_kernel(); /* Call generic open code in order to cache credentials */ if (!res) res = nfs_open(inode, filp); unlock_kernel(); return res; }
File::File(Mount& mnt, const fs::path& p, yt::FDMode fdmode, CreateIfNecessary create_if_necessary) : ctx_(mnt.ctx_) { /* mode is not the same for both nfs_creat and nfs_open. For nfs_open * mode has the same meaning as flags but for nfs_creat is the mode used * for file creation. The newer version of libnfs makes this difference * obvious by introducing a second field 'flags' along with mode. We should * consider upgrading to the latest libnfs then and drop the workaround * introduced below. */ const int mode = create_if_necessary == CreateIfNecessary::T ? translate_create_mode(fdmode) : translate_mode(fdmode); LOG_TRACE(mnt.share_name() << ", " << p << ", mode " << mode << ", create: " << create_if_necessary); nfsfh* fh; int ret; if (create_if_necessary == CreateIfNecessary::T) { ret = nfs_creat(ctx_.get(), p.string().c_str(), mode, &fh); } else { ret = nfs_open(ctx_.get(), p.string().c_str(), mode, &fh); } if (ret < 0) { const char* err = nfs_get_error(ctx_.get()); std::stringstream ss; ss << "Failed to open " << p << ": " << err; LOG_ERROR(ss.str()); throw Exception(ss.str().c_str()); } else { fh_ = std::shared_ptr<nfsfh>(fh, FhDeleter(ctx_)); } }
/* * Open file */ static int nfs_file_open(struct inode *inode, struct file *filp) { int res; dprintk("NFS: open file(%pD2)\n", filp); nfs_inc_stats(inode, NFSIOS_VFSOPEN); res = nfs_check_flags(filp->f_flags); if (res) return res; res = nfs_open(inode, filp); return res; }
static int fuse_nfs_open(const char *path, struct fuse_file_info *fi) { int ret = 0; struct nfsfh *nfsfh; fi->fh = 0; ret = nfs_open(nfs, path, fi->flags, &nfsfh); if (ret < 0) { return ret; } fi->fh = (uint64_t)nfsfh; return ret; }
/* * Open file */ static int nfs_file_open(struct inode *inode, struct file *filp) { int res; dprintk("NFS: open file(%s/%s)\n", filp->f_path.dentry->d_parent->d_name.name, filp->f_path.dentry->d_name.name); res = nfs_check_flags(filp->f_flags); if (res) return res; nfs_inc_stats(inode, NFSIOS_VFSOPEN); res = nfs_open(inode, filp); return res; }
int open(const char *path, int flags, mode_t mode) { if (!strncmp(path, "nfs:", 4)) { struct nfs_context *nfs; struct nfs_url *url; struct nfsfh *fh = NULL; int ret, fd; LD_NFS_DPRINTF(9, "open(%s, %x, %o)", path, flags, mode); nfs = nfs_init_context(); if (nfs == NULL) { LD_NFS_DPRINTF(1, "Failed to create context"); errno = ENOMEM; return -1; } url = nfs_parse_url_full(nfs, path); if (url == NULL) { LD_NFS_DPRINTF(1, "Failed to parse URL: %s\n", nfs_get_error(nfs)); nfs_destroy_context(nfs); errno = EINVAL; return -1; } if (nfs_mount(nfs, url->server, url->path) != 0) { LD_NFS_DPRINTF(1, "Failed to mount nfs share : %s\n", nfs_get_error(nfs)); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = EINVAL; return -1; } if (flags & O_CREAT) { if ((ret = nfs_creat(nfs, url->file, mode, &fh)) != 0) { LD_NFS_DPRINTF(1, "Failed to creat nfs file : " "%s\n", nfs_get_error(nfs)); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = -ret; return -1; } } else { if ((ret = nfs_open(nfs, url->file, flags, &fh)) != 0) { LD_NFS_DPRINTF(1, "Failed to open nfs file : " "%s\n", nfs_get_error(nfs)); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = -ret; return -1; } } fd = nfs_get_fd(nfs); if (fd >= NFS_MAX_FD) { LD_NFS_DPRINTF(1, "Too many files open"); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = ENFILE; return -1; } nfs_fd_list[fd].is_nfs = 1; nfs_fd_list[fd].nfs = nfs; nfs_fd_list[fd].fh = fh; nfs_fd_list[fd].path = strdup(path); nfs_fd_list[fd].flags = flags; nfs_fd_list[fd].mode = mode; nfs_destroy_url(url); LD_NFS_DPRINTF(9, "open(%s) == %d", path, fd); return fd; } return real_open(path, flags, mode); }
int dup2(int oldfd, int newfd) { close(newfd); if (nfs_fd_list[oldfd].is_nfs == 1) { struct nfs_context *nfs; struct nfs_url *url; struct nfsfh *fh = NULL; int ret, fd; LD_NFS_DPRINTF(9, "dup2(%s:%d, %d)", nfs_fd_list[oldfd].path, oldfd, newfd); nfs = nfs_init_context(); if (nfs == NULL) { LD_NFS_DPRINTF(1, "Failed to create context"); errno = ENOMEM; return -1; } url = nfs_parse_url_full(nfs, nfs_fd_list[oldfd].path); if (url == NULL) { LD_NFS_DPRINTF(1, "Failed to parse URL: %s\n", nfs_get_error(nfs)); nfs_destroy_context(nfs); errno = EINVAL; return -1; } if (nfs_mount(nfs, url->server, url->path) != 0) { LD_NFS_DPRINTF(1, "Failed to mount nfs share : %s\n", nfs_get_error(nfs)); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = EINVAL; return -1; } if ((ret = nfs_open(nfs, url->file, nfs_fd_list[oldfd].mode, &fh)) != 0) { LD_NFS_DPRINTF(1, "Failed to open nfs file : %s\n", nfs_get_error(nfs)); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = -ret; return -1; } /* We could actually end on the right descriptor by chance */ if (nfs_get_fd(nfs) != newfd) { if (real_dup2(nfs_get_fd(nfs), newfd) < 0) { LD_NFS_DPRINTF(1, "Failed to dup2 file : %d", errno); return -1; } close(rpc_get_fd(nfs_get_rpc_context(nfs))); rpc_set_fd(nfs_get_rpc_context(nfs), newfd); } fd = nfs_get_fd(nfs); if (fd >= NFS_MAX_FD) { LD_NFS_DPRINTF(1, "Too many files open"); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = ENFILE; return -1; } nfs_fd_list[fd].is_nfs = 1; nfs_fd_list[fd].nfs = nfs; nfs_fd_list[fd].fh = fh; nfs_fd_list[fd].path = strdup(nfs_fd_list[oldfd].path); nfs_fd_list[fd].flags = nfs_fd_list[oldfd].flags; nfs_fd_list[fd].mode = nfs_fd_list[oldfd].mode; nfs_destroy_url(url); LD_NFS_DPRINTF(9, "dup2(%s) successful", nfs_fd_list[oldfd].path); return fd; } return real_dup2(oldfd, newfd); }
static struct file_context * open_file(const char *url, int flags) { struct file_context *file_context; char *server = NULL, *path = NULL, *file = NULL, *strp; file_context = malloc(sizeof(struct file_context)); if (file_context == NULL) { fprintf(stderr, "Failed to malloc file_context\n"); return NULL; } file_context->is_nfs = 0; file_context->fd = -1; file_context->nfs = NULL; file_context->nfsfh = NULL; if (strncmp(url, "nfs://", 6)) { file_context->is_nfs = 0; file_context->fd = open(url, flags, 0660); if (file_context->fd == -1) { fprintf(stderr, "Failed to open %s\n", url); free_file_context(file_context); return NULL; } return file_context; } file_context->is_nfs = 1; file_context->nfs = nfs_init_context(); if (file_context->nfs == NULL) { fprintf(stderr, "failed to init context\n"); free_file_context(file_context); return NULL; } server = strdup(url + 6); if (server == NULL) { fprintf(stderr, "Failed to strdup server string\n"); free_file_context(file_context); return NULL; } if (server[0] == '/' || server[0] == '\0') { fprintf(stderr, "Invalid server string.\n"); free(server); free_file_context(file_context); return NULL; } strp = strchr(server, '/'); path = strdup(strp); *strp = 0; if (path == NULL) { fprintf(stderr, "Invalid URL specified.\n"); free(server); free_file_context(file_context); return NULL; } strp = strrchr(path, '/'); if (strp == NULL) { fprintf(stderr, "Invalid URL specified.\n"); free(path); free(server); free_file_context(file_context); return NULL; } file = strdup(strp); *strp = 0; if (nfs_mount(file_context->nfs, server, path) != 0) { fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(file_context->nfs)); free(file); free(path); free(server); free_file_context(file_context); return NULL; } if (flags == O_RDONLY) { if (nfs_open(file_context->nfs, file, flags, &file_context->nfsfh) != 0) { fprintf(stderr, "Failed to open file : %s\n", nfs_get_error(file_context->nfs)); free(file); free(path); free(server); free_file_context(file_context); return NULL; } } else { if (nfs_creat(file_context->nfs, file, 0660, &file_context->nfsfh) != 0) { fprintf(stderr, "Failed to creat file %s %s\n", file, nfs_get_error(file_context->nfs)); free(file); free(path); free(server); free_file_context(file_context); return NULL; } } free(file); free(path); free(server); return file_context; }