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