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