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