Merge from vendor branch BZIP:
[dragonfly.git] / sys / vfs / hpfs / hpfs_lookup.c
1 /*-
2  * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/fs/hpfs/hpfs_lookup.c,v 1.1 1999/12/09 19:09:59 semenu Exp $
27  * $DragonFly: src/sys/vfs/hpfs/hpfs_lookup.c,v 1.4 2003/08/07 21:17:41 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/proc.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/vnode.h>
38 #include <sys/mount.h>
39 #include <sys/namei.h>
40 #include <sys/malloc.h>
41 #include <sys/buf.h>
42
43 #include "hpfs.h"
44 #include "hpfsmount.h"
45 #include "hpfs_subr.h"
46
47 int     hpfs_removedirent (struct hpfsmount *, lsn_t, char *, int, int *);
48
49 /*
50  * This routine traverse the b+ tree representing directory
51  * looking for file named 'name'. Returns buf struct and hpfsdirent
52  * pointer. Calling routine is supposed to brelse buffer.
53  * name is supposed in Unix encodeing.
54  */
55 int
56 hpfs_genlookupbyname (
57         struct hpfsnode *dhp,
58         char *name,
59         int namelen,
60         struct buf **bpp,
61         struct hpfsdirent **depp)
62 {
63         struct hpfsmount *hpmp = dhp->h_hpmp;
64         struct buf *bp;
65         struct dirblk *dp;
66         struct hpfsdirent *dep;
67         lsn_t lsn;
68         int error, res;
69
70         dprintf(("hpfs_genlookupbyname(0x%x, %s (%d)): \n", 
71                 dhp->h_no, name, namelen));
72
73         lsn = ((alleaf_t *)dhp->h_fn.fn_abd)->al_lsn;
74 dive:
75         error = hpfs_breaddirblk (hpmp, lsn, &bp);
76         if (error)
77                 return (error);
78
79         dp = (struct dirblk *) bp->b_data;
80         dep = D_DIRENT(dp);
81
82         while(!(dep->de_flag & DE_END)) {
83                 dprintf(("no: 0x%x, size: %d, name: %2d:%.*s, flag: 0x%x\n",
84                         dep->de_fnode, dep->de_size, dep->de_namelen,
85                         dep->de_namelen, dep->de_name, dep->de_flag));
86
87                 res = hpfs_cmpfname(hpmp, name, namelen,
88                                 dep->de_name, dep->de_namelen, dep->de_cpid);
89                 if (res == 0) {
90                         *bpp = bp;
91                         *depp = dep;
92                         return (0);
93                 } else if (res < 0)
94                         break;
95
96                 dep = (hpfsdirent_t *)(((caddr_t)dep) + dep->de_reclen);
97         }
98
99         if (dep->de_flag & DE_DOWN) {
100                 lsn = DE_DOWNLSN(dep);
101                 brelse(bp);
102                 goto dive;
103         }
104
105         brelse(bp);
106
107         return (ENOENT);
108 }
109
110 int
111 hpfs_makefnode (
112         struct vnode * dvp,
113         struct vnode ** vpp,
114         struct componentname *cnp,
115         struct vattr *vap)
116 {
117 #ifdef HPFS_DEBUG
118         struct hpfsnode *dhp = VTOHP(dvp);
119         dprintf(("hpfs_makefnode(0x%x, %s, %ld): \n",
120                 dhp->h_no, cnp->cn_nameptr, cnp->cn_namelen));
121 #endif
122
123         return (EOPNOTSUPP);
124 }
125
126 int
127 hpfs_removedirent (
128         struct hpfsmount *hpmp,
129         lsn_t lsn,
130         char *name,
131         int namelen,
132         int *retp)
133 {
134 #if 0
135         struct buf *bp;
136         dirblk_t *dbp;
137         struct hpfsdirent *dep;
138         int deoff;
139         int error, ret;
140
141         dprintf(("hpfs_removedirent(0x%x, %.*s, %d): \n",
142                  lsn, namelen, name, namelen));
143
144         error = hpfs_breaddirblk (hpmp, lsn, &bp);
145         if (error)
146                 return (error);
147
148         dbp = (dirblk_t *) bp->b_data;
149         deoff = sizeof(dirblk_t);
150         dep = DB_DIRENT(dbp);
151
152         while(!(dep->de_flag & DE_END)) {
153                 dprintf(("no: 0x%x, size: %d, name: %2d:%.*s, flag: 0x%x\n",
154                         dep->de_fnode, dep->de_size, dep->de_namelen,
155                         dep->de_namelen, dep->de_name, dep->de_flag));
156
157                 res = hpfs_cmpfname(hpmp, name, namelen,
158                                 dep->de_name, dep->de_namelen, dep->de_cpid);
159                 if (res == 0) {
160                         if (dep->de_flag & DE_DOWN) {
161                                 /*XXXXXX*/
162                         } else {
163                                 /* XXX we can copy less */
164                                 bcopy (DE_NEXTDE(dep), dep, DB_BSIZE - deoff - dep->de_reclen);
165                                 dbp->d_freeoff -= dep->de_reclen;
166                                 *retp = 0;
167                         }
168                         bdwrite (bp);
169                         return (0);
170                 } else if (res < 0)
171                         break;
172
173                 deoff += dep->de_reclen;
174                 dep = DB_NEXTDE(dep);
175         }
176
177         if (dep->de_flag & DE_DOWN) {
178                 error = hpfs_removede (hpmp, DE_DOWNLSN(dep), name, namelen, &ret);
179                 if (error) {
180                         brelse (bp);
181                         return (error);
182                 }
183                 if (ret == 0) {
184                         if (deoff > sizeof (dirblk_t)) {
185                         } else if (deoff + dep->de_reclen < dbp->db_freeoff) {
186                         }
187                 }
188         } else {
189                 error = ENOENT;
190         }
191
192         brelse (bp);
193         return (error);
194 #endif
195         return (EOPNOTSUPP);
196 }
197
198 int
199 hpfs_removefnode (
200         struct vnode * dvp,
201         struct vnode * vp,
202         struct componentname *cnp)
203 {
204 #ifdef HPFS_DEBUG
205         struct hpfsnode *dhp = VTOHP(dvp);
206         struct hpfsnode *hp = VTOHP(vp);
207         dprintf(("hpfs_removefnode(0x%x, 0x%x, %s, %ld): \n",
208                 dhp->h_no, hp->h_no, cnp->cn_nameptr, cnp->cn_namelen));
209 #endif
210
211
212         return (EOPNOTSUPP);
213 }