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