Blame view

fs/hpfs/dnode.c 30.9 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /*
   *  linux/fs/hpfs/dnode.c
   *
   *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
   *
   *  handling directory dnode tree - adding, deleteing & searching for dirents
   */
  
  #include "hpfs_fn.h"
  
  static loff_t get_pos(struct dnode *d, struct hpfs_dirent *fde)
  {
  	struct hpfs_dirent *de;
  	struct hpfs_dirent *de_end = dnode_end_de(d);
  	int i = 1;
  	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de)) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
17
  		if (de == fde) return ((loff_t) le32_to_cpu(d->self) << 4) | (loff_t)i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
  		i++;
  	}
  	printk("HPFS: get_pos: not_found
  ");
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
22
  	return ((loff_t)le32_to_cpu(d->self) << 4) | (loff_t)1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  }
  
  void hpfs_add_pos(struct inode *inode, loff_t *pos)
  {
  	struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
  	int i = 0;
  	loff_t **ppos;
  
  	if (hpfs_inode->i_rddir_off)
  		for (; hpfs_inode->i_rddir_off[i]; i++)
  			if (hpfs_inode->i_rddir_off[i] == pos) return;
  	if (!(i&0x0f)) {
  		if (!(ppos = kmalloc((i+0x11) * sizeof(loff_t*), GFP_NOFS))) {
  			printk("HPFS: out of memory for position list
  ");
  			return;
  		}
  		if (hpfs_inode->i_rddir_off) {
  			memcpy(ppos, hpfs_inode->i_rddir_off, i * sizeof(loff_t));
  			kfree(hpfs_inode->i_rddir_off);
  		}
  		hpfs_inode->i_rddir_off = ppos;
  	}
  	hpfs_inode->i_rddir_off[i] = pos;
  	hpfs_inode->i_rddir_off[i + 1] = NULL;
  }
  
  void hpfs_del_pos(struct inode *inode, loff_t *pos)
  {
  	struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
  	loff_t **i, **j;
  
  	if (!hpfs_inode->i_rddir_off) goto not_f;
  	for (i = hpfs_inode->i_rddir_off; *i; i++) if (*i == pos) goto fnd;
  	goto not_f;
  	fnd:
  	for (j = i + 1; *j; j++) ;
  	*i = *(j - 1);
  	*(j - 1) = NULL;
  	if (j - 1 == hpfs_inode->i_rddir_off) {
  		kfree(hpfs_inode->i_rddir_off);
  		hpfs_inode->i_rddir_off = NULL;
  	}
  	return;
  	not_f:
  	/*printk("HPFS: warning: position pointer %p->%08x not found
  ", pos, (int)*pos);*/
  	return;
  }
  
  static void for_all_poss(struct inode *inode, void (*f)(loff_t *, loff_t, loff_t),
  			 loff_t p1, loff_t p2)
  {
  	struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
  	loff_t **i;
  
  	if (!hpfs_inode->i_rddir_off) return;
  	for (i = hpfs_inode->i_rddir_off; *i; i++) (*f)(*i, p1, p2);
  	return;
  }
  
  static void hpfs_pos_subst(loff_t *p, loff_t f, loff_t t)
  {
  	if (*p == f) *p = t;
  }
  
  /*void hpfs_hpfs_pos_substd(loff_t *p, loff_t f, loff_t t)
  {
  	if ((*p & ~0x3f) == (f & ~0x3f)) *p = (t & ~0x3f) | (*p & 0x3f);
  }*/
  
  static void hpfs_pos_ins(loff_t *p, loff_t d, loff_t c)
  {
  	if ((*p & ~0x3f) == (d & ~0x3f) && (*p & 0x3f) >= (d & 0x3f)) {
  		int n = (*p & 0x3f) + c;
  		if (n > 0x3f) printk("HPFS: hpfs_pos_ins: %08x + %d
  ", (int)*p, (int)c >> 8);
  		else *p = (*p & ~0x3f) | n;
  	}
  }
  
  static void hpfs_pos_del(loff_t *p, loff_t d, loff_t c)
  {
  	if ((*p & ~0x3f) == (d & ~0x3f) && (*p & 0x3f) >= (d & 0x3f)) {
  		int n = (*p & 0x3f) - c;
  		if (n < 1) printk("HPFS: hpfs_pos_ins: %08x - %d
  ", (int)*p, (int)c >> 8);
  		else *p = (*p & ~0x3f) | n;
  	}
  }
  
  static struct hpfs_dirent *dnode_pre_last_de(struct dnode *d)
  {
  	struct hpfs_dirent *de, *de_end, *dee = NULL, *deee = NULL;
  	de_end = dnode_end_de(d);
  	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de)) {
  		deee = dee; dee = de;
  	}	
  	return deee;
  }
  
  static struct hpfs_dirent *dnode_last_de(struct dnode *d)
  {
  	struct hpfs_dirent *de, *de_end, *dee = NULL;
  	de_end = dnode_end_de(d);
  	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de)) {
  		dee = de;
  	}	
  	return dee;
  }
  
  static void set_last_pointer(struct super_block *s, struct dnode *d, dnode_secno ptr)
  {
  	struct hpfs_dirent *de;
  	if (!(de = dnode_last_de(d))) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
138
  		hpfs_error(s, "set_last_pointer: empty dnode %08x", le32_to_cpu(d->self));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
142
143
  		return;
  	}
  	if (hpfs_sb(s)->sb_chk) {
  		if (de->down) {
  			hpfs_error(s, "set_last_pointer: dnode %08x has already last pointer %08x",
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
144
  				le32_to_cpu(d->self), de_down_pointer(de));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
146
  			return;
  		}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
147
148
  		if (le16_to_cpu(de->length) != 32) {
  			hpfs_error(s, "set_last_pointer: bad last dirent in dnode %08x", le32_to_cpu(d->self));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
150
151
152
  			return;
  		}
  	}
  	if (ptr) {
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
153
154
  		d->first_free = cpu_to_le32(le32_to_cpu(d->first_free) + 4);
  		if (le32_to_cpu(d->first_free) > 2048) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
155
  			hpfs_error(s, "set_last_pointer: too long dnode %08x", le32_to_cpu(d->self));
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
156
  			d->first_free = cpu_to_le32(le32_to_cpu(d->first_free) - 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
  			return;
  		}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
159
  		de->length = cpu_to_le16(36);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
  		de->down = 1;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
161
  		*(dnode_secno *)((char *)de + 32) = cpu_to_le32(ptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
163
164
165
  	}
  }
  
  /* Add an entry to dnode and don't care if it grows over 2048 bytes */
7e7742ee0   Al Viro   sanitize signedne...
166
167
  struct hpfs_dirent *hpfs_add_de(struct super_block *s, struct dnode *d,
  				const unsigned char *name,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
169
170
171
172
173
174
175
  				unsigned namelen, secno down_ptr)
  {
  	struct hpfs_dirent *de;
  	struct hpfs_dirent *de_end = dnode_end_de(d);
  	unsigned d_size = de_size(namelen, down_ptr);
  	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de)) {
  		int c = hpfs_compare_names(s, name, namelen, de->name, de->namelen, de->last);
  		if (!c) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
176
  			hpfs_error(s, "name (%c,%d) already exists in dnode %08x", *name, namelen, le32_to_cpu(d->self));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
178
179
180
181
182
183
  			return NULL;
  		}
  		if (c < 0) break;
  	}
  	memmove((char *)de + d_size, de, (char *)de_end - (char *)de);
  	memset(de, 0, d_size);
  	if (down_ptr) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
184
  		*(dnode_secno *)((char *)de + d_size - 4) = cpu_to_le32(down_ptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
186
  		de->down = 1;
  	}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
187
  	de->length = cpu_to_le16(d_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
189
190
  	de->not_8x3 = hpfs_is_name_long(name, namelen);
  	de->namelen = namelen;
  	memcpy(de->name, name, namelen);
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
191
  	d->first_free = cpu_to_le32(le32_to_cpu(d->first_free) + d_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192
193
194
195
196
197
198
199
200
  	return de;
  }
  
  /* Delete dirent and don't care about its subtree */
  
  static void hpfs_delete_de(struct super_block *s, struct dnode *d,
  			   struct hpfs_dirent *de)
  {
  	if (de->last) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
201
  		hpfs_error(s, "attempt to delete last dirent in dnode %08x", le32_to_cpu(d->self));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
203
  		return;
  	}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
204
  	d->first_free = cpu_to_le32(le32_to_cpu(d->first_free) - le16_to_cpu(de->length));
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
205
  	memmove(de, de_next_de(de), le32_to_cpu(d->first_free) + (char *)d - (char *)de);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
207
208
209
210
211
  }
  
  static void fix_up_ptrs(struct super_block *s, struct dnode *d)
  {
  	struct hpfs_dirent *de;
  	struct hpfs_dirent *de_end = dnode_end_de(d);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
212
  	dnode_secno dno = le32_to_cpu(d->self);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
214
215
216
217
  	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de))
  		if (de->down) {
  			struct quad_buffer_head qbh;
  			struct dnode *dd;
  			if ((dd = hpfs_map_dnode(s, de_down_pointer(de), &qbh))) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
218
219
  				if (le32_to_cpu(dd->up) != dno || dd->root_dnode) {
  					dd->up = cpu_to_le32(dno);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
222
223
224
225
226
227
228
229
230
  					dd->root_dnode = 0;
  					hpfs_mark_4buffers_dirty(&qbh);
  				}
  				hpfs_brelse4(&qbh);
  			}
  		}
  }
  
  /* Add an entry to dnode and do dnode splitting if required */
  
  static int hpfs_add_to_dnode(struct inode *i, dnode_secno dno,
7e7742ee0   Al Viro   sanitize signedne...
231
  			     const unsigned char *name, unsigned namelen,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
233
234
235
236
237
238
  			     struct hpfs_dirent *new_de, dnode_secno down_ptr)
  {
  	struct quad_buffer_head qbh, qbh1, qbh2;
  	struct dnode *d, *ad, *rd, *nd = NULL;
  	dnode_secno adno, rdno;
  	struct hpfs_dirent *de;
  	struct hpfs_dirent nde;
7e7742ee0   Al Viro   sanitize signedne...
239
  	unsigned char *nname;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
241
242
243
244
245
246
247
248
249
250
251
252
  	int h;
  	int pos;
  	struct buffer_head *bh;
  	struct fnode *fnode;
  	int c1, c2 = 0;
  	if (!(nname = kmalloc(256, GFP_NOFS))) {
  		printk("HPFS: out of memory, can't add to dnode
  ");
  		return 1;
  	}
  	go_up:
  	if (namelen >= 256) {
  		hpfs_error(i->i_sb, "hpfs_add_to_dnode: namelen == %d", namelen);
f99d49adf   Jesper Juhl   [PATCH] kfree cle...
253
  		kfree(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
256
257
  		kfree(nname);
  		return 1;
  	}
  	if (!(d = hpfs_map_dnode(i->i_sb, dno, &qbh))) {
f99d49adf   Jesper Juhl   [PATCH] kfree cle...
258
  		kfree(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
260
261
262
263
264
265
  		kfree(nname);
  		return 1;
  	}
  	go_up_a:
  	if (hpfs_sb(i->i_sb)->sb_chk)
  		if (hpfs_stop_cycles(i->i_sb, dno, &c1, &c2, "hpfs_add_to_dnode")) {
  			hpfs_brelse4(&qbh);
f99d49adf   Jesper Juhl   [PATCH] kfree cle...
266
  			kfree(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
268
269
  			kfree(nname);
  			return 1;
  		}
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
270
  	if (le32_to_cpu(d->first_free) + de_size(namelen, down_ptr) <= 2048) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
272
273
274
275
276
277
278
  		loff_t t;
  		copy_de(de=hpfs_add_de(i->i_sb, d, name, namelen, down_ptr), new_de);
  		t = get_pos(d, de);
  		for_all_poss(i, hpfs_pos_ins, t, 1);
  		for_all_poss(i, hpfs_pos_subst, 4, t);
  		for_all_poss(i, hpfs_pos_subst, 5, t + 1);
  		hpfs_mark_4buffers_dirty(&qbh);
  		hpfs_brelse4(&qbh);
f99d49adf   Jesper Juhl   [PATCH] kfree cle...
279
  		kfree(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  		kfree(nname);
  		return 0;
  	}
  	if (!nd) if (!(nd = kmalloc(0x924, GFP_NOFS))) {
  		/* 0x924 is a max size of dnode after adding a dirent with
  		   max name length. We alloc this only once. There must
  		   not be any error while splitting dnodes, otherwise the
  		   whole directory, not only file we're adding, would
  		   be lost. */
  		printk("HPFS: out of memory for dnode splitting
  ");
  		hpfs_brelse4(&qbh);
  		kfree(nname);
  		return 1;
  	}	
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
295
  	memcpy(nd, d, le32_to_cpu(d->first_free));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
297
298
  	copy_de(de = hpfs_add_de(i->i_sb, nd, name, namelen, down_ptr), new_de);
  	for_all_poss(i, hpfs_pos_ins, get_pos(nd, de), 1);
  	h = ((char *)dnode_last_de(nd) - (char *)nd) / 2 + 10;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
299
  	if (!(ad = hpfs_alloc_dnode(i->i_sb, le32_to_cpu(d->up), &adno, &qbh1))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  		hpfs_error(i->i_sb, "unable to alloc dnode - dnode tree will be corrupted");
  		hpfs_brelse4(&qbh);
  		kfree(nd);
  		kfree(nname);
  		return 1;
  	}
  	i->i_size += 2048;
  	i->i_blocks += 4;
  	pos = 1;
  	for (de = dnode_first_de(nd); (char *)de_next_de(de) - (char *)nd < h; de = de_next_de(de)) {
  		copy_de(hpfs_add_de(i->i_sb, ad, de->name, de->namelen, de->down ? de_down_pointer(de) : 0), de);
  		for_all_poss(i, hpfs_pos_subst, ((loff_t)dno << 4) | pos, ((loff_t)adno << 4) | pos);
  		pos++;
  	}
  	copy_de(new_de = &nde, de);
7e7742ee0   Al Viro   sanitize signedne...
315
316
317
  	memcpy(nname, de->name, de->namelen);
  	name = nname;
  	namelen = de->namelen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318
319
320
321
  	for_all_poss(i, hpfs_pos_subst, ((loff_t)dno << 4) | pos, 4);
  	down_ptr = adno;
  	set_last_pointer(i->i_sb, ad, de->down ? de_down_pointer(de) : 0);
  	de = de_next_de(de);
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
322
  	memmove((char *)nd + 20, de, le32_to_cpu(nd->first_free) + (char *)nd - (char *)de);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
323
  	nd->first_free = cpu_to_le32(le32_to_cpu(nd->first_free) - ((char *)de - (char *)nd - 20));
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
324
  	memcpy(d, nd, le32_to_cpu(nd->first_free));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325
326
327
  	for_all_poss(i, hpfs_pos_del, (loff_t)dno << 4, pos);
  	fix_up_ptrs(i->i_sb, ad);
  	if (!d->root_dnode) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
328
329
  		ad->up = d->up;
  		dno = le32_to_cpu(ad->up);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
331
332
333
334
335
  		hpfs_mark_4buffers_dirty(&qbh);
  		hpfs_brelse4(&qbh);
  		hpfs_mark_4buffers_dirty(&qbh1);
  		hpfs_brelse4(&qbh1);
  		goto go_up;
  	}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
336
  	if (!(rd = hpfs_alloc_dnode(i->i_sb, le32_to_cpu(d->up), &rdno, &qbh2))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
337
338
339
340
341
342
343
344
345
346
347
  		hpfs_error(i->i_sb, "unable to alloc dnode - dnode tree will be corrupted");
  		hpfs_brelse4(&qbh);
  		hpfs_brelse4(&qbh1);
  		kfree(nd);
  		kfree(nname);
  		return 1;
  	}
  	i->i_size += 2048;
  	i->i_blocks += 4;
  	rd->root_dnode = 1;
  	rd->up = d->up;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
348
  	if (!(fnode = hpfs_map_fnode(i->i_sb, le32_to_cpu(d->up), &bh))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
349
350
351
352
353
354
355
356
  		hpfs_free_dnode(i->i_sb, rdno);
  		hpfs_brelse4(&qbh);
  		hpfs_brelse4(&qbh1);
  		hpfs_brelse4(&qbh2);
  		kfree(nd);
  		kfree(nname);
  		return 1;
  	}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
357
  	fnode->u.external[0].disk_secno = cpu_to_le32(rdno);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
359
  	mark_buffer_dirty(bh);
  	brelse(bh);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
360
361
  	hpfs_i(i)->i_dno = rdno;
  	d->up = ad->up = cpu_to_le32(rdno);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
  	d->root_dnode = ad->root_dnode = 0;
  	hpfs_mark_4buffers_dirty(&qbh);
  	hpfs_brelse4(&qbh);
  	hpfs_mark_4buffers_dirty(&qbh1);
  	hpfs_brelse4(&qbh1);
  	qbh = qbh2;
  	set_last_pointer(i->i_sb, rd, dno);
  	dno = rdno;
  	d = rd;
  	goto go_up_a;
  }
  
  /*
   * Add an entry to directory btree.
   * I hate such crazy directory structure.
   * It's easy to read but terrible to write.
   * I wrote this directory code 4 times.
   * I hope, now it's finally bug-free.
   */
7e7742ee0   Al Viro   sanitize signedne...
381
382
  int hpfs_add_dirent(struct inode *i,
  		    const unsigned char *name, unsigned namelen,
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
383
  		    struct hpfs_dirent *new_de)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
  {
  	struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  	struct dnode *d;
  	struct hpfs_dirent *de, *de_end;
  	struct quad_buffer_head qbh;
  	dnode_secno dno;
  	int c;
  	int c1, c2 = 0;
  	dno = hpfs_inode->i_dno;
  	down:
  	if (hpfs_sb(i->i_sb)->sb_chk)
  		if (hpfs_stop_cycles(i->i_sb, dno, &c1, &c2, "hpfs_add_dirent")) return 1;
  	if (!(d = hpfs_map_dnode(i->i_sb, dno, &qbh))) return 1;
  	de_end = dnode_end_de(d);
  	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de)) {
  		if (!(c = hpfs_compare_names(i->i_sb, name, namelen, de->name, de->namelen, de->last))) {
  			hpfs_brelse4(&qbh);
  			return -1;
  		}	
  		if (c < 0) {
  			if (de->down) {
  				dno = de_down_pointer(de);
  				hpfs_brelse4(&qbh);
  				goto down;
  			}
  			break;
  		}
  	}
  	hpfs_brelse4(&qbh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
414
415
416
417
418
419
  	if (hpfs_check_free_dnodes(i->i_sb, FREE_DNODES_ADD)) {
  		c = 1;
  		goto ret;
  	}	
  	i->i_version++;
  	c = hpfs_add_to_dnode(i, dno, name, namelen, new_de, 0);
  	ret:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
  	return c;
  }
  
  /* 
   * Find dirent with higher name in 'from' subtree and move it to 'to' dnode.
   * Return the dnode we moved from (to be checked later if it's empty)
   */
  
  static secno move_to_top(struct inode *i, dnode_secno from, dnode_secno to)
  {
  	dnode_secno dno, ddno;
  	dnode_secno chk_up = to;
  	struct dnode *dnode;
  	struct quad_buffer_head qbh;
  	struct hpfs_dirent *de, *nde;
  	int a;
  	loff_t t;
  	int c1, c2 = 0;
  	dno = from;
  	while (1) {
  		if (hpfs_sb(i->i_sb)->sb_chk)
  			if (hpfs_stop_cycles(i->i_sb, dno, &c1, &c2, "move_to_top"))
  				return 0;
  		if (!(dnode = hpfs_map_dnode(i->i_sb, dno, &qbh))) return 0;
  		if (hpfs_sb(i->i_sb)->sb_chk) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
445
  			if (le32_to_cpu(dnode->up) != chk_up) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446
  				hpfs_error(i->i_sb, "move_to_top: up pointer from %08x should be %08x, is %08x",
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
447
  					dno, chk_up, le32_to_cpu(dnode->up));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
  				hpfs_brelse4(&qbh);
  				return 0;
  			}
  			chk_up = dno;
  		}
  		if (!(de = dnode_last_de(dnode))) {
  			hpfs_error(i->i_sb, "move_to_top: dnode %08x has no last de", dno);
  			hpfs_brelse4(&qbh);
  			return 0;
  		}
  		if (!de->down) break;
  		dno = de_down_pointer(de);
  		hpfs_brelse4(&qbh);
  	}
  	while (!(de = dnode_pre_last_de(dnode))) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
463
  		dnode_secno up = le32_to_cpu(dnode->up);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
  		hpfs_brelse4(&qbh);
  		hpfs_free_dnode(i->i_sb, dno);
  		i->i_size -= 2048;
  		i->i_blocks -= 4;
  		for_all_poss(i, hpfs_pos_subst, ((loff_t)dno << 4) | 1, 5);
  		if (up == to) return to;
  		if (!(dnode = hpfs_map_dnode(i->i_sb, up, &qbh))) return 0;
  		if (dnode->root_dnode) {
  			hpfs_error(i->i_sb, "move_to_top: got to root_dnode while moving from %08x to %08x", from, to);
  			hpfs_brelse4(&qbh);
  			return 0;
  		}
  		de = dnode_last_de(dnode);
  		if (!de || !de->down) {
  			hpfs_error(i->i_sb, "move_to_top: dnode %08x doesn't point down to %08x", up, dno);
  			hpfs_brelse4(&qbh);
  			return 0;
  		}
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
482
  		dnode->first_free = cpu_to_le32(le32_to_cpu(dnode->first_free) - 4);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
483
  		de->length = cpu_to_le16(le16_to_cpu(de->length) - 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
485
486
487
488
489
490
  		de->down = 0;
  		hpfs_mark_4buffers_dirty(&qbh);
  		dno = up;
  	}
  	t = get_pos(dnode, de);
  	for_all_poss(i, hpfs_pos_subst, t, 4);
  	for_all_poss(i, hpfs_pos_subst, t + 1, 5);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
491
  	if (!(nde = kmalloc(le16_to_cpu(de->length), GFP_NOFS))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
492
493
494
495
  		hpfs_error(i->i_sb, "out of memory for dirent - directory will be corrupted");
  		hpfs_brelse4(&qbh);
  		return 0;
  	}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
496
  	memcpy(nde, de, le16_to_cpu(de->length));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
  	ddno = de->down ? de_down_pointer(de) : 0;
  	hpfs_delete_de(i->i_sb, dnode, de);
  	set_last_pointer(i->i_sb, dnode, ddno);
  	hpfs_mark_4buffers_dirty(&qbh);
  	hpfs_brelse4(&qbh);
  	a = hpfs_add_to_dnode(i, to, nde->name, nde->namelen, nde, from);
  	kfree(nde);
  	if (a) return 0;
  	return dno;
  }
  
  /* 
   * Check if a dnode is empty and delete it from the tree
   * (chkdsk doesn't like empty dnodes)
   */
  
  static void delete_empty_dnode(struct inode *i, dnode_secno dno)
  {
  	struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  	struct quad_buffer_head qbh;
  	struct dnode *dnode;
  	dnode_secno down, up, ndown;
  	int p;
  	struct hpfs_dirent *de;
  	int c1, c2 = 0;
  	try_it_again:
  	if (hpfs_stop_cycles(i->i_sb, dno, &c1, &c2, "delete_empty_dnode")) return;
  	if (!(dnode = hpfs_map_dnode(i->i_sb, dno, &qbh))) return;
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
525
526
  	if (le32_to_cpu(dnode->first_free) > 56) goto end;
  	if (le32_to_cpu(dnode->first_free) == 52 || le32_to_cpu(dnode->first_free) == 56) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
527
528
  		struct hpfs_dirent *de_end;
  		int root = dnode->root_dnode;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
529
  		up = le32_to_cpu(dnode->up);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
  		de = dnode_first_de(dnode);
  		down = de->down ? de_down_pointer(de) : 0;
  		if (hpfs_sb(i->i_sb)->sb_chk) if (root && !down) {
  			hpfs_error(i->i_sb, "delete_empty_dnode: root dnode %08x is empty", dno);
  			goto end;
  		}
  		hpfs_brelse4(&qbh);
  		hpfs_free_dnode(i->i_sb, dno);
  		i->i_size -= 2048;
  		i->i_blocks -= 4;
  		if (root) {
  			struct fnode *fnode;
  			struct buffer_head *bh;
  			struct dnode *d1;
  			struct quad_buffer_head qbh1;
18debbbcc   Randy Dunlap   [PATCH] hpfs: fix...
545
546
547
548
549
  			if (hpfs_sb(i->i_sb)->sb_chk)
  			    if (up != i->i_ino) {
  				hpfs_error(i->i_sb,
  					"bad pointer to fnode, dnode %08x, pointing to %08x, should be %08lx",
  					dno, up, (unsigned long)i->i_ino);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
550
  				return;
18debbbcc   Randy Dunlap   [PATCH] hpfs: fix...
551
  			    }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
  			if ((d1 = hpfs_map_dnode(i->i_sb, down, &qbh1))) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
553
  				d1->up = cpu_to_le32(up);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
554
555
556
557
558
  				d1->root_dnode = 1;
  				hpfs_mark_4buffers_dirty(&qbh1);
  				hpfs_brelse4(&qbh1);
  			}
  			if ((fnode = hpfs_map_fnode(i->i_sb, up, &bh))) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
559
  				fnode->u.external[0].disk_secno = cpu_to_le32(down);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
  				mark_buffer_dirty(bh);
  				brelse(bh);
  			}
  			hpfs_inode->i_dno = down;
  			for_all_poss(i, hpfs_pos_subst, ((loff_t)dno << 4) | 1, (loff_t) 12);
  			return;
  		}
  		if (!(dnode = hpfs_map_dnode(i->i_sb, up, &qbh))) return;
  		p = 1;
  		de_end = dnode_end_de(dnode);
  		for (de = dnode_first_de(dnode); de < de_end; de = de_next_de(de), p++)
  			if (de->down) if (de_down_pointer(de) == dno) goto fnd;
  		hpfs_error(i->i_sb, "delete_empty_dnode: pointer to dnode %08x not found in dnode %08x", dno, up);
  		goto end;
  		fnd:
  		for_all_poss(i, hpfs_pos_subst, ((loff_t)dno << 4) | 1, ((loff_t)up << 4) | p);
  		if (!down) {
  			de->down = 0;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
578
  			de->length = cpu_to_le16(le16_to_cpu(de->length) - 4);
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
579
  			dnode->first_free = cpu_to_le32(le32_to_cpu(dnode->first_free) - 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
580
  			memmove(de_next_de(de), (char *)de_next_de(de) + 4,
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
581
  				(char *)dnode + le32_to_cpu(dnode->first_free) - (char *)de_next_de(de));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
582
583
584
  		} else {
  			struct dnode *d1;
  			struct quad_buffer_head qbh1;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
585
  			*(dnode_secno *) ((void *) de + le16_to_cpu(de->length) - 4) = down;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
586
  			if ((d1 = hpfs_map_dnode(i->i_sb, down, &qbh1))) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
587
  				d1->up = cpu_to_le32(up);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
588
589
590
591
592
  				hpfs_mark_4buffers_dirty(&qbh1);
  				hpfs_brelse4(&qbh1);
  			}
  		}
  	} else {
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
593
  		hpfs_error(i->i_sb, "delete_empty_dnode: dnode %08x, first_free == %03x", dno, le32_to_cpu(dnode->first_free));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
594
595
596
597
598
599
600
601
602
603
  		goto end;
  	}
  
  	if (!de->last) {
  		struct hpfs_dirent *de_next = de_next_de(de);
  		struct hpfs_dirent *de_cp;
  		struct dnode *d1;
  		struct quad_buffer_head qbh1;
  		if (!de_next->down) goto endm;
  		ndown = de_down_pointer(de_next);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
604
  		if (!(de_cp = kmalloc(le16_to_cpu(de->length), GFP_NOFS))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
605
606
607
608
  			printk("HPFS: out of memory for dtree balancing
  ");
  			goto endm;
  		}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
609
  		memcpy(de_cp, de, le16_to_cpu(de->length));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610
611
612
613
614
615
  		hpfs_delete_de(i->i_sb, dnode, de);
  		hpfs_mark_4buffers_dirty(&qbh);
  		hpfs_brelse4(&qbh);
  		for_all_poss(i, hpfs_pos_subst, ((loff_t)up << 4) | p, 4);
  		for_all_poss(i, hpfs_pos_del, ((loff_t)up << 4) | p, 1);
  		if (de_cp->down) if ((d1 = hpfs_map_dnode(i->i_sb, de_down_pointer(de_cp), &qbh1))) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
616
  			d1->up = cpu_to_le32(ndown);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
  			hpfs_mark_4buffers_dirty(&qbh1);
  			hpfs_brelse4(&qbh1);
  		}
  		hpfs_add_to_dnode(i, ndown, de_cp->name, de_cp->namelen, de_cp, de_cp->down ? de_down_pointer(de_cp) : 0);
  		/*printk("UP-TO-DNODE: %08x (ndown = %08x, down = %08x, dno = %08x)
  ", up, ndown, down, dno);*/
  		dno = up;
  		kfree(de_cp);
  		goto try_it_again;
  	} else {
  		struct hpfs_dirent *de_prev = dnode_pre_last_de(dnode);
  		struct hpfs_dirent *de_cp;
  		struct dnode *d1;
  		struct quad_buffer_head qbh1;
  		dnode_secno dlp;
  		if (!de_prev) {
  			hpfs_error(i->i_sb, "delete_empty_dnode: empty dnode %08x", up);
  			hpfs_mark_4buffers_dirty(&qbh);
  			hpfs_brelse4(&qbh);
  			dno = up;
  			goto try_it_again;
  		}
  		if (!de_prev->down) goto endm;
  		ndown = de_down_pointer(de_prev);
  		if ((d1 = hpfs_map_dnode(i->i_sb, ndown, &qbh1))) {
  			struct hpfs_dirent *del = dnode_last_de(d1);
  			dlp = del->down ? de_down_pointer(del) : 0;
  			if (!dlp && down) {
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
645
  				if (le32_to_cpu(d1->first_free) > 2044) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
  					if (hpfs_sb(i->i_sb)->sb_chk >= 2) {
  						printk("HPFS: warning: unbalanced dnode tree, see hpfs.txt 4 more info
  ");
  						printk("HPFS: warning: terminating balancing operation
  ");
  					}
  					hpfs_brelse4(&qbh1);
  					goto endm;
  				}
  				if (hpfs_sb(i->i_sb)->sb_chk >= 2) {
  					printk("HPFS: warning: unbalanced dnode tree, see hpfs.txt 4 more info
  ");
  					printk("HPFS: warning: goin'on
  ");
  				}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
661
  				del->length = cpu_to_le16(le16_to_cpu(del->length) + 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
662
  				del->down = 1;
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
663
  				d1->first_free = cpu_to_le32(le32_to_cpu(d1->first_free) + 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
665
  			}
  			if (dlp && !down) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
666
  				del->length = cpu_to_le16(le16_to_cpu(del->length) - 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
667
  				del->down = 0;
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
668
  				d1->first_free = cpu_to_le32(le32_to_cpu(d1->first_free) - 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
669
  			} else if (down)
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
670
  				*(dnode_secno *) ((void *) del + le16_to_cpu(del->length) - 4) = cpu_to_le32(down);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
  		} else goto endm;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
672
  		if (!(de_cp = kmalloc(le16_to_cpu(de_prev->length), GFP_NOFS))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
674
675
676
677
678
679
  			printk("HPFS: out of memory for dtree balancing
  ");
  			hpfs_brelse4(&qbh1);
  			goto endm;
  		}
  		hpfs_mark_4buffers_dirty(&qbh1);
  		hpfs_brelse4(&qbh1);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
680
  		memcpy(de_cp, de_prev, le16_to_cpu(de_prev->length));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
681
682
  		hpfs_delete_de(i->i_sb, dnode, de_prev);
  		if (!de_prev->down) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
683
  			de_prev->length = cpu_to_le16(le16_to_cpu(de_prev->length) + 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
684
  			de_prev->down = 1;
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
685
  			dnode->first_free = cpu_to_le32(le32_to_cpu(dnode->first_free) + 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686
  		}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
687
  		*(dnode_secno *) ((void *) de_prev + le16_to_cpu(de_prev->length) - 4) = cpu_to_le32(ndown);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
689
690
691
692
  		hpfs_mark_4buffers_dirty(&qbh);
  		hpfs_brelse4(&qbh);
  		for_all_poss(i, hpfs_pos_subst, ((loff_t)up << 4) | (p - 1), 4);
  		for_all_poss(i, hpfs_pos_subst, ((loff_t)up << 4) | p, ((loff_t)up << 4) | (p - 1));
  		if (down) if ((d1 = hpfs_map_dnode(i->i_sb, de_down_pointer(de), &qbh1))) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
693
  			d1->up = cpu_to_le32(ndown);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
  			hpfs_mark_4buffers_dirty(&qbh1);
  			hpfs_brelse4(&qbh1);
  		}
  		hpfs_add_to_dnode(i, ndown, de_cp->name, de_cp->namelen, de_cp, dlp);
  		dno = up;
  		kfree(de_cp);
  		goto try_it_again;
  	}
  	endm:
  	hpfs_mark_4buffers_dirty(&qbh);
  	end:
  	hpfs_brelse4(&qbh);
  }
  
  
  /* Delete dirent from directory */
  
  int hpfs_remove_dirent(struct inode *i, dnode_secno dno, struct hpfs_dirent *de,
  		       struct quad_buffer_head *qbh, int depth)
  {
  	struct dnode *dnode = qbh->data;
  	dnode_secno down = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
716
717
718
719
720
721
722
723
  	loff_t t;
  	if (de->first || de->last) {
  		hpfs_error(i->i_sb, "hpfs_remove_dirent: attempt to delete first or last dirent in dnode %08x", dno);
  		hpfs_brelse4(qbh);
  		return 1;
  	}
  	if (de->down) down = de_down_pointer(de);
  	if (depth && (de->down || (de == dnode_first_de(dnode) && de_next_de(de)->last))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
724
725
  		if (hpfs_check_free_dnodes(i->i_sb, FREE_DNODES_DEL)) {
  			hpfs_brelse4(qbh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726
727
728
729
730
731
732
733
734
735
736
737
  			return 2;
  		}
  	}
  	i->i_version++;
  	for_all_poss(i, hpfs_pos_del, (t = get_pos(dnode, de)) + 1, 1);
  	hpfs_delete_de(i->i_sb, dnode, de);
  	hpfs_mark_4buffers_dirty(qbh);
  	hpfs_brelse4(qbh);
  	if (down) {
  		dnode_secno a = move_to_top(i, down, dno);
  		for_all_poss(i, hpfs_pos_subst, 5, t);
  		if (a) delete_empty_dnode(i, a);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
738
739
740
  		return !a;
  	}
  	delete_empty_dnode(i, dno);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
  	return 0;
  }
  
  void hpfs_count_dnodes(struct super_block *s, dnode_secno dno, int *n_dnodes,
  		       int *n_subdirs, int *n_items)
  {
  	struct dnode *dnode;
  	struct quad_buffer_head qbh;
  	struct hpfs_dirent *de;
  	dnode_secno ptr, odno = 0;
  	int c1, c2 = 0;
  	int d1, d2 = 0;
  	go_down:
  	if (n_dnodes) (*n_dnodes)++;
  	if (hpfs_sb(s)->sb_chk)
  		if (hpfs_stop_cycles(s, dno, &c1, &c2, "hpfs_count_dnodes #1")) return;
  	ptr = 0;
  	go_up:
  	if (!(dnode = hpfs_map_dnode(s, dno, &qbh))) return;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
760
761
  	if (hpfs_sb(s)->sb_chk) if (odno && odno != -1 && le32_to_cpu(dnode->up) != odno)
  		hpfs_error(s, "hpfs_count_dnodes: bad up pointer; dnode %08x, down %08x points to %08x", odno, dno, le32_to_cpu(dnode->up));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
  	de = dnode_first_de(dnode);
  	if (ptr) while(1) {
  		if (de->down) if (de_down_pointer(de) == ptr) goto process_de;
  		if (de->last) {
  			hpfs_brelse4(&qbh);
  			hpfs_error(s, "hpfs_count_dnodes: pointer to dnode %08x not found in dnode %08x, got here from %08x",
  				ptr, dno, odno);
  			return;
  		}
  		de = de_next_de(de);
  	}
  	next_de:
  	if (de->down) {
  		odno = dno;
  		dno = de_down_pointer(de);
  		hpfs_brelse4(&qbh);
  		goto go_down;
  	}
  	process_de:
  	if (!de->first && !de->last && de->directory && n_subdirs) (*n_subdirs)++;
  	if (!de->first && !de->last && n_items) (*n_items)++;
  	if ((de = de_next_de(de)) < dnode_end_de(dnode)) goto next_de;
  	ptr = dno;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
785
  	dno = le32_to_cpu(dnode->up);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
  	if (dnode->root_dnode) {
  		hpfs_brelse4(&qbh);
  		return;
  	}
  	hpfs_brelse4(&qbh);
  	if (hpfs_sb(s)->sb_chk)
  		if (hpfs_stop_cycles(s, ptr, &d1, &d2, "hpfs_count_dnodes #2")) return;
  	odno = -1;
  	goto go_up;
  }
  
  static struct hpfs_dirent *map_nth_dirent(struct super_block *s, dnode_secno dno, int n,
  					  struct quad_buffer_head *qbh, struct dnode **dn)
  {
  	int i;
  	struct hpfs_dirent *de, *de_end;
  	struct dnode *dnode;
  	dnode = hpfs_map_dnode(s, dno, qbh);
  	if (!dnode) return NULL;
  	if (dn) *dn=dnode;
  	de = dnode_first_de(dnode);
  	de_end = dnode_end_de(dnode);
  	for (i = 1; de < de_end; i++, de = de_next_de(de)) {
  		if (i == n) {
  			return de;
  		}	
  		if (de->last) break;
  	}
  	hpfs_brelse4(qbh);
  	hpfs_error(s, "map_nth_dirent: n too high; dnode = %08x, requested %08x", dno, n);
  	return NULL;
  }
  
  dnode_secno hpfs_de_as_down_as_possible(struct super_block *s, dnode_secno dno)
  {
  	struct quad_buffer_head qbh;
  	dnode_secno d = dno;
  	dnode_secno up = 0;
  	struct hpfs_dirent *de;
  	int c1, c2 = 0;
  
  	again:
  	if (hpfs_sb(s)->sb_chk)
  		if (hpfs_stop_cycles(s, d, &c1, &c2, "hpfs_de_as_down_as_possible"))
  			return d;
  	if (!(de = map_nth_dirent(s, d, 1, &qbh, NULL))) return dno;
  	if (hpfs_sb(s)->sb_chk)
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
833
834
  		if (up && le32_to_cpu(((struct dnode *)qbh.data)->up) != up)
  			hpfs_error(s, "hpfs_de_as_down_as_possible: bad up pointer; dnode %08x, down %08x points to %08x", up, d, le32_to_cpu(((struct dnode *)qbh.data)->up));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
  	if (!de->down) {
  		hpfs_brelse4(&qbh);
  		return d;
  	}
  	up = d;
  	d = de_down_pointer(de);
  	hpfs_brelse4(&qbh);
  	goto again;
  }
  
  struct hpfs_dirent *map_pos_dirent(struct inode *inode, loff_t *posp,
  				   struct quad_buffer_head *qbh)
  {
  	loff_t pos;
  	unsigned c;
  	dnode_secno dno;
  	struct hpfs_dirent *de, *d;
  	struct hpfs_dirent *up_de;
  	struct hpfs_dirent *end_up_de;
  	struct dnode *dnode;
  	struct dnode *up_dnode;
  	struct quad_buffer_head qbh0;
  
  	pos = *posp;
  	dno = pos >> 6 << 2;
  	pos &= 077;
  	if (!(de = map_nth_dirent(inode->i_sb, dno, pos, qbh, &dnode)))
  		goto bail;
  
  	/* Going to the next dirent */
  	if ((d = de_next_de(de)) < dnode_end_de(dnode)) {
  		if (!(++*posp & 077)) {
18debbbcc   Randy Dunlap   [PATCH] hpfs: fix...
867
868
869
  			hpfs_error(inode->i_sb,
  				"map_pos_dirent: pos crossed dnode boundary; pos = %08llx",
  				(unsigned long long)*posp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
870
871
872
873
874
875
876
877
878
879
880
881
  			goto bail;
  		}
  		/* We're going down the tree */
  		if (d->down) {
  			*posp = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, de_down_pointer(d)) << 4) + 1;
  		}
  	
  		return de;
  	}
  
  	/* Going up */
  	if (dnode->root_dnode) goto bail;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
882
  	if (!(up_dnode = hpfs_map_dnode(inode->i_sb, le32_to_cpu(dnode->up), &qbh0)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
883
884
885
886
887
888
889
  		goto bail;
  
  	end_up_de = dnode_end_de(up_dnode);
  	c = 0;
  	for (up_de = dnode_first_de(up_dnode); up_de < end_up_de;
  	     up_de = de_next_de(up_de)) {
  		if (!(++c & 077)) hpfs_error(inode->i_sb,
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
890
  			"map_pos_dirent: pos crossed dnode boundary; dnode = %08x", le32_to_cpu(dnode->up));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
891
  		if (up_de->down && de_down_pointer(up_de) == dno) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
892
  			*posp = ((loff_t) le32_to_cpu(dnode->up) << 4) + c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
893
894
895
896
897
898
  			hpfs_brelse4(&qbh0);
  			return de;
  		}
  	}
  	
  	hpfs_error(inode->i_sb, "map_pos_dirent: pointer to dnode %08x not found in parent dnode %08x",
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
899
  		dno, le32_to_cpu(dnode->up));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
900
901
902
903
904
905
906
907
  	hpfs_brelse4(&qbh0);
  	
  	bail:
  	*posp = 12;
  	return de;
  }
  
  /* Find a dirent in tree */
7e7742ee0   Al Viro   sanitize signedne...
908
909
  struct hpfs_dirent *map_dirent(struct inode *inode, dnode_secno dno,
  			       const unsigned char *name, unsigned len,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  			       dnode_secno *dd, struct quad_buffer_head *qbh)
  {
  	struct dnode *dnode;
  	struct hpfs_dirent *de;
  	struct hpfs_dirent *de_end;
  	int c1, c2 = 0;
  
  	if (!S_ISDIR(inode->i_mode)) hpfs_error(inode->i_sb, "map_dirent: not a directory
  ");
  	again:
  	if (hpfs_sb(inode->i_sb)->sb_chk)
  		if (hpfs_stop_cycles(inode->i_sb, dno, &c1, &c2, "map_dirent")) return NULL;
  	if (!(dnode = hpfs_map_dnode(inode->i_sb, dno, qbh))) return NULL;
  	
  	de_end = dnode_end_de(dnode);
  	for (de = dnode_first_de(dnode); de < de_end; de = de_next_de(de)) {
  		int t = hpfs_compare_names(inode->i_sb, name, len, de->name, de->namelen, de->last);
  		if (!t) {
  			if (dd) *dd = dno;
  			return de;
  		}
  		if (t < 0) {
  			if (de->down) {
  				dno = de_down_pointer(de);
  				hpfs_brelse4(qbh);
  				goto again;
  			}
  		break;
  		}
  	}
  	hpfs_brelse4(qbh);
  	return NULL;
  }
  
  /*
   * Remove empty directory. In normal cases it is only one dnode with two
   * entries, but we must handle also such obscure cases when it's a tree
   * of empty dnodes.
   */
  
  void hpfs_remove_dtree(struct super_block *s, dnode_secno dno)
  {
  	struct quad_buffer_head qbh;
  	struct dnode *dnode;
  	struct hpfs_dirent *de;
  	dnode_secno d1, d2, rdno = dno;
  	while (1) {
  		if (!(dnode = hpfs_map_dnode(s, dno, &qbh))) return;
  		de = dnode_first_de(dnode);
  		if (de->last) {
  			if (de->down) d1 = de_down_pointer(de);
  			else goto error;
  			hpfs_brelse4(&qbh);
  			hpfs_free_dnode(s, dno);
  			dno = d1;
  		} else break;
  	}
  	if (!de->first) goto error;
  	d1 = de->down ? de_down_pointer(de) : 0;
  	de = de_next_de(de);
  	if (!de->last) goto error;
  	d2 = de->down ? de_down_pointer(de) : 0;
  	hpfs_brelse4(&qbh);
  	hpfs_free_dnode(s, dno);
  	do {
  		while (d1) {
  			if (!(dnode = hpfs_map_dnode(s, dno = d1, &qbh))) return;
  			de = dnode_first_de(dnode);
  			if (!de->last) goto error;
  			d1 = de->down ? de_down_pointer(de) : 0;
  			hpfs_brelse4(&qbh);
  			hpfs_free_dnode(s, dno);
  		}
  		d1 = d2;
  		d2 = 0;
  	} while (d1);
  	return;
  	error:
  	hpfs_brelse4(&qbh);
  	hpfs_free_dnode(s, dno);
  	hpfs_error(s, "directory %08x is corrupted or not empty", rdno);
  }
  
  /* 
   * Find dirent for specified fnode. Use truncated 15-char name in fnode as
   * a help for searching.
   */
  
  struct hpfs_dirent *map_fnode_dirent(struct super_block *s, fnode_secno fno,
  				     struct fnode *f, struct quad_buffer_head *qbh)
  {
7e7742ee0   Al Viro   sanitize signedne...
1001
1002
  	unsigned char *name1;
  	unsigned char *name2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
  	int name1len, name2len;
  	struct dnode *d;
  	dnode_secno dno, downd;
  	struct fnode *upf;
  	struct buffer_head *bh;
  	struct hpfs_dirent *de, *de_end;
  	int c;
  	int c1, c2 = 0;
  	int d1, d2 = 0;
  	name1 = f->name;
  	if (!(name2 = kmalloc(256, GFP_NOFS))) {
  		printk("HPFS: out of memory, can't map dirent
  ");
  		return NULL;
  	}
  	if (f->len <= 15)
  		memcpy(name2, name1, name1len = name2len = f->len);
  	else {
  		memcpy(name2, name1, 15);
  		memset(name2 + 15, 0xff, 256 - 15);
  		/*name2[15] = 0xff;*/
  		name1len = 15; name2len = 256;
  	}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
1026
  	if (!(upf = hpfs_map_fnode(s, le32_to_cpu(f->up), &bh))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1027
1028
1029
1030
1031
  		kfree(name2);
  		return NULL;
  	}	
  	if (!upf->dirflag) {
  		brelse(bh);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
1032
  		hpfs_error(s, "fnode %08x has non-directory parent %08x", fno, le32_to_cpu(f->up));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1033
1034
1035
  		kfree(name2);
  		return NULL;
  	}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
1036
  	dno = le32_to_cpu(upf->u.external[0].disk_secno);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
  	brelse(bh);
  	go_down:
  	downd = 0;
  	go_up:
  	if (!(d = hpfs_map_dnode(s, dno, qbh))) {
  		kfree(name2);
  		return NULL;
  	}
  	de_end = dnode_end_de(d);
  	de = dnode_first_de(d);
  	if (downd) {
  		while (de < de_end) {
  			if (de->down) if (de_down_pointer(de) == downd) goto f;
  			de = de_next_de(de);
  		}
  		hpfs_error(s, "pointer to dnode %08x not found in dnode %08x", downd, dno);
  		hpfs_brelse4(qbh);
  		kfree(name2);
  		return NULL;
  	}
  	next_de:
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
1058
  	if (le32_to_cpu(de->fnode) == fno) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
  		kfree(name2);
  		return de;
  	}
  	c = hpfs_compare_names(s, name1, name1len, de->name, de->namelen, de->last);
  	if (c < 0 && de->down) {
  		dno = de_down_pointer(de);
  		hpfs_brelse4(qbh);
  		if (hpfs_sb(s)->sb_chk)
  			if (hpfs_stop_cycles(s, dno, &c1, &c2, "map_fnode_dirent #1")) {
  			kfree(name2);
  			return NULL;
  		}
  		goto go_down;
  	}
  	f:
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
1074
  	if (le32_to_cpu(de->fnode) == fno) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1075
1076
1077
1078
1079
1080
1081
1082
  		kfree(name2);
  		return de;
  	}
  	c = hpfs_compare_names(s, name2, name2len, de->name, de->namelen, de->last);
  	if (c < 0 && !de->last) goto not_found;
  	if ((de = de_next_de(de)) < de_end) goto next_de;
  	if (d->root_dnode) goto not_found;
  	downd = dno;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
1083
  	dno = le32_to_cpu(d->up);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
  	hpfs_brelse4(qbh);
  	if (hpfs_sb(s)->sb_chk)
  		if (hpfs_stop_cycles(s, downd, &d1, &d2, "map_fnode_dirent #2")) {
  			kfree(name2);
  			return NULL;
  		}
  	goto go_up;
  	not_found:
  	hpfs_brelse4(qbh);
  	hpfs_error(s, "dirent for fnode %08x not found", fno);
  	kfree(name2);
  	return NULL;
  }