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