[GFS2] Some further style changes
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / gfs2 / dir.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
b3b94faa
DT
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
10/*
61e085a8
SW
11 * Implements Extendible Hashing as described in:
12 * "Extendible Hashing" by Fagin, et al in
13 * __ACM Trans. on Database Systems__, Sept 1979.
14 *
15 *
16 * Here's the layout of dirents which is essentially the same as that of ext2
17 * within a single block. The field de_name_len is the number of bytes
18 * actually required for the name (no null terminator). The field de_rec_len
19 * is the number of bytes allocated to the dirent. The offset of the next
20 * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
21 * deleted, the preceding dirent inherits its allocated space, ie
22 * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
23 * by adding de_rec_len to the current dirent, this essentially causes the
24 * deleted dirent to get jumped over when iterating through all the dirents.
25 *
26 * When deleting the first dirent in a block, there is no previous dirent so
27 * the field de_ino is set to zero to designate it as deleted. When allocating
28 * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
29 * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
30 * dirent is allocated. Otherwise it must go through all the 'used' dirents
31 * searching for one in which the amount of total space minus the amount of
32 * used space will provide enough space for the new dirent.
33 *
34 * There are two types of blocks in which dirents reside. In a stuffed dinode,
35 * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
36 * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
37 * beginning of the leaf block. The dirents reside in leaves when
38 *
39 * dip->i_di.di_flags & GFS2_DIF_EXHASH is true
40 *
41 * Otherwise, the dirents are "linear", within a single stuffed dinode block.
42 *
43 * When the dirents are in leaves, the actual contents of the directory file are
44 * used as an array of 64-bit block pointers pointing to the leaf blocks. The
45 * dirents are NOT in the directory file itself. There can be more than one
46 * block pointer in the array that points to the same leaf. In fact, when a
47 * directory is first converted from linear to exhash, all of the pointers
48 * point to the same leaf.
49 *
50 * When a leaf is completely full, the size of the hash table can be
51 * doubled unless it is already at the maximum size which is hard coded into
52 * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
53 * but never before the maximum hash table size has been reached.
54 */
b3b94faa
DT
55
56#include <linux/sched.h>
57#include <linux/slab.h>
58#include <linux/spinlock.h>
b3b94faa
DT
59#include <linux/buffer_head.h>
60#include <linux/sort.h>
5c676f6d 61#include <linux/gfs2_ondisk.h>
71b86f56 62#include <linux/crc32.h>
fe1bdedc 63#include <linux/vmalloc.h>
b3b94faa
DT
64
65#include "gfs2.h"
5c676f6d
SW
66#include "lm_interface.h"
67#include "incore.h"
b3b94faa
DT
68#include "dir.h"
69#include "glock.h"
70#include "inode.h"
b3b94faa
DT
71#include "meta_io.h"
72#include "quota.h"
73#include "rgrp.h"
74#include "trans.h"
e13940ba 75#include "bmap.h"
5c676f6d 76#include "util.h"
b3b94faa
DT
77
78#define IS_LEAF 1 /* Hashed (leaf) directory */
79#define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
80
cd915493
SW
81#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
82#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
b3b94faa
DT
83
84typedef int (*leaf_call_t) (struct gfs2_inode *dip,
cd915493 85 u32 index, u32 len, u64 leaf_no,
b3b94faa
DT
86 void *data);
87
61e085a8 88
cd915493 89int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
61e085a8 90 struct buffer_head **bhp)
e13940ba
SW
91{
92 struct buffer_head *bh;
e13940ba 93
61e085a8
SW
94 bh = gfs2_meta_new(ip->i_gl, block);
95 gfs2_trans_add_bh(ip->i_gl, bh, 1);
96 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
97 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
e13940ba
SW
98 *bhp = bh;
99 return 0;
100}
101
cd915493 102static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
61e085a8
SW
103 struct buffer_head **bhp)
104{
105 struct buffer_head *bh;
106 int error;
e13940ba 107
61e085a8
SW
108 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT, &bh);
109 if (error)
110 return error;
feaa7bba 111 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
61e085a8
SW
112 brelse(bh);
113 return -EIO;
114 }
115 *bhp = bh;
116 return 0;
117}
e13940ba
SW
118
119static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
120 unsigned int offset, unsigned int size)
121
122{
123 struct buffer_head *dibh;
124 int error;
125
126 error = gfs2_meta_inode_buffer(ip, &dibh);
127 if (error)
128 return error;
129
130 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
c752666c 131 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
e13940ba
SW
132 if (ip->i_di.di_size < offset + size)
133 ip->i_di.di_size = offset + size;
134 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
135 gfs2_dinode_out(&ip->i_di, dibh->b_data);
136
137 brelse(dibh);
138
139 return size;
140}
141
142
143
144/**
145 * gfs2_dir_write_data - Write directory information to the inode
146 * @ip: The GFS2 inode
147 * @buf: The buffer containing information to be written
148 * @offset: The file offset to start writing at
149 * @size: The amount of data to write
150 *
151 * Returns: The number of bytes correctly written or error code
152 */
153static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
cd915493 154 u64 offset, unsigned int size)
e13940ba 155{
feaa7bba 156 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
e13940ba 157 struct buffer_head *dibh;
cd915493
SW
158 u64 lblock, dblock;
159 u32 extlen = 0;
e13940ba
SW
160 unsigned int o;
161 int copied = 0;
162 int error = 0;
163
164 if (!size)
165 return 0;
166
167 if (gfs2_is_stuffed(ip) &&
168 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
568f4c96
SW
169 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
170 size);
e13940ba
SW
171
172 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
173 return -EINVAL;
174
175 if (gfs2_is_stuffed(ip)) {
f25ef0c1 176 error = gfs2_unstuff_dinode(ip, NULL);
e13940ba 177 if (error)
c752666c 178 return error;
e13940ba
SW
179 }
180
181 lblock = offset;
182 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
183
184 while (copied < size) {
185 unsigned int amount;
186 struct buffer_head *bh;
187 int new;
188
189 amount = size - copied;
190 if (amount > sdp->sd_sb.sb_bsize - o)
191 amount = sdp->sd_sb.sb_bsize - o;
192
193 if (!extlen) {
194 new = 1;
feaa7bba 195 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
fd88de56 196 &dblock, &extlen);
e13940ba
SW
197 if (error)
198 goto fail;
199 error = -EIO;
200 if (gfs2_assert_withdraw(sdp, dblock))
201 goto fail;
202 }
203
61e085a8
SW
204 if (amount == sdp->sd_jbsize || new)
205 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
206 else
207 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
208
e13940ba
SW
209 if (error)
210 goto fail;
211
212 gfs2_trans_add_bh(ip->i_gl, bh, 1);
213 memcpy(bh->b_data + o, buf, amount);
214 brelse(bh);
215 if (error)
216 goto fail;
217
899bb264 218 buf += amount;
e13940ba
SW
219 copied += amount;
220 lblock++;
221 dblock++;
222 extlen--;
223
224 o = sizeof(struct gfs2_meta_header);
225 }
226
227out:
228 error = gfs2_meta_inode_buffer(ip, &dibh);
229 if (error)
230 return error;
231
232 if (ip->i_di.di_size < offset + copied)
233 ip->i_di.di_size = offset + copied;
234 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
235
236 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
237 gfs2_dinode_out(&ip->i_di, dibh->b_data);
238 brelse(dibh);
239
240 return copied;
241fail:
242 if (copied)
243 goto out;
244 return error;
245}
246
247static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
c752666c 248 unsigned int offset, unsigned int size)
e13940ba
SW
249{
250 struct buffer_head *dibh;
251 int error;
252
253 error = gfs2_meta_inode_buffer(ip, &dibh);
254 if (!error) {
255 offset += sizeof(struct gfs2_dinode);
256 memcpy(buf, dibh->b_data + offset, size);
257 brelse(dibh);
258 }
259
260 return (error) ? error : size;
261}
262
263
264/**
265 * gfs2_dir_read_data - Read a data from a directory inode
266 * @ip: The GFS2 Inode
267 * @buf: The buffer to place result into
268 * @offset: File offset to begin jdata_readng from
269 * @size: Amount of data to transfer
270 *
271 * Returns: The amount of data actually copied or the error
272 */
273static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
cd915493 274 u64 offset, unsigned int size)
e13940ba 275{
feaa7bba 276 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
cd915493
SW
277 u64 lblock, dblock;
278 u32 extlen = 0;
e13940ba
SW
279 unsigned int o;
280 int copied = 0;
281 int error = 0;
282
283 if (offset >= ip->i_di.di_size)
284 return 0;
285
286 if ((offset + size) > ip->i_di.di_size)
287 size = ip->i_di.di_size - offset;
288
289 if (!size)
290 return 0;
291
292 if (gfs2_is_stuffed(ip))
568f4c96
SW
293 return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset,
294 size);
e13940ba
SW
295
296 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
297 return -EINVAL;
298
299 lblock = offset;
300 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
301
302 while (copied < size) {
303 unsigned int amount;
304 struct buffer_head *bh;
305 int new;
306
307 amount = size - copied;
308 if (amount > sdp->sd_sb.sb_bsize - o)
309 amount = sdp->sd_sb.sb_bsize - o;
310
311 if (!extlen) {
312 new = 0;
feaa7bba 313 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
fd88de56 314 &dblock, &extlen);
e13940ba
SW
315 if (error)
316 goto fail;
317 }
318
319 if (extlen > 1)
320 gfs2_meta_ra(ip->i_gl, dblock, extlen);
321
322 if (dblock) {
61e085a8
SW
323 if (new)
324 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
325 else
326 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
e13940ba
SW
327 if (error)
328 goto fail;
329 dblock++;
330 extlen--;
331 } else
332 bh = NULL;
333
334 memcpy(buf, bh->b_data + o, amount);
335 brelse(bh);
336 if (error)
337 goto fail;
338
899bb264 339 buf += amount;
e13940ba
SW
340 copied += amount;
341 lblock++;
342
343 o = sizeof(struct gfs2_meta_header);
344 }
345
346 return copied;
347fail:
348 return (copied) ? copied : error;
349}
350
c752666c 351typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
71b86f56
SW
352 const struct qstr *name,
353 void *opaque);
c752666c
SW
354
355static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
356 const struct qstr *name, int ret)
357{
358 if (dent->de_inum.no_addr != 0 &&
359 be32_to_cpu(dent->de_hash) == name->hash &&
360 be16_to_cpu(dent->de_name_len) == name->len &&
361 memcmp((char *)(dent+1), name->name, name->len) == 0)
362 return ret;
363 return 0;
364}
365
366static int gfs2_dirent_find(const struct gfs2_dirent *dent,
71b86f56
SW
367 const struct qstr *name,
368 void *opaque)
c752666c
SW
369{
370 return __gfs2_dirent_find(dent, name, 1);
371}
372
373static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
71b86f56
SW
374 const struct qstr *name,
375 void *opaque)
c752666c
SW
376{
377 return __gfs2_dirent_find(dent, name, 2);
378}
379
380/*
381 * name->name holds ptr to start of block.
382 * name->len holds size of block.
b3b94faa 383 */
c752666c 384static int gfs2_dirent_last(const struct gfs2_dirent *dent,
71b86f56
SW
385 const struct qstr *name,
386 void *opaque)
c752666c
SW
387{
388 const char *start = name->name;
389 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
390 if (name->len == (end - start))
391 return 1;
392 return 0;
393}
b3b94faa 394
c752666c 395static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
71b86f56
SW
396 const struct qstr *name,
397 void *opaque)
b3b94faa 398{
c752666c
SW
399 unsigned required = GFS2_DIRENT_SIZE(name->len);
400 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
401 unsigned totlen = be16_to_cpu(dent->de_rec_len);
402
71b86f56
SW
403 if (!dent->de_inum.no_addr)
404 actual = GFS2_DIRENT_SIZE(0);
c752666c
SW
405 if ((totlen - actual) >= required)
406 return 1;
407 return 0;
408}
409
71b86f56
SW
410struct dirent_gather {
411 const struct gfs2_dirent **pdent;
412 unsigned offset;
413};
414
415static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
416 const struct qstr *name,
417 void *opaque)
418{
419 struct dirent_gather *g = opaque;
420 if (dent->de_inum.no_addr) {
421 g->pdent[g->offset++] = dent;
422 }
423 return 0;
424}
425
c752666c
SW
426/*
427 * Other possible things to check:
428 * - Inode located within filesystem size (and on valid block)
429 * - Valid directory entry type
430 * Not sure how heavy-weight we want to make this... could also check
431 * hash is correct for example, but that would take a lot of extra time.
432 * For now the most important thing is to check that the various sizes
433 * are correct.
434 */
435static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
436 unsigned int size, unsigned int len, int first)
437{
438 const char *msg = "gfs2_dirent too small";
439 if (unlikely(size < sizeof(struct gfs2_dirent)))
440 goto error;
441 msg = "gfs2_dirent misaligned";
442 if (unlikely(offset & 0x7))
443 goto error;
444 msg = "gfs2_dirent points beyond end of block";
445 if (unlikely(offset + size > len))
446 goto error;
447 msg = "zero inode number";
448 if (unlikely(!first && !dent->de_inum.no_addr))
449 goto error;
450 msg = "name length is greater than space in dirent";
451 if (dent->de_inum.no_addr &&
452 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
453 size))
454 goto error;
455 return 0;
456error:
457 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
458 first ? "first in block" : "not first in block");
459 return -EIO;
b3b94faa
DT
460}
461
71b86f56 462static int gfs2_dirent_offset(const void *buf)
c752666c 463{
71b86f56
SW
464 const struct gfs2_meta_header *h = buf;
465 int offset;
c752666c
SW
466
467 BUG_ON(buf == NULL);
c752666c 468
e3167ded 469 switch(be32_to_cpu(h->mh_type)) {
c752666c
SW
470 case GFS2_METATYPE_LF:
471 offset = sizeof(struct gfs2_leaf);
472 break;
473 case GFS2_METATYPE_DI:
474 offset = sizeof(struct gfs2_dinode);
475 break;
476 default:
477 goto wrong_type;
478 }
71b86f56
SW
479 return offset;
480wrong_type:
481 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
e3167ded 482 be32_to_cpu(h->mh_type));
71b86f56
SW
483 return -1;
484}
485
486static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
487 void *buf,
488 unsigned int len, gfs2_dscan_t scan,
489 const struct qstr *name,
490 void *opaque)
491{
492 struct gfs2_dirent *dent, *prev;
493 unsigned offset;
494 unsigned size;
495 int ret = 0;
c752666c 496
71b86f56
SW
497 ret = gfs2_dirent_offset(buf);
498 if (ret < 0)
499 goto consist_inode;
500
501 offset = ret;
c752666c
SW
502 prev = NULL;
503 dent = (struct gfs2_dirent *)(buf + offset);
504 size = be16_to_cpu(dent->de_rec_len);
505 if (gfs2_check_dirent(dent, offset, size, len, 1))
506 goto consist_inode;
507 do {
71b86f56 508 ret = scan(dent, name, opaque);
c752666c
SW
509 if (ret)
510 break;
511 offset += size;
512 if (offset == len)
513 break;
514 prev = dent;
515 dent = (struct gfs2_dirent *)(buf + offset);
516 size = be16_to_cpu(dent->de_rec_len);
517 if (gfs2_check_dirent(dent, offset, size, len, 0))
518 goto consist_inode;
519 } while(1);
520
521 switch(ret) {
522 case 0:
523 return NULL;
524 case 1:
525 return dent;
526 case 2:
527 return prev ? prev : dent;
528 default:
529 BUG_ON(ret > 0);
530 return ERR_PTR(ret);
531 }
532
c752666c 533consist_inode:
feaa7bba 534 gfs2_consist_inode(GFS2_I(inode));
c752666c
SW
535 return ERR_PTR(-EIO);
536}
537
538
b3b94faa
DT
539/**
540 * dirent_first - Return the first dirent
541 * @dip: the directory
542 * @bh: The buffer
543 * @dent: Pointer to list of dirents
544 *
545 * return first dirent whether bh points to leaf or stuffed dinode
546 *
547 * Returns: IS_LEAF, IS_DINODE, or -errno
548 */
549
550static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
551 struct gfs2_dirent **dent)
552{
553 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
554
e3167ded 555 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
feaa7bba 556 if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
b3b94faa
DT
557 return -EIO;
558 *dent = (struct gfs2_dirent *)(bh->b_data +
559 sizeof(struct gfs2_leaf));
560 return IS_LEAF;
561 } else {
feaa7bba 562 if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
b3b94faa
DT
563 return -EIO;
564 *dent = (struct gfs2_dirent *)(bh->b_data +
565 sizeof(struct gfs2_dinode));
566 return IS_DINODE;
567 }
568}
569
570/**
571 * dirent_next - Next dirent
572 * @dip: the directory
573 * @bh: The buffer
574 * @dent: Pointer to list of dirents
575 *
576 * Returns: 0 on success, error code otherwise
577 */
578
579static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
580 struct gfs2_dirent **dent)
581{
582 struct gfs2_dirent *tmp, *cur;
583 char *bh_end;
cd915493 584 u16 cur_rec_len;
b3b94faa
DT
585
586 cur = *dent;
587 bh_end = bh->b_data + bh->b_size;
fc69d0d3 588 cur_rec_len = be16_to_cpu(cur->de_rec_len);
b3b94faa
DT
589
590 if ((char *)cur + cur_rec_len >= bh_end) {
591 if ((char *)cur + cur_rec_len > bh_end) {
592 gfs2_consist_inode(dip);
593 return -EIO;
594 }
595 return -ENOENT;
596 }
597
598 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
599
fc69d0d3 600 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
b3b94faa
DT
601 gfs2_consist_inode(dip);
602 return -EIO;
603 }
4dd651ad
SW
604
605 if (cur_rec_len == 0) {
606 gfs2_consist_inode(dip);
607 return -EIO;
608 }
609
b3b94faa
DT
610 /* Only the first dent could ever have de_inum.no_addr == 0 */
611 if (!tmp->de_inum.no_addr) {
612 gfs2_consist_inode(dip);
613 return -EIO;
614 }
615
616 *dent = tmp;
617
618 return 0;
619}
620
621/**
622 * dirent_del - Delete a dirent
623 * @dip: The GFS2 inode
624 * @bh: The buffer
625 * @prev: The previous dirent
626 * @cur: The current dirent
627 *
628 */
629
630static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
631 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
632{
cd915493 633 u16 cur_rec_len, prev_rec_len;
b3b94faa
DT
634
635 if (!cur->de_inum.no_addr) {
636 gfs2_consist_inode(dip);
637 return;
638 }
639
d4e9c4c3 640 gfs2_trans_add_bh(dip->i_gl, bh, 1);
b3b94faa
DT
641
642 /* If there is no prev entry, this is the first entry in the block.
643 The de_rec_len is already as big as it needs to be. Just zero
644 out the inode number and return. */
645
646 if (!prev) {
647 cur->de_inum.no_addr = 0; /* No endianess worries */
648 return;
649 }
650
651 /* Combine this dentry with the previous one. */
652
fc69d0d3
SW
653 prev_rec_len = be16_to_cpu(prev->de_rec_len);
654 cur_rec_len = be16_to_cpu(cur->de_rec_len);
b3b94faa
DT
655
656 if ((char *)prev + prev_rec_len != (char *)cur)
657 gfs2_consist_inode(dip);
658 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
659 gfs2_consist_inode(dip);
660
661 prev_rec_len += cur_rec_len;
fc69d0d3 662 prev->de_rec_len = cpu_to_be16(prev_rec_len);
b3b94faa
DT
663}
664
c752666c
SW
665/*
666 * Takes a dent from which to grab space as an argument. Returns the
667 * newly created dent.
b3b94faa 668 */
08bc2dbc
AB
669static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
670 struct gfs2_dirent *dent,
671 const struct qstr *name,
672 struct buffer_head *bh)
b3b94faa 673{
feaa7bba 674 struct gfs2_inode *ip = GFS2_I(inode);
c752666c
SW
675 struct gfs2_dirent *ndent;
676 unsigned offset = 0, totlen;
677
678 if (dent->de_inum.no_addr)
679 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
680 totlen = be16_to_cpu(dent->de_rec_len);
681 BUG_ON(offset + name->len > totlen);
682 gfs2_trans_add_bh(ip->i_gl, bh, 1);
683 ndent = (struct gfs2_dirent *)((char *)dent + offset);
684 dent->de_rec_len = cpu_to_be16(offset);
685 gfs2_qstr2dirent(name, totlen - offset, ndent);
686 return ndent;
b3b94faa
DT
687}
688
c752666c
SW
689static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
690 struct buffer_head *bh,
691 const struct qstr *name)
b3b94faa
DT
692{
693 struct gfs2_dirent *dent;
71b86f56
SW
694 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
695 gfs2_dirent_find_space, name, NULL);
c752666c
SW
696 if (!dent || IS_ERR(dent))
697 return dent;
698 return gfs2_init_dirent(inode, dent, name, bh);
b3b94faa
DT
699}
700
cd915493 701static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
b3b94faa
DT
702 struct buffer_head **bhp)
703{
704 int error;
705
706 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
feaa7bba
SW
707 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
708 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
b3b94faa 709 error = -EIO;
feaa7bba 710 }
b3b94faa
DT
711
712 return error;
713}
714
715/**
716 * get_leaf_nr - Get a leaf number associated with the index
717 * @dip: The GFS2 inode
718 * @index:
719 * @leaf_out:
720 *
721 * Returns: 0 on success, error code otherwise
722 */
723
cd915493
SW
724static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
725 u64 *leaf_out)
b3b94faa 726{
cd915493 727 u64 leaf_no;
b3b94faa
DT
728 int error;
729
e13940ba 730 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
cd915493
SW
731 index * sizeof(u64),
732 sizeof(u64));
733 if (error != sizeof(u64))
b3b94faa
DT
734 return (error < 0) ? error : -EIO;
735
736 *leaf_out = be64_to_cpu(leaf_no);
737
738 return 0;
739}
740
cd915493 741static int get_first_leaf(struct gfs2_inode *dip, u32 index,
b3b94faa
DT
742 struct buffer_head **bh_out)
743{
cd915493 744 u64 leaf_no;
b3b94faa
DT
745 int error;
746
747 error = get_leaf_nr(dip, index, &leaf_no);
748 if (!error)
749 error = get_leaf(dip, leaf_no, bh_out);
750
751 return error;
752}
753
c752666c
SW
754static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
755 const struct qstr *name,
756 gfs2_dscan_t scan,
757 struct buffer_head **pbh)
b3b94faa 758{
c752666c
SW
759 struct buffer_head *bh;
760 struct gfs2_dirent *dent;
feaa7bba 761 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
762 int error;
763
c752666c
SW
764 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
765 struct gfs2_leaf *leaf;
766 unsigned hsize = 1 << ip->i_di.di_depth;
767 unsigned index;
768 u64 ln;
769 if (hsize * sizeof(u64) != ip->i_di.di_size) {
770 gfs2_consist_inode(ip);
771 return ERR_PTR(-EIO);
772 }
feaa7bba 773
c752666c
SW
774 index = name->hash >> (32 - ip->i_di.di_depth);
775 error = get_first_leaf(ip, index, &bh);
776 if (error)
777 return ERR_PTR(error);
778 do {
779 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
71b86f56 780 scan, name, NULL);
c752666c
SW
781 if (dent)
782 goto got_dent;
783 leaf = (struct gfs2_leaf *)bh->b_data;
784 ln = be64_to_cpu(leaf->lf_next);
f4154ea0 785 brelse(bh);
c752666c
SW
786 if (!ln)
787 break;
feaa7bba 788
c752666c
SW
789 error = get_leaf(ip, ln, &bh);
790 } while(!error);
b3b94faa 791
c752666c 792 return error ? ERR_PTR(error) : NULL;
b3b94faa 793 }
b3b94faa 794
feaa7bba 795
c752666c
SW
796 error = gfs2_meta_inode_buffer(ip, &bh);
797 if (error)
798 return ERR_PTR(error);
71b86f56 799 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
c752666c 800got_dent:
f4154ea0 801 if (unlikely(dent == NULL || IS_ERR(dent))) {
ed386507
SW
802 brelse(bh);
803 bh = NULL;
804 }
c752666c
SW
805 *pbh = bh;
806 return dent;
807}
b3b94faa 808
c752666c
SW
809static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
810{
feaa7bba 811 struct gfs2_inode *ip = GFS2_I(inode);
c752666c
SW
812 u64 bn = gfs2_alloc_meta(ip);
813 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
814 struct gfs2_leaf *leaf;
815 struct gfs2_dirent *dent;
71b86f56 816 struct qstr name = { .name = "", .len = 0, .hash = 0 };
c752666c
SW
817 if (!bh)
818 return NULL;
feaa7bba 819
c752666c
SW
820 gfs2_trans_add_bh(ip->i_gl, bh, 1);
821 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
822 leaf = (struct gfs2_leaf *)bh->b_data;
823 leaf->lf_depth = cpu_to_be16(depth);
824 leaf->lf_entries = cpu_to_be16(0);
825 leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
826 leaf->lf_next = cpu_to_be64(0);
827 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
828 dent = (struct gfs2_dirent *)(leaf+1);
71b86f56 829 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
c752666c
SW
830 *pbh = bh;
831 return leaf;
b3b94faa
DT
832}
833
834/**
835 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
836 * @dip: The GFS2 inode
837 *
838 * Returns: 0 on success, error code otherwise
839 */
840
c752666c 841static int dir_make_exhash(struct inode *inode)
b3b94faa 842{
feaa7bba
SW
843 struct gfs2_inode *dip = GFS2_I(inode);
844 struct gfs2_sbd *sdp = GFS2_SB(inode);
b3b94faa 845 struct gfs2_dirent *dent;
c752666c 846 struct qstr args;
b3b94faa
DT
847 struct buffer_head *bh, *dibh;
848 struct gfs2_leaf *leaf;
849 int y;
cd915493
SW
850 u32 x;
851 u64 *lp, bn;
b3b94faa
DT
852 int error;
853
854 error = gfs2_meta_inode_buffer(dip, &dibh);
855 if (error)
856 return error;
857
b3b94faa
DT
858 /* Turn over a new leaf */
859
c752666c
SW
860 leaf = new_leaf(inode, &bh, 0);
861 if (!leaf)
862 return -ENOSPC;
863 bn = bh->b_blocknr;
b3b94faa
DT
864
865 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
b3b94faa
DT
866 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
867
868 /* Copy dirents */
869
870 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
871 sizeof(struct gfs2_dinode));
872
873 /* Find last entry */
874
875 x = 0;
c752666c
SW
876 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
877 sizeof(struct gfs2_leaf);
878 args.name = bh->b_data;
feaa7bba 879 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
71b86f56 880 gfs2_dirent_last, &args, NULL);
c752666c
SW
881 if (!dent) {
882 brelse(bh);
883 brelse(dibh);
884 return -EIO;
885 }
886 if (IS_ERR(dent)) {
887 brelse(bh);
888 brelse(dibh);
889 return PTR_ERR(dent);
b3b94faa 890 }
b3b94faa
DT
891
892 /* Adjust the last dirent's record length
893 (Remember that dent still points to the last entry.) */
894
4dd651ad 895 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
b3b94faa 896 sizeof(struct gfs2_dinode) -
4dd651ad 897 sizeof(struct gfs2_leaf));
b3b94faa
DT
898
899 brelse(bh);
900
901 /* We're done with the new leaf block, now setup the new
902 hash table. */
903
d4e9c4c3 904 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
b3b94faa
DT
905 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
906
cd915493 907 lp = (u64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
b3b94faa
DT
908
909 for (x = sdp->sd_hash_ptrs; x--; lp++)
910 *lp = cpu_to_be64(bn);
911
912 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
913 dip->i_di.di_blocks++;
914 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
915 dip->i_di.di_payload_format = 0;
916
917 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
918 dip->i_di.di_depth = y;
919
920 gfs2_dinode_out(&dip->i_di, dibh->b_data);
921
922 brelse(dibh);
923
924 return 0;
925}
926
927/**
928 * dir_split_leaf - Split a leaf block into two
929 * @dip: The GFS2 inode
930 * @index:
931 * @leaf_no:
932 *
933 * Returns: 0 on success, error code on failure
934 */
935
c752666c 936static int dir_split_leaf(struct inode *inode, const struct qstr *name)
b3b94faa 937{
feaa7bba 938 struct gfs2_inode *dip = GFS2_I(inode);
b3b94faa
DT
939 struct buffer_head *nbh, *obh, *dibh;
940 struct gfs2_leaf *nleaf, *oleaf;
4da3c646 941 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
cd915493
SW
942 u32 start, len, half_len, divider;
943 u64 bn, *lp, leaf_no;
944 u32 index;
b3b94faa
DT
945 int x, moved = 0;
946 int error;
947
c752666c
SW
948 index = name->hash >> (32 - dip->i_di.di_depth);
949 error = get_leaf_nr(dip, index, &leaf_no);
950 if (error)
951 return error;
b3b94faa
DT
952
953 /* Get the old leaf block */
b3b94faa
DT
954 error = get_leaf(dip, leaf_no, &obh);
955 if (error)
e90deff5 956 return error;
b3b94faa 957
b3b94faa 958 oleaf = (struct gfs2_leaf *)obh->b_data;
e90deff5
SW
959 if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
960 brelse(obh);
961 return 1; /* can't split */
962 }
963
964 gfs2_trans_add_bh(dip->i_gl, obh, 1);
b3b94faa 965
c752666c
SW
966 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
967 if (!nleaf) {
968 brelse(obh);
969 return -ENOSPC;
970 }
971 bn = nbh->b_blocknr;
b3b94faa 972
c752666c 973 /* Compute the start and len of leaf pointers in the hash table. */
b3b94faa
DT
974 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
975 half_len = len >> 1;
976 if (!half_len) {
e90deff5 977 printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
b3b94faa
DT
978 gfs2_consist_inode(dip);
979 error = -EIO;
980 goto fail_brelse;
981 }
982
983 start = (index & ~(len - 1));
984
985 /* Change the pointers.
986 Don't bother distinguishing stuffed from non-stuffed.
987 This code is complicated enough already. */
cd915493 988 lp = kmalloc(half_len * sizeof(u64), GFP_NOFS | __GFP_NOFAIL);
b3b94faa 989 /* Change the pointers */
b3b94faa
DT
990 for (x = 0; x < half_len; x++)
991 lp[x] = cpu_to_be64(bn);
992
cd915493
SW
993 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
994 half_len * sizeof(u64));
995 if (error != half_len * sizeof(u64)) {
b3b94faa
DT
996 if (error >= 0)
997 error = -EIO;
998 goto fail_lpfree;
999 }
1000
1001 kfree(lp);
1002
1003 /* Compute the divider */
b3b94faa
DT
1004 divider = (start + half_len) << (32 - dip->i_di.di_depth);
1005
1006 /* Copy the entries */
b3b94faa
DT
1007 dirent_first(dip, obh, &dent);
1008
1009 do {
1010 next = dent;
1011 if (dirent_next(dip, obh, &next))
1012 next = NULL;
1013
1014 if (dent->de_inum.no_addr &&
1015 be32_to_cpu(dent->de_hash) < divider) {
c752666c
SW
1016 struct qstr str;
1017 str.name = (char*)(dent+1);
1018 str.len = be16_to_cpu(dent->de_name_len);
1019 str.hash = be32_to_cpu(dent->de_hash);
71b86f56 1020 new = gfs2_dirent_alloc(inode, nbh, &str);
c752666c
SW
1021 if (IS_ERR(new)) {
1022 error = PTR_ERR(new);
1023 break;
1024 }
b3b94faa
DT
1025
1026 new->de_inum = dent->de_inum; /* No endian worries */
b3b94faa 1027 new->de_type = dent->de_type; /* No endian worries */
c752666c 1028 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
b3b94faa
DT
1029
1030 dirent_del(dip, obh, prev, dent);
1031
1032 if (!oleaf->lf_entries)
1033 gfs2_consist_inode(dip);
c752666c 1034 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
b3b94faa
DT
1035
1036 if (!prev)
1037 prev = dent;
1038
1039 moved = 1;
c752666c 1040 } else {
b3b94faa 1041 prev = dent;
c752666c 1042 }
b3b94faa 1043 dent = next;
c752666c 1044 } while (dent);
b3b94faa 1045
c752666c 1046 oleaf->lf_depth = nleaf->lf_depth;
b3b94faa
DT
1047
1048 error = gfs2_meta_inode_buffer(dip, &dibh);
feaa7bba 1049 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
b3b94faa
DT
1050 dip->i_di.di_blocks++;
1051 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1052 brelse(dibh);
1053 }
1054
1055 brelse(obh);
1056 brelse(nbh);
1057
1058 return error;
1059
e90deff5 1060fail_lpfree:
b3b94faa
DT
1061 kfree(lp);
1062
e90deff5 1063fail_brelse:
b3b94faa 1064 brelse(obh);
b3b94faa
DT
1065 brelse(nbh);
1066 return error;
1067}
1068
1069/**
1070 * dir_double_exhash - Double size of ExHash table
1071 * @dip: The GFS2 dinode
1072 *
1073 * Returns: 0 on success, error code on failure
1074 */
1075
1076static int dir_double_exhash(struct gfs2_inode *dip)
1077{
feaa7bba 1078 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
b3b94faa 1079 struct buffer_head *dibh;
cd915493
SW
1080 u32 hsize;
1081 u64 *buf;
1082 u64 *from, *to;
1083 u64 block;
b3b94faa
DT
1084 int x;
1085 int error = 0;
1086
1087 hsize = 1 << dip->i_di.di_depth;
cd915493 1088 if (hsize * sizeof(u64) != dip->i_di.di_size) {
b3b94faa
DT
1089 gfs2_consist_inode(dip);
1090 return -EIO;
1091 }
1092
1093 /* Allocate both the "from" and "to" buffers in one big chunk */
1094
1095 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1096
1097 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
e13940ba 1098 error = gfs2_dir_read_data(dip, (char *)buf,
b3b94faa
DT
1099 block * sdp->sd_hash_bsize,
1100 sdp->sd_hash_bsize);
1101 if (error != sdp->sd_hash_bsize) {
1102 if (error >= 0)
1103 error = -EIO;
1104 goto fail;
1105 }
1106
1107 from = buf;
cd915493 1108 to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
b3b94faa
DT
1109
1110 for (x = sdp->sd_hash_ptrs; x--; from++) {
1111 *to++ = *from; /* No endianess worries */
1112 *to++ = *from;
1113 }
1114
e13940ba 1115 error = gfs2_dir_write_data(dip,
b3b94faa
DT
1116 (char *)buf + sdp->sd_hash_bsize,
1117 block * sdp->sd_sb.sb_bsize,
1118 sdp->sd_sb.sb_bsize);
1119 if (error != sdp->sd_sb.sb_bsize) {
1120 if (error >= 0)
1121 error = -EIO;
1122 goto fail;
1123 }
1124 }
1125
1126 kfree(buf);
1127
1128 error = gfs2_meta_inode_buffer(dip, &dibh);
1129 if (!gfs2_assert_withdraw(sdp, !error)) {
1130 dip->i_di.di_depth++;
1131 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1132 brelse(dibh);
1133 }
1134
1135 return error;
1136
a91ea69f 1137fail:
b3b94faa 1138 kfree(buf);
b3b94faa
DT
1139 return error;
1140}
1141
1142/**
1143 * compare_dents - compare directory entries by hash value
1144 * @a: first dent
1145 * @b: second dent
1146 *
1147 * When comparing the hash entries of @a to @b:
1148 * gt: returns 1
1149 * lt: returns -1
1150 * eq: returns 0
1151 */
1152
1153static int compare_dents(const void *a, const void *b)
1154{
1155 struct gfs2_dirent *dent_a, *dent_b;
cd915493 1156 u32 hash_a, hash_b;
b3b94faa
DT
1157 int ret = 0;
1158
1159 dent_a = *(struct gfs2_dirent **)a;
c752666c 1160 hash_a = be32_to_cpu(dent_a->de_hash);
b3b94faa
DT
1161
1162 dent_b = *(struct gfs2_dirent **)b;
c752666c 1163 hash_b = be32_to_cpu(dent_b->de_hash);
b3b94faa
DT
1164
1165 if (hash_a > hash_b)
1166 ret = 1;
1167 else if (hash_a < hash_b)
1168 ret = -1;
1169 else {
4dd651ad
SW
1170 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1171 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
b3b94faa
DT
1172
1173 if (len_a > len_b)
1174 ret = 1;
1175 else if (len_a < len_b)
1176 ret = -1;
1177 else
1178 ret = memcmp((char *)(dent_a + 1),
1179 (char *)(dent_b + 1),
1180 len_a);
1181 }
1182
1183 return ret;
1184}
1185
1186/**
1187 * do_filldir_main - read out directory entries
1188 * @dip: The GFS2 inode
1189 * @offset: The offset in the file to read from
1190 * @opaque: opaque data to pass to filldir
1191 * @filldir: The function to pass entries to
1192 * @darr: an array of struct gfs2_dirent pointers to read
1193 * @entries: the number of entries in darr
1194 * @copied: pointer to int that's non-zero if a entry has been copied out
1195 *
1196 * Jump through some hoops to make sure that if there are hash collsions,
1197 * they are read out at the beginning of a buffer. We want to minimize
1198 * the possibility that they will fall into different readdir buffers or
1199 * that someone will want to seek to that location.
1200 *
1201 * Returns: errno, >0 on exception from filldir
1202 */
1203
cd915493 1204static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
b3b94faa 1205 void *opaque, gfs2_filldir_t filldir,
cd915493 1206 const struct gfs2_dirent **darr, u32 entries,
b3b94faa
DT
1207 int *copied)
1208{
71b86f56 1209 const struct gfs2_dirent *dent, *dent_next;
b3b94faa 1210 struct gfs2_inum inum;
cd915493 1211 u64 off, off_next;
b3b94faa
DT
1212 unsigned int x, y;
1213 int run = 0;
1214 int error = 0;
1215
1216 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1217
1218 dent_next = darr[0];
1219 off_next = be32_to_cpu(dent_next->de_hash);
1220 off_next = gfs2_disk_hash2offset(off_next);
1221
1222 for (x = 0, y = 1; x < entries; x++, y++) {
1223 dent = dent_next;
1224 off = off_next;
1225
1226 if (y < entries) {
1227 dent_next = darr[y];
1228 off_next = be32_to_cpu(dent_next->de_hash);
1229 off_next = gfs2_disk_hash2offset(off_next);
1230
1231 if (off < *offset)
1232 continue;
1233 *offset = off;
1234
1235 if (off_next == off) {
1236 if (*copied && !run)
1237 return 1;
1238 run = 1;
1239 } else
1240 run = 0;
1241 } else {
1242 if (off < *offset)
1243 continue;
1244 *offset = off;
1245 }
1246
1247 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1248
1249 error = filldir(opaque, (char *)(dent + 1),
4dd651ad 1250 be16_to_cpu(dent->de_name_len),
b3b94faa 1251 off, &inum,
4dd651ad 1252 be16_to_cpu(dent->de_type));
b3b94faa
DT
1253 if (error)
1254 return 1;
1255
1256 *copied = 1;
1257 }
1258
1259 /* Increment the *offset by one, so the next time we come into the
1260 do_filldir fxn, we get the next entry instead of the last one in the
1261 current leaf */
1262
1263 (*offset)++;
1264
1265 return 0;
1266}
1267
71b86f56
SW
1268static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1269 gfs2_filldir_t filldir, int *copied,
1270 unsigned *depth, u64 leaf_no)
b3b94faa 1271{
feaa7bba 1272 struct gfs2_inode *ip = GFS2_I(inode);
71b86f56
SW
1273 struct buffer_head *bh;
1274 struct gfs2_leaf *lf;
1275 unsigned entries = 0;
1276 unsigned leaves = 0;
1277 const struct gfs2_dirent **darr, *dent;
1278 struct dirent_gather g;
1279 struct buffer_head **larr;
1280 int leaf = 0;
1281 int error, i;
1282 u64 lfn = leaf_no;
b3b94faa 1283
b3b94faa 1284 do {
71b86f56 1285 error = get_leaf(ip, lfn, &bh);
b3b94faa 1286 if (error)
71b86f56
SW
1287 goto out;
1288 lf = (struct gfs2_leaf *)bh->b_data;
1289 if (leaves == 0)
1290 *depth = be16_to_cpu(lf->lf_depth);
1291 entries += be16_to_cpu(lf->lf_entries);
1292 leaves++;
1293 lfn = be64_to_cpu(lf->lf_next);
1294 brelse(bh);
1295 } while(lfn);
b3b94faa
DT
1296
1297 if (!entries)
1298 return 0;
1299
71b86f56 1300 error = -ENOMEM;
fe1bdedc 1301 larr = vmalloc((leaves + entries) * sizeof(void*));
71b86f56
SW
1302 if (!larr)
1303 goto out;
1304 darr = (const struct gfs2_dirent **)(larr + leaves);
1305 g.pdent = darr;
1306 g.offset = 0;
1307 lfn = leaf_no;
b3b94faa 1308
71b86f56
SW
1309 do {
1310 error = get_leaf(ip, lfn, &bh);
b3b94faa 1311 if (error)
71b86f56
SW
1312 goto out_kfree;
1313 lf = (struct gfs2_leaf *)bh->b_data;
1314 lfn = be64_to_cpu(lf->lf_next);
1315 if (lf->lf_entries) {
1316 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1317 gfs2_dirent_gather, NULL, &g);
1318 error = PTR_ERR(dent);
1319 if (IS_ERR(dent)) {
1320 goto out_kfree;
1321 }
1322 error = 0;
1323 larr[leaf++] = bh;
b3b94faa 1324 } else {
71b86f56 1325 brelse(bh);
b3b94faa 1326 }
71b86f56 1327 } while(lfn);
b3b94faa 1328
71b86f56 1329 error = do_filldir_main(ip, offset, opaque, filldir, darr,
b3b94faa 1330 entries, copied);
71b86f56
SW
1331out_kfree:
1332 for(i = 0; i < leaf; i++)
1333 brelse(larr[i]);
fe1bdedc 1334 vfree(larr);
71b86f56 1335out:
b3b94faa
DT
1336 return error;
1337}
1338
1339/**
c752666c
SW
1340 * dir_e_read - Reads the entries from a directory into a filldir buffer
1341 * @dip: dinode pointer
1342 * @offset: the hash of the last entry read shifted to the right once
1343 * @opaque: buffer for the filldir function to fill
1344 * @filldir: points to the filldir function to use
b3b94faa 1345 *
c752666c 1346 * Returns: errno
b3b94faa
DT
1347 */
1348
cd915493 1349static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
c752666c 1350 gfs2_filldir_t filldir)
b3b94faa 1351{
feaa7bba
SW
1352 struct gfs2_inode *dip = GFS2_I(inode);
1353 struct gfs2_sbd *sdp = GFS2_SB(inode);
cd915493
SW
1354 u32 hsize, len = 0;
1355 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1356 u32 hash, index;
1357 u64 *lp;
c752666c
SW
1358 int copied = 0;
1359 int error = 0;
4da3c646 1360 unsigned depth = 0;
b3b94faa
DT
1361
1362 hsize = 1 << dip->i_di.di_depth;
cd915493 1363 if (hsize * sizeof(u64) != dip->i_di.di_size) {
b3b94faa
DT
1364 gfs2_consist_inode(dip);
1365 return -EIO;
1366 }
1367
1368 hash = gfs2_dir_offset2hash(*offset);
1369 index = hash >> (32 - dip->i_di.di_depth);
1370
1371 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1372 if (!lp)
1373 return -ENOMEM;
1374
1375 while (index < hsize) {
1376 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1377 ht_offset = index - lp_offset;
1378
1379 if (ht_offset_cur != ht_offset) {
e13940ba 1380 error = gfs2_dir_read_data(dip, (char *)lp,
cd915493 1381 ht_offset * sizeof(u64),
b3b94faa
DT
1382 sdp->sd_hash_bsize);
1383 if (error != sdp->sd_hash_bsize) {
1384 if (error >= 0)
1385 error = -EIO;
1386 goto out;
1387 }
1388 ht_offset_cur = ht_offset;
1389 }
1390
71b86f56
SW
1391 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1392 &copied, &depth,
1393 be64_to_cpu(lp[lp_offset]));
b3b94faa 1394 if (error)
71b86f56 1395 break;
b3b94faa 1396
71b86f56 1397 len = 1 << (dip->i_di.di_depth - depth);
b3b94faa
DT
1398 index = (index & ~(len - 1)) + len;
1399 }
1400
71b86f56 1401out:
b3b94faa 1402 kfree(lp);
71b86f56
SW
1403 if (error > 0)
1404 error = 0;
b3b94faa
DT
1405 return error;
1406}
1407
cd915493 1408int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
71b86f56 1409 gfs2_filldir_t filldir)
b3b94faa 1410{
feaa7bba 1411 struct gfs2_inode *dip = GFS2_I(inode);
71b86f56
SW
1412 struct dirent_gather g;
1413 const struct gfs2_dirent **darr, *dent;
b3b94faa
DT
1414 struct buffer_head *dibh;
1415 int copied = 0;
1416 int error;
1417
71b86f56
SW
1418 if (!dip->i_di.di_entries)
1419 return 0;
1420
1421 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1422 return dir_e_read(inode, offset, opaque, filldir);
1423
b3b94faa
DT
1424 if (!gfs2_is_stuffed(dip)) {
1425 gfs2_consist_inode(dip);
1426 return -EIO;
1427 }
1428
b3b94faa
DT
1429 error = gfs2_meta_inode_buffer(dip, &dibh);
1430 if (error)
1431 return error;
1432
71b86f56
SW
1433 error = -ENOMEM;
1434 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1435 GFP_KERNEL);
1436 if (darr) {
1437 g.pdent = darr;
1438 g.offset = 0;
1439 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1440 gfs2_dirent_gather, NULL, &g);
1441 if (IS_ERR(dent)) {
1442 error = PTR_ERR(dent);
1443 goto out;
1444 }
1445 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1446 dip->i_di.di_entries, &copied);
1447out:
1448 kfree(darr);
1449 }
1450
b3b94faa
DT
1451 if (error > 0)
1452 error = 0;
1453
1454 brelse(dibh);
1455
1456 return error;
1457}
1458
b3b94faa
DT
1459/**
1460 * gfs2_dir_search - Search a directory
1461 * @dip: The GFS2 inode
1462 * @filename:
1463 * @inode:
1464 *
1465 * This routine searches a directory for a file or another directory.
1466 * Assumes a glock is held on dip.
1467 *
1468 * Returns: errno
1469 */
1470
c752666c 1471int gfs2_dir_search(struct inode *dir, const struct qstr *name,
b3b94faa
DT
1472 struct gfs2_inum *inum, unsigned int *type)
1473{
c752666c
SW
1474 struct buffer_head *bh;
1475 struct gfs2_dirent *dent;
1476
1477 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1478 if (dent) {
1479 if (IS_ERR(dent))
1480 return PTR_ERR(dent);
1481 if (inum)
1482 gfs2_inum_in(inum, (char *)&dent->de_inum);
1483 if (type)
1484 *type = be16_to_cpu(dent->de_type);
1485 brelse(bh);
1486 return 0;
1487 }
1488 return -ENOENT;
1489}
1490
1491static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1492{
1493 struct buffer_head *bh, *obh;
feaa7bba 1494 struct gfs2_inode *ip = GFS2_I(inode);
c752666c 1495 struct gfs2_leaf *leaf, *oleaf;
b3b94faa 1496 int error;
c752666c
SW
1497 u32 index;
1498 u64 bn;
b3b94faa 1499
c752666c
SW
1500 index = name->hash >> (32 - ip->i_di.di_depth);
1501 error = get_first_leaf(ip, index, &obh);
1502 if (error)
1503 return error;
1504 do {
1505 oleaf = (struct gfs2_leaf *)obh->b_data;
1506 bn = be64_to_cpu(oleaf->lf_next);
1507 if (!bn)
1508 break;
1509 brelse(obh);
1510 error = get_leaf(ip, bn, &obh);
1511 if (error)
1512 return error;
1513 } while(1);
b3b94faa 1514
c752666c
SW
1515 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1516
1517 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1518 if (!leaf) {
1519 brelse(obh);
1520 return -ENOSPC;
1521 }
4d8012b6 1522 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
c752666c
SW
1523 brelse(bh);
1524 brelse(obh);
1525
1526 error = gfs2_meta_inode_buffer(ip, &bh);
1527 if (error)
1528 return error;
1529 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1530 ip->i_di.di_blocks++;
1531 gfs2_dinode_out(&ip->i_di, bh->b_data);
1532 brelse(bh);
1533 return 0;
b3b94faa
DT
1534}
1535
1536/**
1537 * gfs2_dir_add - Add new filename into directory
1538 * @dip: The GFS2 inode
1539 * @filename: The new name
1540 * @inode: The inode number of the entry
1541 * @type: The type of the entry
1542 *
1543 * Returns: 0 on success, error code on failure
1544 */
1545
c752666c
SW
1546int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1547 const struct gfs2_inum *inum, unsigned type)
b3b94faa 1548{
feaa7bba 1549 struct gfs2_inode *ip = GFS2_I(inode);
c752666c
SW
1550 struct buffer_head *bh;
1551 struct gfs2_dirent *dent;
1552 struct gfs2_leaf *leaf;
b3b94faa
DT
1553 int error;
1554
c752666c
SW
1555 while(1) {
1556 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1557 &bh);
1558 if (dent) {
1559 if (IS_ERR(dent))
1560 return PTR_ERR(dent);
1561 dent = gfs2_init_dirent(inode, dent, name, bh);
1562 gfs2_inum_out(inum, (char *)&dent->de_inum);
1563 dent->de_type = cpu_to_be16(type);
1564 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1565 leaf = (struct gfs2_leaf *)bh->b_data;
1566 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1567 }
1568 brelse(bh);
1569 error = gfs2_meta_inode_buffer(ip, &bh);
1570 if (error)
1571 break;
1572 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1573 ip->i_di.di_entries++;
1574 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1575 gfs2_dinode_out(&ip->i_di, bh->b_data);
1576 brelse(bh);
1577 error = 0;
1578 break;
1579 }
1580 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1581 error = dir_make_exhash(inode);
1582 if (error)
1583 break;
1584 continue;
1585 }
1586 error = dir_split_leaf(inode, name);
1587 if (error == 0)
1588 continue;
e90deff5 1589 if (error < 0)
c752666c
SW
1590 break;
1591 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1592 error = dir_double_exhash(ip);
1593 if (error)
1594 break;
1595 error = dir_split_leaf(inode, name);
e90deff5 1596 if (error < 0)
c752666c 1597 break;
e90deff5
SW
1598 if (error == 0)
1599 continue;
c752666c
SW
1600 }
1601 error = dir_new_leaf(inode, name);
1602 if (!error)
1603 continue;
1604 error = -ENOSPC;
1605 break;
1606 }
b3b94faa
DT
1607 return error;
1608}
1609
c752666c 1610
b3b94faa
DT
1611/**
1612 * gfs2_dir_del - Delete a directory entry
1613 * @dip: The GFS2 inode
1614 * @filename: The filename
1615 *
1616 * Returns: 0 on success, error code on failure
1617 */
1618
c752666c 1619int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
b3b94faa 1620{
c752666c
SW
1621 struct gfs2_dirent *dent, *prev = NULL;
1622 struct buffer_head *bh;
b3b94faa
DT
1623 int error;
1624
c752666c
SW
1625 /* Returns _either_ the entry (if its first in block) or the
1626 previous entry otherwise */
feaa7bba 1627 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
c752666c
SW
1628 if (!dent) {
1629 gfs2_consist_inode(dip);
1630 return -EIO;
1631 }
1632 if (IS_ERR(dent)) {
1633 gfs2_consist_inode(dip);
1634 return PTR_ERR(dent);
1635 }
1636 /* If not first in block, adjust pointers accordingly */
71b86f56 1637 if (gfs2_dirent_find(dent, name, NULL) == 0) {
c752666c
SW
1638 prev = dent;
1639 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1640 }
1641
1642 dirent_del(dip, bh, prev, dent);
1643 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1644 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1645 u16 entries = be16_to_cpu(leaf->lf_entries);
1646 if (!entries)
1647 gfs2_consist_inode(dip);
1648 leaf->lf_entries = cpu_to_be16(--entries);
c752666c 1649 }
ed386507 1650 brelse(bh);
c752666c
SW
1651
1652 error = gfs2_meta_inode_buffer(dip, &bh);
1653 if (error)
1654 return error;
1655
1656 if (!dip->i_di.di_entries)
1657 gfs2_consist_inode(dip);
1658 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1659 dip->i_di.di_entries--;
1660 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1661 gfs2_dinode_out(&dip->i_di, bh->b_data);
1662 brelse(bh);
feaa7bba 1663 mark_inode_dirty(&dip->i_inode);
b3b94faa
DT
1664
1665 return error;
1666}
1667
b3b94faa
DT
1668/**
1669 * gfs2_dir_mvino - Change inode number of directory entry
1670 * @dip: The GFS2 inode
1671 * @filename:
1672 * @new_inode:
1673 *
1674 * This routine changes the inode number of a directory entry. It's used
1675 * by rename to change ".." when a directory is moved.
1676 * Assumes a glock is held on dvp.
1677 *
1678 * Returns: errno
1679 */
1680
c752666c 1681int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
b3b94faa
DT
1682 struct gfs2_inum *inum, unsigned int new_type)
1683{
c752666c
SW
1684 struct buffer_head *bh;
1685 struct gfs2_dirent *dent;
b3b94faa
DT
1686 int error;
1687
feaa7bba 1688 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
c752666c
SW
1689 if (!dent) {
1690 gfs2_consist_inode(dip);
1691 return -EIO;
1692 }
1693 if (IS_ERR(dent))
1694 return PTR_ERR(dent);
b3b94faa 1695
c752666c
SW
1696 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1697 gfs2_inum_out(inum, (char *)&dent->de_inum);
1698 dent->de_type = cpu_to_be16(new_type);
1699
1700 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1701 brelse(bh);
1702 error = gfs2_meta_inode_buffer(dip, &bh);
1703 if (error)
1704 return error;
1705 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1706 }
1707
1708 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1709 gfs2_dinode_out(&dip->i_di, bh->b_data);
1710 brelse(bh);
1711 return 0;
b3b94faa
DT
1712}
1713
1714/**
1715 * foreach_leaf - call a function for each leaf in a directory
1716 * @dip: the directory
1717 * @lc: the function to call for each each
1718 * @data: private data to pass to it
1719 *
1720 * Returns: errno
1721 */
1722
1723static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1724{
feaa7bba 1725 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
b3b94faa 1726 struct buffer_head *bh;
c752666c 1727 struct gfs2_leaf *leaf;
cd915493
SW
1728 u32 hsize, len;
1729 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1730 u32 index = 0;
1731 u64 *lp;
1732 u64 leaf_no;
b3b94faa
DT
1733 int error = 0;
1734
1735 hsize = 1 << dip->i_di.di_depth;
cd915493 1736 if (hsize * sizeof(u64) != dip->i_di.di_size) {
b3b94faa
DT
1737 gfs2_consist_inode(dip);
1738 return -EIO;
1739 }
1740
1741 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1742 if (!lp)
1743 return -ENOMEM;
1744
1745 while (index < hsize) {
1746 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1747 ht_offset = index - lp_offset;
1748
1749 if (ht_offset_cur != ht_offset) {
e13940ba 1750 error = gfs2_dir_read_data(dip, (char *)lp,
cd915493 1751 ht_offset * sizeof(u64),
b3b94faa
DT
1752 sdp->sd_hash_bsize);
1753 if (error != sdp->sd_hash_bsize) {
1754 if (error >= 0)
1755 error = -EIO;
1756 goto out;
1757 }
1758 ht_offset_cur = ht_offset;
1759 }
1760
1761 leaf_no = be64_to_cpu(lp[lp_offset]);
1762 if (leaf_no) {
1763 error = get_leaf(dip, leaf_no, &bh);
1764 if (error)
1765 goto out;
c752666c 1766 leaf = (struct gfs2_leaf *)bh->b_data;
c752666c 1767 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
634ee0b9 1768 brelse(bh);
b3b94faa
DT
1769
1770 error = lc(dip, index, len, leaf_no, data);
1771 if (error)
1772 goto out;
1773
1774 index = (index & ~(len - 1)) + len;
1775 } else
1776 index++;
1777 }
1778
1779 if (index != hsize) {
1780 gfs2_consist_inode(dip);
1781 error = -EIO;
1782 }
1783
634ee0b9 1784out:
b3b94faa
DT
1785 kfree(lp);
1786
1787 return error;
1788}
1789
1790/**
1791 * leaf_dealloc - Deallocate a directory leaf
1792 * @dip: the directory
1793 * @index: the hash table offset in the directory
1794 * @len: the number of pointers to this leaf
1795 * @leaf_no: the leaf number
1796 * @data: not used
1797 *
1798 * Returns: errno
1799 */
1800
cd915493
SW
1801static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1802 u64 leaf_no, void *data)
b3b94faa 1803{
feaa7bba 1804 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
c752666c 1805 struct gfs2_leaf *tmp_leaf;
b3b94faa
DT
1806 struct gfs2_rgrp_list rlist;
1807 struct buffer_head *bh, *dibh;
cd915493 1808 u64 blk, nblk;
b3b94faa
DT
1809 unsigned int rg_blocks = 0, l_blocks = 0;
1810 char *ht;
cd915493 1811 unsigned int x, size = len * sizeof(u64);
b3b94faa
DT
1812 int error;
1813
1814 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1815
1816 ht = kzalloc(size, GFP_KERNEL);
1817 if (!ht)
1818 return -ENOMEM;
1819
1820 gfs2_alloc_get(dip);
1821
1822 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1823 if (error)
1824 goto out;
1825
1826 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1827 if (error)
1828 goto out_qs;
1829
1830 /* Count the number of leaves */
1831
c752666c 1832 for (blk = leaf_no; blk; blk = nblk) {
b3b94faa
DT
1833 error = get_leaf(dip, blk, &bh);
1834 if (error)
1835 goto out_rlist;
c752666c
SW
1836 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1837 nblk = be64_to_cpu(tmp_leaf->lf_next);
b3b94faa
DT
1838 brelse(bh);
1839
1840 gfs2_rlist_add(sdp, &rlist, blk);
1841 l_blocks++;
1842 }
1843
1844 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1845
1846 for (x = 0; x < rlist.rl_rgrps; x++) {
1847 struct gfs2_rgrpd *rgd;
5c676f6d 1848 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
b3b94faa
DT
1849 rg_blocks += rgd->rd_ri.ri_length;
1850 }
1851
1852 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1853 if (error)
1854 goto out_rlist;
1855
1856 error = gfs2_trans_begin(sdp,
5c676f6d 1857 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
b3b94faa
DT
1858 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1859 if (error)
1860 goto out_rg_gunlock;
1861
c752666c 1862 for (blk = leaf_no; blk; blk = nblk) {
b3b94faa
DT
1863 error = get_leaf(dip, blk, &bh);
1864 if (error)
1865 goto out_end_trans;
c752666c
SW
1866 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1867 nblk = be64_to_cpu(tmp_leaf->lf_next);
b3b94faa
DT
1868 brelse(bh);
1869
1870 gfs2_free_meta(dip, blk, 1);
1871
1872 if (!dip->i_di.di_blocks)
1873 gfs2_consist_inode(dip);
1874 dip->i_di.di_blocks--;
1875 }
1876
cd915493 1877 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
b3b94faa
DT
1878 if (error != size) {
1879 if (error >= 0)
1880 error = -EIO;
1881 goto out_end_trans;
1882 }
1883
1884 error = gfs2_meta_inode_buffer(dip, &dibh);
1885 if (error)
1886 goto out_end_trans;
1887
d4e9c4c3 1888 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
b3b94faa
DT
1889 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1890 brelse(dibh);
1891
a91ea69f 1892out_end_trans:
b3b94faa 1893 gfs2_trans_end(sdp);
a91ea69f 1894out_rg_gunlock:
b3b94faa 1895 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
a91ea69f 1896out_rlist:
b3b94faa
DT
1897 gfs2_rlist_free(&rlist);
1898 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
a91ea69f 1899out_qs:
b3b94faa 1900 gfs2_quota_unhold(dip);
a91ea69f 1901out:
b3b94faa
DT
1902 gfs2_alloc_put(dip);
1903 kfree(ht);
b3b94faa
DT
1904 return error;
1905}
1906
1907/**
1908 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1909 * @dip: the directory
1910 *
1911 * Dealloc all on-disk directory leaves to FREEMETA state
1912 * Change on-disk inode type to "regular file"
1913 *
1914 * Returns: errno
1915 */
1916
1917int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1918{
feaa7bba 1919 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
b3b94faa
DT
1920 struct buffer_head *bh;
1921 int error;
1922
1923 /* Dealloc on-disk leaves to FREEMETA state */
1924 error = foreach_leaf(dip, leaf_dealloc, NULL);
1925 if (error)
1926 return error;
1927
1928 /* Make this a regular file in case we crash.
1929 (We don't want to free these blocks a second time.) */
1930
1931 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1932 if (error)
1933 return error;
1934
1935 error = gfs2_meta_inode_buffer(dip, &bh);
1936 if (!error) {
d4e9c4c3 1937 gfs2_trans_add_bh(dip->i_gl, bh, 1);
568f4c96
SW
1938 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1939 cpu_to_be32(S_IFREG);
b3b94faa
DT
1940 brelse(bh);
1941 }
1942
1943 gfs2_trans_end(sdp);
1944
1945 return error;
1946}
1947
1948/**
1949 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1950 * @ip: the file being written to
1951 * @filname: the filename that's going to be added
b3b94faa 1952 *
c752666c 1953 * Returns: 1 if alloc required, 0 if not, -ve on error
b3b94faa
DT
1954 */
1955
4d8012b6 1956int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
b3b94faa 1957{
c752666c
SW
1958 struct gfs2_dirent *dent;
1959 struct buffer_head *bh;
b3b94faa 1960
c752666c 1961 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
ed386507 1962 if (!dent) {
c752666c 1963 return 1;
ed386507 1964 }
c752666c
SW
1965 if (IS_ERR(dent))
1966 return PTR_ERR(dent);
1967 brelse(bh);
1968 return 0;
b3b94faa
DT
1969}
1970