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