Thu Jun 13 10:53:00 2013 UTC ()
Pull up following revision(s) (requested by reinoud in ticket #894):
	sys/fs/nilfs/nilfs_vnops.c: revision 1.22-1.23
Fix serious file read problem due to not-so-clear VOP_BMAP description. Should
fix PR 45605.


(msaitoh)
diff -r1.16.6.1 -r1.16.6.1.4.1 src/sys/fs/nilfs/nilfs_vnops.c

cvs diff -r1.16.6.1 -r1.16.6.1.4.1 src/sys/fs/nilfs/nilfs_vnops.c (switch to unified diff)

--- src/sys/fs/nilfs/nilfs_vnops.c 2012/08/12 12:59:47 1.16.6.1
+++ src/sys/fs/nilfs/nilfs_vnops.c 2013/06/13 10:53:00 1.16.6.1.4.1
@@ -1,1366 +1,1372 @@ @@ -1,1366 +1,1372 @@
1/* $NetBSD: nilfs_vnops.c,v 1.16.6.1 2012/08/12 12:59:47 martin Exp $ */ 1/* $NetBSD: nilfs_vnops.c,v 1.16.6.1.4.1 2013/06/13 10:53:00 msaitoh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2008, 2009 Reinoud Zandijk 4 * Copyright (c) 2008, 2009 Reinoud Zandijk
5 * All rights reserved. 5 * All rights reserved.
6 *  6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 *  15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *  26 *
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30#ifndef lint 30#ifndef lint
31__KERNEL_RCSID(0, "$NetBSD: nilfs_vnops.c,v 1.16.6.1 2012/08/12 12:59:47 martin Exp $"); 31__KERNEL_RCSID(0, "$NetBSD: nilfs_vnops.c,v 1.16.6.1.4.1 2013/06/13 10:53:00 msaitoh Exp $");
32#endif /* not lint */ 32#endif /* not lint */
33 33
34 34
35#include <sys/param.h> 35#include <sys/param.h>
36#include <sys/systm.h> 36#include <sys/systm.h>
37#include <sys/namei.h> 37#include <sys/namei.h>
38#include <sys/resourcevar.h> /* defines plimit structure in proc struct */ 38#include <sys/resourcevar.h> /* defines plimit structure in proc struct */
39#include <sys/kernel.h> 39#include <sys/kernel.h>
40#include <sys/file.h> /* define FWRITE ... */ 40#include <sys/file.h> /* define FWRITE ... */
41#include <sys/stat.h> 41#include <sys/stat.h>
42#include <sys/buf.h> 42#include <sys/buf.h>
43#include <sys/proc.h> 43#include <sys/proc.h>
44#include <sys/mount.h> 44#include <sys/mount.h>
45#include <sys/vnode.h> 45#include <sys/vnode.h>
46#include <sys/signalvar.h> 46#include <sys/signalvar.h>
47#include <sys/malloc.h> 47#include <sys/malloc.h>
48#include <sys/dirent.h> 48#include <sys/dirent.h>
49#include <sys/lockf.h> 49#include <sys/lockf.h>
50#include <sys/kauth.h> 50#include <sys/kauth.h>
51 51
52#include <miscfs/genfs/genfs.h> 52#include <miscfs/genfs/genfs.h>
53#include <uvm/uvm_extern.h> 53#include <uvm/uvm_extern.h>
54 54
55#include <fs/nilfs/nilfs_mount.h> 55#include <fs/nilfs/nilfs_mount.h>
56#include "nilfs.h" 56#include "nilfs.h"
57#include "nilfs_subr.h" 57#include "nilfs_subr.h"
58#include "nilfs_bswap.h" 58#include "nilfs_bswap.h"
59 59
60 60
61#define VTOI(vnode) ((struct nilfs_node *) (vnode)->v_data) 61#define VTOI(vnode) ((struct nilfs_node *) (vnode)->v_data)
62 62
63 63
64/* externs */ 64/* externs */
65extern int prtactive; 65extern int prtactive;
66 66
67/* implementations of vnode functions; table follows at end */ 67/* implementations of vnode functions; table follows at end */
68/* --------------------------------------------------------------------- */ 68/* --------------------------------------------------------------------- */
69 69
70int 70int
71nilfs_inactive(void *v) 71nilfs_inactive(void *v)
72{ 72{
73 struct vop_inactive_args /* { 73 struct vop_inactive_args /* {
74 struct vnode *a_vp; 74 struct vnode *a_vp;
75 bool *a_recycle; 75 bool *a_recycle;
76 } */ *ap = v; 76 } */ *ap = v;
77 struct vnode *vp = ap->a_vp; 77 struct vnode *vp = ap->a_vp;
78 struct nilfs_node *nilfs_node = VTOI(vp); 78 struct nilfs_node *nilfs_node = VTOI(vp);
79 79
80 DPRINTF(NODE, ("nilfs_inactive called for nilfs_node %p\n", VTOI(vp))); 80 DPRINTF(NODE, ("nilfs_inactive called for nilfs_node %p\n", VTOI(vp)));
81 81
82 if (nilfs_node == NULL) { 82 if (nilfs_node == NULL) {
83 DPRINTF(NODE, ("nilfs_inactive: inactive NULL NILFS node\n")); 83 DPRINTF(NODE, ("nilfs_inactive: inactive NULL NILFS node\n"));
84 VOP_UNLOCK(vp); 84 VOP_UNLOCK(vp);
85 return 0; 85 return 0;
86 } 86 }
87 87
88 /* 88 /*
89 * Optionally flush metadata to disc. If the file has not been 89 * Optionally flush metadata to disc. If the file has not been
90 * referenced anymore in a directory we ought to free up the resources 90 * referenced anymore in a directory we ought to free up the resources
91 * on disc if applicable. 91 * on disc if applicable.
92 */ 92 */
93 VOP_UNLOCK(vp); 93 VOP_UNLOCK(vp);
94 94
95 return 0; 95 return 0;
96} 96}
97 97
98/* --------------------------------------------------------------------- */ 98/* --------------------------------------------------------------------- */
99 99
100int 100int
101nilfs_reclaim(void *v) 101nilfs_reclaim(void *v)
102{ 102{
103 struct vop_reclaim_args /* { 103 struct vop_reclaim_args /* {
104 struct vnode *a_vp; 104 struct vnode *a_vp;
105 } */ *ap = v; 105 } */ *ap = v;
106 struct vnode *vp = ap->a_vp; 106 struct vnode *vp = ap->a_vp;
107 struct nilfs_node *nilfs_node = VTOI(vp); 107 struct nilfs_node *nilfs_node = VTOI(vp);
108 108
109 DPRINTF(NODE, ("nilfs_reclaim called for node %p\n", nilfs_node)); 109 DPRINTF(NODE, ("nilfs_reclaim called for node %p\n", nilfs_node));
110 if (prtactive && vp->v_usecount > 1) 110 if (prtactive && vp->v_usecount > 1)
111 vprint("nilfs_reclaim(): pushing active", vp); 111 vprint("nilfs_reclaim(): pushing active", vp);
112 112
113 if (nilfs_node == NULL) { 113 if (nilfs_node == NULL) {
114 DPRINTF(NODE, ("nilfs_reclaim(): null nilfsnode\n")); 114 DPRINTF(NODE, ("nilfs_reclaim(): null nilfsnode\n"));
115 return 0; 115 return 0;
116 } 116 }
117 117
118 /* update note for closure */ 118 /* update note for closure */
119 nilfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE); 119 nilfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
120 120
121 /* dispose all node knowledge */ 121 /* dispose all node knowledge */
122 nilfs_dispose_node(&nilfs_node); 122 nilfs_dispose_node(&nilfs_node);
123 123
124 return 0; 124 return 0;
125} 125}
126 126
127/* --------------------------------------------------------------------- */ 127/* --------------------------------------------------------------------- */
128 128
129int 129int
130nilfs_read(void *v) 130nilfs_read(void *v)
131{ 131{
132 struct vop_read_args /* { 132 struct vop_read_args /* {
133 struct vnode *a_vp; 133 struct vnode *a_vp;
134 struct uio *a_uio; 134 struct uio *a_uio;
135 int a_ioflag; 135 int a_ioflag;
136 kauth_cred_t a_cred; 136 kauth_cred_t a_cred;
137 } */ *ap = v; 137 } */ *ap = v;
138 struct vnode *vp = ap->a_vp; 138 struct vnode *vp = ap->a_vp;
139 struct uio *uio = ap->a_uio; 139 struct uio *uio = ap->a_uio;
140 int ioflag = ap->a_ioflag; 140 int ioflag = ap->a_ioflag;
141 int advice = IO_ADV_DECODE(ap->a_ioflag); 141 int advice = IO_ADV_DECODE(ap->a_ioflag);
142 struct uvm_object *uobj; 142 struct uvm_object *uobj;
143 struct nilfs_node *nilfs_node = VTOI(vp); 143 struct nilfs_node *nilfs_node = VTOI(vp);
144 uint64_t file_size; 144 uint64_t file_size;
145 vsize_t len; 145 vsize_t len;
146 int error; 146 int error;
147 147
148 DPRINTF(READ, ("nilfs_read called\n")); 148 DPRINTF(READ, ("nilfs_read called\n"));
149 149
150 /* can this happen? some filingsystems have this check */ 150 /* can this happen? some filingsystems have this check */
151 if (uio->uio_offset < 0) 151 if (uio->uio_offset < 0)
152 return EINVAL; 152 return EINVAL;
153 if (uio->uio_resid == 0) 153 if (uio->uio_resid == 0)
154 return 0; 154 return 0;
155 155
156 /* protect against rogue programs reading raw directories and links */ 156 /* protect against rogue programs reading raw directories and links */
157 if ((ioflag & IO_ALTSEMANTICS) == 0) { 157 if ((ioflag & IO_ALTSEMANTICS) == 0) {
158 if (vp->v_type == VDIR) 158 if (vp->v_type == VDIR)
159 return EISDIR; 159 return EISDIR;
160 /* all but regular files just give EINVAL */ 160 /* all but regular files just give EINVAL */
161 if (vp->v_type != VREG) 161 if (vp->v_type != VREG)
162 return EINVAL; 162 return EINVAL;
163 } 163 }
164 164
165 assert(nilfs_node); 165 assert(nilfs_node);
166 file_size = nilfs_rw64(nilfs_node->inode.i_size); 166 file_size = nilfs_rw64(nilfs_node->inode.i_size);
167 167
168 /* read contents using buffercache */ 168 /* read contents using buffercache */
169 uobj = &vp->v_uobj; 169 uobj = &vp->v_uobj;
170 error = 0; 170 error = 0;
171 while (uio->uio_resid > 0) { 171 while (uio->uio_resid > 0) {
172 /* reached end? */ 172 /* reached end? */
173 if (file_size <= uio->uio_offset) 173 if (file_size <= uio->uio_offset)
174 break; 174 break;
175 175
176 /* maximise length to file extremity */ 176 /* maximise length to file extremity */
177 len = MIN(file_size - uio->uio_offset, uio->uio_resid); 177 len = MIN(file_size - uio->uio_offset, uio->uio_resid);
178 if (len == 0) 178 if (len == 0)
179 break; 179 break;
180 180
181 /* ubc, here we come, prepare to trap */ 181 /* ubc, here we come, prepare to trap */
182 error = ubc_uiomove(uobj, uio, len, advice, 182 error = ubc_uiomove(uobj, uio, len, advice,
183 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp)); 183 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
184 if (error) 184 if (error)
185 break; 185 break;
186 } 186 }
187 187
188 /* note access time unless not requested */ 188 /* note access time unless not requested */
189 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) { 189 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
190 nilfs_node->i_flags |= IN_ACCESS; 190 nilfs_node->i_flags |= IN_ACCESS;
191 if ((ioflag & IO_SYNC) == IO_SYNC) 191 if ((ioflag & IO_SYNC) == IO_SYNC)
192 error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT); 192 error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
193 } 193 }
194 194
195 return error; 195 return error;
196} 196}
197 197
198/* --------------------------------------------------------------------- */ 198/* --------------------------------------------------------------------- */
199 199
200int 200int
201nilfs_write(void *v) 201nilfs_write(void *v)
202{ 202{
203 struct vop_write_args /* { 203 struct vop_write_args /* {
204 struct vnode *a_vp; 204 struct vnode *a_vp;
205 struct uio *a_uio; 205 struct uio *a_uio;
206 int a_ioflag; 206 int a_ioflag;
207 kauth_cred_t a_cred; 207 kauth_cred_t a_cred;
208 } */ *ap = v; 208 } */ *ap = v;
209 struct vnode *vp = ap->a_vp; 209 struct vnode *vp = ap->a_vp;
210 struct uio *uio = ap->a_uio; 210 struct uio *uio = ap->a_uio;
211 int ioflag = ap->a_ioflag; 211 int ioflag = ap->a_ioflag;
212 int advice = IO_ADV_DECODE(ap->a_ioflag); 212 int advice = IO_ADV_DECODE(ap->a_ioflag);
213 struct uvm_object *uobj; 213 struct uvm_object *uobj;
214 struct nilfs_node *nilfs_node = VTOI(vp); 214 struct nilfs_node *nilfs_node = VTOI(vp);
215 uint64_t file_size, old_size; 215 uint64_t file_size, old_size;
216 vsize_t len; 216 vsize_t len;
217 int error, resid, extended; 217 int error, resid, extended;
218 218
219 DPRINTF(WRITE, ("nilfs_write called\n")); 219 DPRINTF(WRITE, ("nilfs_write called\n"));
220 220
221 /* can this happen? some filingsystems have this check */ 221 /* can this happen? some filingsystems have this check */
222 if (uio->uio_offset < 0) 222 if (uio->uio_offset < 0)
223 return EINVAL; 223 return EINVAL;
224 if (uio->uio_resid == 0) 224 if (uio->uio_resid == 0)
225 return 0; 225 return 0;
226 226
227 /* protect against rogue programs writing raw directories or links */ 227 /* protect against rogue programs writing raw directories or links */
228 if ((ioflag & IO_ALTSEMANTICS) == 0) { 228 if ((ioflag & IO_ALTSEMANTICS) == 0) {
229 if (vp->v_type == VDIR) 229 if (vp->v_type == VDIR)
230 return EISDIR; 230 return EISDIR;
231 /* all but regular files just give EINVAL for now */ 231 /* all but regular files just give EINVAL for now */
232 if (vp->v_type != VREG) 232 if (vp->v_type != VREG)
233 return EINVAL; 233 return EINVAL;
234 } 234 }
235 235
236 assert(nilfs_node); 236 assert(nilfs_node);
237 panic("nilfs_write() called\n"); 237 panic("nilfs_write() called\n");
238return EIO; 238return EIO;
239 239
240 /* remember old file size */ 240 /* remember old file size */
241 assert(nilfs_node); 241 assert(nilfs_node);
242 file_size = nilfs_rw64(nilfs_node->inode.i_size); 242 file_size = nilfs_rw64(nilfs_node->inode.i_size);
243 old_size = file_size; 243 old_size = file_size;
244 244
245 /* if explicitly asked to append, uio_offset can be wrong? */ 245 /* if explicitly asked to append, uio_offset can be wrong? */
246 if (ioflag & IO_APPEND) 246 if (ioflag & IO_APPEND)
247 uio->uio_offset = file_size; 247 uio->uio_offset = file_size;
248 248
249#if 0 249#if 0
250 extended = (uio->uio_offset + uio->uio_resid > file_size); 250 extended = (uio->uio_offset + uio->uio_resid > file_size);
251 if (extended) { 251 if (extended) {
252 DPRINTF(WRITE, ("extending file from %"PRIu64" to %"PRIu64"\n", 252 DPRINTF(WRITE, ("extending file from %"PRIu64" to %"PRIu64"\n",
253 file_size, uio->uio_offset + uio->uio_resid)); 253 file_size, uio->uio_offset + uio->uio_resid));
254 error = nilfs_grow_node(nilfs_node, uio->uio_offset + uio->uio_resid); 254 error = nilfs_grow_node(nilfs_node, uio->uio_offset + uio->uio_resid);
255 if (error) 255 if (error)
256 return error; 256 return error;
257 file_size = uio->uio_offset + uio->uio_resid; 257 file_size = uio->uio_offset + uio->uio_resid;
258 } 258 }
259#endif 259#endif
260 260
261 /* write contents using buffercache */ 261 /* write contents using buffercache */
262 uobj = &vp->v_uobj; 262 uobj = &vp->v_uobj;
263 resid = uio->uio_resid; 263 resid = uio->uio_resid;
264 error = 0; 264 error = 0;
265 265
266 uvm_vnp_setwritesize(vp, file_size); 266 uvm_vnp_setwritesize(vp, file_size);
267 while (uio->uio_resid > 0) { 267 while (uio->uio_resid > 0) {
268 /* maximise length to file extremity */ 268 /* maximise length to file extremity */
269 len = MIN(file_size - uio->uio_offset, uio->uio_resid); 269 len = MIN(file_size - uio->uio_offset, uio->uio_resid);
270 if (len == 0) 270 if (len == 0)
271 break; 271 break;
272 272
273 /* ubc, here we come, prepare to trap */ 273 /* ubc, here we come, prepare to trap */
274 error = ubc_uiomove(uobj, uio, len, advice, 274 error = ubc_uiomove(uobj, uio, len, advice,
275 UBC_WRITE | UBC_UNMAP_FLAG(vp)); 275 UBC_WRITE | UBC_UNMAP_FLAG(vp));
276 if (error) 276 if (error)
277 break; 277 break;
278 } 278 }
279 uvm_vnp_setsize(vp, file_size); 279 uvm_vnp_setsize(vp, file_size);
280 280
281 /* mark node changed and request update */ 281 /* mark node changed and request update */
282 nilfs_node->i_flags |= IN_CHANGE | IN_UPDATE; 282 nilfs_node->i_flags |= IN_CHANGE | IN_UPDATE;
283 if (vp->v_mount->mnt_flag & MNT_RELATIME) 283 if (vp->v_mount->mnt_flag & MNT_RELATIME)
284 nilfs_node->i_flags |= IN_ACCESS; 284 nilfs_node->i_flags |= IN_ACCESS;
285 285
286 /* 286 /*
287 * XXX TODO FFS has code here to reset setuid & setgid when we're not 287 * XXX TODO FFS has code here to reset setuid & setgid when we're not
288 * the superuser as a precaution against tampering. 288 * the superuser as a precaution against tampering.
289 */ 289 */
290 290
291 /* if we wrote a thing, note write action on vnode */ 291 /* if we wrote a thing, note write action on vnode */
292 if (resid > uio->uio_resid) 292 if (resid > uio->uio_resid)
293 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0)); 293 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
294 294
295 if (error) { 295 if (error) {
296 /* bring back file size to its former size */ 296 /* bring back file size to its former size */
297 /* take notice of its errors? */ 297 /* take notice of its errors? */
298// (void) nilfs_chsize(vp, (u_quad_t) old_size, NOCRED); 298// (void) nilfs_chsize(vp, (u_quad_t) old_size, NOCRED);
299 299
300 /* roll back uio */ 300 /* roll back uio */
301 uio->uio_offset -= resid - uio->uio_resid; 301 uio->uio_offset -= resid - uio->uio_resid;
302 uio->uio_resid = resid; 302 uio->uio_resid = resid;
303 } else { 303 } else {
304 /* if we write and we're synchronous, update node */ 304 /* if we write and we're synchronous, update node */
305 if ((resid > uio->uio_resid) && ((ioflag & IO_SYNC) == IO_SYNC)) 305 if ((resid > uio->uio_resid) && ((ioflag & IO_SYNC) == IO_SYNC))
306 error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT); 306 error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
307 } 307 }
308 308
309 return error; 309 return error;
310} 310}
311 311
312 312
313/* --------------------------------------------------------------------- */ 313/* --------------------------------------------------------------------- */
314 314
315/* 315/*
316 * bmap functionality that translates logical block numbers to the virtual 316 * bmap functionality that translates logical block numbers to the virtual
317 * block numbers to be stored on the vnode itself. 317 * block numbers to be stored on the vnode itself.
 318 *
 319 * Important alert!
 320 *
 321 * If runp is not NULL, the number of contiguous blocks __starting from the
 322 * next block after the queried block__ will be returned in runp.
318 */ 323 */
319 324
320int 325int
321nilfs_trivial_bmap(void *v) 326nilfs_trivial_bmap(void *v)
322{ 327{
323 struct vop_bmap_args /* { 328 struct vop_bmap_args /* {
324 struct vnode *a_vp; 329 struct vnode *a_vp;
325 daddr_t a_bn; 330 daddr_t a_bn;
326 struct vnode **a_vpp; 331 struct vnode **a_vpp;
327 daddr_t *a_bnp; 332 daddr_t *a_bnp;
328 int *a_runp; 333 int *a_runp;
329 } */ *ap = v; 334 } */ *ap = v;
330 struct vnode *vp = ap->a_vp; /* our node */ 335 struct vnode *vp = ap->a_vp; /* our node */
331 struct vnode **vpp = ap->a_vpp; /* return node */ 336 struct vnode **vpp = ap->a_vpp; /* return node */
332 daddr_t *bnp = ap->a_bnp; /* translated */ 337 daddr_t *bnp = ap->a_bnp; /* translated */
333 daddr_t bn = ap->a_bn; /* origional */ 338 daddr_t bn = ap->a_bn; /* origional */
334 int *runp = ap->a_runp; 339 int *runp = ap->a_runp;
335 struct nilfs_node *node = VTOI(vp); 340 struct nilfs_node *node = VTOI(vp);
336 uint64_t *l2vmap; 341 uint64_t *l2vmap;
337 uint32_t blocksize; 342 uint32_t blocksize;
338 int blks, run, error; 343 int blks, run, error;
339 344
340 DPRINTF(TRANSLATE, ("nilfs_bmap() called\n")); 345 DPRINTF(TRANSLATE, ("nilfs_bmap() called\n"));
341 /* XXX could return `-1' to indicate holes/zero's */ 346 /* XXX could return `-1' to indicate holes/zero's */
342 347
343 blocksize = node->nilfsdev->blocksize; 348 blocksize = node->nilfsdev->blocksize;
344 blks = MAXPHYS / blocksize; 349 blks = MAXPHYS / blocksize;
345 350
346 /* get mapping memory */ 351 /* get mapping memory */
347 l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK); 352 l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
348 353
349 /* get virtual block numbers for the vnode's buffer span */ 354 /* get virtual block numbers for the vnode's buffer span */
350 error = nilfs_btree_nlookup(node, bn, blks, l2vmap); 355 error = nilfs_btree_nlookup(node, bn, blks, l2vmap);
351 if (error) { 356 if (error) {
352 free(l2vmap, M_TEMP); 357 free(l2vmap, M_TEMP);
353 return error; 358 return error;
354 } 359 }
355 360
356 /* store virtual blocks on our own vp */ 361 /* store virtual blocks on our own vp */
357 if (vpp) 362 if (vpp)
358 *vpp = vp; 363 *vpp = vp;
359 364
360 /* start at virt[0] */ 365 /* start at virt[0] */
361 *bnp = l2vmap[0]; 366 *bnp = l2vmap[0];
362 367
363 /* get runlength */ 368 /* get runlength */
364 run = 1; 369 run = 1;
365 while ((run < blks) && (l2vmap[run] == *bnp + run)) 370 while ((run < blks) && (l2vmap[run] == *bnp + run))
366 run++; 371 run++;
367  372 run--; /* see comment at start of function */
 373
368 /* set runlength */ 374 /* set runlength */
369 if (runp) 375 if (runp)
370 *runp = run; 376 *runp = run;
371 377
372 DPRINTF(TRANSLATE, ("\tstart %"PRIu64" -> %"PRIu64" run %d\n", 378 DPRINTF(TRANSLATE, ("\tstart %"PRIu64" -> %"PRIu64" run %d\n",
373 bn, *bnp, run)); 379 bn, *bnp, run));
374 380
375 /* mark not translated on virtual block number 0 */ 381 /* mark not translated on virtual block number 0 */
376 if (*bnp == 0) 382 if (*bnp == 0)
377 *bnp = -1; 383 *bnp = -1;
378 384
379 /* return success */ 385 /* return success */
380 free(l2vmap, M_TEMP); 386 free(l2vmap, M_TEMP);
381 return 0; 387 return 0;
382} 388}
383 389
384/* --------------------------------------------------------------------- */ 390/* --------------------------------------------------------------------- */
385 391
386static void 392static void
387nilfs_read_filebuf(struct nilfs_node *node, struct buf *bp) 393nilfs_read_filebuf(struct nilfs_node *node, struct buf *bp)
388{ 394{
389 struct nilfs_device *nilfsdev = node->nilfsdev; 395 struct nilfs_device *nilfsdev = node->nilfsdev;
390 struct buf *nbp; 396 struct buf *nbp;
391 uint64_t *l2vmap, *v2pmap; 397 uint64_t *l2vmap, *v2pmap;
392 uint64_t from, blks; 398 uint64_t from, blks;
393 uint32_t blocksize, buf_offset; 399 uint32_t blocksize, buf_offset;
394 uint8_t *buf_pos; 400 uint8_t *buf_pos;
395 int blk2dev = nilfsdev->blocksize / DEV_BSIZE; 401 int blk2dev = nilfsdev->blocksize / DEV_BSIZE;
396 int i, error; 402 int i, error;
397 403
398 /* 404 /*
399 * Translate all the block sectors into a series of buffers to read 405 * Translate all the block sectors into a series of buffers to read
400 * asynchronously from the nilfs device. Note that this lookup may 406 * asynchronously from the nilfs device. Note that this lookup may
401 * induce readin's too. 407 * induce readin's too.
402 */ 408 */
403 409
404 blocksize = nilfsdev->blocksize; 410 blocksize = nilfsdev->blocksize;
405 411
406 from = bp->b_blkno; 412 from = bp->b_blkno;
407 blks = bp->b_bcount / blocksize; 413 blks = bp->b_bcount / blocksize;
408 414
409 DPRINTF(READ, ("\tread in from inode %"PRIu64" blkno %"PRIu64" " 415 DPRINTF(READ, ("\tread in from inode %"PRIu64" blkno %"PRIu64" "
410 "+ %"PRIu64" blocks\n", node->ino, from, blks)); 416 "+ %"PRIu64" blocks\n", node->ino, from, blks));
411 417
412 DPRINTF(READ, ("\t\tblkno %"PRIu64" " 418 DPRINTF(READ, ("\t\tblkno %"PRIu64" "
413 "+ %d bytes\n", bp->b_blkno, bp->b_bcount)); 419 "+ %d bytes\n", bp->b_blkno, bp->b_bcount));
414 420
415 /* get mapping memory */ 421 /* get mapping memory */
416 l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK); 422 l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
417 v2pmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK); 423 v2pmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
418 424
419 /* get virtual block numbers for the vnode's buffer span */ 425 /* get virtual block numbers for the vnode's buffer span */
420 for (i = 0; i < blks; i++) 426 for (i = 0; i < blks; i++)
421 l2vmap[i] = from + i; 427 l2vmap[i] = from + i;
422 428
423 /* translate virtual block numbers to physical block numbers */ 429 /* translate virtual block numbers to physical block numbers */
424 error = nilfs_nvtop(node, blks, l2vmap, v2pmap); 430 error = nilfs_nvtop(node, blks, l2vmap, v2pmap);
425 if (error) 431 if (error)
426 goto out; 432 goto out;
427 433
428 /* issue translated blocks */ 434 /* issue translated blocks */
429 bp->b_resid = bp->b_bcount; 435 bp->b_resid = bp->b_bcount;
430 for (i = 0; i < blks; i++) { 436 for (i = 0; i < blks; i++) {
431 DPRINTF(READ, ("read_filebuf : ino %"PRIu64" blk %d -> " 437 DPRINTF(READ, ("read_filebuf : ino %"PRIu64" blk %d -> "
432 "%"PRIu64" -> %"PRIu64"\n", 438 "%"PRIu64" -> %"PRIu64"\n",
433 node->ino, i, l2vmap[i], v2pmap[i])); 439 node->ino, i, l2vmap[i], v2pmap[i]));
434 440
435 buf_offset = i * blocksize; 441 buf_offset = i * blocksize;
436 buf_pos = (uint8_t *) bp->b_data + buf_offset; 442 buf_pos = (uint8_t *) bp->b_data + buf_offset;
437 443
438 /* note virtual block 0 marks not mapped */ 444 /* note virtual block 0 marks not mapped */
439 if (l2vmap[i] == 0) { 445 if (l2vmap[i] == 0) {
440 memset(buf_pos, 0, blocksize); 446 memset(buf_pos, 0, blocksize);
441 nestiobuf_done(bp, blocksize, 0); 447 nestiobuf_done(bp, blocksize, 0);
442 continue; 448 continue;
443 } 449 }
444 450
445 /* nest iobuf */ 451 /* nest iobuf */
446 nbp = getiobuf(NULL, true); 452 nbp = getiobuf(NULL, true);
447 nestiobuf_setup(bp, nbp, buf_offset, blocksize); 453 nestiobuf_setup(bp, nbp, buf_offset, blocksize);
448 KASSERT(nbp->b_vp == node->vnode); 454 KASSERT(nbp->b_vp == node->vnode);
449 /* nbp is B_ASYNC */ 455 /* nbp is B_ASYNC */
450 456
451 nbp->b_lblkno = i; 457 nbp->b_lblkno = i;
452 nbp->b_blkno = v2pmap[i] * blk2dev; /* in DEV_BSIZE */ 458 nbp->b_blkno = v2pmap[i] * blk2dev; /* in DEV_BSIZE */
453 nbp->b_rawblkno = nbp->b_blkno; 459 nbp->b_rawblkno = nbp->b_blkno;
454 460
455 VOP_STRATEGY(nilfsdev->devvp, nbp); 461 VOP_STRATEGY(nilfsdev->devvp, nbp);
456 } 462 }
457 463
458 if ((bp->b_flags & B_ASYNC) == 0) 464 if ((bp->b_flags & B_ASYNC) == 0)
459 biowait(bp); 465 biowait(bp);
460 466
461out: 467out:
462 free(l2vmap, M_TEMP); 468 free(l2vmap, M_TEMP);
463 free(v2pmap, M_TEMP); 469 free(v2pmap, M_TEMP);
464 if (error) { 470 if (error) {
465 bp->b_error = EIO; 471 bp->b_error = EIO;
466 biodone(bp); 472 biodone(bp);
467 } 473 }
468} 474}
469 475
470 476
471static void 477static void
472nilfs_write_filebuf(struct nilfs_node *node, struct buf *bp) 478nilfs_write_filebuf(struct nilfs_node *node, struct buf *bp)
473{ 479{
474 /* TODO pass on to segment collector */ 480 /* TODO pass on to segment collector */
475 panic("nilfs_strategy writing called\n"); 481 panic("nilfs_strategy writing called\n");
476} 482}
477 483
478 484
479int 485int
480nilfs_vfsstrategy(void *v) 486nilfs_vfsstrategy(void *v)
481{ 487{
482 struct vop_strategy_args /* { 488 struct vop_strategy_args /* {
483 struct vnode *a_vp; 489 struct vnode *a_vp;
484 struct buf *a_bp; 490 struct buf *a_bp;
485 } */ *ap = v; 491 } */ *ap = v;
486 struct vnode *vp = ap->a_vp; 492 struct vnode *vp = ap->a_vp;
487 struct buf *bp = ap->a_bp; 493 struct buf *bp = ap->a_bp;
488 struct nilfs_node *node = VTOI(vp); 494 struct nilfs_node *node = VTOI(vp);
489 495
490 DPRINTF(STRATEGY, ("nilfs_strategy called\n")); 496 DPRINTF(STRATEGY, ("nilfs_strategy called\n"));
491 497
492 /* check if we ought to be here */ 498 /* check if we ought to be here */
493 if (vp->v_type == VBLK || vp->v_type == VCHR) 499 if (vp->v_type == VBLK || vp->v_type == VCHR)
494 panic("nilfs_strategy: spec"); 500 panic("nilfs_strategy: spec");
495 501
496 /* translate if needed and pass on */ 502 /* translate if needed and pass on */
497 if (bp->b_flags & B_READ) { 503 if (bp->b_flags & B_READ) {
498 nilfs_read_filebuf(node, bp); 504 nilfs_read_filebuf(node, bp);
499 return bp->b_error; 505 return bp->b_error;
500 } 506 }
501 507
502 /* send to segment collector */ 508 /* send to segment collector */
503 nilfs_write_filebuf(node, bp); 509 nilfs_write_filebuf(node, bp);
504 return bp->b_error; 510 return bp->b_error;
505} 511}
506 512
507/* --------------------------------------------------------------------- */ 513/* --------------------------------------------------------------------- */
508 514
509int 515int
510nilfs_readdir(void *v) 516nilfs_readdir(void *v)
511{ 517{
512 struct vop_readdir_args /* { 518 struct vop_readdir_args /* {
513 struct vnode *a_vp; 519 struct vnode *a_vp;
514 struct uio *a_uio; 520 struct uio *a_uio;
515 kauth_cred_t a_cred; 521 kauth_cred_t a_cred;
516 int *a_eofflag; 522 int *a_eofflag;
517 off_t **a_cookies; 523 off_t **a_cookies;
518 int *a_ncookies; 524 int *a_ncookies;
519 } */ *ap = v; 525 } */ *ap = v;
520 struct uio *uio = ap->a_uio; 526 struct uio *uio = ap->a_uio;
521 struct vnode *vp = ap->a_vp; 527 struct vnode *vp = ap->a_vp;
522 struct nilfs_node *node = VTOI(vp); 528 struct nilfs_node *node = VTOI(vp);
523 struct nilfs_dir_entry *ndirent; 529 struct nilfs_dir_entry *ndirent;
524 struct dirent dirent; 530 struct dirent dirent;
525 struct buf *bp; 531 struct buf *bp;
526 uint64_t file_size, diroffset, transoffset, blkoff; 532 uint64_t file_size, diroffset, transoffset, blkoff;
527 uint64_t blocknr; 533 uint64_t blocknr;
528 uint32_t blocksize = node->nilfsdev->blocksize; 534 uint32_t blocksize = node->nilfsdev->blocksize;
529 uint8_t *pos, name_len; 535 uint8_t *pos, name_len;
530 int error; 536 int error;
531 537
532 DPRINTF(READDIR, ("nilfs_readdir called\n")); 538 DPRINTF(READDIR, ("nilfs_readdir called\n"));
533 539
534 if (vp->v_type != VDIR) 540 if (vp->v_type != VDIR)
535 return ENOTDIR; 541 return ENOTDIR;
536 542
537 file_size = nilfs_rw64(node->inode.i_size); 543 file_size = nilfs_rw64(node->inode.i_size);
538 544
539 /* we are called just as long as we keep on pushing data in */ 545 /* we are called just as long as we keep on pushing data in */
540 error = 0; 546 error = 0;
541 if ((uio->uio_offset < file_size) && 547 if ((uio->uio_offset < file_size) &&
542 (uio->uio_resid >= sizeof(struct dirent))) { 548 (uio->uio_resid >= sizeof(struct dirent))) {
543 diroffset = uio->uio_offset; 549 diroffset = uio->uio_offset;
544 transoffset = diroffset; 550 transoffset = diroffset;
545 551
546 blocknr = diroffset / blocksize; 552 blocknr = diroffset / blocksize;
547 blkoff = diroffset % blocksize; 553 blkoff = diroffset % blocksize;
548 error = nilfs_bread(node, blocknr, NOCRED, 0, &bp); 554 error = nilfs_bread(node, blocknr, NOCRED, 0, &bp);
549 if (error) 555 if (error)
550 return EIO; 556 return EIO;
551 while (diroffset < file_size) { 557 while (diroffset < file_size) {
552 DPRINTF(READDIR, ("readdir : offset = %"PRIu64"\n", 558 DPRINTF(READDIR, ("readdir : offset = %"PRIu64"\n",
553 diroffset)); 559 diroffset));
554 if (blkoff >= blocksize) { 560 if (blkoff >= blocksize) {
555 blkoff = 0; blocknr++; 561 blkoff = 0; blocknr++;
556 brelse(bp, BC_AGE); 562 brelse(bp, BC_AGE);
557 error = nilfs_bread(node, blocknr, NOCRED, 0, 563 error = nilfs_bread(node, blocknr, NOCRED, 0,
558 &bp); 564 &bp);
559 if (error) 565 if (error)
560 return EIO; 566 return EIO;
561 } 567 }
562 568
563 /* read in one dirent */ 569 /* read in one dirent */
564 pos = (uint8_t *) bp->b_data + blkoff; 570 pos = (uint8_t *) bp->b_data + blkoff;
565 ndirent = (struct nilfs_dir_entry *) pos; 571 ndirent = (struct nilfs_dir_entry *) pos;
566 572
567 name_len = ndirent->name_len; 573 name_len = ndirent->name_len;
568 memset(&dirent, 0, sizeof(struct dirent)); 574 memset(&dirent, 0, sizeof(struct dirent));
569 dirent.d_fileno = nilfs_rw64(ndirent->inode); 575 dirent.d_fileno = nilfs_rw64(ndirent->inode);
570 dirent.d_type = ndirent->file_type; /* 1:1 ? */ 576 dirent.d_type = ndirent->file_type; /* 1:1 ? */
571 dirent.d_namlen = name_len; 577 dirent.d_namlen = name_len;
572 strncpy(dirent.d_name, ndirent->name, name_len); 578 strncpy(dirent.d_name, ndirent->name, name_len);
573 dirent.d_reclen = _DIRENT_SIZE(&dirent); 579 dirent.d_reclen = _DIRENT_SIZE(&dirent);
574 DPRINTF(READDIR, ("copying `%*.*s`\n", name_len, 580 DPRINTF(READDIR, ("copying `%*.*s`\n", name_len,
575 name_len, dirent.d_name)); 581 name_len, dirent.d_name));
576 582
577 /*  583 /*
578 * If there isn't enough space in the uio to return a 584 * If there isn't enough space in the uio to return a
579 * whole dirent, break off read 585 * whole dirent, break off read
580 */ 586 */
581 if (uio->uio_resid < _DIRENT_SIZE(&dirent)) 587 if (uio->uio_resid < _DIRENT_SIZE(&dirent))
582 break; 588 break;
583 589
584 /* transfer */ 590 /* transfer */
585 if (name_len) 591 if (name_len)
586 uiomove(&dirent, _DIRENT_SIZE(&dirent), uio); 592 uiomove(&dirent, _DIRENT_SIZE(&dirent), uio);
587 593
588 /* advance */ 594 /* advance */
589 diroffset += nilfs_rw16(ndirent->rec_len); 595 diroffset += nilfs_rw16(ndirent->rec_len);
590 blkoff += nilfs_rw16(ndirent->rec_len); 596 blkoff += nilfs_rw16(ndirent->rec_len);
591 597
592 /* remember the last entry we transfered */ 598 /* remember the last entry we transfered */
593 transoffset = diroffset; 599 transoffset = diroffset;
594 } 600 }
595 brelse(bp, BC_AGE); 601 brelse(bp, BC_AGE);
596 602
597 /* pass on last transfered offset */ 603 /* pass on last transfered offset */
598 uio->uio_offset = transoffset; 604 uio->uio_offset = transoffset;
599 } 605 }
600 606
601 if (ap->a_eofflag) 607 if (ap->a_eofflag)
602 *ap->a_eofflag = (uio->uio_offset >= file_size); 608 *ap->a_eofflag = (uio->uio_offset >= file_size);
603 609
604 return error; 610 return error;
605} 611}
606 612
607/* --------------------------------------------------------------------- */ 613/* --------------------------------------------------------------------- */
608 614
609int 615int
610nilfs_lookup(void *v) 616nilfs_lookup(void *v)
611{ 617{
612 struct vop_lookup_args /* { 618 struct vop_lookup_args /* {
613 struct vnode *a_dvp; 619 struct vnode *a_dvp;
614 struct vnode **a_vpp; 620 struct vnode **a_vpp;
615 struct componentname *a_cnp; 621 struct componentname *a_cnp;
616 } */ *ap = v; 622 } */ *ap = v;
617 struct vnode *dvp = ap->a_dvp; 623 struct vnode *dvp = ap->a_dvp;
618 struct vnode **vpp = ap->a_vpp; 624 struct vnode **vpp = ap->a_vpp;
619 struct componentname *cnp = ap->a_cnp; 625 struct componentname *cnp = ap->a_cnp;
620 struct nilfs_node *dir_node, *res_node; 626 struct nilfs_node *dir_node, *res_node;
621 struct nilfs_mount *ump; 627 struct nilfs_mount *ump;
622 uint64_t ino; 628 uint64_t ino;
623 const char *name; 629 const char *name;
624 int namelen, nameiop, islastcn, mounted_ro; 630 int namelen, nameiop, islastcn, mounted_ro;
625 int vnodetp; 631 int vnodetp;
626 int error, found; 632 int error, found;
627 633
628 dir_node = VTOI(dvp); 634 dir_node = VTOI(dvp);
629 ump = dir_node->ump; 635 ump = dir_node->ump;
630 *vpp = NULL; 636 *vpp = NULL;
631 637
632 DPRINTF(LOOKUP, ("nilfs_lookup called\n")); 638 DPRINTF(LOOKUP, ("nilfs_lookup called\n"));
633 639
634 /* simplify/clarification flags */ 640 /* simplify/clarification flags */
635 nameiop = cnp->cn_nameiop; 641 nameiop = cnp->cn_nameiop;
636 islastcn = cnp->cn_flags & ISLASTCN; 642 islastcn = cnp->cn_flags & ISLASTCN;
637 mounted_ro = dvp->v_mount->mnt_flag & MNT_RDONLY; 643 mounted_ro = dvp->v_mount->mnt_flag & MNT_RDONLY;
638 644
639 /* check exec/dirread permissions first */ 645 /* check exec/dirread permissions first */
640 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred); 646 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
641 if (error) 647 if (error)
642 return error; 648 return error;
643 649
644 DPRINTF(LOOKUP, ("\taccess ok\n")); 650 DPRINTF(LOOKUP, ("\taccess ok\n"));
645 651
646 /* 652 /*
647 * If requesting a modify on the last path element on a read-only 653 * If requesting a modify on the last path element on a read-only
648 * filingsystem, reject lookup; XXX why is this repeated in every FS ? 654 * filingsystem, reject lookup; XXX why is this repeated in every FS ?
649 */ 655 */
650 if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME)) 656 if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
651 return EROFS; 657 return EROFS;
652 658
653 DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n", 659 DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n",
654 cnp->cn_nameptr)); 660 cnp->cn_nameptr));
655 /* look in the nami cache; returns 0 on success!! */ 661 /* look in the nami cache; returns 0 on success!! */
656 error = cache_lookup(dvp, vpp, cnp); 662 error = cache_lookup(dvp, vpp, cnp);
657 if (error >= 0) 663 if (error >= 0)
658 return error; 664 return error;
659 665
660 DPRINTF(LOOKUP, ("\tNOT found in cache\n")); 666 DPRINTF(LOOKUP, ("\tNOT found in cache\n"));
661 667
662 /* 668 /*
663 * Obviously, the file is not (anymore) in the namecache, we have to 669 * Obviously, the file is not (anymore) in the namecache, we have to
664 * search for it. There are three basic cases: '.', '..' and others. 670 * search for it. There are three basic cases: '.', '..' and others.
665 *  671 *
666 * Following the guidelines of VOP_LOOKUP manpage and tmpfs. 672 * Following the guidelines of VOP_LOOKUP manpage and tmpfs.
667 */ 673 */
668 error = 0; 674 error = 0;
669 if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) { 675 if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
670 DPRINTF(LOOKUP, ("\tlookup '.'\n")); 676 DPRINTF(LOOKUP, ("\tlookup '.'\n"));
671 /* special case 1 '.' */ 677 /* special case 1 '.' */
672 vref(dvp); 678 vref(dvp);
673 *vpp = dvp; 679 *vpp = dvp;
674 /* done */ 680 /* done */
675 } else if (cnp->cn_flags & ISDOTDOT) { 681 } else if (cnp->cn_flags & ISDOTDOT) {
676 /* special case 2 '..' */ 682 /* special case 2 '..' */
677 DPRINTF(LOOKUP, ("\tlookup '..'\n")); 683 DPRINTF(LOOKUP, ("\tlookup '..'\n"));
678 684
679 /* get our node */ 685 /* get our node */
680 name = ".."; 686 name = "..";
681 namelen = 2; 687 namelen = 2;
682 error = nilfs_lookup_name_in_dir(dvp, name, namelen, 688 error = nilfs_lookup_name_in_dir(dvp, name, namelen,
683 &ino, &found); 689 &ino, &found);
684 if (error) 690 if (error)
685 goto out; 691 goto out;
686 if (!found) 692 if (!found)
687 error = ENOENT; 693 error = ENOENT;
688 694
689 /* first unlock parent */ 695 /* first unlock parent */
690 VOP_UNLOCK(dvp); 696 VOP_UNLOCK(dvp);
691 697
692 if (error == 0) { 698 if (error == 0) {
693 DPRINTF(LOOKUP, ("\tfound '..'\n")); 699 DPRINTF(LOOKUP, ("\tfound '..'\n"));
694 /* try to create/reuse the node */ 700 /* try to create/reuse the node */
695 error = nilfs_get_node(ump, ino, &res_node); 701 error = nilfs_get_node(ump, ino, &res_node);
696 702
697 if (!error) { 703 if (!error) {
698 DPRINTF(LOOKUP, 704 DPRINTF(LOOKUP,
699 ("\tnode retrieved/created OK\n")); 705 ("\tnode retrieved/created OK\n"));
700 *vpp = res_node->vnode; 706 *vpp = res_node->vnode;
701 } 707 }
702 } 708 }
703 709
704 /* try to relock parent */ 710 /* try to relock parent */
705 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 711 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
706 } else { 712 } else {
707 DPRINTF(LOOKUP, ("\tlookup file\n")); 713 DPRINTF(LOOKUP, ("\tlookup file\n"));
708 /* all other files */ 714 /* all other files */
709 /* lookup filename in the directory returning its inode */ 715 /* lookup filename in the directory returning its inode */
710 name = cnp->cn_nameptr; 716 name = cnp->cn_nameptr;
711 namelen = cnp->cn_namelen; 717 namelen = cnp->cn_namelen;
712 error = nilfs_lookup_name_in_dir(dvp, name, namelen, 718 error = nilfs_lookup_name_in_dir(dvp, name, namelen,
713 &ino, &found); 719 &ino, &found);
714 if (error) 720 if (error)
715 goto out; 721 goto out;
716 if (!found) { 722 if (!found) {
717 DPRINTF(LOOKUP, ("\tNOT found\n")); 723 DPRINTF(LOOKUP, ("\tNOT found\n"));
718 /* 724 /*
719 * UGH, didn't find name. If we're creating or 725 * UGH, didn't find name. If we're creating or
720 * renaming on the last name this is OK and we ought 726 * renaming on the last name this is OK and we ought
721 * to return EJUSTRETURN if its allowed to be created. 727 * to return EJUSTRETURN if its allowed to be created.
722 */ 728 */
723 error = ENOENT; 729 error = ENOENT;
724 if (islastcn && 730 if (islastcn &&
725 (nameiop == CREATE || nameiop == RENAME)) 731 (nameiop == CREATE || nameiop == RENAME))
726 error = 0; 732 error = 0;
727 if (!error) { 733 if (!error) {
728 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred); 734 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
729 if (!error) { 735 if (!error) {
730 error = EJUSTRETURN; 736 error = EJUSTRETURN;
731 } 737 }
732 } 738 }
733 /* done */ 739 /* done */
734 } else { 740 } else {
735 /* try to create/reuse the node */ 741 /* try to create/reuse the node */
736 error = nilfs_get_node(ump, ino, &res_node); 742 error = nilfs_get_node(ump, ino, &res_node);
737 if (!error) { 743 if (!error) {
738 /* 744 /*
739 * If we are not at the last path component 745 * If we are not at the last path component
740 * and found a non-directory or non-link entry 746 * and found a non-directory or non-link entry
741 * (which may itself be pointing to a 747 * (which may itself be pointing to a
742 * directory), raise an error. 748 * directory), raise an error.
743 */ 749 */
744 vnodetp = res_node->vnode->v_type; 750 vnodetp = res_node->vnode->v_type;
745 if ((vnodetp != VDIR) && (vnodetp != VLNK)) { 751 if ((vnodetp != VDIR) && (vnodetp != VLNK)) {
746 if (!islastcn) 752 if (!islastcn)
747 error = ENOTDIR; 753 error = ENOTDIR;
748 } 754 }
749 755
750 } 756 }
751 if (!error) { 757 if (!error) {
752 *vpp = res_node->vnode; 758 *vpp = res_node->vnode;
753 } 759 }
754 } 760 }
755 }  761 }
756 762
757out: 763out:
758 /* 764 /*
759 * Store result in the cache if requested. If we are creating a file, 765 * Store result in the cache if requested. If we are creating a file,
760 * the file might not be found and thus putting it into the namecache 766 * the file might not be found and thus putting it into the namecache
761 * might be seen as negative caching. 767 * might be seen as negative caching.
762 */ 768 */
763 if (nameiop != CREATE) 769 if (nameiop != CREATE)
764 cache_enter(dvp, *vpp, cnp); 770 cache_enter(dvp, *vpp, cnp);
765 771
766 DPRINTFIF(LOOKUP, error, ("nilfs_lookup returing error %d\n", error)); 772 DPRINTFIF(LOOKUP, error, ("nilfs_lookup returing error %d\n", error));
767 773
768 return error; 774 return error;
769} 775}
770 776
771/* --------------------------------------------------------------------- */ 777/* --------------------------------------------------------------------- */
772 778
773static void 779static void
774nilfs_ctime_to_timespec(struct timespec *ts, uint64_t ctime) 780nilfs_ctime_to_timespec(struct timespec *ts, uint64_t ctime)
775{ 781{
776 ts->tv_sec = ctime; 782 ts->tv_sec = ctime;
777 ts->tv_nsec = 0; 783 ts->tv_nsec = 0;
778} 784}
779 785
780 786
781int 787int
782nilfs_getattr(void *v) 788nilfs_getattr(void *v)
783{ 789{
784 struct vop_getattr_args /* { 790 struct vop_getattr_args /* {
785 struct vnode *a_vp; 791 struct vnode *a_vp;
786 struct vattr *a_vap; 792 struct vattr *a_vap;
787 kauth_cred_t a_cred; 793 kauth_cred_t a_cred;
788 struct lwp *a_l; 794 struct lwp *a_l;
789 } */ *ap = v; 795 } */ *ap = v;
790 struct vnode *vp = ap->a_vp; 796 struct vnode *vp = ap->a_vp;
791 struct vattr *vap = ap->a_vap; 797 struct vattr *vap = ap->a_vap;
792 struct nilfs_node *node = VTOI(vp); 798 struct nilfs_node *node = VTOI(vp);
793 struct nilfs_inode *inode = &node->inode; 799 struct nilfs_inode *inode = &node->inode;
794 800
795 DPRINTF(VFSCALL, ("nilfs_getattr called\n")); 801 DPRINTF(VFSCALL, ("nilfs_getattr called\n"));
796 802
797 /* basic info */ 803 /* basic info */
798 vattr_null(vap); 804 vattr_null(vap);
799 vap->va_type = vp->v_type; 805 vap->va_type = vp->v_type;
800 vap->va_mode = nilfs_rw16(inode->i_mode); /* XXX same? */ 806 vap->va_mode = nilfs_rw16(inode->i_mode); /* XXX same? */
801 vap->va_nlink = nilfs_rw16(inode->i_links_count); 807 vap->va_nlink = nilfs_rw16(inode->i_links_count);
802 vap->va_uid = nilfs_rw32(inode->i_uid); 808 vap->va_uid = nilfs_rw32(inode->i_uid);
803 vap->va_gid = nilfs_rw32(inode->i_gid); 809 vap->va_gid = nilfs_rw32(inode->i_gid);
804 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 810 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
805 vap->va_fileid = node->ino; 811 vap->va_fileid = node->ino;
806 vap->va_size = nilfs_rw64(inode->i_size); 812 vap->va_size = nilfs_rw64(inode->i_size);
807 vap->va_blocksize = node->nilfsdev->blocksize; 813 vap->va_blocksize = node->nilfsdev->blocksize;
808 814
809 /* times */ 815 /* times */
810 nilfs_ctime_to_timespec(&vap->va_atime, nilfs_rw64(inode->i_mtime)); 816 nilfs_ctime_to_timespec(&vap->va_atime, nilfs_rw64(inode->i_mtime));
811 nilfs_ctime_to_timespec(&vap->va_mtime, nilfs_rw64(inode->i_mtime)); 817 nilfs_ctime_to_timespec(&vap->va_mtime, nilfs_rw64(inode->i_mtime));
812 nilfs_ctime_to_timespec(&vap->va_ctime, nilfs_rw64(inode->i_ctime)); 818 nilfs_ctime_to_timespec(&vap->va_ctime, nilfs_rw64(inode->i_ctime));
813 nilfs_ctime_to_timespec(&vap->va_birthtime, nilfs_rw64(inode->i_ctime)); 819 nilfs_ctime_to_timespec(&vap->va_birthtime, nilfs_rw64(inode->i_ctime));
814 820
815 vap->va_gen = nilfs_rw32(inode->i_generation); 821 vap->va_gen = nilfs_rw32(inode->i_generation);
816 vap->va_flags = 0; /* vattr flags */ 822 vap->va_flags = 0; /* vattr flags */
817 vap->va_bytes = nilfs_rw64(inode->i_blocks) * vap->va_blocksize; 823 vap->va_bytes = nilfs_rw64(inode->i_blocks) * vap->va_blocksize;
818 vap->va_filerev = vap->va_gen; /* XXX file revision? same as gen? */ 824 vap->va_filerev = vap->va_gen; /* XXX file revision? same as gen? */
819 vap->va_vaflags = 0; /* XXX chflags flags */ 825 vap->va_vaflags = 0; /* XXX chflags flags */
820 826
821 return 0; 827 return 0;
822} 828}
823 829
824/* --------------------------------------------------------------------- */ 830/* --------------------------------------------------------------------- */
825 831
826#if 0 832#if 0
827static int 833static int
828nilfs_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid, 834nilfs_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid,
829 kauth_cred_t cred) 835 kauth_cred_t cred)
830{ 836{
831 return EINVAL; 837 return EINVAL;
832} 838}
833 839
834 840
835static int 841static int
836nilfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred) 842nilfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred)
837{ 843{
838 844
839 return EINVAL; 845 return EINVAL;
840} 846}
841 847
842 848
843/* exported */ 849/* exported */
844int 850int
845nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred) 851nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
846{ 852{
847 return EINVAL; 853 return EINVAL;
848} 854}
849 855
850 856
851static int 857static int
852nilfs_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred) 858nilfs_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred)
853{ 859{
854 return EINVAL; 860 return EINVAL;
855} 861}
856 862
857 863
858static int 864static int
859nilfs_chtimes(struct vnode *vp, 865nilfs_chtimes(struct vnode *vp,
860 struct timespec *atime, struct timespec *mtime, 866 struct timespec *atime, struct timespec *mtime,
861 struct timespec *birthtime, int setattrflags, 867 struct timespec *birthtime, int setattrflags,
862 kauth_cred_t cred) 868 kauth_cred_t cred)
863{ 869{
864 return EINVAL; 870 return EINVAL;
865} 871}
866#endif 872#endif
867 873
868 874
869int 875int
870nilfs_setattr(void *v) 876nilfs_setattr(void *v)
871{ 877{
872 struct vop_setattr_args /* { 878 struct vop_setattr_args /* {
873 struct vnode *a_vp; 879 struct vnode *a_vp;
874 struct vattr *a_vap; 880 struct vattr *a_vap;
875 kauth_cred_t a_cred; 881 kauth_cred_t a_cred;
876 struct lwp *a_l; 882 struct lwp *a_l;
877 } */ *ap = v; 883 } */ *ap = v;
878 struct vnode *vp = ap->a_vp; 884 struct vnode *vp = ap->a_vp;
879 885
880 vp = vp; 886 vp = vp;
881 DPRINTF(VFSCALL, ("nilfs_setattr called\n")); 887 DPRINTF(VFSCALL, ("nilfs_setattr called\n"));
882 return EINVAL; 888 return EINVAL;
883} 889}
884 890
885/* --------------------------------------------------------------------- */ 891/* --------------------------------------------------------------------- */
886 892
887/* 893/*
888 * Return POSIX pathconf information for NILFS file systems. 894 * Return POSIX pathconf information for NILFS file systems.
889 */ 895 */
890int 896int
891nilfs_pathconf(void *v) 897nilfs_pathconf(void *v)
892{ 898{
893 struct vop_pathconf_args /* { 899 struct vop_pathconf_args /* {
894 struct vnode *a_vp; 900 struct vnode *a_vp;
895 int a_name; 901 int a_name;
896 register_t *a_retval; 902 register_t *a_retval;
897 } */ *ap = v; 903 } */ *ap = v;
898 uint32_t bits; 904 uint32_t bits;
899 905
900 DPRINTF(VFSCALL, ("nilfs_pathconf called\n")); 906 DPRINTF(VFSCALL, ("nilfs_pathconf called\n"));
901 907
902 switch (ap->a_name) { 908 switch (ap->a_name) {
903 case _PC_LINK_MAX: 909 case _PC_LINK_MAX:
904 *ap->a_retval = (1<<16)-1; /* 16 bits */ 910 *ap->a_retval = (1<<16)-1; /* 16 bits */
905 return 0; 911 return 0;
906 case _PC_NAME_MAX: 912 case _PC_NAME_MAX:
907 *ap->a_retval = NILFS_MAXNAMLEN; 913 *ap->a_retval = NILFS_MAXNAMLEN;
908 return 0; 914 return 0;
909 case _PC_PATH_MAX: 915 case _PC_PATH_MAX:
910 *ap->a_retval = PATH_MAX; 916 *ap->a_retval = PATH_MAX;
911 return 0; 917 return 0;
912 case _PC_PIPE_BUF: 918 case _PC_PIPE_BUF:
913 *ap->a_retval = PIPE_BUF; 919 *ap->a_retval = PIPE_BUF;
914 return 0; 920 return 0;
915 case _PC_CHOWN_RESTRICTED: 921 case _PC_CHOWN_RESTRICTED:
916 *ap->a_retval = 1; 922 *ap->a_retval = 1;
917 return 0; 923 return 0;
918 case _PC_NO_TRUNC: 924 case _PC_NO_TRUNC:
919 *ap->a_retval = 1; 925 *ap->a_retval = 1;
920 return 0; 926 return 0;
921 case _PC_SYNC_IO: 927 case _PC_SYNC_IO:
922 *ap->a_retval = 0; /* synchronised is off for performance */ 928 *ap->a_retval = 0; /* synchronised is off for performance */
923 return 0; 929 return 0;
924 case _PC_FILESIZEBITS: 930 case _PC_FILESIZEBITS:
925 /* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */ 931 /* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */
926 bits = 64; /* XXX ought to deliver 65 */ 932 bits = 64; /* XXX ought to deliver 65 */
927#if 0 933#if 0
928 if (nilfs_node) 934 if (nilfs_node)
929 bits = 64 * vp->v_mount->mnt_dev_bshift; 935 bits = 64 * vp->v_mount->mnt_dev_bshift;
930#endif 936#endif
931 *ap->a_retval = bits; 937 *ap->a_retval = bits;
932 return 0; 938 return 0;
933 } 939 }
934 940
935 return EINVAL; 941 return EINVAL;
936} 942}
937 943
938 944
939/* --------------------------------------------------------------------- */ 945/* --------------------------------------------------------------------- */
940 946
941int 947int
942nilfs_open(void *v) 948nilfs_open(void *v)
943{ 949{
944 struct vop_open_args /* { 950 struct vop_open_args /* {
945 struct vnode *a_vp; 951 struct vnode *a_vp;
946 int a_mode; 952 int a_mode;
947 kauth_cred_t a_cred; 953 kauth_cred_t a_cred;
948 struct proc *a_p; 954 struct proc *a_p;
949 } */ *ap = v; 955 } */ *ap = v;
950 int flags; 956 int flags;
951 957
952 DPRINTF(VFSCALL, ("nilfs_open called\n")); 958 DPRINTF(VFSCALL, ("nilfs_open called\n"));
953 959
954 /* 960 /*
955 * Files marked append-only must be opened for appending. 961 * Files marked append-only must be opened for appending.
956 */ 962 */
957 flags = 0; 963 flags = 0;
958 if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) 964 if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
959 return (EPERM); 965 return (EPERM);
960 966
961 return 0; 967 return 0;
962} 968}
963 969
964 970
965/* --------------------------------------------------------------------- */ 971/* --------------------------------------------------------------------- */
966 972
967int 973int
968nilfs_close(void *v) 974nilfs_close(void *v)
969{ 975{
970 struct vop_close_args /* { 976 struct vop_close_args /* {
971 struct vnode *a_vp; 977 struct vnode *a_vp;
972 int a_fflag; 978 int a_fflag;
973 kauth_cred_t a_cred; 979 kauth_cred_t a_cred;
974 struct proc *a_p; 980 struct proc *a_p;
975 } */ *ap = v; 981 } */ *ap = v;
976 struct vnode *vp = ap->a_vp; 982 struct vnode *vp = ap->a_vp;
977 struct nilfs_node *nilfs_node = VTOI(vp); 983 struct nilfs_node *nilfs_node = VTOI(vp);
978 984
979 DPRINTF(VFSCALL, ("nilfs_close called\n")); 985 DPRINTF(VFSCALL, ("nilfs_close called\n"));
980 nilfs_node = nilfs_node; /* shut up gcc */ 986 nilfs_node = nilfs_node; /* shut up gcc */
981 987
982 mutex_enter(vp->v_interlock); 988 mutex_enter(vp->v_interlock);
983 if (vp->v_usecount > 1) 989 if (vp->v_usecount > 1)
984 nilfs_itimes(nilfs_node, NULL, NULL, NULL); 990 nilfs_itimes(nilfs_node, NULL, NULL, NULL);
985 mutex_exit(vp->v_interlock); 991 mutex_exit(vp->v_interlock);
986 992
987 return 0; 993 return 0;
988} 994}
989 995
990 996
991/* --------------------------------------------------------------------- */ 997/* --------------------------------------------------------------------- */
992 998
993static int 999static int
994nilfs_check_possible(struct vnode *vp, struct vattr *vap, mode_t mode) 1000nilfs_check_possible(struct vnode *vp, struct vattr *vap, mode_t mode)
995{ 1001{
996 int flags; 1002 int flags;
997 1003
998 /* check if we are allowed to write */ 1004 /* check if we are allowed to write */
999 switch (vap->va_type) { 1005 switch (vap->va_type) {
1000 case VDIR: 1006 case VDIR:
1001 case VLNK: 1007 case VLNK:
1002 case VREG: 1008 case VREG:
1003 /* 1009 /*
1004 * normal nodes: check if we're on a read-only mounted 1010 * normal nodes: check if we're on a read-only mounted
1005 * filingsystem and bomb out if we're trying to write. 1011 * filingsystem and bomb out if we're trying to write.
1006 */ 1012 */
1007 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) 1013 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
1008 return EROFS; 1014 return EROFS;
1009 break; 1015 break;
1010 case VBLK: 1016 case VBLK:
1011 case VCHR: 1017 case VCHR:
1012 case VSOCK: 1018 case VSOCK:
1013 case VFIFO: 1019 case VFIFO:
1014 /* 1020 /*
1015 * special nodes: even on read-only mounted filingsystems 1021 * special nodes: even on read-only mounted filingsystems
1016 * these are allowed to be written to if permissions allow. 1022 * these are allowed to be written to if permissions allow.
1017 */ 1023 */
1018 break; 1024 break;
1019 default: 1025 default:
1020 /* no idea what this is */ 1026 /* no idea what this is */
1021 return EINVAL; 1027 return EINVAL;
1022 } 1028 }
1023 1029
1024 /* noone may write immutable files */ 1030 /* noone may write immutable files */
1025 /* TODO: get chflags(2) flags */ 1031 /* TODO: get chflags(2) flags */
1026 flags = 0; 1032 flags = 0;
1027 if ((mode & VWRITE) && (flags & IMMUTABLE)) 1033 if ((mode & VWRITE) && (flags & IMMUTABLE))
1028 return EPERM; 1034 return EPERM;
1029 1035
1030 return 0; 1036 return 0;
1031} 1037}
1032 1038
1033static int 1039static int
1034nilfs_check_permitted(struct vnode *vp, struct vattr *vap, mode_t mode, 1040nilfs_check_permitted(struct vnode *vp, struct vattr *vap, mode_t mode,
1035 kauth_cred_t cred) 1041 kauth_cred_t cred)
1036{ 1042{
1037 1043
1038 /* ask the generic genfs_can_access to advice on security */ 1044 /* ask the generic genfs_can_access to advice on security */
1039 return genfs_can_access(vp->v_type, 1045 return genfs_can_access(vp->v_type,
1040 vap->va_mode, vap->va_uid, vap->va_gid, 1046 vap->va_mode, vap->va_uid, vap->va_gid,
1041 mode, cred); 1047 mode, cred);
1042} 1048}
1043 1049
1044int 1050int
1045nilfs_access(void *v) 1051nilfs_access(void *v)
1046{ 1052{
1047 struct vop_access_args /* { 1053 struct vop_access_args /* {
1048 struct vnode *a_vp; 1054 struct vnode *a_vp;
1049 int a_mode; 1055 int a_mode;
1050 kauth_cred_t a_cred; 1056 kauth_cred_t a_cred;
1051 struct proc *a_p; 1057 struct proc *a_p;
1052 } */ *ap = v; 1058 } */ *ap = v;
1053 struct vnode *vp = ap->a_vp; 1059 struct vnode *vp = ap->a_vp;
1054 mode_t mode = ap->a_mode; 1060 mode_t mode = ap->a_mode;
1055 kauth_cred_t cred = ap->a_cred; 1061 kauth_cred_t cred = ap->a_cred;
1056 /* struct nilfs_node *nilfs_node = VTOI(vp); */ 1062 /* struct nilfs_node *nilfs_node = VTOI(vp); */
1057 struct vattr vap; 1063 struct vattr vap;
1058 int error; 1064 int error;
1059 1065
1060 DPRINTF(VFSCALL, ("nilfs_access called\n")); 1066 DPRINTF(VFSCALL, ("nilfs_access called\n"));
1061 1067
1062 error = VOP_GETATTR(vp, &vap, NULL); 1068 error = VOP_GETATTR(vp, &vap, NULL);
1063 if (error) 1069 if (error)
1064 return error; 1070 return error;
1065 1071
1066 error = nilfs_check_possible(vp, &vap, mode); 1072 error = nilfs_check_possible(vp, &vap, mode);
1067 if (error) 1073 if (error)
1068 return error; 1074 return error;
1069 1075
1070 error = nilfs_check_permitted(vp, &vap, mode, cred); 1076 error = nilfs_check_permitted(vp, &vap, mode, cred);
1071 1077
1072 return error; 1078 return error;
1073} 1079}
1074 1080
1075/* --------------------------------------------------------------------- */ 1081/* --------------------------------------------------------------------- */
1076 1082
1077int 1083int
1078nilfs_create(void *v) 1084nilfs_create(void *v)
1079{ 1085{
1080 struct vop_create_args /* { 1086 struct vop_create_args /* {
1081 struct vnode *a_dvp; 1087 struct vnode *a_dvp;
1082 struct vnode **a_vpp; 1088 struct vnode **a_vpp;
1083 struct componentname *a_cnp; 1089 struct componentname *a_cnp;
1084 struct vattr *a_vap; 1090 struct vattr *a_vap;
1085 } */ *ap = v; 1091 } */ *ap = v;
1086 struct vnode *dvp = ap->a_dvp; 1092 struct vnode *dvp = ap->a_dvp;
1087 struct vnode **vpp = ap->a_vpp; 1093 struct vnode **vpp = ap->a_vpp;
1088 struct vattr *vap = ap->a_vap; 1094 struct vattr *vap = ap->a_vap;
1089 struct componentname *cnp = ap->a_cnp; 1095 struct componentname *cnp = ap->a_cnp;
1090 int error; 1096 int error;
1091 1097
1092 DPRINTF(VFSCALL, ("nilfs_create called\n")); 1098 DPRINTF(VFSCALL, ("nilfs_create called\n"));
1093 error = nilfs_create_node(dvp, vpp, vap, cnp); 1099 error = nilfs_create_node(dvp, vpp, vap, cnp);
1094 1100
1095 vput(dvp); 1101 vput(dvp);
1096 return error; 1102 return error;
1097} 1103}
1098 1104
1099/* --------------------------------------------------------------------- */ 1105/* --------------------------------------------------------------------- */
1100 1106
1101int 1107int
1102nilfs_mknod(void *v) 1108nilfs_mknod(void *v)
1103{ 1109{
1104 struct vop_mknod_args /* { 1110 struct vop_mknod_args /* {
1105 struct vnode *a_dvp; 1111 struct vnode *a_dvp;
1106 struct vnode **a_vpp; 1112 struct vnode **a_vpp;
1107 struct componentname *a_cnp; 1113 struct componentname *a_cnp;
1108 struct vattr *a_vap; 1114 struct vattr *a_vap;
1109 } */ *ap = v; 1115 } */ *ap = v;
1110 struct vnode *dvp = ap->a_dvp; 1116 struct vnode *dvp = ap->a_dvp;
1111 struct vnode **vpp = ap->a_vpp; 1117 struct vnode **vpp = ap->a_vpp;
1112 struct vattr *vap = ap->a_vap; 1118 struct vattr *vap = ap->a_vap;
1113 struct componentname *cnp = ap->a_cnp; 1119 struct componentname *cnp = ap->a_cnp;
1114 int error; 1120 int error;
1115 1121
1116 DPRINTF(VFSCALL, ("nilfs_mknod called\n")); 1122 DPRINTF(VFSCALL, ("nilfs_mknod called\n"));
1117 error = nilfs_create_node(dvp, vpp, vap, cnp); 1123 error = nilfs_create_node(dvp, vpp, vap, cnp);
1118 1124
1119 vput(dvp); 1125 vput(dvp);
1120 return error; 1126 return error;
1121} 1127}
1122 1128
1123/* --------------------------------------------------------------------- */ 1129/* --------------------------------------------------------------------- */
1124 1130
1125int 1131int
1126nilfs_mkdir(void *v) 1132nilfs_mkdir(void *v)
1127{ 1133{
1128 struct vop_mkdir_args /* { 1134 struct vop_mkdir_args /* {
1129 struct vnode *a_dvp; 1135 struct vnode *a_dvp;
1130 struct vnode **a_vpp; 1136 struct vnode **a_vpp;
1131 struct componentname *a_cnp; 1137 struct componentname *a_cnp;
1132 struct vattr *a_vap; 1138 struct vattr *a_vap;
1133 } */ *ap = v; 1139 } */ *ap = v;
1134 struct vnode *dvp = ap->a_dvp; 1140 struct vnode *dvp = ap->a_dvp;
1135 struct vnode **vpp = ap->a_vpp; 1141 struct vnode **vpp = ap->a_vpp;
1136 struct vattr *vap = ap->a_vap; 1142 struct vattr *vap = ap->a_vap;
1137 struct componentname *cnp = ap->a_cnp; 1143 struct componentname *cnp = ap->a_cnp;
1138 int error; 1144 int error;
1139 1145
1140 DPRINTF(VFSCALL, ("nilfs_mkdir called\n")); 1146 DPRINTF(VFSCALL, ("nilfs_mkdir called\n"));
1141 error = nilfs_create_node(dvp, vpp, vap, cnp); 1147 error = nilfs_create_node(dvp, vpp, vap, cnp);
1142 1148
1143 vput(dvp); 1149 vput(dvp);
1144 return error; 1150 return error;
1145} 1151}
1146 1152
1147/* --------------------------------------------------------------------- */ 1153/* --------------------------------------------------------------------- */
1148 1154
1149static int 1155static int
1150nilfs_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp) 1156nilfs_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1151{ 1157{
1152 struct nilfs_node *nilfs_node, *dir_node; 1158 struct nilfs_node *nilfs_node, *dir_node;
1153 struct vattr vap; 1159 struct vattr vap;
1154 int error; 1160 int error;
1155 1161
1156 DPRINTF(VFSCALL, ("nilfs_link called\n")); 1162 DPRINTF(VFSCALL, ("nilfs_link called\n"));
1157 KASSERT(dvp != vp); 1163 KASSERT(dvp != vp);
1158 KASSERT(vp->v_type != VDIR); 1164 KASSERT(vp->v_type != VDIR);
1159 KASSERT(dvp->v_mount == vp->v_mount); 1165 KASSERT(dvp->v_mount == vp->v_mount);
1160 1166
1161 /* lock node */ 1167 /* lock node */
1162 error = vn_lock(vp, LK_EXCLUSIVE); 1168 error = vn_lock(vp, LK_EXCLUSIVE);
1163 if (error) 1169 if (error)
1164 return error; 1170 return error;
1165 1171
1166 /* get attributes */ 1172 /* get attributes */
1167 dir_node = VTOI(dvp); 1173 dir_node = VTOI(dvp);
1168 nilfs_node = VTOI(vp); 1174 nilfs_node = VTOI(vp);
1169 1175
1170 error = VOP_GETATTR(vp, &vap, FSCRED); 1176 error = VOP_GETATTR(vp, &vap, FSCRED);
1171 if (error) { 1177 if (error) {
1172 VOP_UNLOCK(vp); 1178 VOP_UNLOCK(vp);
1173 return error; 1179 return error;
1174 } 1180 }
1175 1181
1176 /* check link count overflow */ 1182 /* check link count overflow */
1177 if (vap.va_nlink >= (1<<16)-1) { /* uint16_t */ 1183 if (vap.va_nlink >= (1<<16)-1) { /* uint16_t */
1178 VOP_UNLOCK(vp); 1184 VOP_UNLOCK(vp);
1179 return EMLINK; 1185 return EMLINK;
1180 } 1186 }
1181 1187
1182 error = nilfs_dir_attach(dir_node->ump, dir_node, nilfs_node, 1188 error = nilfs_dir_attach(dir_node->ump, dir_node, nilfs_node,
1183 &vap, cnp); 1189 &vap, cnp);
1184 if (error) 1190 if (error)
1185 VOP_UNLOCK(vp); 1191 VOP_UNLOCK(vp);
1186 return error; 1192 return error;
1187} 1193}
1188 1194
1189int 1195int
1190nilfs_link(void *v) 1196nilfs_link(void *v)
1191{ 1197{
1192 struct vop_link_args /* { 1198 struct vop_link_args /* {
1193 struct vnode *a_dvp; 1199 struct vnode *a_dvp;
1194 struct vnode *a_vp; 1200 struct vnode *a_vp;
1195 struct componentname *a_cnp; 1201 struct componentname *a_cnp;
1196 } */ *ap = v; 1202 } */ *ap = v;
1197 struct vnode *dvp = ap->a_dvp; 1203 struct vnode *dvp = ap->a_dvp;
1198 struct vnode *vp = ap->a_vp; 1204 struct vnode *vp = ap->a_vp;
1199 struct componentname *cnp = ap->a_cnp; 1205 struct componentname *cnp = ap->a_cnp;
1200 int error; 1206 int error;
1201 1207
1202 error = nilfs_do_link(dvp, vp, cnp); 1208 error = nilfs_do_link(dvp, vp, cnp);
1203 if (error) 1209 if (error)
1204 VOP_ABORTOP(dvp, cnp); 1210 VOP_ABORTOP(dvp, cnp);
1205 1211
1206 VN_KNOTE(vp, NOTE_LINK); 1212 VN_KNOTE(vp, NOTE_LINK);
1207 VN_KNOTE(dvp, NOTE_WRITE); 1213 VN_KNOTE(dvp, NOTE_WRITE);
1208 vput(dvp); 1214 vput(dvp);
1209 1215
1210 return error; 1216 return error;
1211} 1217}
1212 1218
1213/* --------------------------------------------------------------------- */ 1219/* --------------------------------------------------------------------- */
1214 1220
1215static int 1221static int
1216nilfs_do_symlink(struct nilfs_node *nilfs_node, char *target) 1222nilfs_do_symlink(struct nilfs_node *nilfs_node, char *target)
1217{ 1223{
1218 return EROFS; 1224 return EROFS;
1219} 1225}
1220 1226
1221 1227
1222int 1228int
1223nilfs_symlink(void *v) 1229nilfs_symlink(void *v)
1224{ 1230{
1225 struct vop_symlink_args /* { 1231 struct vop_symlink_args /* {
1226 struct vnode *a_dvp; 1232 struct vnode *a_dvp;
1227 struct vnode **a_vpp; 1233 struct vnode **a_vpp;
1228 struct componentname *a_cnp; 1234 struct componentname *a_cnp;
1229 struct vattr *a_vap; 1235 struct vattr *a_vap;
1230 char *a_target; 1236 char *a_target;
1231 } */ *ap = v; 1237 } */ *ap = v;
1232 struct vnode *dvp = ap->a_dvp; 1238 struct vnode *dvp = ap->a_dvp;
1233 struct vnode **vpp = ap->a_vpp; 1239 struct vnode **vpp = ap->a_vpp;
1234 struct vattr *vap = ap->a_vap; 1240 struct vattr *vap = ap->a_vap;
1235 struct componentname *cnp = ap->a_cnp; 1241 struct componentname *cnp = ap->a_cnp;
1236 struct nilfs_node *dir_node; 1242 struct nilfs_node *dir_node;
1237 struct nilfs_node *nilfs_node; 1243 struct nilfs_node *nilfs_node;
1238 int error; 1244 int error;
1239 1245
1240 DPRINTF(VFSCALL, ("nilfs_symlink called\n")); 1246 DPRINTF(VFSCALL, ("nilfs_symlink called\n"));
1241 DPRINTF(VFSCALL, ("\tlinking to `%s`\n", ap->a_target)); 1247 DPRINTF(VFSCALL, ("\tlinking to `%s`\n", ap->a_target));
1242 error = nilfs_create_node(dvp, vpp, vap, cnp); 1248 error = nilfs_create_node(dvp, vpp, vap, cnp);
1243 KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL)))); 1249 KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL))));
1244 if (!error) { 1250 if (!error) {
1245 dir_node = VTOI(dvp); 1251 dir_node = VTOI(dvp);
1246 nilfs_node = VTOI(*vpp); 1252 nilfs_node = VTOI(*vpp);
1247 KASSERT(nilfs_node); 1253 KASSERT(nilfs_node);
1248 error = nilfs_do_symlink(nilfs_node, ap->a_target); 1254 error = nilfs_do_symlink(nilfs_node, ap->a_target);
1249 if (error) { 1255 if (error) {
1250 /* remove node */ 1256 /* remove node */
1251 nilfs_shrink_node(nilfs_node, 0); 1257 nilfs_shrink_node(nilfs_node, 0);
1252 nilfs_dir_detach(nilfs_node->ump, dir_node, nilfs_node, cnp); 1258 nilfs_dir_detach(nilfs_node->ump, dir_node, nilfs_node, cnp);
1253 } 1259 }
1254 } 1260 }
1255 vput(dvp); 1261 vput(dvp);
1256 return error; 1262 return error;
1257} 1263}
1258 1264
1259/* --------------------------------------------------------------------- */ 1265/* --------------------------------------------------------------------- */
1260 1266
1261int 1267int
1262nilfs_readlink(void *v) 1268nilfs_readlink(void *v)
1263{ 1269{
1264 struct vop_readlink_args /* { 1270 struct vop_readlink_args /* {
1265 struct vnode *a_vp; 1271 struct vnode *a_vp;
1266 struct uio *a_uio; 1272 struct uio *a_uio;
1267 kauth_cred_t a_cred; 1273 kauth_cred_t a_cred;
1268 } */ *ap = v; 1274 } */ *ap = v;
1269#if 0 1275#if 0
1270 struct vnode *vp = ap->a_vp; 1276 struct vnode *vp = ap->a_vp;
1271 struct uio *uio = ap->a_uio; 1277 struct uio *uio = ap->a_uio;
1272 kauth_cred_t cred = ap->a_cred; 1278 kauth_cred_t cred = ap->a_cred;
1273 struct nilfs_node *nilfs_node; 1279 struct nilfs_node *nilfs_node;
1274 struct pathcomp pathcomp; 1280 struct pathcomp pathcomp;
1275 struct vattr vattr; 1281 struct vattr vattr;
1276 uint8_t *pathbuf, *targetbuf, *tmpname; 1282 uint8_t *pathbuf, *targetbuf, *tmpname;
1277 uint8_t *pathpos, *targetpos; 1283 uint8_t *pathpos, *targetpos;
1278 char *mntonname; 1284 char *mntonname;
1279 int pathlen, targetlen, namelen, mntonnamelen, len, l_ci; 1285 int pathlen, targetlen, namelen, mntonnamelen, len, l_ci;
1280 int first, error; 1286 int first, error;
1281#endif 1287#endif
1282 ap = ap; 1288 ap = ap;
1283 1289
1284 DPRINTF(VFSCALL, ("nilfs_readlink called\n")); 1290 DPRINTF(VFSCALL, ("nilfs_readlink called\n"));
1285 1291
1286 return EROFS; 1292 return EROFS;
1287} 1293}
1288 1294
1289/* --------------------------------------------------------------------- */ 1295/* --------------------------------------------------------------------- */
1290 1296
1291/* note: i tried to follow the logics of the tmpfs rename code */ 1297/* note: i tried to follow the logics of the tmpfs rename code */
1292int 1298int
1293nilfs_rename(void *v) 1299nilfs_rename(void *v)
1294{ 1300{
1295 struct vop_rename_args /* { 1301 struct vop_rename_args /* {
1296 struct vnode *a_fdvp; 1302 struct vnode *a_fdvp;
1297 struct vnode *a_fvp; 1303 struct vnode *a_fvp;
1298 struct componentname *a_fcnp; 1304 struct componentname *a_fcnp;
1299 struct vnode *a_tdvp; 1305 struct vnode *a_tdvp;
1300 struct vnode *a_tvp; 1306 struct vnode *a_tvp;
1301 struct componentname *a_tcnp; 1307 struct componentname *a_tcnp;
1302 } */ *ap = v; 1308 } */ *ap = v;
1303 struct vnode *tvp = ap->a_tvp; 1309 struct vnode *tvp = ap->a_tvp;
1304 struct vnode *tdvp = ap->a_tdvp; 1310 struct vnode *tdvp = ap->a_tdvp;
1305 struct vnode *fvp = ap->a_fvp; 1311 struct vnode *fvp = ap->a_fvp;
1306 struct vnode *fdvp = ap->a_fdvp; 1312 struct vnode *fdvp = ap->a_fdvp;
1307 struct componentname *tcnp = ap->a_tcnp; 1313 struct componentname *tcnp = ap->a_tcnp;
1308 struct componentname *fcnp = ap->a_fcnp; 1314 struct componentname *fcnp = ap->a_fcnp;
1309 struct nilfs_node *fnode, *fdnode, *tnode, *tdnode; 1315 struct nilfs_node *fnode, *fdnode, *tnode, *tdnode;
1310 struct vattr fvap, tvap; 1316 struct vattr fvap, tvap;
1311 int error; 1317 int error;
1312 1318
1313 DPRINTF(VFSCALL, ("nilfs_rename called\n")); 1319 DPRINTF(VFSCALL, ("nilfs_rename called\n"));
1314 1320
1315 /* disallow cross-device renames */ 1321 /* disallow cross-device renames */
1316 if (fvp->v_mount != tdvp->v_mount || 1322 if (fvp->v_mount != tdvp->v_mount ||
1317 (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 1323 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1318 error = EXDEV; 1324 error = EXDEV;
1319 goto out_unlocked; 1325 goto out_unlocked;
1320 } 1326 }
1321 1327
1322 fnode = VTOI(fvp); 1328 fnode = VTOI(fvp);
1323 fdnode = VTOI(fdvp); 1329 fdnode = VTOI(fdvp);
1324 tnode = (tvp == NULL) ? NULL : VTOI(tvp); 1330 tnode = (tvp == NULL) ? NULL : VTOI(tvp);
1325 tdnode = VTOI(tdvp); 1331 tdnode = VTOI(tdvp);
1326 1332
1327 /* lock our source dir */ 1333 /* lock our source dir */
1328 if (fdnode != tdnode) { 1334 if (fdnode != tdnode) {
1329 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY); 1335 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
1330 if (error != 0) 1336 if (error != 0)
1331 goto out_unlocked; 1337 goto out_unlocked;
1332 } 1338 }
1333 1339
1334 /* get info about the node to be moved */ 1340 /* get info about the node to be moved */
1335 vn_lock(fvp, LK_SHARED | LK_RETRY); 1341 vn_lock(fvp, LK_SHARED | LK_RETRY);
1336 error = VOP_GETATTR(fvp, &fvap, FSCRED); 1342 error = VOP_GETATTR(fvp, &fvap, FSCRED);
1337 VOP_UNLOCK(fvp); 1343 VOP_UNLOCK(fvp);
1338 KASSERT(error == 0); 1344 KASSERT(error == 0);
1339 1345
1340 /* check when to delete the old already existing entry */ 1346 /* check when to delete the old already existing entry */
1341 if (tvp) { 1347 if (tvp) {
1342 /* get info about the node to be moved to */ 1348 /* get info about the node to be moved to */
1343 error = VOP_GETATTR(tvp, &tvap, FSCRED); 1349 error = VOP_GETATTR(tvp, &tvap, FSCRED);
1344 KASSERT(error == 0); 1350 KASSERT(error == 0);
1345 1351
1346 /* if both dirs, make sure the destination is empty */ 1352 /* if both dirs, make sure the destination is empty */
1347 if (fvp->v_type == VDIR && tvp->v_type == VDIR) { 1353 if (fvp->v_type == VDIR && tvp->v_type == VDIR) {
1348 if (tvap.va_nlink > 2) { 1354 if (tvap.va_nlink > 2) {
1349 error = ENOTEMPTY; 1355 error = ENOTEMPTY;
1350 goto out; 1356 goto out;
1351 } 1357 }
1352 } 1358 }
1353 /* if moving dir, make sure destination is dir too */ 1359 /* if moving dir, make sure destination is dir too */
1354 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1360 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1355 error = ENOTDIR; 1361 error = ENOTDIR;
1356 goto out; 1362 goto out;
1357 } 1363 }
1358 /* if we're moving a non-directory, make sure dest is no dir */ 1364 /* if we're moving a non-directory, make sure dest is no dir */
1359 if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1365 if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1360 error = EISDIR; 1366 error = EISDIR;
1361 goto out; 1367 goto out;
1362 } 1368 }
1363 } 1369 }
1364 1370
1365 /* dont allow renaming directories acros directory for now */ 1371 /* dont allow renaming directories acros directory for now */
1366 if (fdnode != tdnode) { 1372 if (fdnode != tdnode) {