fuse_node.c revision 279536
179265Sdillon/*
279265Sdillon * Copyright (c) 2007-2009 Google Inc. and Amit Singh
379265Sdillon * All rights reserved.
479265Sdillon *
5118848Simp * Redistribution and use in source and binary forms, with or without
6118848Simp * modification, are permitted provided that the following conditions are
7118848Simp * met:
8118848Simp *
9118848Simp * * Redistributions of source code must retain the above copyright
10118848Simp *   notice, this list of conditions and the following disclaimer.
11118848Simp * * Redistributions in binary form must reproduce the above
12118848Simp *   copyright notice, this list of conditions and the following disclaimer
13118848Simp *   in the documentation and/or other materials provided with the
14118848Simp *   distribution.
15118848Simp * * Neither the name of Google Inc. nor the names of its
16118848Simp *   contributors may be used to endorse or promote products derived from
1779265Sdillon *   this software without specific prior written permission.
18118848Simp *
19118848Simp * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20118848Simp * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21118848Simp * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22118848Simp * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23118848Simp * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24118848Simp * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25118848Simp * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26118848Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27118848Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28118848Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29118848Simp * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3079265Sdillon *
3179265Sdillon * Copyright (C) 2005 Csaba Henk.
32126588Sbde * All rights reserved.
3379265Sdillon *
3479265Sdillon * Redistribution and use in source and binary forms, with or without
35116226Sobrien * modification, are permitted provided that the following conditions
36116226Sobrien * are met:
37116226Sobrien * 1. Redistributions of source code must retain the above copyright
38134649Sscottl *    notice, this list of conditions and the following disclaimer.
39134649Sscottl * 2. Redistributions in binary form must reproduce the above copyright
4079265Sdillon *    notice, this list of conditions and the following disclaimer in the
4179265Sdillon *    documentation and/or other materials provided with the distribution.
4282314Speter *
4379265Sdillon * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4479265Sdillon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4582314Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4679265Sdillon * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
47104964Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4879265Sdillon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4982314Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50125314Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5179265Sdillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5279265Sdillon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5379265Sdillon * SUCH DAMAGE.
5479265Sdillon */
5579265Sdillon
56126588Sbde#include <sys/cdefs.h>
57126588Sbde__FBSDID("$FreeBSD: head/sys/fs/fuse/fuse_node.c 279536 2015-03-02 19:04:27Z trasz $");
5879265Sdillon
59134461Siedowse#include <sys/types.h>
60134461Siedowse#include <sys/module.h>
61134461Siedowse#include <sys/systm.h>
62134461Siedowse#include <sys/errno.h>
6382314Speter#include <sys/param.h>
6482314Speter#include <sys/kernel.h>
6582314Speter#include <sys/conf.h>
6682314Speter#include <sys/uio.h>
6782314Speter#include <sys/malloc.h>
6882314Speter#include <sys/queue.h>
6979265Sdillon#include <sys/lock.h>
7079265Sdillon#include <sys/sx.h>
7179265Sdillon#include <sys/mutex.h>
7279265Sdillon#include <sys/proc.h>
7379265Sdillon#include <sys/vnode.h>
7479265Sdillon#include <sys/namei.h>
7579265Sdillon#include <sys/mount.h>
76137104Salc#include <sys/sysctl.h>
7782314Speter#include <sys/fcntl.h>
7882314Speter#include <sys/fnv_hash.h>
7982314Speter#include <sys/priv.h>
8082314Speter#include <security/mac/mac_framework.h>
8179265Sdillon#include <vm/vm.h>
8279265Sdillon#include <vm/vm_extern.h>
8382314Speter
84126588Sbde#include "fuse.h"
8579265Sdillon#include "fuse_node.h"
8679265Sdillon#include "fuse_internal.h"
8779265Sdillon#include "fuse_io.h"
8879265Sdillon#include "fuse_ipc.h"
8979265Sdillon
9079265Sdillon#define FUSE_DEBUG_MODULE VNOPS
9179265Sdillon#include "fuse_debug.h"
9279265Sdillon
93170170SattilioMALLOC_DEFINE(M_FUSEVN, "fuse_vnode", "fuse vnode private data");
94126588Sbde
95170170Sattiliostatic int fuse_node_count = 0;
96126588Sbde
97126588SbdeSYSCTL_INT(_vfs_fuse, OID_AUTO, node_count, CTLFLAG_RD,
9882314Speter    &fuse_node_count, 0, "");
9979265Sdillon
100161489Salcint	fuse_data_cache_enable = 1;
10182314Speter
10282314SpeterSYSCTL_INT(_vfs_fuse, OID_AUTO, data_cache_enable, CTLFLAG_RW,
10382314Speter    &fuse_data_cache_enable, 0, "");
10482314Speter
10582314Speterint	fuse_data_cache_invalidate = 0;
106166637Salc
10782314SpeterSYSCTL_INT(_vfs_fuse, OID_AUTO, data_cache_invalidate, CTLFLAG_RW,
10882314Speter    &fuse_data_cache_invalidate, 0, "");
10982314Speter
110100193Salcint	fuse_mmap_enable = 1;
111166508Salc
11299571SpeterSYSCTL_INT(_vfs_fuse, OID_AUTO, mmap_enable, CTLFLAG_RW,
113166508Salc    &fuse_mmap_enable, 0, "");
114100331Salc
115100193Salcint	fuse_refresh_size = 0;
11682314Speter
11782314SpeterSYSCTL_INT(_vfs_fuse, OID_AUTO, refresh_size, CTLFLAG_RW,
118170170Sattilio    &fuse_refresh_size, 0, "");
11982314Speter
12082314Speterint	fuse_sync_resize = 1;
121153940Snetchild
12282314SpeterSYSCTL_INT(_vfs_fuse, OID_AUTO, sync_resize, CTLFLAG_RW,
12382314Speter    &fuse_sync_resize, 0, "");
124126588Sbde
12582314Speterint	fuse_fix_broken_io = 0;
12682314Speter
12782314SpeterSYSCTL_INT(_vfs_fuse, OID_AUTO, fix_broken_io, CTLFLAG_RW,
12882314Speter    &fuse_fix_broken_io, 0, "");
129166637Salc
130137104Salcstatic void
131137104Salcfuse_vnode_init(struct vnode *vp, struct fuse_vnode_data *fvdat,
13282314Speter    uint64_t nodeid, enum vtype vtyp)
133137104Salc{
13482314Speter	int i;
13582314Speter
13682314Speter	fvdat->nid = nodeid;
137125314Sjeff	if (nodeid == FUSE_ROOT_ID) {
13882314Speter		vp->v_vflag |= VV_ROOT;
13982314Speter	}
140134461Siedowse	vp->v_type = vtyp;
14182314Speter	vp->v_data = fvdat;
142166637Salc
14382314Speter	for (i = 0; i < FUFH_MAXTYPE; i++)
14482314Speter		fvdat->fufh[i].fh_type = FUFH_INVALID;
145137268Sjhb
146131481Sjhb	atomic_add_acq_int(&fuse_node_count, 1);
147137268Sjhb}
148170307Sjeff
149131473Sjhbvoid
150170307Sjefffuse_vnode_destroy(struct vnode *vp)
15182314Speter{
152131481Sjhb	struct fuse_vnode_data *fvdat = vp->v_data;
15382314Speter
154137104Salc	vp->v_data = NULL;
155166637Salc	free(fvdat, M_FUSEVN);
156166637Salc
15779265Sdillon	atomic_subtract_acq_int(&fuse_node_count, 1);
15879265Sdillon}
15979265Sdillon
16079265Sdillonstatic int
161113070Sdesfuse_vnode_cmp(struct vnode *vp, void *nidp)
162125314Sjeff{
163125314Sjeff	return (VTOI(vp) != *((uint64_t *)nidp));
164125314Sjeff}
165125314Sjeff
166125314Sjeffstatic uint32_t __inline
167141247Sssouhlalfuse_vnode_hash(uint64_t id)
168125314Sjeff{
169125314Sjeff	return (fnv_32_buf(&id, sizeof(id), FNV1_32_INIT));
170125314Sjeff}
171125314Sjeff
172125314Sjeffstatic int
173125314Sjefffuse_vnode_alloc(struct mount *mp,
174125314Sjeff    struct thread *td,
175125314Sjeff    uint64_t nodeid,
176125314Sjeff    enum vtype vtyp,
177125314Sjeff    struct vnode **vpp)
178125314Sjeff{
179141247Sssouhlal	struct fuse_vnode_data *fvdat;
180170307Sjeff	struct vnode *vp2;
181163709Sjb	int err = 0;
182141247Sssouhlal
183166188Sjeff	FS_DEBUG("been asked for vno #%ju\n", (uintmax_t)nodeid);
184170307Sjeff
185125314Sjeff	if (vtyp == VNON) {
186125314Sjeff		return EINVAL;
187	}
188	*vpp = NULL;
189	err = vfs_hash_get(mp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE, td, vpp,
190	    fuse_vnode_cmp, &nodeid);
191	if (err)
192		return (err);
193
194	if (*vpp) {
195		MPASS((*vpp)->v_type == vtyp && (*vpp)->v_data != NULL);
196		FS_DEBUG("vnode taken from hash\n");
197		return (0);
198	}
199	fvdat = malloc(sizeof(*fvdat), M_FUSEVN, M_WAITOK | M_ZERO);
200	err = getnewvnode("fuse", mp, &fuse_vnops, vpp);
201	if (err) {
202		free(fvdat, M_FUSEVN);
203		return (err);
204	}
205	lockmgr((*vpp)->v_vnlock, LK_EXCLUSIVE, NULL);
206	fuse_vnode_init(*vpp, fvdat, nodeid, vtyp);
207	err = insmntque(*vpp, mp);
208	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
209	if (err) {
210		free(fvdat, M_FUSEVN);
211		*vpp = NULL;
212		return (err);
213	}
214	err = vfs_hash_insert(*vpp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE,
215	    td, &vp2, fuse_vnode_cmp, &nodeid);
216	if (err)
217		return (err);
218	if (vp2 != NULL) {
219		*vpp = vp2;
220		return (0);
221	}
222
223	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
224
225	return (0);
226}
227
228int
229fuse_vnode_get(struct mount *mp,
230    uint64_t nodeid,
231    struct vnode *dvp,
232    struct vnode **vpp,
233    struct componentname *cnp,
234    enum vtype vtyp)
235{
236	struct thread *td = (cnp != NULL ? cnp->cn_thread : curthread);
237	int err = 0;
238
239	debug_printf("dvp=%p\n", dvp);
240
241	err = fuse_vnode_alloc(mp, td, nodeid, vtyp, vpp);
242	if (err) {
243		return err;
244	}
245	if (dvp != NULL) {
246		MPASS((cnp->cn_flags & ISDOTDOT) == 0);
247		MPASS(!(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'));
248		fuse_vnode_setparent(*vpp, dvp);
249	}
250	if (dvp != NULL && cnp != NULL && (cnp->cn_flags & MAKEENTRY) != 0) {
251		ASSERT_VOP_LOCKED(*vpp, "fuse_vnode_get");
252		ASSERT_VOP_LOCKED(dvp, "fuse_vnode_get");
253		cache_enter(dvp, *vpp, cnp);
254	}
255
256	/*
257	 * In userland, libfuse uses cached lookups for dot and dotdot entries,
258	 * thus it does not really bump the nlookup counter for forget.
259	 * Follow the same semantic and avoid tu bump it in order to keep
260	 * nlookup counters consistent.
261	 */
262	if (cnp == NULL || ((cnp->cn_flags & ISDOTDOT) == 0 &&
263	    (cnp->cn_namelen != 1 || cnp->cn_nameptr[0] != '.')))
264		VTOFUD(*vpp)->nlookup++;
265
266	return 0;
267}
268
269void
270fuse_vnode_open(struct vnode *vp, int32_t fuse_open_flags, struct thread *td)
271{
272	/*
273         * Funcation is called for every vnode open.
274         * Merge fuse_open_flags it may be 0
275         *
276         * XXXIP: Handle FOPEN_KEEP_CACHE
277         */
278        /*
279	  * Ideally speaking, direct io should be enabled on
280         * fd's but do not see of any way of providing that
281         * this implementation.
282
283         * Also cannot think of a reason why would two
284         * different fd's on same vnode would like
285         * have DIRECT_IO turned on and off. But linux
286         * based implementation works on an fd not an
287         * inode and provides such a feature.
288         *
289         * XXXIP: Handle fd based DIRECT_IO
290         */
291	if (fuse_open_flags & FOPEN_DIRECT_IO) {
292		VTOFUD(vp)->flag |= FN_DIRECTIO;
293	} else {
294	        VTOFUD(vp)->flag &= ~FN_DIRECTIO;
295	}
296
297	if (vnode_vtype(vp) == VREG) {
298		/* XXXIP prevent getattr, by using cached node size */
299		vnode_create_vobject(vp, 0, td);
300	}
301}
302
303int
304fuse_vnode_savesize(struct vnode *vp, struct ucred *cred)
305{
306	struct fuse_vnode_data *fvdat = VTOFUD(vp);
307	struct thread *td = curthread;
308	struct fuse_filehandle *fufh = NULL;
309	struct fuse_dispatcher fdi;
310	struct fuse_setattr_in *fsai;
311	int err = 0;
312
313	FS_DEBUG("inode=%ju size=%ju\n", (uintmax_t)VTOI(vp),
314	    (uintmax_t)fvdat->filesize);
315	ASSERT_VOP_ELOCKED(vp, "fuse_io_extend");
316
317	if (fuse_isdeadfs(vp)) {
318		return EBADF;
319	}
320	if (vnode_vtype(vp) == VDIR) {
321		return EISDIR;
322	}
323	if (vfs_isrdonly(vnode_mount(vp))) {
324		return EROFS;
325	}
326	if (cred == NULL) {
327		cred = td->td_ucred;
328	}
329	fdisp_init(&fdi, sizeof(*fsai));
330	fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
331	fsai = fdi.indata;
332	fsai->valid = 0;
333
334	/* Truncate to a new value. */
335	    fsai->size = fvdat->filesize;
336	fsai->valid |= FATTR_SIZE;
337
338	fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
339	if (fufh) {
340		fsai->fh = fufh->fh_id;
341		fsai->valid |= FATTR_FH;
342	}
343	err = fdisp_wait_answ(&fdi);
344	fdisp_destroy(&fdi);
345	if (err == 0)
346		fvdat->flag &= ~FN_SIZECHANGE;
347
348	return err;
349}
350
351void
352fuse_vnode_refreshsize(struct vnode *vp, struct ucred *cred)
353{
354
355	struct fuse_vnode_data *fvdat = VTOFUD(vp);
356	struct vattr va;
357
358	if ((fvdat->flag & FN_SIZECHANGE) != 0 ||
359	    (fuse_refresh_size == 0 && fvdat->filesize != 0))
360		return;
361
362	VOP_GETATTR(vp, &va, cred);
363	FS_DEBUG("refreshed file size: %jd\n", (intmax_t)VTOFUD(vp)->filesize);
364}
365
366int
367fuse_vnode_setsize(struct vnode *vp, struct ucred *cred, off_t newsize)
368{
369	struct fuse_vnode_data *fvdat = VTOFUD(vp);
370	off_t oldsize;
371	int err = 0;
372
373	FS_DEBUG("inode=%ju oldsize=%ju newsize=%ju\n",
374	    (uintmax_t)VTOI(vp), (uintmax_t)fvdat->filesize,
375	    (uintmax_t)newsize);
376	ASSERT_VOP_ELOCKED(vp, "fuse_vnode_setsize");
377
378	oldsize = fvdat->filesize;
379	fvdat->filesize = newsize;
380	fvdat->flag |= FN_SIZECHANGE;
381
382	if (newsize < oldsize) {
383		err = vtruncbuf(vp, cred, newsize, fuse_iosize(vp));
384	}
385	vnode_pager_setsize(vp, newsize);
386	return err;
387}
388