Deleted Added
full compact
tmpfs_vnops.c (232821) tmpfs_vnops.c (232960)
1/* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
2
3/*-
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * 2005 program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * tmpfs vnode interface.
35 */
36#include <sys/cdefs.h>
1/* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
2
3/*-
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * 2005 program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * tmpfs vnode interface.
35 */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_vnops.c 232821 2012-03-11 12:19:58Z kib $");
37__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_vnops.c 232960 2012-03-14 09:15:50Z gleb $");
38
39#include <sys/param.h>
40#include <sys/fcntl.h>
41#include <sys/lockf.h>
42#include <sys/namei.h>
43#include <sys/priv.h>
44#include <sys/proc.h>
45#include <sys/sched.h>
46#include <sys/sf_buf.h>
47#include <sys/stat.h>
48#include <sys/systm.h>
38
39#include <sys/param.h>
40#include <sys/fcntl.h>
41#include <sys/lockf.h>
42#include <sys/namei.h>
43#include <sys/priv.h>
44#include <sys/proc.h>
45#include <sys/sched.h>
46#include <sys/sf_buf.h>
47#include <sys/stat.h>
48#include <sys/systm.h>
49#include <sys/sysctl.h>
49#include <sys/unistd.h>
50#include <sys/vnode.h>
51
52#include <vm/vm.h>
53#include <vm/vm_object.h>
54#include <vm/vm_page.h>
55#include <vm/vm_pager.h>
56
57#include <fs/tmpfs/tmpfs_vnops.h>
58#include <fs/tmpfs/tmpfs.h>
59
50#include <sys/unistd.h>
51#include <sys/vnode.h>
52
53#include <vm/vm.h>
54#include <vm/vm_object.h>
55#include <vm/vm_page.h>
56#include <vm/vm_pager.h>
57
58#include <fs/tmpfs/tmpfs_vnops.h>
59#include <fs/tmpfs/tmpfs.h>
60
61SYSCTL_DECL(_vfs_tmpfs);
62
63static volatile int tmpfs_rename_restarts;
64SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
65 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
66 "Times rename had to restart due to lock contention");
67
60/* --------------------------------------------------------------------- */
61
62static int
63tmpfs_lookup(struct vop_cachedlookup_args *v)
64{
65 struct vnode *dvp = v->a_dvp;
66 struct vnode **vpp = v->a_vpp;
67 struct componentname *cnp = v->a_cnp;
68
69 int error;
70 struct tmpfs_dirent *de;
71 struct tmpfs_node *dnode;
72
73 dnode = VP_TO_TMPFS_DIR(dvp);
74 *vpp = NULLVP;
75
76 /* Check accessibility of requested node as a first step. */
77 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
78 if (error != 0)
79 goto out;
80
81 /* We cannot be requesting the parent directory of the root node. */
82 MPASS(IMPLIES(dnode->tn_type == VDIR &&
83 dnode->tn_dir.tn_parent == dnode,
84 !(cnp->cn_flags & ISDOTDOT)));
85
86 TMPFS_ASSERT_LOCKED(dnode);
87 if (dnode->tn_dir.tn_parent == NULL) {
88 error = ENOENT;
89 goto out;
90 }
91 if (cnp->cn_flags & ISDOTDOT) {
92 int ltype = 0;
93
94 ltype = VOP_ISLOCKED(dvp);
95 vhold(dvp);
96 VOP_UNLOCK(dvp, 0);
97 /* Allocate a new vnode on the matching entry. */
98 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
99 cnp->cn_lkflags, vpp);
100
101 vn_lock(dvp, ltype | LK_RETRY);
102 vdrop(dvp);
103 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
104 VREF(dvp);
105 *vpp = dvp;
106 error = 0;
107 } else {
108 de = tmpfs_dir_lookup(dnode, NULL, cnp);
109 if (de != NULL && de->td_node == NULL)
110 cnp->cn_flags |= ISWHITEOUT;
111 if (de == NULL || de->td_node == NULL) {
112 /* The entry was not found in the directory.
113 * This is OK if we are creating or renaming an
114 * entry and are working on the last component of
115 * the path name. */
116 if ((cnp->cn_flags & ISLASTCN) &&
117 (cnp->cn_nameiop == CREATE || \
118 cnp->cn_nameiop == RENAME ||
119 (cnp->cn_nameiop == DELETE &&
120 cnp->cn_flags & DOWHITEOUT &&
121 cnp->cn_flags & ISWHITEOUT))) {
122 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
123 cnp->cn_thread);
124 if (error != 0)
125 goto out;
126
127 /* Keep the component name in the buffer for
128 * future uses. */
129 cnp->cn_flags |= SAVENAME;
130
131 error = EJUSTRETURN;
132 } else
133 error = ENOENT;
134 } else {
135 struct tmpfs_node *tnode;
136
137 /* The entry was found, so get its associated
138 * tmpfs_node. */
139 tnode = de->td_node;
140
141 /* If we are not at the last path component and
142 * found a non-directory or non-link entry (which
143 * may itself be pointing to a directory), raise
144 * an error. */
145 if ((tnode->tn_type != VDIR &&
146 tnode->tn_type != VLNK) &&
147 !(cnp->cn_flags & ISLASTCN)) {
148 error = ENOTDIR;
149 goto out;
150 }
151
152 /* If we are deleting or renaming the entry, keep
153 * track of its tmpfs_dirent so that it can be
154 * easily deleted later. */
155 if ((cnp->cn_flags & ISLASTCN) &&
156 (cnp->cn_nameiop == DELETE ||
157 cnp->cn_nameiop == RENAME)) {
158 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
159 cnp->cn_thread);
160 if (error != 0)
161 goto out;
162
163 /* Allocate a new vnode on the matching entry. */
164 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
165 cnp->cn_lkflags, vpp);
166 if (error != 0)
167 goto out;
168
169 if ((dnode->tn_mode & S_ISTXT) &&
170 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
171 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
172 error = EPERM;
173 vput(*vpp);
174 *vpp = NULL;
175 goto out;
176 }
177 cnp->cn_flags |= SAVENAME;
178 } else {
179 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
180 cnp->cn_lkflags, vpp);
181 }
182 }
183 }
184
185 /* Store the result of this lookup in the cache. Avoid this if the
186 * request was for creation, as it does not improve timings on
187 * emprical tests. */
188 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
189 cache_enter(dvp, *vpp, cnp);
190
191out:
192 /* If there were no errors, *vpp cannot be null and it must be
193 * locked. */
194 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
195
196 return error;
197}
198
199/* --------------------------------------------------------------------- */
200
201static int
202tmpfs_create(struct vop_create_args *v)
203{
204 struct vnode *dvp = v->a_dvp;
205 struct vnode **vpp = v->a_vpp;
206 struct componentname *cnp = v->a_cnp;
207 struct vattr *vap = v->a_vap;
208
209 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
210
211 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
212}
213/* --------------------------------------------------------------------- */
214
215static int
216tmpfs_mknod(struct vop_mknod_args *v)
217{
218 struct vnode *dvp = v->a_dvp;
219 struct vnode **vpp = v->a_vpp;
220 struct componentname *cnp = v->a_cnp;
221 struct vattr *vap = v->a_vap;
222
223 if (vap->va_type != VBLK && vap->va_type != VCHR &&
224 vap->va_type != VFIFO)
225 return EINVAL;
226
227 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
228}
229
230/* --------------------------------------------------------------------- */
231
232static int
233tmpfs_open(struct vop_open_args *v)
234{
235 struct vnode *vp = v->a_vp;
236 int mode = v->a_mode;
237
238 int error;
239 struct tmpfs_node *node;
240
241 MPASS(VOP_ISLOCKED(vp));
242
243 node = VP_TO_TMPFS_NODE(vp);
244
245 /* The file is still active but all its names have been removed
246 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
247 * it is about to die. */
248 if (node->tn_links < 1)
249 return (ENOENT);
250
251 /* If the file is marked append-only, deny write requests. */
252 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
253 error = EPERM;
254 else {
255 error = 0;
256 vnode_create_vobject(vp, node->tn_size, v->a_td);
257 }
258
259 MPASS(VOP_ISLOCKED(vp));
260 return error;
261}
262
263/* --------------------------------------------------------------------- */
264
265static int
266tmpfs_close(struct vop_close_args *v)
267{
268 struct vnode *vp = v->a_vp;
269
270 MPASS(VOP_ISLOCKED(vp));
271
272 /* Update node times. */
273 tmpfs_update(vp);
274
275 return (0);
276}
277
278/* --------------------------------------------------------------------- */
279
280int
281tmpfs_access(struct vop_access_args *v)
282{
283 struct vnode *vp = v->a_vp;
284 accmode_t accmode = v->a_accmode;
285 struct ucred *cred = v->a_cred;
286
287 int error;
288 struct tmpfs_node *node;
289
290 MPASS(VOP_ISLOCKED(vp));
291
292 node = VP_TO_TMPFS_NODE(vp);
293
294 switch (vp->v_type) {
295 case VDIR:
296 /* FALLTHROUGH */
297 case VLNK:
298 /* FALLTHROUGH */
299 case VREG:
300 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
301 error = EROFS;
302 goto out;
303 }
304 break;
305
306 case VBLK:
307 /* FALLTHROUGH */
308 case VCHR:
309 /* FALLTHROUGH */
310 case VSOCK:
311 /* FALLTHROUGH */
312 case VFIFO:
313 break;
314
315 default:
316 error = EINVAL;
317 goto out;
318 }
319
320 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
321 error = EPERM;
322 goto out;
323 }
324
325 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
326 node->tn_gid, accmode, cred, NULL);
327
328out:
329 MPASS(VOP_ISLOCKED(vp));
330
331 return error;
332}
333
334/* --------------------------------------------------------------------- */
335
336int
337tmpfs_getattr(struct vop_getattr_args *v)
338{
339 struct vnode *vp = v->a_vp;
340 struct vattr *vap = v->a_vap;
341
342 struct tmpfs_node *node;
343
344 node = VP_TO_TMPFS_NODE(vp);
345
346 tmpfs_update(vp);
347
348 vap->va_type = vp->v_type;
349 vap->va_mode = node->tn_mode;
350 vap->va_nlink = node->tn_links;
351 vap->va_uid = node->tn_uid;
352 vap->va_gid = node->tn_gid;
353 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
354 vap->va_fileid = node->tn_id;
355 vap->va_size = node->tn_size;
356 vap->va_blocksize = PAGE_SIZE;
357 vap->va_atime = node->tn_atime;
358 vap->va_mtime = node->tn_mtime;
359 vap->va_ctime = node->tn_ctime;
360 vap->va_birthtime = node->tn_birthtime;
361 vap->va_gen = node->tn_gen;
362 vap->va_flags = node->tn_flags;
363 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
364 node->tn_rdev : NODEV;
365 vap->va_bytes = round_page(node->tn_size);
366 vap->va_filerev = 0;
367
368 return 0;
369}
370
371/* --------------------------------------------------------------------- */
372
373/* XXX Should this operation be atomic? I think it should, but code in
374 * XXX other places (e.g., ufs) doesn't seem to be... */
375int
376tmpfs_setattr(struct vop_setattr_args *v)
377{
378 struct vnode *vp = v->a_vp;
379 struct vattr *vap = v->a_vap;
380 struct ucred *cred = v->a_cred;
381 struct thread *td = curthread;
382
383 int error;
384
385 MPASS(VOP_ISLOCKED(vp));
386
387 error = 0;
388
389 /* Abort if any unsettable attribute is given. */
390 if (vap->va_type != VNON ||
391 vap->va_nlink != VNOVAL ||
392 vap->va_fsid != VNOVAL ||
393 vap->va_fileid != VNOVAL ||
394 vap->va_blocksize != VNOVAL ||
395 vap->va_gen != VNOVAL ||
396 vap->va_rdev != VNOVAL ||
397 vap->va_bytes != VNOVAL)
398 error = EINVAL;
399
400 if (error == 0 && (vap->va_flags != VNOVAL))
401 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
402
403 if (error == 0 && (vap->va_size != VNOVAL))
404 error = tmpfs_chsize(vp, vap->va_size, cred, td);
405
406 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
407 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
408
409 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
410 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
411
412 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
413 vap->va_atime.tv_nsec != VNOVAL) ||
414 (vap->va_mtime.tv_sec != VNOVAL &&
415 vap->va_mtime.tv_nsec != VNOVAL) ||
416 (vap->va_birthtime.tv_sec != VNOVAL &&
417 vap->va_birthtime.tv_nsec != VNOVAL)))
418 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
419 &vap->va_birthtime, vap->va_vaflags, cred, td);
420
421 /* Update the node times. We give preference to the error codes
422 * generated by this function rather than the ones that may arise
423 * from tmpfs_update. */
424 tmpfs_update(vp);
425
426 MPASS(VOP_ISLOCKED(vp));
427
428 return error;
429}
430
431/* --------------------------------------------------------------------- */
432static int
433tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
434 vm_offset_t offset, size_t tlen, struct uio *uio)
435{
436 vm_page_t m;
437 int error, rv;
438
439 VM_OBJECT_LOCK(tobj);
440 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
441 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
442 if (m->valid != VM_PAGE_BITS_ALL) {
443 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
444 rv = vm_pager_get_pages(tobj, &m, 1, 0);
445 if (rv != VM_PAGER_OK) {
446 vm_page_lock(m);
447 vm_page_free(m);
448 vm_page_unlock(m);
449 VM_OBJECT_UNLOCK(tobj);
450 return (EIO);
451 }
452 } else
453 vm_page_zero_invalid(m, TRUE);
454 }
455 VM_OBJECT_UNLOCK(tobj);
456 error = uiomove_fromphys(&m, offset, tlen, uio);
457 VM_OBJECT_LOCK(tobj);
458 vm_page_lock(m);
459 vm_page_unwire(m, TRUE);
460 vm_page_unlock(m);
461 vm_page_wakeup(m);
462 VM_OBJECT_UNLOCK(tobj);
463
464 return (error);
465}
466
467static __inline int
468tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx,
469 vm_offset_t offset, size_t tlen, void *buf)
470{
471 struct uio uio;
472 struct iovec iov;
473
474 uio.uio_iovcnt = 1;
475 uio.uio_iov = &iov;
476 iov.iov_base = buf;
477 iov.iov_len = tlen;
478
479 uio.uio_offset = 0;
480 uio.uio_resid = tlen;
481 uio.uio_rw = UIO_READ;
482 uio.uio_segflg = UIO_SYSSPACE;
483 uio.uio_td = curthread;
484
485 return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio));
486}
487
488static int
489tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
490{
491 struct sf_buf *sf;
492 vm_pindex_t idx;
493 vm_page_t m;
494 vm_offset_t offset;
495 off_t addr;
496 size_t tlen;
497 char *ma;
498 int error;
499
500 addr = uio->uio_offset;
501 idx = OFF_TO_IDX(addr);
502 offset = addr & PAGE_MASK;
503 tlen = MIN(PAGE_SIZE - offset, len);
504
505 if ((vobj == NULL) ||
506 (vobj->resident_page_count == 0 && vobj->cache == NULL))
507 goto nocache;
508
509 VM_OBJECT_LOCK(vobj);
510lookupvpg:
511 if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
512 vm_page_is_valid(m, offset, tlen)) {
513 if ((m->oflags & VPO_BUSY) != 0) {
514 /*
515 * Reference the page before unlocking and sleeping so
516 * that the page daemon is less likely to reclaim it.
517 */
518 vm_page_reference(m);
519 vm_page_sleep(m, "tmfsmr");
520 goto lookupvpg;
521 }
522 vm_page_busy(m);
523 VM_OBJECT_UNLOCK(vobj);
524 error = uiomove_fromphys(&m, offset, tlen, uio);
525 VM_OBJECT_LOCK(vobj);
526 vm_page_wakeup(m);
527 VM_OBJECT_UNLOCK(vobj);
528 return (error);
529 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
530 KASSERT(offset == 0,
531 ("unexpected offset in tmpfs_mappedread for sendfile"));
532 if ((m->oflags & VPO_BUSY) != 0) {
533 /*
534 * Reference the page before unlocking and sleeping so
535 * that the page daemon is less likely to reclaim it.
536 */
537 vm_page_reference(m);
538 vm_page_sleep(m, "tmfsmr");
539 goto lookupvpg;
540 }
541 vm_page_busy(m);
542 VM_OBJECT_UNLOCK(vobj);
543 sched_pin();
544 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
545 ma = (char *)sf_buf_kva(sf);
546 error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma);
547 if (error == 0) {
548 if (tlen != PAGE_SIZE)
549 bzero(ma + tlen, PAGE_SIZE - tlen);
550 uio->uio_offset += tlen;
551 uio->uio_resid -= tlen;
552 }
553 sf_buf_free(sf);
554 sched_unpin();
555 VM_OBJECT_LOCK(vobj);
556 if (error == 0)
557 m->valid = VM_PAGE_BITS_ALL;
558 vm_page_wakeup(m);
559 VM_OBJECT_UNLOCK(vobj);
560 return (error);
561 }
562 VM_OBJECT_UNLOCK(vobj);
563nocache:
564 error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio);
565
566 return (error);
567}
568
569static int
570tmpfs_read(struct vop_read_args *v)
571{
572 struct vnode *vp = v->a_vp;
573 struct uio *uio = v->a_uio;
574
575 struct tmpfs_node *node;
576 vm_object_t uobj;
577 size_t len;
578 int resid;
579
580 int error = 0;
581
582 node = VP_TO_TMPFS_NODE(vp);
583
584 if (vp->v_type != VREG) {
585 error = EISDIR;
586 goto out;
587 }
588
589 if (uio->uio_offset < 0) {
590 error = EINVAL;
591 goto out;
592 }
593
594 node->tn_status |= TMPFS_NODE_ACCESSED;
595
596 uobj = node->tn_reg.tn_aobj;
597 while ((resid = uio->uio_resid) > 0) {
598 error = 0;
599 if (node->tn_size <= uio->uio_offset)
600 break;
601 len = MIN(node->tn_size - uio->uio_offset, resid);
602 if (len == 0)
603 break;
604 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
605 if ((error != 0) || (resid == uio->uio_resid))
606 break;
607 }
608
609out:
610
611 return error;
612}
613
614/* --------------------------------------------------------------------- */
615
616static int
617tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
618{
619 vm_pindex_t idx;
620 vm_page_t vpg, tpg;
621 vm_offset_t offset;
622 off_t addr;
623 size_t tlen;
624 int error, rv;
625
626 error = 0;
627
628 addr = uio->uio_offset;
629 idx = OFF_TO_IDX(addr);
630 offset = addr & PAGE_MASK;
631 tlen = MIN(PAGE_SIZE - offset, len);
632
633 if ((vobj == NULL) ||
634 (vobj->resident_page_count == 0 && vobj->cache == NULL)) {
635 vpg = NULL;
636 goto nocache;
637 }
638
639 VM_OBJECT_LOCK(vobj);
640lookupvpg:
641 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
642 vm_page_is_valid(vpg, offset, tlen)) {
643 if ((vpg->oflags & VPO_BUSY) != 0) {
644 /*
645 * Reference the page before unlocking and sleeping so
646 * that the page daemon is less likely to reclaim it.
647 */
648 vm_page_reference(vpg);
649 vm_page_sleep(vpg, "tmfsmw");
650 goto lookupvpg;
651 }
652 vm_page_busy(vpg);
653 vm_page_undirty(vpg);
654 VM_OBJECT_UNLOCK(vobj);
655 error = uiomove_fromphys(&vpg, offset, tlen, uio);
656 } else {
657 if (__predict_false(vobj->cache != NULL))
658 vm_page_cache_free(vobj, idx, idx + 1);
659 VM_OBJECT_UNLOCK(vobj);
660 vpg = NULL;
661 }
662nocache:
663 VM_OBJECT_LOCK(tobj);
664 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
665 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
666 if (tpg->valid != VM_PAGE_BITS_ALL) {
667 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
668 rv = vm_pager_get_pages(tobj, &tpg, 1, 0);
669 if (rv != VM_PAGER_OK) {
670 vm_page_lock(tpg);
671 vm_page_free(tpg);
672 vm_page_unlock(tpg);
673 error = EIO;
674 goto out;
675 }
676 } else
677 vm_page_zero_invalid(tpg, TRUE);
678 }
679 VM_OBJECT_UNLOCK(tobj);
680 if (vpg == NULL)
681 error = uiomove_fromphys(&tpg, offset, tlen, uio);
682 else {
683 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
684 pmap_copy_page(vpg, tpg);
685 }
686 VM_OBJECT_LOCK(tobj);
687 if (error == 0) {
688 KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
689 ("parts of tpg invalid"));
690 vm_page_dirty(tpg);
691 }
692 vm_page_lock(tpg);
693 vm_page_unwire(tpg, TRUE);
694 vm_page_unlock(tpg);
695 vm_page_wakeup(tpg);
696out:
697 VM_OBJECT_UNLOCK(tobj);
698 if (vpg != NULL) {
699 VM_OBJECT_LOCK(vobj);
700 vm_page_wakeup(vpg);
701 VM_OBJECT_UNLOCK(vobj);
702 }
703
704 return (error);
705}
706
707static int
708tmpfs_write(struct vop_write_args *v)
709{
710 struct vnode *vp = v->a_vp;
711 struct uio *uio = v->a_uio;
712 int ioflag = v->a_ioflag;
713
714 boolean_t extended;
715 int error = 0;
716 off_t oldsize;
717 struct tmpfs_node *node;
718 vm_object_t uobj;
719 size_t len;
720 int resid;
721
722 node = VP_TO_TMPFS_NODE(vp);
723 oldsize = node->tn_size;
724
725 if (uio->uio_offset < 0 || vp->v_type != VREG) {
726 error = EINVAL;
727 goto out;
728 }
729
730 if (uio->uio_resid == 0) {
731 error = 0;
732 goto out;
733 }
734
735 if (ioflag & IO_APPEND)
736 uio->uio_offset = node->tn_size;
737
738 if (uio->uio_offset + uio->uio_resid >
739 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
740 return (EFBIG);
741
742 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
743 return (EFBIG);
744
745 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
746 if (extended) {
747 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
748 FALSE);
749 if (error != 0)
750 goto out;
751 }
752
753 uobj = node->tn_reg.tn_aobj;
754 while ((resid = uio->uio_resid) > 0) {
755 if (node->tn_size <= uio->uio_offset)
756 break;
757 len = MIN(node->tn_size - uio->uio_offset, resid);
758 if (len == 0)
759 break;
760 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
761 if ((error != 0) || (resid == uio->uio_resid))
762 break;
763 }
764
765 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
766 (extended ? TMPFS_NODE_CHANGED : 0);
767
768 if (node->tn_mode & (S_ISUID | S_ISGID)) {
769 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
770 node->tn_mode &= ~(S_ISUID | S_ISGID);
771 }
772
773 if (error != 0)
774 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
775
776out:
777 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
778 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
779
780 return error;
781}
782
783/* --------------------------------------------------------------------- */
784
785static int
786tmpfs_fsync(struct vop_fsync_args *v)
787{
788 struct vnode *vp = v->a_vp;
789
790 MPASS(VOP_ISLOCKED(vp));
791
792 tmpfs_update(vp);
793
794 return 0;
795}
796
797/* --------------------------------------------------------------------- */
798
799static int
800tmpfs_remove(struct vop_remove_args *v)
801{
802 struct vnode *dvp = v->a_dvp;
803 struct vnode *vp = v->a_vp;
804
805 int error;
806 struct tmpfs_dirent *de;
807 struct tmpfs_mount *tmp;
808 struct tmpfs_node *dnode;
809 struct tmpfs_node *node;
810
811 MPASS(VOP_ISLOCKED(dvp));
812 MPASS(VOP_ISLOCKED(vp));
813
814 if (vp->v_type == VDIR) {
815 error = EISDIR;
816 goto out;
817 }
818
819 dnode = VP_TO_TMPFS_DIR(dvp);
820 node = VP_TO_TMPFS_NODE(vp);
821 tmp = VFS_TO_TMPFS(vp->v_mount);
822 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
823 MPASS(de != NULL);
824
825 /* Files marked as immutable or append-only cannot be deleted. */
826 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
827 (dnode->tn_flags & APPEND)) {
828 error = EPERM;
829 goto out;
830 }
831
832 /* Remove the entry from the directory; as it is a file, we do not
833 * have to change the number of hard links of the directory. */
834 tmpfs_dir_detach(dvp, de);
835 if (v->a_cnp->cn_flags & DOWHITEOUT)
836 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
837
838 /* Free the directory entry we just deleted. Note that the node
839 * referred by it will not be removed until the vnode is really
840 * reclaimed. */
841 tmpfs_free_dirent(tmp, de, TRUE);
842
843 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
844 error = 0;
845
846out:
847
848 return error;
849}
850
851/* --------------------------------------------------------------------- */
852
853static int
854tmpfs_link(struct vop_link_args *v)
855{
856 struct vnode *dvp = v->a_tdvp;
857 struct vnode *vp = v->a_vp;
858 struct componentname *cnp = v->a_cnp;
859
860 int error;
861 struct tmpfs_dirent *de;
862 struct tmpfs_node *node;
863
864 MPASS(VOP_ISLOCKED(dvp));
865 MPASS(cnp->cn_flags & HASBUF);
866 MPASS(dvp != vp); /* XXX When can this be false? */
867
868 node = VP_TO_TMPFS_NODE(vp);
869
870 /* XXX: Why aren't the following two tests done by the caller? */
871
872 /* Hard links of directories are forbidden. */
873 if (vp->v_type == VDIR) {
874 error = EPERM;
875 goto out;
876 }
877
878 /* Cannot create cross-device links. */
879 if (dvp->v_mount != vp->v_mount) {
880 error = EXDEV;
881 goto out;
882 }
883
884 /* Ensure that we do not overflow the maximum number of links imposed
885 * by the system. */
886 MPASS(node->tn_links <= LINK_MAX);
887 if (node->tn_links == LINK_MAX) {
888 error = EMLINK;
889 goto out;
890 }
891
892 /* We cannot create links of files marked immutable or append-only. */
893 if (node->tn_flags & (IMMUTABLE | APPEND)) {
894 error = EPERM;
895 goto out;
896 }
897
898 /* Allocate a new directory entry to represent the node. */
899 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
900 cnp->cn_nameptr, cnp->cn_namelen, &de);
901 if (error != 0)
902 goto out;
903
904 /* Insert the new directory entry into the appropriate directory. */
905 if (cnp->cn_flags & ISWHITEOUT)
906 tmpfs_dir_whiteout_remove(dvp, cnp);
907 tmpfs_dir_attach(dvp, de);
908
909 /* vp link count has changed, so update node times. */
910 node->tn_status |= TMPFS_NODE_CHANGED;
911 tmpfs_update(vp);
912
913 error = 0;
914
915out:
916 return error;
917}
918
919/* --------------------------------------------------------------------- */
920
68/* --------------------------------------------------------------------- */
69
70static int
71tmpfs_lookup(struct vop_cachedlookup_args *v)
72{
73 struct vnode *dvp = v->a_dvp;
74 struct vnode **vpp = v->a_vpp;
75 struct componentname *cnp = v->a_cnp;
76
77 int error;
78 struct tmpfs_dirent *de;
79 struct tmpfs_node *dnode;
80
81 dnode = VP_TO_TMPFS_DIR(dvp);
82 *vpp = NULLVP;
83
84 /* Check accessibility of requested node as a first step. */
85 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
86 if (error != 0)
87 goto out;
88
89 /* We cannot be requesting the parent directory of the root node. */
90 MPASS(IMPLIES(dnode->tn_type == VDIR &&
91 dnode->tn_dir.tn_parent == dnode,
92 !(cnp->cn_flags & ISDOTDOT)));
93
94 TMPFS_ASSERT_LOCKED(dnode);
95 if (dnode->tn_dir.tn_parent == NULL) {
96 error = ENOENT;
97 goto out;
98 }
99 if (cnp->cn_flags & ISDOTDOT) {
100 int ltype = 0;
101
102 ltype = VOP_ISLOCKED(dvp);
103 vhold(dvp);
104 VOP_UNLOCK(dvp, 0);
105 /* Allocate a new vnode on the matching entry. */
106 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
107 cnp->cn_lkflags, vpp);
108
109 vn_lock(dvp, ltype | LK_RETRY);
110 vdrop(dvp);
111 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
112 VREF(dvp);
113 *vpp = dvp;
114 error = 0;
115 } else {
116 de = tmpfs_dir_lookup(dnode, NULL, cnp);
117 if (de != NULL && de->td_node == NULL)
118 cnp->cn_flags |= ISWHITEOUT;
119 if (de == NULL || de->td_node == NULL) {
120 /* The entry was not found in the directory.
121 * This is OK if we are creating or renaming an
122 * entry and are working on the last component of
123 * the path name. */
124 if ((cnp->cn_flags & ISLASTCN) &&
125 (cnp->cn_nameiop == CREATE || \
126 cnp->cn_nameiop == RENAME ||
127 (cnp->cn_nameiop == DELETE &&
128 cnp->cn_flags & DOWHITEOUT &&
129 cnp->cn_flags & ISWHITEOUT))) {
130 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
131 cnp->cn_thread);
132 if (error != 0)
133 goto out;
134
135 /* Keep the component name in the buffer for
136 * future uses. */
137 cnp->cn_flags |= SAVENAME;
138
139 error = EJUSTRETURN;
140 } else
141 error = ENOENT;
142 } else {
143 struct tmpfs_node *tnode;
144
145 /* The entry was found, so get its associated
146 * tmpfs_node. */
147 tnode = de->td_node;
148
149 /* If we are not at the last path component and
150 * found a non-directory or non-link entry (which
151 * may itself be pointing to a directory), raise
152 * an error. */
153 if ((tnode->tn_type != VDIR &&
154 tnode->tn_type != VLNK) &&
155 !(cnp->cn_flags & ISLASTCN)) {
156 error = ENOTDIR;
157 goto out;
158 }
159
160 /* If we are deleting or renaming the entry, keep
161 * track of its tmpfs_dirent so that it can be
162 * easily deleted later. */
163 if ((cnp->cn_flags & ISLASTCN) &&
164 (cnp->cn_nameiop == DELETE ||
165 cnp->cn_nameiop == RENAME)) {
166 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
167 cnp->cn_thread);
168 if (error != 0)
169 goto out;
170
171 /* Allocate a new vnode on the matching entry. */
172 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
173 cnp->cn_lkflags, vpp);
174 if (error != 0)
175 goto out;
176
177 if ((dnode->tn_mode & S_ISTXT) &&
178 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
179 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
180 error = EPERM;
181 vput(*vpp);
182 *vpp = NULL;
183 goto out;
184 }
185 cnp->cn_flags |= SAVENAME;
186 } else {
187 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
188 cnp->cn_lkflags, vpp);
189 }
190 }
191 }
192
193 /* Store the result of this lookup in the cache. Avoid this if the
194 * request was for creation, as it does not improve timings on
195 * emprical tests. */
196 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
197 cache_enter(dvp, *vpp, cnp);
198
199out:
200 /* If there were no errors, *vpp cannot be null and it must be
201 * locked. */
202 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
203
204 return error;
205}
206
207/* --------------------------------------------------------------------- */
208
209static int
210tmpfs_create(struct vop_create_args *v)
211{
212 struct vnode *dvp = v->a_dvp;
213 struct vnode **vpp = v->a_vpp;
214 struct componentname *cnp = v->a_cnp;
215 struct vattr *vap = v->a_vap;
216
217 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
218
219 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
220}
221/* --------------------------------------------------------------------- */
222
223static int
224tmpfs_mknod(struct vop_mknod_args *v)
225{
226 struct vnode *dvp = v->a_dvp;
227 struct vnode **vpp = v->a_vpp;
228 struct componentname *cnp = v->a_cnp;
229 struct vattr *vap = v->a_vap;
230
231 if (vap->va_type != VBLK && vap->va_type != VCHR &&
232 vap->va_type != VFIFO)
233 return EINVAL;
234
235 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
236}
237
238/* --------------------------------------------------------------------- */
239
240static int
241tmpfs_open(struct vop_open_args *v)
242{
243 struct vnode *vp = v->a_vp;
244 int mode = v->a_mode;
245
246 int error;
247 struct tmpfs_node *node;
248
249 MPASS(VOP_ISLOCKED(vp));
250
251 node = VP_TO_TMPFS_NODE(vp);
252
253 /* The file is still active but all its names have been removed
254 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
255 * it is about to die. */
256 if (node->tn_links < 1)
257 return (ENOENT);
258
259 /* If the file is marked append-only, deny write requests. */
260 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
261 error = EPERM;
262 else {
263 error = 0;
264 vnode_create_vobject(vp, node->tn_size, v->a_td);
265 }
266
267 MPASS(VOP_ISLOCKED(vp));
268 return error;
269}
270
271/* --------------------------------------------------------------------- */
272
273static int
274tmpfs_close(struct vop_close_args *v)
275{
276 struct vnode *vp = v->a_vp;
277
278 MPASS(VOP_ISLOCKED(vp));
279
280 /* Update node times. */
281 tmpfs_update(vp);
282
283 return (0);
284}
285
286/* --------------------------------------------------------------------- */
287
288int
289tmpfs_access(struct vop_access_args *v)
290{
291 struct vnode *vp = v->a_vp;
292 accmode_t accmode = v->a_accmode;
293 struct ucred *cred = v->a_cred;
294
295 int error;
296 struct tmpfs_node *node;
297
298 MPASS(VOP_ISLOCKED(vp));
299
300 node = VP_TO_TMPFS_NODE(vp);
301
302 switch (vp->v_type) {
303 case VDIR:
304 /* FALLTHROUGH */
305 case VLNK:
306 /* FALLTHROUGH */
307 case VREG:
308 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
309 error = EROFS;
310 goto out;
311 }
312 break;
313
314 case VBLK:
315 /* FALLTHROUGH */
316 case VCHR:
317 /* FALLTHROUGH */
318 case VSOCK:
319 /* FALLTHROUGH */
320 case VFIFO:
321 break;
322
323 default:
324 error = EINVAL;
325 goto out;
326 }
327
328 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
329 error = EPERM;
330 goto out;
331 }
332
333 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
334 node->tn_gid, accmode, cred, NULL);
335
336out:
337 MPASS(VOP_ISLOCKED(vp));
338
339 return error;
340}
341
342/* --------------------------------------------------------------------- */
343
344int
345tmpfs_getattr(struct vop_getattr_args *v)
346{
347 struct vnode *vp = v->a_vp;
348 struct vattr *vap = v->a_vap;
349
350 struct tmpfs_node *node;
351
352 node = VP_TO_TMPFS_NODE(vp);
353
354 tmpfs_update(vp);
355
356 vap->va_type = vp->v_type;
357 vap->va_mode = node->tn_mode;
358 vap->va_nlink = node->tn_links;
359 vap->va_uid = node->tn_uid;
360 vap->va_gid = node->tn_gid;
361 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
362 vap->va_fileid = node->tn_id;
363 vap->va_size = node->tn_size;
364 vap->va_blocksize = PAGE_SIZE;
365 vap->va_atime = node->tn_atime;
366 vap->va_mtime = node->tn_mtime;
367 vap->va_ctime = node->tn_ctime;
368 vap->va_birthtime = node->tn_birthtime;
369 vap->va_gen = node->tn_gen;
370 vap->va_flags = node->tn_flags;
371 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
372 node->tn_rdev : NODEV;
373 vap->va_bytes = round_page(node->tn_size);
374 vap->va_filerev = 0;
375
376 return 0;
377}
378
379/* --------------------------------------------------------------------- */
380
381/* XXX Should this operation be atomic? I think it should, but code in
382 * XXX other places (e.g., ufs) doesn't seem to be... */
383int
384tmpfs_setattr(struct vop_setattr_args *v)
385{
386 struct vnode *vp = v->a_vp;
387 struct vattr *vap = v->a_vap;
388 struct ucred *cred = v->a_cred;
389 struct thread *td = curthread;
390
391 int error;
392
393 MPASS(VOP_ISLOCKED(vp));
394
395 error = 0;
396
397 /* Abort if any unsettable attribute is given. */
398 if (vap->va_type != VNON ||
399 vap->va_nlink != VNOVAL ||
400 vap->va_fsid != VNOVAL ||
401 vap->va_fileid != VNOVAL ||
402 vap->va_blocksize != VNOVAL ||
403 vap->va_gen != VNOVAL ||
404 vap->va_rdev != VNOVAL ||
405 vap->va_bytes != VNOVAL)
406 error = EINVAL;
407
408 if (error == 0 && (vap->va_flags != VNOVAL))
409 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
410
411 if (error == 0 && (vap->va_size != VNOVAL))
412 error = tmpfs_chsize(vp, vap->va_size, cred, td);
413
414 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
415 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
416
417 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
418 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
419
420 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
421 vap->va_atime.tv_nsec != VNOVAL) ||
422 (vap->va_mtime.tv_sec != VNOVAL &&
423 vap->va_mtime.tv_nsec != VNOVAL) ||
424 (vap->va_birthtime.tv_sec != VNOVAL &&
425 vap->va_birthtime.tv_nsec != VNOVAL)))
426 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
427 &vap->va_birthtime, vap->va_vaflags, cred, td);
428
429 /* Update the node times. We give preference to the error codes
430 * generated by this function rather than the ones that may arise
431 * from tmpfs_update. */
432 tmpfs_update(vp);
433
434 MPASS(VOP_ISLOCKED(vp));
435
436 return error;
437}
438
439/* --------------------------------------------------------------------- */
440static int
441tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
442 vm_offset_t offset, size_t tlen, struct uio *uio)
443{
444 vm_page_t m;
445 int error, rv;
446
447 VM_OBJECT_LOCK(tobj);
448 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
449 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
450 if (m->valid != VM_PAGE_BITS_ALL) {
451 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
452 rv = vm_pager_get_pages(tobj, &m, 1, 0);
453 if (rv != VM_PAGER_OK) {
454 vm_page_lock(m);
455 vm_page_free(m);
456 vm_page_unlock(m);
457 VM_OBJECT_UNLOCK(tobj);
458 return (EIO);
459 }
460 } else
461 vm_page_zero_invalid(m, TRUE);
462 }
463 VM_OBJECT_UNLOCK(tobj);
464 error = uiomove_fromphys(&m, offset, tlen, uio);
465 VM_OBJECT_LOCK(tobj);
466 vm_page_lock(m);
467 vm_page_unwire(m, TRUE);
468 vm_page_unlock(m);
469 vm_page_wakeup(m);
470 VM_OBJECT_UNLOCK(tobj);
471
472 return (error);
473}
474
475static __inline int
476tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx,
477 vm_offset_t offset, size_t tlen, void *buf)
478{
479 struct uio uio;
480 struct iovec iov;
481
482 uio.uio_iovcnt = 1;
483 uio.uio_iov = &iov;
484 iov.iov_base = buf;
485 iov.iov_len = tlen;
486
487 uio.uio_offset = 0;
488 uio.uio_resid = tlen;
489 uio.uio_rw = UIO_READ;
490 uio.uio_segflg = UIO_SYSSPACE;
491 uio.uio_td = curthread;
492
493 return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio));
494}
495
496static int
497tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
498{
499 struct sf_buf *sf;
500 vm_pindex_t idx;
501 vm_page_t m;
502 vm_offset_t offset;
503 off_t addr;
504 size_t tlen;
505 char *ma;
506 int error;
507
508 addr = uio->uio_offset;
509 idx = OFF_TO_IDX(addr);
510 offset = addr & PAGE_MASK;
511 tlen = MIN(PAGE_SIZE - offset, len);
512
513 if ((vobj == NULL) ||
514 (vobj->resident_page_count == 0 && vobj->cache == NULL))
515 goto nocache;
516
517 VM_OBJECT_LOCK(vobj);
518lookupvpg:
519 if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
520 vm_page_is_valid(m, offset, tlen)) {
521 if ((m->oflags & VPO_BUSY) != 0) {
522 /*
523 * Reference the page before unlocking and sleeping so
524 * that the page daemon is less likely to reclaim it.
525 */
526 vm_page_reference(m);
527 vm_page_sleep(m, "tmfsmr");
528 goto lookupvpg;
529 }
530 vm_page_busy(m);
531 VM_OBJECT_UNLOCK(vobj);
532 error = uiomove_fromphys(&m, offset, tlen, uio);
533 VM_OBJECT_LOCK(vobj);
534 vm_page_wakeup(m);
535 VM_OBJECT_UNLOCK(vobj);
536 return (error);
537 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
538 KASSERT(offset == 0,
539 ("unexpected offset in tmpfs_mappedread for sendfile"));
540 if ((m->oflags & VPO_BUSY) != 0) {
541 /*
542 * Reference the page before unlocking and sleeping so
543 * that the page daemon is less likely to reclaim it.
544 */
545 vm_page_reference(m);
546 vm_page_sleep(m, "tmfsmr");
547 goto lookupvpg;
548 }
549 vm_page_busy(m);
550 VM_OBJECT_UNLOCK(vobj);
551 sched_pin();
552 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
553 ma = (char *)sf_buf_kva(sf);
554 error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma);
555 if (error == 0) {
556 if (tlen != PAGE_SIZE)
557 bzero(ma + tlen, PAGE_SIZE - tlen);
558 uio->uio_offset += tlen;
559 uio->uio_resid -= tlen;
560 }
561 sf_buf_free(sf);
562 sched_unpin();
563 VM_OBJECT_LOCK(vobj);
564 if (error == 0)
565 m->valid = VM_PAGE_BITS_ALL;
566 vm_page_wakeup(m);
567 VM_OBJECT_UNLOCK(vobj);
568 return (error);
569 }
570 VM_OBJECT_UNLOCK(vobj);
571nocache:
572 error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio);
573
574 return (error);
575}
576
577static int
578tmpfs_read(struct vop_read_args *v)
579{
580 struct vnode *vp = v->a_vp;
581 struct uio *uio = v->a_uio;
582
583 struct tmpfs_node *node;
584 vm_object_t uobj;
585 size_t len;
586 int resid;
587
588 int error = 0;
589
590 node = VP_TO_TMPFS_NODE(vp);
591
592 if (vp->v_type != VREG) {
593 error = EISDIR;
594 goto out;
595 }
596
597 if (uio->uio_offset < 0) {
598 error = EINVAL;
599 goto out;
600 }
601
602 node->tn_status |= TMPFS_NODE_ACCESSED;
603
604 uobj = node->tn_reg.tn_aobj;
605 while ((resid = uio->uio_resid) > 0) {
606 error = 0;
607 if (node->tn_size <= uio->uio_offset)
608 break;
609 len = MIN(node->tn_size - uio->uio_offset, resid);
610 if (len == 0)
611 break;
612 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
613 if ((error != 0) || (resid == uio->uio_resid))
614 break;
615 }
616
617out:
618
619 return error;
620}
621
622/* --------------------------------------------------------------------- */
623
624static int
625tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
626{
627 vm_pindex_t idx;
628 vm_page_t vpg, tpg;
629 vm_offset_t offset;
630 off_t addr;
631 size_t tlen;
632 int error, rv;
633
634 error = 0;
635
636 addr = uio->uio_offset;
637 idx = OFF_TO_IDX(addr);
638 offset = addr & PAGE_MASK;
639 tlen = MIN(PAGE_SIZE - offset, len);
640
641 if ((vobj == NULL) ||
642 (vobj->resident_page_count == 0 && vobj->cache == NULL)) {
643 vpg = NULL;
644 goto nocache;
645 }
646
647 VM_OBJECT_LOCK(vobj);
648lookupvpg:
649 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
650 vm_page_is_valid(vpg, offset, tlen)) {
651 if ((vpg->oflags & VPO_BUSY) != 0) {
652 /*
653 * Reference the page before unlocking and sleeping so
654 * that the page daemon is less likely to reclaim it.
655 */
656 vm_page_reference(vpg);
657 vm_page_sleep(vpg, "tmfsmw");
658 goto lookupvpg;
659 }
660 vm_page_busy(vpg);
661 vm_page_undirty(vpg);
662 VM_OBJECT_UNLOCK(vobj);
663 error = uiomove_fromphys(&vpg, offset, tlen, uio);
664 } else {
665 if (__predict_false(vobj->cache != NULL))
666 vm_page_cache_free(vobj, idx, idx + 1);
667 VM_OBJECT_UNLOCK(vobj);
668 vpg = NULL;
669 }
670nocache:
671 VM_OBJECT_LOCK(tobj);
672 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
673 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
674 if (tpg->valid != VM_PAGE_BITS_ALL) {
675 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
676 rv = vm_pager_get_pages(tobj, &tpg, 1, 0);
677 if (rv != VM_PAGER_OK) {
678 vm_page_lock(tpg);
679 vm_page_free(tpg);
680 vm_page_unlock(tpg);
681 error = EIO;
682 goto out;
683 }
684 } else
685 vm_page_zero_invalid(tpg, TRUE);
686 }
687 VM_OBJECT_UNLOCK(tobj);
688 if (vpg == NULL)
689 error = uiomove_fromphys(&tpg, offset, tlen, uio);
690 else {
691 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
692 pmap_copy_page(vpg, tpg);
693 }
694 VM_OBJECT_LOCK(tobj);
695 if (error == 0) {
696 KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
697 ("parts of tpg invalid"));
698 vm_page_dirty(tpg);
699 }
700 vm_page_lock(tpg);
701 vm_page_unwire(tpg, TRUE);
702 vm_page_unlock(tpg);
703 vm_page_wakeup(tpg);
704out:
705 VM_OBJECT_UNLOCK(tobj);
706 if (vpg != NULL) {
707 VM_OBJECT_LOCK(vobj);
708 vm_page_wakeup(vpg);
709 VM_OBJECT_UNLOCK(vobj);
710 }
711
712 return (error);
713}
714
715static int
716tmpfs_write(struct vop_write_args *v)
717{
718 struct vnode *vp = v->a_vp;
719 struct uio *uio = v->a_uio;
720 int ioflag = v->a_ioflag;
721
722 boolean_t extended;
723 int error = 0;
724 off_t oldsize;
725 struct tmpfs_node *node;
726 vm_object_t uobj;
727 size_t len;
728 int resid;
729
730 node = VP_TO_TMPFS_NODE(vp);
731 oldsize = node->tn_size;
732
733 if (uio->uio_offset < 0 || vp->v_type != VREG) {
734 error = EINVAL;
735 goto out;
736 }
737
738 if (uio->uio_resid == 0) {
739 error = 0;
740 goto out;
741 }
742
743 if (ioflag & IO_APPEND)
744 uio->uio_offset = node->tn_size;
745
746 if (uio->uio_offset + uio->uio_resid >
747 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
748 return (EFBIG);
749
750 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
751 return (EFBIG);
752
753 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
754 if (extended) {
755 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
756 FALSE);
757 if (error != 0)
758 goto out;
759 }
760
761 uobj = node->tn_reg.tn_aobj;
762 while ((resid = uio->uio_resid) > 0) {
763 if (node->tn_size <= uio->uio_offset)
764 break;
765 len = MIN(node->tn_size - uio->uio_offset, resid);
766 if (len == 0)
767 break;
768 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
769 if ((error != 0) || (resid == uio->uio_resid))
770 break;
771 }
772
773 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
774 (extended ? TMPFS_NODE_CHANGED : 0);
775
776 if (node->tn_mode & (S_ISUID | S_ISGID)) {
777 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
778 node->tn_mode &= ~(S_ISUID | S_ISGID);
779 }
780
781 if (error != 0)
782 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
783
784out:
785 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
786 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
787
788 return error;
789}
790
791/* --------------------------------------------------------------------- */
792
793static int
794tmpfs_fsync(struct vop_fsync_args *v)
795{
796 struct vnode *vp = v->a_vp;
797
798 MPASS(VOP_ISLOCKED(vp));
799
800 tmpfs_update(vp);
801
802 return 0;
803}
804
805/* --------------------------------------------------------------------- */
806
807static int
808tmpfs_remove(struct vop_remove_args *v)
809{
810 struct vnode *dvp = v->a_dvp;
811 struct vnode *vp = v->a_vp;
812
813 int error;
814 struct tmpfs_dirent *de;
815 struct tmpfs_mount *tmp;
816 struct tmpfs_node *dnode;
817 struct tmpfs_node *node;
818
819 MPASS(VOP_ISLOCKED(dvp));
820 MPASS(VOP_ISLOCKED(vp));
821
822 if (vp->v_type == VDIR) {
823 error = EISDIR;
824 goto out;
825 }
826
827 dnode = VP_TO_TMPFS_DIR(dvp);
828 node = VP_TO_TMPFS_NODE(vp);
829 tmp = VFS_TO_TMPFS(vp->v_mount);
830 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
831 MPASS(de != NULL);
832
833 /* Files marked as immutable or append-only cannot be deleted. */
834 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
835 (dnode->tn_flags & APPEND)) {
836 error = EPERM;
837 goto out;
838 }
839
840 /* Remove the entry from the directory; as it is a file, we do not
841 * have to change the number of hard links of the directory. */
842 tmpfs_dir_detach(dvp, de);
843 if (v->a_cnp->cn_flags & DOWHITEOUT)
844 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
845
846 /* Free the directory entry we just deleted. Note that the node
847 * referred by it will not be removed until the vnode is really
848 * reclaimed. */
849 tmpfs_free_dirent(tmp, de, TRUE);
850
851 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
852 error = 0;
853
854out:
855
856 return error;
857}
858
859/* --------------------------------------------------------------------- */
860
861static int
862tmpfs_link(struct vop_link_args *v)
863{
864 struct vnode *dvp = v->a_tdvp;
865 struct vnode *vp = v->a_vp;
866 struct componentname *cnp = v->a_cnp;
867
868 int error;
869 struct tmpfs_dirent *de;
870 struct tmpfs_node *node;
871
872 MPASS(VOP_ISLOCKED(dvp));
873 MPASS(cnp->cn_flags & HASBUF);
874 MPASS(dvp != vp); /* XXX When can this be false? */
875
876 node = VP_TO_TMPFS_NODE(vp);
877
878 /* XXX: Why aren't the following two tests done by the caller? */
879
880 /* Hard links of directories are forbidden. */
881 if (vp->v_type == VDIR) {
882 error = EPERM;
883 goto out;
884 }
885
886 /* Cannot create cross-device links. */
887 if (dvp->v_mount != vp->v_mount) {
888 error = EXDEV;
889 goto out;
890 }
891
892 /* Ensure that we do not overflow the maximum number of links imposed
893 * by the system. */
894 MPASS(node->tn_links <= LINK_MAX);
895 if (node->tn_links == LINK_MAX) {
896 error = EMLINK;
897 goto out;
898 }
899
900 /* We cannot create links of files marked immutable or append-only. */
901 if (node->tn_flags & (IMMUTABLE | APPEND)) {
902 error = EPERM;
903 goto out;
904 }
905
906 /* Allocate a new directory entry to represent the node. */
907 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
908 cnp->cn_nameptr, cnp->cn_namelen, &de);
909 if (error != 0)
910 goto out;
911
912 /* Insert the new directory entry into the appropriate directory. */
913 if (cnp->cn_flags & ISWHITEOUT)
914 tmpfs_dir_whiteout_remove(dvp, cnp);
915 tmpfs_dir_attach(dvp, de);
916
917 /* vp link count has changed, so update node times. */
918 node->tn_status |= TMPFS_NODE_CHANGED;
919 tmpfs_update(vp);
920
921 error = 0;
922
923out:
924 return error;
925}
926
927/* --------------------------------------------------------------------- */
928
929/*
930 * We acquire all but fdvp locks using non-blocking acquisitions. If we
931 * fail to acquire any lock in the path we will drop all held locks,
932 * acquire the new lock in a blocking fashion, and then release it and
933 * restart the rename. This acquire/release step ensures that we do not
934 * spin on a lock waiting for release. On error release all vnode locks
935 * and decrement references the way tmpfs_rename() would do.
936 */
921static int
937static int
938tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
939 struct vnode *tdvp, struct vnode **tvpp,
940 struct componentname *fcnp, struct componentname *tcnp)
941{
942 struct vnode *nvp;
943 struct mount *mp;
944 struct tmpfs_dirent *de;
945 int error, restarts = 0;
946
947 VOP_UNLOCK(tdvp, 0);
948 if (*tvpp != NULL && *tvpp != tdvp)
949 VOP_UNLOCK(*tvpp, 0);
950 mp = fdvp->v_mount;
951
952relock:
953 restarts += 1;
954 error = vn_lock(fdvp, LK_EXCLUSIVE);
955 if (error)
956 goto releout;
957 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
958 VOP_UNLOCK(fdvp, 0);
959 error = vn_lock(tdvp, LK_EXCLUSIVE);
960 if (error)
961 goto releout;
962 VOP_UNLOCK(tdvp, 0);
963 goto relock;
964 }
965 /*
966 * Re-resolve fvp to be certain it still exists and fetch the
967 * correct vnode.
968 */
969 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
970 if (de == NULL) {
971 VOP_UNLOCK(fdvp, 0);
972 VOP_UNLOCK(tdvp, 0);
973 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
974 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
975 error = EINVAL;
976 else
977 error = ENOENT;
978 goto releout;
979 }
980 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
981 if (error != 0) {
982 VOP_UNLOCK(fdvp, 0);
983 VOP_UNLOCK(tdvp, 0);
984 if (error != EBUSY)
985 goto releout;
986 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
987 if (error != 0)
988 goto releout;
989 VOP_UNLOCK(nvp, 0);
990 /*
991 * Concurrent rename race.
992 */
993 if (nvp == tdvp) {
994 vrele(nvp);
995 error = EINVAL;
996 goto releout;
997 }
998 vrele(*fvpp);
999 *fvpp = nvp;
1000 goto relock;
1001 }
1002 vrele(*fvpp);
1003 *fvpp = nvp;
1004 VOP_UNLOCK(*fvpp, 0);
1005 /*
1006 * Re-resolve tvp and acquire the vnode lock if present.
1007 */
1008 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
1009 /*
1010 * If tvp disappeared we just carry on.
1011 */
1012 if (de == NULL && *tvpp != NULL) {
1013 vrele(*tvpp);
1014 *tvpp = NULL;
1015 }
1016 /*
1017 * Get the tvp ino if the lookup succeeded. We may have to restart
1018 * if the non-blocking acquire fails.
1019 */
1020 if (de != NULL) {
1021 nvp = NULL;
1022 error = tmpfs_alloc_vp(mp, de->td_node,
1023 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
1024 if (*tvpp != NULL)
1025 vrele(*tvpp);
1026 *tvpp = nvp;
1027 if (error != 0) {
1028 VOP_UNLOCK(fdvp, 0);
1029 VOP_UNLOCK(tdvp, 0);
1030 if (error != EBUSY)
1031 goto releout;
1032 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
1033 &nvp);
1034 if (error != 0)
1035 goto releout;
1036 VOP_UNLOCK(nvp, 0);
1037 /*
1038 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
1039 */
1040 if (nvp == fdvp) {
1041 error = ENOTEMPTY;
1042 goto releout;
1043 }
1044 goto relock;
1045 }
1046 }
1047 tmpfs_rename_restarts += restarts;
1048
1049 return (0);
1050
1051releout:
1052 vrele(fdvp);
1053 vrele(*fvpp);
1054 vrele(tdvp);
1055 if (*tvpp != NULL)
1056 vrele(*tvpp);
1057 tmpfs_rename_restarts += restarts;
1058
1059 return (error);
1060}
1061
1062static int
922tmpfs_rename(struct vop_rename_args *v)
923{
924 struct vnode *fdvp = v->a_fdvp;
925 struct vnode *fvp = v->a_fvp;
926 struct componentname *fcnp = v->a_fcnp;
927 struct vnode *tdvp = v->a_tdvp;
928 struct vnode *tvp = v->a_tvp;
929 struct componentname *tcnp = v->a_tcnp;
1063tmpfs_rename(struct vop_rename_args *v)
1064{
1065 struct vnode *fdvp = v->a_fdvp;
1066 struct vnode *fvp = v->a_fvp;
1067 struct componentname *fcnp = v->a_fcnp;
1068 struct vnode *tdvp = v->a_tdvp;
1069 struct vnode *tvp = v->a_tvp;
1070 struct componentname *tcnp = v->a_tcnp;
1071 struct mount *mp = NULL;
930
931 char *newname;
932 int error;
933 struct tmpfs_dirent *de;
934 struct tmpfs_mount *tmp;
935 struct tmpfs_node *fdnode;
936 struct tmpfs_node *fnode;
937 struct tmpfs_node *tnode;
938 struct tmpfs_node *tdnode;
939
940 MPASS(VOP_ISLOCKED(tdvp));
941 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
942 MPASS(fcnp->cn_flags & HASBUF);
943 MPASS(tcnp->cn_flags & HASBUF);
944
1072
1073 char *newname;
1074 int error;
1075 struct tmpfs_dirent *de;
1076 struct tmpfs_mount *tmp;
1077 struct tmpfs_node *fdnode;
1078 struct tmpfs_node *fnode;
1079 struct tmpfs_node *tnode;
1080 struct tmpfs_node *tdnode;
1081
1082 MPASS(VOP_ISLOCKED(tdvp));
1083 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
1084 MPASS(fcnp->cn_flags & HASBUF);
1085 MPASS(tcnp->cn_flags & HASBUF);
1086
945 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
946
947 /* Disallow cross-device renames.
948 * XXX Why isn't this done by the caller? */
949 if (fvp->v_mount != tdvp->v_mount ||
950 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
951 error = EXDEV;
952 goto out;
953 }
954
1087 /* Disallow cross-device renames.
1088 * XXX Why isn't this done by the caller? */
1089 if (fvp->v_mount != tdvp->v_mount ||
1090 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1091 error = EXDEV;
1092 goto out;
1093 }
1094
955 tmp = VFS_TO_TMPFS(tdvp->v_mount);
956 tdnode = VP_TO_TMPFS_DIR(tdvp);
957
958 /* If source and target are the same file, there is nothing to do. */
959 if (fvp == tvp) {
960 error = 0;
961 goto out;
962 }
963
964 /* If we need to move the directory between entries, lock the
965 * source so that we can safely operate on it. */
1095 /* If source and target are the same file, there is nothing to do. */
1096 if (fvp == tvp) {
1097 error = 0;
1098 goto out;
1099 }
1100
1101 /* If we need to move the directory between entries, lock the
1102 * source so that we can safely operate on it. */
966 if (fdvp != tdvp && fdvp != tvp)
967 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
1103 if (fdvp != tdvp && fdvp != tvp) {
1104 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1105 mp = tdvp->v_mount;
1106 error = vfs_busy(mp, 0);
1107 if (error != 0) {
1108 mp = NULL;
1109 goto out;
1110 }
1111 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1112 fcnp, tcnp);
1113 if (error != 0) {
1114 vfs_unbusy(mp);
1115 return (error);
1116 }
1117 ASSERT_VOP_ELOCKED(fdvp,
1118 "tmpfs_rename: fdvp not locked");
1119 ASSERT_VOP_ELOCKED(tdvp,
1120 "tmpfs_rename: tdvp not locked");
1121 if (tvp != NULL)
1122 ASSERT_VOP_ELOCKED(tvp,
1123 "tmpfs_rename: tvp not locked");
1124 if (fvp == tvp) {
1125 error = 0;
1126 goto out_locked;
1127 }
1128 }
1129 }
1130
1131 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1132 tdnode = VP_TO_TMPFS_DIR(tdvp);
1133 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
968 fdnode = VP_TO_TMPFS_DIR(fdvp);
969 fnode = VP_TO_TMPFS_NODE(fvp);
970 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
971
972 /* Entry can disappear before we lock fdvp,
973 * also avoid manipulating '.' and '..' entries. */
974 if (de == NULL) {
975 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
976 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
977 error = EINVAL;
978 else
979 error = ENOENT;
980 goto out_locked;
981 }
982 MPASS(de->td_node == fnode);
983
984 /* If re-naming a directory to another preexisting directory
985 * ensure that the target directory is empty so that its
986 * removal causes no side effects.
987 * Kern_rename gurantees the destination to be a directory
988 * if the source is one. */
989 if (tvp != NULL) {
990 MPASS(tnode != NULL);
991
992 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
993 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
994 error = EPERM;
995 goto out_locked;
996 }
997
998 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
999 if (tnode->tn_size > 0) {
1000 error = ENOTEMPTY;
1001 goto out_locked;
1002 }
1003 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1004 error = ENOTDIR;
1005 goto out_locked;
1006 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1007 error = EISDIR;
1008 goto out_locked;
1009 } else {
1010 MPASS(fnode->tn_type != VDIR &&
1011 tnode->tn_type != VDIR);
1012 }
1013 }
1014
1015 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1016 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1017 error = EPERM;
1018 goto out_locked;
1019 }
1020
1021 /* Ensure that we have enough memory to hold the new name, if it
1022 * has to be changed. */
1023 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1024 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1025 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1026 } else
1027 newname = NULL;
1028
1029 /* If the node is being moved to another directory, we have to do
1030 * the move. */
1031 if (fdnode != tdnode) {
1032 /* In case we are moving a directory, we have to adjust its
1033 * parent to point to the new parent. */
1034 if (de->td_node->tn_type == VDIR) {
1035 struct tmpfs_node *n;
1036
1037 /* Ensure the target directory is not a child of the
1038 * directory being moved. Otherwise, we'd end up
1039 * with stale nodes. */
1040 n = tdnode;
1041 /* TMPFS_LOCK garanties that no nodes are freed while
1042 * traversing the list. Nodes can only be marked as
1043 * removed: tn_parent == NULL. */
1044 TMPFS_LOCK(tmp);
1045 TMPFS_NODE_LOCK(n);
1046 while (n != n->tn_dir.tn_parent) {
1047 struct tmpfs_node *parent;
1048
1049 if (n == fnode) {
1050 TMPFS_NODE_UNLOCK(n);
1051 TMPFS_UNLOCK(tmp);
1052 error = EINVAL;
1053 if (newname != NULL)
1054 free(newname, M_TMPFSNAME);
1055 goto out_locked;
1056 }
1057 parent = n->tn_dir.tn_parent;
1058 TMPFS_NODE_UNLOCK(n);
1059 if (parent == NULL) {
1060 n = NULL;
1061 break;
1062 }
1063 TMPFS_NODE_LOCK(parent);
1064 if (parent->tn_dir.tn_parent == NULL) {
1065 TMPFS_NODE_UNLOCK(parent);
1066 n = NULL;
1067 break;
1068 }
1069 n = parent;
1070 }
1071 TMPFS_UNLOCK(tmp);
1072 if (n == NULL) {
1073 error = EINVAL;
1074 if (newname != NULL)
1075 free(newname, M_TMPFSNAME);
1076 goto out_locked;
1077 }
1078 TMPFS_NODE_UNLOCK(n);
1079
1080 /* Adjust the parent pointer. */
1081 TMPFS_VALIDATE_DIR(fnode);
1082 TMPFS_NODE_LOCK(de->td_node);
1083 de->td_node->tn_dir.tn_parent = tdnode;
1084 TMPFS_NODE_UNLOCK(de->td_node);
1085
1086 /* As a result of changing the target of the '..'
1087 * entry, the link count of the source and target
1088 * directories has to be adjusted. */
1089 TMPFS_NODE_LOCK(tdnode);
1090 TMPFS_ASSERT_LOCKED(tdnode);
1091 tdnode->tn_links++;
1092 TMPFS_NODE_UNLOCK(tdnode);
1093
1094 TMPFS_NODE_LOCK(fdnode);
1095 TMPFS_ASSERT_LOCKED(fdnode);
1096 fdnode->tn_links--;
1097 TMPFS_NODE_UNLOCK(fdnode);
1098 }
1099
1100 /* Do the move: just remove the entry from the source directory
1101 * and insert it into the target one. */
1102 tmpfs_dir_detach(fdvp, de);
1103 if (fcnp->cn_flags & DOWHITEOUT)
1104 tmpfs_dir_whiteout_add(fdvp, fcnp);
1105 if (tcnp->cn_flags & ISWHITEOUT)
1106 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1107 tmpfs_dir_attach(tdvp, de);
1108 }
1109
1110 /* If the name has changed, we need to make it effective by changing
1111 * it in the directory entry. */
1112 if (newname != NULL) {
1113 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1114
1115 free(de->td_name, M_TMPFSNAME);
1116 de->td_namelen = (uint16_t)tcnp->cn_namelen;
1117 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1118 de->td_name = newname;
1119
1120 fnode->tn_status |= TMPFS_NODE_CHANGED;
1121 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1122 }
1123
1124 /* If we are overwriting an entry, we have to remove the old one
1125 * from the target directory. */
1126 if (tvp != NULL) {
1127 /* Remove the old entry from the target directory. */
1128 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1129 tmpfs_dir_detach(tdvp, de);
1130
1131 /* Free the directory entry we just deleted. Note that the
1132 * node referred by it will not be removed until the vnode is
1133 * really reclaimed. */
1134 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1135 }
1136 cache_purge(fvp);
1137 if (tvp != NULL)
1138 cache_purge(tvp);
1139
1140 error = 0;
1141
1142out_locked:
1143 if (fdvp != tdvp && fdvp != tvp)
1144 VOP_UNLOCK(fdvp, 0);
1145
1146out:
1147 /* Release target nodes. */
1148 /* XXX: I don't understand when tdvp can be the same as tvp, but
1149 * other code takes care of this... */
1150 if (tdvp == tvp)
1151 vrele(tdvp);
1152 else
1153 vput(tdvp);
1154 if (tvp != NULL)
1155 vput(tvp);
1156
1157 /* Release source nodes. */
1158 vrele(fdvp);
1159 vrele(fvp);
1160
1134 fdnode = VP_TO_TMPFS_DIR(fdvp);
1135 fnode = VP_TO_TMPFS_NODE(fvp);
1136 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1137
1138 /* Entry can disappear before we lock fdvp,
1139 * also avoid manipulating '.' and '..' entries. */
1140 if (de == NULL) {
1141 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1142 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1143 error = EINVAL;
1144 else
1145 error = ENOENT;
1146 goto out_locked;
1147 }
1148 MPASS(de->td_node == fnode);
1149
1150 /* If re-naming a directory to another preexisting directory
1151 * ensure that the target directory is empty so that its
1152 * removal causes no side effects.
1153 * Kern_rename gurantees the destination to be a directory
1154 * if the source is one. */
1155 if (tvp != NULL) {
1156 MPASS(tnode != NULL);
1157
1158 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1159 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1160 error = EPERM;
1161 goto out_locked;
1162 }
1163
1164 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1165 if (tnode->tn_size > 0) {
1166 error = ENOTEMPTY;
1167 goto out_locked;
1168 }
1169 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1170 error = ENOTDIR;
1171 goto out_locked;
1172 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1173 error = EISDIR;
1174 goto out_locked;
1175 } else {
1176 MPASS(fnode->tn_type != VDIR &&
1177 tnode->tn_type != VDIR);
1178 }
1179 }
1180
1181 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1182 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1183 error = EPERM;
1184 goto out_locked;
1185 }
1186
1187 /* Ensure that we have enough memory to hold the new name, if it
1188 * has to be changed. */
1189 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1190 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1191 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1192 } else
1193 newname = NULL;
1194
1195 /* If the node is being moved to another directory, we have to do
1196 * the move. */
1197 if (fdnode != tdnode) {
1198 /* In case we are moving a directory, we have to adjust its
1199 * parent to point to the new parent. */
1200 if (de->td_node->tn_type == VDIR) {
1201 struct tmpfs_node *n;
1202
1203 /* Ensure the target directory is not a child of the
1204 * directory being moved. Otherwise, we'd end up
1205 * with stale nodes. */
1206 n = tdnode;
1207 /* TMPFS_LOCK garanties that no nodes are freed while
1208 * traversing the list. Nodes can only be marked as
1209 * removed: tn_parent == NULL. */
1210 TMPFS_LOCK(tmp);
1211 TMPFS_NODE_LOCK(n);
1212 while (n != n->tn_dir.tn_parent) {
1213 struct tmpfs_node *parent;
1214
1215 if (n == fnode) {
1216 TMPFS_NODE_UNLOCK(n);
1217 TMPFS_UNLOCK(tmp);
1218 error = EINVAL;
1219 if (newname != NULL)
1220 free(newname, M_TMPFSNAME);
1221 goto out_locked;
1222 }
1223 parent = n->tn_dir.tn_parent;
1224 TMPFS_NODE_UNLOCK(n);
1225 if (parent == NULL) {
1226 n = NULL;
1227 break;
1228 }
1229 TMPFS_NODE_LOCK(parent);
1230 if (parent->tn_dir.tn_parent == NULL) {
1231 TMPFS_NODE_UNLOCK(parent);
1232 n = NULL;
1233 break;
1234 }
1235 n = parent;
1236 }
1237 TMPFS_UNLOCK(tmp);
1238 if (n == NULL) {
1239 error = EINVAL;
1240 if (newname != NULL)
1241 free(newname, M_TMPFSNAME);
1242 goto out_locked;
1243 }
1244 TMPFS_NODE_UNLOCK(n);
1245
1246 /* Adjust the parent pointer. */
1247 TMPFS_VALIDATE_DIR(fnode);
1248 TMPFS_NODE_LOCK(de->td_node);
1249 de->td_node->tn_dir.tn_parent = tdnode;
1250 TMPFS_NODE_UNLOCK(de->td_node);
1251
1252 /* As a result of changing the target of the '..'
1253 * entry, the link count of the source and target
1254 * directories has to be adjusted. */
1255 TMPFS_NODE_LOCK(tdnode);
1256 TMPFS_ASSERT_LOCKED(tdnode);
1257 tdnode->tn_links++;
1258 TMPFS_NODE_UNLOCK(tdnode);
1259
1260 TMPFS_NODE_LOCK(fdnode);
1261 TMPFS_ASSERT_LOCKED(fdnode);
1262 fdnode->tn_links--;
1263 TMPFS_NODE_UNLOCK(fdnode);
1264 }
1265
1266 /* Do the move: just remove the entry from the source directory
1267 * and insert it into the target one. */
1268 tmpfs_dir_detach(fdvp, de);
1269 if (fcnp->cn_flags & DOWHITEOUT)
1270 tmpfs_dir_whiteout_add(fdvp, fcnp);
1271 if (tcnp->cn_flags & ISWHITEOUT)
1272 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1273 tmpfs_dir_attach(tdvp, de);
1274 }
1275
1276 /* If the name has changed, we need to make it effective by changing
1277 * it in the directory entry. */
1278 if (newname != NULL) {
1279 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1280
1281 free(de->td_name, M_TMPFSNAME);
1282 de->td_namelen = (uint16_t)tcnp->cn_namelen;
1283 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1284 de->td_name = newname;
1285
1286 fnode->tn_status |= TMPFS_NODE_CHANGED;
1287 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1288 }
1289
1290 /* If we are overwriting an entry, we have to remove the old one
1291 * from the target directory. */
1292 if (tvp != NULL) {
1293 /* Remove the old entry from the target directory. */
1294 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1295 tmpfs_dir_detach(tdvp, de);
1296
1297 /* Free the directory entry we just deleted. Note that the
1298 * node referred by it will not be removed until the vnode is
1299 * really reclaimed. */
1300 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1301 }
1302 cache_purge(fvp);
1303 if (tvp != NULL)
1304 cache_purge(tvp);
1305
1306 error = 0;
1307
1308out_locked:
1309 if (fdvp != tdvp && fdvp != tvp)
1310 VOP_UNLOCK(fdvp, 0);
1311
1312out:
1313 /* Release target nodes. */
1314 /* XXX: I don't understand when tdvp can be the same as tvp, but
1315 * other code takes care of this... */
1316 if (tdvp == tvp)
1317 vrele(tdvp);
1318 else
1319 vput(tdvp);
1320 if (tvp != NULL)
1321 vput(tvp);
1322
1323 /* Release source nodes. */
1324 vrele(fdvp);
1325 vrele(fvp);
1326
1327 if (mp != NULL)
1328 vfs_unbusy(mp);
1329
1161 return error;
1162}
1163
1164/* --------------------------------------------------------------------- */
1165
1166static int
1167tmpfs_mkdir(struct vop_mkdir_args *v)
1168{
1169 struct vnode *dvp = v->a_dvp;
1170 struct vnode **vpp = v->a_vpp;
1171 struct componentname *cnp = v->a_cnp;
1172 struct vattr *vap = v->a_vap;
1173
1174 MPASS(vap->va_type == VDIR);
1175
1176 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1177}
1178
1179/* --------------------------------------------------------------------- */
1180
1181static int
1182tmpfs_rmdir(struct vop_rmdir_args *v)
1183{
1184 struct vnode *dvp = v->a_dvp;
1185 struct vnode *vp = v->a_vp;
1186
1187 int error;
1188 struct tmpfs_dirent *de;
1189 struct tmpfs_mount *tmp;
1190 struct tmpfs_node *dnode;
1191 struct tmpfs_node *node;
1192
1193 MPASS(VOP_ISLOCKED(dvp));
1194 MPASS(VOP_ISLOCKED(vp));
1195
1196 tmp = VFS_TO_TMPFS(dvp->v_mount);
1197 dnode = VP_TO_TMPFS_DIR(dvp);
1198 node = VP_TO_TMPFS_DIR(vp);
1199
1200 /* Directories with more than two entries ('.' and '..') cannot be
1201 * removed. */
1202 if (node->tn_size > 0) {
1203 error = ENOTEMPTY;
1204 goto out;
1205 }
1206
1207 if ((dnode->tn_flags & APPEND)
1208 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1209 error = EPERM;
1210 goto out;
1211 }
1212
1213 /* This invariant holds only if we are not trying to remove "..".
1214 * We checked for that above so this is safe now. */
1215 MPASS(node->tn_dir.tn_parent == dnode);
1216
1217 /* Get the directory entry associated with node (vp). This was
1218 * filled by tmpfs_lookup while looking up the entry. */
1219 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1220 MPASS(TMPFS_DIRENT_MATCHES(de,
1221 v->a_cnp->cn_nameptr,
1222 v->a_cnp->cn_namelen));
1223
1224 /* Check flags to see if we are allowed to remove the directory. */
1225 if (dnode->tn_flags & APPEND
1226 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1227 error = EPERM;
1228 goto out;
1229 }
1230
1231
1232 /* Detach the directory entry from the directory (dnode). */
1233 tmpfs_dir_detach(dvp, de);
1234 if (v->a_cnp->cn_flags & DOWHITEOUT)
1235 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1236
1237 /* No vnode should be allocated for this entry from this point */
1238 TMPFS_NODE_LOCK(node);
1239 TMPFS_ASSERT_ELOCKED(node);
1240 node->tn_links--;
1241 node->tn_dir.tn_parent = NULL;
1242 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1243 TMPFS_NODE_MODIFIED;
1244
1245 TMPFS_NODE_UNLOCK(node);
1246
1247 TMPFS_NODE_LOCK(dnode);
1248 TMPFS_ASSERT_ELOCKED(dnode);
1249 dnode->tn_links--;
1250 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1251 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1252 TMPFS_NODE_UNLOCK(dnode);
1253
1254 cache_purge(dvp);
1255 cache_purge(vp);
1256
1257 /* Free the directory entry we just deleted. Note that the node
1258 * referred by it will not be removed until the vnode is really
1259 * reclaimed. */
1260 tmpfs_free_dirent(tmp, de, TRUE);
1261
1262 /* Release the deleted vnode (will destroy the node, notify
1263 * interested parties and clean it from the cache). */
1264
1265 dnode->tn_status |= TMPFS_NODE_CHANGED;
1266 tmpfs_update(dvp);
1267
1268 error = 0;
1269
1270out:
1271 return error;
1272}
1273
1274/* --------------------------------------------------------------------- */
1275
1276static int
1277tmpfs_symlink(struct vop_symlink_args *v)
1278{
1279 struct vnode *dvp = v->a_dvp;
1280 struct vnode **vpp = v->a_vpp;
1281 struct componentname *cnp = v->a_cnp;
1282 struct vattr *vap = v->a_vap;
1283 char *target = v->a_target;
1284
1285#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1286 MPASS(vap->va_type == VLNK);
1287#else
1288 vap->va_type = VLNK;
1289#endif
1290
1291 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1292}
1293
1294/* --------------------------------------------------------------------- */
1295
1296static int
1297tmpfs_readdir(struct vop_readdir_args *v)
1298{
1299 struct vnode *vp = v->a_vp;
1300 struct uio *uio = v->a_uio;
1301 int *eofflag = v->a_eofflag;
1302 u_long **cookies = v->a_cookies;
1303 int *ncookies = v->a_ncookies;
1304
1305 int error;
1306 off_t startoff;
1307 off_t cnt = 0;
1308 struct tmpfs_node *node;
1309
1310 /* This operation only makes sense on directory nodes. */
1311 if (vp->v_type != VDIR)
1312 return ENOTDIR;
1313
1314 node = VP_TO_TMPFS_DIR(vp);
1315
1316 startoff = uio->uio_offset;
1317
1318 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1319 error = tmpfs_dir_getdotdent(node, uio);
1320 if (error != 0)
1321 goto outok;
1322 cnt++;
1323 }
1324
1325 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1326 error = tmpfs_dir_getdotdotdent(node, uio);
1327 if (error != 0)
1328 goto outok;
1329 cnt++;
1330 }
1331
1332 error = tmpfs_dir_getdents(node, uio, &cnt);
1333
1334outok:
1335 MPASS(error >= -1);
1336
1337 if (error == -1)
1338 error = (cnt != 0) ? 0 : EINVAL;
1339
1340 if (eofflag != NULL)
1341 *eofflag =
1342 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1343
1344 /* Update NFS-related variables. */
1345 if (error == 0 && cookies != NULL && ncookies != NULL) {
1346 off_t i;
1347 off_t off = startoff;
1348 struct tmpfs_dirent *de = NULL;
1349
1350 *ncookies = cnt;
1351 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1352
1353 for (i = 0; i < cnt; i++) {
1354 MPASS(off != TMPFS_DIRCOOKIE_EOF);
1355 if (off == TMPFS_DIRCOOKIE_DOT) {
1356 off = TMPFS_DIRCOOKIE_DOTDOT;
1357 } else {
1358 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1359 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1360 } else if (de != NULL) {
1361 de = TAILQ_NEXT(de, td_entries);
1362 } else {
1363 de = tmpfs_dir_lookupbycookie(node,
1364 off);
1365 MPASS(de != NULL);
1366 de = TAILQ_NEXT(de, td_entries);
1367 }
1368 if (de == NULL)
1369 off = TMPFS_DIRCOOKIE_EOF;
1370 else
1371 off = tmpfs_dircookie(de);
1372 }
1373
1374 (*cookies)[i] = off;
1375 }
1376 MPASS(uio->uio_offset == off);
1377 }
1378
1379 return error;
1380}
1381
1382/* --------------------------------------------------------------------- */
1383
1384static int
1385tmpfs_readlink(struct vop_readlink_args *v)
1386{
1387 struct vnode *vp = v->a_vp;
1388 struct uio *uio = v->a_uio;
1389
1390 int error;
1391 struct tmpfs_node *node;
1392
1393 MPASS(uio->uio_offset == 0);
1394 MPASS(vp->v_type == VLNK);
1395
1396 node = VP_TO_TMPFS_NODE(vp);
1397
1398 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1399 uio);
1400 node->tn_status |= TMPFS_NODE_ACCESSED;
1401
1402 return error;
1403}
1404
1405/* --------------------------------------------------------------------- */
1406
1407static int
1408tmpfs_inactive(struct vop_inactive_args *v)
1409{
1410 struct vnode *vp = v->a_vp;
1411 struct thread *l = v->a_td;
1412
1413 struct tmpfs_node *node;
1414
1415 MPASS(VOP_ISLOCKED(vp));
1416
1417 node = VP_TO_TMPFS_NODE(vp);
1418
1419 if (node->tn_links == 0)
1420 vrecycle(vp, l);
1421
1422 return 0;
1423}
1424
1425/* --------------------------------------------------------------------- */
1426
1427int
1428tmpfs_reclaim(struct vop_reclaim_args *v)
1429{
1430 struct vnode *vp = v->a_vp;
1431
1432 struct tmpfs_mount *tmp;
1433 struct tmpfs_node *node;
1434
1435 node = VP_TO_TMPFS_NODE(vp);
1436 tmp = VFS_TO_TMPFS(vp->v_mount);
1437
1438 vnode_destroy_vobject(vp);
1439 cache_purge(vp);
1440
1441 TMPFS_NODE_LOCK(node);
1442 TMPFS_ASSERT_ELOCKED(node);
1443 tmpfs_free_vp(vp);
1444
1445 /* If the node referenced by this vnode was deleted by the user,
1446 * we must free its associated data structures (now that the vnode
1447 * is being reclaimed). */
1448 if (node->tn_links == 0 &&
1449 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1450 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1451 TMPFS_NODE_UNLOCK(node);
1452 tmpfs_free_node(tmp, node);
1453 } else
1454 TMPFS_NODE_UNLOCK(node);
1455
1456 MPASS(vp->v_data == NULL);
1457 return 0;
1458}
1459
1460/* --------------------------------------------------------------------- */
1461
1462static int
1463tmpfs_print(struct vop_print_args *v)
1464{
1465 struct vnode *vp = v->a_vp;
1466
1467 struct tmpfs_node *node;
1468
1469 node = VP_TO_TMPFS_NODE(vp);
1470
1471 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1472 node, node->tn_flags, node->tn_links);
1473 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1474 node->tn_mode, node->tn_uid, node->tn_gid,
1475 (intmax_t)node->tn_size, node->tn_status);
1476
1477 if (vp->v_type == VFIFO)
1478 fifo_printinfo(vp);
1479
1480 printf("\n");
1481
1482 return 0;
1483}
1484
1485/* --------------------------------------------------------------------- */
1486
1487static int
1488tmpfs_pathconf(struct vop_pathconf_args *v)
1489{
1490 int name = v->a_name;
1491 register_t *retval = v->a_retval;
1492
1493 int error;
1494
1495 error = 0;
1496
1497 switch (name) {
1498 case _PC_LINK_MAX:
1499 *retval = LINK_MAX;
1500 break;
1501
1502 case _PC_NAME_MAX:
1503 *retval = NAME_MAX;
1504 break;
1505
1506 case _PC_PATH_MAX:
1507 *retval = PATH_MAX;
1508 break;
1509
1510 case _PC_PIPE_BUF:
1511 *retval = PIPE_BUF;
1512 break;
1513
1514 case _PC_CHOWN_RESTRICTED:
1515 *retval = 1;
1516 break;
1517
1518 case _PC_NO_TRUNC:
1519 *retval = 1;
1520 break;
1521
1522 case _PC_SYNC_IO:
1523 *retval = 1;
1524 break;
1525
1526 case _PC_FILESIZEBITS:
1527 *retval = 0; /* XXX Don't know which value should I return. */
1528 break;
1529
1530 default:
1531 error = EINVAL;
1532 }
1533
1534 return error;
1535}
1536
1537static int
1538tmpfs_vptofh(struct vop_vptofh_args *ap)
1539{
1540 struct tmpfs_fid *tfhp;
1541 struct tmpfs_node *node;
1542
1543 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1544 node = VP_TO_TMPFS_NODE(ap->a_vp);
1545
1546 tfhp->tf_len = sizeof(struct tmpfs_fid);
1547 tfhp->tf_id = node->tn_id;
1548 tfhp->tf_gen = node->tn_gen;
1549
1550 return (0);
1551}
1552
1553static int
1554tmpfs_whiteout(struct vop_whiteout_args *ap)
1555{
1556 struct vnode *dvp = ap->a_dvp;
1557 struct componentname *cnp = ap->a_cnp;
1558 struct tmpfs_dirent *de;
1559
1560 switch (ap->a_flags) {
1561 case LOOKUP:
1562 return (0);
1563 case CREATE:
1564 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1565 if (de != NULL)
1566 return (de->td_node == NULL ? 0 : EEXIST);
1567 return (tmpfs_dir_whiteout_add(dvp, cnp));
1568 case DELETE:
1569 tmpfs_dir_whiteout_remove(dvp, cnp);
1570 return (0);
1571 default:
1572 panic("tmpfs_whiteout: unknown op");
1573 }
1574}
1575
1576/* --------------------------------------------------------------------- */
1577
1578/*
1579 * vnode operations vector used for files stored in a tmpfs file system.
1580 */
1581struct vop_vector tmpfs_vnodeop_entries = {
1582 .vop_default = &default_vnodeops,
1583 .vop_lookup = vfs_cache_lookup,
1584 .vop_cachedlookup = tmpfs_lookup,
1585 .vop_create = tmpfs_create,
1586 .vop_mknod = tmpfs_mknod,
1587 .vop_open = tmpfs_open,
1588 .vop_close = tmpfs_close,
1589 .vop_access = tmpfs_access,
1590 .vop_getattr = tmpfs_getattr,
1591 .vop_setattr = tmpfs_setattr,
1592 .vop_read = tmpfs_read,
1593 .vop_write = tmpfs_write,
1594 .vop_fsync = tmpfs_fsync,
1595 .vop_remove = tmpfs_remove,
1596 .vop_link = tmpfs_link,
1597 .vop_rename = tmpfs_rename,
1598 .vop_mkdir = tmpfs_mkdir,
1599 .vop_rmdir = tmpfs_rmdir,
1600 .vop_symlink = tmpfs_symlink,
1601 .vop_readdir = tmpfs_readdir,
1602 .vop_readlink = tmpfs_readlink,
1603 .vop_inactive = tmpfs_inactive,
1604 .vop_reclaim = tmpfs_reclaim,
1605 .vop_print = tmpfs_print,
1606 .vop_pathconf = tmpfs_pathconf,
1607 .vop_vptofh = tmpfs_vptofh,
1608 .vop_whiteout = tmpfs_whiteout,
1609 .vop_bmap = VOP_EOPNOTSUPP,
1610};
1611
1330 return error;
1331}
1332
1333/* --------------------------------------------------------------------- */
1334
1335static int
1336tmpfs_mkdir(struct vop_mkdir_args *v)
1337{
1338 struct vnode *dvp = v->a_dvp;
1339 struct vnode **vpp = v->a_vpp;
1340 struct componentname *cnp = v->a_cnp;
1341 struct vattr *vap = v->a_vap;
1342
1343 MPASS(vap->va_type == VDIR);
1344
1345 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1346}
1347
1348/* --------------------------------------------------------------------- */
1349
1350static int
1351tmpfs_rmdir(struct vop_rmdir_args *v)
1352{
1353 struct vnode *dvp = v->a_dvp;
1354 struct vnode *vp = v->a_vp;
1355
1356 int error;
1357 struct tmpfs_dirent *de;
1358 struct tmpfs_mount *tmp;
1359 struct tmpfs_node *dnode;
1360 struct tmpfs_node *node;
1361
1362 MPASS(VOP_ISLOCKED(dvp));
1363 MPASS(VOP_ISLOCKED(vp));
1364
1365 tmp = VFS_TO_TMPFS(dvp->v_mount);
1366 dnode = VP_TO_TMPFS_DIR(dvp);
1367 node = VP_TO_TMPFS_DIR(vp);
1368
1369 /* Directories with more than two entries ('.' and '..') cannot be
1370 * removed. */
1371 if (node->tn_size > 0) {
1372 error = ENOTEMPTY;
1373 goto out;
1374 }
1375
1376 if ((dnode->tn_flags & APPEND)
1377 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1378 error = EPERM;
1379 goto out;
1380 }
1381
1382 /* This invariant holds only if we are not trying to remove "..".
1383 * We checked for that above so this is safe now. */
1384 MPASS(node->tn_dir.tn_parent == dnode);
1385
1386 /* Get the directory entry associated with node (vp). This was
1387 * filled by tmpfs_lookup while looking up the entry. */
1388 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1389 MPASS(TMPFS_DIRENT_MATCHES(de,
1390 v->a_cnp->cn_nameptr,
1391 v->a_cnp->cn_namelen));
1392
1393 /* Check flags to see if we are allowed to remove the directory. */
1394 if (dnode->tn_flags & APPEND
1395 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1396 error = EPERM;
1397 goto out;
1398 }
1399
1400
1401 /* Detach the directory entry from the directory (dnode). */
1402 tmpfs_dir_detach(dvp, de);
1403 if (v->a_cnp->cn_flags & DOWHITEOUT)
1404 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1405
1406 /* No vnode should be allocated for this entry from this point */
1407 TMPFS_NODE_LOCK(node);
1408 TMPFS_ASSERT_ELOCKED(node);
1409 node->tn_links--;
1410 node->tn_dir.tn_parent = NULL;
1411 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1412 TMPFS_NODE_MODIFIED;
1413
1414 TMPFS_NODE_UNLOCK(node);
1415
1416 TMPFS_NODE_LOCK(dnode);
1417 TMPFS_ASSERT_ELOCKED(dnode);
1418 dnode->tn_links--;
1419 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1420 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1421 TMPFS_NODE_UNLOCK(dnode);
1422
1423 cache_purge(dvp);
1424 cache_purge(vp);
1425
1426 /* Free the directory entry we just deleted. Note that the node
1427 * referred by it will not be removed until the vnode is really
1428 * reclaimed. */
1429 tmpfs_free_dirent(tmp, de, TRUE);
1430
1431 /* Release the deleted vnode (will destroy the node, notify
1432 * interested parties and clean it from the cache). */
1433
1434 dnode->tn_status |= TMPFS_NODE_CHANGED;
1435 tmpfs_update(dvp);
1436
1437 error = 0;
1438
1439out:
1440 return error;
1441}
1442
1443/* --------------------------------------------------------------------- */
1444
1445static int
1446tmpfs_symlink(struct vop_symlink_args *v)
1447{
1448 struct vnode *dvp = v->a_dvp;
1449 struct vnode **vpp = v->a_vpp;
1450 struct componentname *cnp = v->a_cnp;
1451 struct vattr *vap = v->a_vap;
1452 char *target = v->a_target;
1453
1454#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1455 MPASS(vap->va_type == VLNK);
1456#else
1457 vap->va_type = VLNK;
1458#endif
1459
1460 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1461}
1462
1463/* --------------------------------------------------------------------- */
1464
1465static int
1466tmpfs_readdir(struct vop_readdir_args *v)
1467{
1468 struct vnode *vp = v->a_vp;
1469 struct uio *uio = v->a_uio;
1470 int *eofflag = v->a_eofflag;
1471 u_long **cookies = v->a_cookies;
1472 int *ncookies = v->a_ncookies;
1473
1474 int error;
1475 off_t startoff;
1476 off_t cnt = 0;
1477 struct tmpfs_node *node;
1478
1479 /* This operation only makes sense on directory nodes. */
1480 if (vp->v_type != VDIR)
1481 return ENOTDIR;
1482
1483 node = VP_TO_TMPFS_DIR(vp);
1484
1485 startoff = uio->uio_offset;
1486
1487 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1488 error = tmpfs_dir_getdotdent(node, uio);
1489 if (error != 0)
1490 goto outok;
1491 cnt++;
1492 }
1493
1494 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1495 error = tmpfs_dir_getdotdotdent(node, uio);
1496 if (error != 0)
1497 goto outok;
1498 cnt++;
1499 }
1500
1501 error = tmpfs_dir_getdents(node, uio, &cnt);
1502
1503outok:
1504 MPASS(error >= -1);
1505
1506 if (error == -1)
1507 error = (cnt != 0) ? 0 : EINVAL;
1508
1509 if (eofflag != NULL)
1510 *eofflag =
1511 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1512
1513 /* Update NFS-related variables. */
1514 if (error == 0 && cookies != NULL && ncookies != NULL) {
1515 off_t i;
1516 off_t off = startoff;
1517 struct tmpfs_dirent *de = NULL;
1518
1519 *ncookies = cnt;
1520 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1521
1522 for (i = 0; i < cnt; i++) {
1523 MPASS(off != TMPFS_DIRCOOKIE_EOF);
1524 if (off == TMPFS_DIRCOOKIE_DOT) {
1525 off = TMPFS_DIRCOOKIE_DOTDOT;
1526 } else {
1527 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1528 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1529 } else if (de != NULL) {
1530 de = TAILQ_NEXT(de, td_entries);
1531 } else {
1532 de = tmpfs_dir_lookupbycookie(node,
1533 off);
1534 MPASS(de != NULL);
1535 de = TAILQ_NEXT(de, td_entries);
1536 }
1537 if (de == NULL)
1538 off = TMPFS_DIRCOOKIE_EOF;
1539 else
1540 off = tmpfs_dircookie(de);
1541 }
1542
1543 (*cookies)[i] = off;
1544 }
1545 MPASS(uio->uio_offset == off);
1546 }
1547
1548 return error;
1549}
1550
1551/* --------------------------------------------------------------------- */
1552
1553static int
1554tmpfs_readlink(struct vop_readlink_args *v)
1555{
1556 struct vnode *vp = v->a_vp;
1557 struct uio *uio = v->a_uio;
1558
1559 int error;
1560 struct tmpfs_node *node;
1561
1562 MPASS(uio->uio_offset == 0);
1563 MPASS(vp->v_type == VLNK);
1564
1565 node = VP_TO_TMPFS_NODE(vp);
1566
1567 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1568 uio);
1569 node->tn_status |= TMPFS_NODE_ACCESSED;
1570
1571 return error;
1572}
1573
1574/* --------------------------------------------------------------------- */
1575
1576static int
1577tmpfs_inactive(struct vop_inactive_args *v)
1578{
1579 struct vnode *vp = v->a_vp;
1580 struct thread *l = v->a_td;
1581
1582 struct tmpfs_node *node;
1583
1584 MPASS(VOP_ISLOCKED(vp));
1585
1586 node = VP_TO_TMPFS_NODE(vp);
1587
1588 if (node->tn_links == 0)
1589 vrecycle(vp, l);
1590
1591 return 0;
1592}
1593
1594/* --------------------------------------------------------------------- */
1595
1596int
1597tmpfs_reclaim(struct vop_reclaim_args *v)
1598{
1599 struct vnode *vp = v->a_vp;
1600
1601 struct tmpfs_mount *tmp;
1602 struct tmpfs_node *node;
1603
1604 node = VP_TO_TMPFS_NODE(vp);
1605 tmp = VFS_TO_TMPFS(vp->v_mount);
1606
1607 vnode_destroy_vobject(vp);
1608 cache_purge(vp);
1609
1610 TMPFS_NODE_LOCK(node);
1611 TMPFS_ASSERT_ELOCKED(node);
1612 tmpfs_free_vp(vp);
1613
1614 /* If the node referenced by this vnode was deleted by the user,
1615 * we must free its associated data structures (now that the vnode
1616 * is being reclaimed). */
1617 if (node->tn_links == 0 &&
1618 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1619 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1620 TMPFS_NODE_UNLOCK(node);
1621 tmpfs_free_node(tmp, node);
1622 } else
1623 TMPFS_NODE_UNLOCK(node);
1624
1625 MPASS(vp->v_data == NULL);
1626 return 0;
1627}
1628
1629/* --------------------------------------------------------------------- */
1630
1631static int
1632tmpfs_print(struct vop_print_args *v)
1633{
1634 struct vnode *vp = v->a_vp;
1635
1636 struct tmpfs_node *node;
1637
1638 node = VP_TO_TMPFS_NODE(vp);
1639
1640 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1641 node, node->tn_flags, node->tn_links);
1642 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1643 node->tn_mode, node->tn_uid, node->tn_gid,
1644 (intmax_t)node->tn_size, node->tn_status);
1645
1646 if (vp->v_type == VFIFO)
1647 fifo_printinfo(vp);
1648
1649 printf("\n");
1650
1651 return 0;
1652}
1653
1654/* --------------------------------------------------------------------- */
1655
1656static int
1657tmpfs_pathconf(struct vop_pathconf_args *v)
1658{
1659 int name = v->a_name;
1660 register_t *retval = v->a_retval;
1661
1662 int error;
1663
1664 error = 0;
1665
1666 switch (name) {
1667 case _PC_LINK_MAX:
1668 *retval = LINK_MAX;
1669 break;
1670
1671 case _PC_NAME_MAX:
1672 *retval = NAME_MAX;
1673 break;
1674
1675 case _PC_PATH_MAX:
1676 *retval = PATH_MAX;
1677 break;
1678
1679 case _PC_PIPE_BUF:
1680 *retval = PIPE_BUF;
1681 break;
1682
1683 case _PC_CHOWN_RESTRICTED:
1684 *retval = 1;
1685 break;
1686
1687 case _PC_NO_TRUNC:
1688 *retval = 1;
1689 break;
1690
1691 case _PC_SYNC_IO:
1692 *retval = 1;
1693 break;
1694
1695 case _PC_FILESIZEBITS:
1696 *retval = 0; /* XXX Don't know which value should I return. */
1697 break;
1698
1699 default:
1700 error = EINVAL;
1701 }
1702
1703 return error;
1704}
1705
1706static int
1707tmpfs_vptofh(struct vop_vptofh_args *ap)
1708{
1709 struct tmpfs_fid *tfhp;
1710 struct tmpfs_node *node;
1711
1712 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1713 node = VP_TO_TMPFS_NODE(ap->a_vp);
1714
1715 tfhp->tf_len = sizeof(struct tmpfs_fid);
1716 tfhp->tf_id = node->tn_id;
1717 tfhp->tf_gen = node->tn_gen;
1718
1719 return (0);
1720}
1721
1722static int
1723tmpfs_whiteout(struct vop_whiteout_args *ap)
1724{
1725 struct vnode *dvp = ap->a_dvp;
1726 struct componentname *cnp = ap->a_cnp;
1727 struct tmpfs_dirent *de;
1728
1729 switch (ap->a_flags) {
1730 case LOOKUP:
1731 return (0);
1732 case CREATE:
1733 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1734 if (de != NULL)
1735 return (de->td_node == NULL ? 0 : EEXIST);
1736 return (tmpfs_dir_whiteout_add(dvp, cnp));
1737 case DELETE:
1738 tmpfs_dir_whiteout_remove(dvp, cnp);
1739 return (0);
1740 default:
1741 panic("tmpfs_whiteout: unknown op");
1742 }
1743}
1744
1745/* --------------------------------------------------------------------- */
1746
1747/*
1748 * vnode operations vector used for files stored in a tmpfs file system.
1749 */
1750struct vop_vector tmpfs_vnodeop_entries = {
1751 .vop_default = &default_vnodeops,
1752 .vop_lookup = vfs_cache_lookup,
1753 .vop_cachedlookup = tmpfs_lookup,
1754 .vop_create = tmpfs_create,
1755 .vop_mknod = tmpfs_mknod,
1756 .vop_open = tmpfs_open,
1757 .vop_close = tmpfs_close,
1758 .vop_access = tmpfs_access,
1759 .vop_getattr = tmpfs_getattr,
1760 .vop_setattr = tmpfs_setattr,
1761 .vop_read = tmpfs_read,
1762 .vop_write = tmpfs_write,
1763 .vop_fsync = tmpfs_fsync,
1764 .vop_remove = tmpfs_remove,
1765 .vop_link = tmpfs_link,
1766 .vop_rename = tmpfs_rename,
1767 .vop_mkdir = tmpfs_mkdir,
1768 .vop_rmdir = tmpfs_rmdir,
1769 .vop_symlink = tmpfs_symlink,
1770 .vop_readdir = tmpfs_readdir,
1771 .vop_readlink = tmpfs_readlink,
1772 .vop_inactive = tmpfs_inactive,
1773 .vop_reclaim = tmpfs_reclaim,
1774 .vop_print = tmpfs_print,
1775 .vop_pathconf = tmpfs_pathconf,
1776 .vop_vptofh = tmpfs_vptofh,
1777 .vop_whiteout = tmpfs_whiteout,
1778 .vop_bmap = VOP_EOPNOTSUPP,
1779};
1780