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