Annotation of src/sys/fs/msdosfs/msdosfs_vnops.c, Revision 1.38.2.5
1.38.2.5! ad 1: /* $NetBSD: msdosfs_vnops.c,v 1.38.2.4 2007/08/19 19:24:50 ad Exp $ */
1.1 jdolecek 2:
3: /*-
4: * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5: * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6: * All rights reserved.
7: * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
8: *
9: * Redistribution and use in source and binary forms, with or without
10: * modification, are permitted provided that the following conditions
11: * are met:
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: * 2. Redistributions in binary form must reproduce the above copyright
15: * notice, this list of conditions and the following disclaimer in the
16: * documentation and/or other materials provided with the distribution.
17: * 3. All advertising materials mentioning features or use of this software
18: * must display the following acknowledgement:
19: * This product includes software developed by TooLs GmbH.
20: * 4. The name of TooLs GmbH may not be used to endorse or promote products
21: * derived from this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26: * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33: */
34: /*
35: * Written by Paul Popelka (paulp@uts.amdahl.com)
36: *
37: * You can do anything you want with this software, just don't say you wrote
38: * it, and don't remove this notice.
39: *
40: * This software is provided "as is".
41: *
42: * The author supplies this software to be publicly redistributed on the
43: * understanding that the author is not responsible for the correct
44: * functioning of this software in any circumstances and is not liable for
45: * any damages caused by this software.
46: *
47: * October 1992
48: */
49:
50: #include <sys/cdefs.h>
1.38.2.5! ad 51: __KERNEL_RCSID(0, "$NetBSD: msdosfs_vnops.c,v 1.38.2.4 2007/08/19 19:24:50 ad Exp $");
1.1 jdolecek 52:
53: #include <sys/param.h>
54: #include <sys/systm.h>
55: #include <sys/namei.h>
56: #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
57: #include <sys/kernel.h>
58: #include <sys/file.h> /* define FWRITE ... */
59: #include <sys/stat.h>
60: #include <sys/buf.h>
61: #include <sys/proc.h>
62: #include <sys/mount.h>
63: #include <sys/vnode.h>
64: #include <sys/signalvar.h>
65: #include <sys/malloc.h>
66: #include <sys/dirent.h>
67: #include <sys/lockf.h>
1.28 elad 68: #include <sys/kauth.h>
1.1 jdolecek 69:
70: #include <miscfs/genfs/genfs.h>
71: #include <miscfs/specfs/specdev.h> /* XXX */ /* defines v_rdev */
72:
73: #include <uvm/uvm_extern.h>
74:
75: #include <fs/msdosfs/bpb.h>
76: #include <fs/msdosfs/direntry.h>
77: #include <fs/msdosfs/denode.h>
78: #include <fs/msdosfs/msdosfsmount.h>
79: #include <fs/msdosfs/fat.h>
80:
81: /*
82: * Some general notes:
83: *
84: * In the ufs filesystem the inodes, superblocks, and indirect blocks are
85: * read/written using the vnode for the filesystem. Blocks that represent
86: * the contents of a file are read/written using the vnode for the file
87: * (including directories when they are read/written as files). This
88: * presents problems for the dos filesystem because data that should be in
89: * an inode (if dos had them) resides in the directory itself. Since we
90: * must update directory entries without the benefit of having the vnode
91: * for the directory we must use the vnode for the filesystem. This means
92: * that when a directory is actually read/written (via read, write, or
93: * readdir, or seek) we must use the vnode for the filesystem instead of
94: * the vnode for the directory as would happen in ufs. This is to insure we
95: * retrieve the correct block from the buffer cache since the hash value is
96: * based upon the vnode address and the desired block number.
97: */
98:
99: /*
100: * Create a regular file. On entry the directory to contain the file being
101: * created is locked. We must release before we return. We must also free
102: * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
103: * only if the SAVESTART bit in cn_flags is clear on success.
104: */
105: int
106: msdosfs_create(v)
107: void *v;
108: {
109: struct vop_create_args /* {
110: struct vnode *a_dvp;
111: struct vnode **a_vpp;
112: struct componentname *a_cnp;
113: struct vattr *a_vap;
114: } */ *ap = v;
115: struct componentname *cnp = ap->a_cnp;
116: struct denode ndirent;
117: struct denode *dep;
118: struct denode *pdep = VTODE(ap->a_dvp);
119: int error;
120:
121: #ifdef MSDOSFS_DEBUG
122: printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
123: #endif
124:
125: /*
126: * If this is the root directory and there is no space left we
127: * can't do anything. This is because the root directory can not
128: * change size.
129: */
130: if (pdep->de_StartCluster == MSDOSFSROOT
131: && pdep->de_fndoffset >= pdep->de_FileSize) {
132: error = ENOSPC;
133: goto bad;
134: }
135:
136: /*
137: * Create a directory entry for the file, then call createde() to
138: * have it installed. NOTE: DOS files are always executable. We
139: * use the absence of the owner write bit to make the file
140: * readonly.
141: */
142: #ifdef DIAGNOSTIC
143: if ((cnp->cn_flags & HASBUF) == 0)
144: panic("msdosfs_create: no name");
145: #endif
146: memset(&ndirent, 0, sizeof(ndirent));
147: if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
148: goto bad;
149:
150: ndirent.de_Attributes = (ap->a_vap->va_mode & S_IWUSR) ?
151: ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
152: ndirent.de_StartCluster = 0;
153: ndirent.de_FileSize = 0;
154: ndirent.de_dev = pdep->de_dev;
155: ndirent.de_devvp = pdep->de_devvp;
156: ndirent.de_pmp = pdep->de_pmp;
157: ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1.20 christos 158: DETIMES(&ndirent, NULL, NULL, NULL, pdep->de_pmp->pm_gmtoff);
1.1 jdolecek 159: if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
160: goto bad;
161: if ((cnp->cn_flags & SAVESTART) == 0)
162: PNBUF_PUT(cnp->cn_pnbuf);
163: VN_KNOTE(ap->a_dvp, NOTE_WRITE);
164: vput(ap->a_dvp);
165: *ap->a_vpp = DETOV(dep);
166: return (0);
167:
168: bad:
169: PNBUF_PUT(cnp->cn_pnbuf);
170: vput(ap->a_dvp);
171: return (error);
172: }
173:
174: int
175: msdosfs_mknod(v)
176: void *v;
177: {
178: struct vop_mknod_args /* {
179: struct vnode *a_dvp;
180: struct vnode **a_vpp;
181: struct componentname *a_cnp;
182: struct vattr *a_vap;
183: } */ *ap = v;
184:
185: PNBUF_PUT(ap->a_cnp->cn_pnbuf);
186: vput(ap->a_dvp);
187: return (EINVAL);
188: }
189:
190: int
1.33 christos 191: msdosfs_open(void *v)
1.1 jdolecek 192: {
193: #if 0
194: struct vop_open_args /* {
195: struct vnode *a_vp;
196: int a_mode;
1.28 elad 197: kauth_cred_t a_cred;
1.24 christos 198: struct lwp *a_l;
1.1 jdolecek 199: } */ *ap;
200: #endif
201:
202: return (0);
203: }
204:
205: int
206: msdosfs_close(v)
207: void *v;
208: {
209: struct vop_close_args /* {
210: struct vnode *a_vp;
211: int a_fflag;
1.28 elad 212: kauth_cred_t a_cred;
1.24 christos 213: struct lwp *a_l;
1.1 jdolecek 214: } */ *ap = v;
215: struct vnode *vp = ap->a_vp;
216: struct denode *dep = VTODE(vp);
217:
1.38.2.1 ad 218: mutex_enter(&vp->v_interlock);
1.20 christos 219: if (vp->v_usecount > 1)
220: DETIMES(dep, NULL, NULL, NULL, dep->de_pmp->pm_gmtoff);
1.38.2.1 ad 221: mutex_exit(&vp->v_interlock);
1.1 jdolecek 222: return (0);
223: }
224:
225: int
226: msdosfs_access(v)
227: void *v;
228: {
229: struct vop_access_args /* {
230: struct vnode *a_vp;
231: int a_mode;
1.28 elad 232: kauth_cred_t a_cred;
1.24 christos 233: struct lwp *a_l;
1.1 jdolecek 234: } */ *ap = v;
235: struct vnode *vp = ap->a_vp;
236: struct denode *dep = VTODE(vp);
237: struct msdosfsmount *pmp = dep->de_pmp;
238: mode_t mode = ap->a_mode;
239:
240: /*
241: * Disallow write attempts on read-only file systems;
242: * unless the file is a socket, fifo, or a block or
243: * character device resident on the file system.
244: */
245: if (mode & VWRITE) {
246: switch (vp->v_type) {
247: case VDIR:
248: case VLNK:
249: case VREG:
250: if (vp->v_mount->mnt_flag & MNT_RDONLY)
251: return (EROFS);
252: default:
253: break;
254: }
255: }
256:
257: if ((dep->de_Attributes & ATTR_READONLY) == 0)
258: mode = S_IRWXU|S_IRWXG|S_IRWXO;
259: else
260: mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
1.6 jdolecek 261: return (vaccess(ap->a_vp->v_type,
262: mode & (vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask),
1.1 jdolecek 263: pmp->pm_uid, pmp->pm_gid, ap->a_mode, ap->a_cred));
264: }
265:
266: int
267: msdosfs_getattr(v)
268: void *v;
269: {
270: struct vop_getattr_args /* {
271: struct vnode *a_vp;
272: struct vattr *a_vap;
1.28 elad 273: kauth_cred_t a_cred;
1.24 christos 274: struct lwp *a_l;
1.1 jdolecek 275: } */ *ap = v;
276: struct denode *dep = VTODE(ap->a_vp);
277: struct msdosfsmount *pmp = dep->de_pmp;
278: struct vattr *vap = ap->a_vap;
279: mode_t mode;
280: u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1.19 christos 281: ino_t fileid;
1.1 jdolecek 282:
1.20 christos 283: DETIMES(dep, NULL, NULL, NULL, pmp->pm_gmtoff);
1.1 jdolecek 284: vap->va_fsid = dep->de_dev;
285: /*
286: * The following computation of the fileid must be the same as that
287: * used in msdosfs_readdir() to compute d_fileno. If not, pwd
288: * doesn't work.
289: */
290: if (dep->de_Attributes & ATTR_DIRECTORY) {
1.19 christos 291: fileid = cntobn(pmp, (ino_t)dep->de_StartCluster) * dirsperblk;
1.1 jdolecek 292: if (dep->de_StartCluster == MSDOSFSROOT)
293: fileid = 1;
294: } else {
1.19 christos 295: fileid = cntobn(pmp, (ino_t)dep->de_dirclust) * dirsperblk;
1.1 jdolecek 296: if (dep->de_dirclust == MSDOSFSROOT)
297: fileid = roottobn(pmp, 0) * dirsperblk;
298: fileid += dep->de_diroffset / sizeof(struct direntry);
299: }
300: vap->va_fileid = fileid;
301: if ((dep->de_Attributes & ATTR_READONLY) == 0)
302: mode = S_IRWXU|S_IRWXG|S_IRWXO;
303: else
304: mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
1.6 jdolecek 305: vap->va_mode =
306: mode & (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
1.1 jdolecek 307: vap->va_uid = pmp->pm_uid;
308: vap->va_gid = pmp->pm_gid;
309: vap->va_nlink = 1;
310: vap->va_rdev = 0;
311: vap->va_size = ap->a_vp->v_size;
1.7 itojun 312: dos2unixtime(dep->de_MDate, dep->de_MTime, 0, pmp->pm_gmtoff,
313: &vap->va_mtime);
1.1 jdolecek 314: if (dep->de_pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
1.7 itojun 315: dos2unixtime(dep->de_ADate, 0, 0, pmp->pm_gmtoff,
316: &vap->va_atime);
317: dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun,
318: pmp->pm_gmtoff, &vap->va_ctime);
1.1 jdolecek 319: } else {
320: vap->va_atime = vap->va_mtime;
321: vap->va_ctime = vap->va_mtime;
322: }
323: vap->va_flags = 0;
324: if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
325: vap->va_mode |= S_ARCH1;
326: vap->va_gen = 0;
327: vap->va_blocksize = pmp->pm_bpcluster;
328: vap->va_bytes =
329: (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
330: vap->va_type = ap->a_vp->v_type;
331: return (0);
332: }
333:
334: int
335: msdosfs_setattr(v)
336: void *v;
337: {
338: struct vop_setattr_args /* {
339: struct vnode *a_vp;
340: struct vattr *a_vap;
1.28 elad 341: kauth_cred_t a_cred;
1.24 christos 342: struct lwp *a_l;
1.1 jdolecek 343: } */ *ap = v;
344: int error = 0, de_changed = 0;
345: struct denode *dep = VTODE(ap->a_vp);
346: struct msdosfsmount *pmp = dep->de_pmp;
347: struct vnode *vp = ap->a_vp;
348: struct vattr *vap = ap->a_vap;
1.28 elad 349: kauth_cred_t cred = ap->a_cred;
1.1 jdolecek 350:
351: #ifdef MSDOSFS_DEBUG
352: printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
1.24 christos 353: ap->a_vp, vap, cred, ap->a_l);
1.1 jdolecek 354: #endif
355: /*
356: * Note we silently ignore uid or gid changes.
357: */
358: if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) ||
359: (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
360: (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
361: (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL) ||
1.14 perry 362: (vap->va_uid != VNOVAL && vap->va_uid != pmp->pm_uid) ||
1.1 jdolecek 363: (vap->va_gid != VNOVAL && vap->va_gid != pmp->pm_gid)) {
364: #ifdef MSDOSFS_DEBUG
365: printf("msdosfs_setattr(): returning EINVAL\n");
1.16 christos 366: printf(" va_type %d, va_nlink %x, va_fsid %lx, va_fileid %llx\n",
367: vap->va_type, vap->va_nlink, vap->va_fsid,
368: (unsigned long long)vap->va_fileid);
1.1 jdolecek 369: printf(" va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
370: vap->va_blocksize, vap->va_rdev, (long long)vap->va_bytes, vap->va_gen);
371: #endif
372: return (EINVAL);
373: }
374: /*
375: * Silently ignore attributes modifications on directories.
376: */
377: if (ap->a_vp->v_type == VDIR)
378: return 0;
379:
380: if (vap->va_size != VNOVAL) {
381: if (vp->v_mount->mnt_flag & MNT_RDONLY)
382: return (EROFS);
1.24 christos 383: error = detrunc(dep, (u_long)vap->va_size, 0, cred, ap->a_l);
1.1 jdolecek 384: if (error)
385: return (error);
386: de_changed = 1;
387: }
388: if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
389: if (vp->v_mount->mnt_flag & MNT_RDONLY)
390: return (EROFS);
1.28 elad 391: if (kauth_cred_geteuid(cred) != pmp->pm_uid &&
392: (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
1.36 elad 393: NULL)) &&
1.1 jdolecek 394: ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1.24 christos 395: (error = VOP_ACCESS(ap->a_vp, VWRITE, cred, ap->a_l))))
1.1 jdolecek 396: return (error);
397: if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
398: vap->va_atime.tv_sec != VNOVAL)
1.7 itojun 399: unix2dostime(&vap->va_atime, pmp->pm_gmtoff, &dep->de_ADate, NULL, NULL);
1.1 jdolecek 400: if (vap->va_mtime.tv_sec != VNOVAL)
1.7 itojun 401: unix2dostime(&vap->va_mtime, pmp->pm_gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
1.1 jdolecek 402: dep->de_Attributes |= ATTR_ARCHIVE;
403: dep->de_flag |= DE_MODIFIED;
404: de_changed = 1;
405: }
406: /*
407: * DOS files only have the ability to have their writability
408: * attribute set, so we use the owner write bit to set the readonly
409: * attribute.
410: */
411: if (vap->va_mode != (mode_t)VNOVAL) {
412: if (vp->v_mount->mnt_flag & MNT_RDONLY)
413: return (EROFS);
1.28 elad 414: if (kauth_cred_geteuid(cred) != pmp->pm_uid &&
415: (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
1.36 elad 416: NULL)))
1.1 jdolecek 417: return (error);
418: /* We ignore the read and execute bits. */
419: if (vap->va_mode & S_IWUSR)
420: dep->de_Attributes &= ~ATTR_READONLY;
421: else
422: dep->de_Attributes |= ATTR_READONLY;
423: dep->de_flag |= DE_MODIFIED;
424: de_changed = 1;
425: }
426: /*
427: * Allow the `archived' bit to be toggled.
428: */
429: if (vap->va_flags != VNOVAL) {
430: if (vp->v_mount->mnt_flag & MNT_RDONLY)
431: return (EROFS);
1.28 elad 432: if (kauth_cred_geteuid(cred) != pmp->pm_uid &&
433: (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
1.36 elad 434: NULL)))
1.1 jdolecek 435: return (error);
436: if (vap->va_flags & SF_ARCHIVED)
437: dep->de_Attributes &= ~ATTR_ARCHIVE;
438: else
439: dep->de_Attributes |= ATTR_ARCHIVE;
440: dep->de_flag |= DE_MODIFIED;
441: de_changed = 1;
442: }
443:
444: if (de_changed) {
445: VN_KNOTE(vp, NOTE_ATTRIB);
446: return (deupdat(dep, 1));
447: } else
448: return (0);
449: }
450:
451: int
452: msdosfs_read(v)
453: void *v;
454: {
455: struct vop_read_args /* {
456: struct vnode *a_vp;
457: struct uio *a_uio;
458: int a_ioflag;
1.28 elad 459: kauth_cred_t a_cred;
1.1 jdolecek 460: } */ *ap = v;
1.13 chs 461: int error = 0, flags;
1.1 jdolecek 462: int64_t diff;
463: int blsize;
464: long n;
465: long on;
466: daddr_t lbn;
467: void *win;
468: vsize_t bytelen;
469: struct buf *bp;
470: struct vnode *vp = ap->a_vp;
471: struct denode *dep = VTODE(vp);
472: struct msdosfsmount *pmp = dep->de_pmp;
473: struct uio *uio = ap->a_uio;
474:
475: /*
476: * If they didn't ask for any data, then we are done.
477: */
478:
479: if (uio->uio_resid == 0)
480: return (0);
481: if (uio->uio_offset < 0)
482: return (EINVAL);
483: if (uio->uio_offset >= dep->de_FileSize)
484: return (0);
485:
486: if (vp->v_type == VREG) {
1.23 yamt 487: const int advice = IO_ADV_DECODE(ap->a_ioflag);
488:
1.1 jdolecek 489: while (uio->uio_resid > 0) {
490: bytelen = MIN(dep->de_FileSize - uio->uio_offset,
491: uio->uio_resid);
492:
493: if (bytelen == 0)
494: break;
495: win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
1.23 yamt 496: &bytelen, advice, UBC_READ);
1.1 jdolecek 497: error = uiomove(win, bytelen, uio);
1.13 chs 498: flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
499: ubc_release(win, flags);
1.1 jdolecek 500: if (error)
501: break;
502: }
503: dep->de_flag |= DE_ACCESS;
504: goto out;
505: }
506:
507: /* this loop is only for directories now */
508: do {
509: lbn = de_cluster(pmp, uio->uio_offset);
510: on = uio->uio_offset & pmp->pm_crbomask;
511: n = MIN(pmp->pm_bpcluster - on, uio->uio_resid);
512: if (uio->uio_offset >= dep->de_FileSize)
513: return (0);
514: /* file size (and hence diff) may be up to 4GB */
515: diff = dep->de_FileSize - uio->uio_offset;
516: if (diff < n)
517: n = (long) diff;
518:
1.34 scw 519: /* convert cluster # to sector # */
1.1 jdolecek 520: error = pcbmap(dep, lbn, &lbn, 0, &blsize);
521: if (error)
522: return (error);
523:
524: /*
525: * If we are operating on a directory file then be sure to
526: * do i/o with the vnode for the filesystem instead of the
527: * vnode for the directory.
528: */
1.34 scw 529: error = bread(pmp->pm_devvp, de_bn2kb(pmp, lbn), blsize,
530: NOCRED, &bp);
1.1 jdolecek 531: n = MIN(n, pmp->pm_bpcluster - bp->b_resid);
532: if (error) {
1.38.2.2 ad 533: brelse(bp, 0);
1.1 jdolecek 534: return (error);
535: }
1.38 christos 536: error = uiomove((char *)bp->b_data + on, (int) n, uio);
1.38.2.2 ad 537: brelse(bp, 0);
1.1 jdolecek 538: } while (error == 0 && uio->uio_resid > 0 && n != 0);
1.14 perry 539:
1.1 jdolecek 540: out:
541: if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
542: error = deupdat(dep, 1);
543: return (error);
544: }
545:
546: /*
547: * Write data to a file or directory.
548: */
549: int
550: msdosfs_write(v)
551: void *v;
552: {
553: struct vop_write_args /* {
554: struct vnode *a_vp;
555: struct uio *a_uio;
556: int a_ioflag;
1.28 elad 557: kauth_cred_t a_cred;
1.1 jdolecek 558: } */ *ap = v;
1.13 chs 559: int resid, flags, extended = 0;
560: int error = 0;
561: int ioflag = ap->a_ioflag;
1.1 jdolecek 562: u_long osize;
563: u_long count;
564: void *win;
565: vsize_t bytelen;
566: off_t oldoff;
567: struct uio *uio = ap->a_uio;
1.26 yamt 568: struct proc *p = curproc;
1.1 jdolecek 569: struct vnode *vp = ap->a_vp;
570: struct denode *dep = VTODE(vp);
571: struct msdosfsmount *pmp = dep->de_pmp;
1.28 elad 572: kauth_cred_t cred = ap->a_cred;
1.37 thorpej 573: bool async;
1.1 jdolecek 574:
575: #ifdef MSDOSFS_DEBUG
576: printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
577: vp, uio, ioflag, cred);
578: printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
579: dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
580: #endif
581:
582: switch (vp->v_type) {
583: case VREG:
584: if (ioflag & IO_APPEND)
585: uio->uio_offset = dep->de_FileSize;
586: break;
587: case VDIR:
588: return EISDIR;
589: default:
590: panic("msdosfs_write(): bad file type");
591: }
592:
593: if (uio->uio_offset < 0)
594: return (EINVAL);
595:
596: if (uio->uio_resid == 0)
597: return (0);
598:
1.17 xtraeme 599: /* Don't bother to try to write files larger than the fs limit */
600: if (uio->uio_offset + uio->uio_resid > MSDOSFS_FILESIZE_MAX)
601: return (EFBIG);
602:
1.1 jdolecek 603: /*
604: * If they've exceeded their filesize limit, tell them about it.
605: */
1.26 yamt 606: if (((uio->uio_offset + uio->uio_resid) >
1.1 jdolecek 607: p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
1.38.2.3 ad 608: mutex_enter(&proclist_mutex);
1.1 jdolecek 609: psignal(p, SIGXFSZ);
1.38.2.3 ad 610: mutex_exit(&proclist_mutex);
1.1 jdolecek 611: return (EFBIG);
612: }
613:
614: /*
615: * If the offset we are starting the write at is beyond the end of
616: * the file, then they've done a seek. Unix filesystems allow
617: * files with holes in them, DOS doesn't so we must fill the hole
618: * with zeroed blocks.
619: */
620: if (uio->uio_offset > dep->de_FileSize) {
621: if ((error = deextend(dep, uio->uio_offset, cred)) != 0)
622: return (error);
623: }
624:
625: /*
626: * Remember some values in case the write fails.
627: */
628: async = vp->v_mount->mnt_flag & MNT_ASYNC;
629: resid = uio->uio_resid;
630: osize = dep->de_FileSize;
631:
632: /*
633: * If we write beyond the end of the file, extend it to its ultimate
634: * size ahead of the time to hopefully get a contiguous area.
635: */
636: if (uio->uio_offset + resid > osize) {
637: count = de_clcount(pmp, uio->uio_offset + resid) -
638: de_clcount(pmp, osize);
639: if ((error = extendfile(dep, count, NULL, NULL, 0)) &&
640: (error != ENOSPC || (ioflag & IO_UNIT)))
641: goto errexit;
642:
643: dep->de_FileSize = uio->uio_offset + resid;
644: uvm_vnp_setsize(vp, dep->de_FileSize);
645: extended = 1;
646: }
647:
648: do {
649: oldoff = uio->uio_offset;
650: bytelen = uio->uio_resid;
651:
1.23 yamt 652: win = ubc_alloc(&vp->v_uobj, oldoff, &bytelen, UVM_ADV_NORMAL,
653: UBC_WRITE);
1.1 jdolecek 654: error = uiomove(win, bytelen, uio);
1.13 chs 655: flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
656: ubc_release(win, flags);
1.34 scw 657: if (error)
1.1 jdolecek 658: break;
659:
660: /*
661: * flush what we just wrote if necessary.
662: * XXXUBC simplistic async flushing.
663: */
664:
665: if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
1.38.2.1 ad 666: mutex_enter(&vp->v_interlock);
1.1 jdolecek 667: error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
668: (uio->uio_offset >> 16) << 16, PGO_CLEANIT);
669: }
670: } while (error == 0 && uio->uio_resid > 0);
671: if (error == 0 && ioflag & IO_SYNC) {
1.38.2.1 ad 672: mutex_enter(&vp->v_interlock);
1.1 jdolecek 673: error = VOP_PUTPAGES(vp, trunc_page(oldoff),
674: round_page(oldoff + bytelen), PGO_CLEANIT | PGO_SYNCIO);
675: }
676: dep->de_flag |= DE_UPDATE;
677:
678: /*
679: * If the write failed and they want us to, truncate the file back
680: * to the size it was before the write was attempted.
681: */
682: errexit:
683: if (resid > uio->uio_resid)
684: VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
685: if (error) {
686: detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
687: uio->uio_offset -= resid - uio->uio_resid;
688: uio->uio_resid = resid;
689: } else if ((ioflag & IO_SYNC) == IO_SYNC)
690: error = deupdat(dep, 1);
691: KASSERT(vp->v_size == dep->de_FileSize);
692: return (error);
693: }
694:
695: int
1.21 yamt 696: msdosfs_update(struct vnode *vp, const struct timespec *acc,
697: const struct timespec *mod, int flags)
1.1 jdolecek 698: {
699: struct buf *bp;
700: struct direntry *dirp;
701: struct denode *dep;
702: int error;
703:
1.21 yamt 704: if (vp->v_mount->mnt_flag & MNT_RDONLY)
1.1 jdolecek 705: return (0);
1.21 yamt 706: dep = VTODE(vp);
707: DETIMES(dep, acc, mod, NULL, dep->de_pmp->pm_gmtoff);
1.1 jdolecek 708: if ((dep->de_flag & DE_MODIFIED) == 0)
709: return (0);
710: dep->de_flag &= ~DE_MODIFIED;
711: if (dep->de_Attributes & ATTR_DIRECTORY)
712: return (0);
713: if (dep->de_refcnt <= 0)
714: return (0);
715: error = readde(dep, &bp, &dirp);
716: if (error)
717: return (error);
718: DE_EXTERNALIZE(dirp, dep);
1.21 yamt 719: if (flags & (UPDATE_WAIT|UPDATE_DIROP))
1.1 jdolecek 720: return (bwrite(bp));
721: else {
722: bdwrite(bp);
723: return (0);
724: }
725: }
726:
727: /*
728: * Flush the blocks of a file to disk.
729: *
730: * This function is worthless for vnodes that represent directories. Maybe we
731: * could just do a sync if they try an fsync on a directory file.
732: */
733: int
734: msdosfs_remove(v)
735: void *v;
736: {
737: struct vop_remove_args /* {
738: struct vnode *a_dvp;
739: struct vnode *a_vp;
740: struct componentname *a_cnp;
741: } */ *ap = v;
742: struct denode *dep = VTODE(ap->a_vp);
743: struct denode *ddep = VTODE(ap->a_dvp);
744: int error;
745:
746: if (ap->a_vp->v_type == VDIR)
747: error = EPERM;
748: else
749: error = removede(ddep, dep);
750: #ifdef MSDOSFS_DEBUG
751: printf("msdosfs_remove(), dep %p, v_usecount %d\n",
752: dep, ap->a_vp->v_usecount);
753: #endif
754: VN_KNOTE(ap->a_vp, NOTE_DELETE);
755: VN_KNOTE(ap->a_dvp, NOTE_WRITE);
756: if (ddep == dep)
757: vrele(ap->a_vp);
758: else
759: vput(ap->a_vp); /* causes msdosfs_inactive() to be called
760: * via vrele() */
761: vput(ap->a_dvp);
762: return (error);
763: }
764:
765: /*
766: * DOS filesystems don't know what links are. But since we already called
767: * msdosfs_lookup() with create and lockparent, the parent is locked so we
768: * have to free it before we return the error.
769: */
770: int
771: msdosfs_link(v)
772: void *v;
773: {
774: struct vop_link_args /* {
775: struct vnode *a_dvp;
776: struct vnode *a_vp;
777: struct componentname *a_cnp;
778: } */ *ap = v;
779:
780: VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
781: vput(ap->a_dvp);
782: return (EOPNOTSUPP);
783: }
784:
785: /*
786: * Renames on files require moving the denode to a new hash queue since the
787: * denode's location is used to compute which hash queue to put the file
788: * in. Unless it is a rename in place. For example "mv a b".
789: *
790: * What follows is the basic algorithm:
791: *
792: * if (file move) {
793: * if (dest file exists) {
794: * remove dest file
795: * }
796: * if (dest and src in same directory) {
797: * rewrite name in existing directory slot
798: * } else {
799: * write new entry in dest directory
800: * update offset and dirclust in denode
801: * move denode to new hash chain
802: * clear old directory entry
803: * }
804: * } else {
805: * directory move
806: * if (dest directory exists) {
807: * if (dest is not empty) {
808: * return ENOTEMPTY
809: * }
810: * remove dest directory
811: * }
812: * if (dest and src in same directory) {
813: * rewrite name in existing entry
814: * } else {
815: * be sure dest is not a child of src directory
816: * write entry in dest directory
817: * update "." and ".." in moved directory
818: * update offset and dirclust in denode
819: * move denode to new hash chain
820: * clear old directory entry for moved directory
821: * }
822: * }
823: *
824: * On entry:
825: * source's parent directory is unlocked
826: * source file or directory is unlocked
827: * destination's parent directory is locked
828: * destination file or directory is locked if it exists
829: *
830: * On exit:
831: * all denodes should be released
832: *
833: * Notes:
834: * I'm not sure how the memory containing the pathnames pointed at by the
835: * componentname structures is freed, there may be some memory bleeding
836: * for each rename done.
837: */
838: int
839: msdosfs_rename(v)
840: void *v;
841: {
842: struct vop_rename_args /* {
843: struct vnode *a_fdvp;
844: struct vnode *a_fvp;
845: struct componentname *a_fcnp;
846: struct vnode *a_tdvp;
847: struct vnode *a_tvp;
848: struct componentname *a_tcnp;
849: } */ *ap = v;
850: struct vnode *tvp = ap->a_tvp;
851: struct vnode *tdvp = ap->a_tdvp;
852: struct vnode *fvp = ap->a_fvp;
853: struct vnode *fdvp = ap->a_fdvp;
854: struct componentname *tcnp = ap->a_tcnp;
855: struct componentname *fcnp = ap->a_fcnp;
1.24 christos 856: struct lwp *l = tcnp->cn_lwp;
1.1 jdolecek 857: struct denode *ip, *xp, *dp, *zp;
858: u_char toname[11], oldname[11];
859: u_long from_diroffset, to_diroffset;
860: u_char to_count;
861: int doingdirectory = 0, newparent = 0;
862: int error;
863: u_long cn;
864: daddr_t bn;
865: struct msdosfsmount *pmp;
866: struct direntry *dotdotp;
867: struct buf *bp;
868: int fdvp_dorele = 0;
869:
870: pmp = VFSTOMSDOSFS(fdvp->v_mount);
871:
872: #ifdef DIAGNOSTIC
873: if ((tcnp->cn_flags & HASBUF) == 0 ||
874: (fcnp->cn_flags & HASBUF) == 0)
875: panic("msdosfs_rename: no name");
876: #endif
877: /*
878: * Check for cross-device rename.
879: */
880: if ((fvp->v_mount != tdvp->v_mount) ||
881: (tvp && (fvp->v_mount != tvp->v_mount))) {
882: error = EXDEV;
883: abortit:
884: VOP_ABORTOP(tdvp, tcnp);
885: if (tdvp == tvp)
886: vrele(tdvp);
887: else
888: vput(tdvp);
889: if (tvp)
890: vput(tvp);
891: VOP_ABORTOP(fdvp, fcnp);
892: vrele(fdvp);
893: vrele(fvp);
894: return (error);
895: }
896:
897: /*
898: * If source and dest are the same, do nothing.
899: */
900: if (tvp == fvp) {
901: error = 0;
902: goto abortit;
903: }
904:
905: /* */
906: if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
907: goto abortit;
908: dp = VTODE(fdvp);
909: ip = VTODE(fvp);
910:
911: /*
912: * Be sure we are not renaming ".", "..", or an alias of ".". This
913: * leads to a crippled directory tree. It's pretty tough to do a
914: * "ls" or "pwd" with the "." directory entry missing, and "cd .."
915: * doesn't work if the ".." entry is missing.
916: */
917: if (ip->de_Attributes & ATTR_DIRECTORY) {
918: /*
919: * Avoid ".", "..", and aliases of "." for obvious reasons.
920: */
921: if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
922: dp == ip ||
923: (fcnp->cn_flags & ISDOTDOT) ||
924: (tcnp->cn_flags & ISDOTDOT) ||
925: (ip->de_flag & DE_RENAME)) {
926: VOP_UNLOCK(fvp, 0);
927: error = EINVAL;
928: goto abortit;
929: }
930: ip->de_flag |= DE_RENAME;
931: doingdirectory++;
932: }
933: VN_KNOTE(fdvp, NOTE_WRITE); /* XXXLUKEM/XXX: right place? */
934:
935: /*
936: * When the target exists, both the directory
937: * and target vnodes are returned locked.
938: */
939: dp = VTODE(tdvp);
940: xp = tvp ? VTODE(tvp) : NULL;
941: /*
942: * Remember direntry place to use for destination
943: */
944: to_diroffset = dp->de_fndoffset;
945: to_count = dp->de_fndcnt;
946:
947: /*
948: * If ".." must be changed (ie the directory gets a new
949: * parent) then the source directory must not be in the
950: * directory hierarchy above the target, as this would
951: * orphan everything below the source directory. Also
952: * the user must have write permission in the source so
953: * as to be able to change "..". We must repeat the call
954: * to namei, as the parent directory is unlocked by the
955: * call to doscheckpath().
956: */
1.24 christos 957: error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, l);
1.1 jdolecek 958: VOP_UNLOCK(fvp, 0);
959: if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
960: newparent = 1;
961: vrele(fdvp);
962: if (doingdirectory && newparent) {
963: if (error) /* write access check above */
964: goto bad;
965: if (xp != NULL)
966: vput(tvp);
967: /*
968: * doscheckpath() vput()'s dp,
969: * so we have to do a relookup afterwards
970: */
971: if ((error = doscheckpath(ip, dp)) != 0)
972: goto out;
973: if ((tcnp->cn_flags & SAVESTART) == 0)
974: panic("msdosfs_rename: lost to startdir");
975: if ((error = relookup(tdvp, &tvp, tcnp)) != 0)
976: goto out;
977: dp = VTODE(tdvp);
978: xp = tvp ? VTODE(tvp) : NULL;
979: }
980:
981: if (xp != NULL) {
982: /*
983: * Target must be empty if a directory and have no links
984: * to it. Also, ensure source and target are compatible
985: * (both directories, or both not directories).
986: */
987: if (xp->de_Attributes & ATTR_DIRECTORY) {
988: if (!dosdirempty(xp)) {
989: error = ENOTEMPTY;
990: goto bad;
991: }
992: if (!doingdirectory) {
993: error = ENOTDIR;
994: goto bad;
995: }
996: } else if (doingdirectory) {
997: error = EISDIR;
998: goto bad;
999: }
1000: if ((error = removede(dp, xp)) != 0)
1001: goto bad;
1002: VN_KNOTE(tdvp, NOTE_WRITE);
1003: VN_KNOTE(tvp, NOTE_DELETE);
1004: cache_purge(tvp);
1005: vput(tvp);
1006: xp = NULL;
1007: }
1008:
1009: /*
1010: * Convert the filename in tcnp into a dos filename. We copy this
1011: * into the denode and directory entry for the destination
1012: * file/directory.
1013: */
1014: if ((error = uniqdosname(VTODE(tdvp), tcnp, toname)) != 0)
1015: goto abortit;
1016:
1017: /*
1018: * Since from wasn't locked at various places above,
1019: * have to do a relookup here.
1020: */
1021: fcnp->cn_flags &= ~MODMASK;
1022: fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1023: if ((fcnp->cn_flags & SAVESTART) == 0)
1024: panic("msdosfs_rename: lost from startdir");
1025: if (!newparent)
1026: VOP_UNLOCK(tdvp, 0);
1.35 chs 1027: vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
1028: if ((error = relookup(fdvp, &fvp, fcnp))) {
1029: vput(fdvp);
1030: vrele(ap->a_fvp);
1031: vrele(tdvp);
1032: return (error);
1033: }
1.1 jdolecek 1034: if (fvp == NULL) {
1035: /*
1036: * From name has disappeared.
1037: */
1038: if (doingdirectory)
1039: panic("rename: lost dir entry");
1.35 chs 1040: vput(fdvp);
1.1 jdolecek 1041: vrele(ap->a_fvp);
1042: vrele(tdvp);
1043: return 0;
1044: }
1045: fdvp_dorele = 1;
1046: xp = VTODE(fvp);
1047: zp = VTODE(fdvp);
1048: from_diroffset = zp->de_fndoffset;
1049:
1050: /*
1051: * Ensure that the directory entry still exists and has not
1052: * changed till now. If the source is a file the entry may
1053: * have been unlinked or renamed. In either case there is
1054: * no further work to be done. If the source is a directory
1055: * then it cannot have been rmdir'ed or renamed; this is
1056: * prohibited by the DE_RENAME flag.
1057: */
1058: if (xp != ip) {
1059: if (doingdirectory)
1060: panic("rename: lost dir entry");
1061: vrele(ap->a_fvp);
1062: VOP_UNLOCK(fvp, 0);
1063: if (newparent)
1064: VOP_UNLOCK(fdvp, 0);
1065: xp = NULL;
1066: } else {
1067: vrele(fvp);
1068: xp = NULL;
1069:
1070: /*
1071: * First write a new entry in the destination
1072: * directory and mark the entry in the source directory
1073: * as deleted. Then move the denode to the correct hash
1074: * chain for its new location in the filesystem. And, if
1075: * we moved a directory, then update its .. entry to point
1076: * to the new parent directory.
1077: */
1078: memcpy(oldname, ip->de_Name, 11);
1079: memcpy(ip->de_Name, toname, 11); /* update denode */
1080: dp->de_fndoffset = to_diroffset;
1081: dp->de_fndcnt = to_count;
1082: error = createde(ip, dp, (struct denode **)0, tcnp);
1083: if (error) {
1084: memcpy(ip->de_Name, oldname, 11);
1085: if (newparent)
1086: VOP_UNLOCK(fdvp, 0);
1087: VOP_UNLOCK(fvp, 0);
1088: goto bad;
1089: }
1090: ip->de_refcnt++;
1091: zp->de_fndoffset = from_diroffset;
1092: if ((error = removede(zp, ip)) != 0) {
1093: /* XXX should really panic here, fs is corrupt */
1094: if (newparent)
1095: VOP_UNLOCK(fdvp, 0);
1096: VOP_UNLOCK(fvp, 0);
1097: goto bad;
1098: }
1099: cache_purge(fvp);
1100: if (!doingdirectory) {
1101: error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1102: &ip->de_dirclust, 0);
1103: if (error) {
1104: /* XXX should really panic here, fs is corrupt */
1105: if (newparent)
1106: VOP_UNLOCK(fdvp, 0);
1107: VOP_UNLOCK(fvp, 0);
1108: goto bad;
1109: }
1110: ip->de_diroffset = to_diroffset;
1111: if (ip->de_dirclust != MSDOSFSROOT)
1112: ip->de_diroffset &= pmp->pm_crbomask;
1113: }
1114: reinsert(ip);
1115: if (newparent)
1116: VOP_UNLOCK(fdvp, 0);
1117: }
1118:
1119: /*
1120: * If we moved a directory to a new parent directory, then we must
1121: * fixup the ".." entry in the moved directory.
1122: */
1123: if (doingdirectory && newparent) {
1124: cn = ip->de_StartCluster;
1125: if (cn == MSDOSFSROOT) {
1126: /* this should never happen */
1127: panic("msdosfs_rename: updating .. in root directory?");
1128: } else
1129: bn = cntobn(pmp, cn);
1.34 scw 1130: error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
1131: pmp->pm_bpcluster, NOCRED, &bp);
1.1 jdolecek 1132: if (error) {
1133: /* XXX should really panic here, fs is corrupt */
1.38.2.2 ad 1134: brelse(bp, 0);
1.1 jdolecek 1135: VOP_UNLOCK(fvp, 0);
1136: goto bad;
1137: }
1138: dotdotp = (struct direntry *)bp->b_data + 1;
1139: putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1140: if (FAT32(pmp)) {
1141: putushort(dotdotp->deHighClust,
1142: dp->de_StartCluster >> 16);
1.22 christos 1143: } else {
1144: putushort(dotdotp->deHighClust, 0);
1.1 jdolecek 1145: }
1146: if ((error = bwrite(bp)) != 0) {
1147: /* XXX should really panic here, fs is corrupt */
1148: VOP_UNLOCK(fvp, 0);
1149: goto bad;
1150: }
1151: }
1152:
1153: VN_KNOTE(fvp, NOTE_RENAME);
1154: VOP_UNLOCK(fvp, 0);
1155: bad:
1156: if (xp)
1157: vput(tvp);
1158: vput(tdvp);
1159: out:
1160: ip->de_flag &= ~DE_RENAME;
1161: if (fdvp_dorele)
1162: vrele(fdvp);
1163: vrele(fvp);
1164: return (error);
1165:
1166: }
1167:
1.2 jdolecek 1168: static const struct {
1.1 jdolecek 1169: struct direntry dot;
1170: struct direntry dotdot;
1171: } dosdirtemplate = {
1172: { ". ", " ", /* the . entry */
1173: ATTR_DIRECTORY, /* file attribute */
1174: 0, /* reserved */
1175: 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1176: { 0, 0 }, /* access date */
1177: { 0, 0 }, /* high bits of start cluster */
1178: { 210, 4 }, { 210, 4 }, /* modify time & date */
1179: { 0, 0 }, /* startcluster */
1180: { 0, 0, 0, 0 } /* filesize */
1181: },
1182: { ".. ", " ", /* the .. entry */
1183: ATTR_DIRECTORY, /* file attribute */
1184: 0, /* reserved */
1185: 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1186: { 0, 0 }, /* access date */
1187: { 0, 0 }, /* high bits of start cluster */
1188: { 210, 4 }, { 210, 4 }, /* modify time & date */
1189: { 0, 0 }, /* startcluster */
1190: { 0, 0, 0, 0 } /* filesize */
1191: }
1192: };
1193:
1194: int
1195: msdosfs_mkdir(v)
1196: void *v;
1197: {
1198: struct vop_mkdir_args /* {
1199: struct vnode *a_dvp;
1200: struvt vnode **a_vpp;
1201: struvt componentname *a_cnp;
1202: struct vattr *a_vap;
1203: } */ *ap = v;
1204: struct componentname *cnp = ap->a_cnp;
1205: struct denode ndirent;
1206: struct denode *dep;
1207: struct denode *pdep = VTODE(ap->a_dvp);
1208: int error;
1209: int bn;
1210: u_long newcluster, pcl;
1211: struct direntry *denp;
1212: struct msdosfsmount *pmp = pdep->de_pmp;
1213: struct buf *bp;
1214: int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
1215:
1216: /*
1217: * If this is the root directory and there is no space left we
1218: * can't do anything. This is because the root directory can not
1219: * change size.
1220: */
1221: if (pdep->de_StartCluster == MSDOSFSROOT
1222: && pdep->de_fndoffset >= pdep->de_FileSize) {
1223: error = ENOSPC;
1224: goto bad2;
1225: }
1226:
1227: /*
1228: * Allocate a cluster to hold the about to be created directory.
1229: */
1230: error = clusteralloc(pmp, 0, 1, &newcluster, NULL);
1231: if (error)
1232: goto bad2;
1233:
1234: memset(&ndirent, 0, sizeof(ndirent));
1235: ndirent.de_pmp = pmp;
1236: ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1.20 christos 1237: DETIMES(&ndirent, NULL, NULL, NULL, pmp->pm_gmtoff);
1.1 jdolecek 1238:
1239: /*
1240: * Now fill the cluster with the "." and ".." entries. And write
1241: * the cluster to disk. This way it is there for the parent
1242: * directory to be pointing at if there were a crash.
1243: */
1244: bn = cntobn(pmp, newcluster);
1245: /* always succeeds */
1.34 scw 1246: bp = getblk(pmp->pm_devvp, de_bn2kb(pmp, bn), pmp->pm_bpcluster, 0, 0);
1.1 jdolecek 1247: memset(bp->b_data, 0, pmp->pm_bpcluster);
1248: memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
1249: denp = (struct direntry *)bp->b_data;
1250: putushort(denp[0].deStartCluster, newcluster);
1251: putushort(denp[0].deCDate, ndirent.de_CDate);
1252: putushort(denp[0].deCTime, ndirent.de_CTime);
1253: denp[0].deCHundredth = ndirent.de_CHun;
1254: putushort(denp[0].deADate, ndirent.de_ADate);
1255: putushort(denp[0].deMDate, ndirent.de_MDate);
1256: putushort(denp[0].deMTime, ndirent.de_MTime);
1257: pcl = pdep->de_StartCluster;
1258: if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1259: pcl = 0;
1260: putushort(denp[1].deStartCluster, pcl);
1261: putushort(denp[1].deCDate, ndirent.de_CDate);
1262: putushort(denp[1].deCTime, ndirent.de_CTime);
1263: denp[1].deCHundredth = ndirent.de_CHun;
1264: putushort(denp[1].deADate, ndirent.de_ADate);
1265: putushort(denp[1].deMDate, ndirent.de_MDate);
1266: putushort(denp[1].deMTime, ndirent.de_MTime);
1267: if (FAT32(pmp)) {
1268: putushort(denp[0].deHighClust, newcluster >> 16);
1269: putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1.22 christos 1270: } else {
1271: putushort(denp[0].deHighClust, 0);
1272: putushort(denp[1].deHighClust, 0);
1.1 jdolecek 1273: }
1274:
1275: if (async)
1276: bdwrite(bp);
1277: else if ((error = bwrite(bp)) != 0)
1278: goto bad;
1279:
1280: /*
1281: * Now build up a directory entry pointing to the newly allocated
1282: * cluster. This will be written to an empty slot in the parent
1283: * directory.
1284: */
1285: #ifdef DIAGNOSTIC
1286: if ((cnp->cn_flags & HASBUF) == 0)
1287: panic("msdosfs_mkdir: no name");
1288: #endif
1289: if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
1290: goto bad;
1291:
1292: ndirent.de_Attributes = ATTR_DIRECTORY;
1293: ndirent.de_StartCluster = newcluster;
1294: ndirent.de_FileSize = 0;
1295: ndirent.de_dev = pdep->de_dev;
1296: ndirent.de_devvp = pdep->de_devvp;
1297: if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
1298: goto bad;
1299: if ((cnp->cn_flags & SAVESTART) == 0)
1300: PNBUF_PUT(cnp->cn_pnbuf);
1301: VN_KNOTE(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1302: vput(ap->a_dvp);
1303: *ap->a_vpp = DETOV(dep);
1304: return (0);
1305:
1306: bad:
1307: clusterfree(pmp, newcluster, NULL);
1308: bad2:
1309: PNBUF_PUT(cnp->cn_pnbuf);
1310: vput(ap->a_dvp);
1311: return (error);
1312: }
1313:
1314: int
1315: msdosfs_rmdir(v)
1316: void *v;
1317: {
1318: struct vop_rmdir_args /* {
1319: struct vnode *a_dvp;
1320: struct vnode *a_vp;
1321: struct componentname *a_cnp;
1322: } */ *ap = v;
1323: struct vnode *vp = ap->a_vp;
1324: struct vnode *dvp = ap->a_dvp;
1325: struct componentname *cnp = ap->a_cnp;
1326: struct denode *ip, *dp;
1327: int error;
1328:
1329: ip = VTODE(vp);
1330: dp = VTODE(dvp);
1331: /*
1332: * No rmdir "." please.
1333: */
1334: if (dp == ip) {
1335: vrele(dvp);
1336: vput(vp);
1337: return (EINVAL);
1338: }
1339: /*
1340: * Verify the directory is empty (and valid).
1341: * (Rmdir ".." won't be valid since
1342: * ".." will contain a reference to
1343: * the current directory and thus be
1344: * non-empty.)
1345: */
1346: error = 0;
1347: if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1348: error = ENOTEMPTY;
1349: goto out;
1350: }
1351: /*
1352: * Delete the entry from the directory. For dos filesystems this
1353: * gets rid of the directory entry on disk, the in memory copy
1354: * still exists but the de_refcnt is <= 0. This prevents it from
1355: * being found by deget(). When the vput() on dep is done we give
1356: * up access and eventually msdosfs_reclaim() will be called which
1357: * will remove it from the denode cache.
1358: */
1359: if ((error = removede(dp, ip)) != 0)
1360: goto out;
1361: /*
1362: * This is where we decrement the link count in the parent
1363: * directory. Since dos filesystems don't do this we just purge
1364: * the name cache and let go of the parent directory denode.
1365: */
1366: VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1367: cache_purge(dvp);
1368: vput(dvp);
1369: dvp = NULL;
1370: /*
1371: * Truncate the directory that is being deleted.
1372: */
1.24 christos 1373: error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred, cnp->cn_lwp);
1.1 jdolecek 1374: cache_purge(vp);
1375: out:
1376: VN_KNOTE(vp, NOTE_DELETE);
1377: if (dvp)
1378: vput(dvp);
1379: vput(vp);
1380: return (error);
1381: }
1382:
1383: /*
1384: * DOS filesystems don't know what symlinks are.
1385: */
1386: int
1387: msdosfs_symlink(v)
1388: void *v;
1389: {
1390: struct vop_symlink_args /* {
1391: struct vnode *a_dvp;
1392: struct vnode **a_vpp;
1393: struct componentname *a_cnp;
1394: struct vattr *a_vap;
1395: char *a_target;
1396: } */ *ap = v;
1397:
1398: VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1399: vput(ap->a_dvp);
1400: return (EOPNOTSUPP);
1401: }
1402:
1403: int
1404: msdosfs_readdir(v)
1405: void *v;
1406: {
1407: struct vop_readdir_args /* {
1408: struct vnode *a_vp;
1409: struct uio *a_uio;
1.28 elad 1410: kauth_cred_t a_cred;
1.1 jdolecek 1411: int *a_eofflag;
1412: off_t **a_cookies;
1413: int *a_ncookies;
1414: } */ *ap = v;
1415: int error = 0;
1416: int diff;
1417: long n;
1418: int blsize;
1419: long on;
1420: long lost;
1421: long count;
1422: u_long cn;
1.19 christos 1423: ino_t fileno;
1.1 jdolecek 1424: u_long dirsperblk;
1425: long bias = 0;
1426: daddr_t bn, lbn;
1427: struct buf *bp;
1428: struct denode *dep = VTODE(ap->a_vp);
1429: struct msdosfsmount *pmp = dep->de_pmp;
1430: struct direntry *dentp;
1431: struct dirent dirbuf;
1432: struct uio *uio = ap->a_uio;
1433: off_t *cookies = NULL;
1434: int ncookies = 0, nc = 0;
1435: off_t offset, uio_off;
1436: int chksum = -1;
1437:
1438: #ifdef MSDOSFS_DEBUG
1439: printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1440: ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1441: #endif
1442:
1443: /*
1444: * msdosfs_readdir() won't operate properly on regular files since
1445: * it does i/o only with the filesystem vnode, and hence can
1446: * retrieve the wrong block from the buffer cache for a plain file.
1447: * So, fail attempts to readdir() on a plain file.
1448: */
1449: if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1450: return (ENOTDIR);
1451:
1452: /*
1453: * To be safe, initialize dirbuf
1454: */
1455: memset(dirbuf.d_name, 0, sizeof(dirbuf.d_name));
1456:
1457: /*
1458: * If the user buffer is smaller than the size of one dos directory
1459: * entry or the file offset is not a multiple of the size of a
1460: * directory entry, then we fail the read.
1461: */
1462: count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
1463: offset = uio->uio_offset;
1464: if (count < sizeof(struct direntry) ||
1465: (offset & (sizeof(struct direntry) - 1)))
1466: return (EINVAL);
1467: lost = uio->uio_resid - count;
1468: uio->uio_resid = count;
1469: uio_off = uio->uio_offset;
1470:
1471: if (ap->a_ncookies) {
1.38.2.5! ad 1472: nc = uio->uio_resid / _DIRENT_MINSIZE((struct dirent *)0);
1.1 jdolecek 1473: cookies = malloc(nc * sizeof (off_t), M_TEMP, M_WAITOK);
1474: *ap->a_cookies = cookies;
1475: }
1476:
1477: dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1478:
1479: /*
1480: * If they are reading from the root directory then, we simulate
1481: * the . and .. entries since these don't exist in the root
1482: * directory. We also set the offset bias to make up for having to
1483: * simulate these entries. By this I mean that at file offset 64 we
1484: * read the first entry in the root directory that lives on disk.
1485: */
1486: if (dep->de_StartCluster == MSDOSFSROOT
1487: || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1488: #if 0
1.34 scw 1489: printf("msdosfs_readdir(): going after . or .. in root dir, "
1490: "offset %" PRIu64 "\n", offset);
1.1 jdolecek 1491: #endif
1492: bias = 2 * sizeof(struct direntry);
1493: if (offset < bias) {
1494: for (n = (int)offset / sizeof(struct direntry);
1495: n < 2; n++) {
1496: if (FAT32(pmp))
1497: dirbuf.d_fileno = cntobn(pmp,
1.19 christos 1498: (ino_t)pmp->pm_rootdirblk)
1499: * dirsperblk;
1.1 jdolecek 1500: else
1501: dirbuf.d_fileno = 1;
1502: dirbuf.d_type = DT_DIR;
1503: switch (n) {
1504: case 0:
1505: dirbuf.d_namlen = 1;
1.3 itojun 1506: strlcpy(dirbuf.d_name, ".",
1507: sizeof(dirbuf.d_name));
1.1 jdolecek 1508: break;
1509: case 1:
1510: dirbuf.d_namlen = 2;
1.3 itojun 1511: strlcpy(dirbuf.d_name, "..",
1512: sizeof(dirbuf.d_name));
1.1 jdolecek 1513: break;
1514: }
1.15 christos 1515: dirbuf.d_reclen = _DIRENT_SIZE(&dirbuf);
1.1 jdolecek 1516: if (uio->uio_resid < dirbuf.d_reclen)
1517: goto out;
1.14 perry 1518: error = uiomove(&dirbuf,
1.1 jdolecek 1519: dirbuf.d_reclen, uio);
1520: if (error)
1521: goto out;
1522: offset += sizeof(struct direntry);
1523: uio_off = offset;
1524: if (cookies) {
1525: *cookies++ = offset;
1526: ncookies++;
1527: if (ncookies >= nc)
1528: goto out;
1529: }
1530: }
1531: }
1532: }
1533:
1534: while (uio->uio_resid > 0) {
1535: lbn = de_cluster(pmp, offset - bias);
1536: on = (offset - bias) & pmp->pm_crbomask;
1537: n = MIN(pmp->pm_bpcluster - on, uio->uio_resid);
1538: diff = dep->de_FileSize - (offset - bias);
1539: if (diff <= 0)
1540: break;
1541: n = MIN(n, diff);
1542: if ((error = pcbmap(dep, lbn, &bn, &cn, &blsize)) != 0)
1543: break;
1.34 scw 1544: error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
1545: NOCRED, &bp);
1.1 jdolecek 1546: if (error) {
1.38.2.2 ad 1547: brelse(bp, 0);
1.1 jdolecek 1548: return (error);
1549: }
1550: n = MIN(n, blsize - bp->b_resid);
1551:
1552: /*
1553: * Convert from dos directory entries to fs-independent
1554: * directory entries.
1555: */
1.38 christos 1556: for (dentp = (struct direntry *)((char *)bp->b_data + on);
1557: (char *)dentp < (char *)bp->b_data + on + n;
1.1 jdolecek 1558: dentp++, offset += sizeof(struct direntry)) {
1559: #if 0
1560:
1561: printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1562: dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1563: #endif
1564: /*
1565: * If this is an unused entry, we can stop.
1566: */
1567: if (dentp->deName[0] == SLOT_EMPTY) {
1.38.2.2 ad 1568: brelse(bp, 0);
1.1 jdolecek 1569: goto out;
1570: }
1571: /*
1572: * Skip deleted entries.
1573: */
1574: if (dentp->deName[0] == SLOT_DELETED) {
1575: chksum = -1;
1576: continue;
1577: }
1578:
1579: /*
1580: * Handle Win95 long directory entries
1581: */
1582: if (dentp->deAttributes == ATTR_WIN95) {
1583: if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1584: continue;
1585: chksum = win2unixfn((struct winentry *)dentp, &dirbuf, chksum);
1586: continue;
1587: }
1588:
1589: /*
1590: * Skip volume labels
1591: */
1592: if (dentp->deAttributes & ATTR_VOLUME) {
1593: chksum = -1;
1594: continue;
1595: }
1596: /*
1597: * This computation of d_fileno must match
1598: * the computation of va_fileid in
1599: * msdosfs_getattr.
1600: */
1601: if (dentp->deAttributes & ATTR_DIRECTORY) {
1602: fileno = getushort(dentp->deStartCluster);
1603: if (FAT32(pmp))
1.19 christos 1604: fileno |= ((ino_t)getushort(dentp->deHighClust)) << 16;
1.1 jdolecek 1605: /* if this is the root directory */
1606: if (fileno == MSDOSFSROOT)
1607: if (FAT32(pmp))
1608: fileno = cntobn(pmp,
1.19 christos 1609: (ino_t)pmp->pm_rootdirblk)
1610: * dirsperblk;
1.1 jdolecek 1611: else
1612: fileno = 1;
1613: else
1614: fileno = cntobn(pmp, fileno) * dirsperblk;
1615: dirbuf.d_fileno = fileno;
1616: dirbuf.d_type = DT_DIR;
1617: } else {
1618: dirbuf.d_fileno = offset / sizeof(struct direntry);
1619: dirbuf.d_type = DT_REG;
1620: }
1621: if (chksum != winChksum(dentp->deName))
1622: dirbuf.d_namlen = dos2unixfn(dentp->deName,
1623: (u_char *)dirbuf.d_name,
1624: pmp->pm_flags & MSDOSFSMNT_SHORTNAME);
1625: else
1626: dirbuf.d_name[dirbuf.d_namlen] = 0;
1627: chksum = -1;
1.15 christos 1628: dirbuf.d_reclen = _DIRENT_SIZE(&dirbuf);
1.1 jdolecek 1629: if (uio->uio_resid < dirbuf.d_reclen) {
1.38.2.2 ad 1630: brelse(bp, 0);
1.1 jdolecek 1631: goto out;
1632: }
1.10 jrf 1633: error = uiomove(&dirbuf,
1.1 jdolecek 1634: dirbuf.d_reclen, uio);
1635: if (error) {
1.38.2.2 ad 1636: brelse(bp, 0);
1.1 jdolecek 1637: goto out;
1638: }
1639: uio_off = offset + sizeof(struct direntry);
1640: if (cookies) {
1641: *cookies++ = offset + sizeof(struct direntry);
1642: ncookies++;
1643: if (ncookies >= nc) {
1.38.2.2 ad 1644: brelse(bp, 0);
1.1 jdolecek 1645: goto out;
1646: }
1647: }
1648: }
1.38.2.2 ad 1649: brelse(bp, 0);
1.1 jdolecek 1650: }
1651:
1652: out:
1653: uio->uio_offset = uio_off;
1654: uio->uio_resid += lost;
1655: if (dep->de_FileSize - (offset - bias) <= 0)
1656: *ap->a_eofflag = 1;
1657: else
1658: *ap->a_eofflag = 0;
1659:
1660: if (ap->a_ncookies) {
1661: if (error) {
1662: free(*ap->a_cookies, M_TEMP);
1663: *ap->a_ncookies = 0;
1664: *ap->a_cookies = NULL;
1665: } else
1666: *ap->a_ncookies = ncookies;
1667: }
1668: return (error);
1669: }
1670:
1671: /*
1672: * DOS filesystems don't know what symlinks are.
1673: */
1674: int
1.33 christos 1675: msdosfs_readlink(void *v)
1.1 jdolecek 1676: {
1677: #if 0
1678: struct vop_readlink_args /* {
1679: struct vnode *a_vp;
1680: struct uio *a_uio;
1.28 elad 1681: kauth_cred_t a_cred;
1.1 jdolecek 1682: } */ *ap;
1683: #endif
1684:
1685: return (EINVAL);
1686: }
1687:
1688: /*
1689: * vp - address of vnode file the file
1690: * bn - which cluster we are interested in mapping to a filesystem block number.
1691: * vpp - returns the vnode for the block special file holding the filesystem
1692: * containing the file of interest
1693: * bnp - address of where to return the filesystem relative block number
1694: */
1695: int
1696: msdosfs_bmap(v)
1697: void *v;
1698: {
1699: struct vop_bmap_args /* {
1700: struct vnode *a_vp;
1701: daddr_t a_bn;
1702: struct vnode **a_vpp;
1703: daddr_t *a_bnp;
1704: int *a_runp;
1705: } */ *ap = v;
1706: struct denode *dep = VTODE(ap->a_vp);
1.34 scw 1707: int status;
1.1 jdolecek 1708:
1709: if (ap->a_vpp != NULL)
1710: *ap->a_vpp = dep->de_devvp;
1711: if (ap->a_bnp == NULL)
1712: return (0);
1713: if (ap->a_runp) {
1714: /*
1715: * Sequential clusters should be counted here.
1716: */
1717: *ap->a_runp = 0;
1718: }
1.34 scw 1719: status = pcbmap(dep, ap->a_bn, ap->a_bnp, 0, 0);
1720: /*
1721: * We need to scale *ap->a_bnp by sector_size/DEV_BSIZE
1722: */
1723: *ap->a_bnp = de_bn2kb(dep->de_pmp, *ap->a_bnp);
1724: return status;
1.1 jdolecek 1725: }
1726:
1727: int
1728: msdosfs_strategy(v)
1729: void *v;
1730: {
1731: struct vop_strategy_args /* {
1.9 hannken 1732: struct vnode *a_vp;
1.1 jdolecek 1733: struct buf *a_bp;
1734: } */ *ap = v;
1.9 hannken 1735: struct vnode *vp = ap->a_vp;
1.1 jdolecek 1736: struct buf *bp = ap->a_bp;
1737: struct denode *dep = VTODE(bp->b_vp);
1738: int error = 0;
1739:
1.9 hannken 1740: if (vp->v_type == VBLK || vp->v_type == VCHR)
1.1 jdolecek 1741: panic("msdosfs_strategy: spec");
1742: /*
1743: * If we don't already know the filesystem relative block number
1744: * then get it using pcbmap(). If pcbmap() returns the block
1745: * number as -1 then we've got a hole in the file. DOS filesystems
1746: * don't allow files with holes, so we shouldn't ever see this.
1747: */
1748: if (bp->b_blkno == bp->b_lblkno) {
1749: error = pcbmap(dep, de_bn2cn(dep->de_pmp, bp->b_lblkno),
1750: &bp->b_blkno, 0, 0);
1751: if (error)
1752: bp->b_blkno = -1;
1753: if (bp->b_blkno == -1)
1754: clrbuf(bp);
1.34 scw 1755: else
1756: bp->b_blkno = de_bn2kb(dep->de_pmp, bp->b_blkno);
1.1 jdolecek 1757: }
1758: if (bp->b_blkno == -1) {
1.38.2.4 ad 1759: biodone(bp);
1.1 jdolecek 1760: return (error);
1761: }
1762:
1763: /*
1764: * Read/write the block from/to the disk that contains the desired
1765: * file block.
1766: */
1767:
1768: vp = dep->de_devvp;
1.8 hannken 1769: return (VOP_STRATEGY(vp, bp));
1.1 jdolecek 1770: }
1771:
1772: int
1773: msdosfs_print(v)
1774: void *v;
1775: {
1776: struct vop_print_args /* {
1777: struct vnode *vp;
1778: } */ *ap = v;
1779: struct denode *dep = VTODE(ap->a_vp);
1780:
1781: printf(
1782: "tag VT_MSDOSFS, startcluster %ld, dircluster %ld, diroffset %ld ",
1783: dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1784: printf(" dev %d, %d ", major(dep->de_dev), minor(dep->de_dev));
1785: lockmgr_printinfo(&ap->a_vp->v_lock);
1786: printf("\n");
1787: return (0);
1788: }
1789:
1790: int
1791: msdosfs_advlock(v)
1792: void *v;
1793: {
1794: struct vop_advlock_args /* {
1795: struct vnode *a_vp;
1.11 jrf 1796: void *a_id;
1.1 jdolecek 1797: int a_op;
1798: struct flock *a_fl;
1799: int a_flags;
1800: } */ *ap = v;
1801: struct denode *dep = VTODE(ap->a_vp);
1802:
1803: return lf_advlock(ap, &dep->de_lockf, dep->de_FileSize);
1804: }
1805:
1806: int
1807: msdosfs_pathconf(v)
1808: void *v;
1809: {
1810: struct vop_pathconf_args /* {
1811: struct vnode *a_vp;
1812: int a_name;
1813: register_t *a_retval;
1814: } */ *ap = v;
1815:
1816: switch (ap->a_name) {
1817: case _PC_LINK_MAX:
1818: *ap->a_retval = 1;
1819: return (0);
1820: case _PC_NAME_MAX:
1.12 jdolecek 1821: *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
1.1 jdolecek 1822: return (0);
1823: case _PC_PATH_MAX:
1824: *ap->a_retval = PATH_MAX;
1825: return (0);
1826: case _PC_CHOWN_RESTRICTED:
1827: *ap->a_retval = 1;
1828: return (0);
1829: case _PC_NO_TRUNC:
1830: *ap->a_retval = 0;
1831: return (0);
1832: case _PC_SYNC_IO:
1833: *ap->a_retval = 1;
1834: return (0);
1835: case _PC_FILESIZEBITS:
1836: *ap->a_retval = 32;
1837: return (0);
1838: default:
1839: return (EINVAL);
1840: }
1841: /* NOTREACHED */
1842: }
1843:
1.21 yamt 1844: int
1845: msdosfs_fsync(v)
1846: void *v;
1847: {
1848: struct vop_fsync_args /* {
1849: struct vnode *a_vp;
1.28 elad 1850: kauth_cred_t a_cred;
1.21 yamt 1851: int a_flags;
1852: off_t offlo;
1853: off_t offhi;
1.30 ad 1854: struct lwp *a_l;
1.21 yamt 1855: } */ *ap = v;
1856: struct vnode *vp = ap->a_vp;
1857: int wait;
1858: int error;
1859:
1860: wait = (ap->a_flags & FSYNC_WAIT) != 0;
1861: vflushbuf(vp, wait);
1862: if ((ap->a_flags & FSYNC_DATAONLY) != 0)
1863: error = 0;
1864: else
1865: error = msdosfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
1866:
1867: if (error == 0 && ap->a_flags & FSYNC_CACHE) {
1868: struct denode *dep = VTODE(vp);
1869: struct vnode *devvp = dep->de_devvp;
1870:
1871: int l = 0;
1872: error = VOP_IOCTL(devvp, DIOCCACHESYNC, &l, FWRITE,
1.30 ad 1873: ap->a_l->l_cred, ap->a_l);
1.21 yamt 1874: }
1875:
1876: return (error);
1877: }
1878:
1.20 christos 1879: void
1880: msdosfs_detimes(struct denode *dep, const struct timespec *acc,
1881: const struct timespec *mod, const struct timespec *cre, int gmtoff)
1882: {
1883: struct timespec *ts = NULL, tsb;
1884:
1885: KASSERT(dep->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS));
1.29 kardel 1886: /* XXX just call getnanotime early and use result if needed? */
1.20 christos 1887: dep->de_flag |= DE_MODIFIED;
1888: if (dep->de_flag & DE_UPDATE) {
1.29 kardel 1889: if (mod == NULL) {
1890: getnanotime(&tsb);
1891: mod = ts = &tsb;
1892: }
1.20 christos 1893: unix2dostime(mod, gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
1894: dep->de_Attributes |= ATTR_ARCHIVE;
1895: }
1896: if ((dep->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0) {
1897: if (dep->de_flag & DE_ACCESS) {
1898: if (acc == NULL)
1.29 kardel 1899: acc = ts == NULL ?
1900: (getnanotime(&tsb), ts = &tsb) : ts;
1.20 christos 1901: unix2dostime(acc, gmtoff, &dep->de_ADate, NULL, NULL);
1902: }
1903: if (dep->de_flag & DE_CREATE) {
1904: if (cre == NULL)
1.29 kardel 1905: cre = ts == NULL ?
1906: (getnanotime(&tsb), ts = &tsb) : ts;
1.20 christos 1907: unix2dostime(cre, gmtoff, &dep->de_CDate,
1908: &dep->de_CTime, &dep->de_CHun);
1909: }
1910: }
1911:
1912: dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS);
1913: }
1914:
1.1 jdolecek 1915: /* Global vfs data structures for msdosfs */
1.18 xtraeme 1916: int (**msdosfs_vnodeop_p)(void *);
1.1 jdolecek 1917: const struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
1918: { &vop_default_desc, vn_default_error },
1919: { &vop_lookup_desc, msdosfs_lookup }, /* lookup */
1920: { &vop_create_desc, msdosfs_create }, /* create */
1921: { &vop_mknod_desc, msdosfs_mknod }, /* mknod */
1922: { &vop_open_desc, msdosfs_open }, /* open */
1923: { &vop_close_desc, msdosfs_close }, /* close */
1924: { &vop_access_desc, msdosfs_access }, /* access */
1925: { &vop_getattr_desc, msdosfs_getattr }, /* getattr */
1926: { &vop_setattr_desc, msdosfs_setattr }, /* setattr */
1927: { &vop_read_desc, msdosfs_read }, /* read */
1928: { &vop_write_desc, msdosfs_write }, /* write */
1929: { &vop_lease_desc, msdosfs_lease_check }, /* lease */
1930: { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
1931: { &vop_ioctl_desc, msdosfs_ioctl }, /* ioctl */
1932: { &vop_poll_desc, msdosfs_poll }, /* poll */
1933: { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
1934: { &vop_revoke_desc, msdosfs_revoke }, /* revoke */
1935: { &vop_mmap_desc, msdosfs_mmap }, /* mmap */
1936: { &vop_fsync_desc, msdosfs_fsync }, /* fsync */
1937: { &vop_seek_desc, msdosfs_seek }, /* seek */
1938: { &vop_remove_desc, msdosfs_remove }, /* remove */
1939: { &vop_link_desc, msdosfs_link }, /* link */
1940: { &vop_rename_desc, msdosfs_rename }, /* rename */
1941: { &vop_mkdir_desc, msdosfs_mkdir }, /* mkdir */
1942: { &vop_rmdir_desc, msdosfs_rmdir }, /* rmdir */
1943: { &vop_symlink_desc, msdosfs_symlink }, /* symlink */
1944: { &vop_readdir_desc, msdosfs_readdir }, /* readdir */
1945: { &vop_readlink_desc, msdosfs_readlink }, /* readlink */
1946: { &vop_abortop_desc, msdosfs_abortop }, /* abortop */
1947: { &vop_inactive_desc, msdosfs_inactive }, /* inactive */
1948: { &vop_reclaim_desc, msdosfs_reclaim }, /* reclaim */
1949: { &vop_lock_desc, genfs_lock }, /* lock */
1950: { &vop_unlock_desc, genfs_unlock }, /* unlock */
1951: { &vop_bmap_desc, msdosfs_bmap }, /* bmap */
1952: { &vop_strategy_desc, msdosfs_strategy }, /* strategy */
1953: { &vop_print_desc, msdosfs_print }, /* print */
1954: { &vop_islocked_desc, genfs_islocked }, /* islocked */
1955: { &vop_pathconf_desc, msdosfs_pathconf }, /* pathconf */
1956: { &vop_advlock_desc, msdosfs_advlock }, /* advlock */
1957: { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1958: { &vop_getpages_desc, genfs_getpages }, /* getpages */
1959: { &vop_putpages_desc, genfs_putpages }, /* putpages */
1960: { NULL, NULL }
1961: };
1962: const struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
1963: { &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
CVSweb <webmaster@jp.NetBSD.org>