1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
57
58#include <linux/slab.h>
59#include <linux/spinlock.h>
60#include <linux/buffer_head.h>
61#include <linux/sort.h>
62#include <linux/gfs2_ondisk.h>
63#include <linux/crc32.h>
64#include <linux/vmalloc.h>
65#include <linux/bio.h>
66
67#include "gfs2.h"
68#include "incore.h"
69#include "dir.h"
70#include "glock.h"
71#include "inode.h"
72#include "meta_io.h"
73#include "quota.h"
74#include "rgrp.h"
75#include "trans.h"
76#include "bmap.h"
77#include "util.h"
78
79#define IS_LEAF 1
80#define IS_DINODE 2
81
82#define MAX_RA_BLOCKS 32
83
84#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
85#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
86#define GFS2_HASH_INDEX_MASK 0xffffc000
87#define GFS2_USE_HASH_FLAG 0x2000
88
89struct qstr gfs2_qdot __read_mostly;
90struct qstr gfs2_qdotdot __read_mostly;
91
92typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
93 const struct qstr *name, void *opaque);
94
95int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
96 struct buffer_head **bhp)
97{
98 struct buffer_head *bh;
99
100 bh = gfs2_meta_new(ip->i_gl, block);
101 gfs2_trans_add_meta(ip->i_gl, bh);
102 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
103 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
104 *bhp = bh;
105 return 0;
106}
107
108static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
109 struct buffer_head **bhp)
110{
111 struct buffer_head *bh;
112 int error;
113
114 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, 0, &bh);
115 if (error)
116 return error;
117 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
118 brelse(bh);
119 return -EIO;
120 }
121 *bhp = bh;
122 return 0;
123}
124
125static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
126 unsigned int offset, unsigned int size)
127{
128 struct buffer_head *dibh;
129 int error;
130
131 error = gfs2_meta_inode_buffer(ip, &dibh);
132 if (error)
133 return error;
134
135 gfs2_trans_add_meta(ip->i_gl, dibh);
136 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
137 if (ip->i_inode.i_size < offset + size)
138 i_size_write(&ip->i_inode, offset + size);
139 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
140 gfs2_dinode_out(ip, dibh->b_data);
141
142 brelse(dibh);
143
144 return size;
145}
146
147
148
149
150
151
152
153
154
155
156
157
158static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
159 u64 offset, unsigned int size)
160{
161 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
162 struct buffer_head *dibh;
163 u64 lblock, dblock;
164 u32 extlen = 0;
165 unsigned int o;
166 int copied = 0;
167 int error = 0;
168 bool new = false;
169
170 if (!size)
171 return 0;
172
173 if (gfs2_is_stuffed(ip) && offset + size <= gfs2_max_stuffed_size(ip))
174 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
175 size);
176
177 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
178 return -EINVAL;
179
180 if (gfs2_is_stuffed(ip)) {
181 error = gfs2_unstuff_dinode(ip);
182 if (error)
183 return error;
184 }
185
186 lblock = offset;
187 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
188
189 while (copied < size) {
190 unsigned int amount;
191 struct buffer_head *bh;
192
193 amount = size - copied;
194 if (amount > sdp->sd_sb.sb_bsize - o)
195 amount = sdp->sd_sb.sb_bsize - o;
196
197 if (!extlen) {
198 extlen = 1;
199 error = gfs2_alloc_extent(&ip->i_inode, lblock, &dblock,
200 &extlen, &new);
201 if (error)
202 goto fail;
203 error = -EIO;
204 if (gfs2_assert_withdraw(sdp, dblock))
205 goto fail;
206 }
207
208 if (amount == sdp->sd_jbsize || new)
209 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
210 else
211 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
212
213 if (error)
214 goto fail;
215
216 gfs2_trans_add_meta(ip->i_gl, bh);
217 memcpy(bh->b_data + o, buf, amount);
218 brelse(bh);
219
220 buf += amount;
221 copied += amount;
222 lblock++;
223 dblock++;
224 extlen--;
225
226 o = sizeof(struct gfs2_meta_header);
227 }
228
229out:
230 error = gfs2_meta_inode_buffer(ip, &dibh);
231 if (error)
232 return error;
233
234 if (ip->i_inode.i_size < offset + copied)
235 i_size_write(&ip->i_inode, offset + copied);
236 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
237
238 gfs2_trans_add_meta(ip->i_gl, dibh);
239 gfs2_dinode_out(ip, dibh->b_data);
240 brelse(dibh);
241
242 return copied;
243fail:
244 if (copied)
245 goto out;
246 return error;
247}
248
249static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf,
250 unsigned int size)
251{
252 struct buffer_head *dibh;
253 int error;
254
255 error = gfs2_meta_inode_buffer(ip, &dibh);
256 if (!error) {
257 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
258 brelse(dibh);
259 }
260
261 return (error) ? error : size;
262}
263
264
265
266
267
268
269
270
271
272
273static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf,
274 unsigned int size)
275{
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(ip->i_gl, dblock, extlen);
308 } else {
309 error = gfs2_meta_read(ip->i_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
335
336
337
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, PAGE_KERNEL);
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
386
387
388
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
434
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
448
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
501
502
503
504
505
506
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 goto consist_inode;
573
574 offset = ret;
575 prev = NULL;
576 dent = buf + offset;
577 size = be16_to_cpu(dent->de_rec_len);
578 if (gfs2_check_dirent(GFS2_SB(inode), dent, offset, size, len, 1))
579 goto consist_inode;
580 do {
581 ret = scan(dent, name, opaque);
582 if (ret)
583 break;
584 offset += size;
585 if (offset == len)
586 break;
587 prev = dent;
588 dent = buf + offset;
589 size = be16_to_cpu(dent->de_rec_len);
590 if (gfs2_check_dirent(GFS2_SB(inode), dent, offset, size,
591 len, 0))
592 goto consist_inode;
593 } while(1);
594
595 switch(ret) {
596 case 0:
597 return NULL;
598 case 1:
599 return dent;
600 case 2:
601 return prev ? prev : dent;
602 default:
603 BUG_ON(ret > 0);
604 return ERR_PTR(ret);
605 }
606
607consist_inode:
608 gfs2_consist_inode(GFS2_I(inode));
609 return ERR_PTR(-EIO);
610}
611
612static int dirent_check_reclen(struct gfs2_inode *dip,
613 const struct gfs2_dirent *d, const void *end_p)
614{
615 const void *ptr = d;
616 u16 rec_len = be16_to_cpu(d->de_rec_len);
617
618 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
619 goto broken;
620 ptr += rec_len;
621 if (ptr < end_p)
622 return rec_len;
623 if (ptr == end_p)
624 return -ENOENT;
625broken:
626 gfs2_consist_inode(dip);
627 return -EIO;
628}
629
630
631
632
633
634
635
636
637
638
639static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
640 struct gfs2_dirent **dent)
641{
642 struct gfs2_dirent *cur = *dent, *tmp;
643 char *bh_end = bh->b_data + bh->b_size;
644 int ret;
645
646 ret = dirent_check_reclen(dip, cur, bh_end);
647 if (ret < 0)
648 return ret;
649
650 tmp = (void *)cur + ret;
651 ret = dirent_check_reclen(dip, tmp, bh_end);
652 if (ret == -EIO)
653 return ret;
654
655
656 if (gfs2_dirent_sentinel(tmp)) {
657 gfs2_consist_inode(dip);
658 return -EIO;
659 }
660
661 *dent = tmp;
662 return 0;
663}
664
665
666
667
668
669
670
671
672
673
674static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
675 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
676{
677 u16 cur_rec_len, prev_rec_len;
678
679 if (gfs2_dirent_sentinel(cur)) {
680 gfs2_consist_inode(dip);
681 return;
682 }
683
684 gfs2_trans_add_meta(dip->i_gl, bh);
685
686
687
688
689
690 if (!prev) {
691 cur->de_inum.no_addr = 0;
692 cur->de_inum.no_formal_ino = 0;
693 return;
694 }
695
696
697
698 prev_rec_len = be16_to_cpu(prev->de_rec_len);
699 cur_rec_len = be16_to_cpu(cur->de_rec_len);
700
701 if ((char *)prev + prev_rec_len != (char *)cur)
702 gfs2_consist_inode(dip);
703 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
704 gfs2_consist_inode(dip);
705
706 prev_rec_len += cur_rec_len;
707 prev->de_rec_len = cpu_to_be16(prev_rec_len);
708}
709
710
711static struct gfs2_dirent *do_init_dirent(struct inode *inode,
712 struct gfs2_dirent *dent,
713 const struct qstr *name,
714 struct buffer_head *bh,
715 unsigned offset)
716{
717 struct gfs2_inode *ip = GFS2_I(inode);
718 struct gfs2_dirent *ndent;
719 unsigned totlen;
720
721 totlen = be16_to_cpu(dent->de_rec_len);
722 BUG_ON(offset + name->len > totlen);
723 gfs2_trans_add_meta(ip->i_gl, bh);
724 ndent = (struct gfs2_dirent *)((char *)dent + offset);
725 dent->de_rec_len = cpu_to_be16(offset);
726 gfs2_qstr2dirent(name, totlen - offset, ndent);
727 return ndent;
728}
729
730
731
732
733
734
735static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
736 struct gfs2_dirent *dent,
737 const struct qstr *name,
738 struct buffer_head *bh)
739{
740 unsigned offset = 0;
741
742 if (!gfs2_dirent_sentinel(dent))
743 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
744 return do_init_dirent(inode, dent, name, bh, offset);
745}
746
747static struct gfs2_dirent *gfs2_dirent_split_alloc(struct inode *inode,
748 struct buffer_head *bh,
749 const struct qstr *name,
750 void *ptr)
751{
752 struct gfs2_dirent *dent;
753 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
754 gfs2_dirent_find_offset, name, ptr);
755 if (IS_ERR_OR_NULL(dent))
756 return dent;
757 return do_init_dirent(inode, dent, name, bh,
758 (unsigned)(ptr - (void *)dent));
759}
760
761static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
762 struct buffer_head **bhp)
763{
764 int error;
765
766 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, 0, bhp);
767 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
768
769 error = -EIO;
770 }
771
772 return error;
773}
774
775
776
777
778
779
780
781
782
783
784static int get_leaf_nr(struct gfs2_inode *dip, u32 index, u64 *leaf_out)
785{
786 __be64 *hash;
787 int error;
788
789 hash = gfs2_dir_get_hash_table(dip);
790 error = PTR_ERR_OR_ZERO(hash);
791
792 if (!error)
793 *leaf_out = be64_to_cpu(*(hash + index));
794
795 return error;
796}
797
798static int get_first_leaf(struct gfs2_inode *dip, u32 index,
799 struct buffer_head **bh_out)
800{
801 u64 leaf_no;
802 int error;
803
804 error = get_leaf_nr(dip, index, &leaf_no);
805 if (!error)
806 error = get_leaf(dip, leaf_no, bh_out);
807
808 return error;
809}
810
811static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
812 const struct qstr *name,
813 gfs2_dscan_t scan,
814 struct buffer_head **pbh)
815{
816 struct buffer_head *bh;
817 struct gfs2_dirent *dent;
818 struct gfs2_inode *ip = GFS2_I(inode);
819 int error;
820
821 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
822 struct gfs2_leaf *leaf;
823 unsigned int hsize = BIT(ip->i_depth);
824 unsigned int index;
825 u64 ln;
826 if (hsize * sizeof(u64) != i_size_read(inode)) {
827 gfs2_consist_inode(ip);
828 return ERR_PTR(-EIO);
829 }
830
831 index = name->hash >> (32 - ip->i_depth);
832 error = get_first_leaf(ip, index, &bh);
833 if (error)
834 return ERR_PTR(error);
835 do {
836 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
837 scan, name, NULL);
838 if (dent)
839 goto got_dent;
840 leaf = (struct gfs2_leaf *)bh->b_data;
841 ln = be64_to_cpu(leaf->lf_next);
842 brelse(bh);
843 if (!ln)
844 break;
845
846 error = get_leaf(ip, ln, &bh);
847 } while(!error);
848
849 return error ? ERR_PTR(error) : NULL;
850 }
851
852
853 error = gfs2_meta_inode_buffer(ip, &bh);
854 if (error)
855 return ERR_PTR(error);
856 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
857got_dent:
858 if (IS_ERR_OR_NULL(dent)) {
859 brelse(bh);
860 bh = NULL;
861 }
862 *pbh = bh;
863 return dent;
864}
865
866static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
867{
868 struct gfs2_inode *ip = GFS2_I(inode);
869 unsigned int n = 1;
870 u64 bn;
871 int error;
872 struct buffer_head *bh;
873 struct gfs2_leaf *leaf;
874 struct gfs2_dirent *dent;
875 struct timespec64 tv = current_time(inode);
876
877 error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
878 if (error)
879 return NULL;
880 bh = gfs2_meta_new(ip->i_gl, bn);
881 if (!bh)
882 return NULL;
883
884 gfs2_trans_remove_revoke(GFS2_SB(inode), bn, 1);
885 gfs2_trans_add_meta(ip->i_gl, bh);
886 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
887 leaf = (struct gfs2_leaf *)bh->b_data;
888 leaf->lf_depth = cpu_to_be16(depth);
889 leaf->lf_entries = 0;
890 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
891 leaf->lf_next = 0;
892 leaf->lf_inode = cpu_to_be64(ip->i_no_addr);
893 leaf->lf_dist = cpu_to_be32(1);
894 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
895 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
896 memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2));
897 dent = (struct gfs2_dirent *)(leaf+1);
898 gfs2_qstr2dirent(&empty_name, bh->b_size - sizeof(struct gfs2_leaf), dent);
899 *pbh = bh;
900 return leaf;
901}
902
903
904
905
906
907
908
909
910static int dir_make_exhash(struct inode *inode)
911{
912 struct gfs2_inode *dip = GFS2_I(inode);
913 struct gfs2_sbd *sdp = GFS2_SB(inode);
914 struct gfs2_dirent *dent;
915 struct qstr args;
916 struct buffer_head *bh, *dibh;
917 struct gfs2_leaf *leaf;
918 int y;
919 u32 x;
920 __be64 *lp;
921 u64 bn;
922 int error;
923
924 error = gfs2_meta_inode_buffer(dip, &dibh);
925 if (error)
926 return error;
927
928
929
930 leaf = new_leaf(inode, &bh, 0);
931 if (!leaf)
932 return -ENOSPC;
933 bn = bh->b_blocknr;
934
935 gfs2_assert(sdp, dip->i_entries < BIT(16));
936 leaf->lf_entries = cpu_to_be16(dip->i_entries);
937
938
939
940 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
941 sizeof(struct gfs2_dinode));
942
943
944
945 x = 0;
946 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
947 sizeof(struct gfs2_leaf);
948 args.name = bh->b_data;
949 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
950 gfs2_dirent_last, &args, NULL);
951 if (!dent) {
952 brelse(bh);
953 brelse(dibh);
954 return -EIO;
955 }
956 if (IS_ERR(dent)) {
957 brelse(bh);
958 brelse(dibh);
959 return PTR_ERR(dent);
960 }
961
962
963
964
965 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
966 sizeof(struct gfs2_dinode) -
967 sizeof(struct gfs2_leaf));
968
969 brelse(bh);
970
971
972
973
974 gfs2_trans_add_meta(dip->i_gl, dibh);
975 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
976
977 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
978
979 for (x = sdp->sd_hash_ptrs; x--; lp++)
980 *lp = cpu_to_be64(bn);
981
982 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
983 gfs2_add_inode_blocks(&dip->i_inode, 1);
984 dip->i_diskflags |= GFS2_DIF_EXHASH;
985
986 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
987 dip->i_depth = y;
988
989 gfs2_dinode_out(dip, dibh->b_data);
990
991 brelse(dibh);
992
993 return 0;
994}
995
996
997
998
999
1000
1001
1002
1003
1004static int dir_split_leaf(struct inode *inode, const struct qstr *name)
1005{
1006 struct gfs2_inode *dip = GFS2_I(inode);
1007 struct buffer_head *nbh, *obh, *dibh;
1008 struct gfs2_leaf *nleaf, *oleaf;
1009 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
1010 u32 start, len, half_len, divider;
1011 u64 bn, leaf_no;
1012 __be64 *lp;
1013 u32 index;
1014 int x;
1015 int error;
1016
1017 index = name->hash >> (32 - dip->i_depth);
1018 error = get_leaf_nr(dip, index, &leaf_no);
1019 if (error)
1020 return error;
1021
1022
1023 error = get_leaf(dip, leaf_no, &obh);
1024 if (error)
1025 return error;
1026
1027 oleaf = (struct gfs2_leaf *)obh->b_data;
1028 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
1029 brelse(obh);
1030 return 1;
1031 }
1032
1033 gfs2_trans_add_meta(dip->i_gl, obh);
1034
1035 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
1036 if (!nleaf) {
1037 brelse(obh);
1038 return -ENOSPC;
1039 }
1040 bn = nbh->b_blocknr;
1041
1042
1043 len = BIT(dip->i_depth - be16_to_cpu(oleaf->lf_depth));
1044 half_len = len >> 1;
1045 if (!half_len) {
1046 fs_warn(GFS2_SB(inode), "i_depth %u lf_depth %u index %u\n",
1047 dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
1048 gfs2_consist_inode(dip);
1049 error = -EIO;
1050 goto fail_brelse;
1051 }
1052
1053 start = (index & ~(len - 1));
1054
1055
1056
1057
1058 lp = kmalloc_array(half_len, sizeof(__be64), GFP_NOFS);
1059 if (!lp) {
1060 error = -ENOMEM;
1061 goto fail_brelse;
1062 }
1063
1064
1065 for (x = 0; x < half_len; x++)
1066 lp[x] = cpu_to_be64(bn);
1067
1068 gfs2_dir_hash_inval(dip);
1069
1070 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
1071 half_len * sizeof(u64));
1072 if (error != half_len * sizeof(u64)) {
1073 if (error >= 0)
1074 error = -EIO;
1075 goto fail_lpfree;
1076 }
1077
1078 kfree(lp);
1079
1080
1081 divider = (start + half_len) << (32 - dip->i_depth);
1082
1083
1084 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
1085
1086 do {
1087 next = dent;
1088 if (dirent_next(dip, obh, &next))
1089 next = NULL;
1090
1091 if (!gfs2_dirent_sentinel(dent) &&
1092 be32_to_cpu(dent->de_hash) < divider) {
1093 struct qstr str;
1094 void *ptr = ((char *)dent - obh->b_data) + nbh->b_data;
1095 str.name = (char*)(dent+1);
1096 str.len = be16_to_cpu(dent->de_name_len);
1097 str.hash = be32_to_cpu(dent->de_hash);
1098 new = gfs2_dirent_split_alloc(inode, nbh, &str, ptr);
1099 if (IS_ERR(new)) {
1100 error = PTR_ERR(new);
1101 break;
1102 }
1103
1104 new->de_inum = dent->de_inum;
1105 new->de_type = dent->de_type;
1106 be16_add_cpu(&nleaf->lf_entries, 1);
1107
1108 dirent_del(dip, obh, prev, dent);
1109
1110 if (!oleaf->lf_entries)
1111 gfs2_consist_inode(dip);
1112 be16_add_cpu(&oleaf->lf_entries, -1);
1113
1114 if (!prev)
1115 prev = dent;
1116 } else {
1117 prev = dent;
1118 }
1119 dent = next;
1120 } while (dent);
1121
1122 oleaf->lf_depth = nleaf->lf_depth;
1123
1124 error = gfs2_meta_inode_buffer(dip, &dibh);
1125 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
1126 gfs2_trans_add_meta(dip->i_gl, dibh);
1127 gfs2_add_inode_blocks(&dip->i_inode, 1);
1128 gfs2_dinode_out(dip, dibh->b_data);
1129 brelse(dibh);
1130 }
1131
1132 brelse(obh);
1133 brelse(nbh);
1134
1135 return error;
1136
1137fail_lpfree:
1138 kfree(lp);
1139
1140fail_brelse:
1141 brelse(obh);
1142 brelse(nbh);
1143 return error;
1144}
1145
1146
1147
1148
1149
1150
1151
1152
1153static int dir_double_exhash(struct gfs2_inode *dip)
1154{
1155 struct buffer_head *dibh;
1156 u32 hsize;
1157 u32 hsize_bytes;
1158 __be64 *hc;
1159 __be64 *hc2, *h;
1160 int x;
1161 int error = 0;
1162
1163 hsize = BIT(dip->i_depth);
1164 hsize_bytes = hsize * sizeof(__be64);
1165
1166 hc = gfs2_dir_get_hash_table(dip);
1167 if (IS_ERR(hc))
1168 return PTR_ERR(hc);
1169
1170 hc2 = kmalloc_array(hsize_bytes, 2, GFP_NOFS | __GFP_NOWARN);
1171 if (hc2 == NULL)
1172 hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS, PAGE_KERNEL);
1173
1174 if (!hc2)
1175 return -ENOMEM;
1176
1177 h = hc2;
1178 error = gfs2_meta_inode_buffer(dip, &dibh);
1179 if (error)
1180 goto out_kfree;
1181
1182 for (x = 0; x < hsize; x++) {
1183 *h++ = *hc;
1184 *h++ = *hc;
1185 hc++;
1186 }
1187
1188 error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
1189 if (error != (hsize_bytes * 2))
1190 goto fail;
1191
1192 gfs2_dir_hash_inval(dip);
1193 dip->i_hash_cache = hc2;
1194 dip->i_depth++;
1195 gfs2_dinode_out(dip, dibh->b_data);
1196 brelse(dibh);
1197 return 0;
1198
1199fail:
1200
1201 gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
1202 i_size_write(&dip->i_inode, hsize_bytes);
1203 gfs2_dinode_out(dip, dibh->b_data);
1204 brelse(dibh);
1205out_kfree:
1206 kvfree(hc2);
1207 return error;
1208}
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221static int compare_dents(const void *a, const void *b)
1222{
1223 const struct gfs2_dirent *dent_a, *dent_b;
1224 u32 hash_a, hash_b;
1225 int ret = 0;
1226
1227 dent_a = *(const struct gfs2_dirent **)a;
1228 hash_a = dent_a->de_cookie;
1229
1230 dent_b = *(const struct gfs2_dirent **)b;
1231 hash_b = dent_b->de_cookie;
1232
1233 if (hash_a > hash_b)
1234 ret = 1;
1235 else if (hash_a < hash_b)
1236 ret = -1;
1237 else {
1238 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1239 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1240
1241 if (len_a > len_b)
1242 ret = 1;
1243 else if (len_a < len_b)
1244 ret = -1;
1245 else
1246 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
1247 }
1248
1249 return ret;
1250}
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
1270 struct gfs2_dirent **darr, u32 entries,
1271 u32 sort_start, int *copied)
1272{
1273 const struct gfs2_dirent *dent, *dent_next;
1274 u64 off, off_next;
1275 unsigned int x, y;
1276 int run = 0;
1277
1278 if (sort_start < entries)
1279 sort(&darr[sort_start], entries - sort_start,
1280 sizeof(struct gfs2_dirent *), compare_dents, NULL);
1281
1282 dent_next = darr[0];
1283 off_next = dent_next->de_cookie;
1284
1285 for (x = 0, y = 1; x < entries; x++, y++) {
1286 dent = dent_next;
1287 off = off_next;
1288
1289 if (y < entries) {
1290 dent_next = darr[y];
1291 off_next = dent_next->de_cookie;
1292
1293 if (off < ctx->pos)
1294 continue;
1295 ctx->pos = off;
1296
1297 if (off_next == off) {
1298 if (*copied && !run)
1299 return 1;
1300 run = 1;
1301 } else
1302 run = 0;
1303 } else {
1304 if (off < ctx->pos)
1305 continue;
1306 ctx->pos = off;
1307 }
1308
1309 if (!dir_emit(ctx, (const char *)(dent + 1),
1310 be16_to_cpu(dent->de_name_len),
1311 be64_to_cpu(dent->de_inum.no_addr),
1312 be16_to_cpu(dent->de_type)))
1313 return 1;
1314
1315 *copied = 1;
1316 }
1317
1318
1319
1320
1321
1322 ctx->pos++;
1323
1324 return 0;
1325}
1326
1327static void *gfs2_alloc_sort_buffer(unsigned size)
1328{
1329 void *ptr = NULL;
1330
1331 if (size < KMALLOC_MAX_SIZE)
1332 ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
1333 if (!ptr)
1334 ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
1335 return ptr;
1336}
1337
1338
1339static int gfs2_set_cookies(struct gfs2_sbd *sdp, struct buffer_head *bh,
1340 unsigned leaf_nr, struct gfs2_dirent **darr,
1341 unsigned entries)
1342{
1343 int sort_id = -1;
1344 int i;
1345
1346 for (i = 0; i < entries; i++) {
1347 unsigned offset;
1348
1349 darr[i]->de_cookie = be32_to_cpu(darr[i]->de_hash);
1350 darr[i]->de_cookie = gfs2_disk_hash2offset(darr[i]->de_cookie);
1351
1352 if (!sdp->sd_args.ar_loccookie)
1353 continue;
1354 offset = (char *)(darr[i]) -
1355 (bh->b_data + gfs2_dirent_offset(sdp, bh->b_data));
1356 offset /= GFS2_MIN_DIRENT_SIZE;
1357 offset += leaf_nr * sdp->sd_max_dents_per_leaf;
1358 if (offset >= GFS2_USE_HASH_FLAG ||
1359 leaf_nr >= GFS2_USE_HASH_FLAG) {
1360 darr[i]->de_cookie |= GFS2_USE_HASH_FLAG;
1361 if (sort_id < 0)
1362 sort_id = i;
1363 continue;
1364 }
1365 darr[i]->de_cookie &= GFS2_HASH_INDEX_MASK;
1366 darr[i]->de_cookie |= offset;
1367 }
1368 return sort_id;
1369}
1370
1371
1372static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
1373 int *copied, unsigned *depth,
1374 u64 leaf_no)
1375{
1376 struct gfs2_inode *ip = GFS2_I(inode);
1377 struct gfs2_sbd *sdp = GFS2_SB(inode);
1378 struct buffer_head *bh;
1379 struct gfs2_leaf *lf;
1380 unsigned entries = 0, entries2 = 0;
1381 unsigned leaves = 0, leaf = 0, offset, sort_offset;
1382 struct gfs2_dirent **darr, *dent;
1383 struct dirent_gather g;
1384 struct buffer_head **larr;
1385 int error, i, need_sort = 0, sort_id;
1386 u64 lfn = leaf_no;
1387
1388 do {
1389 error = get_leaf(ip, lfn, &bh);
1390 if (error)
1391 goto out;
1392 lf = (struct gfs2_leaf *)bh->b_data;
1393 if (leaves == 0)
1394 *depth = be16_to_cpu(lf->lf_depth);
1395 entries += be16_to_cpu(lf->lf_entries);
1396 leaves++;
1397 lfn = be64_to_cpu(lf->lf_next);
1398 brelse(bh);
1399 } while(lfn);
1400
1401 if (*depth < GFS2_DIR_MAX_DEPTH || !sdp->sd_args.ar_loccookie) {
1402 need_sort = 1;
1403 sort_offset = 0;
1404 }
1405
1406 if (!entries)
1407 return 0;
1408
1409 error = -ENOMEM;
1410
1411
1412
1413
1414
1415
1416 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
1417 if (!larr)
1418 goto out;
1419 darr = (struct gfs2_dirent **)(larr + leaves);
1420 g.pdent = (const struct gfs2_dirent **)darr;
1421 g.offset = 0;
1422 lfn = leaf_no;
1423
1424 do {
1425 error = get_leaf(ip, lfn, &bh);
1426 if (error)
1427 goto out_free;
1428 lf = (struct gfs2_leaf *)bh->b_data;
1429 lfn = be64_to_cpu(lf->lf_next);
1430 if (lf->lf_entries) {
1431 offset = g.offset;
1432 entries2 += be16_to_cpu(lf->lf_entries);
1433 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1434 gfs2_dirent_gather, NULL, &g);
1435 error = PTR_ERR(dent);
1436 if (IS_ERR(dent))
1437 goto out_free;
1438 if (entries2 != g.offset) {
1439 fs_warn(sdp, "Number of entries corrupt in dir "
1440 "leaf %llu, entries2 (%u) != "
1441 "g.offset (%u)\n",
1442 (unsigned long long)bh->b_blocknr,
1443 entries2, g.offset);
1444 gfs2_consist_inode(ip);
1445 error = -EIO;
1446 goto out_free;
1447 }
1448 error = 0;
1449 sort_id = gfs2_set_cookies(sdp, bh, leaf, &darr[offset],
1450 be16_to_cpu(lf->lf_entries));
1451 if (!need_sort && sort_id >= 0) {
1452 need_sort = 1;
1453 sort_offset = offset + sort_id;
1454 }
1455 larr[leaf++] = bh;
1456 } else {
1457 larr[leaf++] = NULL;
1458 brelse(bh);
1459 }
1460 } while(lfn);
1461
1462 BUG_ON(entries2 != entries);
1463 error = do_filldir_main(ip, ctx, darr, entries, need_sort ?
1464 sort_offset : entries, copied);
1465out_free:
1466 for(i = 0; i < leaf; i++)
1467 if (larr[i])
1468 brelse(larr[i]);
1469 kvfree(larr);
1470out:
1471 return error;
1472}
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
1487 struct file_ra_state *f_ra)
1488{
1489 struct gfs2_inode *ip = GFS2_I(inode);
1490 struct gfs2_glock *gl = ip->i_gl;
1491 struct buffer_head *bh;
1492 u64 blocknr = 0, last;
1493 unsigned count;
1494
1495
1496 if (index + MAX_RA_BLOCKS < f_ra->start)
1497 return;
1498
1499 f_ra->start = max((pgoff_t)index, f_ra->start);
1500 for (count = 0; count < MAX_RA_BLOCKS; count++) {
1501 if (f_ra->start >= hsize)
1502 break;
1503
1504 last = blocknr;
1505 blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
1506 f_ra->start++;
1507 if (blocknr == last)
1508 continue;
1509
1510 bh = gfs2_getbuf(gl, blocknr, 1);
1511 if (trylock_buffer(bh)) {
1512 if (buffer_uptodate(bh)) {
1513 unlock_buffer(bh);
1514 brelse(bh);
1515 continue;
1516 }
1517 bh->b_end_io = end_buffer_read_sync;
1518 submit_bh(REQ_OP_READ,
1519 REQ_RAHEAD | REQ_META | REQ_PRIO,
1520 bh);
1521 continue;
1522 }
1523 brelse(bh);
1524 }
1525}
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536static int dir_e_read(struct inode *inode, struct dir_context *ctx,
1537 struct file_ra_state *f_ra)
1538{
1539 struct gfs2_inode *dip = GFS2_I(inode);
1540 u32 hsize, len = 0;
1541 u32 hash, index;
1542 __be64 *lp;
1543 int copied = 0;
1544 int error = 0;
1545 unsigned depth = 0;
1546
1547 hsize = BIT(dip->i_depth);
1548 hash = gfs2_dir_offset2hash(ctx->pos);
1549 index = hash >> (32 - dip->i_depth);
1550
1551 if (dip->i_hash_cache == NULL)
1552 f_ra->start = 0;
1553 lp = gfs2_dir_get_hash_table(dip);
1554 if (IS_ERR(lp))
1555 return PTR_ERR(lp);
1556
1557 gfs2_dir_readahead(inode, hsize, index, f_ra);
1558
1559 while (index < hsize) {
1560 error = gfs2_dir_read_leaf(inode, ctx,
1561 &copied, &depth,
1562 be64_to_cpu(lp[index]));
1563 if (error)
1564 break;
1565
1566 len = BIT(dip->i_depth - depth);
1567 index = (index & ~(len - 1)) + len;
1568 }
1569
1570 if (error > 0)
1571 error = 0;
1572 return error;
1573}
1574
1575int gfs2_dir_read(struct inode *inode, struct dir_context *ctx,
1576 struct file_ra_state *f_ra)
1577{
1578 struct gfs2_inode *dip = GFS2_I(inode);
1579 struct gfs2_sbd *sdp = GFS2_SB(inode);
1580 struct dirent_gather g;
1581 struct gfs2_dirent **darr, *dent;
1582 struct buffer_head *dibh;
1583 int copied = 0;
1584 int error;
1585
1586 if (!dip->i_entries)
1587 return 0;
1588
1589 if (dip->i_diskflags & GFS2_DIF_EXHASH)
1590 return dir_e_read(inode, ctx, f_ra);
1591
1592 if (!gfs2_is_stuffed(dip)) {
1593 gfs2_consist_inode(dip);
1594 return -EIO;
1595 }
1596
1597 error = gfs2_meta_inode_buffer(dip, &dibh);
1598 if (error)
1599 return error;
1600
1601 error = -ENOMEM;
1602
1603 darr = kmalloc_array(96, sizeof(struct gfs2_dirent *), GFP_NOFS);
1604 if (darr) {
1605 g.pdent = (const struct gfs2_dirent **)darr;
1606 g.offset = 0;
1607 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1608 gfs2_dirent_gather, NULL, &g);
1609 if (IS_ERR(dent)) {
1610 error = PTR_ERR(dent);
1611 goto out;
1612 }
1613 if (dip->i_entries != g.offset) {
1614 fs_warn(sdp, "Number of entries corrupt in dir %llu, "
1615 "ip->i_entries (%u) != g.offset (%u)\n",
1616 (unsigned long long)dip->i_no_addr,
1617 dip->i_entries,
1618 g.offset);
1619 gfs2_consist_inode(dip);
1620 error = -EIO;
1621 goto out;
1622 }
1623 gfs2_set_cookies(sdp, dibh, 0, darr, dip->i_entries);
1624 error = do_filldir_main(dip, ctx, darr,
1625 dip->i_entries, 0, &copied);
1626out:
1627 kfree(darr);
1628 }
1629
1630 if (error > 0)
1631 error = 0;
1632
1633 brelse(dibh);
1634
1635 return error;
1636}
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name,
1651 bool fail_on_exist)
1652{
1653 struct buffer_head *bh;
1654 struct gfs2_dirent *dent;
1655 u64 addr, formal_ino;
1656 u16 dtype;
1657
1658 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1659 if (dent) {
1660 struct inode *inode;
1661 u16 rahead;
1662
1663 if (IS_ERR(dent))
1664 return ERR_CAST(dent);
1665 dtype = be16_to_cpu(dent->de_type);
1666 rahead = be16_to_cpu(dent->de_rahead);
1667 addr = be64_to_cpu(dent->de_inum.no_addr);
1668 formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino);
1669 brelse(bh);
1670 if (fail_on_exist)
1671 return ERR_PTR(-EEXIST);
1672 inode = gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino,
1673 GFS2_BLKST_FREE );
1674 if (!IS_ERR(inode))
1675 GFS2_I(inode)->i_rahead = rahead;
1676 return inode;
1677 }
1678 return ERR_PTR(-ENOENT);
1679}
1680
1681int gfs2_dir_check(struct inode *dir, const struct qstr *name,
1682 const struct gfs2_inode *ip)
1683{
1684 struct buffer_head *bh;
1685 struct gfs2_dirent *dent;
1686 int ret = -ENOENT;
1687
1688 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1689 if (dent) {
1690 if (IS_ERR(dent))
1691 return PTR_ERR(dent);
1692 if (ip) {
1693 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
1694 goto out;
1695 if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
1696 ip->i_no_formal_ino)
1697 goto out;
1698 if (unlikely(IF2DT(ip->i_inode.i_mode) !=
1699 be16_to_cpu(dent->de_type))) {
1700 gfs2_consist_inode(GFS2_I(dir));
1701 ret = -EIO;
1702 goto out;
1703 }
1704 }
1705 ret = 0;
1706out:
1707 brelse(bh);
1708 }
1709 return ret;
1710}
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1732{
1733 struct buffer_head *bh, *obh;
1734 struct gfs2_inode *ip = GFS2_I(inode);
1735 struct gfs2_leaf *leaf, *oleaf;
1736 u32 dist = 1;
1737 int error;
1738 u32 index;
1739 u64 bn;
1740
1741 index = name->hash >> (32 - ip->i_depth);
1742 error = get_first_leaf(ip, index, &obh);
1743 if (error)
1744 return error;
1745 do {
1746 dist++;
1747 oleaf = (struct gfs2_leaf *)obh->b_data;
1748 bn = be64_to_cpu(oleaf->lf_next);
1749 if (!bn)
1750 break;
1751 brelse(obh);
1752 error = get_leaf(ip, bn, &obh);
1753 if (error)
1754 return error;
1755 } while(1);
1756
1757 gfs2_trans_add_meta(ip->i_gl, obh);
1758
1759 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1760 if (!leaf) {
1761 brelse(obh);
1762 return -ENOSPC;
1763 }
1764 leaf->lf_dist = cpu_to_be32(dist);
1765 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
1766 brelse(bh);
1767 brelse(obh);
1768
1769 error = gfs2_meta_inode_buffer(ip, &bh);
1770 if (error)
1771 return error;
1772 gfs2_trans_add_meta(ip->i_gl, bh);
1773 gfs2_add_inode_blocks(&ip->i_inode, 1);
1774 gfs2_dinode_out(ip, bh->b_data);
1775 brelse(bh);
1776 return 0;
1777}
1778
1779static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip)
1780{
1781 u64 where = ip->i_no_addr + 1;
1782 if (ip->i_eattr == where)
1783 return 1;
1784 return 0;
1785}
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1804 const struct gfs2_inode *nip, struct gfs2_diradd *da)
1805{
1806 struct gfs2_inode *ip = GFS2_I(inode);
1807 struct buffer_head *bh = da->bh;
1808 struct gfs2_dirent *dent = da->dent;
1809 struct timespec64 tv;
1810 struct gfs2_leaf *leaf;
1811 int error;
1812
1813 while(1) {
1814 if (da->bh == NULL) {
1815 dent = gfs2_dirent_search(inode, name,
1816 gfs2_dirent_find_space, &bh);
1817 }
1818 if (dent) {
1819 if (IS_ERR(dent))
1820 return PTR_ERR(dent);
1821 dent = gfs2_init_dirent(inode, dent, name, bh);
1822 gfs2_inum_out(nip, dent);
1823 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
1824 dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip));
1825 tv = current_time(&ip->i_inode);
1826 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
1827 leaf = (struct gfs2_leaf *)bh->b_data;
1828 be16_add_cpu(&leaf->lf_entries, 1);
1829 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1830 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
1831 }
1832 da->dent = NULL;
1833 da->bh = NULL;
1834 brelse(bh);
1835 ip->i_entries++;
1836 ip->i_inode.i_mtime = ip->i_inode.i_ctime = tv;
1837 if (S_ISDIR(nip->i_inode.i_mode))
1838 inc_nlink(&ip->i_inode);
1839 mark_inode_dirty(inode);
1840 error = 0;
1841 break;
1842 }
1843 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
1844 error = dir_make_exhash(inode);
1845 if (error)
1846 break;
1847 continue;
1848 }
1849 error = dir_split_leaf(inode, name);
1850 if (error == 0)
1851 continue;
1852 if (error < 0)
1853 break;
1854 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
1855 error = dir_double_exhash(ip);
1856 if (error)
1857 break;
1858 error = dir_split_leaf(inode, name);
1859 if (error < 0)
1860 break;
1861 if (error == 0)
1862 continue;
1863 }
1864 error = dir_new_leaf(inode, name);
1865 if (!error)
1866 continue;
1867 error = -ENOSPC;
1868 break;
1869 }
1870 return error;
1871}
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
1883{
1884 const struct qstr *name = &dentry->d_name;
1885 struct gfs2_dirent *dent, *prev = NULL;
1886 struct buffer_head *bh;
1887 struct timespec64 tv = current_time(&dip->i_inode);
1888
1889
1890
1891 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
1892 if (!dent) {
1893 gfs2_consist_inode(dip);
1894 return -EIO;
1895 }
1896 if (IS_ERR(dent)) {
1897 gfs2_consist_inode(dip);
1898 return PTR_ERR(dent);
1899 }
1900
1901 if (gfs2_dirent_find(dent, name, NULL) == 0) {
1902 prev = dent;
1903 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1904 }
1905
1906 dirent_del(dip, bh, prev, dent);
1907 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
1908 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1909 u16 entries = be16_to_cpu(leaf->lf_entries);
1910 if (!entries)
1911 gfs2_consist_inode(dip);
1912 leaf->lf_entries = cpu_to_be16(--entries);
1913 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1914 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
1915 }
1916 brelse(bh);
1917
1918 if (!dip->i_entries)
1919 gfs2_consist_inode(dip);
1920 dip->i_entries--;
1921 dip->i_inode.i_mtime = dip->i_inode.i_ctime = tv;
1922 if (d_is_dir(dentry))
1923 drop_nlink(&dip->i_inode);
1924 mark_inode_dirty(&dip->i_inode);
1925
1926 return 0;
1927}
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1944 const struct gfs2_inode *nip, unsigned int new_type)
1945{
1946 struct buffer_head *bh;
1947 struct gfs2_dirent *dent;
1948
1949 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
1950 if (!dent) {
1951 gfs2_consist_inode(dip);
1952 return -EIO;
1953 }
1954 if (IS_ERR(dent))
1955 return PTR_ERR(dent);
1956
1957 gfs2_trans_add_meta(dip->i_gl, bh);
1958 gfs2_inum_out(nip, dent);
1959 dent->de_type = cpu_to_be16(new_type);
1960 brelse(bh);
1961
1962 dip->i_inode.i_mtime = dip->i_inode.i_ctime = current_time(&dip->i_inode);
1963 mark_inode_dirty_sync(&dip->i_inode);
1964 return 0;
1965}
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1980 u64 leaf_no, struct buffer_head *leaf_bh,
1981 int last_dealloc)
1982{
1983 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1984 struct gfs2_leaf *tmp_leaf;
1985 struct gfs2_rgrp_list rlist;
1986 struct buffer_head *bh, *dibh;
1987 u64 blk, nblk;
1988 unsigned int rg_blocks = 0, l_blocks = 0;
1989 char *ht;
1990 unsigned int x, size = len * sizeof(u64);
1991 int error;
1992
1993 error = gfs2_rindex_update(sdp);
1994 if (error)
1995 return error;
1996
1997 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1998
1999 ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
2000 if (ht == NULL)
2001 ht = __vmalloc(size, GFP_NOFS | __GFP_NOWARN | __GFP_ZERO,
2002 PAGE_KERNEL);
2003 if (!ht)
2004 return -ENOMEM;
2005
2006 error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
2007 if (error)
2008 goto out;
2009
2010
2011 bh = leaf_bh;
2012
2013 for (blk = leaf_no; blk; blk = nblk) {
2014 if (blk != leaf_no) {
2015 error = get_leaf(dip, blk, &bh);
2016 if (error)
2017 goto out_rlist;
2018 }
2019 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
2020 nblk = be64_to_cpu(tmp_leaf->lf_next);
2021 if (blk != leaf_no)
2022 brelse(bh);
2023
2024 gfs2_rlist_add(dip, &rlist, blk);
2025 l_blocks++;
2026 }
2027
2028 gfs2_rlist_alloc(&rlist);
2029
2030 for (x = 0; x < rlist.rl_rgrps; x++) {
2031 struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl);
2032
2033 rg_blocks += rgd->rd_length;
2034 }
2035
2036 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
2037 if (error)
2038 goto out_rlist;
2039
2040 error = gfs2_trans_begin(sdp,
2041 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
2042 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
2043 if (error)
2044 goto out_rg_gunlock;
2045
2046 bh = leaf_bh;
2047
2048 for (blk = leaf_no; blk; blk = nblk) {
2049 struct gfs2_rgrpd *rgd;
2050
2051 if (blk != leaf_no) {
2052 error = get_leaf(dip, blk, &bh);
2053 if (error)
2054 goto out_end_trans;
2055 }
2056 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
2057 nblk = be64_to_cpu(tmp_leaf->lf_next);
2058 if (blk != leaf_no)
2059 brelse(bh);
2060
2061 rgd = gfs2_blk2rgrpd(sdp, blk, true);
2062 gfs2_free_meta(dip, rgd, blk, 1);
2063 gfs2_add_inode_blocks(&dip->i_inode, -1);
2064 }
2065
2066 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
2067 if (error != size) {
2068 if (error >= 0)
2069 error = -EIO;
2070 goto out_end_trans;
2071 }
2072
2073 error = gfs2_meta_inode_buffer(dip, &dibh);
2074 if (error)
2075 goto out_end_trans;
2076
2077 gfs2_trans_add_meta(dip->i_gl, dibh);
2078
2079
2080 if (last_dealloc)
2081 dip->i_inode.i_mode = S_IFREG;
2082 gfs2_dinode_out(dip, dibh->b_data);
2083 brelse(dibh);
2084
2085out_end_trans:
2086 gfs2_trans_end(sdp);
2087out_rg_gunlock:
2088 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
2089out_rlist:
2090 gfs2_rlist_free(&rlist);
2091 gfs2_quota_unhold(dip);
2092out:
2093 kvfree(ht);
2094 return error;
2095}
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
2108{
2109 struct buffer_head *bh;
2110 struct gfs2_leaf *leaf;
2111 u32 hsize, len;
2112 u32 index = 0, next_index;
2113 __be64 *lp;
2114 u64 leaf_no;
2115 int error = 0, last;
2116
2117 hsize = BIT(dip->i_depth);
2118
2119 lp = gfs2_dir_get_hash_table(dip);
2120 if (IS_ERR(lp))
2121 return PTR_ERR(lp);
2122
2123 while (index < hsize) {
2124 leaf_no = be64_to_cpu(lp[index]);
2125 if (leaf_no) {
2126 error = get_leaf(dip, leaf_no, &bh);
2127 if (error)
2128 goto out;
2129 leaf = (struct gfs2_leaf *)bh->b_data;
2130 len = BIT(dip->i_depth - be16_to_cpu(leaf->lf_depth));
2131
2132 next_index = (index & ~(len - 1)) + len;
2133 last = ((next_index >= hsize) ? 1 : 0);
2134 error = leaf_dealloc(dip, index, len, leaf_no, bh,
2135 last);
2136 brelse(bh);
2137 if (error)
2138 goto out;
2139 index = next_index;
2140 } else
2141 index++;
2142 }
2143
2144 if (index != hsize) {
2145 gfs2_consist_inode(dip);
2146 error = -EIO;
2147 }
2148
2149out:
2150
2151 return error;
2152}
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name,
2164 struct gfs2_diradd *da)
2165{
2166 struct gfs2_inode *ip = GFS2_I(inode);
2167 struct gfs2_sbd *sdp = GFS2_SB(inode);
2168 const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf);
2169 struct gfs2_dirent *dent;
2170 struct buffer_head *bh;
2171
2172 da->nr_blocks = 0;
2173 da->bh = NULL;
2174 da->dent = NULL;
2175
2176 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
2177 if (!dent) {
2178 da->nr_blocks = sdp->sd_max_dirres;
2179 if (!(ip->i_diskflags & GFS2_DIF_EXHASH) &&
2180 (GFS2_DIRENT_SIZE(name->len) < extra))
2181 da->nr_blocks = 1;
2182 return 0;
2183 }
2184 if (IS_ERR(dent))
2185 return PTR_ERR(dent);
2186
2187 if (da->save_loc) {
2188 da->bh = bh;
2189 da->dent = dent;
2190 } else {
2191 brelse(bh);
2192 }
2193 return 0;
2194}
2195
2196