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