ntfs_vfsops.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: ntfs_vfsops.c,v 1.1.2.2 2002/12/29 19:56:18 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*-
4 1.1.2.2 thorpej * Copyright (c) 1998, 1999 Semen Ustimenko
5 1.1.2.2 thorpej * All rights reserved.
6 1.1.2.2 thorpej *
7 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
9 1.1.2.2 thorpej * are met:
10 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.1.2.2 thorpej *
16 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1.2.2 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.2.2 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.2.2 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1.2.2 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.2.2 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.2.2 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.2.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.2.2 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.2.2 thorpej * SUCH DAMAGE.
27 1.1.2.2 thorpej *
28 1.1.2.2 thorpej * Id: ntfs_vfsops.c,v 1.7 1999/05/31 11:28:30 phk Exp
29 1.1.2.2 thorpej */
30 1.1.2.2 thorpej
31 1.1.2.2 thorpej #include <sys/cdefs.h>
32 1.1.2.2 thorpej __KERNEL_RCSID(0, "$NetBSD: ntfs_vfsops.c,v 1.1.2.2 2002/12/29 19:56:18 thorpej Exp $");
33 1.1.2.2 thorpej
34 1.1.2.2 thorpej #include <sys/param.h>
35 1.1.2.2 thorpej #include <sys/systm.h>
36 1.1.2.2 thorpej #include <sys/namei.h>
37 1.1.2.2 thorpej #include <sys/proc.h>
38 1.1.2.2 thorpej #include <sys/kernel.h>
39 1.1.2.2 thorpej #include <sys/vnode.h>
40 1.1.2.2 thorpej #include <sys/mount.h>
41 1.1.2.2 thorpej #include <sys/buf.h>
42 1.1.2.2 thorpej #include <sys/fcntl.h>
43 1.1.2.2 thorpej #include <sys/malloc.h>
44 1.1.2.2 thorpej #include <sys/systm.h>
45 1.1.2.2 thorpej #include <sys/device.h>
46 1.1.2.2 thorpej #include <sys/conf.h>
47 1.1.2.2 thorpej
48 1.1.2.2 thorpej #if defined(__NetBSD__)
49 1.1.2.2 thorpej #include <uvm/uvm_extern.h>
50 1.1.2.2 thorpej #else
51 1.1.2.2 thorpej #include <vm/vm.h>
52 1.1.2.2 thorpej #endif
53 1.1.2.2 thorpej
54 1.1.2.2 thorpej #include <miscfs/specfs/specdev.h>
55 1.1.2.2 thorpej
56 1.1.2.2 thorpej /*#define NTFS_DEBUG 1*/
57 1.1.2.2 thorpej #include <fs/ntfs/ntfs.h>
58 1.1.2.2 thorpej #include <fs/ntfs/ntfs_inode.h>
59 1.1.2.2 thorpej #include <fs/ntfs/ntfs_subr.h>
60 1.1.2.2 thorpej #include <fs/ntfs/ntfs_vfsops.h>
61 1.1.2.2 thorpej #include <fs/ntfs/ntfs_ihash.h>
62 1.1.2.2 thorpej #include <fs/ntfs/ntfsmount.h>
63 1.1.2.2 thorpej
64 1.1.2.2 thorpej #if defined(__FreeBSD__)
65 1.1.2.2 thorpej MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
66 1.1.2.2 thorpej MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode", "NTFS ntnode information");
67 1.1.2.2 thorpej MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode", "NTFS fnode information");
68 1.1.2.2 thorpej MALLOC_DEFINE(M_NTFSDIR,"NTFS dir", "NTFS dir buffer");
69 1.1.2.2 thorpej #endif
70 1.1.2.2 thorpej
71 1.1.2.2 thorpej #if defined(__FreeBSD__)
72 1.1.2.2 thorpej static int ntfs_mount __P((struct mount *, char *, caddr_t,
73 1.1.2.2 thorpej struct nameidata *, struct proc *));
74 1.1.2.2 thorpej #else
75 1.1.2.2 thorpej static int ntfs_mount __P((struct mount *, const char *, void *,
76 1.1.2.2 thorpej struct nameidata *, struct proc *));
77 1.1.2.2 thorpej #endif
78 1.1.2.2 thorpej static int ntfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
79 1.1.2.2 thorpej struct proc *));
80 1.1.2.2 thorpej static int ntfs_root __P((struct mount *, struct vnode **));
81 1.1.2.2 thorpej static int ntfs_start __P((struct mount *, int, struct proc *));
82 1.1.2.2 thorpej static int ntfs_statfs __P((struct mount *, struct statfs *,
83 1.1.2.2 thorpej struct proc *));
84 1.1.2.2 thorpej static int ntfs_sync __P((struct mount *, int, struct ucred *,
85 1.1.2.2 thorpej struct proc *));
86 1.1.2.2 thorpej static int ntfs_unmount __P((struct mount *, int, struct proc *));
87 1.1.2.2 thorpej static int ntfs_vget __P((struct mount *mp, ino_t ino,
88 1.1.2.2 thorpej struct vnode **vpp));
89 1.1.2.2 thorpej static int ntfs_mountfs __P((struct vnode *, struct mount *,
90 1.1.2.2 thorpej struct ntfs_args *, struct proc *));
91 1.1.2.2 thorpej static int ntfs_vptofh __P((struct vnode *, struct fid *));
92 1.1.2.2 thorpej
93 1.1.2.2 thorpej #if defined(__FreeBSD__)
94 1.1.2.2 thorpej static int ntfs_init __P((struct vfsconf *));
95 1.1.2.2 thorpej static int ntfs_fhtovp __P((struct mount *, struct fid *,
96 1.1.2.2 thorpej struct sockaddr *, struct vnode **,
97 1.1.2.2 thorpej int *, struct ucred **));
98 1.1.2.2 thorpej #elif defined(__NetBSD__)
99 1.1.2.2 thorpej static void ntfs_init __P((void));
100 1.1.2.2 thorpej static void ntfs_reinit __P((void));
101 1.1.2.2 thorpej static void ntfs_done __P((void));
102 1.1.2.2 thorpej static int ntfs_fhtovp __P((struct mount *, struct fid *,
103 1.1.2.2 thorpej struct vnode **));
104 1.1.2.2 thorpej static int ntfs_checkexp __P((struct mount *, struct mbuf *,
105 1.1.2.2 thorpej int *, struct ucred **));
106 1.1.2.2 thorpej static int ntfs_mountroot __P((void));
107 1.1.2.2 thorpej static int ntfs_sysctl __P((int *, u_int, void *, size_t *, void *,
108 1.1.2.2 thorpej size_t, struct proc *));
109 1.1.2.2 thorpej #else
110 1.1.2.2 thorpej static int ntfs_init __P((void));
111 1.1.2.2 thorpej static int ntfs_fhtovp __P((struct mount *, struct fid *,
112 1.1.2.2 thorpej struct mbuf *, struct vnode **,
113 1.1.2.2 thorpej int *, struct ucred **));
114 1.1.2.2 thorpej #endif
115 1.1.2.2 thorpej
116 1.1.2.2 thorpej struct genfs_ops ntfs_genfsops = {
117 1.1.2.2 thorpej NULL,
118 1.1.2.2 thorpej NULL,
119 1.1.2.2 thorpej genfs_compat_gop_write,
120 1.1.2.2 thorpej };
121 1.1.2.2 thorpej
122 1.1.2.2 thorpej #ifdef __NetBSD__
123 1.1.2.2 thorpej /*
124 1.1.2.2 thorpej * Verify a remote client has export rights and return these rights via.
125 1.1.2.2 thorpej * exflagsp and credanonp.
126 1.1.2.2 thorpej */
127 1.1.2.2 thorpej static int
128 1.1.2.2 thorpej ntfs_checkexp(mp, nam, exflagsp, credanonp)
129 1.1.2.2 thorpej struct mount *mp;
130 1.1.2.2 thorpej struct mbuf *nam;
131 1.1.2.2 thorpej int *exflagsp;
132 1.1.2.2 thorpej struct ucred **credanonp;
133 1.1.2.2 thorpej {
134 1.1.2.2 thorpej struct netcred *np;
135 1.1.2.2 thorpej struct ntfsmount *ntm = VFSTONTFS(mp);
136 1.1.2.2 thorpej
137 1.1.2.2 thorpej /*
138 1.1.2.2 thorpej * Get the export permission structure for this <mp, client> tuple.
139 1.1.2.2 thorpej */
140 1.1.2.2 thorpej np = vfs_export_lookup(mp, &ntm->ntm_export, nam);
141 1.1.2.2 thorpej if (np == NULL)
142 1.1.2.2 thorpej return (EACCES);
143 1.1.2.2 thorpej
144 1.1.2.2 thorpej *exflagsp = np->netc_exflags;
145 1.1.2.2 thorpej *credanonp = &np->netc_anon;
146 1.1.2.2 thorpej return (0);
147 1.1.2.2 thorpej }
148 1.1.2.2 thorpej
149 1.1.2.2 thorpej /*ARGSUSED*/
150 1.1.2.2 thorpej static int
151 1.1.2.2 thorpej ntfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
152 1.1.2.2 thorpej int *name;
153 1.1.2.2 thorpej u_int namelen;
154 1.1.2.2 thorpej void *oldp;
155 1.1.2.2 thorpej size_t *oldlenp;
156 1.1.2.2 thorpej void *newp;
157 1.1.2.2 thorpej size_t newlen;
158 1.1.2.2 thorpej struct proc *p;
159 1.1.2.2 thorpej {
160 1.1.2.2 thorpej return (EINVAL);
161 1.1.2.2 thorpej }
162 1.1.2.2 thorpej
163 1.1.2.2 thorpej static int
164 1.1.2.2 thorpej ntfs_mountroot()
165 1.1.2.2 thorpej {
166 1.1.2.2 thorpej struct mount *mp;
167 1.1.2.2 thorpej struct proc *p = curproc; /* XXX */
168 1.1.2.2 thorpej int error;
169 1.1.2.2 thorpej struct ntfs_args args;
170 1.1.2.2 thorpej
171 1.1.2.2 thorpej if (root_device->dv_class != DV_DISK)
172 1.1.2.2 thorpej return (ENODEV);
173 1.1.2.2 thorpej
174 1.1.2.2 thorpej /*
175 1.1.2.2 thorpej * Get vnodes for rootdev.
176 1.1.2.2 thorpej */
177 1.1.2.2 thorpej if (bdevvp(rootdev, &rootvp))
178 1.1.2.2 thorpej panic("ntfs_mountroot: can't setup rootvp");
179 1.1.2.2 thorpej
180 1.1.2.2 thorpej if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) {
181 1.1.2.2 thorpej vrele(rootvp);
182 1.1.2.2 thorpej return (error);
183 1.1.2.2 thorpej }
184 1.1.2.2 thorpej
185 1.1.2.2 thorpej args.flag = 0;
186 1.1.2.2 thorpej args.uid = 0;
187 1.1.2.2 thorpej args.gid = 0;
188 1.1.2.2 thorpej args.mode = 0777;
189 1.1.2.2 thorpej
190 1.1.2.2 thorpej if ((error = ntfs_mountfs(rootvp, mp, &args, p)) != 0) {
191 1.1.2.2 thorpej mp->mnt_op->vfs_refcount--;
192 1.1.2.2 thorpej vfs_unbusy(mp);
193 1.1.2.2 thorpej free(mp, M_MOUNT);
194 1.1.2.2 thorpej vrele(rootvp);
195 1.1.2.2 thorpej return (error);
196 1.1.2.2 thorpej }
197 1.1.2.2 thorpej
198 1.1.2.2 thorpej simple_lock(&mountlist_slock);
199 1.1.2.2 thorpej CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
200 1.1.2.2 thorpej simple_unlock(&mountlist_slock);
201 1.1.2.2 thorpej (void)ntfs_statfs(mp, &mp->mnt_stat, p);
202 1.1.2.2 thorpej vfs_unbusy(mp);
203 1.1.2.2 thorpej return (0);
204 1.1.2.2 thorpej }
205 1.1.2.2 thorpej
206 1.1.2.2 thorpej static void
207 1.1.2.2 thorpej ntfs_init()
208 1.1.2.2 thorpej {
209 1.1.2.2 thorpej ntfs_nthashinit();
210 1.1.2.2 thorpej ntfs_toupper_init();
211 1.1.2.2 thorpej }
212 1.1.2.2 thorpej
213 1.1.2.2 thorpej static void
214 1.1.2.2 thorpej ntfs_reinit()
215 1.1.2.2 thorpej {
216 1.1.2.2 thorpej ntfs_nthashreinit();
217 1.1.2.2 thorpej }
218 1.1.2.2 thorpej
219 1.1.2.2 thorpej static void
220 1.1.2.2 thorpej ntfs_done()
221 1.1.2.2 thorpej {
222 1.1.2.2 thorpej ntfs_nthashdone();
223 1.1.2.2 thorpej }
224 1.1.2.2 thorpej
225 1.1.2.2 thorpej #elif defined(__FreeBSD__)
226 1.1.2.2 thorpej
227 1.1.2.2 thorpej static int
228 1.1.2.2 thorpej ntfs_init (
229 1.1.2.2 thorpej struct vfsconf *vcp )
230 1.1.2.2 thorpej {
231 1.1.2.2 thorpej ntfs_nthashinit();
232 1.1.2.2 thorpej ntfs_toupper_init();
233 1.1.2.2 thorpej return 0;
234 1.1.2.2 thorpej }
235 1.1.2.2 thorpej
236 1.1.2.2 thorpej #endif /* NetBSD */
237 1.1.2.2 thorpej
238 1.1.2.2 thorpej static int
239 1.1.2.2 thorpej ntfs_mount (
240 1.1.2.2 thorpej struct mount *mp,
241 1.1.2.2 thorpej #if defined(__FreeBSD__)
242 1.1.2.2 thorpej char *path,
243 1.1.2.2 thorpej caddr_t data,
244 1.1.2.2 thorpej #else
245 1.1.2.2 thorpej const char *path,
246 1.1.2.2 thorpej void *data,
247 1.1.2.2 thorpej #endif
248 1.1.2.2 thorpej struct nameidata *ndp,
249 1.1.2.2 thorpej struct proc *p )
250 1.1.2.2 thorpej {
251 1.1.2.2 thorpej size_t size;
252 1.1.2.2 thorpej int err = 0;
253 1.1.2.2 thorpej struct vnode *devvp;
254 1.1.2.2 thorpej struct ntfs_args args;
255 1.1.2.2 thorpej
256 1.1.2.2 thorpej #ifdef __FreeBSD__
257 1.1.2.2 thorpej /*
258 1.1.2.2 thorpej * Use NULL path to flag a root mount
259 1.1.2.2 thorpej */
260 1.1.2.2 thorpej if( path == NULL) {
261 1.1.2.2 thorpej /*
262 1.1.2.2 thorpej ***
263 1.1.2.2 thorpej * Mounting root file system
264 1.1.2.2 thorpej ***
265 1.1.2.2 thorpej */
266 1.1.2.2 thorpej
267 1.1.2.2 thorpej /* Get vnode for root device*/
268 1.1.2.2 thorpej if( bdevvp( rootdev, &rootvp))
269 1.1.2.2 thorpej panic("ffs_mountroot: can't setup bdevvp for root");
270 1.1.2.2 thorpej
271 1.1.2.2 thorpej /*
272 1.1.2.2 thorpej * FS specific handling
273 1.1.2.2 thorpej */
274 1.1.2.2 thorpej mp->mnt_flag |= MNT_RDONLY; /* XXX globally applicable?*/
275 1.1.2.2 thorpej
276 1.1.2.2 thorpej /*
277 1.1.2.2 thorpej * Attempt mount
278 1.1.2.2 thorpej */
279 1.1.2.2 thorpej if( ( err = ntfs_mountfs(rootvp, mp, &args, p)) != 0) {
280 1.1.2.2 thorpej /* fs specific cleanup (if any)*/
281 1.1.2.2 thorpej goto error_1;
282 1.1.2.2 thorpej }
283 1.1.2.2 thorpej
284 1.1.2.2 thorpej goto dostatfs; /* success*/
285 1.1.2.2 thorpej
286 1.1.2.2 thorpej }
287 1.1.2.2 thorpej #endif /* FreeBSD */
288 1.1.2.2 thorpej
289 1.1.2.2 thorpej if (mp->mnt_flag & MNT_GETARGS) {
290 1.1.2.2 thorpej struct ntfsmount *ntmp = VFSTONTFS(mp);
291 1.1.2.2 thorpej if (ntmp == NULL)
292 1.1.2.2 thorpej return EIO;
293 1.1.2.2 thorpej args.fspec = NULL;
294 1.1.2.2 thorpej args.uid = ntmp->ntm_uid;
295 1.1.2.2 thorpej args.gid = ntmp->ntm_gid;
296 1.1.2.2 thorpej args.mode = ntmp->ntm_mode;
297 1.1.2.2 thorpej args.flag = ntmp->ntm_flag;
298 1.1.2.2 thorpej vfs_showexport(mp, &args.export, &ntmp->ntm_export);
299 1.1.2.2 thorpej return copyout(&args, data, sizeof(args));
300 1.1.2.2 thorpej }
301 1.1.2.2 thorpej /*
302 1.1.2.2 thorpej ***
303 1.1.2.2 thorpej * Mounting non-root file system or updating a file system
304 1.1.2.2 thorpej ***
305 1.1.2.2 thorpej */
306 1.1.2.2 thorpej
307 1.1.2.2 thorpej /* copy in user arguments*/
308 1.1.2.2 thorpej err = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args));
309 1.1.2.2 thorpej if (err)
310 1.1.2.2 thorpej goto error_1; /* can't get arguments*/
311 1.1.2.2 thorpej
312 1.1.2.2 thorpej /*
313 1.1.2.2 thorpej * If updating, check whether changing from read-only to
314 1.1.2.2 thorpej * read/write; if there is no device name, that's all we do.
315 1.1.2.2 thorpej */
316 1.1.2.2 thorpej if (mp->mnt_flag & MNT_UPDATE) {
317 1.1.2.2 thorpej /* if not updating name...*/
318 1.1.2.2 thorpej if (args.fspec == 0) {
319 1.1.2.2 thorpej /*
320 1.1.2.2 thorpej * Process export requests. Jumping to "success"
321 1.1.2.2 thorpej * will return the vfs_export() error code.
322 1.1.2.2 thorpej */
323 1.1.2.2 thorpej struct ntfsmount *ntm = VFSTONTFS(mp);
324 1.1.2.2 thorpej err = vfs_export(mp, &ntm->ntm_export, &args.export);
325 1.1.2.2 thorpej goto success;
326 1.1.2.2 thorpej }
327 1.1.2.2 thorpej
328 1.1.2.2 thorpej printf("ntfs_mount(): MNT_UPDATE not supported\n");
329 1.1.2.2 thorpej err = EINVAL;
330 1.1.2.2 thorpej goto error_1;
331 1.1.2.2 thorpej }
332 1.1.2.2 thorpej
333 1.1.2.2 thorpej /*
334 1.1.2.2 thorpej * Not an update, or updating the name: look up the name
335 1.1.2.2 thorpej * and verify that it refers to a sensible block device.
336 1.1.2.2 thorpej */
337 1.1.2.2 thorpej NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
338 1.1.2.2 thorpej err = namei(ndp);
339 1.1.2.2 thorpej if (err) {
340 1.1.2.2 thorpej /* can't get devvp!*/
341 1.1.2.2 thorpej goto error_1;
342 1.1.2.2 thorpej }
343 1.1.2.2 thorpej
344 1.1.2.2 thorpej devvp = ndp->ni_vp;
345 1.1.2.2 thorpej
346 1.1.2.2 thorpej if (devvp->v_type != VBLK) {
347 1.1.2.2 thorpej err = ENOTBLK;
348 1.1.2.2 thorpej goto error_2;
349 1.1.2.2 thorpej }
350 1.1.2.2 thorpej #ifdef __FreeBSD__
351 1.1.2.2 thorpej if (bdevsw(devvp->v_rdev) == NULL) {
352 1.1.2.2 thorpej #else
353 1.1.2.2 thorpej if (bdevsw_lookup(devvp->v_rdev) == NULL) {
354 1.1.2.2 thorpej #endif
355 1.1.2.2 thorpej err = ENXIO;
356 1.1.2.2 thorpej goto error_2;
357 1.1.2.2 thorpej }
358 1.1.2.2 thorpej if (mp->mnt_flag & MNT_UPDATE) {
359 1.1.2.2 thorpej #if 0
360 1.1.2.2 thorpej /*
361 1.1.2.2 thorpej ********************
362 1.1.2.2 thorpej * UPDATE
363 1.1.2.2 thorpej ********************
364 1.1.2.2 thorpej */
365 1.1.2.2 thorpej
366 1.1.2.2 thorpej if (devvp != ntmp->um_devvp)
367 1.1.2.2 thorpej err = EINVAL; /* needs translation */
368 1.1.2.2 thorpej else
369 1.1.2.2 thorpej vrele(devvp);
370 1.1.2.2 thorpej /*
371 1.1.2.2 thorpej * Update device name only on success
372 1.1.2.2 thorpej */
373 1.1.2.2 thorpej if( !err) {
374 1.1.2.2 thorpej /* Save "mounted from" info for mount point (NULL pad)*/
375 1.1.2.2 thorpej copyinstr( args.fspec,
376 1.1.2.2 thorpej mp->mnt_stat.f_mntfromname,
377 1.1.2.2 thorpej MNAMELEN - 1,
378 1.1.2.2 thorpej &size);
379 1.1.2.2 thorpej bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
380 1.1.2.2 thorpej }
381 1.1.2.2 thorpej #endif
382 1.1.2.2 thorpej } else {
383 1.1.2.2 thorpej /*
384 1.1.2.2 thorpej ********************
385 1.1.2.2 thorpej * NEW MOUNT
386 1.1.2.2 thorpej ********************
387 1.1.2.2 thorpej */
388 1.1.2.2 thorpej
389 1.1.2.2 thorpej /*
390 1.1.2.2 thorpej * Since this is a new mount, we want the names for
391 1.1.2.2 thorpej * the device and the mount point copied in. If an
392 1.1.2.2 thorpej * error occurs, the mountpoint is discarded by the
393 1.1.2.2 thorpej * upper level code.
394 1.1.2.2 thorpej */
395 1.1.2.2 thorpej /* Save "last mounted on" info for mount point (NULL pad)*/
396 1.1.2.2 thorpej copyinstr( path, /* mount point*/
397 1.1.2.2 thorpej mp->mnt_stat.f_mntonname, /* save area*/
398 1.1.2.2 thorpej MNAMELEN - 1, /* max size*/
399 1.1.2.2 thorpej &size); /* real size*/
400 1.1.2.2 thorpej bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
401 1.1.2.2 thorpej
402 1.1.2.2 thorpej /* Save "mounted from" info for mount point (NULL pad)*/
403 1.1.2.2 thorpej copyinstr( args.fspec, /* device name*/
404 1.1.2.2 thorpej mp->mnt_stat.f_mntfromname, /* save area*/
405 1.1.2.2 thorpej MNAMELEN - 1, /* max size*/
406 1.1.2.2 thorpej &size); /* real size*/
407 1.1.2.2 thorpej bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
408 1.1.2.2 thorpej
409 1.1.2.2 thorpej err = ntfs_mountfs(devvp, mp, &args, p);
410 1.1.2.2 thorpej }
411 1.1.2.2 thorpej if (err) {
412 1.1.2.2 thorpej goto error_2;
413 1.1.2.2 thorpej }
414 1.1.2.2 thorpej
415 1.1.2.2 thorpej #ifdef __FreeBSD__
416 1.1.2.2 thorpej dostatfs:
417 1.1.2.2 thorpej #endif
418 1.1.2.2 thorpej /*
419 1.1.2.2 thorpej * Initialize FS stat information in mount struct; uses both
420 1.1.2.2 thorpej * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
421 1.1.2.2 thorpej *
422 1.1.2.2 thorpej * This code is common to root and non-root mounts
423 1.1.2.2 thorpej */
424 1.1.2.2 thorpej (void)VFS_STATFS(mp, &mp->mnt_stat, p);
425 1.1.2.2 thorpej
426 1.1.2.2 thorpej goto success;
427 1.1.2.2 thorpej
428 1.1.2.2 thorpej
429 1.1.2.2 thorpej error_2: /* error with devvp held*/
430 1.1.2.2 thorpej
431 1.1.2.2 thorpej /* release devvp before failing*/
432 1.1.2.2 thorpej vrele(devvp);
433 1.1.2.2 thorpej
434 1.1.2.2 thorpej error_1: /* no state to back out*/
435 1.1.2.2 thorpej
436 1.1.2.2 thorpej success:
437 1.1.2.2 thorpej return(err);
438 1.1.2.2 thorpej }
439 1.1.2.2 thorpej
440 1.1.2.2 thorpej /*
441 1.1.2.2 thorpej * Common code for mount and mountroot
442 1.1.2.2 thorpej */
443 1.1.2.2 thorpej int
444 1.1.2.2 thorpej ntfs_mountfs(devvp, mp, argsp, p)
445 1.1.2.2 thorpej struct vnode *devvp;
446 1.1.2.2 thorpej struct mount *mp;
447 1.1.2.2 thorpej struct ntfs_args *argsp;
448 1.1.2.2 thorpej struct proc *p;
449 1.1.2.2 thorpej {
450 1.1.2.2 thorpej struct buf *bp;
451 1.1.2.2 thorpej struct ntfsmount *ntmp;
452 1.1.2.2 thorpej dev_t dev = devvp->v_rdev;
453 1.1.2.2 thorpej int error, ronly, ncount, i;
454 1.1.2.2 thorpej struct vnode *vp;
455 1.1.2.2 thorpej
456 1.1.2.2 thorpej /*
457 1.1.2.2 thorpej * Disallow multiple mounts of the same device.
458 1.1.2.2 thorpej * Disallow mounting of a device that is currently in use
459 1.1.2.2 thorpej * (except for root, which might share swap device for miniroot).
460 1.1.2.2 thorpej * Flush out any old buffers remaining from a previous use.
461 1.1.2.2 thorpej */
462 1.1.2.2 thorpej error = vfs_mountedon(devvp);
463 1.1.2.2 thorpej if (error)
464 1.1.2.2 thorpej return (error);
465 1.1.2.2 thorpej ncount = vcount(devvp);
466 1.1.2.2 thorpej #if defined(__FreeBSD__)
467 1.1.2.2 thorpej if (devvp->v_object)
468 1.1.2.2 thorpej ncount -= 1;
469 1.1.2.2 thorpej #endif
470 1.1.2.2 thorpej if (ncount > 1 && devvp != rootvp)
471 1.1.2.2 thorpej return (EBUSY);
472 1.1.2.2 thorpej #if defined(__FreeBSD__)
473 1.1.2.2 thorpej VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, p);
474 1.1.2.2 thorpej error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
475 1.1.2.2 thorpej VOP__UNLOCK(devvp, 0, p);
476 1.1.2.2 thorpej #else
477 1.1.2.2 thorpej error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
478 1.1.2.2 thorpej #endif
479 1.1.2.2 thorpej if (error)
480 1.1.2.2 thorpej return (error);
481 1.1.2.2 thorpej
482 1.1.2.2 thorpej ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
483 1.1.2.2 thorpej error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
484 1.1.2.2 thorpej if (error)
485 1.1.2.2 thorpej return (error);
486 1.1.2.2 thorpej
487 1.1.2.2 thorpej bp = NULL;
488 1.1.2.2 thorpej
489 1.1.2.2 thorpej error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
490 1.1.2.2 thorpej if (error)
491 1.1.2.2 thorpej goto out;
492 1.1.2.2 thorpej ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK );
493 1.1.2.2 thorpej bzero( ntmp, sizeof *ntmp );
494 1.1.2.2 thorpej bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
495 1.1.2.2 thorpej brelse( bp );
496 1.1.2.2 thorpej bp = NULL;
497 1.1.2.2 thorpej
498 1.1.2.2 thorpej if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
499 1.1.2.2 thorpej error = EINVAL;
500 1.1.2.2 thorpej dprintf(("ntfs_mountfs: invalid boot block\n"));
501 1.1.2.2 thorpej goto out;
502 1.1.2.2 thorpej }
503 1.1.2.2 thorpej
504 1.1.2.2 thorpej {
505 1.1.2.2 thorpej int8_t cpr = ntmp->ntm_mftrecsz;
506 1.1.2.2 thorpej if( cpr > 0 )
507 1.1.2.2 thorpej ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
508 1.1.2.2 thorpej else
509 1.1.2.2 thorpej ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
510 1.1.2.2 thorpej }
511 1.1.2.2 thorpej dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
512 1.1.2.2 thorpej ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
513 1.1.2.2 thorpej ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
514 1.1.2.2 thorpej dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
515 1.1.2.2 thorpej (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
516 1.1.2.2 thorpej
517 1.1.2.2 thorpej ntmp->ntm_mountp = mp;
518 1.1.2.2 thorpej ntmp->ntm_dev = dev;
519 1.1.2.2 thorpej ntmp->ntm_devvp = devvp;
520 1.1.2.2 thorpej ntmp->ntm_uid = argsp->uid;
521 1.1.2.2 thorpej ntmp->ntm_gid = argsp->gid;
522 1.1.2.2 thorpej ntmp->ntm_mode = argsp->mode;
523 1.1.2.2 thorpej ntmp->ntm_flag = argsp->flag;
524 1.1.2.2 thorpej mp->mnt_data = ntmp;
525 1.1.2.2 thorpej
526 1.1.2.2 thorpej /* set file name encode/decode hooks XXX utf-8 only for now */
527 1.1.2.2 thorpej ntmp->ntm_wget = ntfs_utf8_wget;
528 1.1.2.2 thorpej ntmp->ntm_wput = ntfs_utf8_wput;
529 1.1.2.2 thorpej ntmp->ntm_wcmp = ntfs_utf8_wcmp;
530 1.1.2.2 thorpej
531 1.1.2.2 thorpej dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
532 1.1.2.2 thorpej (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
533 1.1.2.2 thorpej (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
534 1.1.2.2 thorpej ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
535 1.1.2.2 thorpej
536 1.1.2.2 thorpej /*
537 1.1.2.2 thorpej * We read in some system nodes to do not allow
538 1.1.2.2 thorpej * reclaim them and to have everytime access to them.
539 1.1.2.2 thorpej */
540 1.1.2.2 thorpej {
541 1.1.2.2 thorpej int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
542 1.1.2.2 thorpej for (i=0; i<3; i++) {
543 1.1.2.2 thorpej error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
544 1.1.2.2 thorpej if(error)
545 1.1.2.2 thorpej goto out1;
546 1.1.2.2 thorpej ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM;
547 1.1.2.2 thorpej VREF(ntmp->ntm_sysvn[pi[i]]);
548 1.1.2.2 thorpej vput(ntmp->ntm_sysvn[pi[i]]);
549 1.1.2.2 thorpej }
550 1.1.2.2 thorpej }
551 1.1.2.2 thorpej
552 1.1.2.2 thorpej /* read the Unicode lowercase --> uppercase translation table,
553 1.1.2.2 thorpej * if necessary */
554 1.1.2.2 thorpej if ((error = ntfs_toupper_use(mp, ntmp)))
555 1.1.2.2 thorpej goto out1;
556 1.1.2.2 thorpej
557 1.1.2.2 thorpej /*
558 1.1.2.2 thorpej * Scan $BitMap and count free clusters
559 1.1.2.2 thorpej */
560 1.1.2.2 thorpej error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
561 1.1.2.2 thorpej if(error)
562 1.1.2.2 thorpej goto out1;
563 1.1.2.2 thorpej
564 1.1.2.2 thorpej /*
565 1.1.2.2 thorpej * Read and translate to internal format attribute
566 1.1.2.2 thorpej * definition file.
567 1.1.2.2 thorpej */
568 1.1.2.2 thorpej {
569 1.1.2.2 thorpej int num,j;
570 1.1.2.2 thorpej struct attrdef ad;
571 1.1.2.2 thorpej
572 1.1.2.2 thorpej /* Open $AttrDef */
573 1.1.2.2 thorpej error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
574 1.1.2.2 thorpej if(error)
575 1.1.2.2 thorpej goto out1;
576 1.1.2.2 thorpej
577 1.1.2.2 thorpej /* Count valid entries */
578 1.1.2.2 thorpej for(num=0;;num++) {
579 1.1.2.2 thorpej error = ntfs_readattr(ntmp, VTONT(vp),
580 1.1.2.2 thorpej NTFS_A_DATA, NULL,
581 1.1.2.2 thorpej num * sizeof(ad), sizeof(ad),
582 1.1.2.2 thorpej &ad, NULL);
583 1.1.2.2 thorpej if (error)
584 1.1.2.2 thorpej goto out1;
585 1.1.2.2 thorpej if (ad.ad_name[0] == 0)
586 1.1.2.2 thorpej break;
587 1.1.2.2 thorpej }
588 1.1.2.2 thorpej
589 1.1.2.2 thorpej /* Alloc memory for attribute definitions */
590 1.1.2.2 thorpej ntmp->ntm_ad = (struct ntvattrdef *) malloc(
591 1.1.2.2 thorpej num * sizeof(struct ntvattrdef),
592 1.1.2.2 thorpej M_NTFSMNT, M_WAITOK);
593 1.1.2.2 thorpej
594 1.1.2.2 thorpej ntmp->ntm_adnum = num;
595 1.1.2.2 thorpej
596 1.1.2.2 thorpej /* Read them and translate */
597 1.1.2.2 thorpej for(i=0;i<num;i++){
598 1.1.2.2 thorpej error = ntfs_readattr(ntmp, VTONT(vp),
599 1.1.2.2 thorpej NTFS_A_DATA, NULL,
600 1.1.2.2 thorpej i * sizeof(ad), sizeof(ad),
601 1.1.2.2 thorpej &ad, NULL);
602 1.1.2.2 thorpej if (error)
603 1.1.2.2 thorpej goto out1;
604 1.1.2.2 thorpej j = 0;
605 1.1.2.2 thorpej do {
606 1.1.2.2 thorpej ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
607 1.1.2.2 thorpej } while(ad.ad_name[j++]);
608 1.1.2.2 thorpej ntmp->ntm_ad[i].ad_namelen = j - 1;
609 1.1.2.2 thorpej ntmp->ntm_ad[i].ad_type = ad.ad_type;
610 1.1.2.2 thorpej }
611 1.1.2.2 thorpej
612 1.1.2.2 thorpej vput(vp);
613 1.1.2.2 thorpej }
614 1.1.2.2 thorpej
615 1.1.2.2 thorpej #if defined(__FreeBSD__)
616 1.1.2.2 thorpej mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
617 1.1.2.2 thorpej mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
618 1.1.2.2 thorpej #else
619 1.1.2.2 thorpej mp->mnt_stat.f_fsid.val[0] = dev;
620 1.1.2.2 thorpej mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS);
621 1.1.2.2 thorpej #endif
622 1.1.2.2 thorpej mp->mnt_maxsymlinklen = 0;
623 1.1.2.2 thorpej mp->mnt_flag |= MNT_LOCAL;
624 1.1.2.2 thorpej devvp->v_specmountpoint = mp;
625 1.1.2.2 thorpej return (0);
626 1.1.2.2 thorpej
627 1.1.2.2 thorpej out1:
628 1.1.2.2 thorpej for(i=0;i<NTFS_SYSNODESNUM;i++)
629 1.1.2.2 thorpej if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
630 1.1.2.2 thorpej
631 1.1.2.2 thorpej if (vflush(mp,NULLVP,0))
632 1.1.2.2 thorpej dprintf(("ntfs_mountfs: vflush failed\n"));
633 1.1.2.2 thorpej
634 1.1.2.2 thorpej out:
635 1.1.2.2 thorpej devvp->v_specmountpoint = NULL;
636 1.1.2.2 thorpej if (bp)
637 1.1.2.2 thorpej brelse(bp);
638 1.1.2.2 thorpej
639 1.1.2.2 thorpej #if defined __NetBSD__
640 1.1.2.2 thorpej /* lock the device vnode before calling VOP_CLOSE() */
641 1.1.2.2 thorpej VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, p);
642 1.1.2.2 thorpej (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
643 1.1.2.2 thorpej VOP__UNLOCK(devvp, 0, p);
644 1.1.2.2 thorpej #else
645 1.1.2.2 thorpej (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
646 1.1.2.2 thorpej #endif
647 1.1.2.2 thorpej
648 1.1.2.2 thorpej return (error);
649 1.1.2.2 thorpej }
650 1.1.2.2 thorpej
651 1.1.2.2 thorpej static int
652 1.1.2.2 thorpej ntfs_start (
653 1.1.2.2 thorpej struct mount *mp,
654 1.1.2.2 thorpej int flags,
655 1.1.2.2 thorpej struct proc *p )
656 1.1.2.2 thorpej {
657 1.1.2.2 thorpej return (0);
658 1.1.2.2 thorpej }
659 1.1.2.2 thorpej
660 1.1.2.2 thorpej static int
661 1.1.2.2 thorpej ntfs_unmount(
662 1.1.2.2 thorpej struct mount *mp,
663 1.1.2.2 thorpej int mntflags,
664 1.1.2.2 thorpej struct proc *p)
665 1.1.2.2 thorpej {
666 1.1.2.2 thorpej struct ntfsmount *ntmp;
667 1.1.2.2 thorpej int error, ronly = 0, flags, i;
668 1.1.2.2 thorpej
669 1.1.2.2 thorpej dprintf(("ntfs_unmount: unmounting...\n"));
670 1.1.2.2 thorpej ntmp = VFSTONTFS(mp);
671 1.1.2.2 thorpej
672 1.1.2.2 thorpej flags = 0;
673 1.1.2.2 thorpej if(mntflags & MNT_FORCE)
674 1.1.2.2 thorpej flags |= FORCECLOSE;
675 1.1.2.2 thorpej
676 1.1.2.2 thorpej dprintf(("ntfs_unmount: vflushing...\n"));
677 1.1.2.2 thorpej error = vflush(mp,NULLVP,flags | SKIPSYSTEM);
678 1.1.2.2 thorpej if (error) {
679 1.1.2.2 thorpej dprintf(("ntfs_unmount: vflush failed: %d\n",error));
680 1.1.2.2 thorpej return (error);
681 1.1.2.2 thorpej }
682 1.1.2.2 thorpej
683 1.1.2.2 thorpej /* Check if only system vnodes are rest */
684 1.1.2.2 thorpej for(i=0;i<NTFS_SYSNODESNUM;i++)
685 1.1.2.2 thorpej if((ntmp->ntm_sysvn[i]) &&
686 1.1.2.2 thorpej (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
687 1.1.2.2 thorpej
688 1.1.2.2 thorpej /* Dereference all system vnodes */
689 1.1.2.2 thorpej for(i=0;i<NTFS_SYSNODESNUM;i++)
690 1.1.2.2 thorpej if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
691 1.1.2.2 thorpej
692 1.1.2.2 thorpej /* vflush system vnodes */
693 1.1.2.2 thorpej error = vflush(mp,NULLVP,flags);
694 1.1.2.2 thorpej if (error) {
695 1.1.2.2 thorpej /* XXX should this be panic() ? */
696 1.1.2.2 thorpej printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
697 1.1.2.2 thorpej }
698 1.1.2.2 thorpej
699 1.1.2.2 thorpej /* Check if the type of device node isn't VBAD before
700 1.1.2.2 thorpej * touching v_specinfo. If the device vnode is revoked, the
701 1.1.2.2 thorpej * field is NULL and touching it causes null pointer derefercence.
702 1.1.2.2 thorpej */
703 1.1.2.2 thorpej if (ntmp->ntm_devvp->v_type != VBAD)
704 1.1.2.2 thorpej ntmp->ntm_devvp->v_specmountpoint = NULL;
705 1.1.2.2 thorpej
706 1.1.2.2 thorpej vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, p, 0, 0);
707 1.1.2.2 thorpej
708 1.1.2.2 thorpej #if defined(__NetBSD__)
709 1.1.2.2 thorpej /* lock the device vnode before calling VOP_CLOSE() */
710 1.1.2.2 thorpej VOP_LOCK(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
711 1.1.2.2 thorpej error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
712 1.1.2.2 thorpej NOCRED, p);
713 1.1.2.2 thorpej VOP__UNLOCK(ntmp->ntm_devvp, 0, p);
714 1.1.2.2 thorpej #else
715 1.1.2.2 thorpej error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
716 1.1.2.2 thorpej NOCRED, p);
717 1.1.2.2 thorpej #endif
718 1.1.2.2 thorpej
719 1.1.2.2 thorpej vrele(ntmp->ntm_devvp);
720 1.1.2.2 thorpej
721 1.1.2.2 thorpej /* free the toupper table, if this has been last mounted ntfs volume */
722 1.1.2.2 thorpej ntfs_toupper_unuse();
723 1.1.2.2 thorpej
724 1.1.2.2 thorpej dprintf(("ntfs_umount: freeing memory...\n"));
725 1.1.2.2 thorpej mp->mnt_data = NULL;
726 1.1.2.2 thorpej mp->mnt_flag &= ~MNT_LOCAL;
727 1.1.2.2 thorpej free(ntmp->ntm_ad, M_NTFSMNT);
728 1.1.2.2 thorpej FREE(ntmp, M_NTFSMNT);
729 1.1.2.2 thorpej return (error);
730 1.1.2.2 thorpej }
731 1.1.2.2 thorpej
732 1.1.2.2 thorpej static int
733 1.1.2.2 thorpej ntfs_root(
734 1.1.2.2 thorpej struct mount *mp,
735 1.1.2.2 thorpej struct vnode **vpp )
736 1.1.2.2 thorpej {
737 1.1.2.2 thorpej struct vnode *nvp;
738 1.1.2.2 thorpej int error = 0;
739 1.1.2.2 thorpej
740 1.1.2.2 thorpej dprintf(("ntfs_root(): sysvn: %p\n",
741 1.1.2.2 thorpej VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
742 1.1.2.2 thorpej error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
743 1.1.2.2 thorpej if(error) {
744 1.1.2.2 thorpej printf("ntfs_root: VFS_VGET failed: %d\n",error);
745 1.1.2.2 thorpej return (error);
746 1.1.2.2 thorpej }
747 1.1.2.2 thorpej
748 1.1.2.2 thorpej *vpp = nvp;
749 1.1.2.2 thorpej return (0);
750 1.1.2.2 thorpej }
751 1.1.2.2 thorpej
752 1.1.2.2 thorpej static int
753 1.1.2.2 thorpej ntfs_quotactl (
754 1.1.2.2 thorpej struct mount *mp,
755 1.1.2.2 thorpej int cmds,
756 1.1.2.2 thorpej uid_t uid,
757 1.1.2.2 thorpej caddr_t arg,
758 1.1.2.2 thorpej struct proc *p)
759 1.1.2.2 thorpej {
760 1.1.2.2 thorpej printf("\nntfs_quotactl():\n");
761 1.1.2.2 thorpej return EOPNOTSUPP;
762 1.1.2.2 thorpej }
763 1.1.2.2 thorpej
764 1.1.2.2 thorpej int
765 1.1.2.2 thorpej ntfs_calccfree(
766 1.1.2.2 thorpej struct ntfsmount *ntmp,
767 1.1.2.2 thorpej cn_t *cfreep)
768 1.1.2.2 thorpej {
769 1.1.2.2 thorpej struct vnode *vp;
770 1.1.2.2 thorpej u_int8_t *tmp;
771 1.1.2.2 thorpej int j, error;
772 1.1.2.2 thorpej long cfree = 0;
773 1.1.2.2 thorpej size_t bmsize, i;
774 1.1.2.2 thorpej
775 1.1.2.2 thorpej vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
776 1.1.2.2 thorpej
777 1.1.2.2 thorpej bmsize = VTOF(vp)->f_size;
778 1.1.2.2 thorpej
779 1.1.2.2 thorpej tmp = (u_int8_t *) malloc(bmsize, M_TEMP, M_WAITOK);
780 1.1.2.2 thorpej
781 1.1.2.2 thorpej error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
782 1.1.2.2 thorpej 0, bmsize, tmp, NULL);
783 1.1.2.2 thorpej if (error)
784 1.1.2.2 thorpej goto out;
785 1.1.2.2 thorpej
786 1.1.2.2 thorpej for(i=0;i<bmsize;i++)
787 1.1.2.2 thorpej for(j=0;j<8;j++)
788 1.1.2.2 thorpej if(~tmp[i] & (1 << j)) cfree++;
789 1.1.2.2 thorpej *cfreep = cfree;
790 1.1.2.2 thorpej
791 1.1.2.2 thorpej out:
792 1.1.2.2 thorpej free(tmp, M_TEMP);
793 1.1.2.2 thorpej return(error);
794 1.1.2.2 thorpej }
795 1.1.2.2 thorpej
796 1.1.2.2 thorpej static int
797 1.1.2.2 thorpej ntfs_statfs(
798 1.1.2.2 thorpej struct mount *mp,
799 1.1.2.2 thorpej struct statfs *sbp,
800 1.1.2.2 thorpej struct proc *p)
801 1.1.2.2 thorpej {
802 1.1.2.2 thorpej struct ntfsmount *ntmp = VFSTONTFS(mp);
803 1.1.2.2 thorpej u_int64_t mftallocated;
804 1.1.2.2 thorpej
805 1.1.2.2 thorpej dprintf(("ntfs_statfs():\n"));
806 1.1.2.2 thorpej
807 1.1.2.2 thorpej mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
808 1.1.2.2 thorpej
809 1.1.2.2 thorpej #if defined(__FreeBSD__)
810 1.1.2.2 thorpej sbp->f_type = mp->mnt_vfc->vfc_typenum;
811 1.1.2.2 thorpej #elif defined(__NetBSD__)
812 1.1.2.2 thorpej sbp->f_type = 0;
813 1.1.2.2 thorpej #else
814 1.1.2.2 thorpej sbp->f_type = MOUNT_NTFS;
815 1.1.2.2 thorpej #endif
816 1.1.2.2 thorpej sbp->f_bsize = ntmp->ntm_bps;
817 1.1.2.2 thorpej sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
818 1.1.2.2 thorpej sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
819 1.1.2.2 thorpej sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
820 1.1.2.2 thorpej sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
821 1.1.2.2 thorpej sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
822 1.1.2.2 thorpej sbp->f_ffree;
823 1.1.2.2 thorpej if (sbp != &mp->mnt_stat) {
824 1.1.2.2 thorpej bcopy((caddr_t)mp->mnt_stat.f_mntonname,
825 1.1.2.2 thorpej (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
826 1.1.2.2 thorpej bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
827 1.1.2.2 thorpej (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
828 1.1.2.2 thorpej }
829 1.1.2.2 thorpej sbp->f_flags = mp->mnt_flag;
830 1.1.2.2 thorpej #ifdef __NetBSD__
831 1.1.2.2 thorpej strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
832 1.1.2.2 thorpej #endif
833 1.1.2.2 thorpej
834 1.1.2.2 thorpej return (0);
835 1.1.2.2 thorpej }
836 1.1.2.2 thorpej
837 1.1.2.2 thorpej static int
838 1.1.2.2 thorpej ntfs_sync (
839 1.1.2.2 thorpej struct mount *mp,
840 1.1.2.2 thorpej int waitfor,
841 1.1.2.2 thorpej struct ucred *cred,
842 1.1.2.2 thorpej struct proc *p)
843 1.1.2.2 thorpej {
844 1.1.2.2 thorpej /*dprintf(("ntfs_sync():\n"));*/
845 1.1.2.2 thorpej return (0);
846 1.1.2.2 thorpej }
847 1.1.2.2 thorpej
848 1.1.2.2 thorpej /*ARGSUSED*/
849 1.1.2.2 thorpej static int
850 1.1.2.2 thorpej ntfs_fhtovp(
851 1.1.2.2 thorpej #if defined(__FreeBSD__)
852 1.1.2.2 thorpej struct mount *mp,
853 1.1.2.2 thorpej struct fid *fhp,
854 1.1.2.2 thorpej struct sockaddr *nam,
855 1.1.2.2 thorpej struct vnode **vpp,
856 1.1.2.2 thorpej int *exflagsp,
857 1.1.2.2 thorpej struct ucred **credanonp)
858 1.1.2.2 thorpej #elif defined(__NetBSD__)
859 1.1.2.2 thorpej struct mount *mp,
860 1.1.2.2 thorpej struct fid *fhp,
861 1.1.2.2 thorpej struct vnode **vpp)
862 1.1.2.2 thorpej #else
863 1.1.2.2 thorpej struct mount *mp,
864 1.1.2.2 thorpej struct fid *fhp,
865 1.1.2.2 thorpej struct mbuf *nam,
866 1.1.2.2 thorpej struct vnode **vpp,
867 1.1.2.2 thorpej int *exflagsp,
868 1.1.2.2 thorpej struct ucred **credanonp)
869 1.1.2.2 thorpej #endif
870 1.1.2.2 thorpej {
871 1.1.2.2 thorpej struct ntfid *ntfhp = (struct ntfid *)fhp;
872 1.1.2.2 thorpej int error;
873 1.1.2.2 thorpej
874 1.1.2.2 thorpej ddprintf(("ntfs_fhtovp(): %s: %d\n", mp->mnt_stat.f_mntonname,
875 1.1.2.2 thorpej ntfhp->ntfid_ino));
876 1.1.2.2 thorpej
877 1.1.2.2 thorpej error = ntfs_vgetex(mp, ntfhp->ntfid_ino, ntfhp->ntfid_attr, NULL,
878 1.1.2.2 thorpej LK_EXCLUSIVE | LK_RETRY, 0, curproc, vpp); /* XXX */
879 1.1.2.2 thorpej if (error != 0) {
880 1.1.2.2 thorpej *vpp = NULLVP;
881 1.1.2.2 thorpej return (error);
882 1.1.2.2 thorpej }
883 1.1.2.2 thorpej
884 1.1.2.2 thorpej /* XXX as unlink/rmdir/mkdir/creat are not currently possible
885 1.1.2.2 thorpej * with NTFS, we don't need to check anything else for now */
886 1.1.2.2 thorpej return (0);
887 1.1.2.2 thorpej }
888 1.1.2.2 thorpej
889 1.1.2.2 thorpej static int
890 1.1.2.2 thorpej ntfs_vptofh(
891 1.1.2.2 thorpej struct vnode *vp,
892 1.1.2.2 thorpej struct fid *fhp)
893 1.1.2.2 thorpej {
894 1.1.2.2 thorpej struct ntnode *ntp;
895 1.1.2.2 thorpej struct ntfid *ntfhp;
896 1.1.2.2 thorpej struct fnode *fn;
897 1.1.2.2 thorpej
898 1.1.2.2 thorpej ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat.f_mntonname,
899 1.1.2.2 thorpej vp));
900 1.1.2.2 thorpej
901 1.1.2.2 thorpej fn = VTOF(vp);
902 1.1.2.2 thorpej ntp = VTONT(vp);
903 1.1.2.2 thorpej ntfhp = (struct ntfid *)fhp;
904 1.1.2.2 thorpej ntfhp->ntfid_len = sizeof(struct ntfid);
905 1.1.2.2 thorpej ntfhp->ntfid_ino = ntp->i_number;
906 1.1.2.2 thorpej ntfhp->ntfid_attr = fn->f_attrtype;
907 1.1.2.2 thorpej #ifdef notyet
908 1.1.2.2 thorpej ntfhp->ntfid_gen = ntp->i_gen;
909 1.1.2.2 thorpej #endif
910 1.1.2.2 thorpej return (0);
911 1.1.2.2 thorpej }
912 1.1.2.2 thorpej
913 1.1.2.2 thorpej int
914 1.1.2.2 thorpej ntfs_vgetex(
915 1.1.2.2 thorpej struct mount *mp,
916 1.1.2.2 thorpej ino_t ino,
917 1.1.2.2 thorpej u_int32_t attrtype,
918 1.1.2.2 thorpej char *attrname,
919 1.1.2.2 thorpej u_long lkflags,
920 1.1.2.2 thorpej u_long flags,
921 1.1.2.2 thorpej struct proc *p,
922 1.1.2.2 thorpej struct vnode **vpp)
923 1.1.2.2 thorpej {
924 1.1.2.2 thorpej int error;
925 1.1.2.2 thorpej struct ntfsmount *ntmp;
926 1.1.2.2 thorpej struct ntnode *ip;
927 1.1.2.2 thorpej struct fnode *fp;
928 1.1.2.2 thorpej struct vnode *vp;
929 1.1.2.2 thorpej enum vtype f_type;
930 1.1.2.2 thorpej
931 1.1.2.2 thorpej dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
932 1.1.2.2 thorpej ino, attrtype, attrname?attrname:"", (u_long)lkflags,
933 1.1.2.2 thorpej (u_long)flags ));
934 1.1.2.2 thorpej
935 1.1.2.2 thorpej ntmp = VFSTONTFS(mp);
936 1.1.2.2 thorpej *vpp = NULL;
937 1.1.2.2 thorpej
938 1.1.2.2 thorpej /* Get ntnode */
939 1.1.2.2 thorpej error = ntfs_ntlookup(ntmp, ino, &ip);
940 1.1.2.2 thorpej if (error) {
941 1.1.2.2 thorpej printf("ntfs_vget: ntfs_ntget failed\n");
942 1.1.2.2 thorpej return (error);
943 1.1.2.2 thorpej }
944 1.1.2.2 thorpej
945 1.1.2.2 thorpej /* It may be not initialized fully, so force load it */
946 1.1.2.2 thorpej if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
947 1.1.2.2 thorpej error = ntfs_loadntnode(ntmp, ip);
948 1.1.2.2 thorpej if(error) {
949 1.1.2.2 thorpej printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
950 1.1.2.2 thorpej ip->i_number);
951 1.1.2.2 thorpej ntfs_ntput(ip);
952 1.1.2.2 thorpej return (error);
953 1.1.2.2 thorpej }
954 1.1.2.2 thorpej }
955 1.1.2.2 thorpej
956 1.1.2.2 thorpej error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
957 1.1.2.2 thorpej if (error) {
958 1.1.2.2 thorpej printf("ntfs_vget: ntfs_fget failed\n");
959 1.1.2.2 thorpej ntfs_ntput(ip);
960 1.1.2.2 thorpej return (error);
961 1.1.2.2 thorpej }
962 1.1.2.2 thorpej
963 1.1.2.2 thorpej if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
964 1.1.2.2 thorpej if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
965 1.1.2.2 thorpej (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
966 1.1.2.2 thorpej f_type = VDIR;
967 1.1.2.2 thorpej } else if (flags & VG_EXT) {
968 1.1.2.2 thorpej f_type = VNON;
969 1.1.2.2 thorpej fp->f_size = fp->f_allocated = 0;
970 1.1.2.2 thorpej } else {
971 1.1.2.2 thorpej f_type = VREG;
972 1.1.2.2 thorpej
973 1.1.2.2 thorpej error = ntfs_filesize(ntmp, fp,
974 1.1.2.2 thorpej &fp->f_size, &fp->f_allocated);
975 1.1.2.2 thorpej if (error) {
976 1.1.2.2 thorpej ntfs_ntput(ip);
977 1.1.2.2 thorpej return (error);
978 1.1.2.2 thorpej }
979 1.1.2.2 thorpej }
980 1.1.2.2 thorpej
981 1.1.2.2 thorpej fp->f_flag |= FN_VALID;
982 1.1.2.2 thorpej }
983 1.1.2.2 thorpej
984 1.1.2.2 thorpej /*
985 1.1.2.2 thorpej * We may be calling vget() now. To avoid potential deadlock, we need
986 1.1.2.2 thorpej * to release ntnode lock, since due to locking order vnode
987 1.1.2.2 thorpej * lock has to be acquired first.
988 1.1.2.2 thorpej * ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled
989 1.1.2.2 thorpej * prematurely.
990 1.1.2.2 thorpej */
991 1.1.2.2 thorpej ntfs_ntput(ip);
992 1.1.2.2 thorpej
993 1.1.2.2 thorpej if (FTOV(fp)) {
994 1.1.2.2 thorpej /* vget() returns error if the vnode has been recycled */
995 1.1.2.2 thorpej if (VGET(FTOV(fp), lkflags, p) == 0) {
996 1.1.2.2 thorpej *vpp = FTOV(fp);
997 1.1.2.2 thorpej return (0);
998 1.1.2.2 thorpej }
999 1.1.2.2 thorpej }
1000 1.1.2.2 thorpej
1001 1.1.2.2 thorpej error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp);
1002 1.1.2.2 thorpej if(error) {
1003 1.1.2.2 thorpej ntfs_frele(fp);
1004 1.1.2.2 thorpej ntfs_ntput(ip);
1005 1.1.2.2 thorpej return (error);
1006 1.1.2.2 thorpej }
1007 1.1.2.2 thorpej dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
1008 1.1.2.2 thorpej
1009 1.1.2.2 thorpej #ifdef __FreeBSD__
1010 1.1.2.2 thorpej lockinit(&fp->f_lock, PINOD, "fnode", 0, 0);
1011 1.1.2.2 thorpej #endif
1012 1.1.2.2 thorpej fp->f_vp = vp;
1013 1.1.2.2 thorpej vp->v_data = fp;
1014 1.1.2.2 thorpej vp->v_type = f_type;
1015 1.1.2.2 thorpej
1016 1.1.2.2 thorpej if (ino == NTFS_ROOTINO)
1017 1.1.2.2 thorpej vp->v_flag |= VROOT;
1018 1.1.2.2 thorpej
1019 1.1.2.2 thorpej if (lkflags & LK_TYPE_MASK) {
1020 1.1.2.2 thorpej error = VN_LOCK(vp, lkflags, p);
1021 1.1.2.2 thorpej if (error) {
1022 1.1.2.2 thorpej vput(vp);
1023 1.1.2.2 thorpej return (error);
1024 1.1.2.2 thorpej }
1025 1.1.2.2 thorpej }
1026 1.1.2.2 thorpej
1027 1.1.2.2 thorpej genfs_node_init(vp, &ntfs_genfsops);
1028 1.1.2.2 thorpej VREF(ip->i_devvp);
1029 1.1.2.2 thorpej *vpp = vp;
1030 1.1.2.2 thorpej return (0);
1031 1.1.2.2 thorpej }
1032 1.1.2.2 thorpej
1033 1.1.2.2 thorpej static int
1034 1.1.2.2 thorpej ntfs_vget(
1035 1.1.2.2 thorpej struct mount *mp,
1036 1.1.2.2 thorpej ino_t ino,
1037 1.1.2.2 thorpej struct vnode **vpp)
1038 1.1.2.2 thorpej {
1039 1.1.2.2 thorpej return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
1040 1.1.2.2 thorpej LK_EXCLUSIVE | LK_RETRY, 0, curproc, vpp); /* XXX */
1041 1.1.2.2 thorpej }
1042 1.1.2.2 thorpej
1043 1.1.2.2 thorpej #if defined(__FreeBSD__)
1044 1.1.2.2 thorpej static struct vfsops ntfs_vfsops = {
1045 1.1.2.2 thorpej ntfs_mount,
1046 1.1.2.2 thorpej ntfs_start,
1047 1.1.2.2 thorpej ntfs_unmount,
1048 1.1.2.2 thorpej ntfs_root,
1049 1.1.2.2 thorpej ntfs_quotactl,
1050 1.1.2.2 thorpej ntfs_statfs,
1051 1.1.2.2 thorpej ntfs_sync,
1052 1.1.2.2 thorpej ntfs_vget,
1053 1.1.2.2 thorpej ntfs_fhtovp,
1054 1.1.2.2 thorpej ntfs_vptofh,
1055 1.1.2.2 thorpej ntfs_init,
1056 1.1.2.2 thorpej NULL
1057 1.1.2.2 thorpej };
1058 1.1.2.2 thorpej VFS_SET(ntfs_vfsops, ntfs, 0);
1059 1.1.2.2 thorpej #elif defined(__NetBSD__)
1060 1.1.2.2 thorpej extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc;
1061 1.1.2.2 thorpej
1062 1.1.2.2 thorpej const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = {
1063 1.1.2.2 thorpej &ntfs_vnodeop_opv_desc,
1064 1.1.2.2 thorpej NULL,
1065 1.1.2.2 thorpej };
1066 1.1.2.2 thorpej
1067 1.1.2.2 thorpej struct vfsops ntfs_vfsops = {
1068 1.1.2.2 thorpej MOUNT_NTFS,
1069 1.1.2.2 thorpej ntfs_mount,
1070 1.1.2.2 thorpej ntfs_start,
1071 1.1.2.2 thorpej ntfs_unmount,
1072 1.1.2.2 thorpej ntfs_root,
1073 1.1.2.2 thorpej ntfs_quotactl,
1074 1.1.2.2 thorpej ntfs_statfs,
1075 1.1.2.2 thorpej ntfs_sync,
1076 1.1.2.2 thorpej ntfs_vget,
1077 1.1.2.2 thorpej ntfs_fhtovp,
1078 1.1.2.2 thorpej ntfs_vptofh,
1079 1.1.2.2 thorpej ntfs_init,
1080 1.1.2.2 thorpej ntfs_reinit,
1081 1.1.2.2 thorpej ntfs_done,
1082 1.1.2.2 thorpej ntfs_sysctl,
1083 1.1.2.2 thorpej ntfs_mountroot,
1084 1.1.2.2 thorpej ntfs_checkexp,
1085 1.1.2.2 thorpej ntfs_vnodeopv_descs,
1086 1.1.2.2 thorpej };
1087 1.1.2.2 thorpej #endif
1088