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