1/* $NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $ */ 2 3/*- 4 * Copyright (c) 2005 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 9 * 2005 program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33/* 34 * Efficient memory file system supporting functions. 35 */ 36#include <sys/cdefs.h>
| 1/* $NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $ */ 2 3/*- 4 * Copyright (c) 2005 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 9 * 2005 program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33/* 34 * Efficient memory file system supporting functions. 35 */ 36#include <sys/cdefs.h>
|
37__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_subr.c 194766 2009-06-23 20:45:22Z kib $");
| 37__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_subr.c 197953 2009-10-11 07:03:56Z delphij $");
|
38 39#include <sys/param.h> 40#include <sys/namei.h> 41#include <sys/priv.h> 42#include <sys/proc.h> 43#include <sys/stat.h> 44#include <sys/systm.h> 45#include <sys/vnode.h> 46#include <sys/vmmeter.h> 47 48#include <vm/vm.h> 49#include <vm/vm_object.h> 50#include <vm/vm_page.h> 51#include <vm/vm_pager.h> 52#include <vm/vm_extern.h> 53 54#include <fs/tmpfs/tmpfs.h> 55#include <fs/tmpfs/tmpfs_fifoops.h> 56#include <fs/tmpfs/tmpfs_vnops.h> 57 58/* --------------------------------------------------------------------- */ 59 60/* 61 * Allocates a new node of type 'type' inside the 'tmp' mount point, with 62 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode', 63 * using the credentials of the process 'p'. 64 * 65 * If the node type is set to 'VDIR', then the parent parameter must point 66 * to the parent directory of the node being created. It may only be NULL 67 * while allocating the root node. 68 * 69 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter 70 * specifies the device the node represents. 71 * 72 * If the node type is set to 'VLNK', then the parameter target specifies 73 * the file name of the target file for the symbolic link that is being 74 * created. 75 * 76 * Note that new nodes are retrieved from the available list if it has 77 * items or, if it is empty, from the node pool as long as there is enough 78 * space to create them. 79 * 80 * Returns zero on success or an appropriate error code on failure. 81 */ 82int 83tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type, 84 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent, 85 char *target, dev_t rdev, struct tmpfs_node **node) 86{ 87 struct tmpfs_node *nnode; 88 89 /* If the root directory of the 'tmp' file system is not yet 90 * allocated, this must be the request to do it. */ 91 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR)); 92 93 MPASS(IFF(type == VLNK, target != NULL)); 94 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL)); 95 96 if (tmp->tm_nodes_inuse > tmp->tm_nodes_max) 97 return (ENOSPC); 98 99 nnode = (struct tmpfs_node *)uma_zalloc_arg( 100 tmp->tm_node_pool, tmp, M_WAITOK); 101 102 /* Generic initialization. */ 103 nnode->tn_type = type; 104 vfs_timestamp(&nnode->tn_atime); 105 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime = 106 nnode->tn_atime; 107 nnode->tn_uid = uid; 108 nnode->tn_gid = gid; 109 nnode->tn_mode = mode; 110 nnode->tn_id = alloc_unr(tmp->tm_ino_unr); 111 112 /* Type-specific initialization. */ 113 switch (nnode->tn_type) { 114 case VBLK: 115 case VCHR: 116 nnode->tn_rdev = rdev; 117 break; 118 119 case VDIR: 120 TAILQ_INIT(&nnode->tn_dir.tn_dirhead); 121 MPASS(parent != nnode); 122 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL)); 123 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent; 124 nnode->tn_dir.tn_readdir_lastn = 0; 125 nnode->tn_dir.tn_readdir_lastp = NULL; 126 nnode->tn_links++;
| 38 39#include <sys/param.h> 40#include <sys/namei.h> 41#include <sys/priv.h> 42#include <sys/proc.h> 43#include <sys/stat.h> 44#include <sys/systm.h> 45#include <sys/vnode.h> 46#include <sys/vmmeter.h> 47 48#include <vm/vm.h> 49#include <vm/vm_object.h> 50#include <vm/vm_page.h> 51#include <vm/vm_pager.h> 52#include <vm/vm_extern.h> 53 54#include <fs/tmpfs/tmpfs.h> 55#include <fs/tmpfs/tmpfs_fifoops.h> 56#include <fs/tmpfs/tmpfs_vnops.h> 57 58/* --------------------------------------------------------------------- */ 59 60/* 61 * Allocates a new node of type 'type' inside the 'tmp' mount point, with 62 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode', 63 * using the credentials of the process 'p'. 64 * 65 * If the node type is set to 'VDIR', then the parent parameter must point 66 * to the parent directory of the node being created. It may only be NULL 67 * while allocating the root node. 68 * 69 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter 70 * specifies the device the node represents. 71 * 72 * If the node type is set to 'VLNK', then the parameter target specifies 73 * the file name of the target file for the symbolic link that is being 74 * created. 75 * 76 * Note that new nodes are retrieved from the available list if it has 77 * items or, if it is empty, from the node pool as long as there is enough 78 * space to create them. 79 * 80 * Returns zero on success or an appropriate error code on failure. 81 */ 82int 83tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type, 84 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent, 85 char *target, dev_t rdev, struct tmpfs_node **node) 86{ 87 struct tmpfs_node *nnode; 88 89 /* If the root directory of the 'tmp' file system is not yet 90 * allocated, this must be the request to do it. */ 91 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR)); 92 93 MPASS(IFF(type == VLNK, target != NULL)); 94 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL)); 95 96 if (tmp->tm_nodes_inuse > tmp->tm_nodes_max) 97 return (ENOSPC); 98 99 nnode = (struct tmpfs_node *)uma_zalloc_arg( 100 tmp->tm_node_pool, tmp, M_WAITOK); 101 102 /* Generic initialization. */ 103 nnode->tn_type = type; 104 vfs_timestamp(&nnode->tn_atime); 105 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime = 106 nnode->tn_atime; 107 nnode->tn_uid = uid; 108 nnode->tn_gid = gid; 109 nnode->tn_mode = mode; 110 nnode->tn_id = alloc_unr(tmp->tm_ino_unr); 111 112 /* Type-specific initialization. */ 113 switch (nnode->tn_type) { 114 case VBLK: 115 case VCHR: 116 nnode->tn_rdev = rdev; 117 break; 118 119 case VDIR: 120 TAILQ_INIT(&nnode->tn_dir.tn_dirhead); 121 MPASS(parent != nnode); 122 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL)); 123 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent; 124 nnode->tn_dir.tn_readdir_lastn = 0; 125 nnode->tn_dir.tn_readdir_lastp = NULL; 126 nnode->tn_links++;
|
| 127 TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
|
127 nnode->tn_dir.tn_parent->tn_links++;
| 128 nnode->tn_dir.tn_parent->tn_links++;
|
| 129 TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
|
128 break; 129 130 case VFIFO: 131 /* FALLTHROUGH */ 132 case VSOCK: 133 break; 134 135 case VLNK: 136 MPASS(strlen(target) < MAXPATHLEN); 137 nnode->tn_size = strlen(target); 138 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME, 139 M_WAITOK); 140 memcpy(nnode->tn_link, target, nnode->tn_size); 141 break; 142 143 case VREG: 144 nnode->tn_reg.tn_aobj = 145 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0, 146 NULL /* XXXKIB - tmpfs needs swap reservation */); 147 nnode->tn_reg.tn_aobj_pages = 0; 148 break; 149 150 default: 151 panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type); 152 } 153 154 TMPFS_LOCK(tmp); 155 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries); 156 tmp->tm_nodes_inuse++; 157 TMPFS_UNLOCK(tmp); 158 159 *node = nnode; 160 return 0; 161} 162 163/* --------------------------------------------------------------------- */ 164 165/* 166 * Destroys the node pointed to by node from the file system 'tmp'. 167 * If the node does not belong to the given mount point, the results are 168 * unpredicted. 169 * 170 * If the node references a directory; no entries are allowed because 171 * their removal could need a recursive algorithm, something forbidden in 172 * kernel space. Furthermore, there is not need to provide such 173 * functionality (recursive removal) because the only primitives offered 174 * to the user are the removal of empty directories and the deletion of 175 * individual files. 176 * 177 * Note that nodes are not really deleted; in fact, when a node has been 178 * allocated, it cannot be deleted during the whole life of the file 179 * system. Instead, they are moved to the available list and remain there 180 * until reused. 181 */ 182void 183tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node) 184{ 185 size_t pages = 0; 186 187#ifdef INVARIANTS 188 TMPFS_NODE_LOCK(node); 189 MPASS(node->tn_vnode == NULL);
| 130 break; 131 132 case VFIFO: 133 /* FALLTHROUGH */ 134 case VSOCK: 135 break; 136 137 case VLNK: 138 MPASS(strlen(target) < MAXPATHLEN); 139 nnode->tn_size = strlen(target); 140 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME, 141 M_WAITOK); 142 memcpy(nnode->tn_link, target, nnode->tn_size); 143 break; 144 145 case VREG: 146 nnode->tn_reg.tn_aobj = 147 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0, 148 NULL /* XXXKIB - tmpfs needs swap reservation */); 149 nnode->tn_reg.tn_aobj_pages = 0; 150 break; 151 152 default: 153 panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type); 154 } 155 156 TMPFS_LOCK(tmp); 157 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries); 158 tmp->tm_nodes_inuse++; 159 TMPFS_UNLOCK(tmp); 160 161 *node = nnode; 162 return 0; 163} 164 165/* --------------------------------------------------------------------- */ 166 167/* 168 * Destroys the node pointed to by node from the file system 'tmp'. 169 * If the node does not belong to the given mount point, the results are 170 * unpredicted. 171 * 172 * If the node references a directory; no entries are allowed because 173 * their removal could need a recursive algorithm, something forbidden in 174 * kernel space. Furthermore, there is not need to provide such 175 * functionality (recursive removal) because the only primitives offered 176 * to the user are the removal of empty directories and the deletion of 177 * individual files. 178 * 179 * Note that nodes are not really deleted; in fact, when a node has been 180 * allocated, it cannot be deleted during the whole life of the file 181 * system. Instead, they are moved to the available list and remain there 182 * until reused. 183 */ 184void 185tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node) 186{ 187 size_t pages = 0; 188 189#ifdef INVARIANTS 190 TMPFS_NODE_LOCK(node); 191 MPASS(node->tn_vnode == NULL);
|
| 192 MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
|
190 TMPFS_NODE_UNLOCK(node); 191#endif 192 193 TMPFS_LOCK(tmp); 194 LIST_REMOVE(node, tn_entries); 195 tmp->tm_nodes_inuse--; 196 TMPFS_UNLOCK(tmp); 197 198 switch (node->tn_type) { 199 case VNON: 200 /* Do not do anything. VNON is provided to let the 201 * allocation routine clean itself easily by avoiding 202 * duplicating code in it. */ 203 /* FALLTHROUGH */ 204 case VBLK: 205 /* FALLTHROUGH */ 206 case VCHR: 207 /* FALLTHROUGH */ 208 case VDIR: 209 /* FALLTHROUGH */ 210 case VFIFO: 211 /* FALLTHROUGH */ 212 case VSOCK: 213 break; 214 215 case VLNK: 216 free(node->tn_link, M_TMPFSNAME); 217 break; 218 219 case VREG: 220 if (node->tn_reg.tn_aobj != NULL) 221 vm_object_deallocate(node->tn_reg.tn_aobj); 222 pages = node->tn_reg.tn_aobj_pages; 223 break; 224 225 default: 226 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type); 227 } 228 229 free_unr(tmp->tm_ino_unr, node->tn_id); 230 uma_zfree(tmp->tm_node_pool, node); 231 232 TMPFS_LOCK(tmp); 233 tmp->tm_pages_used -= pages; 234 TMPFS_UNLOCK(tmp); 235} 236 237/* --------------------------------------------------------------------- */ 238 239/* 240 * Allocates a new directory entry for the node node with a name of name. 241 * The new directory entry is returned in *de. 242 * 243 * The link count of node is increased by one to reflect the new object 244 * referencing it. 245 * 246 * Returns zero on success or an appropriate error code on failure. 247 */ 248int 249tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node, 250 const char *name, uint16_t len, struct tmpfs_dirent **de) 251{ 252 struct tmpfs_dirent *nde; 253 254 nde = (struct tmpfs_dirent *)uma_zalloc( 255 tmp->tm_dirent_pool, M_WAITOK); 256 nde->td_name = malloc(len, M_TMPFSNAME, M_WAITOK); 257 nde->td_namelen = len; 258 memcpy(nde->td_name, name, len); 259 260 nde->td_node = node; 261 node->tn_links++; 262 263 *de = nde; 264 265 return 0; 266} 267 268/* --------------------------------------------------------------------- */ 269 270/* 271 * Frees a directory entry. It is the caller's responsibility to destroy 272 * the node referenced by it if needed. 273 * 274 * The link count of node is decreased by one to reflect the removal of an 275 * object that referenced it. This only happens if 'node_exists' is true; 276 * otherwise the function will not access the node referred to by the 277 * directory entry, as it may already have been released from the outside. 278 */ 279void 280tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de, 281 boolean_t node_exists) 282{ 283 if (node_exists) { 284 struct tmpfs_node *node; 285 286 node = de->td_node; 287 288 MPASS(node->tn_links > 0); 289 node->tn_links--; 290 } 291 292 free(de->td_name, M_TMPFSNAME); 293 uma_zfree(tmp->tm_dirent_pool, de); 294} 295 296/* --------------------------------------------------------------------- */ 297 298/* 299 * Allocates a new vnode for the node node or returns a new reference to 300 * an existing one if the node had already a vnode referencing it. The 301 * resulting locked vnode is returned in *vpp. 302 * 303 * Returns zero on success or an appropriate error code on failure. 304 */ 305int 306tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag, 307 struct vnode **vpp) 308{ 309 int error = 0; 310 struct vnode *vp; 311 312loop: 313 TMPFS_NODE_LOCK(node); 314 if ((vp = node->tn_vnode) != NULL) {
| 193 TMPFS_NODE_UNLOCK(node); 194#endif 195 196 TMPFS_LOCK(tmp); 197 LIST_REMOVE(node, tn_entries); 198 tmp->tm_nodes_inuse--; 199 TMPFS_UNLOCK(tmp); 200 201 switch (node->tn_type) { 202 case VNON: 203 /* Do not do anything. VNON is provided to let the 204 * allocation routine clean itself easily by avoiding 205 * duplicating code in it. */ 206 /* FALLTHROUGH */ 207 case VBLK: 208 /* FALLTHROUGH */ 209 case VCHR: 210 /* FALLTHROUGH */ 211 case VDIR: 212 /* FALLTHROUGH */ 213 case VFIFO: 214 /* FALLTHROUGH */ 215 case VSOCK: 216 break; 217 218 case VLNK: 219 free(node->tn_link, M_TMPFSNAME); 220 break; 221 222 case VREG: 223 if (node->tn_reg.tn_aobj != NULL) 224 vm_object_deallocate(node->tn_reg.tn_aobj); 225 pages = node->tn_reg.tn_aobj_pages; 226 break; 227 228 default: 229 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type); 230 } 231 232 free_unr(tmp->tm_ino_unr, node->tn_id); 233 uma_zfree(tmp->tm_node_pool, node); 234 235 TMPFS_LOCK(tmp); 236 tmp->tm_pages_used -= pages; 237 TMPFS_UNLOCK(tmp); 238} 239 240/* --------------------------------------------------------------------- */ 241 242/* 243 * Allocates a new directory entry for the node node with a name of name. 244 * The new directory entry is returned in *de. 245 * 246 * The link count of node is increased by one to reflect the new object 247 * referencing it. 248 * 249 * Returns zero on success or an appropriate error code on failure. 250 */ 251int 252tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node, 253 const char *name, uint16_t len, struct tmpfs_dirent **de) 254{ 255 struct tmpfs_dirent *nde; 256 257 nde = (struct tmpfs_dirent *)uma_zalloc( 258 tmp->tm_dirent_pool, M_WAITOK); 259 nde->td_name = malloc(len, M_TMPFSNAME, M_WAITOK); 260 nde->td_namelen = len; 261 memcpy(nde->td_name, name, len); 262 263 nde->td_node = node; 264 node->tn_links++; 265 266 *de = nde; 267 268 return 0; 269} 270 271/* --------------------------------------------------------------------- */ 272 273/* 274 * Frees a directory entry. It is the caller's responsibility to destroy 275 * the node referenced by it if needed. 276 * 277 * The link count of node is decreased by one to reflect the removal of an 278 * object that referenced it. This only happens if 'node_exists' is true; 279 * otherwise the function will not access the node referred to by the 280 * directory entry, as it may already have been released from the outside. 281 */ 282void 283tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de, 284 boolean_t node_exists) 285{ 286 if (node_exists) { 287 struct tmpfs_node *node; 288 289 node = de->td_node; 290 291 MPASS(node->tn_links > 0); 292 node->tn_links--; 293 } 294 295 free(de->td_name, M_TMPFSNAME); 296 uma_zfree(tmp->tm_dirent_pool, de); 297} 298 299/* --------------------------------------------------------------------- */ 300 301/* 302 * Allocates a new vnode for the node node or returns a new reference to 303 * an existing one if the node had already a vnode referencing it. The 304 * resulting locked vnode is returned in *vpp. 305 * 306 * Returns zero on success or an appropriate error code on failure. 307 */ 308int 309tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag, 310 struct vnode **vpp) 311{ 312 int error = 0; 313 struct vnode *vp; 314 315loop: 316 TMPFS_NODE_LOCK(node); 317 if ((vp = node->tn_vnode) != NULL) {
|
| 318 MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
|
315 VI_LOCK(vp); 316 TMPFS_NODE_UNLOCK(node); 317 vholdl(vp); 318 (void) vget(vp, lkflag | LK_INTERLOCK | LK_RETRY, curthread); 319 vdrop(vp); 320 321 /* 322 * Make sure the vnode is still there after 323 * getting the interlock to avoid racing a free. 324 */ 325 if (node->tn_vnode == NULL || node->tn_vnode != vp) { 326 vput(vp); 327 goto loop; 328 } 329 330 goto out; 331 } 332
| 319 VI_LOCK(vp); 320 TMPFS_NODE_UNLOCK(node); 321 vholdl(vp); 322 (void) vget(vp, lkflag | LK_INTERLOCK | LK_RETRY, curthread); 323 vdrop(vp); 324 325 /* 326 * Make sure the vnode is still there after 327 * getting the interlock to avoid racing a free. 328 */ 329 if (node->tn_vnode == NULL || node->tn_vnode != vp) { 330 vput(vp); 331 goto loop; 332 } 333 334 goto out; 335 } 336
|
| 337 if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) || 338 (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) { 339 TMPFS_NODE_UNLOCK(node); 340 error = ENOENT; 341 vp = NULL; 342 goto out; 343 } 344
|
333 /* 334 * otherwise lock the vp list while we call getnewvnode 335 * since that can block. 336 */ 337 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) { 338 node->tn_vpstate |= TMPFS_VNODE_WANT; 339 error = msleep((caddr_t) &node->tn_vpstate, 340 TMPFS_NODE_MTX(node), PDROP | PCATCH, 341 "tmpfs_alloc_vp", 0); 342 if (error) 343 return error; 344 345 goto loop; 346 } else 347 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING; 348 349 TMPFS_NODE_UNLOCK(node); 350 351 /* Get a new vnode and associate it with our node. */ 352 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp); 353 if (error != 0) 354 goto unlock; 355 MPASS(vp != NULL); 356 357 (void) vn_lock(vp, lkflag | LK_RETRY); 358 359 vp->v_data = node; 360 vp->v_type = node->tn_type; 361 362 /* Type-specific initialization. */ 363 switch (node->tn_type) { 364 case VBLK: 365 /* FALLTHROUGH */ 366 case VCHR: 367 /* FALLTHROUGH */ 368 case VLNK: 369 /* FALLTHROUGH */ 370 case VREG: 371 /* FALLTHROUGH */ 372 case VSOCK: 373 break; 374 case VFIFO: 375 vp->v_op = &tmpfs_fifoop_entries; 376 break; 377 case VDIR:
| 345 /* 346 * otherwise lock the vp list while we call getnewvnode 347 * since that can block. 348 */ 349 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) { 350 node->tn_vpstate |= TMPFS_VNODE_WANT; 351 error = msleep((caddr_t) &node->tn_vpstate, 352 TMPFS_NODE_MTX(node), PDROP | PCATCH, 353 "tmpfs_alloc_vp", 0); 354 if (error) 355 return error; 356 357 goto loop; 358 } else 359 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING; 360 361 TMPFS_NODE_UNLOCK(node); 362 363 /* Get a new vnode and associate it with our node. */ 364 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp); 365 if (error != 0) 366 goto unlock; 367 MPASS(vp != NULL); 368 369 (void) vn_lock(vp, lkflag | LK_RETRY); 370 371 vp->v_data = node; 372 vp->v_type = node->tn_type; 373 374 /* Type-specific initialization. */ 375 switch (node->tn_type) { 376 case VBLK: 377 /* FALLTHROUGH */ 378 case VCHR: 379 /* FALLTHROUGH */ 380 case VLNK: 381 /* FALLTHROUGH */ 382 case VREG: 383 /* FALLTHROUGH */ 384 case VSOCK: 385 break; 386 case VFIFO: 387 vp->v_op = &tmpfs_fifoop_entries; 388 break; 389 case VDIR:
|
| 390 MPASS(node->tn_dir.tn_parent != NULL);
|
378 if (node->tn_dir.tn_parent == node) 379 vp->v_vflag |= VV_ROOT; 380 break; 381 382 default: 383 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type); 384 } 385 386 vnode_pager_setsize(vp, node->tn_size); 387 error = insmntque(vp, mp); 388 if (error) 389 vp = NULL; 390 391unlock: 392 TMPFS_NODE_LOCK(node); 393 394 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING); 395 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING; 396 node->tn_vnode = vp; 397 398 if (node->tn_vpstate & TMPFS_VNODE_WANT) { 399 node->tn_vpstate &= ~TMPFS_VNODE_WANT; 400 TMPFS_NODE_UNLOCK(node); 401 wakeup((caddr_t) &node->tn_vpstate); 402 } else 403 TMPFS_NODE_UNLOCK(node); 404 405out: 406 *vpp = vp; 407 408 MPASS(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp))); 409#ifdef INVARIANTS 410 TMPFS_NODE_LOCK(node); 411 MPASS(*vpp == node->tn_vnode); 412 TMPFS_NODE_UNLOCK(node); 413#endif 414 415 return error; 416} 417 418/* --------------------------------------------------------------------- */ 419 420/* 421 * Destroys the association between the vnode vp and the node it 422 * references. 423 */ 424void 425tmpfs_free_vp(struct vnode *vp) 426{ 427 struct tmpfs_node *node; 428 429 node = VP_TO_TMPFS_NODE(vp); 430
| 391 if (node->tn_dir.tn_parent == node) 392 vp->v_vflag |= VV_ROOT; 393 break; 394 395 default: 396 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type); 397 } 398 399 vnode_pager_setsize(vp, node->tn_size); 400 error = insmntque(vp, mp); 401 if (error) 402 vp = NULL; 403 404unlock: 405 TMPFS_NODE_LOCK(node); 406 407 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING); 408 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING; 409 node->tn_vnode = vp; 410 411 if (node->tn_vpstate & TMPFS_VNODE_WANT) { 412 node->tn_vpstate &= ~TMPFS_VNODE_WANT; 413 TMPFS_NODE_UNLOCK(node); 414 wakeup((caddr_t) &node->tn_vpstate); 415 } else 416 TMPFS_NODE_UNLOCK(node); 417 418out: 419 *vpp = vp; 420 421 MPASS(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp))); 422#ifdef INVARIANTS 423 TMPFS_NODE_LOCK(node); 424 MPASS(*vpp == node->tn_vnode); 425 TMPFS_NODE_UNLOCK(node); 426#endif 427 428 return error; 429} 430 431/* --------------------------------------------------------------------- */ 432 433/* 434 * Destroys the association between the vnode vp and the node it 435 * references. 436 */ 437void 438tmpfs_free_vp(struct vnode *vp) 439{ 440 struct tmpfs_node *node; 441 442 node = VP_TO_TMPFS_NODE(vp); 443
|
431 TMPFS_NODE_LOCK(node);
| 444 mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED);
|
432 node->tn_vnode = NULL; 433 vp->v_data = NULL;
| 445 node->tn_vnode = NULL; 446 vp->v_data = NULL;
|
434 TMPFS_NODE_UNLOCK(node);
| |
435} 436 437/* --------------------------------------------------------------------- */ 438 439/* 440 * Allocates a new file of type 'type' and adds it to the parent directory 441 * 'dvp'; this addition is done using the component name given in 'cnp'. 442 * The ownership of the new file is automatically assigned based on the 443 * credentials of the caller (through 'cnp'), the group is set based on 444 * the parent directory and the mode is determined from the 'vap' argument. 445 * If successful, *vpp holds a vnode to the newly created file and zero 446 * is returned. Otherwise *vpp is NULL and the function returns an 447 * appropriate error code. 448 */ 449int 450tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, 451 struct componentname *cnp, char *target) 452{ 453 int error; 454 struct tmpfs_dirent *de; 455 struct tmpfs_mount *tmp; 456 struct tmpfs_node *dnode; 457 struct tmpfs_node *node; 458 struct tmpfs_node *parent; 459 460 MPASS(VOP_ISLOCKED(dvp)); 461 MPASS(cnp->cn_flags & HASBUF); 462 463 tmp = VFS_TO_TMPFS(dvp->v_mount); 464 dnode = VP_TO_TMPFS_DIR(dvp); 465 *vpp = NULL; 466 467 /* If the entry we are creating is a directory, we cannot overflow 468 * the number of links of its parent, because it will get a new 469 * link. */ 470 if (vap->va_type == VDIR) { 471 /* Ensure that we do not overflow the maximum number of links 472 * imposed by the system. */ 473 MPASS(dnode->tn_links <= LINK_MAX); 474 if (dnode->tn_links == LINK_MAX) { 475 error = EMLINK; 476 goto out; 477 } 478 479 parent = dnode; 480 MPASS(parent != NULL); 481 } else 482 parent = NULL; 483 484 /* Allocate a node that represents the new file. */ 485 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid, 486 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node); 487 if (error != 0) 488 goto out; 489 490 /* Allocate a directory entry that points to the new file. */ 491 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen, 492 &de); 493 if (error != 0) { 494 tmpfs_free_node(tmp, node); 495 goto out; 496 } 497 498 /* Allocate a vnode for the new file. */ 499 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp); 500 if (error != 0) { 501 tmpfs_free_dirent(tmp, de, TRUE); 502 tmpfs_free_node(tmp, node); 503 goto out; 504 } 505 506 /* Now that all required items are allocated, we can proceed to 507 * insert the new node into the directory, an operation that 508 * cannot fail. */ 509 tmpfs_dir_attach(dvp, de); 510 511out: 512 513 return error; 514} 515 516/* --------------------------------------------------------------------- */ 517 518/* 519 * Attaches the directory entry de to the directory represented by vp. 520 * Note that this does not change the link count of the node pointed by 521 * the directory entry, as this is done by tmpfs_alloc_dirent. 522 */ 523void 524tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de) 525{ 526 struct tmpfs_node *dnode; 527 528 ASSERT_VOP_ELOCKED(vp, __func__); 529 dnode = VP_TO_TMPFS_DIR(vp); 530 TAILQ_INSERT_TAIL(&dnode->tn_dir.tn_dirhead, de, td_entries); 531 dnode->tn_size += sizeof(struct tmpfs_dirent); 532 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 533 TMPFS_NODE_MODIFIED; 534} 535 536/* --------------------------------------------------------------------- */ 537 538/* 539 * Detaches the directory entry de from the directory represented by vp. 540 * Note that this does not change the link count of the node pointed by 541 * the directory entry, as this is done by tmpfs_free_dirent. 542 */ 543void 544tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de) 545{ 546 struct tmpfs_node *dnode; 547 548 ASSERT_VOP_ELOCKED(vp, __func__); 549 dnode = VP_TO_TMPFS_DIR(vp); 550 551 if (dnode->tn_dir.tn_readdir_lastp == de) { 552 dnode->tn_dir.tn_readdir_lastn = 0; 553 dnode->tn_dir.tn_readdir_lastp = NULL; 554 } 555 556 TAILQ_REMOVE(&dnode->tn_dir.tn_dirhead, de, td_entries); 557 dnode->tn_size -= sizeof(struct tmpfs_dirent); 558 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 559 TMPFS_NODE_MODIFIED; 560} 561 562/* --------------------------------------------------------------------- */ 563 564/* 565 * Looks for a directory entry in the directory represented by node. 566 * 'cnp' describes the name of the entry to look for. Note that the . 567 * and .. components are not allowed as they do not physically exist 568 * within directories. 569 * 570 * Returns a pointer to the entry when found, otherwise NULL. 571 */ 572struct tmpfs_dirent * 573tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f, 574 struct componentname *cnp) 575{ 576 boolean_t found; 577 struct tmpfs_dirent *de; 578 579 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.')); 580 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' && 581 cnp->cn_nameptr[1] == '.'))); 582 TMPFS_VALIDATE_DIR(node); 583 584 found = 0; 585 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) { 586 if (f != NULL && de->td_node != f) 587 continue; 588 MPASS(cnp->cn_namelen < 0xffff); 589 if (de->td_namelen == (uint16_t)cnp->cn_namelen && 590 bcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) { 591 found = 1; 592 break; 593 } 594 } 595 node->tn_status |= TMPFS_NODE_ACCESSED; 596 597 return found ? de : NULL; 598} 599 600/* --------------------------------------------------------------------- */ 601 602/* 603 * Helper function for tmpfs_readdir. Creates a '.' entry for the given 604 * directory and returns it in the uio space. The function returns 0 605 * on success, -1 if there was not enough space in the uio structure to 606 * hold the directory entry or an appropriate error code if another 607 * error happens. 608 */ 609int 610tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio) 611{ 612 int error; 613 struct dirent dent; 614 615 TMPFS_VALIDATE_DIR(node); 616 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT); 617 618 dent.d_fileno = node->tn_id; 619 dent.d_type = DT_DIR; 620 dent.d_namlen = 1; 621 dent.d_name[0] = '.'; 622 dent.d_name[1] = '\0'; 623 dent.d_reclen = GENERIC_DIRSIZ(&dent); 624 625 if (dent.d_reclen > uio->uio_resid) 626 error = -1; 627 else { 628 error = uiomove(&dent, dent.d_reclen, uio); 629 if (error == 0) 630 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT; 631 } 632 633 node->tn_status |= TMPFS_NODE_ACCESSED; 634 635 return error; 636} 637 638/* --------------------------------------------------------------------- */ 639 640/* 641 * Helper function for tmpfs_readdir. Creates a '..' entry for the given 642 * directory and returns it in the uio space. The function returns 0 643 * on success, -1 if there was not enough space in the uio structure to 644 * hold the directory entry or an appropriate error code if another 645 * error happens. 646 */ 647int 648tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio) 649{ 650 int error; 651 struct dirent dent; 652 653 TMPFS_VALIDATE_DIR(node); 654 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT); 655
| 447} 448 449/* --------------------------------------------------------------------- */ 450 451/* 452 * Allocates a new file of type 'type' and adds it to the parent directory 453 * 'dvp'; this addition is done using the component name given in 'cnp'. 454 * The ownership of the new file is automatically assigned based on the 455 * credentials of the caller (through 'cnp'), the group is set based on 456 * the parent directory and the mode is determined from the 'vap' argument. 457 * If successful, *vpp holds a vnode to the newly created file and zero 458 * is returned. Otherwise *vpp is NULL and the function returns an 459 * appropriate error code. 460 */ 461int 462tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, 463 struct componentname *cnp, char *target) 464{ 465 int error; 466 struct tmpfs_dirent *de; 467 struct tmpfs_mount *tmp; 468 struct tmpfs_node *dnode; 469 struct tmpfs_node *node; 470 struct tmpfs_node *parent; 471 472 MPASS(VOP_ISLOCKED(dvp)); 473 MPASS(cnp->cn_flags & HASBUF); 474 475 tmp = VFS_TO_TMPFS(dvp->v_mount); 476 dnode = VP_TO_TMPFS_DIR(dvp); 477 *vpp = NULL; 478 479 /* If the entry we are creating is a directory, we cannot overflow 480 * the number of links of its parent, because it will get a new 481 * link. */ 482 if (vap->va_type == VDIR) { 483 /* Ensure that we do not overflow the maximum number of links 484 * imposed by the system. */ 485 MPASS(dnode->tn_links <= LINK_MAX); 486 if (dnode->tn_links == LINK_MAX) { 487 error = EMLINK; 488 goto out; 489 } 490 491 parent = dnode; 492 MPASS(parent != NULL); 493 } else 494 parent = NULL; 495 496 /* Allocate a node that represents the new file. */ 497 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid, 498 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node); 499 if (error != 0) 500 goto out; 501 502 /* Allocate a directory entry that points to the new file. */ 503 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen, 504 &de); 505 if (error != 0) { 506 tmpfs_free_node(tmp, node); 507 goto out; 508 } 509 510 /* Allocate a vnode for the new file. */ 511 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp); 512 if (error != 0) { 513 tmpfs_free_dirent(tmp, de, TRUE); 514 tmpfs_free_node(tmp, node); 515 goto out; 516 } 517 518 /* Now that all required items are allocated, we can proceed to 519 * insert the new node into the directory, an operation that 520 * cannot fail. */ 521 tmpfs_dir_attach(dvp, de); 522 523out: 524 525 return error; 526} 527 528/* --------------------------------------------------------------------- */ 529 530/* 531 * Attaches the directory entry de to the directory represented by vp. 532 * Note that this does not change the link count of the node pointed by 533 * the directory entry, as this is done by tmpfs_alloc_dirent. 534 */ 535void 536tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de) 537{ 538 struct tmpfs_node *dnode; 539 540 ASSERT_VOP_ELOCKED(vp, __func__); 541 dnode = VP_TO_TMPFS_DIR(vp); 542 TAILQ_INSERT_TAIL(&dnode->tn_dir.tn_dirhead, de, td_entries); 543 dnode->tn_size += sizeof(struct tmpfs_dirent); 544 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 545 TMPFS_NODE_MODIFIED; 546} 547 548/* --------------------------------------------------------------------- */ 549 550/* 551 * Detaches the directory entry de from the directory represented by vp. 552 * Note that this does not change the link count of the node pointed by 553 * the directory entry, as this is done by tmpfs_free_dirent. 554 */ 555void 556tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de) 557{ 558 struct tmpfs_node *dnode; 559 560 ASSERT_VOP_ELOCKED(vp, __func__); 561 dnode = VP_TO_TMPFS_DIR(vp); 562 563 if (dnode->tn_dir.tn_readdir_lastp == de) { 564 dnode->tn_dir.tn_readdir_lastn = 0; 565 dnode->tn_dir.tn_readdir_lastp = NULL; 566 } 567 568 TAILQ_REMOVE(&dnode->tn_dir.tn_dirhead, de, td_entries); 569 dnode->tn_size -= sizeof(struct tmpfs_dirent); 570 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 571 TMPFS_NODE_MODIFIED; 572} 573 574/* --------------------------------------------------------------------- */ 575 576/* 577 * Looks for a directory entry in the directory represented by node. 578 * 'cnp' describes the name of the entry to look for. Note that the . 579 * and .. components are not allowed as they do not physically exist 580 * within directories. 581 * 582 * Returns a pointer to the entry when found, otherwise NULL. 583 */ 584struct tmpfs_dirent * 585tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f, 586 struct componentname *cnp) 587{ 588 boolean_t found; 589 struct tmpfs_dirent *de; 590 591 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.')); 592 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' && 593 cnp->cn_nameptr[1] == '.'))); 594 TMPFS_VALIDATE_DIR(node); 595 596 found = 0; 597 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) { 598 if (f != NULL && de->td_node != f) 599 continue; 600 MPASS(cnp->cn_namelen < 0xffff); 601 if (de->td_namelen == (uint16_t)cnp->cn_namelen && 602 bcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) { 603 found = 1; 604 break; 605 } 606 } 607 node->tn_status |= TMPFS_NODE_ACCESSED; 608 609 return found ? de : NULL; 610} 611 612/* --------------------------------------------------------------------- */ 613 614/* 615 * Helper function for tmpfs_readdir. Creates a '.' entry for the given 616 * directory and returns it in the uio space. The function returns 0 617 * on success, -1 if there was not enough space in the uio structure to 618 * hold the directory entry or an appropriate error code if another 619 * error happens. 620 */ 621int 622tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio) 623{ 624 int error; 625 struct dirent dent; 626 627 TMPFS_VALIDATE_DIR(node); 628 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT); 629 630 dent.d_fileno = node->tn_id; 631 dent.d_type = DT_DIR; 632 dent.d_namlen = 1; 633 dent.d_name[0] = '.'; 634 dent.d_name[1] = '\0'; 635 dent.d_reclen = GENERIC_DIRSIZ(&dent); 636 637 if (dent.d_reclen > uio->uio_resid) 638 error = -1; 639 else { 640 error = uiomove(&dent, dent.d_reclen, uio); 641 if (error == 0) 642 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT; 643 } 644 645 node->tn_status |= TMPFS_NODE_ACCESSED; 646 647 return error; 648} 649 650/* --------------------------------------------------------------------- */ 651 652/* 653 * Helper function for tmpfs_readdir. Creates a '..' entry for the given 654 * directory and returns it in the uio space. The function returns 0 655 * on success, -1 if there was not enough space in the uio structure to 656 * hold the directory entry or an appropriate error code if another 657 * error happens. 658 */ 659int 660tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio) 661{ 662 int error; 663 struct dirent dent; 664 665 TMPFS_VALIDATE_DIR(node); 666 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT); 667
|
| 668 /* 669 * Return ENOENT if the current node is already removed. 670 */ 671 TMPFS_ASSERT_LOCKED(node); 672 if (node->tn_dir.tn_parent == NULL) { 673 return (ENOENT); 674 } 675 676 TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
|
656 dent.d_fileno = node->tn_dir.tn_parent->tn_id;
| 677 dent.d_fileno = node->tn_dir.tn_parent->tn_id;
|
| 678 TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent); 679
|
657 dent.d_type = DT_DIR; 658 dent.d_namlen = 2; 659 dent.d_name[0] = '.'; 660 dent.d_name[1] = '.'; 661 dent.d_name[2] = '\0'; 662 dent.d_reclen = GENERIC_DIRSIZ(&dent); 663 664 if (dent.d_reclen > uio->uio_resid) 665 error = -1; 666 else { 667 error = uiomove(&dent, dent.d_reclen, uio); 668 if (error == 0) { 669 struct tmpfs_dirent *de; 670 671 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead); 672 if (de == NULL) 673 uio->uio_offset = TMPFS_DIRCOOKIE_EOF; 674 else 675 uio->uio_offset = tmpfs_dircookie(de); 676 } 677 } 678 679 node->tn_status |= TMPFS_NODE_ACCESSED; 680 681 return error; 682} 683 684/* --------------------------------------------------------------------- */ 685 686/* 687 * Lookup a directory entry by its associated cookie. 688 */ 689struct tmpfs_dirent * 690tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie) 691{ 692 struct tmpfs_dirent *de; 693 694 if (cookie == node->tn_dir.tn_readdir_lastn && 695 node->tn_dir.tn_readdir_lastp != NULL) { 696 return node->tn_dir.tn_readdir_lastp; 697 } 698 699 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) { 700 if (tmpfs_dircookie(de) == cookie) { 701 break; 702 } 703 } 704 705 return de; 706} 707 708/* --------------------------------------------------------------------- */ 709 710/* 711 * Helper function for tmpfs_readdir. Returns as much directory entries 712 * as can fit in the uio space. The read starts at uio->uio_offset. 713 * The function returns 0 on success, -1 if there was not enough space 714 * in the uio structure to hold the directory entry or an appropriate 715 * error code if another error happens. 716 */ 717int 718tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp) 719{ 720 int error; 721 off_t startcookie; 722 struct tmpfs_dirent *de; 723 724 TMPFS_VALIDATE_DIR(node); 725 726 /* Locate the first directory entry we have to return. We have cached 727 * the last readdir in the node, so use those values if appropriate. 728 * Otherwise do a linear scan to find the requested entry. */ 729 startcookie = uio->uio_offset; 730 MPASS(startcookie != TMPFS_DIRCOOKIE_DOT); 731 MPASS(startcookie != TMPFS_DIRCOOKIE_DOTDOT); 732 if (startcookie == TMPFS_DIRCOOKIE_EOF) { 733 return 0; 734 } else { 735 de = tmpfs_dir_lookupbycookie(node, startcookie); 736 } 737 if (de == NULL) { 738 return EINVAL; 739 } 740 741 /* Read as much entries as possible; i.e., until we reach the end of 742 * the directory or we exhaust uio space. */ 743 do { 744 struct dirent d; 745 746 /* Create a dirent structure representing the current 747 * tmpfs_node and fill it. */ 748 d.d_fileno = de->td_node->tn_id; 749 switch (de->td_node->tn_type) { 750 case VBLK: 751 d.d_type = DT_BLK; 752 break; 753 754 case VCHR: 755 d.d_type = DT_CHR; 756 break; 757 758 case VDIR: 759 d.d_type = DT_DIR; 760 break; 761 762 case VFIFO: 763 d.d_type = DT_FIFO; 764 break; 765 766 case VLNK: 767 d.d_type = DT_LNK; 768 break; 769 770 case VREG: 771 d.d_type = DT_REG; 772 break; 773 774 case VSOCK: 775 d.d_type = DT_SOCK; 776 break; 777 778 default: 779 panic("tmpfs_dir_getdents: type %p %d", 780 de->td_node, (int)de->td_node->tn_type); 781 } 782 d.d_namlen = de->td_namelen; 783 MPASS(de->td_namelen < sizeof(d.d_name)); 784 (void)memcpy(d.d_name, de->td_name, de->td_namelen); 785 d.d_name[de->td_namelen] = '\0'; 786 d.d_reclen = GENERIC_DIRSIZ(&d); 787 788 /* Stop reading if the directory entry we are treating is 789 * bigger than the amount of data that can be returned. */ 790 if (d.d_reclen > uio->uio_resid) { 791 error = -1; 792 break; 793 } 794 795 /* Copy the new dirent structure into the output buffer and 796 * advance pointers. */ 797 error = uiomove(&d, d.d_reclen, uio); 798 799 (*cntp)++; 800 de = TAILQ_NEXT(de, td_entries); 801 } while (error == 0 && uio->uio_resid > 0 && de != NULL); 802 803 /* Update the offset and cache. */ 804 if (de == NULL) { 805 uio->uio_offset = TMPFS_DIRCOOKIE_EOF; 806 node->tn_dir.tn_readdir_lastn = 0; 807 node->tn_dir.tn_readdir_lastp = NULL; 808 } else { 809 node->tn_dir.tn_readdir_lastn = uio->uio_offset = tmpfs_dircookie(de); 810 node->tn_dir.tn_readdir_lastp = de; 811 } 812 813 node->tn_status |= TMPFS_NODE_ACCESSED; 814 return error; 815} 816 817/* --------------------------------------------------------------------- */ 818 819/* 820 * Resizes the aobj associated to the regular file pointed to by vp to 821 * the size newsize. 'vp' must point to a vnode that represents a regular 822 * file. 'newsize' must be positive. 823 * 824 * Returns zero on success or an appropriate error code on failure. 825 */ 826int 827tmpfs_reg_resize(struct vnode *vp, off_t newsize) 828{ 829 int error; 830 size_t newpages, oldpages; 831 struct tmpfs_mount *tmp; 832 struct tmpfs_node *node; 833 off_t oldsize; 834 835 MPASS(vp->v_type == VREG); 836 MPASS(newsize >= 0); 837 838 node = VP_TO_TMPFS_NODE(vp); 839 tmp = VFS_TO_TMPFS(vp->v_mount); 840 841 /* Convert the old and new sizes to the number of pages needed to 842 * store them. It may happen that we do not need to do anything 843 * because the last allocated page can accommodate the change on 844 * its own. */ 845 oldsize = node->tn_size; 846 oldpages = round_page(oldsize) / PAGE_SIZE; 847 MPASS(oldpages == node->tn_reg.tn_aobj_pages); 848 newpages = round_page(newsize) / PAGE_SIZE; 849 850 if (newpages > oldpages && 851 newpages - oldpages > TMPFS_PAGES_AVAIL(tmp)) { 852 error = ENOSPC; 853 goto out; 854 } 855 856 node->tn_reg.tn_aobj_pages = newpages; 857 858 TMPFS_LOCK(tmp); 859 tmp->tm_pages_used += (newpages - oldpages); 860 TMPFS_UNLOCK(tmp); 861 862 node->tn_size = newsize; 863 vnode_pager_setsize(vp, newsize); 864 if (newsize < oldsize) { 865 size_t zerolen = round_page(newsize) - newsize; 866 vm_object_t uobj = node->tn_reg.tn_aobj; 867 vm_page_t m; 868 869 /* 870 * free "backing store" 871 */ 872 VM_OBJECT_LOCK(uobj); 873 if (newpages < oldpages) { 874 swap_pager_freespace(uobj, 875 newpages, oldpages - newpages); 876 vm_object_page_remove(uobj, 877 OFF_TO_IDX(newsize + PAGE_MASK), 0, FALSE); 878 } 879 880 /* 881 * zero out the truncated part of the last page. 882 */ 883 884 if (zerolen > 0) { 885 m = vm_page_grab(uobj, OFF_TO_IDX(newsize), 886 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 887 pmap_zero_page_area(m, PAGE_SIZE - zerolen, 888 zerolen); 889 vm_page_wakeup(m); 890 } 891 VM_OBJECT_UNLOCK(uobj); 892 893 } 894 895 error = 0; 896 897out: 898 return error; 899} 900 901/* --------------------------------------------------------------------- */ 902 903/* 904 * Change flags of the given vnode. 905 * Caller should execute tmpfs_update on vp after a successful execution. 906 * The vnode must be locked on entry and remain locked on exit. 907 */ 908int 909tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct thread *p) 910{ 911 int error; 912 struct tmpfs_node *node; 913 914 MPASS(VOP_ISLOCKED(vp)); 915 916 node = VP_TO_TMPFS_NODE(vp); 917 918 /* Disallow this operation if the file system is mounted read-only. */ 919 if (vp->v_mount->mnt_flag & MNT_RDONLY) 920 return EROFS; 921 922 /* 923 * Callers may only modify the file flags on objects they 924 * have VADMIN rights for. 925 */ 926 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 927 return (error); 928 /* 929 * Unprivileged processes are not permitted to unset system 930 * flags, or modify flags if any system flags are set. 931 */ 932 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) { 933 if (node->tn_flags 934 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) { 935 error = securelevel_gt(cred, 0); 936 if (error) 937 return (error); 938 } 939 /* Snapshot flag cannot be set or cleared */ 940 if (((flags & SF_SNAPSHOT) != 0 && 941 (node->tn_flags & SF_SNAPSHOT) == 0) || 942 ((flags & SF_SNAPSHOT) == 0 && 943 (node->tn_flags & SF_SNAPSHOT) != 0)) 944 return (EPERM); 945 node->tn_flags = flags; 946 } else { 947 if (node->tn_flags 948 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) || 949 (flags & UF_SETTABLE) != flags) 950 return (EPERM); 951 node->tn_flags &= SF_SETTABLE; 952 node->tn_flags |= (flags & UF_SETTABLE); 953 } 954 node->tn_status |= TMPFS_NODE_CHANGED; 955 956 MPASS(VOP_ISLOCKED(vp)); 957 958 return 0; 959} 960 961/* --------------------------------------------------------------------- */ 962 963/* 964 * Change access mode on the given vnode. 965 * Caller should execute tmpfs_update on vp after a successful execution. 966 * The vnode must be locked on entry and remain locked on exit. 967 */ 968int 969tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p) 970{ 971 int error; 972 struct tmpfs_node *node; 973 974 MPASS(VOP_ISLOCKED(vp)); 975 976 node = VP_TO_TMPFS_NODE(vp); 977 978 /* Disallow this operation if the file system is mounted read-only. */ 979 if (vp->v_mount->mnt_flag & MNT_RDONLY) 980 return EROFS; 981 982 /* Immutable or append-only files cannot be modified, either. */ 983 if (node->tn_flags & (IMMUTABLE | APPEND)) 984 return EPERM; 985 986 /* 987 * To modify the permissions on a file, must possess VADMIN 988 * for that file. 989 */ 990 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 991 return (error); 992 993 /* 994 * Privileged processes may set the sticky bit on non-directories, 995 * as well as set the setgid bit on a file with a group that the 996 * process is not a member of. 997 */ 998 if (vp->v_type != VDIR && (mode & S_ISTXT)) { 999 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0)) 1000 return (EFTYPE); 1001 } 1002 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) { 1003 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0); 1004 if (error) 1005 return (error); 1006 } 1007 1008 1009 node->tn_mode &= ~ALLPERMS; 1010 node->tn_mode |= mode & ALLPERMS; 1011 1012 node->tn_status |= TMPFS_NODE_CHANGED; 1013 1014 MPASS(VOP_ISLOCKED(vp)); 1015 1016 return 0; 1017} 1018 1019/* --------------------------------------------------------------------- */ 1020 1021/* 1022 * Change ownership of the given vnode. At least one of uid or gid must 1023 * be different than VNOVAL. If one is set to that value, the attribute 1024 * is unchanged. 1025 * Caller should execute tmpfs_update on vp after a successful execution. 1026 * The vnode must be locked on entry and remain locked on exit. 1027 */ 1028int 1029tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred, 1030 struct thread *p) 1031{ 1032 int error; 1033 struct tmpfs_node *node; 1034 uid_t ouid; 1035 gid_t ogid; 1036 1037 MPASS(VOP_ISLOCKED(vp)); 1038 1039 node = VP_TO_TMPFS_NODE(vp); 1040 1041 /* Assign default values if they are unknown. */ 1042 MPASS(uid != VNOVAL || gid != VNOVAL); 1043 if (uid == VNOVAL) 1044 uid = node->tn_uid; 1045 if (gid == VNOVAL) 1046 gid = node->tn_gid; 1047 MPASS(uid != VNOVAL && gid != VNOVAL); 1048 1049 /* Disallow this operation if the file system is mounted read-only. */ 1050 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1051 return EROFS; 1052 1053 /* Immutable or append-only files cannot be modified, either. */ 1054 if (node->tn_flags & (IMMUTABLE | APPEND)) 1055 return EPERM; 1056 1057 /* 1058 * To modify the ownership of a file, must possess VADMIN for that 1059 * file. 1060 */ 1061 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 1062 return (error); 1063 1064 /* 1065 * To change the owner of a file, or change the group of a file to a 1066 * group of which we are not a member, the caller must have 1067 * privilege. 1068 */ 1069 if ((uid != node->tn_uid || 1070 (gid != node->tn_gid && !groupmember(gid, cred))) && 1071 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0))) 1072 return (error); 1073 1074 ogid = node->tn_gid; 1075 ouid = node->tn_uid; 1076 1077 node->tn_uid = uid; 1078 node->tn_gid = gid; 1079 1080 node->tn_status |= TMPFS_NODE_CHANGED; 1081 1082 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) { 1083 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) 1084 node->tn_mode &= ~(S_ISUID | S_ISGID); 1085 } 1086 1087 MPASS(VOP_ISLOCKED(vp)); 1088 1089 return 0; 1090} 1091 1092/* --------------------------------------------------------------------- */ 1093 1094/* 1095 * Change size of the given vnode. 1096 * Caller should execute tmpfs_update on vp after a successful execution. 1097 * The vnode must be locked on entry and remain locked on exit. 1098 */ 1099int 1100tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred, 1101 struct thread *p) 1102{ 1103 int error; 1104 struct tmpfs_node *node; 1105 1106 MPASS(VOP_ISLOCKED(vp)); 1107 1108 node = VP_TO_TMPFS_NODE(vp); 1109 1110 /* Decide whether this is a valid operation based on the file type. */ 1111 error = 0; 1112 switch (vp->v_type) { 1113 case VDIR: 1114 return EISDIR; 1115 1116 case VREG: 1117 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1118 return EROFS; 1119 break; 1120 1121 case VBLK: 1122 /* FALLTHROUGH */ 1123 case VCHR: 1124 /* FALLTHROUGH */ 1125 case VFIFO: 1126 /* Allow modifications of special files even if in the file 1127 * system is mounted read-only (we are not modifying the 1128 * files themselves, but the objects they represent). */ 1129 return 0; 1130 1131 default: 1132 /* Anything else is unsupported. */ 1133 return EOPNOTSUPP; 1134 } 1135 1136 /* Immutable or append-only files cannot be modified, either. */ 1137 if (node->tn_flags & (IMMUTABLE | APPEND)) 1138 return EPERM; 1139 1140 error = tmpfs_truncate(vp, size); 1141 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents 1142 * for us, as will update tn_status; no need to do that here. */ 1143 1144 MPASS(VOP_ISLOCKED(vp)); 1145 1146 return error; 1147} 1148 1149/* --------------------------------------------------------------------- */ 1150 1151/* 1152 * Change access and modification times of the given vnode. 1153 * Caller should execute tmpfs_update on vp after a successful execution. 1154 * The vnode must be locked on entry and remain locked on exit. 1155 */ 1156int 1157tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime, 1158 struct timespec *birthtime, int vaflags, struct ucred *cred, struct thread *l) 1159{ 1160 int error; 1161 struct tmpfs_node *node; 1162 1163 MPASS(VOP_ISLOCKED(vp)); 1164 1165 node = VP_TO_TMPFS_NODE(vp); 1166 1167 /* Disallow this operation if the file system is mounted read-only. */ 1168 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1169 return EROFS; 1170 1171 /* Immutable or append-only files cannot be modified, either. */ 1172 if (node->tn_flags & (IMMUTABLE | APPEND)) 1173 return EPERM; 1174 1175 /* Determine if the user have proper privilege to update time. */ 1176 if (vaflags & VA_UTIMES_NULL) { 1177 error = VOP_ACCESS(vp, VADMIN, cred, l); 1178 if (error) 1179 error = VOP_ACCESS(vp, VWRITE, cred, l); 1180 } else 1181 error = VOP_ACCESS(vp, VADMIN, cred, l); 1182 if (error) 1183 return (error); 1184 1185 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL) 1186 node->tn_status |= TMPFS_NODE_ACCESSED; 1187 1188 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL) 1189 node->tn_status |= TMPFS_NODE_MODIFIED; 1190 1191 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL) 1192 node->tn_status |= TMPFS_NODE_MODIFIED; 1193 1194 tmpfs_itimes(vp, atime, mtime); 1195 1196 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL) 1197 node->tn_birthtime = *birthtime; 1198 MPASS(VOP_ISLOCKED(vp)); 1199 1200 return 0; 1201} 1202 1203/* --------------------------------------------------------------------- */ 1204/* Sync timestamps */ 1205void 1206tmpfs_itimes(struct vnode *vp, const struct timespec *acc, 1207 const struct timespec *mod) 1208{ 1209 struct tmpfs_node *node; 1210 struct timespec now; 1211 1212 node = VP_TO_TMPFS_NODE(vp); 1213 1214 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 1215 TMPFS_NODE_CHANGED)) == 0) 1216 return; 1217 1218 vfs_timestamp(&now); 1219 if (node->tn_status & TMPFS_NODE_ACCESSED) { 1220 if (acc == NULL) 1221 acc = &now; 1222 node->tn_atime = *acc; 1223 } 1224 if (node->tn_status & TMPFS_NODE_MODIFIED) { 1225 if (mod == NULL) 1226 mod = &now; 1227 node->tn_mtime = *mod; 1228 } 1229 if (node->tn_status & TMPFS_NODE_CHANGED) { 1230 node->tn_ctime = now; 1231 } 1232 node->tn_status &= 1233 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED); 1234} 1235 1236/* --------------------------------------------------------------------- */ 1237 1238void 1239tmpfs_update(struct vnode *vp) 1240{ 1241 1242 tmpfs_itimes(vp, NULL, NULL); 1243} 1244 1245/* --------------------------------------------------------------------- */ 1246 1247int 1248tmpfs_truncate(struct vnode *vp, off_t length) 1249{ 1250 int error; 1251 struct tmpfs_node *node; 1252 1253 node = VP_TO_TMPFS_NODE(vp); 1254 1255 if (length < 0) { 1256 error = EINVAL; 1257 goto out; 1258 } 1259 1260 if (node->tn_size == length) { 1261 error = 0; 1262 goto out; 1263 } 1264 1265 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 1266 return (EFBIG); 1267 1268 error = tmpfs_reg_resize(vp, length); 1269 if (error == 0) { 1270 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1271 } 1272 1273out: 1274 tmpfs_update(vp); 1275 1276 return error; 1277}
| 680 dent.d_type = DT_DIR; 681 dent.d_namlen = 2; 682 dent.d_name[0] = '.'; 683 dent.d_name[1] = '.'; 684 dent.d_name[2] = '\0'; 685 dent.d_reclen = GENERIC_DIRSIZ(&dent); 686 687 if (dent.d_reclen > uio->uio_resid) 688 error = -1; 689 else { 690 error = uiomove(&dent, dent.d_reclen, uio); 691 if (error == 0) { 692 struct tmpfs_dirent *de; 693 694 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead); 695 if (de == NULL) 696 uio->uio_offset = TMPFS_DIRCOOKIE_EOF; 697 else 698 uio->uio_offset = tmpfs_dircookie(de); 699 } 700 } 701 702 node->tn_status |= TMPFS_NODE_ACCESSED; 703 704 return error; 705} 706 707/* --------------------------------------------------------------------- */ 708 709/* 710 * Lookup a directory entry by its associated cookie. 711 */ 712struct tmpfs_dirent * 713tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie) 714{ 715 struct tmpfs_dirent *de; 716 717 if (cookie == node->tn_dir.tn_readdir_lastn && 718 node->tn_dir.tn_readdir_lastp != NULL) { 719 return node->tn_dir.tn_readdir_lastp; 720 } 721 722 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) { 723 if (tmpfs_dircookie(de) == cookie) { 724 break; 725 } 726 } 727 728 return de; 729} 730 731/* --------------------------------------------------------------------- */ 732 733/* 734 * Helper function for tmpfs_readdir. Returns as much directory entries 735 * as can fit in the uio space. The read starts at uio->uio_offset. 736 * The function returns 0 on success, -1 if there was not enough space 737 * in the uio structure to hold the directory entry or an appropriate 738 * error code if another error happens. 739 */ 740int 741tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp) 742{ 743 int error; 744 off_t startcookie; 745 struct tmpfs_dirent *de; 746 747 TMPFS_VALIDATE_DIR(node); 748 749 /* Locate the first directory entry we have to return. We have cached 750 * the last readdir in the node, so use those values if appropriate. 751 * Otherwise do a linear scan to find the requested entry. */ 752 startcookie = uio->uio_offset; 753 MPASS(startcookie != TMPFS_DIRCOOKIE_DOT); 754 MPASS(startcookie != TMPFS_DIRCOOKIE_DOTDOT); 755 if (startcookie == TMPFS_DIRCOOKIE_EOF) { 756 return 0; 757 } else { 758 de = tmpfs_dir_lookupbycookie(node, startcookie); 759 } 760 if (de == NULL) { 761 return EINVAL; 762 } 763 764 /* Read as much entries as possible; i.e., until we reach the end of 765 * the directory or we exhaust uio space. */ 766 do { 767 struct dirent d; 768 769 /* Create a dirent structure representing the current 770 * tmpfs_node and fill it. */ 771 d.d_fileno = de->td_node->tn_id; 772 switch (de->td_node->tn_type) { 773 case VBLK: 774 d.d_type = DT_BLK; 775 break; 776 777 case VCHR: 778 d.d_type = DT_CHR; 779 break; 780 781 case VDIR: 782 d.d_type = DT_DIR; 783 break; 784 785 case VFIFO: 786 d.d_type = DT_FIFO; 787 break; 788 789 case VLNK: 790 d.d_type = DT_LNK; 791 break; 792 793 case VREG: 794 d.d_type = DT_REG; 795 break; 796 797 case VSOCK: 798 d.d_type = DT_SOCK; 799 break; 800 801 default: 802 panic("tmpfs_dir_getdents: type %p %d", 803 de->td_node, (int)de->td_node->tn_type); 804 } 805 d.d_namlen = de->td_namelen; 806 MPASS(de->td_namelen < sizeof(d.d_name)); 807 (void)memcpy(d.d_name, de->td_name, de->td_namelen); 808 d.d_name[de->td_namelen] = '\0'; 809 d.d_reclen = GENERIC_DIRSIZ(&d); 810 811 /* Stop reading if the directory entry we are treating is 812 * bigger than the amount of data that can be returned. */ 813 if (d.d_reclen > uio->uio_resid) { 814 error = -1; 815 break; 816 } 817 818 /* Copy the new dirent structure into the output buffer and 819 * advance pointers. */ 820 error = uiomove(&d, d.d_reclen, uio); 821 822 (*cntp)++; 823 de = TAILQ_NEXT(de, td_entries); 824 } while (error == 0 && uio->uio_resid > 0 && de != NULL); 825 826 /* Update the offset and cache. */ 827 if (de == NULL) { 828 uio->uio_offset = TMPFS_DIRCOOKIE_EOF; 829 node->tn_dir.tn_readdir_lastn = 0; 830 node->tn_dir.tn_readdir_lastp = NULL; 831 } else { 832 node->tn_dir.tn_readdir_lastn = uio->uio_offset = tmpfs_dircookie(de); 833 node->tn_dir.tn_readdir_lastp = de; 834 } 835 836 node->tn_status |= TMPFS_NODE_ACCESSED; 837 return error; 838} 839 840/* --------------------------------------------------------------------- */ 841 842/* 843 * Resizes the aobj associated to the regular file pointed to by vp to 844 * the size newsize. 'vp' must point to a vnode that represents a regular 845 * file. 'newsize' must be positive. 846 * 847 * Returns zero on success or an appropriate error code on failure. 848 */ 849int 850tmpfs_reg_resize(struct vnode *vp, off_t newsize) 851{ 852 int error; 853 size_t newpages, oldpages; 854 struct tmpfs_mount *tmp; 855 struct tmpfs_node *node; 856 off_t oldsize; 857 858 MPASS(vp->v_type == VREG); 859 MPASS(newsize >= 0); 860 861 node = VP_TO_TMPFS_NODE(vp); 862 tmp = VFS_TO_TMPFS(vp->v_mount); 863 864 /* Convert the old and new sizes to the number of pages needed to 865 * store them. It may happen that we do not need to do anything 866 * because the last allocated page can accommodate the change on 867 * its own. */ 868 oldsize = node->tn_size; 869 oldpages = round_page(oldsize) / PAGE_SIZE; 870 MPASS(oldpages == node->tn_reg.tn_aobj_pages); 871 newpages = round_page(newsize) / PAGE_SIZE; 872 873 if (newpages > oldpages && 874 newpages - oldpages > TMPFS_PAGES_AVAIL(tmp)) { 875 error = ENOSPC; 876 goto out; 877 } 878 879 node->tn_reg.tn_aobj_pages = newpages; 880 881 TMPFS_LOCK(tmp); 882 tmp->tm_pages_used += (newpages - oldpages); 883 TMPFS_UNLOCK(tmp); 884 885 node->tn_size = newsize; 886 vnode_pager_setsize(vp, newsize); 887 if (newsize < oldsize) { 888 size_t zerolen = round_page(newsize) - newsize; 889 vm_object_t uobj = node->tn_reg.tn_aobj; 890 vm_page_t m; 891 892 /* 893 * free "backing store" 894 */ 895 VM_OBJECT_LOCK(uobj); 896 if (newpages < oldpages) { 897 swap_pager_freespace(uobj, 898 newpages, oldpages - newpages); 899 vm_object_page_remove(uobj, 900 OFF_TO_IDX(newsize + PAGE_MASK), 0, FALSE); 901 } 902 903 /* 904 * zero out the truncated part of the last page. 905 */ 906 907 if (zerolen > 0) { 908 m = vm_page_grab(uobj, OFF_TO_IDX(newsize), 909 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 910 pmap_zero_page_area(m, PAGE_SIZE - zerolen, 911 zerolen); 912 vm_page_wakeup(m); 913 } 914 VM_OBJECT_UNLOCK(uobj); 915 916 } 917 918 error = 0; 919 920out: 921 return error; 922} 923 924/* --------------------------------------------------------------------- */ 925 926/* 927 * Change flags of the given vnode. 928 * Caller should execute tmpfs_update on vp after a successful execution. 929 * The vnode must be locked on entry and remain locked on exit. 930 */ 931int 932tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct thread *p) 933{ 934 int error; 935 struct tmpfs_node *node; 936 937 MPASS(VOP_ISLOCKED(vp)); 938 939 node = VP_TO_TMPFS_NODE(vp); 940 941 /* Disallow this operation if the file system is mounted read-only. */ 942 if (vp->v_mount->mnt_flag & MNT_RDONLY) 943 return EROFS; 944 945 /* 946 * Callers may only modify the file flags on objects they 947 * have VADMIN rights for. 948 */ 949 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 950 return (error); 951 /* 952 * Unprivileged processes are not permitted to unset system 953 * flags, or modify flags if any system flags are set. 954 */ 955 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) { 956 if (node->tn_flags 957 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) { 958 error = securelevel_gt(cred, 0); 959 if (error) 960 return (error); 961 } 962 /* Snapshot flag cannot be set or cleared */ 963 if (((flags & SF_SNAPSHOT) != 0 && 964 (node->tn_flags & SF_SNAPSHOT) == 0) || 965 ((flags & SF_SNAPSHOT) == 0 && 966 (node->tn_flags & SF_SNAPSHOT) != 0)) 967 return (EPERM); 968 node->tn_flags = flags; 969 } else { 970 if (node->tn_flags 971 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) || 972 (flags & UF_SETTABLE) != flags) 973 return (EPERM); 974 node->tn_flags &= SF_SETTABLE; 975 node->tn_flags |= (flags & UF_SETTABLE); 976 } 977 node->tn_status |= TMPFS_NODE_CHANGED; 978 979 MPASS(VOP_ISLOCKED(vp)); 980 981 return 0; 982} 983 984/* --------------------------------------------------------------------- */ 985 986/* 987 * Change access mode on the given vnode. 988 * Caller should execute tmpfs_update on vp after a successful execution. 989 * The vnode must be locked on entry and remain locked on exit. 990 */ 991int 992tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p) 993{ 994 int error; 995 struct tmpfs_node *node; 996 997 MPASS(VOP_ISLOCKED(vp)); 998 999 node = VP_TO_TMPFS_NODE(vp); 1000 1001 /* Disallow this operation if the file system is mounted read-only. */ 1002 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1003 return EROFS; 1004 1005 /* Immutable or append-only files cannot be modified, either. */ 1006 if (node->tn_flags & (IMMUTABLE | APPEND)) 1007 return EPERM; 1008 1009 /* 1010 * To modify the permissions on a file, must possess VADMIN 1011 * for that file. 1012 */ 1013 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 1014 return (error); 1015 1016 /* 1017 * Privileged processes may set the sticky bit on non-directories, 1018 * as well as set the setgid bit on a file with a group that the 1019 * process is not a member of. 1020 */ 1021 if (vp->v_type != VDIR && (mode & S_ISTXT)) { 1022 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0)) 1023 return (EFTYPE); 1024 } 1025 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) { 1026 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0); 1027 if (error) 1028 return (error); 1029 } 1030 1031 1032 node->tn_mode &= ~ALLPERMS; 1033 node->tn_mode |= mode & ALLPERMS; 1034 1035 node->tn_status |= TMPFS_NODE_CHANGED; 1036 1037 MPASS(VOP_ISLOCKED(vp)); 1038 1039 return 0; 1040} 1041 1042/* --------------------------------------------------------------------- */ 1043 1044/* 1045 * Change ownership of the given vnode. At least one of uid or gid must 1046 * be different than VNOVAL. If one is set to that value, the attribute 1047 * is unchanged. 1048 * Caller should execute tmpfs_update on vp after a successful execution. 1049 * The vnode must be locked on entry and remain locked on exit. 1050 */ 1051int 1052tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred, 1053 struct thread *p) 1054{ 1055 int error; 1056 struct tmpfs_node *node; 1057 uid_t ouid; 1058 gid_t ogid; 1059 1060 MPASS(VOP_ISLOCKED(vp)); 1061 1062 node = VP_TO_TMPFS_NODE(vp); 1063 1064 /* Assign default values if they are unknown. */ 1065 MPASS(uid != VNOVAL || gid != VNOVAL); 1066 if (uid == VNOVAL) 1067 uid = node->tn_uid; 1068 if (gid == VNOVAL) 1069 gid = node->tn_gid; 1070 MPASS(uid != VNOVAL && gid != VNOVAL); 1071 1072 /* Disallow this operation if the file system is mounted read-only. */ 1073 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1074 return EROFS; 1075 1076 /* Immutable or append-only files cannot be modified, either. */ 1077 if (node->tn_flags & (IMMUTABLE | APPEND)) 1078 return EPERM; 1079 1080 /* 1081 * To modify the ownership of a file, must possess VADMIN for that 1082 * file. 1083 */ 1084 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 1085 return (error); 1086 1087 /* 1088 * To change the owner of a file, or change the group of a file to a 1089 * group of which we are not a member, the caller must have 1090 * privilege. 1091 */ 1092 if ((uid != node->tn_uid || 1093 (gid != node->tn_gid && !groupmember(gid, cred))) && 1094 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0))) 1095 return (error); 1096 1097 ogid = node->tn_gid; 1098 ouid = node->tn_uid; 1099 1100 node->tn_uid = uid; 1101 node->tn_gid = gid; 1102 1103 node->tn_status |= TMPFS_NODE_CHANGED; 1104 1105 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) { 1106 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) 1107 node->tn_mode &= ~(S_ISUID | S_ISGID); 1108 } 1109 1110 MPASS(VOP_ISLOCKED(vp)); 1111 1112 return 0; 1113} 1114 1115/* --------------------------------------------------------------------- */ 1116 1117/* 1118 * Change size of the given vnode. 1119 * Caller should execute tmpfs_update on vp after a successful execution. 1120 * The vnode must be locked on entry and remain locked on exit. 1121 */ 1122int 1123tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred, 1124 struct thread *p) 1125{ 1126 int error; 1127 struct tmpfs_node *node; 1128 1129 MPASS(VOP_ISLOCKED(vp)); 1130 1131 node = VP_TO_TMPFS_NODE(vp); 1132 1133 /* Decide whether this is a valid operation based on the file type. */ 1134 error = 0; 1135 switch (vp->v_type) { 1136 case VDIR: 1137 return EISDIR; 1138 1139 case VREG: 1140 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1141 return EROFS; 1142 break; 1143 1144 case VBLK: 1145 /* FALLTHROUGH */ 1146 case VCHR: 1147 /* FALLTHROUGH */ 1148 case VFIFO: 1149 /* Allow modifications of special files even if in the file 1150 * system is mounted read-only (we are not modifying the 1151 * files themselves, but the objects they represent). */ 1152 return 0; 1153 1154 default: 1155 /* Anything else is unsupported. */ 1156 return EOPNOTSUPP; 1157 } 1158 1159 /* Immutable or append-only files cannot be modified, either. */ 1160 if (node->tn_flags & (IMMUTABLE | APPEND)) 1161 return EPERM; 1162 1163 error = tmpfs_truncate(vp, size); 1164 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents 1165 * for us, as will update tn_status; no need to do that here. */ 1166 1167 MPASS(VOP_ISLOCKED(vp)); 1168 1169 return error; 1170} 1171 1172/* --------------------------------------------------------------------- */ 1173 1174/* 1175 * Change access and modification times of the given vnode. 1176 * Caller should execute tmpfs_update on vp after a successful execution. 1177 * The vnode must be locked on entry and remain locked on exit. 1178 */ 1179int 1180tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime, 1181 struct timespec *birthtime, int vaflags, struct ucred *cred, struct thread *l) 1182{ 1183 int error; 1184 struct tmpfs_node *node; 1185 1186 MPASS(VOP_ISLOCKED(vp)); 1187 1188 node = VP_TO_TMPFS_NODE(vp); 1189 1190 /* Disallow this operation if the file system is mounted read-only. */ 1191 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1192 return EROFS; 1193 1194 /* Immutable or append-only files cannot be modified, either. */ 1195 if (node->tn_flags & (IMMUTABLE | APPEND)) 1196 return EPERM; 1197 1198 /* Determine if the user have proper privilege to update time. */ 1199 if (vaflags & VA_UTIMES_NULL) { 1200 error = VOP_ACCESS(vp, VADMIN, cred, l); 1201 if (error) 1202 error = VOP_ACCESS(vp, VWRITE, cred, l); 1203 } else 1204 error = VOP_ACCESS(vp, VADMIN, cred, l); 1205 if (error) 1206 return (error); 1207 1208 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL) 1209 node->tn_status |= TMPFS_NODE_ACCESSED; 1210 1211 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL) 1212 node->tn_status |= TMPFS_NODE_MODIFIED; 1213 1214 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL) 1215 node->tn_status |= TMPFS_NODE_MODIFIED; 1216 1217 tmpfs_itimes(vp, atime, mtime); 1218 1219 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL) 1220 node->tn_birthtime = *birthtime; 1221 MPASS(VOP_ISLOCKED(vp)); 1222 1223 return 0; 1224} 1225 1226/* --------------------------------------------------------------------- */ 1227/* Sync timestamps */ 1228void 1229tmpfs_itimes(struct vnode *vp, const struct timespec *acc, 1230 const struct timespec *mod) 1231{ 1232 struct tmpfs_node *node; 1233 struct timespec now; 1234 1235 node = VP_TO_TMPFS_NODE(vp); 1236 1237 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 1238 TMPFS_NODE_CHANGED)) == 0) 1239 return; 1240 1241 vfs_timestamp(&now); 1242 if (node->tn_status & TMPFS_NODE_ACCESSED) { 1243 if (acc == NULL) 1244 acc = &now; 1245 node->tn_atime = *acc; 1246 } 1247 if (node->tn_status & TMPFS_NODE_MODIFIED) { 1248 if (mod == NULL) 1249 mod = &now; 1250 node->tn_mtime = *mod; 1251 } 1252 if (node->tn_status & TMPFS_NODE_CHANGED) { 1253 node->tn_ctime = now; 1254 } 1255 node->tn_status &= 1256 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED); 1257} 1258 1259/* --------------------------------------------------------------------- */ 1260 1261void 1262tmpfs_update(struct vnode *vp) 1263{ 1264 1265 tmpfs_itimes(vp, NULL, NULL); 1266} 1267 1268/* --------------------------------------------------------------------- */ 1269 1270int 1271tmpfs_truncate(struct vnode *vp, off_t length) 1272{ 1273 int error; 1274 struct tmpfs_node *node; 1275 1276 node = VP_TO_TMPFS_NODE(vp); 1277 1278 if (length < 0) { 1279 error = EINVAL; 1280 goto out; 1281 } 1282 1283 if (node->tn_size == length) { 1284 error = 0; 1285 goto out; 1286 } 1287 1288 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 1289 return (EFBIG); 1290 1291 error = tmpfs_reg_resize(vp, length); 1292 if (error == 0) { 1293 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1294 } 1295 1296out: 1297 tmpfs_update(vp); 1298 1299 return error; 1300}
|