]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/fs/tmpfs/tmpfs_vnops.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / fs / tmpfs / tmpfs_vnops.c
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$");
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/resourcevar.h>
46 #include <sys/stat.h>
47 #include <sys/systm.h>
48 #include <sys/unistd.h>
49 #include <sys/vnode.h>
50
51 #include <vm/vm.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_pager.h>
55 #include <sys/sched.h>
56 #include <sys/sf_buf.h>
57 #include <machine/_inttypes.h>
58
59 #include <fs/fifofs/fifo.h>
60 #include <fs/tmpfs/tmpfs_vnops.h>
61 #include <fs/tmpfs/tmpfs.h>
62
63 /* --------------------------------------------------------------------- */
64
65 static int
66 tmpfs_lookup(struct vop_cachedlookup_args *v)
67 {
68         struct vnode *dvp = v->a_dvp;
69         struct vnode **vpp = v->a_vpp;
70         struct componentname *cnp = v->a_cnp;
71         struct thread *td = cnp->cn_thread;
72
73         int error;
74         struct tmpfs_dirent *de;
75         struct tmpfs_node *dnode;
76
77         dnode = VP_TO_TMPFS_DIR(dvp);
78         *vpp = NULLVP;
79
80         /* Check accessibility of requested node as a first step. */
81         error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
82         if (error != 0)
83                 goto out;
84
85         /* We cannot be requesting the parent directory of the root node. */
86         MPASS(IMPLIES(dnode->tn_type == VDIR &&
87             dnode->tn_dir.tn_parent == dnode,
88             !(cnp->cn_flags & ISDOTDOT)));
89
90         if (cnp->cn_flags & ISDOTDOT) {
91                 int ltype = 0;
92
93                 ltype = VOP_ISLOCKED(dvp, td);
94                 vhold(dvp);
95                 VOP_UNLOCK(dvp, 0, td);
96                 /* Allocate a new vnode on the matching entry. */
97                 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
98                     cnp->cn_lkflags, vpp, td);
99
100                 vn_lock(dvp, ltype | LK_RETRY, td);
101                 vdrop(dvp);
102         } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
103                 VREF(dvp);
104                 *vpp = dvp;
105                 error = 0;
106         } else {
107                 de = tmpfs_dir_lookup(dnode, NULL, cnp);
108                 if (de == NULL) {
109                         /* The entry was not found in the directory.
110                          * This is OK if we are creating or renaming an
111                          * entry and are working on the last component of
112                          * the path name. */
113                         if ((cnp->cn_flags & ISLASTCN) &&
114                             (cnp->cn_nameiop == CREATE || \
115                             cnp->cn_nameiop == RENAME)) {
116                                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
117                                     cnp->cn_thread);
118                                 if (error != 0)
119                                         goto out;
120
121                                 /* Keep the component name in the buffer for
122                                  * future uses. */
123                                 cnp->cn_flags |= SAVENAME;
124
125                                 error = EJUSTRETURN;
126                         } else
127                                 error = ENOENT;
128                 } else {
129                         struct tmpfs_node *tnode;
130
131                         /* The entry was found, so get its associated
132                          * tmpfs_node. */
133                         tnode = de->td_node;
134
135                         /* If we are not at the last path component and
136                          * found a non-directory or non-link entry (which
137                          * may itself be pointing to a directory), raise
138                          * an error. */
139                         if ((tnode->tn_type != VDIR &&
140                             tnode->tn_type != VLNK) &&
141                             !(cnp->cn_flags & ISLASTCN)) {
142                                 error = ENOTDIR;
143                                 goto out;
144                         }
145
146                         /* If we are deleting or renaming the entry, keep
147                          * track of its tmpfs_dirent so that it can be
148                          * easily deleted later. */
149                         if ((cnp->cn_flags & ISLASTCN) &&
150                             (cnp->cn_nameiop == DELETE ||
151                             cnp->cn_nameiop == RENAME)) {
152                                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
153                                     cnp->cn_thread);
154                                 if (error != 0)
155                                         goto out;
156
157                                 /* Allocate a new vnode on the matching entry. */
158                                 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
159                                                 cnp->cn_lkflags, vpp, td);
160                                 if (error != 0)
161                                         goto out;
162
163                                 if ((dnode->tn_mode & S_ISTXT) &&
164                                   VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
165                                   VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
166                                         error = EPERM;
167                                         vput(*vpp);
168                                         *vpp = NULL;
169                                         goto out;
170                                 }
171                                 cnp->cn_flags |= SAVENAME;
172                         } else {
173                                 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
174                                                 cnp->cn_lkflags, vpp, td);
175                         }
176                 }
177         }
178
179         /* Store the result of this lookup in the cache.  Avoid this if the
180          * request was for creation, as it does not improve timings on
181          * emprical tests. */
182         if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
183                 cache_enter(dvp, *vpp, cnp);
184
185 out:
186         /* If there were no errors, *vpp cannot be null and it must be
187          * locked. */
188         MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp, td)));
189
190         return error;
191 }
192
193 /* --------------------------------------------------------------------- */
194
195 static int
196 tmpfs_create(struct vop_create_args *v)
197 {
198         struct vnode *dvp = v->a_dvp;
199         struct vnode **vpp = v->a_vpp;
200         struct componentname *cnp = v->a_cnp;
201         struct vattr *vap = v->a_vap;
202
203         MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
204
205         return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
206 }
207 /* --------------------------------------------------------------------- */
208
209 static int
210 tmpfs_mknod(struct vop_mknod_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         if (vap->va_type != VBLK && vap->va_type != VCHR &&
218             vap->va_type != VFIFO)
219                 return EINVAL;
220
221         return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
222 }
223
224 /* --------------------------------------------------------------------- */
225
226 static int
227 tmpfs_open(struct vop_open_args *v)
228 {
229         struct vnode *vp = v->a_vp;
230         int mode = v->a_mode;
231
232         int error;
233         struct tmpfs_node *node;
234
235         MPASS(VOP_ISLOCKED(vp, v->a_td));
236
237         node = VP_TO_TMPFS_NODE(vp);
238
239         /* The file is still active but all its names have been removed
240          * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
241          * it is about to die. */
242         if (node->tn_links < 1)
243                 return (ENOENT);
244
245         /* If the file is marked append-only, deny write requests. */
246         if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
247                 error = EPERM;
248         else {
249                 error = 0;
250                 vnode_create_vobject(vp, node->tn_size, v->a_td);
251         }
252
253         MPASS(VOP_ISLOCKED(vp, v->a_td));
254         return error;
255 }
256
257 /* --------------------------------------------------------------------- */
258
259 static int
260 tmpfs_close(struct vop_close_args *v)
261 {
262         struct vnode *vp = v->a_vp;
263
264         struct tmpfs_node *node;
265
266         MPASS(VOP_ISLOCKED(vp, v->a_td));
267
268         node = VP_TO_TMPFS_NODE(vp);
269
270         if (node->tn_links > 0) {
271                 /* Update node times.  No need to do it if the node has
272                  * been deleted, because it will vanish after we return. */
273                 tmpfs_update(vp);
274         }
275
276         return 0;
277 }
278
279 /* --------------------------------------------------------------------- */
280
281 int
282 tmpfs_access(struct vop_access_args *v)
283 {
284         struct vnode *vp = v->a_vp;
285         int mode = v->a_mode;
286         struct ucred *cred = v->a_cred;
287
288         int error;
289         struct tmpfs_node *node;
290
291         MPASS(VOP_ISLOCKED(vp, v->a_td));
292
293         node = VP_TO_TMPFS_NODE(vp);
294
295         switch (vp->v_type) {
296         case VDIR:
297                 /* FALLTHROUGH */
298         case VLNK:
299                 /* FALLTHROUGH */
300         case VREG:
301                 if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
302                         error = EROFS;
303                         goto out;
304                 }
305                 break;
306
307         case VBLK:
308                 /* FALLTHROUGH */
309         case VCHR:
310                 /* FALLTHROUGH */
311         case VSOCK:
312                 /* FALLTHROUGH */
313         case VFIFO:
314                 break;
315
316         default:
317                 error = EINVAL;
318                 goto out;
319         }
320
321         if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
322                 error = EPERM;
323                 goto out;
324         }
325
326         error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
327             node->tn_gid, mode, cred, NULL);
328
329 out:
330         MPASS(VOP_ISLOCKED(vp, v->a_td));
331
332         return error;
333 }
334
335 /* --------------------------------------------------------------------- */
336
337 int
338 tmpfs_getattr(struct vop_getattr_args *v)
339 {
340         struct vnode *vp = v->a_vp;
341         struct vattr *vap = v->a_vap;
342
343         struct tmpfs_node *node;
344
345         node = VP_TO_TMPFS_NODE(vp);
346
347         tmpfs_update(vp);
348
349         vap->va_type = vp->v_type;
350         vap->va_mode = node->tn_mode;
351         vap->va_nlink = node->tn_links;
352         vap->va_uid = node->tn_uid;
353         vap->va_gid = node->tn_gid;
354         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
355         vap->va_fileid = node->tn_id;
356         vap->va_size = node->tn_size;
357         vap->va_blocksize = PAGE_SIZE;
358         vap->va_atime = node->tn_atime;
359         vap->va_mtime = node->tn_mtime;
360         vap->va_ctime = node->tn_ctime;
361         vap->va_birthtime = node->tn_birthtime;
362         vap->va_gen = node->tn_gen;
363         vap->va_flags = node->tn_flags;
364         vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
365                 node->tn_rdev : NODEV;
366         vap->va_bytes = round_page(node->tn_size);
367         vap->va_filerev = 0;
368
369         return 0;
370 }
371
372 /* --------------------------------------------------------------------- */
373
374 /* XXX Should this operation be atomic?  I think it should, but code in
375  * XXX other places (e.g., ufs) doesn't seem to be... */
376 int
377 tmpfs_setattr(struct vop_setattr_args *v)
378 {
379         struct vnode *vp = v->a_vp;
380         struct vattr *vap = v->a_vap;
381         struct ucred *cred = v->a_cred;
382         struct thread *l = v->a_td;
383
384         int error;
385
386         MPASS(VOP_ISLOCKED(vp, l));
387
388         error = 0;
389
390         /* Abort if any unsettable attribute is given. */
391         if (vap->va_type != VNON ||
392             vap->va_nlink != VNOVAL ||
393             vap->va_fsid != VNOVAL ||
394             vap->va_fileid != VNOVAL ||
395             vap->va_blocksize != VNOVAL ||
396             vap->va_gen != VNOVAL ||
397             vap->va_rdev != VNOVAL ||
398             vap->va_bytes != VNOVAL)
399                 error = EINVAL;
400
401         if (error == 0 && (vap->va_flags != VNOVAL))
402                 error = tmpfs_chflags(vp, vap->va_flags, cred, l);
403
404         if (error == 0 && (vap->va_size != VNOVAL))
405                 error = tmpfs_chsize(vp, vap->va_size, cred, l);
406
407         if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
408                 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred,
409                     l);
410
411         if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
412                 error = tmpfs_chmod(vp, vap->va_mode, cred, l);
413
414         if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
415             vap->va_atime.tv_nsec != VNOVAL) ||
416             (vap->va_mtime.tv_sec != VNOVAL &&
417             vap->va_mtime.tv_nsec != VNOVAL) ||
418             (vap->va_birthtime.tv_sec != VNOVAL &&
419             vap->va_birthtime.tv_nsec != VNOVAL)))
420                 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
421                         &vap->va_birthtime, vap->va_vaflags, cred, l);
422
423         /* Update the node times.  We give preference to the error codes
424          * generated by this function rather than the ones that may arise
425          * from tmpfs_update. */
426         tmpfs_update(vp);
427
428         MPASS(VOP_ISLOCKED(vp, l));
429
430         return error;
431 }
432
433 /* --------------------------------------------------------------------- */
434
435 static int
436 tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
437 {
438         vm_pindex_t     idx;
439         vm_page_t       m;
440         struct sf_buf   *sf;
441         off_t           offset, addr;
442         size_t          tlen;
443         caddr_t         va;
444         int             error;
445
446         addr = uio->uio_offset;
447         idx = OFF_TO_IDX(addr);
448         offset = addr & PAGE_MASK;
449         tlen = MIN(PAGE_SIZE - offset, len);
450
451         if ((vobj == NULL) || (vobj->resident_page_count == 0))
452                 goto nocache;
453
454         VM_OBJECT_LOCK(vobj);
455 lookupvpg:
456         if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
457             vm_page_is_valid(m, offset, tlen)) {
458                 if (vm_page_sleep_if_busy(m, FALSE, "tmfsmr"))
459                         goto lookupvpg;
460                 vm_page_busy(m);
461                 VM_OBJECT_UNLOCK(vobj);
462                 sched_pin();
463                 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
464                 va = (caddr_t)sf_buf_kva(sf);
465                 error = uiomove(va + offset, tlen, uio);
466                 sf_buf_free(sf);
467                 sched_unpin();
468                 VM_OBJECT_LOCK(vobj);
469                 vm_page_wakeup(m);
470                 VM_OBJECT_UNLOCK(vobj);
471                 return  (error);
472         }
473         VM_OBJECT_UNLOCK(vobj);
474 nocache:
475         VM_OBJECT_LOCK(tobj);
476         vm_object_pip_add(tobj, 1);
477         m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
478             VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
479         if (m->valid != VM_PAGE_BITS_ALL) {
480                 int behind, ahead;
481                 if (vm_pager_has_page(tobj, idx, &behind, &ahead)) {
482                         error = vm_pager_get_pages(tobj, &m, 1, 0);
483                         if (error != 0) {
484                                 printf("tmpfs get pages from pager error [read]\n");
485                                 goto out;
486                         }
487                 } else
488                         vm_page_zero_invalid(m, TRUE);
489         }
490         VM_OBJECT_UNLOCK(tobj);
491         sched_pin();
492         sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
493         va = (caddr_t)sf_buf_kva(sf);
494         error = uiomove(va + offset, tlen, uio);
495         sf_buf_free(sf);
496         sched_unpin();
497         VM_OBJECT_LOCK(tobj);
498 out:
499         vm_page_lock_queues();
500         vm_page_unwire(m, 0);
501         vm_page_activate(m);
502         vm_page_unlock_queues();
503         vm_page_wakeup(m);
504         vm_object_pip_subtract(tobj, 1);
505         VM_OBJECT_UNLOCK(tobj);
506
507         return  (error);
508 }
509
510 static int
511 tmpfs_read(struct vop_read_args *v)
512 {
513         struct vnode *vp = v->a_vp;
514         struct uio *uio = v->a_uio;
515
516         struct tmpfs_node *node;
517         vm_object_t uobj;
518         size_t len;
519         int resid;
520
521         int error;
522
523         node = VP_TO_TMPFS_NODE(vp);
524
525         if (vp->v_type != VREG) {
526                 error = EISDIR;
527                 goto out;
528         }
529
530         if (uio->uio_offset < 0) {
531                 error = EINVAL;
532                 goto out;
533         }
534
535         node->tn_status |= TMPFS_NODE_ACCESSED;
536
537         uobj = node->tn_reg.tn_aobj;
538         while ((resid = uio->uio_resid) > 0) {
539                 error = 0;
540                 if (node->tn_size <= uio->uio_offset)
541                         break;
542                 len = MIN(node->tn_size - uio->uio_offset, resid);
543                 if (len == 0)
544                         break;
545                 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
546                 if ((error != 0) || (resid == uio->uio_resid))
547                         break;
548         }
549
550 out:
551
552         return error;
553 }
554
555 /* --------------------------------------------------------------------- */
556
557 static int
558 tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
559 {
560         vm_pindex_t     idx;
561         vm_page_t       vpg, tpg;
562         struct sf_buf   *sf;
563         off_t           offset, addr;
564         size_t          tlen;
565         caddr_t         va;
566         int             error;
567
568         addr = uio->uio_offset;
569         idx = OFF_TO_IDX(addr);
570         offset = addr & PAGE_MASK;
571         tlen = MIN(PAGE_SIZE - offset, len);
572
573         if ((vobj == NULL) || (vobj->resident_page_count == 0)) {
574                 vpg = NULL;
575                 goto nocache;
576         }
577
578         VM_OBJECT_LOCK(vobj);
579 lookupvpg:
580         if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
581             vm_page_is_valid(vpg, offset, tlen)) {
582                 if (vm_page_sleep_if_busy(vpg, FALSE, "tmfsmw"))
583                         goto lookupvpg;
584                 vm_page_busy(vpg);
585                 vm_page_lock_queues();
586                 vm_page_undirty(vpg);
587                 vm_page_unlock_queues();
588                 VM_OBJECT_UNLOCK(vobj);
589                 sched_pin();
590                 sf = sf_buf_alloc(vpg, SFB_CPUPRIVATE);
591                 va = (caddr_t)sf_buf_kva(sf);
592                 error = uiomove(va + offset, tlen, uio);
593                 sf_buf_free(sf);
594                 sched_unpin();
595         } else {
596                 VM_OBJECT_UNLOCK(vobj);
597                 vpg = NULL;
598         }
599 nocache:
600         VM_OBJECT_LOCK(tobj);
601         vm_object_pip_add(tobj, 1);
602         tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
603             VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
604         if (tpg->valid != VM_PAGE_BITS_ALL) {
605                 int behind, ahead;
606                 if (vm_pager_has_page(tobj, idx, &behind, &ahead)) {
607                         error = vm_pager_get_pages(tobj, &tpg, 1, 0);
608                         if (error != 0) {
609                                 printf("tmpfs get pages from pager error [write]\n");
610                                 goto out;
611                         }
612                 } else
613                         vm_page_zero_invalid(tpg, TRUE);
614         }
615         VM_OBJECT_UNLOCK(tobj);
616         if (vpg == NULL) {
617                 sched_pin();
618                 sf = sf_buf_alloc(tpg, SFB_CPUPRIVATE);
619                 va = (caddr_t)sf_buf_kva(sf);
620                 error = uiomove(va + offset, tlen, uio);
621                 sf_buf_free(sf);
622                 sched_unpin();
623         } else {
624                 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
625                 pmap_copy_page(vpg, tpg);
626         }
627         VM_OBJECT_LOCK(tobj);
628 out:
629         if (vobj != NULL)
630                 VM_OBJECT_LOCK(vobj);
631         vm_page_lock_queues();
632         if (error == 0) {
633                 vm_page_set_validclean(tpg, offset, tlen);
634                 vm_page_zero_invalid(tpg, TRUE);
635                 vm_page_dirty(tpg);
636         }
637         vm_page_unwire(tpg, 0);
638         vm_page_activate(tpg);
639         vm_page_unlock_queues();
640         vm_page_wakeup(tpg);
641         if (vpg != NULL)
642                 vm_page_wakeup(vpg);
643         if (vobj != NULL)
644                 VM_OBJECT_UNLOCK(vobj);
645         vm_object_pip_subtract(tobj, 1);
646         VM_OBJECT_UNLOCK(tobj);
647
648         return  (error);
649 }
650
651 static int
652 tmpfs_write(struct vop_write_args *v)
653 {
654         struct vnode *vp = v->a_vp;
655         struct uio *uio = v->a_uio;
656         int ioflag = v->a_ioflag;
657         struct thread *td = uio->uio_td;
658
659         boolean_t extended;
660         int error = 0;
661         off_t oldsize;
662         struct tmpfs_node *node;
663         vm_object_t uobj;
664         size_t len;
665         int resid;
666
667         node = VP_TO_TMPFS_NODE(vp);
668         oldsize = node->tn_size;
669
670         if (uio->uio_offset < 0 || vp->v_type != VREG) {
671                 error = EINVAL;
672                 goto out;
673         }
674
675         if (uio->uio_resid == 0) {
676                 error = 0;
677                 goto out;
678         }
679
680         if (ioflag & IO_APPEND)
681                 uio->uio_offset = node->tn_size;
682
683         if (uio->uio_offset + uio->uio_resid >
684           VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
685                 return (EFBIG);
686
687         if (vp->v_type == VREG && td != NULL) {
688                 PROC_LOCK(td->td_proc);
689                 if (uio->uio_offset + uio->uio_resid >
690                   lim_cur(td->td_proc, RLIMIT_FSIZE)) {
691                         psignal(td->td_proc, SIGXFSZ);
692                         PROC_UNLOCK(td->td_proc);
693                         return (EFBIG);
694                 }
695                 PROC_UNLOCK(td->td_proc);
696         }
697
698         extended = uio->uio_offset + uio->uio_resid > node->tn_size;
699         if (extended) {
700                 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
701                 if (error != 0)
702                         goto out;
703         }
704
705         uobj = node->tn_reg.tn_aobj;
706         while ((resid = uio->uio_resid) > 0) {
707                 if (node->tn_size <= uio->uio_offset)
708                         break;
709                 len = MIN(node->tn_size - uio->uio_offset, resid);
710                 if (len == 0)
711                         break;
712                 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
713                 if ((error != 0) || (resid == uio->uio_resid))
714                         break;
715         }
716
717         node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
718             (extended ? TMPFS_NODE_CHANGED : 0);
719
720         if (node->tn_mode & (S_ISUID | S_ISGID)) {
721                 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
722                         node->tn_mode &= ~(S_ISUID | S_ISGID);
723         }
724
725         if (error != 0)
726                 (void)tmpfs_reg_resize(vp, oldsize);
727
728 out:
729         MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
730         MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
731
732         return error;
733 }
734
735 /* --------------------------------------------------------------------- */
736
737 static int
738 tmpfs_fsync(struct vop_fsync_args *v)
739 {
740         struct vnode *vp = v->a_vp;
741
742         MPASS(VOP_ISLOCKED(vp, v->a_td));
743
744         tmpfs_update(vp);
745
746         return 0;
747 }
748
749 /* --------------------------------------------------------------------- */
750
751 static int
752 tmpfs_remove(struct vop_remove_args *v)
753 {
754         struct vnode *dvp = v->a_dvp;
755         struct vnode *vp = v->a_vp;
756
757         int error;
758         struct tmpfs_dirent *de;
759         struct tmpfs_mount *tmp;
760         struct tmpfs_node *dnode;
761         struct tmpfs_node *node;
762
763         MPASS(VOP_ISLOCKED(dvp, v->a_cnp->cn_thread));
764         MPASS(VOP_ISLOCKED(vp, v->a_cnp->cn_thread));
765
766         if (vp->v_type == VDIR) {
767                 error = EISDIR;
768                 goto out;
769         }
770
771         dnode = VP_TO_TMPFS_DIR(dvp);
772         node = VP_TO_TMPFS_NODE(vp);
773         tmp = VFS_TO_TMPFS(vp->v_mount);
774         de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
775         MPASS(de != NULL);
776
777         /* Files marked as immutable or append-only cannot be deleted. */
778         if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
779             (dnode->tn_flags & APPEND)) {
780                 error = EPERM;
781                 goto out;
782         }
783
784         /* Remove the entry from the directory; as it is a file, we do not
785          * have to change the number of hard links of the directory. */
786         tmpfs_dir_detach(dvp, de);
787
788         /* Free the directory entry we just deleted.  Note that the node
789          * referred by it will not be removed until the vnode is really
790          * reclaimed. */
791         tmpfs_free_dirent(tmp, de, TRUE);
792
793         if (node->tn_links > 0)
794                 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
795             TMPFS_NODE_MODIFIED;
796         error = 0;
797
798 out:
799
800         return error;
801 }
802
803 /* --------------------------------------------------------------------- */
804
805 static int
806 tmpfs_link(struct vop_link_args *v)
807 {
808         struct vnode *dvp = v->a_tdvp;
809         struct vnode *vp = v->a_vp;
810         struct componentname *cnp = v->a_cnp;
811
812         int error;
813         struct tmpfs_dirent *de;
814         struct tmpfs_node *node;
815
816         MPASS(VOP_ISLOCKED(dvp, cnp->cn_thread));
817         MPASS(cnp->cn_flags & HASBUF);
818         MPASS(dvp != vp); /* XXX When can this be false? */
819
820         node = VP_TO_TMPFS_NODE(vp);
821
822         /* XXX: Why aren't the following two tests done by the caller? */
823
824         /* Hard links of directories are forbidden. */
825         if (vp->v_type == VDIR) {
826                 error = EPERM;
827                 goto out;
828         }
829
830         /* Cannot create cross-device links. */
831         if (dvp->v_mount != vp->v_mount) {
832                 error = EXDEV;
833                 goto out;
834         }
835
836         /* Ensure that we do not overflow the maximum number of links imposed
837          * by the system. */
838         MPASS(node->tn_links <= LINK_MAX);
839         if (node->tn_links == LINK_MAX) {
840                 error = EMLINK;
841                 goto out;
842         }
843
844         /* We cannot create links of files marked immutable or append-only. */
845         if (node->tn_flags & (IMMUTABLE | APPEND)) {
846                 error = EPERM;
847                 goto out;
848         }
849
850         /* Allocate a new directory entry to represent the node. */
851         error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
852             cnp->cn_nameptr, cnp->cn_namelen, &de);
853         if (error != 0)
854                 goto out;
855
856         /* Insert the new directory entry into the appropriate directory. */
857         tmpfs_dir_attach(dvp, de);
858
859         /* vp link count has changed, so update node times. */
860         node->tn_status |= TMPFS_NODE_CHANGED;
861         tmpfs_update(vp);
862
863         error = 0;
864
865 out:
866         return error;
867 }
868
869 /* --------------------------------------------------------------------- */
870
871 static int
872 tmpfs_rename(struct vop_rename_args *v)
873 {
874         struct vnode *fdvp = v->a_fdvp;
875         struct vnode *fvp = v->a_fvp;
876         struct componentname *fcnp = v->a_fcnp;
877         struct vnode *tdvp = v->a_tdvp;
878         struct vnode *tvp = v->a_tvp;
879         struct componentname *tcnp = v->a_tcnp;
880
881         char *newname;
882         int error;
883         struct tmpfs_dirent *de;
884         struct tmpfs_node *fdnode;
885         struct tmpfs_node *fnode;
886         struct tmpfs_node *tnode;
887         struct tmpfs_node *tdnode;
888
889         MPASS(VOP_ISLOCKED(tdvp, tcnp->cn_thread));
890         MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp, tcnp->cn_thread)));
891         MPASS(fcnp->cn_flags & HASBUF);
892         MPASS(tcnp->cn_flags & HASBUF);
893
894         tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
895
896         /* Disallow cross-device renames.
897          * XXX Why isn't this done by the caller? */
898         if (fvp->v_mount != tdvp->v_mount ||
899             (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
900                 error = EXDEV;
901                 goto out;
902         }
903
904         tdnode = VP_TO_TMPFS_DIR(tdvp);
905
906         /* If source and target are the same file, there is nothing to do. */
907         if (fvp == tvp) {
908                 error = 0;
909                 goto out;
910         }
911
912         /* If we need to move the directory between entries, lock the
913          * source so that we can safely operate on it. */
914         if (tdvp != fdvp) {
915                 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY, tcnp->cn_thread);
916                 if (error != 0)
917                         goto out;
918         }
919         fdnode = VP_TO_TMPFS_DIR(fdvp);
920         fnode = VP_TO_TMPFS_NODE(fvp);
921         de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
922
923         /* Avoid manipulating '.' and '..' entries. */
924         if (de == NULL) {
925                 MPASS(fvp->v_type == VDIR);
926                 error = EINVAL;
927                 goto out_locked;
928         }
929         MPASS(de->td_node == fnode);
930
931         /* If re-naming a directory to another preexisting directory
932          * ensure that the target directory is empty so that its
933          * removal causes no side effects.
934          * Kern_rename gurantees the destination to be a directory
935          * if the source is one. */
936         if (tvp != NULL) {
937                 MPASS(tnode != NULL);
938
939                 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
940                     (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
941                         error = EPERM;
942                         goto out_locked;
943                 }
944
945                 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
946                         if (tnode->tn_size > 0) {
947                                 error = ENOTEMPTY;
948                                 goto out_locked;
949                         }
950                 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
951                         error = ENOTDIR;
952                         goto out_locked;
953                 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
954                         error = EISDIR;
955                         goto out_locked;
956                 } else {
957                         MPASS(fnode->tn_type != VDIR &&
958                                 tnode->tn_type != VDIR);
959                 }
960         }
961
962         if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
963             || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
964                 error = EPERM;
965                 goto out_locked;
966         }
967
968         /* Ensure that we have enough memory to hold the new name, if it
969          * has to be changed. */
970         if (fcnp->cn_namelen != tcnp->cn_namelen ||
971             memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
972                 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
973         } else
974                 newname = NULL;
975
976         /* If the node is being moved to another directory, we have to do
977          * the move. */
978         if (fdnode != tdnode) {
979                 /* In case we are moving a directory, we have to adjust its
980                  * parent to point to the new parent. */
981                 if (de->td_node->tn_type == VDIR) {
982                         struct tmpfs_node *n;
983
984                         /* Ensure the target directory is not a child of the
985                          * directory being moved.  Otherwise, we'd end up
986                          * with stale nodes. */
987                         n = tdnode;
988                         while (n != n->tn_dir.tn_parent) {
989                                 if (n == fnode) {
990                                         error = EINVAL;
991                                         if (newname != NULL)
992                                                     free(newname, M_TMPFSNAME);
993                                         goto out_locked;
994                                 }
995                                 n = n->tn_dir.tn_parent;
996                         }
997
998                         /* Adjust the parent pointer. */
999                         TMPFS_VALIDATE_DIR(fnode);
1000                         de->td_node->tn_dir.tn_parent = tdnode;
1001
1002                         /* As a result of changing the target of the '..'
1003                          * entry, the link count of the source and target
1004                          * directories has to be adjusted. */
1005                         fdnode->tn_links--;
1006                         tdnode->tn_links++;
1007                 }
1008
1009                 /* Do the move: just remove the entry from the source directory
1010                  * and insert it into the target one. */
1011                 tmpfs_dir_detach(fdvp, de);
1012                 tmpfs_dir_attach(tdvp, de);
1013         }
1014
1015         /* If the name has changed, we need to make it effective by changing
1016          * it in the directory entry. */
1017         if (newname != NULL) {
1018                 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1019
1020                 free(de->td_name, M_TMPFSNAME);
1021                 de->td_namelen = (uint16_t)tcnp->cn_namelen;
1022                 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1023                 de->td_name = newname;
1024
1025                 fnode->tn_status |= TMPFS_NODE_CHANGED;
1026                 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1027         }
1028
1029         /* If we are overwriting an entry, we have to remove the old one
1030          * from the target directory. */
1031         if (tvp != NULL) {
1032                 /* Remove the old entry from the target directory. */
1033                 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1034                 tmpfs_dir_detach(tdvp, de);
1035
1036                 /* Free the directory entry we just deleted.  Note that the
1037                  * node referred by it will not be removed until the vnode is
1038                  * really reclaimed. */
1039                 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1040         }
1041
1042         error = 0;
1043
1044 out_locked:
1045         if (fdnode != tdnode)
1046                 VOP_UNLOCK(fdvp, 0, tcnp->cn_thread);
1047
1048 out:
1049         /* Release target nodes. */
1050         /* XXX: I don't understand when tdvp can be the same as tvp, but
1051          * other code takes care of this... */
1052         if (tdvp == tvp)
1053                 vrele(tdvp);
1054         else
1055                 vput(tdvp);
1056         if (tvp != NULL)
1057                 vput(tvp);
1058
1059         /* Release source nodes. */
1060         vrele(fdvp);
1061         vrele(fvp);
1062
1063         return error;
1064 }
1065
1066 /* --------------------------------------------------------------------- */
1067
1068 static int
1069 tmpfs_mkdir(struct vop_mkdir_args *v)
1070 {
1071         struct vnode *dvp = v->a_dvp;
1072         struct vnode **vpp = v->a_vpp;
1073         struct componentname *cnp = v->a_cnp;
1074         struct vattr *vap = v->a_vap;
1075
1076         MPASS(vap->va_type == VDIR);
1077
1078         return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1079 }
1080
1081 /* --------------------------------------------------------------------- */
1082
1083 static int
1084 tmpfs_rmdir(struct vop_rmdir_args *v)
1085 {
1086         struct vnode *dvp = v->a_dvp;
1087         struct vnode *vp = v->a_vp;
1088
1089         int error;
1090         struct tmpfs_dirent *de;
1091         struct tmpfs_mount *tmp;
1092         struct tmpfs_node *dnode;
1093         struct tmpfs_node *node;
1094
1095         MPASS(VOP_ISLOCKED(dvp, v->a_cnp->cn_thread));
1096         MPASS(VOP_ISLOCKED(vp, v->a_cnp->cn_thread));
1097
1098         tmp = VFS_TO_TMPFS(dvp->v_mount);
1099         dnode = VP_TO_TMPFS_DIR(dvp);
1100         node = VP_TO_TMPFS_DIR(vp);
1101
1102         /* Directories with more than two entries ('.' and '..') cannot be
1103          * removed. */
1104          if (node->tn_size > 0) {
1105                  error = ENOTEMPTY;
1106                  goto out;
1107          }
1108
1109         if ((dnode->tn_flags & APPEND)
1110             || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1111                 error = EPERM;
1112                 goto out;
1113         }
1114
1115         /* This invariant holds only if we are not trying to remove "..".
1116           * We checked for that above so this is safe now. */
1117         MPASS(node->tn_dir.tn_parent == dnode);
1118
1119         /* Get the directory entry associated with node (vp).  This was
1120          * filled by tmpfs_lookup while looking up the entry. */
1121         de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1122         MPASS(TMPFS_DIRENT_MATCHES(de,
1123             v->a_cnp->cn_nameptr,
1124             v->a_cnp->cn_namelen));
1125
1126         /* Check flags to see if we are allowed to remove the directory. */
1127         if (dnode->tn_flags & APPEND
1128                 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1129                 error = EPERM;
1130                 goto out;
1131         }
1132
1133         /* Detach the directory entry from the directory (dnode). */
1134         tmpfs_dir_detach(dvp, de);
1135
1136         node->tn_links--;
1137         node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1138             TMPFS_NODE_MODIFIED;
1139         node->tn_dir.tn_parent->tn_links--;
1140         node->tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1141             TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1142
1143         cache_purge(dvp);
1144         cache_purge(vp);
1145
1146         /* Free the directory entry we just deleted.  Note that the node
1147          * referred by it will not be removed until the vnode is really
1148          * reclaimed. */
1149         tmpfs_free_dirent(tmp, de, TRUE);
1150
1151         /* Release the deleted vnode (will destroy the node, notify
1152          * interested parties and clean it from the cache). */
1153
1154         dnode->tn_status |= TMPFS_NODE_CHANGED;
1155         tmpfs_update(dvp);
1156
1157         error = 0;
1158
1159 out:
1160         return error;
1161 }
1162
1163 /* --------------------------------------------------------------------- */
1164
1165 static int
1166 tmpfs_symlink(struct vop_symlink_args *v)
1167 {
1168         struct vnode *dvp = v->a_dvp;
1169         struct vnode **vpp = v->a_vpp;
1170         struct componentname *cnp = v->a_cnp;
1171         struct vattr *vap = v->a_vap;
1172         char *target = v->a_target;
1173
1174 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1175         MPASS(vap->va_type == VLNK);
1176 #else
1177         vap->va_type = VLNK;
1178 #endif
1179
1180         return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1181 }
1182
1183 /* --------------------------------------------------------------------- */
1184
1185 static int
1186 tmpfs_readdir(struct vop_readdir_args *v)
1187 {
1188         struct vnode *vp = v->a_vp;
1189         struct uio *uio = v->a_uio;
1190         int *eofflag = v->a_eofflag;
1191         u_long **cookies = v->a_cookies;
1192         int *ncookies = v->a_ncookies;
1193
1194         int error;
1195         off_t startoff;
1196         off_t cnt = 0;
1197         struct tmpfs_node *node;
1198
1199         /* This operation only makes sense on directory nodes. */
1200         if (vp->v_type != VDIR)
1201                 return ENOTDIR;
1202
1203         node = VP_TO_TMPFS_DIR(vp);
1204
1205         startoff = uio->uio_offset;
1206
1207         if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1208                 error = tmpfs_dir_getdotdent(node, uio);
1209                 if (error != 0)
1210                         goto outok;
1211                 cnt++;
1212         }
1213
1214         if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1215                 error = tmpfs_dir_getdotdotdent(node, uio);
1216                 if (error != 0)
1217                         goto outok;
1218                 cnt++;
1219         }
1220
1221         error = tmpfs_dir_getdents(node, uio, &cnt);
1222
1223 outok:
1224         MPASS(error >= -1);
1225
1226         if (error == -1)
1227                 error = 0;
1228
1229         if (eofflag != NULL)
1230                 *eofflag =
1231                     (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1232
1233         /* Update NFS-related variables. */
1234         if (error == 0 && cookies != NULL && ncookies != NULL) {
1235                 off_t i;
1236                 off_t off = startoff;
1237                 struct tmpfs_dirent *de = NULL;
1238
1239                 *ncookies = cnt;
1240                 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1241
1242                 for (i = 0; i < cnt; i++) {
1243                         MPASS(off != TMPFS_DIRCOOKIE_EOF);
1244                         if (off == TMPFS_DIRCOOKIE_DOT) {
1245                                 off = TMPFS_DIRCOOKIE_DOTDOT;
1246                         } else {
1247                                 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1248                                         de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1249                                 } else if (de != NULL) {
1250                                         de = TAILQ_NEXT(de, td_entries);
1251                                 } else {
1252                                         de = tmpfs_dir_lookupbycookie(node,
1253                                             off);
1254                                         MPASS(de != NULL);
1255                                         de = TAILQ_NEXT(de, td_entries);
1256                                 }
1257                                 if (de == NULL)
1258                                         off = TMPFS_DIRCOOKIE_EOF;
1259                                 else
1260                                         off = tmpfs_dircookie(de);
1261                         }
1262
1263                         (*cookies)[i] = off;
1264                 }
1265                 MPASS(uio->uio_offset == off);
1266         }
1267
1268         return error;
1269 }
1270
1271 /* --------------------------------------------------------------------- */
1272
1273 static int
1274 tmpfs_readlink(struct vop_readlink_args *v)
1275 {
1276         struct vnode *vp = v->a_vp;
1277         struct uio *uio = v->a_uio;
1278
1279         int error;
1280         struct tmpfs_node *node;
1281
1282         MPASS(uio->uio_offset == 0);
1283         MPASS(vp->v_type == VLNK);
1284
1285         node = VP_TO_TMPFS_NODE(vp);
1286
1287         error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1288             uio);
1289         node->tn_status |= TMPFS_NODE_ACCESSED;
1290
1291         return error;
1292 }
1293
1294 /* --------------------------------------------------------------------- */
1295
1296 static int
1297 tmpfs_inactive(struct vop_inactive_args *v)
1298 {
1299         struct vnode *vp = v->a_vp;
1300         struct thread *l = v->a_td;
1301
1302         struct tmpfs_node *node;
1303
1304         MPASS(VOP_ISLOCKED(vp, l));
1305
1306         node = VP_TO_TMPFS_NODE(vp);
1307
1308         if (node->tn_links == 0)
1309                 vrecycle(vp, l);
1310
1311         return 0;
1312 }
1313
1314 /* --------------------------------------------------------------------- */
1315
1316 int
1317 tmpfs_reclaim(struct vop_reclaim_args *v)
1318 {
1319         struct vnode *vp = v->a_vp;
1320
1321         struct tmpfs_mount *tmp;
1322         struct tmpfs_node *node;
1323
1324         node = VP_TO_TMPFS_NODE(vp);
1325         tmp = VFS_TO_TMPFS(vp->v_mount);
1326
1327         vnode_destroy_vobject(vp);
1328         cache_purge(vp);
1329         tmpfs_free_vp(vp);
1330
1331         /* If the node referenced by this vnode was deleted by the user,
1332          * we must free its associated data structures (now that the vnode
1333          * is being reclaimed). */
1334         if (node->tn_links == 0)
1335                 tmpfs_free_node(tmp, node);
1336
1337         MPASS(vp->v_data == NULL);
1338         return 0;
1339 }
1340
1341 /* --------------------------------------------------------------------- */
1342
1343 static int
1344 tmpfs_print(struct vop_print_args *v)
1345 {
1346         struct vnode *vp = v->a_vp;
1347
1348         struct tmpfs_node *node;
1349
1350         node = VP_TO_TMPFS_NODE(vp);
1351
1352         printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1353             node, node->tn_flags, node->tn_links);
1354         printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1355             ", status 0x%x\n",
1356             node->tn_mode, node->tn_uid, node->tn_gid,
1357             (uintmax_t)node->tn_size, node->tn_status);
1358
1359         if (vp->v_type == VFIFO)
1360                 fifo_printinfo(vp);
1361
1362         printf("\n");
1363
1364         return 0;
1365 }
1366
1367 /* --------------------------------------------------------------------- */
1368
1369 static int
1370 tmpfs_pathconf(struct vop_pathconf_args *v)
1371 {
1372         int name = v->a_name;
1373         register_t *retval = v->a_retval;
1374
1375         int error;
1376
1377         error = 0;
1378
1379         switch (name) {
1380         case _PC_LINK_MAX:
1381                 *retval = LINK_MAX;
1382                 break;
1383
1384         case _PC_NAME_MAX:
1385                 *retval = NAME_MAX;
1386                 break;
1387
1388         case _PC_PATH_MAX:
1389                 *retval = PATH_MAX;
1390                 break;
1391
1392         case _PC_PIPE_BUF:
1393                 *retval = PIPE_BUF;
1394                 break;
1395
1396         case _PC_CHOWN_RESTRICTED:
1397                 *retval = 1;
1398                 break;
1399
1400         case _PC_NO_TRUNC:
1401                 *retval = 1;
1402                 break;
1403
1404         case _PC_SYNC_IO:
1405                 *retval = 1;
1406                 break;
1407
1408         case _PC_FILESIZEBITS:
1409                 *retval = 0; /* XXX Don't know which value should I return. */
1410                 break;
1411
1412         default:
1413                 error = EINVAL;
1414         }
1415
1416         return error;
1417 }
1418
1419 static int
1420 tmpfs_vptofh(struct vop_vptofh_args *ap)
1421 {
1422         struct tmpfs_fid *tfhp;
1423         struct tmpfs_node *node;
1424
1425         tfhp = (struct tmpfs_fid *)ap->a_fhp;
1426         node = VP_TO_TMPFS_NODE(ap->a_vp);
1427
1428         tfhp->tf_len = sizeof(struct tmpfs_fid);
1429         tfhp->tf_id = node->tn_id;
1430         tfhp->tf_gen = node->tn_gen;
1431
1432         return (0);
1433 }
1434
1435 /* --------------------------------------------------------------------- */
1436
1437 /*
1438  * vnode operations vector used for files stored in a tmpfs file system.
1439  */
1440 struct vop_vector tmpfs_vnodeop_entries = {
1441         .vop_default =                  &default_vnodeops,
1442         .vop_lookup =                   vfs_cache_lookup,
1443         .vop_cachedlookup =             tmpfs_lookup,
1444         .vop_create =                   tmpfs_create,
1445         .vop_mknod =                    tmpfs_mknod,
1446         .vop_open =                     tmpfs_open,
1447         .vop_close =                    tmpfs_close,
1448         .vop_access =                   tmpfs_access,
1449         .vop_getattr =                  tmpfs_getattr,
1450         .vop_setattr =                  tmpfs_setattr,
1451         .vop_read =                     tmpfs_read,
1452         .vop_write =                    tmpfs_write,
1453         .vop_fsync =                    tmpfs_fsync,
1454         .vop_remove =                   tmpfs_remove,
1455         .vop_link =                     tmpfs_link,
1456         .vop_rename =                   tmpfs_rename,
1457         .vop_mkdir =                    tmpfs_mkdir,
1458         .vop_rmdir =                    tmpfs_rmdir,
1459         .vop_symlink =                  tmpfs_symlink,
1460         .vop_readdir =                  tmpfs_readdir,
1461         .vop_readlink =                 tmpfs_readlink,
1462         .vop_inactive =                 tmpfs_inactive,
1463         .vop_reclaim =                  tmpfs_reclaim,
1464         .vop_print =                    tmpfs_print,
1465         .vop_pathconf =                 tmpfs_pathconf,
1466         .vop_vptofh =                   tmpfs_vptofh,
1467         .vop_bmap =                     VOP_EOPNOTSUPP,
1468 };
1469