tmpfs_vnops.c revision 1.91 1 1.91 christos /* $NetBSD: tmpfs_vnops.c,v 1.91 2011/09/27 01:10:43 christos Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.45 ad * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * This code is derived from software contributed to The NetBSD Foundation
8 1.12 jmmv * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 1.12 jmmv * 2005 program.
10 1.1 jmmv *
11 1.1 jmmv * Redistribution and use in source and binary forms, with or without
12 1.1 jmmv * modification, are permitted provided that the following conditions
13 1.1 jmmv * are met:
14 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
15 1.1 jmmv * notice, this list of conditions and the following disclaimer.
16 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
18 1.1 jmmv * documentation and/or other materials provided with the distribution.
19 1.1 jmmv *
20 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
31 1.1 jmmv */
32 1.1 jmmv
33 1.1 jmmv /*
34 1.1 jmmv * tmpfs vnode interface.
35 1.1 jmmv */
36 1.1 jmmv
37 1.1 jmmv #include <sys/cdefs.h>
38 1.91 christos __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.91 2011/09/27 01:10:43 christos Exp $");
39 1.1 jmmv
40 1.1 jmmv #include <sys/param.h>
41 1.1 jmmv #include <sys/dirent.h>
42 1.1 jmmv #include <sys/fcntl.h>
43 1.1 jmmv #include <sys/event.h>
44 1.1 jmmv #include <sys/malloc.h>
45 1.1 jmmv #include <sys/namei.h>
46 1.1 jmmv #include <sys/stat.h>
47 1.1 jmmv #include <sys/uio.h>
48 1.1 jmmv #include <sys/unistd.h>
49 1.1 jmmv #include <sys/vnode.h>
50 1.15 jmmv #include <sys/lockf.h>
51 1.24 christos #include <sys/kauth.h>
52 1.1 jmmv
53 1.1 jmmv #include <uvm/uvm.h>
54 1.1 jmmv
55 1.1 jmmv #include <miscfs/fifofs/fifo.h>
56 1.60 elad #include <miscfs/genfs/genfs.h>
57 1.1 jmmv #include <fs/tmpfs/tmpfs_vnops.h>
58 1.1 jmmv #include <fs/tmpfs/tmpfs.h>
59 1.1 jmmv
60 1.1 jmmv /*
61 1.2 jmmv * vnode operations vector used for files stored in a tmpfs file system.
62 1.1 jmmv */
63 1.1 jmmv int (**tmpfs_vnodeop_p)(void *);
64 1.1 jmmv const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
65 1.1 jmmv { &vop_default_desc, vn_default_error },
66 1.1 jmmv { &vop_lookup_desc, tmpfs_lookup },
67 1.1 jmmv { &vop_create_desc, tmpfs_create },
68 1.1 jmmv { &vop_mknod_desc, tmpfs_mknod },
69 1.1 jmmv { &vop_open_desc, tmpfs_open },
70 1.1 jmmv { &vop_close_desc, tmpfs_close },
71 1.1 jmmv { &vop_access_desc, tmpfs_access },
72 1.1 jmmv { &vop_getattr_desc, tmpfs_getattr },
73 1.1 jmmv { &vop_setattr_desc, tmpfs_setattr },
74 1.1 jmmv { &vop_read_desc, tmpfs_read },
75 1.1 jmmv { &vop_write_desc, tmpfs_write },
76 1.1 jmmv { &vop_ioctl_desc, tmpfs_ioctl },
77 1.1 jmmv { &vop_fcntl_desc, tmpfs_fcntl },
78 1.1 jmmv { &vop_poll_desc, tmpfs_poll },
79 1.1 jmmv { &vop_kqfilter_desc, tmpfs_kqfilter },
80 1.1 jmmv { &vop_revoke_desc, tmpfs_revoke },
81 1.1 jmmv { &vop_mmap_desc, tmpfs_mmap },
82 1.1 jmmv { &vop_fsync_desc, tmpfs_fsync },
83 1.1 jmmv { &vop_seek_desc, tmpfs_seek },
84 1.1 jmmv { &vop_remove_desc, tmpfs_remove },
85 1.1 jmmv { &vop_link_desc, tmpfs_link },
86 1.1 jmmv { &vop_rename_desc, tmpfs_rename },
87 1.1 jmmv { &vop_mkdir_desc, tmpfs_mkdir },
88 1.1 jmmv { &vop_rmdir_desc, tmpfs_rmdir },
89 1.1 jmmv { &vop_symlink_desc, tmpfs_symlink },
90 1.1 jmmv { &vop_readdir_desc, tmpfs_readdir },
91 1.1 jmmv { &vop_readlink_desc, tmpfs_readlink },
92 1.1 jmmv { &vop_abortop_desc, tmpfs_abortop },
93 1.1 jmmv { &vop_inactive_desc, tmpfs_inactive },
94 1.1 jmmv { &vop_reclaim_desc, tmpfs_reclaim },
95 1.1 jmmv { &vop_lock_desc, tmpfs_lock },
96 1.1 jmmv { &vop_unlock_desc, tmpfs_unlock },
97 1.1 jmmv { &vop_bmap_desc, tmpfs_bmap },
98 1.1 jmmv { &vop_strategy_desc, tmpfs_strategy },
99 1.1 jmmv { &vop_print_desc, tmpfs_print },
100 1.1 jmmv { &vop_pathconf_desc, tmpfs_pathconf },
101 1.1 jmmv { &vop_islocked_desc, tmpfs_islocked },
102 1.1 jmmv { &vop_advlock_desc, tmpfs_advlock },
103 1.1 jmmv { &vop_bwrite_desc, tmpfs_bwrite },
104 1.1 jmmv { &vop_getpages_desc, tmpfs_getpages },
105 1.1 jmmv { &vop_putpages_desc, tmpfs_putpages },
106 1.76 pooka { &vop_whiteout_desc, tmpfs_whiteout },
107 1.1 jmmv { NULL, NULL }
108 1.1 jmmv };
109 1.83 rmind
110 1.83 rmind const struct vnodeopv_desc tmpfs_vnodeop_opv_desc = {
111 1.83 rmind &tmpfs_vnodeop_p, tmpfs_vnodeop_entries
112 1.83 rmind };
113 1.1 jmmv
114 1.72 rmind /*
115 1.82 rmind * tmpfs_lookup: path name traversal routine.
116 1.72 rmind *
117 1.72 rmind * Arguments: dvp (directory being searched), vpp (result),
118 1.72 rmind * cnp (component name - path).
119 1.72 rmind *
120 1.72 rmind * => Caller holds a reference and lock on dvp.
121 1.72 rmind * => We return looked-up vnode (vpp) locked, with a reference held.
122 1.72 rmind */
123 1.1 jmmv int
124 1.1 jmmv tmpfs_lookup(void *v)
125 1.1 jmmv {
126 1.72 rmind struct vop_lookup_args /* {
127 1.72 rmind struct vnode *a_dvp;
128 1.72 rmind struct vnode **a_vpp;
129 1.72 rmind struct componentname *a_cnp;
130 1.72 rmind } */ *ap = v;
131 1.83 rmind vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
132 1.72 rmind struct componentname *cnp = ap->a_cnp;
133 1.84 rmind const bool lastcn = (cnp->cn_flags & ISLASTCN) != 0;
134 1.84 rmind tmpfs_node_t *dnode, *tnode;
135 1.83 rmind tmpfs_dirent_t *de;
136 1.72 rmind int error;
137 1.1 jmmv
138 1.1 jmmv KASSERT(VOP_ISLOCKED(dvp));
139 1.1 jmmv
140 1.1 jmmv dnode = VP_TO_TMPFS_DIR(dvp);
141 1.1 jmmv *vpp = NULL;
142 1.1 jmmv
143 1.85 rmind /* Check accessibility of directory. */
144 1.44 pooka error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
145 1.83 rmind if (error) {
146 1.1 jmmv goto out;
147 1.83 rmind }
148 1.85 rmind
149 1.72 rmind /*
150 1.72 rmind * If requesting the last path component on a read-only file system
151 1.72 rmind * with a write operation, deny it.
152 1.72 rmind */
153 1.84 rmind if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 &&
154 1.1 jmmv (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
155 1.1 jmmv error = EROFS;
156 1.1 jmmv goto out;
157 1.1 jmmv }
158 1.1 jmmv
159 1.72 rmind /*
160 1.72 rmind * Avoid doing a linear scan of the directory if the requested
161 1.72 rmind * directory/name couple is already in the cache.
162 1.72 rmind */
163 1.1 jmmv error = cache_lookup(dvp, vpp, cnp);
164 1.82 rmind if (error >= 0) {
165 1.82 rmind /* Both cache-hit or an error case. */
166 1.1 jmmv goto out;
167 1.82 rmind }
168 1.1 jmmv
169 1.1 jmmv if (cnp->cn_flags & ISDOTDOT) {
170 1.83 rmind tmpfs_node_t *pnode;
171 1.85 rmind
172 1.82 rmind /*
173 1.82 rmind * Lookup of ".." case.
174 1.82 rmind */
175 1.85 rmind if (lastcn && cnp->cn_nameiop == RENAME) {
176 1.85 rmind error = EINVAL;
177 1.85 rmind goto out;
178 1.85 rmind }
179 1.85 rmind KASSERT(dnode->tn_type == VDIR);
180 1.82 rmind pnode = dnode->tn_spec.tn_dir.tn_parent;
181 1.85 rmind if (pnode == NULL) {
182 1.85 rmind error = ENOENT;
183 1.85 rmind goto out;
184 1.85 rmind }
185 1.85 rmind
186 1.85 rmind /*
187 1.85 rmind * Lock the parent tn_vlock before releasing the vnode lock,
188 1.85 rmind * and thus prevents parent from disappearing.
189 1.85 rmind */
190 1.85 rmind mutex_enter(&pnode->tn_vlock);
191 1.71 hannken VOP_UNLOCK(dvp);
192 1.1 jmmv
193 1.85 rmind /*
194 1.85 rmind * Get a vnode of the '..' entry and re-acquire the lock.
195 1.85 rmind * Release the tn_vlock.
196 1.85 rmind */
197 1.85 rmind error = tmpfs_vnode_get(dvp->v_mount, pnode, vpp);
198 1.33 chs vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
199 1.82 rmind goto out;
200 1.72 rmind
201 1.1 jmmv } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
202 1.82 rmind /*
203 1.82 rmind * Lookup of "." case.
204 1.82 rmind */
205 1.84 rmind if (lastcn && cnp->cn_nameiop == RENAME) {
206 1.73 pooka error = EISDIR;
207 1.73 pooka goto out;
208 1.73 pooka }
209 1.66 pooka vref(dvp);
210 1.1 jmmv *vpp = dvp;
211 1.1 jmmv error = 0;
212 1.72 rmind goto done;
213 1.72 rmind }
214 1.1 jmmv
215 1.82 rmind /*
216 1.82 rmind * Other lookup cases: perform directory scan.
217 1.82 rmind */
218 1.72 rmind de = tmpfs_dir_lookup(dnode, cnp);
219 1.76 pooka if (de == NULL || de->td_node == TMPFS_NODE_WHITEOUT) {
220 1.72 rmind /*
221 1.72 rmind * The entry was not found in the directory. This is valid
222 1.72 rmind * if we are creating or renaming an entry and are working
223 1.72 rmind * on the last component of the path name.
224 1.72 rmind */
225 1.84 rmind if (lastcn && (cnp->cn_nameiop == CREATE ||
226 1.72 rmind cnp->cn_nameiop == RENAME)) {
227 1.72 rmind error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
228 1.72 rmind if (error) {
229 1.1 jmmv goto out;
230 1.1 jmmv }
231 1.72 rmind error = EJUSTRETURN;
232 1.72 rmind } else {
233 1.72 rmind error = ENOENT;
234 1.72 rmind }
235 1.76 pooka if (de) {
236 1.76 pooka KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
237 1.76 pooka cnp->cn_flags |= ISWHITEOUT;
238 1.76 pooka }
239 1.84 rmind goto done;
240 1.84 rmind }
241 1.1 jmmv
242 1.84 rmind tnode = de->td_node;
243 1.62 elad
244 1.84 rmind /*
245 1.84 rmind * If it is not the last path component and found a non-directory
246 1.84 rmind * or non-link entry (which may itself be pointing to a directory),
247 1.84 rmind * raise an error.
248 1.84 rmind */
249 1.84 rmind if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
250 1.84 rmind error = ENOTDIR;
251 1.84 rmind goto out;
252 1.84 rmind }
253 1.72 rmind
254 1.84 rmind /* Check the permissions. */
255 1.84 rmind if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
256 1.84 rmind kauth_action_t action = 0;
257 1.84 rmind
258 1.84 rmind /* This is the file-system's decision. */
259 1.84 rmind if ((dnode->tn_mode & S_ISTXT) != 0 &&
260 1.84 rmind kauth_cred_geteuid(cnp->cn_cred) != dnode->tn_uid &&
261 1.84 rmind kauth_cred_geteuid(cnp->cn_cred) != tnode->tn_uid) {
262 1.84 rmind error = EPERM;
263 1.84 rmind } else {
264 1.84 rmind error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
265 1.84 rmind }
266 1.62 elad
267 1.84 rmind if (cnp->cn_nameiop == DELETE) {
268 1.84 rmind action |= KAUTH_VNODE_DELETE;
269 1.84 rmind } else {
270 1.84 rmind KASSERT(cnp->cn_nameiop == RENAME);
271 1.84 rmind action |= KAUTH_VNODE_RENAME;
272 1.84 rmind }
273 1.84 rmind error = kauth_authorize_vnode(cnp->cn_cred,
274 1.84 rmind action, *vpp, dvp, error);
275 1.84 rmind if (error) {
276 1.84 rmind goto out;
277 1.1 jmmv }
278 1.1 jmmv }
279 1.84 rmind
280 1.85 rmind /* Get a vnode for the matching entry. */
281 1.85 rmind mutex_enter(&tnode->tn_vlock);
282 1.85 rmind error = tmpfs_vnode_get(dvp->v_mount, tnode, vpp);
283 1.72 rmind done:
284 1.72 rmind /*
285 1.82 rmind * Cache the result, unless request was for creation (as it does
286 1.82 rmind * not improve the performance).
287 1.72 rmind */
288 1.82 rmind if ((cnp->cn_flags & MAKEENTRY) != 0 && cnp->cn_nameiop != CREATE) {
289 1.1 jmmv cache_enter(dvp, *vpp, cnp);
290 1.82 rmind }
291 1.1 jmmv out:
292 1.83 rmind KASSERT((*vpp && VOP_ISLOCKED(*vpp)) || error);
293 1.33 chs KASSERT(VOP_ISLOCKED(dvp));
294 1.76 pooka
295 1.1 jmmv return error;
296 1.1 jmmv }
297 1.1 jmmv
298 1.1 jmmv int
299 1.1 jmmv tmpfs_create(void *v)
300 1.1 jmmv {
301 1.83 rmind struct vop_create_args /* {
302 1.83 rmind struct vnode *a_dvp;
303 1.83 rmind struct vnode **a_vpp;
304 1.83 rmind struct componentname *a_cnp;
305 1.83 rmind struct vattr *a_vap;
306 1.83 rmind } */ *ap = v;
307 1.83 rmind vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
308 1.83 rmind struct componentname *cnp = ap->a_cnp;
309 1.83 rmind struct vattr *vap = ap->a_vap;
310 1.1 jmmv
311 1.83 rmind KASSERT(VOP_ISLOCKED(dvp));
312 1.1 jmmv KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
313 1.1 jmmv return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
314 1.1 jmmv }
315 1.1 jmmv
316 1.1 jmmv int
317 1.1 jmmv tmpfs_mknod(void *v)
318 1.1 jmmv {
319 1.83 rmind struct vop_mknod_args /* {
320 1.83 rmind struct vnode *a_dvp;
321 1.83 rmind struct vnode **a_vpp;
322 1.83 rmind struct componentname *a_cnp;
323 1.83 rmind struct vattr *a_vap;
324 1.83 rmind } */ *ap = v;
325 1.83 rmind vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
326 1.83 rmind struct componentname *cnp = ap->a_cnp;
327 1.83 rmind struct vattr *vap = ap->a_vap;
328 1.83 rmind enum vtype vt = vap->va_type;
329 1.1 jmmv
330 1.83 rmind if (vt != VBLK && vt != VCHR && vt != VFIFO) {
331 1.54 pooka vput(dvp);
332 1.1 jmmv return EINVAL;
333 1.54 pooka }
334 1.1 jmmv return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
335 1.1 jmmv }
336 1.1 jmmv
337 1.1 jmmv int
338 1.1 jmmv tmpfs_open(void *v)
339 1.1 jmmv {
340 1.83 rmind struct vop_open_args /* {
341 1.83 rmind struct vnode *a_vp;
342 1.83 rmind int a_mode;
343 1.83 rmind kauth_cred_t a_cred;
344 1.83 rmind } */ *ap = v;
345 1.83 rmind vnode_t *vp = ap->a_vp;
346 1.83 rmind mode_t mode = ap->a_mode;
347 1.83 rmind tmpfs_node_t *node;
348 1.1 jmmv
349 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
350 1.1 jmmv
351 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
352 1.32 jmmv if (node->tn_links < 1) {
353 1.83 rmind /*
354 1.83 rmind * The file is still active, but all its names have been
355 1.83 rmind * removed (e.g. by a "rmdir $(pwd)"). It cannot be opened
356 1.83 rmind * any more, as it is about to be destroyed.
357 1.83 rmind */
358 1.83 rmind return ENOENT;
359 1.32 jmmv }
360 1.32 jmmv
361 1.1 jmmv /* If the file is marked append-only, deny write requests. */
362 1.83 rmind if ((node->tn_flags & APPEND) != 0 &&
363 1.83 rmind (mode & (FWRITE | O_APPEND)) == FWRITE) {
364 1.83 rmind return EPERM;
365 1.83 rmind }
366 1.83 rmind return 0;
367 1.1 jmmv }
368 1.1 jmmv
369 1.1 jmmv int
370 1.1 jmmv tmpfs_close(void *v)
371 1.1 jmmv {
372 1.83 rmind struct vop_close_args /* {
373 1.83 rmind struct vnode *a_vp;
374 1.83 rmind int a_fflag;
375 1.83 rmind kauth_cred_t a_cred;
376 1.83 rmind } */ *ap = v;
377 1.83 rmind vnode_t *vp = ap->a_vp;
378 1.1 jmmv
379 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
380 1.1 jmmv
381 1.85 rmind tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
382 1.17 yamt return 0;
383 1.1 jmmv }
384 1.1 jmmv
385 1.61 elad static int
386 1.83 rmind tmpfs_check_possible(vnode_t *vp, tmpfs_node_t *node, mode_t mode)
387 1.1 jmmv {
388 1.83 rmind const bool writing = (mode & VWRITE) != 0;
389 1.1 jmmv
390 1.1 jmmv switch (vp->v_type) {
391 1.1 jmmv case VDIR:
392 1.1 jmmv case VLNK:
393 1.1 jmmv case VREG:
394 1.83 rmind if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
395 1.83 rmind return EROFS;
396 1.1 jmmv }
397 1.1 jmmv break;
398 1.1 jmmv case VBLK:
399 1.1 jmmv case VCHR:
400 1.1 jmmv case VSOCK:
401 1.1 jmmv case VFIFO:
402 1.1 jmmv break;
403 1.1 jmmv default:
404 1.83 rmind return EINVAL;
405 1.1 jmmv }
406 1.83 rmind return (writing && (node->tn_flags & IMMUTABLE) != 0) ? EPERM : 0;
407 1.61 elad }
408 1.61 elad
409 1.61 elad static int
410 1.83 rmind tmpfs_check_permitted(vnode_t *vp, tmpfs_node_t *node, mode_t mode,
411 1.61 elad kauth_cred_t cred)
412 1.61 elad {
413 1.61 elad
414 1.61 elad return genfs_can_access(vp->v_type, node->tn_mode, node->tn_uid,
415 1.1 jmmv node->tn_gid, mode, cred);
416 1.61 elad }
417 1.61 elad
418 1.61 elad int
419 1.61 elad tmpfs_access(void *v)
420 1.61 elad {
421 1.83 rmind struct vop_access_args /* {
422 1.83 rmind struct vnode *a_vp;
423 1.83 rmind int a_mode;
424 1.83 rmind kauth_cred_t a_cred;
425 1.83 rmind } */ *ap = v;
426 1.83 rmind vnode_t *vp = ap->a_vp;
427 1.83 rmind mode_t mode = ap->a_mode;
428 1.83 rmind kauth_cred_t cred = ap->a_cred;
429 1.83 rmind tmpfs_node_t *node;
430 1.61 elad int error;
431 1.61 elad
432 1.61 elad KASSERT(VOP_ISLOCKED(vp));
433 1.61 elad
434 1.61 elad node = VP_TO_TMPFS_NODE(vp);
435 1.61 elad error = tmpfs_check_possible(vp, node, mode);
436 1.83 rmind if (error) {
437 1.83 rmind return error;
438 1.83 rmind }
439 1.83 rmind return kauth_authorize_vnode(cred, kauth_mode_to_action(mode), vp,
440 1.83 rmind NULL, tmpfs_check_permitted(vp, node, mode, cred));
441 1.1 jmmv }
442 1.1 jmmv
443 1.1 jmmv int
444 1.1 jmmv tmpfs_getattr(void *v)
445 1.1 jmmv {
446 1.83 rmind struct vop_getattr_args /* {
447 1.83 rmind struct vnode *a_vp;
448 1.83 rmind struct vattr *a_vap;
449 1.83 rmind kauth_cred_t a_cred;
450 1.83 rmind } */ *ap = v;
451 1.83 rmind vnode_t *vp = ap->a_vp;
452 1.83 rmind struct vattr *vap = ap->a_vap;
453 1.83 rmind tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
454 1.1 jmmv
455 1.66 pooka vattr_null(vap);
456 1.1 jmmv
457 1.86 rmind tmpfs_update(vp, NULL, NULL, NULL, 0);
458 1.86 rmind
459 1.1 jmmv vap->va_type = vp->v_type;
460 1.1 jmmv vap->va_mode = node->tn_mode;
461 1.1 jmmv vap->va_nlink = node->tn_links;
462 1.1 jmmv vap->va_uid = node->tn_uid;
463 1.1 jmmv vap->va_gid = node->tn_gid;
464 1.1 jmmv vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
465 1.1 jmmv vap->va_fileid = node->tn_id;
466 1.1 jmmv vap->va_size = node->tn_size;
467 1.1 jmmv vap->va_blocksize = PAGE_SIZE;
468 1.1 jmmv vap->va_atime = node->tn_atime;
469 1.1 jmmv vap->va_mtime = node->tn_mtime;
470 1.1 jmmv vap->va_ctime = node->tn_ctime;
471 1.1 jmmv vap->va_birthtime = node->tn_birthtime;
472 1.85 rmind vap->va_gen = TMPFS_NODE_GEN(node);
473 1.1 jmmv vap->va_flags = node->tn_flags;
474 1.1 jmmv vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
475 1.83 rmind node->tn_spec.tn_dev.tn_rdev : VNOVAL;
476 1.1 jmmv vap->va_bytes = round_page(node->tn_size);
477 1.1 jmmv vap->va_filerev = VNOVAL;
478 1.1 jmmv vap->va_vaflags = 0;
479 1.1 jmmv vap->va_spare = VNOVAL; /* XXX */
480 1.1 jmmv
481 1.1 jmmv return 0;
482 1.1 jmmv }
483 1.1 jmmv
484 1.51 christos #define GOODTIME(tv) ((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
485 1.1 jmmv /* XXX Should this operation be atomic? I think it should, but code in
486 1.1 jmmv * XXX other places (e.g., ufs) doesn't seem to be... */
487 1.1 jmmv int
488 1.1 jmmv tmpfs_setattr(void *v)
489 1.1 jmmv {
490 1.83 rmind struct vop_setattr_args /* {
491 1.83 rmind struct vnode *a_vp;
492 1.83 rmind struct vattr *a_vap;
493 1.83 rmind kauth_cred_t a_cred;
494 1.83 rmind } */ *ap = v;
495 1.83 rmind vnode_t *vp = ap->a_vp;
496 1.83 rmind struct vattr *vap = ap->a_vap;
497 1.83 rmind kauth_cred_t cred = ap->a_cred;
498 1.83 rmind lwp_t *l = curlwp;
499 1.83 rmind int error = 0;
500 1.1 jmmv
501 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
502 1.1 jmmv
503 1.1 jmmv /* Abort if any unsettable attribute is given. */
504 1.83 rmind if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
505 1.83 rmind vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
506 1.83 rmind vap->va_blocksize != VNOVAL || GOODTIME(&vap->va_ctime) ||
507 1.83 rmind vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
508 1.83 rmind vap->va_bytes != VNOVAL) {
509 1.83 rmind return EINVAL;
510 1.83 rmind }
511 1.1 jmmv if (error == 0 && (vap->va_flags != VNOVAL))
512 1.25 ad error = tmpfs_chflags(vp, vap->va_flags, cred, l);
513 1.1 jmmv
514 1.1 jmmv if (error == 0 && (vap->va_size != VNOVAL))
515 1.25 ad error = tmpfs_chsize(vp, vap->va_size, cred, l);
516 1.1 jmmv
517 1.1 jmmv if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
518 1.25 ad error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
519 1.1 jmmv
520 1.1 jmmv if (error == 0 && (vap->va_mode != VNOVAL))
521 1.25 ad error = tmpfs_chmod(vp, vap->va_mode, cred, l);
522 1.1 jmmv
523 1.51 christos if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
524 1.83 rmind || GOODTIME(&vap->va_birthtime))) {
525 1.83 rmind error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
526 1.83 rmind &vap->va_birthtime, vap->va_vaflags, cred, l);
527 1.83 rmind if (error == 0)
528 1.51 christos return 0;
529 1.83 rmind }
530 1.51 christos tmpfs_update(vp, NULL, NULL, NULL, 0);
531 1.1 jmmv return error;
532 1.1 jmmv }
533 1.1 jmmv
534 1.1 jmmv int
535 1.1 jmmv tmpfs_read(void *v)
536 1.1 jmmv {
537 1.83 rmind struct vop_read_args /* {
538 1.83 rmind struct vnode *a_vp;
539 1.83 rmind struct uio *a_uio;
540 1.83 rmind int a_ioflag;
541 1.83 rmind kauth_cred_t a_cred;
542 1.83 rmind } */ *ap = v;
543 1.83 rmind vnode_t *vp = ap->a_vp;
544 1.83 rmind struct uio *uio = ap->a_uio;
545 1.83 rmind const int ioflag = ap->a_ioflag;
546 1.83 rmind tmpfs_node_t *node;
547 1.83 rmind struct uvm_object *uobj;
548 1.7 jmmv int error;
549 1.1 jmmv
550 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
551 1.1 jmmv
552 1.5 yamt if (vp->v_type != VREG) {
553 1.83 rmind return EISDIR;
554 1.5 yamt }
555 1.5 yamt if (uio->uio_offset < 0) {
556 1.83 rmind return EINVAL;
557 1.1 jmmv }
558 1.1 jmmv
559 1.83 rmind node = VP_TO_TMPFS_NODE(vp);
560 1.1 jmmv node->tn_status |= TMPFS_NODE_ACCESSED;
561 1.21 jmmv uobj = node->tn_spec.tn_reg.tn_aobj;
562 1.6 yamt error = 0;
563 1.83 rmind
564 1.7 jmmv while (error == 0 && uio->uio_resid > 0) {
565 1.6 yamt vsize_t len;
566 1.6 yamt
567 1.83 rmind if (node->tn_size <= uio->uio_offset) {
568 1.8 yamt break;
569 1.83 rmind }
570 1.6 yamt len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
571 1.83 rmind if (len == 0) {
572 1.6 yamt break;
573 1.83 rmind }
574 1.52 pooka error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
575 1.52 pooka UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
576 1.1 jmmv }
577 1.1 jmmv return error;
578 1.1 jmmv }
579 1.1 jmmv
580 1.1 jmmv int
581 1.1 jmmv tmpfs_write(void *v)
582 1.1 jmmv {
583 1.83 rmind struct vop_write_args /* {
584 1.83 rmind struct vnode *a_vp;
585 1.83 rmind struct uio *a_uio;
586 1.83 rmind int a_ioflag;
587 1.83 rmind kauth_cred_t a_cred;
588 1.83 rmind } */ *ap = v;
589 1.83 rmind vnode_t *vp = ap->a_vp;
590 1.83 rmind struct uio *uio = ap->a_uio;
591 1.83 rmind const int ioflag = ap->a_ioflag;
592 1.83 rmind tmpfs_node_t *node;
593 1.83 rmind struct uvm_object *uobj;
594 1.83 rmind off_t oldsize;
595 1.36 thorpej bool extended;
596 1.1 jmmv int error;
597 1.1 jmmv
598 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
599 1.1 jmmv
600 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
601 1.1 jmmv oldsize = node->tn_size;
602 1.1 jmmv
603 1.1 jmmv if (uio->uio_offset < 0 || vp->v_type != VREG) {
604 1.1 jmmv error = EINVAL;
605 1.1 jmmv goto out;
606 1.1 jmmv }
607 1.1 jmmv if (uio->uio_resid == 0) {
608 1.1 jmmv error = 0;
609 1.1 jmmv goto out;
610 1.1 jmmv }
611 1.83 rmind if (ioflag & IO_APPEND) {
612 1.1 jmmv uio->uio_offset = node->tn_size;
613 1.83 rmind }
614 1.1 jmmv
615 1.1 jmmv extended = uio->uio_offset + uio->uio_resid > node->tn_size;
616 1.1 jmmv if (extended) {
617 1.1 jmmv error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
618 1.83 rmind if (error)
619 1.1 jmmv goto out;
620 1.1 jmmv }
621 1.1 jmmv
622 1.21 jmmv uobj = node->tn_spec.tn_reg.tn_aobj;
623 1.6 yamt error = 0;
624 1.7 jmmv while (error == 0 && uio->uio_resid > 0) {
625 1.6 yamt vsize_t len;
626 1.6 yamt
627 1.6 yamt len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
628 1.83 rmind if (len == 0) {
629 1.6 yamt break;
630 1.83 rmind }
631 1.52 pooka error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
632 1.52 pooka UBC_WRITE | UBC_UNMAP_FLAG(vp));
633 1.1 jmmv }
634 1.83 rmind if (error) {
635 1.83 rmind (void)tmpfs_reg_resize(vp, oldsize);
636 1.83 rmind }
637 1.6 yamt
638 1.1 jmmv node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
639 1.1 jmmv (extended ? TMPFS_NODE_CHANGED : 0);
640 1.31 jmmv VN_KNOTE(vp, NOTE_WRITE);
641 1.1 jmmv out:
642 1.83 rmind if (error) {
643 1.83 rmind KASSERT(oldsize == node->tn_size);
644 1.83 rmind } else {
645 1.83 rmind KASSERT(uio->uio_resid == 0);
646 1.83 rmind }
647 1.1 jmmv return error;
648 1.1 jmmv }
649 1.1 jmmv
650 1.1 jmmv int
651 1.1 jmmv tmpfs_fsync(void *v)
652 1.1 jmmv {
653 1.83 rmind struct vop_fsync_args /* {
654 1.83 rmind struct vnode *a_vp;
655 1.83 rmind kauth_cred_t a_cred;
656 1.83 rmind int a_flags;
657 1.83 rmind off_t a_offlo;
658 1.83 rmind off_t a_offhi;
659 1.83 rmind struct lwp *a_l;
660 1.83 rmind } */ *ap = v;
661 1.83 rmind vnode_t *vp = ap->a_vp;
662 1.1 jmmv
663 1.83 rmind /* Nothing to do. Just update. */
664 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
665 1.51 christos tmpfs_update(vp, NULL, NULL, NULL, 0);
666 1.17 yamt return 0;
667 1.1 jmmv }
668 1.1 jmmv
669 1.83 rmind /*
670 1.83 rmind * tmpfs_remove: unlink a file.
671 1.83 rmind *
672 1.83 rmind * => Both directory (dvp) and file (vp) are locked.
673 1.83 rmind * => We unlock and drop the reference on both.
674 1.83 rmind */
675 1.1 jmmv int
676 1.1 jmmv tmpfs_remove(void *v)
677 1.1 jmmv {
678 1.82 rmind struct vop_remove_args /* {
679 1.82 rmind struct vnode *a_dvp;
680 1.82 rmind struct vnode *a_vp;
681 1.82 rmind struct componentname *a_cnp;
682 1.82 rmind } */ *ap = v;
683 1.83 rmind vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp;
684 1.85 rmind tmpfs_node_t *node;
685 1.83 rmind tmpfs_dirent_t *de;
686 1.82 rmind int error;
687 1.1 jmmv
688 1.1 jmmv KASSERT(VOP_ISLOCKED(dvp));
689 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
690 1.1 jmmv
691 1.34 pooka if (vp->v_type == VDIR) {
692 1.34 pooka error = EPERM;
693 1.34 pooka goto out;
694 1.34 pooka }
695 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
696 1.1 jmmv
697 1.1 jmmv /* Files marked as immutable or append-only cannot be deleted. */
698 1.1 jmmv if (node->tn_flags & (IMMUTABLE | APPEND)) {
699 1.1 jmmv error = EPERM;
700 1.1 jmmv goto out;
701 1.1 jmmv }
702 1.1 jmmv
703 1.85 rmind /* Lookup the directory entry (check the cached hint first). */
704 1.85 rmind de = tmpfs_dir_cached(node);
705 1.85 rmind if (de == NULL) {
706 1.85 rmind tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
707 1.85 rmind struct componentname *cnp = ap->a_cnp;
708 1.85 rmind de = tmpfs_dir_lookup(dnode, cnp);
709 1.85 rmind }
710 1.82 rmind KASSERT(de && de->td_node == node);
711 1.1 jmmv
712 1.82 rmind /*
713 1.85 rmind * Remove the entry from the directory (drops the link count) and
714 1.90 hannken * destroy it or replace it with a whiteout.
715 1.90 hannken * Note: the inode referred by it will not be destroyed
716 1.85 rmind * until the vnode is reclaimed/recycled.
717 1.82 rmind */
718 1.85 rmind tmpfs_dir_detach(dvp, de);
719 1.90 hannken if (ap->a_cnp->cn_flags & DOWHITEOUT)
720 1.90 hannken tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
721 1.90 hannken else
722 1.90 hannken tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
723 1.1 jmmv error = 0;
724 1.1 jmmv out:
725 1.83 rmind /* Drop the references and unlock the vnodes. */
726 1.1 jmmv vput(vp);
727 1.83 rmind if (dvp == vp) {
728 1.34 pooka vrele(dvp);
729 1.83 rmind } else {
730 1.34 pooka vput(dvp);
731 1.83 rmind }
732 1.1 jmmv return error;
733 1.1 jmmv }
734 1.1 jmmv
735 1.77 rmind /*
736 1.83 rmind * tmpfs_link: create a hard link.
737 1.77 rmind */
738 1.1 jmmv int
739 1.1 jmmv tmpfs_link(void *v)
740 1.1 jmmv {
741 1.77 rmind struct vop_link_args /* {
742 1.77 rmind struct vnode *a_dvp;
743 1.77 rmind struct vnode *a_vp;
744 1.77 rmind struct componentname *a_cnp;
745 1.77 rmind } */ *ap = v;
746 1.83 rmind vnode_t *dvp = ap->a_dvp;
747 1.83 rmind vnode_t *vp = ap->a_vp;
748 1.82 rmind struct componentname *cnp = ap->a_cnp;
749 1.83 rmind tmpfs_node_t *dnode, *node;
750 1.83 rmind tmpfs_dirent_t *de;
751 1.1 jmmv int error;
752 1.1 jmmv
753 1.77 rmind KASSERT(dvp != vp);
754 1.1 jmmv KASSERT(VOP_ISLOCKED(dvp));
755 1.77 rmind KASSERT(vp->v_type != VDIR);
756 1.77 rmind KASSERT(dvp->v_mount == vp->v_mount);
757 1.1 jmmv
758 1.1 jmmv dnode = VP_TO_TMPFS_DIR(dvp);
759 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
760 1.1 jmmv
761 1.63 rmind vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
762 1.1 jmmv
763 1.84 rmind /* Check for maximum number of links limit. */
764 1.1 jmmv if (node->tn_links == LINK_MAX) {
765 1.1 jmmv error = EMLINK;
766 1.1 jmmv goto out;
767 1.1 jmmv }
768 1.85 rmind KASSERT(node->tn_links < LINK_MAX);
769 1.1 jmmv
770 1.1 jmmv /* We cannot create links of files marked immutable or append-only. */
771 1.1 jmmv if (node->tn_flags & (IMMUTABLE | APPEND)) {
772 1.1 jmmv error = EPERM;
773 1.1 jmmv goto out;
774 1.1 jmmv }
775 1.1 jmmv
776 1.85 rmind /* Allocate a new directory entry to represent the inode. */
777 1.85 rmind error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
778 1.1 jmmv cnp->cn_nameptr, cnp->cn_namelen, &de);
779 1.83 rmind if (error) {
780 1.1 jmmv goto out;
781 1.83 rmind }
782 1.1 jmmv
783 1.85 rmind /*
784 1.85 rmind * Insert the entry into the directory.
785 1.85 rmind * It will increase the inode link count.
786 1.85 rmind */
787 1.85 rmind tmpfs_dir_attach(dvp, de, node);
788 1.1 jmmv
789 1.85 rmind /* Update the timestamps and trigger the event. */
790 1.85 rmind if (node->tn_vnode) {
791 1.85 rmind VN_KNOTE(node->tn_vnode, NOTE_LINK);
792 1.85 rmind }
793 1.1 jmmv node->tn_status |= TMPFS_NODE_CHANGED;
794 1.51 christos tmpfs_update(vp, NULL, NULL, NULL, 0);
795 1.1 jmmv error = 0;
796 1.1 jmmv out:
797 1.71 hannken VOP_UNLOCK(vp);
798 1.1 jmmv vput(dvp);
799 1.1 jmmv return error;
800 1.1 jmmv }
801 1.1 jmmv
802 1.63 rmind /*
803 1.89 riastrad * tmpfs_rename: rename routine, the hairiest system call, with the
804 1.89 riastrad * insane API.
805 1.63 rmind *
806 1.63 rmind * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent)
807 1.63 rmind * and tvp (to-leaf), if exists (NULL if not).
808 1.63 rmind *
809 1.63 rmind * => Caller holds a reference on fdvp and fvp, they are unlocked.
810 1.63 rmind * Note: fdvp and fvp can refer to the same object (i.e. when it is root).
811 1.63 rmind *
812 1.63 rmind * => Both tdvp and tvp are referenced and locked. It is our responsibility
813 1.63 rmind * to release the references and unlock them (or destroy).
814 1.63 rmind */
815 1.89 riastrad
816 1.89 riastrad /*
817 1.89 riastrad * First, some forward declarations of subroutines.
818 1.89 riastrad */
819 1.89 riastrad
820 1.89 riastrad static int tmpfs_sane_rename(struct vnode *, struct componentname *,
821 1.89 riastrad struct vnode *, struct componentname *, kauth_cred_t, bool);
822 1.89 riastrad static int tmpfs_rename_enter(struct mount *, struct tmpfs_mount *,
823 1.89 riastrad kauth_cred_t,
824 1.89 riastrad struct vnode *, struct tmpfs_node *, struct componentname *,
825 1.89 riastrad struct tmpfs_dirent **, struct vnode **,
826 1.89 riastrad struct vnode *, struct tmpfs_node *, struct componentname *,
827 1.89 riastrad struct tmpfs_dirent **, struct vnode **);
828 1.89 riastrad static int tmpfs_rename_enter_common(struct mount *, struct tmpfs_mount *,
829 1.89 riastrad kauth_cred_t,
830 1.89 riastrad struct vnode *, struct tmpfs_node *,
831 1.89 riastrad struct componentname *, struct tmpfs_dirent **, struct vnode **,
832 1.89 riastrad struct componentname *, struct tmpfs_dirent **, struct vnode **);
833 1.89 riastrad static int tmpfs_rename_enter_separate(struct mount *, struct tmpfs_mount *,
834 1.89 riastrad kauth_cred_t,
835 1.89 riastrad struct vnode *, struct tmpfs_node *, struct componentname *,
836 1.89 riastrad struct tmpfs_dirent **, struct vnode **,
837 1.89 riastrad struct vnode *, struct tmpfs_node *, struct componentname *,
838 1.89 riastrad struct tmpfs_dirent **, struct vnode **);
839 1.89 riastrad static void tmpfs_rename_exit(struct tmpfs_mount *,
840 1.89 riastrad struct vnode *, struct vnode *, struct vnode *, struct vnode *);
841 1.89 riastrad static int tmpfs_rename_lock_directory(struct vnode *, struct tmpfs_node *);
842 1.89 riastrad static int tmpfs_rename_genealogy(struct tmpfs_node *, struct tmpfs_node *,
843 1.89 riastrad struct tmpfs_node **);
844 1.89 riastrad static int tmpfs_rename_lock(struct mount *, kauth_cred_t, int,
845 1.89 riastrad struct vnode *, struct tmpfs_node *, struct componentname *, bool,
846 1.89 riastrad struct tmpfs_dirent **, struct vnode **,
847 1.89 riastrad struct vnode *, struct tmpfs_node *, struct componentname *, bool,
848 1.89 riastrad struct tmpfs_dirent **, struct vnode **);
849 1.89 riastrad static void tmpfs_rename_attachdetach(struct tmpfs_mount *,
850 1.89 riastrad struct vnode *, struct tmpfs_dirent *, struct vnode *,
851 1.89 riastrad struct vnode *, struct tmpfs_dirent *, struct vnode *);
852 1.89 riastrad static int tmpfs_do_remove(struct tmpfs_mount *, struct vnode *,
853 1.89 riastrad struct tmpfs_node *, struct tmpfs_dirent *, struct vnode *, kauth_cred_t);
854 1.89 riastrad static int tmpfs_rename_check_possible(struct tmpfs_node *,
855 1.89 riastrad struct tmpfs_node *, struct tmpfs_node *, struct tmpfs_node *);
856 1.89 riastrad static int tmpfs_rename_check_permitted(kauth_cred_t,
857 1.89 riastrad struct tmpfs_node *, struct tmpfs_node *,
858 1.89 riastrad struct tmpfs_node *, struct tmpfs_node *);
859 1.89 riastrad static int tmpfs_remove_check_possible(struct tmpfs_node *,
860 1.89 riastrad struct tmpfs_node *);
861 1.89 riastrad static int tmpfs_remove_check_permitted(kauth_cred_t,
862 1.89 riastrad struct tmpfs_node *, struct tmpfs_node *);
863 1.89 riastrad static int tmpfs_check_sticky(kauth_cred_t,
864 1.89 riastrad struct tmpfs_node *, struct tmpfs_node *);
865 1.89 riastrad
866 1.1 jmmv int
867 1.1 jmmv tmpfs_rename(void *v)
868 1.1 jmmv {
869 1.83 rmind struct vop_rename_args /* {
870 1.83 rmind struct vnode *a_fdvp;
871 1.83 rmind struct vnode *a_fvp;
872 1.83 rmind struct componentname *a_fcnp;
873 1.83 rmind struct vnode *a_tdvp;
874 1.83 rmind struct vnode *a_tvp;
875 1.83 rmind struct componentname *a_tcnp;
876 1.83 rmind } */ *ap = v;
877 1.89 riastrad struct vnode *fdvp = ap->a_fdvp;
878 1.89 riastrad struct vnode *fvp = ap->a_fvp;
879 1.83 rmind struct componentname *fcnp = ap->a_fcnp;
880 1.89 riastrad struct vnode *tdvp = ap->a_tdvp;
881 1.89 riastrad struct vnode *tvp = ap->a_tvp;
882 1.83 rmind struct componentname *tcnp = ap->a_tcnp;
883 1.89 riastrad kauth_cred_t cred;
884 1.89 riastrad int error;
885 1.89 riastrad
886 1.89 riastrad KASSERT(fdvp != NULL);
887 1.89 riastrad KASSERT(fvp != NULL);
888 1.89 riastrad KASSERT(fcnp != NULL);
889 1.89 riastrad KASSERT(fcnp->cn_nameptr != NULL);
890 1.89 riastrad KASSERT(tdvp != NULL);
891 1.89 riastrad KASSERT(tcnp != NULL);
892 1.89 riastrad KASSERT(fcnp->cn_nameptr != NULL);
893 1.89 riastrad /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
894 1.89 riastrad /* KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
895 1.89 riastrad KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
896 1.89 riastrad KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
897 1.89 riastrad KASSERT(fdvp->v_type == VDIR);
898 1.89 riastrad KASSERT(tdvp->v_type == VDIR);
899 1.89 riastrad
900 1.89 riastrad cred = fcnp->cn_cred;
901 1.89 riastrad KASSERT(tcnp->cn_cred == cred);
902 1.89 riastrad
903 1.89 riastrad /*
904 1.89 riastrad * Sanitize our world from the VFS insanity. Unlock the target
905 1.89 riastrad * directory and node, which are locked. Release the children,
906 1.89 riastrad * which are referenced. Check for rename("x", "y/."), which
907 1.89 riastrad * it is our responsibility to reject, not the caller's. (But
908 1.89 riastrad * the caller does reject rename("x/.", "y"). Go figure.)
909 1.89 riastrad */
910 1.89 riastrad
911 1.89 riastrad VOP_UNLOCK(tdvp);
912 1.89 riastrad if ((tvp != NULL) && (tvp != tdvp))
913 1.89 riastrad VOP_UNLOCK(tvp);
914 1.89 riastrad
915 1.89 riastrad vrele(fvp);
916 1.89 riastrad if (tvp != NULL)
917 1.89 riastrad vrele(tvp);
918 1.89 riastrad
919 1.89 riastrad if (tvp == tdvp) {
920 1.89 riastrad error = EINVAL;
921 1.89 riastrad goto out;
922 1.89 riastrad }
923 1.89 riastrad
924 1.89 riastrad error = tmpfs_sane_rename(fdvp, fcnp, tdvp, tcnp, cred, false);
925 1.89 riastrad
926 1.89 riastrad out: /*
927 1.89 riastrad * All done, whether with success or failure. Release the
928 1.89 riastrad * directory nodes now, as the caller expects from the VFS
929 1.89 riastrad * protocol.
930 1.89 riastrad */
931 1.89 riastrad vrele(fdvp);
932 1.89 riastrad vrele(tdvp);
933 1.89 riastrad
934 1.89 riastrad return error;
935 1.89 riastrad }
936 1.89 riastrad
937 1.89 riastrad /*
938 1.89 riastrad * tmpfs_sane_rename: rename routine, the hairiest system call, with
939 1.89 riastrad * the sane API.
940 1.89 riastrad *
941 1.89 riastrad * Arguments:
942 1.89 riastrad *
943 1.89 riastrad * . fdvp (from directory vnode),
944 1.89 riastrad * . fcnp (from component name),
945 1.89 riastrad * . tdvp (to directory vnode), and
946 1.89 riastrad * . tcnp (to component name).
947 1.89 riastrad *
948 1.89 riastrad * fdvp and tdvp must be referenced and unlocked.
949 1.89 riastrad */
950 1.89 riastrad static int
951 1.89 riastrad tmpfs_sane_rename(struct vnode *fdvp, struct componentname *fcnp,
952 1.89 riastrad struct vnode *tdvp, struct componentname *tcnp, kauth_cred_t cred,
953 1.89 riastrad bool posixly_correct)
954 1.89 riastrad {
955 1.89 riastrad struct mount *mount;
956 1.89 riastrad struct tmpfs_mount *tmpfs;
957 1.89 riastrad struct tmpfs_node *fdnode, *tdnode;
958 1.89 riastrad struct tmpfs_dirent *fde, *tde;
959 1.89 riastrad struct vnode *fvp, *tvp;
960 1.1 jmmv char *newname;
961 1.1 jmmv int error;
962 1.1 jmmv
963 1.89 riastrad KASSERT(fdvp != NULL);
964 1.89 riastrad KASSERT(fcnp != NULL);
965 1.89 riastrad KASSERT(tdvp != NULL);
966 1.89 riastrad KASSERT(tcnp != NULL);
967 1.89 riastrad /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
968 1.89 riastrad /* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
969 1.89 riastrad KASSERT(fdvp->v_type == VDIR);
970 1.89 riastrad KASSERT(tdvp->v_type == VDIR);
971 1.89 riastrad KASSERT(fdvp->v_mount == tdvp->v_mount);
972 1.85 rmind KASSERT((fcnp->cn_flags & ISDOTDOT) == 0);
973 1.85 rmind KASSERT((tcnp->cn_flags & ISDOTDOT) == 0);
974 1.89 riastrad KASSERT((fcnp->cn_namelen != 1) || (fcnp->cn_nameptr[0] != '.'));
975 1.89 riastrad KASSERT((tcnp->cn_namelen != 1) || (tcnp->cn_nameptr[0] != '.'));
976 1.89 riastrad KASSERT((fcnp->cn_namelen != 2) || (fcnp->cn_nameptr[0] != '.') ||
977 1.89 riastrad (fcnp->cn_nameptr[1] != '.'));
978 1.89 riastrad KASSERT((tcnp->cn_namelen != 2) || (tcnp->cn_nameptr[0] != '.') ||
979 1.89 riastrad (tcnp->cn_nameptr[1] != '.'));
980 1.1 jmmv
981 1.89 riastrad /*
982 1.89 riastrad * Pull out the tmpfs data structures.
983 1.89 riastrad */
984 1.89 riastrad fdnode = VP_TO_TMPFS_NODE(fdvp);
985 1.89 riastrad tdnode = VP_TO_TMPFS_NODE(tdvp);
986 1.89 riastrad KASSERT(fdnode != NULL);
987 1.89 riastrad KASSERT(tdnode != NULL);
988 1.89 riastrad KASSERT(fdnode->tn_vnode == fdvp);
989 1.89 riastrad KASSERT(tdnode->tn_vnode == tdvp);
990 1.89 riastrad KASSERT(fdnode->tn_type == VDIR);
991 1.89 riastrad KASSERT(tdnode->tn_type == VDIR);
992 1.89 riastrad
993 1.89 riastrad mount = fdvp->v_mount;
994 1.89 riastrad KASSERT(mount != NULL);
995 1.89 riastrad KASSERT(mount == tdvp->v_mount);
996 1.89 riastrad /* XXX How can we be sure this stays true? (Not that you're
997 1.89 riastrad * likely to mount a tmpfs read-only...) */
998 1.89 riastrad KASSERT((mount->mnt_flag & MNT_RDONLY) == 0);
999 1.89 riastrad tmpfs = VFS_TO_TMPFS(mount);
1000 1.89 riastrad KASSERT(tmpfs != NULL);
1001 1.1 jmmv
1002 1.89 riastrad /*
1003 1.89 riastrad * Decide whether we need a new name, and allocate memory for
1004 1.89 riastrad * it if so. Do this before locking anything or taking
1005 1.89 riastrad * destructive actions so that we can back out safely and sleep
1006 1.89 riastrad * safely. XXX Is sleeping an issue here? Can this just be
1007 1.89 riastrad * moved into tmpfs_rename_attachdetach?
1008 1.89 riastrad */
1009 1.70 rmind if (tmpfs_strname_neqlen(fcnp, tcnp)) {
1010 1.89 riastrad newname = tmpfs_strname_alloc(tmpfs, tcnp->cn_namelen);
1011 1.70 rmind if (newname == NULL) {
1012 1.70 rmind error = ENOSPC;
1013 1.70 rmind goto out_unlocked;
1014 1.70 rmind }
1015 1.89 riastrad } else {
1016 1.89 riastrad newname = NULL;
1017 1.70 rmind }
1018 1.70 rmind
1019 1.89 riastrad /*
1020 1.89 riastrad * Lock and look up everything. GCC is not very clever.
1021 1.89 riastrad */
1022 1.89 riastrad fde = tde = NULL;
1023 1.89 riastrad fvp = tvp = NULL;
1024 1.89 riastrad error = tmpfs_rename_enter(mount, tmpfs, cred,
1025 1.89 riastrad fdvp, fdnode, fcnp, &fde, &fvp,
1026 1.89 riastrad tdvp, tdnode, tcnp, &tde, &tvp);
1027 1.89 riastrad if (error)
1028 1.89 riastrad goto out_unlocked;
1029 1.45 ad
1030 1.89 riastrad /*
1031 1.89 riastrad * Check that everything is locked and looks right.
1032 1.89 riastrad */
1033 1.89 riastrad KASSERT(fde != NULL);
1034 1.89 riastrad KASSERT(fvp != NULL);
1035 1.89 riastrad KASSERT(fde->td_node != NULL);
1036 1.89 riastrad KASSERT(fde->td_node->tn_vnode == fvp);
1037 1.89 riastrad KASSERT(fde->td_node->tn_type == fvp->v_type);
1038 1.89 riastrad KASSERT((tde == NULL) == (tvp == NULL));
1039 1.89 riastrad KASSERT((tde == NULL) || (tde->td_node != NULL));
1040 1.89 riastrad KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
1041 1.89 riastrad KASSERT((tde == NULL) || (tde->td_node->tn_type == tvp->v_type));
1042 1.89 riastrad KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
1043 1.89 riastrad KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1044 1.89 riastrad KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
1045 1.89 riastrad KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1046 1.1 jmmv
1047 1.85 rmind /*
1048 1.89 riastrad * If the source and destination are the same object, we need
1049 1.89 riastrad * only at most delete the source entry.
1050 1.85 rmind */
1051 1.1 jmmv if (fvp == tvp) {
1052 1.89 riastrad KASSERT(tvp != NULL);
1053 1.89 riastrad if (fde->td_node->tn_type == VDIR) {
1054 1.89 riastrad /* XXX How can this possibly happen? */
1055 1.85 rmind error = EINVAL;
1056 1.89 riastrad goto out_locked;
1057 1.89 riastrad }
1058 1.89 riastrad if (!posixly_correct && (fde != tde)) {
1059 1.89 riastrad /* XXX Doesn't work because of locking.
1060 1.89 riastrad * error = VOP_REMOVE(fdvp, fvp);
1061 1.89 riastrad */
1062 1.89 riastrad error = tmpfs_do_remove(tmpfs, fdvp, fdnode, fde, fvp,
1063 1.89 riastrad cred);
1064 1.89 riastrad if (error)
1065 1.89 riastrad goto out_locked;
1066 1.85 rmind }
1067 1.89 riastrad goto success;
1068 1.1 jmmv }
1069 1.89 riastrad KASSERT(fde != tde);
1070 1.89 riastrad KASSERT(fvp != tvp);
1071 1.1 jmmv
1072 1.89 riastrad /*
1073 1.89 riastrad * If the target exists, refuse to rename a directory over a
1074 1.89 riastrad * non-directory or vice versa, or to clobber a non-empty
1075 1.89 riastrad * directory.
1076 1.89 riastrad */
1077 1.39 jmmv if (tvp != NULL) {
1078 1.89 riastrad KASSERT(tde != NULL);
1079 1.89 riastrad KASSERT(tde->td_node != NULL);
1080 1.89 riastrad if (fvp->v_type == VDIR && tvp->v_type == VDIR)
1081 1.89 riastrad error = ((tde->td_node->tn_size > 0)? ENOTEMPTY : 0);
1082 1.89 riastrad else if (fvp->v_type == VDIR && tvp->v_type != VDIR)
1083 1.39 jmmv error = ENOTDIR;
1084 1.89 riastrad else if (fvp->v_type != VDIR && tvp->v_type == VDIR)
1085 1.39 jmmv error = EISDIR;
1086 1.89 riastrad else
1087 1.89 riastrad error = 0;
1088 1.89 riastrad if (error)
1089 1.89 riastrad goto out_locked;
1090 1.89 riastrad KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
1091 1.89 riastrad }
1092 1.89 riastrad
1093 1.89 riastrad /*
1094 1.89 riastrad * Authorize the rename.
1095 1.89 riastrad */
1096 1.89 riastrad error = tmpfs_rename_check_possible(fdnode, fde->td_node,
1097 1.89 riastrad tdnode, (tde? tde->td_node : NULL));
1098 1.89 riastrad if (error)
1099 1.89 riastrad goto out_locked;
1100 1.89 riastrad error = tmpfs_rename_check_permitted(cred, fdnode, fde->td_node,
1101 1.89 riastrad tdnode, (tde? tde->td_node : NULL));
1102 1.89 riastrad error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE, fvp, fdvp,
1103 1.89 riastrad error);
1104 1.89 riastrad error = kauth_authorize_vnode(cred, KAUTH_VNODE_RENAME, tvp, tdvp,
1105 1.89 riastrad error);
1106 1.89 riastrad if (error)
1107 1.89 riastrad goto out_locked;
1108 1.89 riastrad
1109 1.89 riastrad /*
1110 1.89 riastrad * Everything is hunky-dory. Shuffle the directory entries.
1111 1.89 riastrad */
1112 1.89 riastrad tmpfs_rename_attachdetach(tmpfs, fdvp, fde, fvp, tdvp, tde, tvp);
1113 1.89 riastrad
1114 1.89 riastrad /*
1115 1.89 riastrad * Update the directory entry's name necessary, and flag
1116 1.89 riastrad * metadata updates. A memory allocation failure here is not
1117 1.89 riastrad * OK because we've already committed some changes that we
1118 1.89 riastrad * can't back out at this point, and we have things locked so
1119 1.89 riastrad * we can't sleep, hence the early allocation above.
1120 1.89 riastrad */
1121 1.89 riastrad if (newname != NULL) {
1122 1.91 christos KASSERT(tcnp->cn_namelen <= TMPFS_MAXNAMLEN);
1123 1.89 riastrad
1124 1.89 riastrad tmpfs_strname_free(tmpfs, fde->td_name, fde->td_namelen);
1125 1.89 riastrad fde->td_namelen = (uint16_t)tcnp->cn_namelen;
1126 1.89 riastrad (void)memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1127 1.89 riastrad /* Commit newname and don't free it on the way out. */
1128 1.89 riastrad fde->td_name = newname;
1129 1.89 riastrad newname = NULL;
1130 1.89 riastrad
1131 1.89 riastrad fde->td_node->tn_status |= TMPFS_NODE_CHANGED;
1132 1.89 riastrad tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1133 1.89 riastrad }
1134 1.89 riastrad
1135 1.89 riastrad success:
1136 1.89 riastrad VN_KNOTE(fvp, NOTE_RENAME);
1137 1.89 riastrad error = 0;
1138 1.89 riastrad
1139 1.89 riastrad out_locked:
1140 1.89 riastrad tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
1141 1.89 riastrad
1142 1.89 riastrad out_unlocked:
1143 1.89 riastrad /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
1144 1.89 riastrad /* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
1145 1.89 riastrad /* KASSERT((fvp == NULL) || (VOP_ISLOCKED(fvp) != LK_EXCLUSIVE)); */
1146 1.89 riastrad /* KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
1147 1.89 riastrad
1148 1.89 riastrad if (newname != NULL)
1149 1.89 riastrad tmpfs_strname_free(tmpfs, newname, tcnp->cn_namelen);
1150 1.89 riastrad
1151 1.89 riastrad return error;
1152 1.89 riastrad }
1153 1.89 riastrad
1154 1.89 riastrad /*
1155 1.89 riastrad * Look up fcnp in fdnode/fdvp and store its directory entry in fde_ret
1156 1.89 riastrad * and the associated vnode in fvp_ret; fail if not found. Look up
1157 1.89 riastrad * tcnp in tdnode/tdvp and store its directory entry in tde_ret and the
1158 1.89 riastrad * associated vnode in tvp_ret; store null instead if not found. Fail
1159 1.89 riastrad * if anything has been mounted on any of the nodes involved.
1160 1.89 riastrad *
1161 1.89 riastrad * fdvp and tdvp must be referenced.
1162 1.89 riastrad *
1163 1.89 riastrad * On entry, nothing is locked.
1164 1.89 riastrad *
1165 1.89 riastrad * On success, everything is locked, and *fvp_ret, and *tvp_ret if
1166 1.89 riastrad * nonnull, are referenced. The only pairs of vnodes that may be
1167 1.89 riastrad * identical are {fdvp, tdvp} and {fvp, tvp}.
1168 1.89 riastrad *
1169 1.89 riastrad * On failure, everything remains as was.
1170 1.89 riastrad *
1171 1.89 riastrad * Locking everything including the source and target nodes is
1172 1.89 riastrad * necessary to make sure that, e.g., link count updates are OK. The
1173 1.89 riastrad * locking order is, in general, ancestor-first, matching the order you
1174 1.89 riastrad * need to use to look up a descendant anyway.
1175 1.89 riastrad */
1176 1.89 riastrad static int
1177 1.89 riastrad tmpfs_rename_enter(struct mount *mount, struct tmpfs_mount *tmpfs,
1178 1.89 riastrad kauth_cred_t cred,
1179 1.89 riastrad struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
1180 1.89 riastrad struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
1181 1.89 riastrad struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
1182 1.89 riastrad struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
1183 1.89 riastrad {
1184 1.89 riastrad int error;
1185 1.89 riastrad
1186 1.89 riastrad KASSERT(mount != NULL);
1187 1.89 riastrad KASSERT(tmpfs != NULL);
1188 1.89 riastrad KASSERT(fdvp != NULL);
1189 1.89 riastrad KASSERT(fdnode != NULL);
1190 1.89 riastrad KASSERT(fcnp != NULL);
1191 1.89 riastrad KASSERT(fde_ret != NULL);
1192 1.89 riastrad KASSERT(fvp_ret != NULL);
1193 1.89 riastrad KASSERT(tdvp != NULL);
1194 1.89 riastrad KASSERT(tdnode != NULL);
1195 1.89 riastrad KASSERT(tcnp != NULL);
1196 1.89 riastrad KASSERT(tde_ret != NULL);
1197 1.89 riastrad KASSERT(tvp_ret != NULL);
1198 1.89 riastrad KASSERT(fdnode->tn_vnode == fdvp);
1199 1.89 riastrad KASSERT(tdnode->tn_vnode == tdvp);
1200 1.89 riastrad KASSERT(fdnode->tn_type == VDIR);
1201 1.89 riastrad KASSERT(tdnode->tn_type == VDIR);
1202 1.89 riastrad
1203 1.89 riastrad if (fdvp == tdvp) {
1204 1.89 riastrad KASSERT(fdnode == tdnode);
1205 1.89 riastrad error = tmpfs_rename_enter_common(mount, tmpfs, cred, fdvp,
1206 1.89 riastrad fdnode, fcnp, fde_ret, fvp_ret, tcnp, tde_ret, tvp_ret);
1207 1.89 riastrad } else {
1208 1.89 riastrad KASSERT(fdnode != tdnode);
1209 1.89 riastrad error = tmpfs_rename_enter_separate(mount, tmpfs, cred,
1210 1.89 riastrad fdvp, fdnode, fcnp, fde_ret, fvp_ret,
1211 1.89 riastrad tdvp, tdnode, tcnp, tde_ret, tvp_ret);
1212 1.89 riastrad }
1213 1.89 riastrad
1214 1.89 riastrad if (error)
1215 1.89 riastrad return error;
1216 1.89 riastrad
1217 1.89 riastrad KASSERT(*fde_ret != NULL);
1218 1.89 riastrad KASSERT(*fvp_ret != NULL);
1219 1.89 riastrad KASSERT((*tde_ret == NULL) == (*tvp_ret == NULL));
1220 1.89 riastrad KASSERT((*tde_ret == NULL) || ((*tde_ret)->td_node != NULL));
1221 1.89 riastrad KASSERT((*tde_ret == NULL) ||
1222 1.89 riastrad ((*tde_ret)->td_node->tn_vnode == *tvp_ret));
1223 1.89 riastrad KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
1224 1.89 riastrad KASSERT(VOP_ISLOCKED(*fvp_ret) == LK_EXCLUSIVE);
1225 1.89 riastrad KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1226 1.89 riastrad KASSERT((*tvp_ret == NULL) ||
1227 1.89 riastrad (VOP_ISLOCKED(*tvp_ret) == LK_EXCLUSIVE));
1228 1.89 riastrad KASSERT(*fvp_ret != fdvp);
1229 1.89 riastrad KASSERT(*fvp_ret != tdvp);
1230 1.89 riastrad KASSERT(*tvp_ret != fdvp);
1231 1.89 riastrad KASSERT(*tvp_ret != tdvp);
1232 1.89 riastrad return 0;
1233 1.89 riastrad }
1234 1.89 riastrad
1235 1.89 riastrad /*
1236 1.89 riastrad * Lock and look up with a common source/target directory.
1237 1.89 riastrad */
1238 1.89 riastrad static int
1239 1.89 riastrad tmpfs_rename_enter_common(struct mount *mount, struct tmpfs_mount *tmpfs,
1240 1.89 riastrad kauth_cred_t cred,
1241 1.89 riastrad struct vnode *dvp, struct tmpfs_node *dnode,
1242 1.89 riastrad struct componentname *fcnp,
1243 1.89 riastrad struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
1244 1.89 riastrad struct componentname *tcnp,
1245 1.89 riastrad struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
1246 1.89 riastrad {
1247 1.89 riastrad struct tmpfs_dirent *fde, *tde;
1248 1.89 riastrad struct vnode *fvp, *tvp;
1249 1.89 riastrad int error;
1250 1.89 riastrad
1251 1.89 riastrad error = tmpfs_rename_lock_directory(dvp, dnode);
1252 1.89 riastrad if (error)
1253 1.89 riastrad goto fail0;
1254 1.89 riastrad
1255 1.89 riastrad /* Did we lose a race with mount? */
1256 1.89 riastrad if (dvp->v_mountedhere != NULL) {
1257 1.89 riastrad error = EBUSY;
1258 1.89 riastrad goto fail1;
1259 1.89 riastrad }
1260 1.89 riastrad
1261 1.89 riastrad /* Make sure the caller may read the directory. */
1262 1.89 riastrad error = VOP_ACCESS(dvp, VEXEC, cred);
1263 1.89 riastrad if (error)
1264 1.89 riastrad goto fail1;
1265 1.89 riastrad
1266 1.89 riastrad /*
1267 1.89 riastrad * The order in which we lock the source and target nodes is
1268 1.89 riastrad * irrelevant because there can only be one rename on this
1269 1.89 riastrad * directory in flight at a time, and we have it locked.
1270 1.89 riastrad */
1271 1.89 riastrad
1272 1.89 riastrad fde = tmpfs_dir_lookup(dnode, fcnp);
1273 1.89 riastrad if (fde == NULL) {
1274 1.89 riastrad error = ENOENT;
1275 1.89 riastrad goto fail1;
1276 1.89 riastrad }
1277 1.89 riastrad
1278 1.89 riastrad KASSERT(fde->td_node != NULL);
1279 1.89 riastrad /* We ruled out `.' earlier. */
1280 1.89 riastrad KASSERT(fde->td_node != dnode);
1281 1.89 riastrad /* We ruled out `..' earlier. */
1282 1.89 riastrad KASSERT(fde->td_node != dnode->tn_spec.tn_dir.tn_parent);
1283 1.89 riastrad mutex_enter(&fde->td_node->tn_vlock);
1284 1.89 riastrad error = tmpfs_vnode_get(mount, fde->td_node, &fvp);
1285 1.89 riastrad if (error)
1286 1.89 riastrad goto fail1;
1287 1.89 riastrad KASSERT(fvp != NULL);
1288 1.89 riastrad KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
1289 1.89 riastrad KASSERT(fvp != dvp);
1290 1.89 riastrad KASSERT(fvp->v_mount == mount);
1291 1.89 riastrad
1292 1.89 riastrad /* Refuse to rename a mount point. */
1293 1.89 riastrad if ((fvp->v_type == VDIR) && (fvp->v_mountedhere != NULL)) {
1294 1.89 riastrad error = EBUSY;
1295 1.89 riastrad goto fail2;
1296 1.89 riastrad }
1297 1.89 riastrad
1298 1.89 riastrad tde = tmpfs_dir_lookup(dnode, tcnp);
1299 1.89 riastrad if (tde == NULL) {
1300 1.89 riastrad tvp = NULL;
1301 1.89 riastrad } else {
1302 1.89 riastrad KASSERT(tde->td_node != NULL);
1303 1.89 riastrad /* We ruled out `.' earlier. */
1304 1.89 riastrad KASSERT(tde->td_node != dnode);
1305 1.89 riastrad /* We ruled out `..' earlier. */
1306 1.89 riastrad KASSERT(tde->td_node != dnode->tn_spec.tn_dir.tn_parent);
1307 1.89 riastrad if (tde->td_node != fde->td_node) {
1308 1.89 riastrad mutex_enter(&tde->td_node->tn_vlock);
1309 1.89 riastrad error = tmpfs_vnode_get(mount, tde->td_node, &tvp);
1310 1.89 riastrad if (error)
1311 1.89 riastrad goto fail2;
1312 1.89 riastrad KASSERT(tvp->v_mount == mount);
1313 1.89 riastrad /* Refuse to rename over a mount point. */
1314 1.89 riastrad if ((tvp->v_type == VDIR) &&
1315 1.89 riastrad (tvp->v_mountedhere != NULL)) {
1316 1.89 riastrad error = EBUSY;
1317 1.89 riastrad goto fail3;
1318 1.89 riastrad }
1319 1.39 jmmv } else {
1320 1.89 riastrad tvp = fvp;
1321 1.89 riastrad vref(tvp);
1322 1.39 jmmv }
1323 1.89 riastrad KASSERT(tvp != NULL);
1324 1.89 riastrad KASSERT(VOP_ISLOCKED(tvp) == LK_EXCLUSIVE);
1325 1.89 riastrad }
1326 1.89 riastrad KASSERT(tvp != dvp);
1327 1.89 riastrad
1328 1.89 riastrad *fde_ret = fde;
1329 1.89 riastrad *fvp_ret = fvp;
1330 1.89 riastrad *tde_ret = tde;
1331 1.89 riastrad *tvp_ret = tvp;
1332 1.89 riastrad return 0;
1333 1.89 riastrad
1334 1.89 riastrad fail3: if (tvp != NULL) {
1335 1.89 riastrad if (tvp != fvp)
1336 1.89 riastrad vput(tvp);
1337 1.89 riastrad else
1338 1.89 riastrad vrele(tvp);
1339 1.89 riastrad }
1340 1.89 riastrad
1341 1.89 riastrad fail2: vput(fvp);
1342 1.89 riastrad fail1: VOP_UNLOCK(dvp);
1343 1.89 riastrad fail0: return error;
1344 1.89 riastrad }
1345 1.89 riastrad
1346 1.89 riastrad /*
1347 1.89 riastrad * Lock and look up with separate source and target directories.
1348 1.89 riastrad */
1349 1.89 riastrad static int
1350 1.89 riastrad tmpfs_rename_enter_separate(struct mount *mount, struct tmpfs_mount *tmpfs,
1351 1.89 riastrad kauth_cred_t cred,
1352 1.89 riastrad struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
1353 1.89 riastrad struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
1354 1.89 riastrad struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
1355 1.89 riastrad struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
1356 1.89 riastrad {
1357 1.89 riastrad struct tmpfs_node *intermediate_node;
1358 1.89 riastrad struct tmpfs_dirent *fde, *tde;
1359 1.89 riastrad struct vnode *fvp, *tvp;
1360 1.89 riastrad int error;
1361 1.89 riastrad
1362 1.89 riastrad KASSERT(fdvp != tdvp);
1363 1.89 riastrad KASSERT(fdnode != tdnode);
1364 1.89 riastrad
1365 1.89 riastrad #if 0 /* XXX */
1366 1.89 riastrad mutex_enter(&tmpfs->tm_rename_lock);
1367 1.89 riastrad #endif
1368 1.89 riastrad
1369 1.89 riastrad error = tmpfs_rename_genealogy(fdnode, tdnode, &intermediate_node);
1370 1.89 riastrad if (error)
1371 1.89 riastrad goto fail;
1372 1.89 riastrad
1373 1.89 riastrad /*
1374 1.89 riastrad * intermediate_node == NULL means fdnode is not an ancestor of
1375 1.89 riastrad * tdnode.
1376 1.89 riastrad */
1377 1.89 riastrad if (intermediate_node == NULL)
1378 1.89 riastrad error = tmpfs_rename_lock(mount, cred, ENOTEMPTY,
1379 1.89 riastrad tdvp, tdnode, tcnp, true, &tde, &tvp,
1380 1.89 riastrad fdvp, fdnode, fcnp, false, &fde, &fvp);
1381 1.89 riastrad else
1382 1.89 riastrad error = tmpfs_rename_lock(mount, cred, EINVAL,
1383 1.89 riastrad fdvp, fdnode, fcnp, false, &fde, &fvp,
1384 1.89 riastrad tdvp, tdnode, tcnp, true, &tde, &tvp);
1385 1.89 riastrad if (error)
1386 1.89 riastrad goto fail;
1387 1.89 riastrad
1388 1.89 riastrad KASSERT(fde != NULL);
1389 1.89 riastrad KASSERT(fde->td_node != NULL);
1390 1.89 riastrad
1391 1.89 riastrad /*
1392 1.89 riastrad * Reject rename("foo/bar", "foo/bar/baz/quux/zot").
1393 1.89 riastrad */
1394 1.89 riastrad if (fde->td_node == intermediate_node) {
1395 1.89 riastrad tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
1396 1.89 riastrad return EINVAL;
1397 1.89 riastrad }
1398 1.89 riastrad
1399 1.89 riastrad *fde_ret = fde;
1400 1.89 riastrad *fvp_ret = fvp;
1401 1.89 riastrad *tde_ret = tde;
1402 1.89 riastrad *tvp_ret = tvp;
1403 1.89 riastrad return 0;
1404 1.89 riastrad
1405 1.89 riastrad fail:
1406 1.89 riastrad #if 0 /* XXX */
1407 1.89 riastrad mutex_exit(&tmpfs->tm_rename_lock);
1408 1.89 riastrad #endif
1409 1.89 riastrad return error;
1410 1.89 riastrad }
1411 1.89 riastrad
1412 1.89 riastrad /*
1413 1.89 riastrad * Unlock everything we locked for rename.
1414 1.89 riastrad *
1415 1.89 riastrad * fdvp and tdvp must be referenced.
1416 1.89 riastrad *
1417 1.89 riastrad * On entry, everything is locked, and fvp and tvp referenced.
1418 1.89 riastrad *
1419 1.89 riastrad * On exit, everything is unlocked, and fvp and tvp are released.
1420 1.89 riastrad */
1421 1.89 riastrad static void
1422 1.89 riastrad tmpfs_rename_exit(struct tmpfs_mount *tmpfs,
1423 1.89 riastrad struct vnode *fdvp, struct vnode *fvp,
1424 1.89 riastrad struct vnode *tdvp, struct vnode *tvp)
1425 1.89 riastrad {
1426 1.89 riastrad
1427 1.89 riastrad KASSERT(tmpfs != NULL);
1428 1.89 riastrad KASSERT(fdvp != NULL);
1429 1.89 riastrad KASSERT(fvp != NULL);
1430 1.89 riastrad KASSERT(fdvp != fvp);
1431 1.89 riastrad KASSERT(fdvp != tvp);
1432 1.89 riastrad KASSERT(tdvp != tvp);
1433 1.89 riastrad KASSERT(tdvp != fvp);
1434 1.89 riastrad KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
1435 1.89 riastrad KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1436 1.89 riastrad KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
1437 1.89 riastrad KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1438 1.89 riastrad
1439 1.89 riastrad if (tvp != NULL) {
1440 1.89 riastrad if (tvp != fvp)
1441 1.89 riastrad vput(tvp);
1442 1.89 riastrad else
1443 1.89 riastrad vrele(tvp);
1444 1.89 riastrad }
1445 1.89 riastrad VOP_UNLOCK(tdvp);
1446 1.89 riastrad vput(fvp);
1447 1.89 riastrad if (fdvp != tdvp)
1448 1.89 riastrad VOP_UNLOCK(fdvp);
1449 1.89 riastrad
1450 1.89 riastrad #if 0 /* XXX */
1451 1.89 riastrad if (fdvp != tdvp)
1452 1.89 riastrad mutex_exit(&tmpfs->tm_rename_lock);
1453 1.89 riastrad #endif
1454 1.89 riastrad }
1455 1.89 riastrad
1456 1.89 riastrad /*
1457 1.89 riastrad * Lock a directory, but fail if it has been rmdir'd.
1458 1.89 riastrad *
1459 1.89 riastrad * vp must be referenced.
1460 1.89 riastrad */
1461 1.89 riastrad static int
1462 1.89 riastrad tmpfs_rename_lock_directory(struct vnode *vp, struct tmpfs_node *node)
1463 1.89 riastrad {
1464 1.89 riastrad
1465 1.89 riastrad KASSERT(vp != NULL);
1466 1.89 riastrad KASSERT(node != NULL);
1467 1.89 riastrad KASSERT(node->tn_vnode == vp);
1468 1.89 riastrad KASSERT(node->tn_type == VDIR);
1469 1.89 riastrad
1470 1.89 riastrad vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1471 1.89 riastrad if (node->tn_spec.tn_dir.tn_parent == NULL) {
1472 1.89 riastrad VOP_UNLOCK(vp);
1473 1.89 riastrad return ENOENT;
1474 1.39 jmmv }
1475 1.39 jmmv
1476 1.89 riastrad return 0;
1477 1.89 riastrad }
1478 1.89 riastrad
1479 1.89 riastrad /*
1480 1.89 riastrad * Analyze the genealogy of the source and target nodes.
1481 1.89 riastrad *
1482 1.89 riastrad * On success, stores in *intermediate_node_ret either the child of
1483 1.89 riastrad * fdnode of which tdnode is a descendant, or null if tdnode is not a
1484 1.89 riastrad * descendant of fdnode at all.
1485 1.89 riastrad *
1486 1.89 riastrad * fdnode and tdnode must be unlocked and referenced. The file
1487 1.89 riastrad * system's rename lock must also be held, to exclude concurrent
1488 1.89 riastrad * changes to the file system's genealogy other than rmdir.
1489 1.89 riastrad *
1490 1.89 riastrad * XXX This causes an extra lock/unlock of tdnode in the case when
1491 1.89 riastrad * we're just about to lock it again before locking anything else.
1492 1.89 riastrad * However, changing that requires reorganizing the code to make it
1493 1.89 riastrad * even more horrifically obscure.
1494 1.89 riastrad */
1495 1.89 riastrad static int
1496 1.89 riastrad tmpfs_rename_genealogy(struct tmpfs_node *fdnode, struct tmpfs_node *tdnode,
1497 1.89 riastrad struct tmpfs_node **intermediate_node_ret)
1498 1.89 riastrad {
1499 1.89 riastrad struct tmpfs_node *node = tdnode, *parent;
1500 1.89 riastrad int error;
1501 1.89 riastrad
1502 1.89 riastrad KASSERT(fdnode != NULL);
1503 1.89 riastrad KASSERT(tdnode != NULL);
1504 1.89 riastrad KASSERT(fdnode != tdnode);
1505 1.89 riastrad KASSERT(intermediate_node_ret != NULL);
1506 1.89 riastrad
1507 1.89 riastrad KASSERT(fdnode->tn_vnode != NULL);
1508 1.89 riastrad KASSERT(tdnode->tn_vnode != NULL);
1509 1.89 riastrad KASSERT(fdnode->tn_type == VDIR);
1510 1.89 riastrad KASSERT(tdnode->tn_type == VDIR);
1511 1.89 riastrad
1512 1.89 riastrad /*
1513 1.89 riastrad * We need to provisionally lock tdnode->tn_vnode to keep rmdir
1514 1.89 riastrad * from deleting it -- or any ancestor -- at an inopportune
1515 1.89 riastrad * moment.
1516 1.89 riastrad */
1517 1.89 riastrad error = tmpfs_rename_lock_directory(tdnode->tn_vnode, tdnode);
1518 1.89 riastrad if (error)
1519 1.89 riastrad return error;
1520 1.89 riastrad
1521 1.89 riastrad for (;;) {
1522 1.89 riastrad parent = node->tn_spec.tn_dir.tn_parent;
1523 1.89 riastrad KASSERT(parent != NULL);
1524 1.89 riastrad KASSERT(parent->tn_type == VDIR);
1525 1.89 riastrad
1526 1.89 riastrad /* Did we hit the root without finding fdnode? */
1527 1.89 riastrad if (parent == node) {
1528 1.89 riastrad *intermediate_node_ret = NULL;
1529 1.89 riastrad break;
1530 1.1 jmmv }
1531 1.1 jmmv
1532 1.89 riastrad /* Did we find that fdnode is an ancestor? */
1533 1.89 riastrad if (parent == fdnode) {
1534 1.89 riastrad *intermediate_node_ret = node;
1535 1.89 riastrad break;
1536 1.89 riastrad }
1537 1.1 jmmv
1538 1.89 riastrad /* Neither -- keep ascending the family tree. */
1539 1.89 riastrad node = parent;
1540 1.1 jmmv }
1541 1.1 jmmv
1542 1.89 riastrad VOP_UNLOCK(tdnode->tn_vnode);
1543 1.89 riastrad return 0;
1544 1.89 riastrad }
1545 1.89 riastrad
1546 1.89 riastrad /*
1547 1.89 riastrad * Lock directories a and b, which must be distinct, and look up and
1548 1.89 riastrad * lock nodes a and b. Do a first and then b. Directory b may not be
1549 1.89 riastrad * an ancestor of directory a, although directory a may be an ancestor
1550 1.89 riastrad * of directory b. Fail with overlap_error if node a is directory b.
1551 1.89 riastrad * Neither componentname may be `.' or `..'.
1552 1.89 riastrad *
1553 1.89 riastrad * a_dvp and b_dvp must be referenced.
1554 1.89 riastrad *
1555 1.89 riastrad * On entry, a_dvp and b_dvp are unlocked.
1556 1.89 riastrad *
1557 1.89 riastrad * On success,
1558 1.89 riastrad * . a_dvp and b_dvp are locked,
1559 1.89 riastrad * . *a_dirent_ret is filled with a directory entry whose node is
1560 1.89 riastrad * locked and referenced,
1561 1.89 riastrad * . *b_vp_ret is filled with the corresponding vnode,
1562 1.89 riastrad * . *b_dirent_ret is filled either with null or with a directory entry
1563 1.89 riastrad * whose node is locked and referenced,
1564 1.89 riastrad * . *b_vp is filled either with null or with the corresponding vnode,
1565 1.89 riastrad * and
1566 1.89 riastrad * . the only pair of vnodes that may be identical is a_vp and b_vp.
1567 1.89 riastrad *
1568 1.89 riastrad * On failure, a_dvp and b_dvp are left unlocked, and *a_dirent_ret,
1569 1.89 riastrad * *a_vp, *b_dirent_ret, and *b_vp are left alone.
1570 1.89 riastrad */
1571 1.89 riastrad static int
1572 1.89 riastrad tmpfs_rename_lock(struct mount *mount, kauth_cred_t cred, int overlap_error,
1573 1.89 riastrad struct vnode *a_dvp, struct tmpfs_node *a_dnode,
1574 1.89 riastrad struct componentname *a_cnp, bool a_missing_ok,
1575 1.89 riastrad struct tmpfs_dirent **a_dirent_ret, struct vnode **a_vp_ret,
1576 1.89 riastrad struct vnode *b_dvp, struct tmpfs_node *b_dnode,
1577 1.89 riastrad struct componentname *b_cnp, bool b_missing_ok,
1578 1.89 riastrad struct tmpfs_dirent **b_dirent_ret, struct vnode **b_vp_ret)
1579 1.89 riastrad {
1580 1.89 riastrad struct tmpfs_dirent *a_dirent, *b_dirent;
1581 1.89 riastrad struct vnode *a_vp, *b_vp;
1582 1.89 riastrad int error;
1583 1.85 rmind
1584 1.89 riastrad KASSERT(a_dvp != NULL);
1585 1.89 riastrad KASSERT(a_dnode != NULL);
1586 1.89 riastrad KASSERT(a_cnp != NULL);
1587 1.89 riastrad KASSERT(a_dirent_ret != NULL);
1588 1.89 riastrad KASSERT(a_vp_ret != NULL);
1589 1.89 riastrad KASSERT(b_dvp != NULL);
1590 1.89 riastrad KASSERT(b_dnode != NULL);
1591 1.89 riastrad KASSERT(b_cnp != NULL);
1592 1.89 riastrad KASSERT(b_dirent_ret != NULL);
1593 1.89 riastrad KASSERT(b_vp_ret != NULL);
1594 1.89 riastrad KASSERT(a_dvp != b_dvp);
1595 1.89 riastrad KASSERT(a_dnode != b_dnode);
1596 1.89 riastrad KASSERT(a_dnode->tn_vnode == a_dvp);
1597 1.89 riastrad KASSERT(b_dnode->tn_vnode == b_dvp);
1598 1.89 riastrad KASSERT(a_dnode->tn_type == VDIR);
1599 1.89 riastrad KASSERT(b_dnode->tn_type == VDIR);
1600 1.89 riastrad KASSERT(a_missing_ok != b_missing_ok);
1601 1.89 riastrad
1602 1.89 riastrad error = tmpfs_rename_lock_directory(a_dvp, a_dnode);
1603 1.89 riastrad if (error)
1604 1.89 riastrad goto fail0;
1605 1.89 riastrad
1606 1.89 riastrad /* Did we lose a race with mount? */
1607 1.89 riastrad if (a_dvp->v_mountedhere != NULL) {
1608 1.89 riastrad error = EBUSY;
1609 1.89 riastrad goto fail1;
1610 1.89 riastrad }
1611 1.89 riastrad
1612 1.89 riastrad /* Make sure the caller may read the directory. */
1613 1.89 riastrad error = VOP_ACCESS(a_dvp, VEXEC, cred);
1614 1.89 riastrad if (error)
1615 1.89 riastrad goto fail1;
1616 1.89 riastrad
1617 1.89 riastrad a_dirent = tmpfs_dir_lookup(a_dnode, a_cnp);
1618 1.89 riastrad if (a_dirent != NULL) {
1619 1.89 riastrad KASSERT(a_dirent->td_node != NULL);
1620 1.89 riastrad /* We ruled out `.' earlier. */
1621 1.89 riastrad KASSERT(a_dirent->td_node != a_dnode);
1622 1.89 riastrad /* We ruled out `..' earlier. */
1623 1.89 riastrad KASSERT(a_dirent->td_node !=
1624 1.89 riastrad a_dnode->tn_spec.tn_dir.tn_parent);
1625 1.89 riastrad if (a_dirent->td_node == b_dnode) {
1626 1.89 riastrad error = overlap_error;
1627 1.89 riastrad goto fail1;
1628 1.89 riastrad }
1629 1.89 riastrad mutex_enter(&a_dirent->td_node->tn_vlock);
1630 1.89 riastrad error = tmpfs_vnode_get(mount, a_dirent->td_node, &a_vp);
1631 1.89 riastrad if (error)
1632 1.89 riastrad goto fail1;
1633 1.89 riastrad KASSERT(a_vp->v_mount == mount);
1634 1.89 riastrad /* Refuse to rename (over) a mount point. */
1635 1.89 riastrad if ((a_vp->v_type == VDIR) && (a_vp->v_mountedhere != NULL)) {
1636 1.89 riastrad error = EBUSY;
1637 1.89 riastrad goto fail2;
1638 1.85 rmind }
1639 1.89 riastrad } else if (!a_missing_ok) {
1640 1.89 riastrad error = ENOENT;
1641 1.89 riastrad goto fail1;
1642 1.89 riastrad } else {
1643 1.89 riastrad a_vp = NULL;
1644 1.89 riastrad }
1645 1.89 riastrad KASSERT(a_vp != a_dvp);
1646 1.89 riastrad KASSERT(a_vp != b_dvp);
1647 1.45 ad
1648 1.89 riastrad error = tmpfs_rename_lock_directory(b_dvp, b_dnode);
1649 1.89 riastrad if (error)
1650 1.89 riastrad goto fail2;
1651 1.89 riastrad
1652 1.89 riastrad /* Did we lose a race with mount? */
1653 1.89 riastrad if (b_dvp->v_mountedhere != NULL) {
1654 1.89 riastrad error = EBUSY;
1655 1.89 riastrad goto fail3;
1656 1.89 riastrad }
1657 1.89 riastrad
1658 1.89 riastrad /* Make sure the caller may read the directory. */
1659 1.89 riastrad error = VOP_ACCESS(b_dvp, VEXEC, cred);
1660 1.89 riastrad if (error)
1661 1.89 riastrad goto fail3;
1662 1.89 riastrad
1663 1.89 riastrad b_dirent = tmpfs_dir_lookup(b_dnode, b_cnp);
1664 1.89 riastrad if (b_dirent != NULL) {
1665 1.89 riastrad KASSERT(b_dirent->td_node != NULL);
1666 1.89 riastrad /* We ruled out `.' earlier. */
1667 1.89 riastrad KASSERT(b_dirent->td_node != b_dnode);
1668 1.89 riastrad /* We ruled out `..' earlier. */
1669 1.89 riastrad KASSERT(b_dirent->td_node !=
1670 1.89 riastrad b_dnode->tn_spec.tn_dir.tn_parent);
1671 1.89 riastrad /* b is not an ancestor of a. */
1672 1.89 riastrad KASSERT(b_dirent->td_node != a_dnode);
1673 1.89 riastrad /* But the source and target nodes might be the same. */
1674 1.89 riastrad if ((a_dirent == NULL) ||
1675 1.89 riastrad (a_dirent->td_node != b_dirent->td_node)) {
1676 1.89 riastrad mutex_enter(&b_dirent->td_node->tn_vlock);
1677 1.89 riastrad error = tmpfs_vnode_get(mount, b_dirent->td_node,
1678 1.89 riastrad &b_vp);
1679 1.89 riastrad if (error)
1680 1.89 riastrad goto fail3;
1681 1.89 riastrad KASSERT(b_vp->v_mount == mount);
1682 1.89 riastrad KASSERT(a_vp != b_vp);
1683 1.89 riastrad /* Refuse to rename (over) a mount point. */
1684 1.89 riastrad if ((b_vp->v_type == VDIR) &&
1685 1.89 riastrad (b_vp->v_mountedhere != NULL)) {
1686 1.89 riastrad error = EBUSY;
1687 1.89 riastrad goto fail4;
1688 1.89 riastrad }
1689 1.89 riastrad } else {
1690 1.89 riastrad b_vp = a_vp;
1691 1.89 riastrad vref(b_vp);
1692 1.89 riastrad }
1693 1.89 riastrad } else if (!b_missing_ok) {
1694 1.89 riastrad error = ENOENT;
1695 1.89 riastrad goto fail3;
1696 1.89 riastrad } else {
1697 1.89 riastrad b_vp = NULL;
1698 1.45 ad }
1699 1.89 riastrad KASSERT(b_vp != a_dvp);
1700 1.89 riastrad KASSERT(b_vp != b_dvp);
1701 1.89 riastrad
1702 1.89 riastrad KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
1703 1.89 riastrad KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
1704 1.89 riastrad KASSERT(a_missing_ok || (a_dirent != NULL));
1705 1.89 riastrad KASSERT(a_missing_ok || (a_dirent->td_node != NULL));
1706 1.89 riastrad KASSERT(b_missing_ok || (b_dirent != NULL));
1707 1.89 riastrad KASSERT(b_missing_ok || (b_dirent->td_node != NULL));
1708 1.89 riastrad KASSERT((a_dirent == NULL) || (a_dirent->td_node != NULL));
1709 1.89 riastrad KASSERT((a_dirent == NULL) || (a_dirent->td_node->tn_vnode == a_vp));
1710 1.89 riastrad KASSERT((b_dirent == NULL) || (b_dirent->td_node != NULL));
1711 1.89 riastrad KASSERT((b_dirent == NULL) || (b_dirent->td_node->tn_vnode == b_vp));
1712 1.89 riastrad KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE));
1713 1.89 riastrad KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE));
1714 1.89 riastrad
1715 1.89 riastrad *a_dirent_ret = a_dirent;
1716 1.89 riastrad *b_dirent_ret = b_dirent;
1717 1.89 riastrad *a_vp_ret = a_vp;
1718 1.89 riastrad *b_vp_ret = b_vp;
1719 1.89 riastrad return 0;
1720 1.45 ad
1721 1.89 riastrad fail4: if (b_vp != NULL) {
1722 1.89 riastrad KASSERT(VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE);
1723 1.89 riastrad if (b_vp != a_vp)
1724 1.89 riastrad vput(b_vp);
1725 1.89 riastrad else
1726 1.89 riastrad vrele(a_vp);
1727 1.89 riastrad }
1728 1.1 jmmv
1729 1.89 riastrad fail3: KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
1730 1.89 riastrad VOP_UNLOCK(b_dvp);
1731 1.1 jmmv
1732 1.89 riastrad fail2: if (a_vp != NULL) {
1733 1.89 riastrad KASSERT(VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE);
1734 1.89 riastrad vput(a_vp);
1735 1.1 jmmv }
1736 1.89 riastrad
1737 1.89 riastrad fail1: KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
1738 1.89 riastrad VOP_UNLOCK(a_dvp);
1739 1.89 riastrad
1740 1.89 riastrad fail0: /* KASSERT(VOP_ISLOCKED(a_dvp) != LK_EXCLUSIVE); */
1741 1.89 riastrad /* KASSERT(VOP_ISLOCKED(b_dvp) != LK_EXCLUSIVE); */
1742 1.89 riastrad /* KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) != LK_EXCLUSIVE)); */
1743 1.89 riastrad /* KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) != LK_EXCLUSIVE)); */
1744 1.89 riastrad return error;
1745 1.89 riastrad }
1746 1.89 riastrad
1747 1.89 riastrad /*
1748 1.89 riastrad * Shuffle the directory entries to move fvp from the directory fdvp
1749 1.89 riastrad * into the directory tdvp. fde is fvp's directory entry in fdvp. If
1750 1.89 riastrad * we are overwriting a target node, it is tvp, and tde is its
1751 1.89 riastrad * directory entry in tdvp.
1752 1.89 riastrad *
1753 1.89 riastrad * fdvp, fvp, tdvp, and tvp must all be locked and referenced.
1754 1.89 riastrad */
1755 1.89 riastrad static void
1756 1.89 riastrad tmpfs_rename_attachdetach(struct tmpfs_mount *tmpfs,
1757 1.89 riastrad struct vnode *fdvp, struct tmpfs_dirent *fde, struct vnode *fvp,
1758 1.89 riastrad struct vnode *tdvp, struct tmpfs_dirent *tde, struct vnode *tvp)
1759 1.89 riastrad {
1760 1.89 riastrad
1761 1.89 riastrad KASSERT(tmpfs != NULL);
1762 1.89 riastrad KASSERT(fdvp != NULL);
1763 1.89 riastrad KASSERT(fde != NULL);
1764 1.89 riastrad KASSERT(fvp != NULL);
1765 1.89 riastrad KASSERT(tdvp != NULL);
1766 1.89 riastrad KASSERT(fde->td_node != NULL);
1767 1.89 riastrad KASSERT(fde->td_node->tn_vnode == fvp);
1768 1.89 riastrad KASSERT((tde == NULL) == (tvp == NULL));
1769 1.89 riastrad KASSERT((tde == NULL) || (tde->td_node != NULL));
1770 1.89 riastrad KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
1771 1.89 riastrad KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
1772 1.89 riastrad KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1773 1.89 riastrad KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
1774 1.89 riastrad KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1775 1.89 riastrad
1776 1.89 riastrad /*
1777 1.89 riastrad * If we are moving from one directory to another, detach the
1778 1.89 riastrad * source entry and reattach it to the target directory.
1779 1.89 riastrad */
1780 1.85 rmind if (fdvp != tdvp) {
1781 1.89 riastrad /* tmpfs_dir_detach clobbers fde->td_node, so save it. */
1782 1.89 riastrad struct tmpfs_node *fnode = fde->td_node;
1783 1.89 riastrad tmpfs_dir_detach(fdvp, fde);
1784 1.89 riastrad tmpfs_dir_attach(tdvp, fde, fnode);
1785 1.89 riastrad } else if (tvp == NULL) {
1786 1.89 riastrad /*
1787 1.89 riastrad * We are changing the directory. tmpfs_dir_attach and
1788 1.89 riastrad * tmpfs_dir_detach note the events for us, but for
1789 1.89 riastrad * this case we don't call them, so we must note the
1790 1.89 riastrad * event explicitly.
1791 1.89 riastrad */
1792 1.89 riastrad VN_KNOTE(fdvp, NOTE_WRITE);
1793 1.83 rmind }
1794 1.89 riastrad
1795 1.89 riastrad /*
1796 1.89 riastrad * If we are replacing an existing target entry, delete it.
1797 1.89 riastrad */
1798 1.89 riastrad if (tde != NULL) {
1799 1.89 riastrad KASSERT(tvp != NULL);
1800 1.89 riastrad KASSERT(tde->td_node != NULL);
1801 1.89 riastrad KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
1802 1.89 riastrad if (tde->td_node->tn_type == VDIR) {
1803 1.89 riastrad KASSERT(tde->td_node->tn_size == 0);
1804 1.89 riastrad KASSERT(tde->td_node->tn_links == 2);
1805 1.89 riastrad /* Decrement the extra link count for `.' so
1806 1.89 riastrad * the vnode will be recycled when released. */
1807 1.89 riastrad tde->td_node->tn_links--;
1808 1.89 riastrad }
1809 1.89 riastrad tmpfs_dir_detach(tdvp, tde);
1810 1.89 riastrad tmpfs_free_dirent(tmpfs, tde);
1811 1.85 rmind }
1812 1.89 riastrad }
1813 1.89 riastrad
1814 1.89 riastrad /*
1815 1.89 riastrad * Remove the entry de for the non-directory vp from the directory dvp.
1816 1.89 riastrad *
1817 1.89 riastrad * Everything must be locked and referenced.
1818 1.89 riastrad */
1819 1.89 riastrad static int
1820 1.89 riastrad tmpfs_do_remove(struct tmpfs_mount *tmpfs, struct vnode *dvp,
1821 1.89 riastrad struct tmpfs_node *dnode, struct tmpfs_dirent *de, struct vnode *vp,
1822 1.89 riastrad kauth_cred_t cred)
1823 1.89 riastrad {
1824 1.89 riastrad int error;
1825 1.89 riastrad
1826 1.89 riastrad KASSERT(tmpfs != NULL);
1827 1.89 riastrad KASSERT(dvp != NULL);
1828 1.89 riastrad KASSERT(dnode != NULL);
1829 1.89 riastrad KASSERT(de != NULL);
1830 1.89 riastrad KASSERT(vp != NULL);
1831 1.89 riastrad KASSERT(dnode->tn_vnode == dvp);
1832 1.89 riastrad KASSERT(de->td_node != NULL);
1833 1.89 riastrad KASSERT(de->td_node->tn_vnode == vp);
1834 1.89 riastrad KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
1835 1.89 riastrad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1836 1.89 riastrad
1837 1.89 riastrad error = tmpfs_remove_check_possible(dnode, de->td_node);
1838 1.89 riastrad if (error)
1839 1.89 riastrad return error;
1840 1.89 riastrad
1841 1.89 riastrad error = tmpfs_remove_check_permitted(cred, dnode, de->td_node);
1842 1.89 riastrad error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE, vp, dvp,
1843 1.89 riastrad error);
1844 1.89 riastrad if (error)
1845 1.89 riastrad return error;
1846 1.89 riastrad
1847 1.89 riastrad tmpfs_dir_detach(dvp, de);
1848 1.89 riastrad tmpfs_free_dirent(tmpfs, de);
1849 1.89 riastrad
1850 1.89 riastrad return 0;
1851 1.89 riastrad }
1852 1.89 riastrad
1853 1.89 riastrad /*
1854 1.89 riastrad * Check whether a rename is possible independent of credentials.
1855 1.89 riastrad *
1856 1.89 riastrad * Everything must be locked and referenced.
1857 1.89 riastrad */
1858 1.89 riastrad static int
1859 1.89 riastrad tmpfs_rename_check_possible(
1860 1.89 riastrad struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
1861 1.89 riastrad struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
1862 1.89 riastrad {
1863 1.89 riastrad
1864 1.89 riastrad KASSERT(fdnode != NULL);
1865 1.89 riastrad KASSERT(fnode != NULL);
1866 1.89 riastrad KASSERT(tdnode != NULL);
1867 1.89 riastrad KASSERT(fdnode != fnode);
1868 1.89 riastrad KASSERT(tdnode != tnode);
1869 1.89 riastrad KASSERT(fnode != tnode);
1870 1.89 riastrad KASSERT(fdnode->tn_vnode != NULL);
1871 1.89 riastrad KASSERT(fnode->tn_vnode != NULL);
1872 1.89 riastrad KASSERT(tdnode->tn_vnode != NULL);
1873 1.89 riastrad KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
1874 1.89 riastrad KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
1875 1.89 riastrad KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
1876 1.89 riastrad KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
1877 1.89 riastrad KASSERT((tnode == NULL) ||
1878 1.89 riastrad (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
1879 1.89 riastrad
1880 1.89 riastrad /*
1881 1.89 riastrad * If fdnode is immutable, we can't write to it. If fdnode is
1882 1.89 riastrad * append-only, the only change we can make is to add entries
1883 1.89 riastrad * to it. If fnode is immutable, we can't change the links to
1884 1.89 riastrad * it. If fnode is append-only...well, this is what UFS does.
1885 1.89 riastrad */
1886 1.89 riastrad if ((fdnode->tn_flags | fnode->tn_flags) & (IMMUTABLE | APPEND))
1887 1.89 riastrad return EPERM;
1888 1.89 riastrad
1889 1.89 riastrad /*
1890 1.89 riastrad * If tdnode is immutable, we can't write to it. If tdnode is
1891 1.89 riastrad * append-only, we can add entries, but we can't change
1892 1.89 riastrad * existing entries.
1893 1.89 riastrad */
1894 1.89 riastrad if (tdnode->tn_flags & (IMMUTABLE | (tnode? APPEND : 0)))
1895 1.89 riastrad return EPERM;
1896 1.89 riastrad
1897 1.89 riastrad /*
1898 1.89 riastrad * If tnode is immutable, we can't replace links to it. If
1899 1.89 riastrad * tnode is append-only...well, this is what UFS does.
1900 1.89 riastrad */
1901 1.89 riastrad if (tnode != NULL) {
1902 1.89 riastrad KASSERT(tnode != NULL);
1903 1.89 riastrad if ((tnode->tn_flags & (IMMUTABLE | APPEND)) != 0)
1904 1.89 riastrad return EPERM;
1905 1.83 rmind }
1906 1.89 riastrad
1907 1.89 riastrad return 0;
1908 1.89 riastrad }
1909 1.89 riastrad
1910 1.89 riastrad /*
1911 1.89 riastrad * Check whether a rename is permitted given our credentials.
1912 1.89 riastrad *
1913 1.89 riastrad * Everything must be locked and referenced.
1914 1.89 riastrad */
1915 1.89 riastrad static int
1916 1.89 riastrad tmpfs_rename_check_permitted(kauth_cred_t cred,
1917 1.89 riastrad struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
1918 1.89 riastrad struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
1919 1.89 riastrad {
1920 1.89 riastrad int error;
1921 1.89 riastrad
1922 1.89 riastrad KASSERT(fdnode != NULL);
1923 1.89 riastrad KASSERT(fnode != NULL);
1924 1.89 riastrad KASSERT(tdnode != NULL);
1925 1.89 riastrad KASSERT(fdnode != fnode);
1926 1.89 riastrad KASSERT(tdnode != tnode);
1927 1.89 riastrad KASSERT(fnode != tnode);
1928 1.89 riastrad KASSERT(fdnode->tn_vnode != NULL);
1929 1.89 riastrad KASSERT(fnode->tn_vnode != NULL);
1930 1.89 riastrad KASSERT(tdnode->tn_vnode != NULL);
1931 1.89 riastrad KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
1932 1.89 riastrad KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
1933 1.89 riastrad KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
1934 1.89 riastrad KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
1935 1.89 riastrad KASSERT((tnode == NULL) ||
1936 1.89 riastrad (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
1937 1.89 riastrad
1938 1.89 riastrad /*
1939 1.89 riastrad * We need to remove or change an entry in the source directory.
1940 1.89 riastrad */
1941 1.89 riastrad error = VOP_ACCESS(fdnode->tn_vnode, VWRITE, cred);
1942 1.89 riastrad if (error)
1943 1.89 riastrad return error;
1944 1.89 riastrad
1945 1.89 riastrad /*
1946 1.89 riastrad * If we are changing directories, then we need to write to the
1947 1.89 riastrad * target directory to add or change an entry. Also, if fnode
1948 1.89 riastrad * is a directory, we need to write to it to change its `..'
1949 1.89 riastrad * entry.
1950 1.89 riastrad */
1951 1.89 riastrad if (fdnode != tdnode) {
1952 1.89 riastrad error = VOP_ACCESS(tdnode->tn_vnode, VWRITE, cred);
1953 1.89 riastrad if (error)
1954 1.89 riastrad return error;
1955 1.89 riastrad if (fnode->tn_type == VDIR) {
1956 1.89 riastrad error = VOP_ACCESS(fnode->tn_vnode, VWRITE, cred);
1957 1.89 riastrad if (error)
1958 1.89 riastrad return error;
1959 1.89 riastrad }
1960 1.83 rmind }
1961 1.1 jmmv
1962 1.89 riastrad error = tmpfs_check_sticky(cred, fdnode, fnode);
1963 1.89 riastrad if (error)
1964 1.89 riastrad return error;
1965 1.89 riastrad
1966 1.89 riastrad error = tmpfs_check_sticky(cred, tdnode, tnode);
1967 1.89 riastrad if (error)
1968 1.89 riastrad return error;
1969 1.89 riastrad
1970 1.89 riastrad return 0;
1971 1.89 riastrad }
1972 1.89 riastrad
1973 1.89 riastrad /*
1974 1.89 riastrad * Check whether removing node's entry in dnode is possible independent
1975 1.89 riastrad * of credentials.
1976 1.89 riastrad *
1977 1.89 riastrad * Everything must be locked and referenced.
1978 1.89 riastrad */
1979 1.89 riastrad static int
1980 1.89 riastrad tmpfs_remove_check_possible(struct tmpfs_node *dnode, struct tmpfs_node *node)
1981 1.89 riastrad {
1982 1.89 riastrad
1983 1.89 riastrad KASSERT(dnode != NULL);
1984 1.89 riastrad KASSERT(dnode->tn_vnode != NULL);
1985 1.89 riastrad KASSERT(node != NULL);
1986 1.89 riastrad KASSERT(dnode != node);
1987 1.89 riastrad KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
1988 1.89 riastrad KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
1989 1.89 riastrad
1990 1.89 riastrad /*
1991 1.89 riastrad * We want to delete the entry. If dnode is immutable, we
1992 1.89 riastrad * can't write to it to delete the entry. If dnode is
1993 1.89 riastrad * append-only, the only change we can make is to add entries,
1994 1.89 riastrad * so we can't delete entries. If node is immutable, we can't
1995 1.89 riastrad * change the links to it, so we can't delete the entry. If
1996 1.89 riastrad * node is append-only...well, this is what UFS does.
1997 1.89 riastrad */
1998 1.89 riastrad if ((dnode->tn_flags | node->tn_flags) & (IMMUTABLE | APPEND))
1999 1.89 riastrad return EPERM;
2000 1.89 riastrad
2001 1.89 riastrad return 0;
2002 1.89 riastrad }
2003 1.89 riastrad
2004 1.89 riastrad /*
2005 1.89 riastrad * Check whether removing node's entry in dnode is permitted given our
2006 1.89 riastrad * credentials.
2007 1.89 riastrad *
2008 1.89 riastrad * Everything must be locked and referenced.
2009 1.89 riastrad */
2010 1.89 riastrad static int
2011 1.89 riastrad tmpfs_remove_check_permitted(kauth_cred_t cred,
2012 1.89 riastrad struct tmpfs_node *dnode, struct tmpfs_node *node)
2013 1.89 riastrad {
2014 1.89 riastrad int error;
2015 1.89 riastrad
2016 1.89 riastrad KASSERT(dnode != NULL);
2017 1.89 riastrad KASSERT(dnode->tn_vnode != NULL);
2018 1.89 riastrad KASSERT(node != NULL);
2019 1.89 riastrad KASSERT(dnode != node);
2020 1.89 riastrad KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
2021 1.89 riastrad KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
2022 1.89 riastrad
2023 1.89 riastrad /*
2024 1.89 riastrad * Check whether we are permitted to write to the source
2025 1.89 riastrad * directory in order to delete an entry from it.
2026 1.89 riastrad */
2027 1.89 riastrad error = VOP_ACCESS(dnode->tn_vnode, VWRITE, cred);
2028 1.89 riastrad if (error)
2029 1.89 riastrad return error;
2030 1.89 riastrad
2031 1.89 riastrad error = tmpfs_check_sticky(cred, dnode, node);
2032 1.89 riastrad if (error)
2033 1.89 riastrad return error;
2034 1.89 riastrad
2035 1.89 riastrad return 0;
2036 1.89 riastrad }
2037 1.89 riastrad
2038 1.89 riastrad /*
2039 1.89 riastrad * Check whether we may change an entry in a sticky directory. If the
2040 1.89 riastrad * directory is sticky, the user must own either the directory or, if
2041 1.89 riastrad * it exists, the node, in order to change the entry.
2042 1.89 riastrad *
2043 1.89 riastrad * Everything must be locked and referenced.
2044 1.89 riastrad */
2045 1.89 riastrad static int
2046 1.89 riastrad tmpfs_check_sticky(kauth_cred_t cred,
2047 1.89 riastrad struct tmpfs_node *dnode, struct tmpfs_node *node)
2048 1.89 riastrad {
2049 1.1 jmmv
2050 1.89 riastrad KASSERT(dnode != NULL);
2051 1.89 riastrad KASSERT(dnode->tn_vnode != NULL);
2052 1.89 riastrad KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
2053 1.89 riastrad KASSERT((node == NULL) || (node->tn_vnode != NULL));
2054 1.89 riastrad KASSERT((node == NULL) ||
2055 1.89 riastrad (VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE));
2056 1.89 riastrad
2057 1.89 riastrad if (dnode->tn_mode & S_ISTXT) {
2058 1.89 riastrad uid_t euid = kauth_cred_geteuid(cred);
2059 1.89 riastrad if (euid == dnode->tn_uid)
2060 1.89 riastrad return 0;
2061 1.89 riastrad if ((node == NULL) || (euid == node->tn_uid))
2062 1.89 riastrad return 0;
2063 1.89 riastrad return EPERM;
2064 1.70 rmind }
2065 1.89 riastrad
2066 1.89 riastrad return 0;
2067 1.1 jmmv }
2068 1.1 jmmv
2069 1.1 jmmv int
2070 1.1 jmmv tmpfs_mkdir(void *v)
2071 1.1 jmmv {
2072 1.83 rmind struct vop_mkdir_args /* {
2073 1.83 rmind struct vnode *a_dvp;
2074 1.83 rmind struct vnode **a_vpp;
2075 1.83 rmind struct componentname *a_cnp;
2076 1.83 rmind struct vattr *a_vap;
2077 1.83 rmind } */ *ap = v;
2078 1.83 rmind vnode_t *dvp = ap->a_dvp;
2079 1.83 rmind vnode_t **vpp = ap->a_vpp;
2080 1.83 rmind struct componentname *cnp = ap->a_cnp;
2081 1.83 rmind struct vattr *vap = ap->a_vap;
2082 1.1 jmmv
2083 1.1 jmmv KASSERT(vap->va_type == VDIR);
2084 1.1 jmmv return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
2085 1.1 jmmv }
2086 1.1 jmmv
2087 1.1 jmmv int
2088 1.1 jmmv tmpfs_rmdir(void *v)
2089 1.1 jmmv {
2090 1.83 rmind struct vop_rmdir_args /* {
2091 1.83 rmind struct vnode *a_dvp;
2092 1.83 rmind struct vnode *a_vp;
2093 1.83 rmind struct componentname *a_cnp;
2094 1.83 rmind } */ *ap = v;
2095 1.83 rmind vnode_t *dvp = ap->a_dvp;
2096 1.83 rmind vnode_t *vp = ap->a_vp;
2097 1.83 rmind tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
2098 1.83 rmind tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
2099 1.83 rmind tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
2100 1.83 rmind tmpfs_dirent_t *de;
2101 1.83 rmind int error = 0;
2102 1.1 jmmv
2103 1.1 jmmv KASSERT(VOP_ISLOCKED(dvp));
2104 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
2105 1.83 rmind KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
2106 1.1 jmmv
2107 1.83 rmind /*
2108 1.90 hannken * Directories with more than two non-whiteout
2109 1.90 hannken * entries ('.' and '..') cannot be removed.
2110 1.83 rmind */
2111 1.34 pooka if (node->tn_size > 0) {
2112 1.90 hannken KASSERT(error == 0);
2113 1.90 hannken TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
2114 1.90 hannken if (de->td_node != TMPFS_NODE_WHITEOUT) {
2115 1.90 hannken error = ENOTEMPTY;
2116 1.90 hannken break;
2117 1.90 hannken }
2118 1.90 hannken }
2119 1.90 hannken if (error)
2120 1.90 hannken goto out;
2121 1.34 pooka }
2122 1.34 pooka
2123 1.85 rmind /* Lookup the directory entry (check the cached hint first). */
2124 1.85 rmind de = tmpfs_dir_cached(node);
2125 1.85 rmind if (de == NULL) {
2126 1.85 rmind struct componentname *cnp = ap->a_cnp;
2127 1.85 rmind de = tmpfs_dir_lookup(dnode, cnp);
2128 1.85 rmind }
2129 1.83 rmind KASSERT(de && de->td_node == node);
2130 1.1 jmmv
2131 1.1 jmmv /* Check flags to see if we are allowed to remove the directory. */
2132 1.1 jmmv if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
2133 1.1 jmmv error = EPERM;
2134 1.1 jmmv goto out;
2135 1.1 jmmv }
2136 1.1 jmmv
2137 1.85 rmind /* Decrement the link count for the virtual '.' entry. */
2138 1.1 jmmv node->tn_links--;
2139 1.83 rmind node->tn_status |= TMPFS_NODE_STATUSALL;
2140 1.1 jmmv
2141 1.86 rmind /* Detach the directory entry from the directory. */
2142 1.86 rmind tmpfs_dir_detach(dvp, de);
2143 1.86 rmind
2144 1.83 rmind /* Purge the cache for parent. */
2145 1.83 rmind cache_purge(dvp);
2146 1.1 jmmv
2147 1.83 rmind /*
2148 1.90 hannken * Destroy the directory entry or replace it with a whiteout.
2149 1.90 hannken * Note: the inode referred by it will not be destroyed
2150 1.90 hannken * until the vnode is reclaimed.
2151 1.83 rmind */
2152 1.90 hannken if (ap->a_cnp->cn_flags & DOWHITEOUT)
2153 1.90 hannken tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
2154 1.90 hannken else
2155 1.90 hannken tmpfs_free_dirent(tmp, de);
2156 1.90 hannken
2157 1.90 hannken /* Destroy the whiteout entries from the node. */
2158 1.90 hannken while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
2159 1.90 hannken KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
2160 1.90 hannken tmpfs_dir_detach(vp, de);
2161 1.90 hannken tmpfs_free_dirent(tmp, de);
2162 1.90 hannken }
2163 1.90 hannken
2164 1.45 ad KASSERT(node->tn_links == 0);
2165 1.83 rmind out:
2166 1.40 dyoung /* Release the nodes. */
2167 1.40 dyoung vput(dvp);
2168 1.1 jmmv vput(vp);
2169 1.1 jmmv return error;
2170 1.1 jmmv }
2171 1.1 jmmv
2172 1.1 jmmv int
2173 1.1 jmmv tmpfs_symlink(void *v)
2174 1.1 jmmv {
2175 1.83 rmind struct vop_symlink_args /* {
2176 1.83 rmind struct vnode *a_dvp;
2177 1.83 rmind struct vnode **a_vpp;
2178 1.83 rmind struct componentname *a_cnp;
2179 1.83 rmind struct vattr *a_vap;
2180 1.83 rmind char *a_target;
2181 1.83 rmind } */ *ap = v;
2182 1.83 rmind vnode_t *dvp = ap->a_dvp;
2183 1.83 rmind vnode_t **vpp = ap->a_vpp;
2184 1.83 rmind struct componentname *cnp = ap->a_cnp;
2185 1.83 rmind struct vattr *vap = ap->a_vap;
2186 1.83 rmind char *target = ap->a_target;
2187 1.1 jmmv
2188 1.1 jmmv KASSERT(vap->va_type == VLNK);
2189 1.1 jmmv return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
2190 1.1 jmmv }
2191 1.1 jmmv
2192 1.1 jmmv int
2193 1.1 jmmv tmpfs_readdir(void *v)
2194 1.1 jmmv {
2195 1.83 rmind struct vop_readdir_args /* {
2196 1.83 rmind struct vnode *a_vp;
2197 1.83 rmind struct uio *a_uio;
2198 1.83 rmind kauth_cred_t a_cred;
2199 1.83 rmind int *a_eofflag;
2200 1.83 rmind off_t **a_cookies;
2201 1.83 rmind int *ncookies;
2202 1.83 rmind } */ *ap = v;
2203 1.83 rmind vnode_t *vp = ap->a_vp;
2204 1.83 rmind struct uio *uio = ap->a_uio;
2205 1.83 rmind int *eofflag = ap->a_eofflag;
2206 1.83 rmind off_t **cookies = ap->a_cookies;
2207 1.83 rmind int *ncookies = ap->a_ncookies;
2208 1.83 rmind off_t startoff, cnt;
2209 1.83 rmind tmpfs_node_t *node;
2210 1.1 jmmv int error;
2211 1.1 jmmv
2212 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
2213 1.1 jmmv
2214 1.1 jmmv /* This operation only makes sense on directory nodes. */
2215 1.1 jmmv if (vp->v_type != VDIR) {
2216 1.83 rmind return ENOTDIR;
2217 1.1 jmmv }
2218 1.1 jmmv node = VP_TO_TMPFS_DIR(vp);
2219 1.1 jmmv startoff = uio->uio_offset;
2220 1.83 rmind cnt = 0;
2221 1.1 jmmv
2222 1.10 yamt if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
2223 1.1 jmmv error = tmpfs_dir_getdotdent(node, uio);
2224 1.83 rmind if (error != 0) {
2225 1.83 rmind if (error == -1)
2226 1.83 rmind error = 0;
2227 1.83 rmind goto out;
2228 1.83 rmind }
2229 1.10 yamt cnt++;
2230 1.1 jmmv }
2231 1.10 yamt if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
2232 1.1 jmmv error = tmpfs_dir_getdotdotdent(node, uio);
2233 1.83 rmind if (error != 0) {
2234 1.83 rmind if (error == -1)
2235 1.83 rmind error = 0;
2236 1.83 rmind goto out;
2237 1.83 rmind }
2238 1.10 yamt cnt++;
2239 1.1 jmmv }
2240 1.10 yamt error = tmpfs_dir_getdents(node, uio, &cnt);
2241 1.83 rmind if (error == -1) {
2242 1.1 jmmv error = 0;
2243 1.83 rmind }
2244 1.1 jmmv KASSERT(error >= 0);
2245 1.83 rmind out:
2246 1.83 rmind if (eofflag != NULL) {
2247 1.83 rmind *eofflag = (!error && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
2248 1.83 rmind }
2249 1.83 rmind if (error || cookies == NULL || ncookies == NULL) {
2250 1.83 rmind return error;
2251 1.83 rmind }
2252 1.1 jmmv
2253 1.83 rmind /* Update NFS-related variables, if any. */
2254 1.83 rmind off_t i, off = startoff;
2255 1.83 rmind tmpfs_dirent_t *de = NULL;
2256 1.83 rmind
2257 1.83 rmind *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
2258 1.83 rmind *ncookies = cnt;
2259 1.83 rmind
2260 1.83 rmind for (i = 0; i < cnt; i++) {
2261 1.83 rmind KASSERT(off != TMPFS_DIRCOOKIE_EOF);
2262 1.83 rmind if (off != TMPFS_DIRCOOKIE_DOT) {
2263 1.83 rmind if (off == TMPFS_DIRCOOKIE_DOTDOT) {
2264 1.83 rmind de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
2265 1.83 rmind } else if (de != NULL) {
2266 1.83 rmind de = TAILQ_NEXT(de, td_entries);
2267 1.10 yamt } else {
2268 1.83 rmind de = tmpfs_dir_lookupbycookie(node, off);
2269 1.83 rmind KASSERT(de != NULL);
2270 1.83 rmind de = TAILQ_NEXT(de, td_entries);
2271 1.83 rmind }
2272 1.83 rmind if (de == NULL) {
2273 1.83 rmind off = TMPFS_DIRCOOKIE_EOF;
2274 1.83 rmind } else {
2275 1.83 rmind off = tmpfs_dircookie(de);
2276 1.10 yamt }
2277 1.83 rmind } else {
2278 1.83 rmind off = TMPFS_DIRCOOKIE_DOTDOT;
2279 1.10 yamt }
2280 1.83 rmind (*cookies)[i] = off;
2281 1.1 jmmv }
2282 1.83 rmind KASSERT(uio->uio_offset == off);
2283 1.1 jmmv return error;
2284 1.1 jmmv }
2285 1.1 jmmv
2286 1.1 jmmv int
2287 1.1 jmmv tmpfs_readlink(void *v)
2288 1.1 jmmv {
2289 1.83 rmind struct vop_readlink_args /* {
2290 1.83 rmind struct vnode *a_vp;
2291 1.83 rmind struct uio *a_uio;
2292 1.83 rmind kauth_cred_t a_cred;
2293 1.83 rmind } */ *ap = v;
2294 1.83 rmind vnode_t *vp = ap->a_vp;
2295 1.83 rmind struct uio *uio = ap->a_uio;
2296 1.83 rmind tmpfs_node_t *node;
2297 1.1 jmmv int error;
2298 1.1 jmmv
2299 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
2300 1.1 jmmv KASSERT(uio->uio_offset == 0);
2301 1.1 jmmv KASSERT(vp->v_type == VLNK);
2302 1.1 jmmv
2303 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
2304 1.21 jmmv error = uiomove(node->tn_spec.tn_lnk.tn_link,
2305 1.21 jmmv MIN(node->tn_size, uio->uio_resid), uio);
2306 1.1 jmmv node->tn_status |= TMPFS_NODE_ACCESSED;
2307 1.1 jmmv
2308 1.1 jmmv return error;
2309 1.1 jmmv }
2310 1.1 jmmv
2311 1.1 jmmv int
2312 1.1 jmmv tmpfs_inactive(void *v)
2313 1.1 jmmv {
2314 1.82 rmind struct vop_inactive_args /* {
2315 1.82 rmind struct vnode *a_vp;
2316 1.82 rmind bool *a_recycle;
2317 1.82 rmind } */ *ap = v;
2318 1.83 rmind vnode_t *vp = ap->a_vp;
2319 1.83 rmind tmpfs_node_t *node;
2320 1.1 jmmv
2321 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
2322 1.1 jmmv
2323 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
2324 1.82 rmind *ap->a_recycle = (node->tn_links == 0);
2325 1.71 hannken VOP_UNLOCK(vp);
2326 1.1 jmmv
2327 1.1 jmmv return 0;
2328 1.1 jmmv }
2329 1.1 jmmv
2330 1.1 jmmv int
2331 1.1 jmmv tmpfs_reclaim(void *v)
2332 1.1 jmmv {
2333 1.82 rmind struct vop_reclaim_args /* {
2334 1.82 rmind struct vnode *a_vp;
2335 1.82 rmind } */ *ap = v;
2336 1.83 rmind vnode_t *vp = ap->a_vp;
2337 1.83 rmind tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
2338 1.83 rmind tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
2339 1.85 rmind bool racing;
2340 1.1 jmmv
2341 1.82 rmind /* Disassociate inode from vnode. */
2342 1.85 rmind mutex_enter(&node->tn_vlock);
2343 1.85 rmind node->tn_vnode = NULL;
2344 1.85 rmind vp->v_data = NULL;
2345 1.85 rmind /* Check if tmpfs_vnode_get() is racing with us. */
2346 1.85 rmind racing = TMPFS_NODE_RECLAIMING(node);
2347 1.85 rmind mutex_exit(&node->tn_vlock);
2348 1.1 jmmv
2349 1.85 rmind /*
2350 1.85 rmind * If inode is not referenced, i.e. no links, then destroy it.
2351 1.85 rmind * Note: if racing - inode is about to get a new vnode, leave it.
2352 1.85 rmind */
2353 1.85 rmind if (node->tn_links == 0 && !racing) {
2354 1.1 jmmv tmpfs_free_node(tmp, node);
2355 1.82 rmind }
2356 1.1 jmmv return 0;
2357 1.1 jmmv }
2358 1.1 jmmv
2359 1.1 jmmv int
2360 1.1 jmmv tmpfs_pathconf(void *v)
2361 1.1 jmmv {
2362 1.83 rmind struct vop_pathconf_args /* {
2363 1.83 rmind struct vnode *a_vp;
2364 1.83 rmind int a_name;
2365 1.83 rmind register_t *a_retval;
2366 1.83 rmind } */ *ap = v;
2367 1.83 rmind const int name = ap->a_name;
2368 1.83 rmind register_t *retval = ap->a_retval;
2369 1.83 rmind int error = 0;
2370 1.1 jmmv
2371 1.1 jmmv switch (name) {
2372 1.1 jmmv case _PC_LINK_MAX:
2373 1.1 jmmv *retval = LINK_MAX;
2374 1.1 jmmv break;
2375 1.1 jmmv case _PC_NAME_MAX:
2376 1.1 jmmv *retval = NAME_MAX;
2377 1.1 jmmv break;
2378 1.1 jmmv case _PC_PATH_MAX:
2379 1.1 jmmv *retval = PATH_MAX;
2380 1.1 jmmv break;
2381 1.1 jmmv case _PC_PIPE_BUF:
2382 1.1 jmmv *retval = PIPE_BUF;
2383 1.1 jmmv break;
2384 1.1 jmmv case _PC_CHOWN_RESTRICTED:
2385 1.1 jmmv *retval = 1;
2386 1.1 jmmv break;
2387 1.1 jmmv case _PC_NO_TRUNC:
2388 1.1 jmmv *retval = 1;
2389 1.1 jmmv break;
2390 1.1 jmmv case _PC_SYNC_IO:
2391 1.1 jmmv *retval = 1;
2392 1.1 jmmv break;
2393 1.1 jmmv case _PC_FILESIZEBITS:
2394 1.85 rmind *retval = sizeof(off_t) * CHAR_BIT;
2395 1.1 jmmv break;
2396 1.1 jmmv default:
2397 1.1 jmmv error = EINVAL;
2398 1.1 jmmv }
2399 1.1 jmmv return error;
2400 1.1 jmmv }
2401 1.1 jmmv
2402 1.1 jmmv int
2403 1.15 jmmv tmpfs_advlock(void *v)
2404 1.15 jmmv {
2405 1.83 rmind struct vop_advlock_args /* {
2406 1.83 rmind struct vnode *a_vp;
2407 1.83 rmind void * a_id;
2408 1.83 rmind int a_op;
2409 1.83 rmind struct flock *a_fl;
2410 1.83 rmind int a_flags;
2411 1.83 rmind } */ *ap = v;
2412 1.83 rmind vnode_t *vp = ap->a_vp;
2413 1.83 rmind tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
2414 1.15 jmmv
2415 1.15 jmmv return lf_advlock(v, &node->tn_lockf, node->tn_size);
2416 1.15 jmmv }
2417 1.15 jmmv
2418 1.15 jmmv int
2419 1.1 jmmv tmpfs_getpages(void *v)
2420 1.1 jmmv {
2421 1.78 rmind struct vop_getpages_args /* {
2422 1.78 rmind struct vnode *a_vp;
2423 1.78 rmind voff_t a_offset;
2424 1.78 rmind struct vm_page **a_m;
2425 1.78 rmind int *a_count;
2426 1.78 rmind int a_centeridx;
2427 1.78 rmind vm_prot_t a_access_type;
2428 1.78 rmind int a_advice;
2429 1.78 rmind int a_flags;
2430 1.78 rmind } */ * const ap = v;
2431 1.83 rmind vnode_t *vp = ap->a_vp;
2432 1.78 rmind const voff_t offset = ap->a_offset;
2433 1.78 rmind struct vm_page **pgs = ap->a_m;
2434 1.78 rmind const int centeridx = ap->a_centeridx;
2435 1.78 rmind const vm_prot_t access_type = ap->a_access_type;
2436 1.78 rmind const int advice = ap->a_advice;
2437 1.78 rmind const int flags = ap->a_flags;
2438 1.78 rmind int error, npages = *ap->a_count;
2439 1.83 rmind tmpfs_node_t *node;
2440 1.6 yamt struct uvm_object *uobj;
2441 1.1 jmmv
2442 1.6 yamt KASSERT(vp->v_type == VREG);
2443 1.87 rmind KASSERT(mutex_owned(vp->v_interlock));
2444 1.1 jmmv
2445 1.7 jmmv node = VP_TO_TMPFS_NODE(vp);
2446 1.21 jmmv uobj = node->tn_spec.tn_reg.tn_aobj;
2447 1.1 jmmv
2448 1.78 rmind /*
2449 1.78 rmind * Currently, PGO_PASTEOF is not supported.
2450 1.78 rmind */
2451 1.9 yamt if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
2452 1.9 yamt if ((flags & PGO_LOCKED) == 0)
2453 1.87 rmind mutex_exit(vp->v_interlock);
2454 1.9 yamt return EINVAL;
2455 1.9 yamt }
2456 1.9 yamt
2457 1.9 yamt if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
2458 1.9 yamt npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
2459 1.9 yamt }
2460 1.9 yamt
2461 1.7 jmmv if ((flags & PGO_LOCKED) != 0)
2462 1.6 yamt return EBUSY;
2463 1.1 jmmv
2464 1.6 yamt if ((flags & PGO_NOTIMESTAMP) == 0) {
2465 1.7 jmmv if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
2466 1.6 yamt node->tn_status |= TMPFS_NODE_ACCESSED;
2467 1.7 jmmv
2468 1.7 jmmv if ((access_type & VM_PROT_WRITE) != 0)
2469 1.6 yamt node->tn_status |= TMPFS_NODE_MODIFIED;
2470 1.1 jmmv }
2471 1.1 jmmv
2472 1.28 jmmv /*
2473 1.78 rmind * Invoke the pager.
2474 1.49 jmmv *
2475 1.78 rmind * Clean the array of pages before. XXX: PR/32166
2476 1.78 rmind * Note that vnode lock is shared with underlying UVM object.
2477 1.28 jmmv */
2478 1.78 rmind if (pgs) {
2479 1.78 rmind memset(pgs, 0, sizeof(struct vm_pages *) * npages);
2480 1.78 rmind }
2481 1.87 rmind KASSERT(vp->v_interlock == uobj->vmobjlock);
2482 1.87 rmind
2483 1.78 rmind error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx,
2484 1.28 jmmv access_type, advice, flags | PGO_ALLPAGES);
2485 1.78 rmind
2486 1.28 jmmv #if defined(DEBUG)
2487 1.78 rmind if (!error && pgs) {
2488 1.80 matt for (int i = 0; i < npages; i++) {
2489 1.78 rmind KASSERT(pgs[i] != NULL);
2490 1.78 rmind }
2491 1.28 jmmv }
2492 1.28 jmmv #endif
2493 1.6 yamt return error;
2494 1.6 yamt }
2495 1.6 yamt
2496 1.6 yamt int
2497 1.6 yamt tmpfs_putpages(void *v)
2498 1.6 yamt {
2499 1.78 rmind struct vop_putpages_args /* {
2500 1.78 rmind struct vnode *a_vp;
2501 1.78 rmind voff_t a_offlo;
2502 1.78 rmind voff_t a_offhi;
2503 1.78 rmind int a_flags;
2504 1.78 rmind } */ * const ap = v;
2505 1.83 rmind vnode_t *vp = ap->a_vp;
2506 1.78 rmind const voff_t offlo = ap->a_offlo;
2507 1.78 rmind const voff_t offhi = ap->a_offhi;
2508 1.78 rmind const int flags = ap->a_flags;
2509 1.83 rmind tmpfs_node_t *node;
2510 1.6 yamt struct uvm_object *uobj;
2511 1.78 rmind int error;
2512 1.6 yamt
2513 1.87 rmind KASSERT(mutex_owned(vp->v_interlock));
2514 1.7 jmmv
2515 1.6 yamt if (vp->v_type != VREG) {
2516 1.87 rmind mutex_exit(vp->v_interlock);
2517 1.6 yamt return 0;
2518 1.1 jmmv }
2519 1.1 jmmv
2520 1.87 rmind node = VP_TO_TMPFS_NODE(vp);
2521 1.21 jmmv uobj = node->tn_spec.tn_reg.tn_aobj;
2522 1.6 yamt
2523 1.87 rmind KASSERT(vp->v_interlock == uobj->vmobjlock);
2524 1.7 jmmv error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
2525 1.6 yamt
2526 1.6 yamt /* XXX mtime */
2527 1.1 jmmv
2528 1.1 jmmv return error;
2529 1.1 jmmv }
2530 1.76 pooka
2531 1.76 pooka int
2532 1.76 pooka tmpfs_whiteout(void *v)
2533 1.76 pooka {
2534 1.83 rmind struct vop_whiteout_args /* {
2535 1.83 rmind struct vnode *a_dvp;
2536 1.83 rmind struct componentname *a_cnp;
2537 1.83 rmind int a_flags;
2538 1.83 rmind } */ *ap = v;
2539 1.83 rmind vnode_t *dvp = ap->a_dvp;
2540 1.83 rmind struct componentname *cnp = ap->a_cnp;
2541 1.83 rmind const int flags = ap->a_flags;
2542 1.83 rmind tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
2543 1.83 rmind tmpfs_dirent_t *de;
2544 1.76 pooka int error;
2545 1.76 pooka
2546 1.76 pooka switch (flags) {
2547 1.76 pooka case LOOKUP:
2548 1.76 pooka break;
2549 1.76 pooka case CREATE:
2550 1.85 rmind error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr,
2551 1.85 rmind cnp->cn_namelen, &de);
2552 1.76 pooka if (error)
2553 1.76 pooka return error;
2554 1.85 rmind tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
2555 1.76 pooka break;
2556 1.76 pooka case DELETE:
2557 1.76 pooka cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */
2558 1.76 pooka de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), cnp);
2559 1.76 pooka if (de == NULL)
2560 1.76 pooka return ENOENT;
2561 1.76 pooka tmpfs_dir_detach(dvp, de);
2562 1.85 rmind tmpfs_free_dirent(tmp, de);
2563 1.76 pooka break;
2564 1.76 pooka }
2565 1.83 rmind return 0;
2566 1.83 rmind }
2567 1.76 pooka
2568 1.83 rmind int
2569 1.83 rmind tmpfs_print(void *v)
2570 1.83 rmind {
2571 1.83 rmind struct vop_print_args /* {
2572 1.83 rmind struct vnode *a_vp;
2573 1.83 rmind } */ *ap = v;
2574 1.83 rmind vnode_t *vp = ap->a_vp;
2575 1.83 rmind tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
2576 1.83 rmind
2577 1.83 rmind printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
2578 1.83 rmind "\tmode 0%o, owner %d, group %d, size %" PRIdMAX ", status 0x%x",
2579 1.83 rmind node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
2580 1.83 rmind node->tn_gid, (uintmax_t)node->tn_size, node->tn_status);
2581 1.83 rmind if (vp->v_type == VFIFO) {
2582 1.83 rmind VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
2583 1.83 rmind }
2584 1.83 rmind printf("\n");
2585 1.76 pooka return 0;
2586 1.76 pooka }
2587