projects/linux·blob master · refs

2202 lines · 53.0 KiB

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