Commit | Line | Data |
---|---|---|
984263bc MD |
1 | /* $NetBSD: ntfs_vnops.c,v 1.23 1999/10/31 19:45:27 jdolecek Exp $ */ |
2 | ||
3 | /* | |
4 | * Copyright (c) 1992, 1993 | |
5 | * The Regents of the University of California. All rights reserved. | |
6 | * | |
7 | * This code is derived from software contributed to Berkeley by | |
8 | * John Heidemann of the UCLA Ficus project. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
dc71b7ab | 18 | * 3. Neither the name of the University nor the names of its contributors |
984263bc MD |
19 | * may be used to endorse or promote products derived from this software |
20 | * without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | |
33 | * | |
34 | * $FreeBSD: src/sys/ntfs/ntfs_vnops.c,v 1.9.2.4 2002/08/06 19:35:18 semenu Exp $ | |
35 | * | |
36 | */ | |
37 | ||
38 | #include <sys/param.h> | |
39 | #include <sys/systm.h> | |
13dd34d8 | 40 | #include <sys/uio.h> |
984263bc MD |
41 | #include <sys/kernel.h> |
42 | #include <sys/time.h> | |
43 | #include <sys/types.h> | |
44 | #include <sys/stat.h> | |
45 | #include <sys/vnode.h> | |
46 | #include <sys/mount.h> | |
dadab5e9 | 47 | #include <sys/proc.h> |
984263bc MD |
48 | #include <sys/namei.h> |
49 | #include <sys/malloc.h> | |
50 | #include <sys/buf.h> | |
51 | #include <sys/dirent.h> | |
7bf50a33 | 52 | #include <machine/limits.h> |
984263bc MD |
53 | |
54 | #include <vm/vm.h> | |
55 | #include <vm/vm_param.h> | |
984263bc MD |
56 | #include <vm/vm_page.h> |
57 | #include <vm/vm_object.h> | |
58 | #include <vm/vm_pager.h> | |
984263bc | 59 | #include <vm/vnode_pager.h> |
984263bc MD |
60 | #include <vm/vm_extern.h> |
61 | ||
62 | #include <sys/sysctl.h> | |
63 | ||
54341a3b MD |
64 | #include <sys/buf2.h> |
65 | ||
1f2de5d4 MD |
66 | #include "ntfs.h" |
67 | #include "ntfs_inode.h" | |
68 | #include "ntfs_subr.h" | |
984263bc MD |
69 | |
70 | #include <sys/unistd.h> /* for pathconf(2) constants */ | |
71 | ||
a6ee311a RG |
72 | static int ntfs_read (struct vop_read_args *); |
73 | static int ntfs_write (struct vop_write_args *ap); | |
74 | static int ntfs_getattr (struct vop_getattr_args *ap); | |
75 | static int ntfs_inactive (struct vop_inactive_args *ap); | |
76 | static int ntfs_print (struct vop_print_args *ap); | |
77 | static int ntfs_reclaim (struct vop_reclaim_args *ap); | |
78 | static int ntfs_strategy (struct vop_strategy_args *ap); | |
79 | static int ntfs_access (struct vop_access_args *ap); | |
80 | static int ntfs_open (struct vop_open_args *ap); | |
81 | static int ntfs_close (struct vop_close_args *ap); | |
82 | static int ntfs_readdir (struct vop_readdir_args *ap); | |
e62afb5f | 83 | static int ntfs_lookup (struct vop_old_lookup_args *ap); |
a6ee311a | 84 | static int ntfs_bmap (struct vop_bmap_args *ap); |
a6ee311a | 85 | static int ntfs_fsync (struct vop_fsync_args *ap); |
66a1ddf5 | 86 | static int ntfs_pathconf (struct vop_pathconf_args *); |
984263bc MD |
87 | |
88 | int ntfs_prtactive = 1; /* 1 => print out reclaim of active vnodes */ | |
89 | ||
984263bc MD |
90 | /* |
91 | * This is a noop, simply returning what one has been given. | |
cd0a65a2 | 92 | * |
08daea96 | 93 | * ntfs_bmap(struct vnode *a_vp, off_t a_loffset, |
54078292 | 94 | * daddr_t *a_doffsetp, int *a_runp, int *a_runb) |
984263bc MD |
95 | */ |
96 | int | |
cd0a65a2 | 97 | ntfs_bmap(struct vop_bmap_args *ap) |
984263bc | 98 | { |
f3696fdb AHJ |
99 | dprintf(("ntfs_bmap: vn: %p, blk: %u\n", ap->a_vp, |
100 | (u_int32_t)ap->a_loffset)); | |
54078292 MD |
101 | if (ap->a_doffsetp != NULL) |
102 | *ap->a_doffsetp = ap->a_loffset; | |
984263bc MD |
103 | if (ap->a_runp != NULL) |
104 | *ap->a_runp = 0; | |
984263bc MD |
105 | if (ap->a_runb != NULL) |
106 | *ap->a_runb = 0; | |
984263bc MD |
107 | return (0); |
108 | } | |
109 | ||
cd0a65a2 CP |
110 | /* |
111 | * ntfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, | |
112 | * struct ucred *a_cred) | |
113 | */ | |
984263bc | 114 | static int |
cd0a65a2 | 115 | ntfs_read(struct vop_read_args *ap) |
984263bc | 116 | { |
34ee9825 RG |
117 | struct vnode *vp = ap->a_vp; |
118 | struct fnode *fp = VTOF(vp); | |
119 | struct ntnode *ip = FTONT(fp); | |
984263bc MD |
120 | struct uio *uio = ap->a_uio; |
121 | struct ntfsmount *ntmp = ip->i_mp; | |
122 | struct buf *bp; | |
123 | daddr_t cn; | |
124 | int resid, off, toread; | |
125 | int error; | |
126 | ||
f3696fdb AHJ |
127 | dprintf(("ntfs_read: ino: %ju, off: %u resid: %zd, segflg: %d\n", |
128 | (uintmax_t)ip->i_number, (uint32_t)uio->uio_offset, | |
e54488bb | 129 | uio->uio_resid, uio->uio_segflg)); |
984263bc | 130 | |
f3696fdb | 131 | dprintf(("ntfs_read: filesize: %ju", (uintmax_t)fp->f_size)); |
984263bc MD |
132 | |
133 | /* don't allow reading after end of file */ | |
134 | if (uio->uio_offset > fp->f_size) | |
135 | return (0); | |
136 | ||
e54488bb | 137 | resid = (int)szmin(uio->uio_resid, fp->f_size - uio->uio_offset); |
984263bc MD |
138 | |
139 | dprintf((", resid: %d\n", resid)); | |
140 | ||
141 | error = 0; | |
142 | while (resid) { | |
143 | cn = ntfs_btocn(uio->uio_offset); | |
144 | off = ntfs_btocnoff(uio->uio_offset); | |
145 | ||
146 | toread = min(off + resid, ntfs_cntob(1)); | |
147 | ||
54078292 | 148 | error = bread(vp, ntfs_cntodoff(cn), ntfs_cntob(1), &bp); |
984263bc MD |
149 | if (error) { |
150 | brelse(bp); | |
151 | break; | |
152 | } | |
153 | ||
44480e31 | 154 | error = uiomovebp(bp, bp->b_data + off, toread - off, uio); |
984263bc MD |
155 | if(error) { |
156 | brelse(bp); | |
157 | break; | |
158 | } | |
159 | brelse(bp); | |
160 | ||
161 | resid -= toread - off; | |
162 | } | |
163 | ||
164 | return (error); | |
165 | } | |
166 | ||
cd0a65a2 | 167 | /* |
b478fdce | 168 | * ntfs_getattr(struct vnode *a_vp, struct vattr *a_vap) |
cd0a65a2 | 169 | */ |
984263bc | 170 | static int |
cd0a65a2 | 171 | ntfs_getattr(struct vop_getattr_args *ap) |
984263bc | 172 | { |
34ee9825 RG |
173 | struct vnode *vp = ap->a_vp; |
174 | struct fnode *fp = VTOF(vp); | |
175 | struct ntnode *ip = FTONT(fp); | |
176 | struct vattr *vap = ap->a_vap; | |
984263bc | 177 | |
f3696fdb AHJ |
178 | dprintf(("ntfs_getattr: %ju, flags: %d\n", (uintmax_t)ip->i_number, |
179 | ip->i_flag)); | |
984263bc | 180 | |
2ac7d105 | 181 | vap->va_fsid = devid_from_dev(ip->i_dev); |
984263bc MD |
182 | vap->va_fileid = ip->i_number; |
183 | vap->va_mode = ip->i_mp->ntm_mode; | |
184 | vap->va_nlink = ip->i_nlink; | |
185 | vap->va_uid = ip->i_mp->ntm_uid; | |
186 | vap->va_gid = ip->i_mp->ntm_gid; | |
0e9b9130 MD |
187 | vap->va_rmajor = VNOVAL; |
188 | vap->va_rminor = VNOVAL; | |
984263bc MD |
189 | vap->va_size = fp->f_size; |
190 | vap->va_bytes = fp->f_allocated; | |
191 | vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access); | |
192 | vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write); | |
193 | vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create); | |
194 | vap->va_flags = ip->i_flag; | |
195 | vap->va_gen = 0; | |
196 | vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps; | |
197 | vap->va_type = vp->v_type; | |
198 | vap->va_filerev = 0; | |
199 | return (0); | |
200 | } | |
201 | ||
202 | ||
203 | /* | |
204 | * Last reference to an ntnode. If necessary, write or delete it. | |
cd0a65a2 CP |
205 | * |
206 | * ntfs_inactive(struct vnode *a_vp) | |
984263bc MD |
207 | */ |
208 | int | |
cd0a65a2 | 209 | ntfs_inactive(struct vop_inactive_args *ap) |
984263bc | 210 | { |
34ee9825 | 211 | struct vnode *vp = ap->a_vp; |
984263bc | 212 | #ifdef NTFS_DEBUG |
34ee9825 | 213 | struct ntnode *ip = VTONT(vp); |
984263bc MD |
214 | #endif |
215 | ||
f3696fdb | 216 | dprintf(("ntfs_inactive: vnode: %p, ntnode: %ju\n", vp, (uintmax_t)ip->i_number)); |
984263bc | 217 | |
ee173d09 | 218 | if (ntfs_prtactive && VREFCNT(vp) > 1) |
984263bc MD |
219 | vprint("ntfs_inactive: pushing active", vp); |
220 | ||
5fd012e0 MD |
221 | /* |
222 | * XXX since we don't support any filesystem changes | |
984263bc MD |
223 | * right now, nothing more needs to be done |
224 | */ | |
225 | return (0); | |
226 | } | |
227 | ||
228 | /* | |
229 | * Reclaim an fnode/ntnode so that it can be used for other purposes. | |
cd0a65a2 CP |
230 | * |
231 | * ntfs_reclaim(struct vnode *a_vp) | |
984263bc MD |
232 | */ |
233 | int | |
cd0a65a2 | 234 | ntfs_reclaim(struct vop_reclaim_args *ap) |
984263bc | 235 | { |
34ee9825 RG |
236 | struct vnode *vp = ap->a_vp; |
237 | struct fnode *fp = VTOF(vp); | |
238 | struct ntnode *ip = FTONT(fp); | |
984263bc MD |
239 | int error; |
240 | ||
f3696fdb | 241 | dprintf(("ntfs_reclaim: vnode: %p, ntnode: %ju\n", vp, (uintmax_t)ip->i_number)); |
984263bc | 242 | |
ee173d09 | 243 | if (ntfs_prtactive && VREFCNT(vp) > 1) |
984263bc MD |
244 | vprint("ntfs_reclaim: pushing active", vp); |
245 | ||
246 | if ((error = ntfs_ntget(ip)) != 0) | |
247 | return (error); | |
248 | ||
984263bc MD |
249 | ntfs_frele(fp); |
250 | ntfs_ntput(ip); | |
251 | vp->v_data = NULL; | |
252 | ||
253 | return (0); | |
254 | } | |
255 | ||
cd0a65a2 CP |
256 | /* |
257 | * ntfs_print(struct vnode *a_vp) | |
258 | */ | |
984263bc | 259 | static int |
cd0a65a2 | 260 | ntfs_print(struct vop_print_args *ap) |
984263bc MD |
261 | { |
262 | return (0); | |
263 | } | |
264 | ||
265 | /* | |
266 | * Calculate the logical to physical mapping if not done already, | |
267 | * then call the device strategy routine. | |
cd0a65a2 | 268 | * |
81b5c339 | 269 | * ntfs_strategy(struct vnode *a_vp, struct bio *a_bio) |
984263bc MD |
270 | */ |
271 | int | |
cd0a65a2 | 272 | ntfs_strategy(struct vop_strategy_args *ap) |
984263bc | 273 | { |
81b5c339 MD |
274 | struct bio *bio = ap->a_bio; |
275 | struct buf *bp = bio->bio_buf; | |
276 | struct vnode *vp = ap->a_vp; | |
34ee9825 RG |
277 | struct fnode *fp = VTOF(vp); |
278 | struct ntnode *ip = FTONT(fp); | |
984263bc | 279 | struct ntfsmount *ntmp = ip->i_mp; |
10f3fee5 MD |
280 | u_int32_t toread; |
281 | u_int32_t towrite; | |
282 | size_t tmp; | |
984263bc MD |
283 | int error; |
284 | ||
f3696fdb AHJ |
285 | dprintf(("ntfs_strategy: loffset: %u, doffset: %u\n", |
286 | (uint32_t)bp->b_loffset, (uint32_t)bio->bio_offset)); | |
984263bc | 287 | |
f3696fdb AHJ |
288 | dprintf(("strategy: bcount: %u flags: 0x%x\n", |
289 | bp->b_bcount, bp->b_flags)); | |
984263bc | 290 | |
10f3fee5 | 291 | bp->b_error = 0; |
984263bc | 292 | |
10f3fee5 MD |
293 | switch(bp->b_cmd) { |
294 | case BUF_CMD_READ: | |
54078292 | 295 | if (bio->bio_offset >= fp->f_size) { |
984263bc MD |
296 | clrbuf(bp); |
297 | error = 0; | |
298 | } else { | |
299 | toread = min(bp->b_bcount, | |
54078292 | 300 | fp->f_size - bio->bio_offset); |
f3696fdb AHJ |
301 | dprintf(("ntfs_strategy: toread: %u, fsize: %ju\n", |
302 | toread, (uintmax_t)fp->f_size)); | |
984263bc MD |
303 | |
304 | error = ntfs_readattr(ntmp, ip, fp->f_attrtype, | |
54078292 | 305 | fp->f_attrname, bio->bio_offset, |
984263bc MD |
306 | toread, bp->b_data, NULL); |
307 | ||
308 | if (error) { | |
086c1d7e | 309 | kprintf("ntfs_strategy: ntfs_readattr failed\n"); |
984263bc MD |
310 | bp->b_error = error; |
311 | bp->b_flags |= B_ERROR; | |
312 | } | |
313 | ||
314 | bzero(bp->b_data + toread, bp->b_bcount - toread); | |
315 | } | |
10f3fee5 MD |
316 | break; |
317 | case BUF_CMD_WRITE: | |
54078292 | 318 | if (bio->bio_offset + bp->b_bcount >= fp->f_size) { |
086c1d7e | 319 | kprintf("ntfs_strategy: CAN'T EXTEND FILE\n"); |
984263bc MD |
320 | bp->b_error = error = EFBIG; |
321 | bp->b_flags |= B_ERROR; | |
322 | } else { | |
323 | towrite = min(bp->b_bcount, | |
54078292 | 324 | fp->f_size - bio->bio_offset); |
f3696fdb AHJ |
325 | dprintf(("ntfs_strategy: towrite: %d, fsize: %ju\n", |
326 | towrite, (uintmax_t)fp->f_size)); | |
984263bc MD |
327 | |
328 | error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype, | |
54078292 | 329 | fp->f_attrname, bio->bio_offset,towrite, |
984263bc MD |
330 | bp->b_data, &tmp, NULL); |
331 | ||
332 | if (error) { | |
086c1d7e | 333 | kprintf("ntfs_strategy: ntfs_writeattr fail\n"); |
984263bc MD |
334 | bp->b_error = error; |
335 | bp->b_flags |= B_ERROR; | |
336 | } | |
337 | } | |
10f3fee5 MD |
338 | break; |
339 | default: | |
ed20d0e3 | 340 | panic("ntfs: bad b_cmd %d", bp->b_cmd); |
984263bc | 341 | } |
81b5c339 | 342 | biodone(bio); |
984263bc MD |
343 | return (error); |
344 | } | |
345 | ||
cd0a65a2 CP |
346 | /* |
347 | * ntfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, | |
348 | * struct ucred *a_cred) | |
349 | */ | |
984263bc | 350 | static int |
cd0a65a2 | 351 | ntfs_write(struct vop_write_args *ap) |
984263bc | 352 | { |
34ee9825 RG |
353 | struct vnode *vp = ap->a_vp; |
354 | struct fnode *fp = VTOF(vp); | |
355 | struct ntnode *ip = FTONT(fp); | |
984263bc MD |
356 | struct uio *uio = ap->a_uio; |
357 | struct ntfsmount *ntmp = ip->i_mp; | |
e54488bb | 358 | size_t towrite; |
984263bc MD |
359 | size_t written; |
360 | int error; | |
361 | ||
f3696fdb AHJ |
362 | dprintf(("ntfs_write: ino: %ju, off: %u resid: %zd, segflg: %d\n", |
363 | (uintmax_t)ip->i_number, (uint32_t)uio->uio_offset, | |
e54488bb | 364 | uio->uio_resid, uio->uio_segflg)); |
f3696fdb | 365 | dprintf(("ntfs_write: filesize: %ju ", (uintmax_t)fp->f_size)); |
984263bc MD |
366 | |
367 | if (uio->uio_resid + uio->uio_offset > fp->f_size) { | |
086c1d7e | 368 | kprintf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n"); |
984263bc MD |
369 | return (EFBIG); |
370 | } | |
e54488bb MD |
371 | if (uio->uio_offset > fp->f_size) |
372 | return (EFBIG); | |
984263bc | 373 | |
e54488bb | 374 | towrite = szmin(uio->uio_resid, fp->f_size - uio->uio_offset); |
984263bc | 375 | |
f3696fdb | 376 | dprintf((", towrite: %zd\n", towrite)); |
984263bc MD |
377 | |
378 | error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype, | |
379 | fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio); | |
380 | #ifdef NTFS_DEBUG | |
381 | if (error) | |
086c1d7e | 382 | kprintf("ntfs_write: ntfs_writeattr failed: %d\n", error); |
984263bc MD |
383 | #endif |
384 | ||
385 | return (error); | |
386 | } | |
387 | ||
cd0a65a2 | 388 | /* |
b478fdce | 389 | * ntfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred) |
cd0a65a2 | 390 | */ |
984263bc | 391 | int |
cd0a65a2 | 392 | ntfs_access(struct vop_access_args *ap) |
984263bc MD |
393 | { |
394 | struct vnode *vp = ap->a_vp; | |
395 | struct ntnode *ip = VTONT(vp); | |
396 | struct ucred *cred = ap->a_cred; | |
397 | mode_t mask, mode = ap->a_mode; | |
34ee9825 | 398 | gid_t *gp; |
984263bc MD |
399 | int i; |
400 | #ifdef QUOTA | |
401 | int error; | |
402 | #endif | |
403 | ||
f3696fdb | 404 | dprintf(("ntfs_access: %ju\n", (uintmax_t)ip->i_number)); |
984263bc MD |
405 | |
406 | /* | |
407 | * Disallow write attempts on read-only file systems; | |
408 | * unless the file is a socket, fifo, or a block or | |
409 | * character device resident on the file system. | |
410 | */ | |
411 | if (mode & VWRITE) { | |
412 | switch ((int)vp->v_type) { | |
413 | case VDIR: | |
414 | case VLNK: | |
415 | case VREG: | |
416 | if (vp->v_mount->mnt_flag & MNT_RDONLY) | |
417 | return (EROFS); | |
418 | #ifdef QUOTA | |
419 | if (error = getinoquota(ip)) | |
420 | return (error); | |
421 | #endif | |
422 | break; | |
423 | } | |
424 | } | |
425 | ||
426 | /* Otherwise, user id 0 always gets access. */ | |
427 | if (cred->cr_uid == 0) | |
428 | return (0); | |
429 | ||
430 | mask = 0; | |
431 | ||
432 | /* Otherwise, check the owner. */ | |
433 | if (cred->cr_uid == ip->i_mp->ntm_uid) { | |
434 | if (mode & VEXEC) | |
435 | mask |= S_IXUSR; | |
436 | if (mode & VREAD) | |
437 | mask |= S_IRUSR; | |
438 | if (mode & VWRITE) | |
439 | mask |= S_IWUSR; | |
440 | return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES); | |
441 | } | |
442 | ||
443 | /* Otherwise, check the groups. */ | |
444 | for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++) | |
445 | if (ip->i_mp->ntm_gid == *gp) { | |
446 | if (mode & VEXEC) | |
447 | mask |= S_IXGRP; | |
448 | if (mode & VREAD) | |
449 | mask |= S_IRGRP; | |
450 | if (mode & VWRITE) | |
451 | mask |= S_IWGRP; | |
452 | return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES); | |
453 | } | |
454 | ||
455 | /* Otherwise, check everyone else. */ | |
456 | if (mode & VEXEC) | |
457 | mask |= S_IXOTH; | |
458 | if (mode & VREAD) | |
459 | mask |= S_IROTH; | |
460 | if (mode & VWRITE) | |
461 | mask |= S_IWOTH; | |
462 | return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES); | |
463 | } | |
464 | ||
465 | /* | |
466 | * Open called. | |
467 | * | |
468 | * Nothing to do. | |
cd0a65a2 CP |
469 | * |
470 | * ntfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred, | |
b478fdce | 471 | * struct file *a_fp) |
984263bc MD |
472 | */ |
473 | /* ARGSUSED */ | |
474 | static int | |
cd0a65a2 | 475 | ntfs_open(struct vop_open_args *ap) |
984263bc | 476 | { |
8ddc6004 | 477 | return (vop_stdopen(ap)); |
984263bc MD |
478 | } |
479 | ||
480 | /* | |
481 | * Close called. | |
482 | * | |
483 | * Update the times on the inode. | |
cd0a65a2 | 484 | * |
b478fdce | 485 | * ntfs_close(struct vnode *a_vp, int a_fflag) |
984263bc MD |
486 | */ |
487 | /* ARGSUSED */ | |
488 | static int | |
cd0a65a2 | 489 | ntfs_close(struct vop_close_args *ap) |
984263bc | 490 | { |
e02350b6 | 491 | #ifdef NTFS_DEBUG |
34ee9825 RG |
492 | struct vnode *vp = ap->a_vp; |
493 | struct ntnode *ip = VTONT(vp); | |
984263bc | 494 | |
f3696fdb | 495 | kprintf("ntfs_close: %ju\n", (uintmax_t)ip->i_number); |
984263bc MD |
496 | #endif |
497 | ||
8ddc6004 | 498 | return (vop_stdclose(ap)); |
984263bc MD |
499 | } |
500 | ||
cd0a65a2 CP |
501 | /* |
502 | * ntfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred, | |
84009d92 | 503 | * int *a_ncookies, off_t **cookies) |
cd0a65a2 | 504 | */ |
984263bc | 505 | int |
cd0a65a2 | 506 | ntfs_readdir(struct vop_readdir_args *ap) |
984263bc | 507 | { |
34ee9825 RG |
508 | struct vnode *vp = ap->a_vp; |
509 | struct fnode *fp = VTOF(vp); | |
510 | struct ntnode *ip = FTONT(fp); | |
984263bc MD |
511 | struct uio *uio = ap->a_uio; |
512 | struct ntfsmount *ntmp = ip->i_mp; | |
23731001 AP |
513 | int i, j, error = 0; |
514 | wchar c; | |
7bf50a33 | 515 | u_int32_t faked = 0, num, off; |
984263bc | 516 | int ncookies = 0; |
7bf50a33 | 517 | char convname[NTFS_MAXFILENAME + 1]; |
984263bc | 518 | |
f3696fdb AHJ |
519 | dprintf(("ntfs_readdir %ju off: %u resid: %zd\n", |
520 | (uintmax_t)ip->i_number, (uint32_t)uio->uio_offset, | |
e54488bb | 521 | uio->uio_resid)); |
984263bc | 522 | |
7bf50a33 SS |
523 | if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX) |
524 | return (EINVAL); | |
b458d1ab MD |
525 | error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM); |
526 | if (error) | |
885ecb13 | 527 | return (error); |
984263bc | 528 | |
7bf50a33 SS |
529 | /* |
530 | * uio->uio_offset carries the number of the entry | |
531 | * where we should start returning dirents. | |
532 | * | |
533 | * We fake up "." if we're not reading the FS root | |
534 | * and we always fake up "..". | |
535 | * | |
536 | * off contains the entry we are starting at, | |
537 | * num increments while we are reading. | |
538 | */ | |
984263bc | 539 | |
7bf50a33 SS |
540 | off = num = uio->uio_offset; |
541 | faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2; | |
984263bc | 542 | |
7bf50a33 SS |
543 | /* Simulate . in every dir except ROOT */ |
544 | if (ip->i_number != NTFS_ROOTINO && num == 0) { | |
545 | if (vop_write_dirent(&error, uio, ip->i_number, | |
546 | DT_DIR, 1, ".")) | |
885ecb13 | 547 | goto done; |
7bf50a33 | 548 | if (error) |
885ecb13 | 549 | goto done; |
7bf50a33 SS |
550 | |
551 | num++; | |
552 | ncookies++; | |
984263bc MD |
553 | } |
554 | ||
555 | /* Simulate .. in every dir including ROOT */ | |
7bf50a33 SS |
556 | if (num == faked - 1) { |
557 | /* XXX NTFS_ROOTINO seems to be wrong here */ | |
558 | if (vop_write_dirent(&error, uio, NTFS_ROOTINO, | |
559 | DT_DIR, 2, "..")) | |
560 | goto readdone; | |
885ecb13 MD |
561 | if (error) |
562 | goto done; | |
984263bc | 563 | |
7bf50a33 SS |
564 | num++; |
565 | ncookies++; | |
984263bc MD |
566 | } |
567 | ||
7bf50a33 | 568 | for (;;) { |
984263bc MD |
569 | struct attr_indexentry *iep; |
570 | ||
7bf50a33 SS |
571 | /* |
572 | * num is the number of the entry we will return, | |
573 | * but ntfs_ntreaddir takes the entry number of the | |
574 | * ntfs directory listing, so subtract the faked | |
575 | * . and .. entries. | |
576 | */ | |
577 | error = ntfs_ntreaddir(ntmp, fp, num - faked, &iep); | |
984263bc | 578 | |
885ecb13 MD |
579 | if (error) |
580 | goto done; | |
984263bc MD |
581 | |
582 | if( NULL == iep ) | |
583 | break; | |
584 | ||
7bf50a33 | 585 | for (; !(iep->ie_flag & NTFS_IEFLAG_LAST); |
984263bc MD |
586 | iep = NTFS_NEXTREC(iep, struct attr_indexentry *)) |
587 | { | |
588 | if(!ntfs_isnamepermitted(ntmp,iep)) | |
589 | continue; | |
23731001 AP |
590 | for(i=0, j=0; i < iep->ie_fnamelen; i++, j++) { |
591 | c = NTFS_U28(iep->ie_fname[i]); | |
592 | if (c&0xFF00) | |
593 | convname[j++] = (char)(c>>8); | |
594 | convname[j] = (char)c&0xFF; | |
595 | } | |
596 | convname[j] = '\0'; | |
7bf50a33 SS |
597 | if (vop_write_dirent(&error, uio, iep->ie_number, |
598 | (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG, | |
23731001 | 599 | j, convname)) |
7bf50a33 SS |
600 | goto readdone; |
601 | ||
602 | dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, " | |
603 | "flag: %d, %s\n", | |
604 | ncookies, convname, iep->ie_fnametype, | |
605 | iep->ie_flag, | |
606 | (iep->ie_fflag & NTFS_FFLAG_DIR) ? | |
607 | "dir" : "reg")); | |
608 | ||
885ecb13 MD |
609 | if (error) |
610 | goto done; | |
984263bc MD |
611 | |
612 | ncookies++; | |
613 | num++; | |
614 | } | |
615 | } | |
616 | ||
7bf50a33 SS |
617 | readdone: |
618 | uio->uio_offset = num; | |
619 | ||
984263bc MD |
620 | dprintf(("ntfs_readdir: %d entries (%d bytes) read\n", |
621 | ncookies,(u_int)(uio->uio_offset - off))); | |
f3696fdb AHJ |
622 | dprintf(("ntfs_readdir: off: %u resid: %zd\n", |
623 | (uint32_t)uio->uio_offset, uio->uio_resid)); | |
984263bc MD |
624 | |
625 | if (!error && ap->a_ncookies != NULL) { | |
984263bc MD |
626 | off_t *cookies; |
627 | off_t *cookiep; | |
984263bc MD |
628 | |
629 | ddprintf(("ntfs_readdir: %d cookies\n",ncookies)); | |
630 | if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) | |
631 | panic("ntfs_readdir: unexpected uio from NFS server"); | |
884717e1 | 632 | cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK); |
7bf50a33 SS |
633 | cookiep = cookies; |
634 | while (off < num) | |
635 | *cookiep++ = ++off; | |
636 | ||
984263bc MD |
637 | *ap->a_ncookies = ncookies; |
638 | *ap->a_cookies = cookies; | |
639 | } | |
640 | /* | |
641 | if (ap->a_eofflag) | |
885ecb13 | 642 | *ap->a_eofflag = VTONT(vp)->i_size <= uio->uio_offset; |
984263bc | 643 | */ |
885ecb13 MD |
644 | done: |
645 | vn_unlock(vp); | |
984263bc MD |
646 | return (error); |
647 | } | |
648 | ||
cd0a65a2 CP |
649 | /* |
650 | * ntfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp, | |
651 | * struct componentname *a_cnp) | |
652 | */ | |
984263bc | 653 | int |
e62afb5f | 654 | ntfs_lookup(struct vop_old_lookup_args *ap) |
984263bc | 655 | { |
34ee9825 RG |
656 | struct vnode *dvp = ap->a_dvp; |
657 | struct ntnode *dip = VTONT(dvp); | |
984263bc MD |
658 | struct ntfsmount *ntmp = dip->i_mp; |
659 | struct componentname *cnp = ap->a_cnp; | |
984263bc | 660 | int error; |
2b69e610 | 661 | int lockparent = cnp->cn_flags & CNP_LOCKPARENT; |
e02350b6 | 662 | #ifdef NTFS_DEBUG |
2b69e610 | 663 | int wantparent = cnp->cn_flags & (CNP_LOCKPARENT | CNP_WANTPARENT); |
984263bc | 664 | #endif |
f3696fdb | 665 | dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %ju, lp: %d, wp: %d \n", |
984263bc | 666 | (int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen, |
f3696fdb | 667 | (uintmax_t)dip->i_number, lockparent, wantparent)); |
984263bc | 668 | |
9df3a184 MD |
669 | *ap->a_vpp = NULL; |
670 | ||
fad57d0e | 671 | if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { |
f3696fdb AHJ |
672 | dprintf(("ntfs_lookup: faking . directory in %u\n", |
673 | (uint32_t)dip->i_number)); | |
984263bc | 674 | |
597aea93 | 675 | vref(dvp); |
984263bc MD |
676 | *ap->a_vpp = dvp; |
677 | error = 0; | |
2b69e610 | 678 | } else if (cnp->cn_flags & CNP_ISDOTDOT) { |
984263bc MD |
679 | struct ntvattr *vap; |
680 | ||
681 | dprintf(("ntfs_lookup: faking .. directory in %d\n", | |
f3696fdb | 682 | (uint32_t)dip->i_number)); |
984263bc MD |
683 | |
684 | error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap); | |
685 | if(error) | |
686 | return (error); | |
687 | ||
acde96db | 688 | VOP__UNLOCK(dvp, 0); |
2b69e610 | 689 | cnp->cn_flags |= CNP_PDIRUNLOCK; |
984263bc MD |
690 | |
691 | dprintf(("ntfs_lookup: parentdir: %d\n", | |
692 | vap->va_a_name->n_pnumber)); | |
b9b0a6d0 | 693 | error = VFS_VGET(ntmp->ntm_mountp, NULL, |
984263bc MD |
694 | vap->va_a_name->n_pnumber,ap->a_vpp); |
695 | ntfs_ntvattrrele(vap); | |
696 | if (error) { | |
b458d1ab | 697 | if (VOP_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY) == 0) |
2b69e610 | 698 | cnp->cn_flags &= ~CNP_PDIRUNLOCK; |
984263bc MD |
699 | return (error); |
700 | } | |
701 | ||
fad57d0e | 702 | if (lockparent) { |
b458d1ab | 703 | error = VOP_LOCK(dvp, LK_EXCLUSIVE); |
984263bc | 704 | if (error) { |
9df3a184 MD |
705 | vput(*ap->a_vpp); |
706 | *ap->a_vpp = NULL; | |
984263bc MD |
707 | return (error); |
708 | } | |
2b69e610 | 709 | cnp->cn_flags &= ~CNP_PDIRUNLOCK; |
984263bc MD |
710 | } |
711 | } else { | |
712 | error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp); | |
713 | if (error) { | |
714 | dprintf(("ntfs_ntlookupfile: returned %d\n", error)); | |
715 | return (error); | |
716 | } | |
717 | ||
f3696fdb AHJ |
718 | dprintf(("ntfs_lookup: found ino: %u\n", |
719 | (uint32_t)VTONT(*ap->a_vpp)->i_number)); | |
984263bc | 720 | |
abe9d9ec | 721 | if (!lockparent) { |
acde96db | 722 | VOP__UNLOCK(dvp, 0); |
abe9d9ec AHJ |
723 | cnp->cn_flags |= CNP_PDIRUNLOCK; |
724 | } | |
984263bc | 725 | } |
984263bc MD |
726 | return (error); |
727 | } | |
728 | ||
984263bc MD |
729 | /* |
730 | * Flush the blocks of a file to disk. | |
731 | * | |
732 | * This function is worthless for vnodes that represent directories. Maybe we | |
733 | * could just do a sync if they try an fsync on a directory file. | |
cd0a65a2 | 734 | * |
b478fdce | 735 | * ntfs_fsync(struct vnode *a_vp, int a_waitfor) |
984263bc MD |
736 | */ |
737 | static int | |
cd0a65a2 | 738 | ntfs_fsync(struct vop_fsync_args *ap) |
984263bc MD |
739 | { |
740 | return (0); | |
741 | } | |
984263bc MD |
742 | |
743 | /* | |
744 | * Return POSIX pathconf information applicable to NTFS filesystem | |
745 | */ | |
746 | int | |
66a1ddf5 | 747 | ntfs_pathconf(struct vop_pathconf_args *ap) |
984263bc | 748 | { |
984263bc MD |
749 | switch (ap->a_name) { |
750 | case _PC_LINK_MAX: | |
751 | *ap->a_retval = 1; | |
752 | return (0); | |
753 | case _PC_NAME_MAX: | |
754 | *ap->a_retval = NTFS_MAXFILENAME; | |
755 | return (0); | |
756 | case _PC_PATH_MAX: | |
757 | *ap->a_retval = PATH_MAX; | |
758 | return (0); | |
759 | case _PC_CHOWN_RESTRICTED: | |
760 | *ap->a_retval = 1; | |
761 | return (0); | |
762 | case _PC_NO_TRUNC: | |
763 | *ap->a_retval = 0; | |
764 | return (0); | |
984263bc MD |
765 | default: |
766 | return (EINVAL); | |
767 | } | |
768 | /* NOTREACHED */ | |
769 | } | |
770 | ||
771 | /* | |
772 | * Global vfs data structures | |
773 | */ | |
66a1ddf5 MD |
774 | struct vop_ops ntfs_vnode_vops = { |
775 | .vop_default = vop_defaultop, | |
776 | .vop_getattr = ntfs_getattr, | |
777 | .vop_inactive = ntfs_inactive, | |
778 | .vop_reclaim = ntfs_reclaim, | |
779 | .vop_print = ntfs_print, | |
780 | .vop_pathconf = ntfs_pathconf, | |
66a1ddf5 MD |
781 | .vop_old_lookup = ntfs_lookup, |
782 | .vop_access = ntfs_access, | |
783 | .vop_close = ntfs_close, | |
784 | .vop_open = ntfs_open, | |
785 | .vop_readdir = ntfs_readdir, | |
786 | .vop_fsync = ntfs_fsync, | |
787 | .vop_bmap = ntfs_bmap, | |
1787385d MD |
788 | .vop_getpages = vop_stdgetpages, |
789 | .vop_putpages = vop_stdputpages, | |
66a1ddf5 MD |
790 | .vop_strategy = ntfs_strategy, |
791 | .vop_read = ntfs_read, | |
792 | .vop_write = ntfs_write | |
984263bc MD |
793 | }; |
794 |