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