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