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