1 #include "clusterautoconfig.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <stdint.h>
7 #include <inttypes.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <errno.h>
13
14 #include "libgfs2.h"
15 #include "rgrp.h"
16
17 static __inline__ __be64 *metapointer(char *buf,
18 unsigned int height,
19 struct lgfs2_metapath *mp)
20 {
21 unsigned int head_size = (height > 0) ?
22 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
23
24 return ((__be64 *)(buf + head_size)) + mp->mp_list[height];
25 }
26
27 /* Detect directory is a stuffed inode */
28 static int inode_is_stuffed(const struct lgfs2_inode *ip)
29 {
30 return !ip->i_height;
31 }
32
33 struct lgfs2_inode *lgfs2_inode_get(struct lgfs2_sbd *sdp, struct lgfs2_buffer_head *bh)
34 {
35 struct lgfs2_inode *ip;
36
37 ip = calloc(1, sizeof(struct lgfs2_inode));
38 if (ip == NULL) {
39 return NULL;
40 }
41 lgfs2_dinode_in(ip, bh->b_data);
42 ip->i_bh = bh;
43 ip->i_sbd = sdp;
44 return ip;
45 }
46
47 struct lgfs2_inode *lgfs2_inode_read(struct lgfs2_sbd *sdp, uint64_t di_addr)
48 {
49 struct lgfs2_inode *ip;
50 struct lgfs2_buffer_head *bh = lgfs2_bread(sdp, di_addr);
51 if (bh == NULL) {
52 return NULL;
53 }
54 ip = lgfs2_inode_get(sdp, bh);
55 if (ip == NULL) {
56 lgfs2_brelse(bh);
57 return NULL;
58 }
59 ip->bh_owned = 1; /* We did the lgfs2_bread so we own the bh */
60 return ip;
61 }
62
63 struct lgfs2_inode *lgfs2_is_system_inode(struct lgfs2_sbd *sdp, uint64_t block)
64 {
65 int j;
66
67 if (sdp->md.inum && block == sdp->md.inum->i_num.in_addr)
68 return sdp->md.inum;
69 if (sdp->md.statfs && block == sdp->md.statfs->i_num.in_addr)
70 return sdp->md.statfs;
71 if (sdp->md.jiinode && block == sdp->md.jiinode->i_num.in_addr)
72 return sdp->md.jiinode;
73 if (sdp->md.riinode && block == sdp->md.riinode->i_num.in_addr)
74 return sdp->md.riinode;
75 if (sdp->md.qinode && block == sdp->md.qinode->i_num.in_addr)
76 return sdp->md.qinode;
77 if (sdp->md.pinode && block == sdp->md.pinode->i_num.in_addr)
78 return sdp->md.pinode;
79 if (sdp->md.rooti && block == sdp->md.rooti->i_num.in_addr)
80 return sdp->md.rooti;
81 if (sdp->master_dir && block == sdp->master_dir->i_num.in_addr)
82 return sdp->master_dir;
83 for (j = 0; j < sdp->md.journals; j++)
84 if (sdp->md.journal && sdp->md.journal[j] &&
85 block == sdp->md.journal[j]->i_num.in_addr)
86 return sdp->md.journal[j];
87 return NULL;
88 }
89
90 void lgfs2_inode_put(struct lgfs2_inode **ip_in)
91 {
|
(1) Event var_assign_parm: |
Assigning: "ip" = "*ip_in". |
| Also see events: |
[dereference] |
92 struct lgfs2_inode *ip = *ip_in;
|
(2) Event dereference: |
Dereferencing pointer "ip" (which is a copy of "ip_in*"). |
| Also see events: |
[var_assign_parm] |
93 uint64_t block = ip->i_num.in_addr;
94 struct lgfs2_sbd *sdp = ip->i_sbd;
95
96 if (ip->i_bh != NULL) {
97 if (ip->i_bh->b_modified) {
98 lgfs2_dinode_out(ip, ip->i_bh->b_data);
99 if (!ip->bh_owned && lgfs2_is_system_inode(sdp, block))
100 fprintf(stderr, "Warning: Changes made to inode were discarded.\n");
101 }
102 if (ip->bh_owned)
103 lgfs2_brelse(ip->i_bh);
104 ip->i_bh = NULL;
105 }
106 free(ip);
107 *ip_in = NULL; /* make sure the memory isn't accessed again */
108 }
109
110 /**
111 * Free an inode, discarding modifications.
112 * @ipp: A pointer to the inode.
113 */
114 void lgfs2_inode_free(struct lgfs2_inode **ipp)
115 {
116 struct lgfs2_inode *ip = *ipp;
117
118 if (ip)
119 free(ip->i_bh);
120 free(ip);
121 *ipp = NULL;
122 }
123
124 static uint64_t find_free_block(struct lgfs2_rgrp_tree *rgd)
125 {
126 unsigned bm;
127 uint64_t blkno = 0;
128
129 if (rgd == NULL || rgd->rt_free == 0) {
130 errno = ENOSPC;
131 return 0;
132 }
133
134 for (bm = 0; bm < rgd->rt_length; bm++) {
135 unsigned long blk = 0;
136 struct lgfs2_bitmap *bits = &rgd->rt_bits[bm];
137
138 blk = lgfs2_bitfit((uint8_t *)bits->bi_data + bits->bi_offset,
139 bits->bi_len, blk, GFS2_BLKST_FREE);
140 if (blk != LGFS2_BFITNOENT) {
141 blkno = blk + (bits->bi_start * GFS2_NBBY) + rgd->rt_data0;
142 break;
143 }
144 }
145 return blkno;
146 }
147
148 static int blk_alloc_in_rg(struct lgfs2_sbd *sdp, unsigned state, struct lgfs2_rgrp_tree *rgd, uint64_t blkno, int dinode)
149 {
150 if (blkno == 0)
151 return -1;
152
153 if (lgfs2_set_bitmap(rgd, blkno, state))
154 return -1;
155
156 if (state == GFS2_BLKST_DINODE) {
157 if (dinode)
158 rgd->rt_dinodes++;
159 }
160
161 rgd->rt_free--;
162 lgfs2_rgrp_out(rgd, rgd->rt_bits[0].bi_data);
163 rgd->rt_bits[0].bi_modified = 1;
164 sdp->blks_alloced++;
165 return 0;
166 }
167
168 /**
169 * Allocate a block in a bitmap. In order to plan ahead we look for a
170 * resource group with blksreq free blocks but only allocate the one block.
171 * Returns 0 on success with the allocated block number in *blkno or non-zero otherwise.
172 */
173 static int block_alloc(struct lgfs2_sbd *sdp, const uint64_t blksreq, int state, uint64_t *blkno, int dinode)
174 {
175 int ret;
176 int release = 0;
177 struct lgfs2_rgrp_tree *rgt = NULL;
178 struct osi_node *n = NULL;
179 uint64_t bn = 0;
180
181 for (n = osi_first(&sdp->rgtree); n; n = osi_next(n)) {
182 rgt = (struct lgfs2_rgrp_tree *)n;
183 if (rgt->rt_free >= blksreq)
184 break;
185 }
186 if (n == NULL)
187 return -1;
188
189 if (rgt->rt_bits[0].bi_data == NULL) {
190 if (lgfs2_rgrp_read(sdp, rgt))
191 return -1;
192 release = 1;
193 }
194
195 bn = find_free_block(rgt);
196 ret = blk_alloc_in_rg(sdp, state, rgt, bn, dinode);
197 if (release)
198 lgfs2_rgrp_relse(sdp, rgt);
199 *blkno = bn;
200 return ret;
201 }
202
203 int lgfs2_dinode_alloc(struct lgfs2_sbd *sdp, const uint64_t blksreq, uint64_t *blkno)
204 {
205 int ret = block_alloc(sdp, blksreq, GFS2_BLKST_DINODE, blkno, 1);
206 if (ret == 0)
207 sdp->dinodes_alloced++;
208 return ret;
209 }
210
211 int lgfs2_meta_alloc(struct lgfs2_inode *ip, uint64_t *blkno)
212 {
213 int ret = block_alloc(ip->i_sbd, 1, GFS2_BLKST_USED, blkno, 0);
214 if (ret == 0) {
215 ip->i_goal_meta = *blkno;
216 lgfs2_bmodified(ip->i_bh);
217 }
218 return ret;
219 }
220
221 static __inline__ void buffer_clear_tail(struct lgfs2_sbd *sdp,
222 struct lgfs2_buffer_head *bh, int head)
223 {
224 memset(bh->b_data + head, 0, sdp->sd_bsize - head);
225 lgfs2_bmodified(bh);
226 }
227
228 static __inline__ void
229 buffer_copy_tail(struct lgfs2_sbd *sdp,
230 struct lgfs2_buffer_head *to_bh, int to_head,
231 struct lgfs2_buffer_head *from_bh, int from_head)
232 {
233 memcpy(to_bh->b_data + to_head, from_bh->b_data + from_head,
234 sdp->sd_bsize - from_head);
235 memset(to_bh->b_data + sdp->sd_bsize + to_head - from_head, 0,
236 from_head - to_head);
237 lgfs2_bmodified(to_bh);
238 }
239
240 int lgfs2_unstuff_dinode(struct lgfs2_inode *ip)
241 {
242 struct lgfs2_sbd *sdp = ip->i_sbd;
243 struct lgfs2_buffer_head *bh;
244 uint64_t block = 0;
245 int isdir = S_ISDIR(ip->i_mode);
246
247 if (ip->i_size) {
248 if (lgfs2_meta_alloc(ip, &block))
249 return -1;
250 if (isdir) {
251 struct gfs2_meta_header mh = {
252 .mh_magic = cpu_to_be32(GFS2_MAGIC),
253 .mh_type = cpu_to_be32(GFS2_METATYPE_JD),
254 .mh_format = cpu_to_be32(GFS2_FORMAT_JD)
255 };
256
257 bh = lgfs2_bget(sdp, block);
258 memcpy(bh->b_data, &mh, sizeof(mh));
259 buffer_copy_tail(sdp, bh,
260 sizeof(struct gfs2_meta_header),
261 ip->i_bh, sizeof(struct gfs2_dinode));
262
263 lgfs2_bmodified(bh);
264 lgfs2_brelse(bh);
265 } else {
266 bh = lgfs2_bget(sdp, block);
267
268 buffer_copy_tail(sdp, bh, 0,
269 ip->i_bh, sizeof(struct gfs2_dinode));
270 lgfs2_brelse(bh);
271 }
272 }
273
274 buffer_clear_tail(sdp, ip->i_bh, sizeof(struct gfs2_dinode));
275
276 if (ip->i_size) {
277 *(__be64 *)(ip->i_bh->b_data + sizeof(struct gfs2_dinode)) = cpu_to_be64(block);
278 /* no need: lgfs2_bmodified(ip->i_bh); buffer_clear_tail does it */
279 ip->i_blocks++;
280 }
281
282 ip->i_height = 1;
283 return 0;
284 }
285
286 /**
287 * Calculate the total number of blocks required by a file containing 'bytes' bytes of data.
288 */
289 uint64_t lgfs2_space_for_data(const struct lgfs2_sbd *sdp, const unsigned bsize, const uint64_t bytes)
290 {
291 uint64_t blks = (bytes + bsize - 1) / bsize;
292 uint64_t ptrs = blks;
293
294 if (bytes <= bsize - sizeof(struct gfs2_dinode))
295 return 1;
296
297 while (ptrs > sdp->sd_diptrs) {
298 ptrs = (ptrs + sdp->sd_inptrs - 1) / sdp->sd_inptrs;
299 blks += ptrs;
300 }
301 return blks + 1;
302 }
303
304 /**
305 * Allocate an extent for a file in a resource group's bitmaps.
306 * rg: The resource group in which to allocate the extent
307 * di_size: The size of the file in bytes
308 * ip: A pointer to the inode structure, whose fields will be set appropriately.
309 * If ip->i_num.no_addr is not 0, the extent search will be skipped and
310 * the file allocated from that address.
311 * flags: GFS2_DIF_* flags
312 * mode: File mode flags, see creat(2)
313 * Returns 0 on success with the contents of ip set accordingly, or non-zero
314 * with errno set on error. If errno is ENOSPC then rg does not contain a
315 * large enough free extent for the given di_size.
316 */
317 int lgfs2_file_alloc(lgfs2_rgrp_t rg, uint64_t di_size, struct lgfs2_inode *ip, uint32_t flags, unsigned mode)
318 {
319 unsigned extlen;
320 struct lgfs2_sbd *sdp = rg->rt_rgrps->rgs_sdp;
321 struct lgfs2_rbm rbm = { .rgd = rg, .offset = 0, .bii = 0 };
322 uint32_t blocks = lgfs2_space_for_data(sdp, sdp->sd_bsize, di_size);
323
324 if (ip->i_num.in_addr != 0) {
325 if (lgfs2_rbm_from_block(&rbm, ip->i_num.in_addr) != 0)
326 return 1;
327 } else if (lgfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &blocks) != 0) {
328 return 1;
329 }
330
331 extlen = lgfs2_alloc_extent(&rbm, GFS2_BLKST_DINODE, blocks);
332 if (extlen < blocks) {
333 errno = EINVAL;
334 return 1;
335 }
336
337 ip->i_sbd = sdp;
338
339 ip->i_magic = GFS2_MAGIC;
340 ip->i_mh_type = GFS2_METATYPE_DI;
341 ip->i_format = GFS2_FORMAT_DI;
342 ip->i_size = di_size;
343 ip->i_num.in_addr = lgfs2_rbm_to_block(&rbm);
344 ip->i_num.in_formal_ino = sdp->md.next_inum++;
345 ip->i_mode = mode;
346 ip->i_nlink = 1;
347 ip->i_blocks = blocks;
348 ip->i_atime = ip->i_mtime = ip->i_ctime = sdp->sd_time;
349 ip->i_goal_data = ip->i_num.in_addr + ip->i_blocks - 1;
350 ip->i_goal_meta = ip->i_goal_data - ((di_size + sdp->sd_bsize - 1) / sdp->sd_bsize);
351 ip->i_height = lgfs2_calc_tree_height(ip, di_size);
352 ip->i_flags = flags;
353
354 rg->rt_free -= blocks;
355 rg->rt_dinodes += 1;
356
357 sdp->dinodes_alloced++;
358 sdp->blks_alloced += blocks;
359
360 return 0;
361 }
362
363 unsigned int lgfs2_calc_tree_height(struct lgfs2_inode *ip, uint64_t size)
364 {
365 struct lgfs2_sbd *sdp = ip->i_sbd;
366 uint64_t *arr;
367 unsigned int max, height;
368
369 if (ip->i_size > size)
370 size = ip->i_size;
371
372 if (S_ISDIR(ip->i_mode)) {
373 arr = sdp->sd_jheightsize;
374 max = sdp->sd_max_jheight;
375 } else {
376 arr = sdp->sd_heightsize;
377 max = sdp->sd_max_height;
378 }
379
380 for (height = 0; height < max; height++)
381 if (arr[height] >= size)
382 break;
383
384 return height;
385 }
386
387 int lgfs2_build_height(struct lgfs2_inode *ip, int height)
388 {
389 struct lgfs2_sbd *sdp = ip->i_sbd;
390 struct lgfs2_buffer_head *bh;
391 uint64_t block = 0, *bp;
392 unsigned int x;
393 int new_block;
394
395 while (ip->i_height < height) {
396 new_block = 0;
397 bp = (uint64_t *)(ip->i_bh->b_data + sizeof(struct gfs2_dinode));
398 for (x = 0; x < sdp->sd_diptrs; x++, bp++)
399 if (*bp) {
400 new_block = 1;
401 break;
402 }
403
404 if (new_block) {
405 struct gfs2_meta_header mh = {
406 .mh_magic = cpu_to_be32(GFS2_MAGIC),
407 .mh_type = cpu_to_be32(GFS2_METATYPE_IN),
408 .mh_format = cpu_to_be32(GFS2_FORMAT_IN)
409 };
410
411 if (lgfs2_meta_alloc(ip, &block))
412 return -1;
413 bh = lgfs2_bget(sdp, block);
414 memcpy(bh->b_data, &mh, sizeof(mh));
415 buffer_copy_tail(sdp, bh,
416 sizeof(struct gfs2_meta_header),
417 ip->i_bh, sizeof(struct gfs2_dinode));
418 lgfs2_bmodified(bh);
419 lgfs2_brelse(bh);
420 }
421
422 buffer_clear_tail(sdp, ip->i_bh, sizeof(struct gfs2_dinode));
423
424 if (new_block) {
425 *(__be64 *)(ip->i_bh->b_data + sizeof(struct gfs2_dinode)) = cpu_to_be64(block);
426 /* no need: lgfs2_bmodified(ip->i_bh);*/
427 ip->i_blocks++;
428 }
429
430 ip->i_height++;
431 }
432 return 0;
433 }
434
435 void lgfs2_find_metapath(struct lgfs2_inode *ip, uint64_t block, struct lgfs2_metapath *mp)
436 {
437 const uint32_t inptrs = ip->i_sbd->sd_inptrs;
438 unsigned int i = ip->i_height;
439
440 memset(mp, 0, sizeof(struct lgfs2_metapath));
441 while (i--) {
442 mp->mp_list[i] = block % inptrs;
443 block /= inptrs;
444 }
445 }
446
447 void lgfs2_lookup_block(struct lgfs2_inode *ip, struct lgfs2_buffer_head *bh,
448 unsigned int height, struct lgfs2_metapath *mp,
449 int create, int *new, uint64_t *block)
450 {
451 __be64 *ptr = metapointer(bh->b_data, height, mp);
452
453 if (*ptr) {
454 *block = be64_to_cpu(*ptr);
455 return;
456 }
457
458 *block = 0;
459
460 if (!create)
461 return;
462
463 if (lgfs2_meta_alloc(ip, block))
464 return;
465 *ptr = cpu_to_be64(*block);
466 lgfs2_bmodified(bh);
467 ip->i_blocks++;
468 lgfs2_bmodified(ip->i_bh);
469
470 *new = 1;
471 }
472
473 int lgfs2_block_map(struct lgfs2_inode *ip, uint64_t lblock, int *new,
474 uint64_t *dblock, uint32_t *extlen, int prealloc)
475 {
476 struct lgfs2_sbd *sdp = ip->i_sbd;
477 struct lgfs2_buffer_head *bh;
478 struct lgfs2_metapath mp;
479 int create = *new;
480 unsigned int bsize;
481 unsigned int height;
482 unsigned int end_of_metadata;
483 unsigned int x;
484
485 *new = 0;
486 *dblock = 0;
487 if (extlen)
488 *extlen = 0;
489
490 if (inode_is_stuffed(ip)) {
491 if (!lblock) {
492 *dblock = ip->i_num.in_addr;
493 if (extlen)
494 *extlen = 1;
495 }
496 return 0;
497 }
498
499 bsize = (S_ISDIR(ip->i_mode)) ? sdp->sd_jbsize : sdp->sd_bsize;
500
501 height = lgfs2_calc_tree_height(ip, (lblock + 1) * bsize);
502 if (ip->i_height < height) {
503 if (!create)
504 return 0;
505
506 if (lgfs2_build_height(ip, height))
507 return -1;
508 }
509
510 lgfs2_find_metapath(ip, lblock, &mp);
511 end_of_metadata = ip->i_height - 1;
512
513 bh = ip->i_bh;
514
515 for (x = 0; x < end_of_metadata; x++) {
516 lgfs2_lookup_block(ip, bh, x, &mp, create, new, dblock);
517 if (bh != ip->i_bh)
518 lgfs2_brelse(bh);
519 if (!*dblock)
520 return 0;
521
522 if (*new) {
523 struct gfs2_meta_header mh = {
524 .mh_magic = cpu_to_be32(GFS2_MAGIC),
525 .mh_type = cpu_to_be32(GFS2_METATYPE_IN),
526 .mh_format = cpu_to_be32(GFS2_FORMAT_IN)
527 };
528 bh = lgfs2_bget(sdp, *dblock);
529 memcpy(bh->b_data, &mh, sizeof(mh));
530 lgfs2_bmodified(bh);
531 } else {
532 if (*dblock == ip->i_num.in_addr)
533 bh = ip->i_bh;
534 else
535 bh = lgfs2_bread(sdp, *dblock);
536 }
537 }
538
539 if (!prealloc)
540 lgfs2_lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
541
542 if (extlen && *dblock) {
543 *extlen = 1;
544
545 if (!*new) {
546 uint64_t tmp_dblock;
547 int tmp_new;
548 unsigned int nptrs;
549
550 nptrs = (end_of_metadata) ? sdp->sd_inptrs : sdp->sd_diptrs;
551
552 while (++mp.mp_list[end_of_metadata] < nptrs) {
553 lgfs2_lookup_block(ip, bh, end_of_metadata, &mp, 0, &tmp_new,
554 &tmp_dblock);
555
556 if (*dblock + *extlen != tmp_dblock)
557 break;
558
559 (*extlen)++;
560 }
561 }
562 }
563
564 if (bh != ip->i_bh)
565 lgfs2_brelse(bh);
566 return 0;
567 }
568
569 static void
570 copy2mem(struct lgfs2_buffer_head *bh, void **buf, unsigned int offset,
571 unsigned int size)
572 {
573 char **p = (char **)buf;
574
575 if (bh)
576 memcpy(*p, bh->b_data + offset, size);
577 else
578 memset(*p, 0, size);
579
580 *p += size;
581 }
582
583 int lgfs2_readi(struct lgfs2_inode *ip, void *buf, uint64_t offset, unsigned int size)
584 {
585 struct lgfs2_sbd *sdp = ip->i_sbd;
586 struct lgfs2_buffer_head *bh;
587 uint64_t lblock, dblock;
588 unsigned int o;
589 uint32_t extlen = 0;
590 unsigned int amount;
591 int not_new = 0;
592 int isdir = !!(S_ISDIR(ip->i_mode));
593 int copied = 0;
594
595 if (offset >= ip->i_size)
596 return 0;
597
598 if ((offset + size) > ip->i_size)
599 size = ip->i_size - offset;
600
601 if (!size)
602 return 0;
603
604 if (isdir) {
605 lblock = offset;
606 o = lblock % sdp->sd_jbsize;
607 lblock /= sdp->sd_jbsize;
608 } else {
609 lblock = offset >> sdp->sd_bsize_shift;
610 o = offset & (sdp->sd_bsize - 1);
611 }
612
613 if (inode_is_stuffed(ip))
614 o += sizeof(struct gfs2_dinode);
615 else if (isdir)
616 o += sizeof(struct gfs2_meta_header);
617
618 while (copied < size) {
619 amount = size - copied;
620 if (amount > sdp->sd_bsize - o)
621 amount = sdp->sd_bsize - o;
622
623 if (!extlen) {
624 if (lgfs2_block_map(ip, lblock, ¬_new, &dblock, &extlen, 0))
625 return -1;
626 }
627
628 if (dblock) {
629 if (dblock == ip->i_num.in_addr)
630 bh = ip->i_bh;
631 else
632 bh = lgfs2_bread(sdp, dblock);
633 dblock++;
634 extlen--;
635 } else
636 bh = NULL;
637
638 copy2mem(bh, &buf, o, amount);
639 if (bh && bh != ip->i_bh)
640 lgfs2_brelse(bh);
641
642 copied += amount;
643 lblock++;
644 o = (isdir) ? sizeof(struct gfs2_meta_header) : 0;
645 }
646
647 return copied;
648 }
649
650 static void copy_from_mem(struct lgfs2_buffer_head *bh, void **buf,
651 unsigned int offset, unsigned int size)
652 {
653 char **p = (char **)buf;
654
655 memcpy(bh->b_data + offset, *p, size);
656 lgfs2_bmodified(bh);
657 *p += size;
658 }
659
660 int __lgfs2_writei(struct lgfs2_inode *ip, void *buf,
661 uint64_t offset, unsigned int size, int resize)
662 {
663 struct lgfs2_sbd *sdp = ip->i_sbd;
664 struct lgfs2_buffer_head *bh;
665 uint64_t lblock, dblock;
666 unsigned int o;
667 uint32_t extlen = 0;
668 unsigned int amount;
669 int new;
670 int isdir = !!(S_ISDIR(ip->i_mode));
671 const uint64_t start = offset;
672 int copied = 0;
673
674 if (!size)
675 return 0;
676
677 if (inode_is_stuffed(ip) &&
678 ((start + size) > (sdp->sd_bsize - sizeof(struct gfs2_dinode))))
679 if (lgfs2_unstuff_dinode(ip))
680 return -1;
681
682 if (isdir) {
683 lblock = offset;
684 o = lblock % sdp->sd_jbsize;
685 lblock /= sdp->sd_jbsize;
686 } else {
687 lblock = offset >> sdp->sd_bsize_shift;
688 o = offset & (sdp->sd_bsize - 1);
689 }
690
691 if (inode_is_stuffed(ip))
692 o += sizeof(struct gfs2_dinode);
693 else if (isdir)
694 o += sizeof(struct gfs2_meta_header);
695
696 while (copied < size) {
697 amount = size - copied;
698 if (amount > sdp->sd_bsize - o)
699 amount = sdp->sd_bsize - o;
700
701 if (!extlen) {
702 new = 1;
703 if (lgfs2_block_map(ip, lblock, &new, &dblock, &extlen, 0))
704 return -1;
705 }
706
707 if (new) {
708 bh = lgfs2_bget(sdp, dblock);
709 if (isdir) {
710 struct gfs2_meta_header mh = {
711 .mh_magic = cpu_to_be32(GFS2_MAGIC),
712 .mh_type = cpu_to_be32(GFS2_METATYPE_JD),
713 .mh_format = cpu_to_be32(GFS2_FORMAT_JD),
714 };
715 memcpy(bh->b_data, &mh, sizeof(mh));
716 lgfs2_bmodified(bh);
717 }
718 } else {
719 if (dblock == ip->i_num.in_addr)
720 bh = ip->i_bh;
721 else
722 bh = lgfs2_bread(sdp, dblock);
723 }
724 copy_from_mem(bh, &buf, o, amount);
725 if (bh != ip->i_bh)
726 lgfs2_brelse(bh);
727
728 copied += amount;
729 lblock++;
730 dblock++;
731 extlen--;
732
733 o = (isdir) ? sizeof(struct gfs2_meta_header) : 0;
734 }
735
736 if (resize && ip->i_size < start + copied) {
737 lgfs2_bmodified(ip->i_bh);
738 ip->i_size = start + copied;
739 }
740
741 return copied;
742 }
743
744 int lgfs2_dirent_first(struct lgfs2_inode *dip, struct lgfs2_buffer_head *bh,
745 struct gfs2_dirent **dent)
746 {
747 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
748
749 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
750 *dent = (struct gfs2_dirent *)(bh->b_data + sizeof(struct gfs2_leaf));
751 return LGFS2_IS_LEAF;
752 } else {
753 *dent = (struct gfs2_dirent *)(bh->b_data + sizeof(struct gfs2_dinode));
754 return LGFS2_IS_DINODE;
755 }
756 }
757
758 int lgfs2_dirent_next(struct lgfs2_inode *dip, struct lgfs2_buffer_head *bh,
759 struct gfs2_dirent **dent)
760 {
761 char *bh_end;
762 uint16_t cur_rec_len;
763
764 bh_end = bh->b_data + dip->i_sbd->sd_bsize;
765 cur_rec_len = be16_to_cpu((*dent)->de_rec_len);
766
767 if (cur_rec_len == 0 || (char *)(*dent) + cur_rec_len >= bh_end)
768 return -ENOENT;
769
770 *dent = (struct gfs2_dirent *)((char *)(*dent) + cur_rec_len);
771
772 return 0;
773 }
774
775 /**
776 * Allocate a gfs2 dirent
777 * Returns 0 on success, with *dent_out pointing to the new dirent,
778 * or -1 on failure, with errno set
779 */
780 static int dirent_alloc(struct lgfs2_inode *dip, struct lgfs2_buffer_head *bh,
781 int name_len, struct gfs2_dirent **dent_out)
782 {
783 struct gfs2_dirent *dent, *new;
784 unsigned int rec_len = GFS2_DIRENT_SIZE(name_len);
785 unsigned int entries = 0, offset = 0;
786 int type;
787
788 type = lgfs2_dirent_first(dip, bh, &dent);
789
790 if (type == LGFS2_IS_LEAF) {
791 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
792 entries = be16_to_cpu(leaf->lf_entries);
793 offset = sizeof(struct gfs2_leaf);
794 } else {
795 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
796 entries = be32_to_cpu(dinode->di_entries);
797 offset = sizeof(struct gfs2_dinode);
798 }
799
800 if (!entries) {
801 dent->de_rec_len = cpu_to_be16(dip->i_sbd->sd_bsize - offset);
802 dent->de_name_len = cpu_to_be16(name_len);
803 lgfs2_bmodified(bh);
804 *dent_out = dent;
805 dip->i_entries++;
806 lgfs2_bmodified(dip->i_bh);
807 return 0;
808 }
809
810 do {
811 uint16_t cur_rec_len;
812 uint16_t cur_name_len;
813 uint16_t new_rec_len;
814
815 cur_rec_len = be16_to_cpu(dent->de_rec_len);
816 cur_name_len = be16_to_cpu(dent->de_name_len);
817
818 if ((!dent->de_inum.no_formal_ino && cur_rec_len >= rec_len) ||
819 (cur_rec_len >= GFS2_DIRENT_SIZE(cur_name_len) + rec_len)) {
820
821 if (dent->de_inum.no_formal_ino) {
822 new = (struct gfs2_dirent *)((char *)dent +
823 GFS2_DIRENT_SIZE(cur_name_len));
824 memset(new, 0, sizeof(struct gfs2_dirent));
825
826 new->de_rec_len = cpu_to_be16(cur_rec_len -
827 GFS2_DIRENT_SIZE(cur_name_len));
828 new->de_name_len = cpu_to_be16(name_len);
829
830 new_rec_len = be16_to_cpu(new->de_rec_len);
831 dent->de_rec_len = cpu_to_be16(cur_rec_len - new_rec_len);
832
833 *dent_out = new;
834 lgfs2_bmodified(bh);
835 dip->i_entries++;
836 lgfs2_bmodified(dip->i_bh);
837 return 0;
838 }
839
840 dent->de_name_len = cpu_to_be16(name_len);
841
842 *dent_out = dent;
843 lgfs2_bmodified(bh);
844 dip->i_entries++;
845 lgfs2_bmodified(dip->i_bh);
846 return 0;
847 }
848 } while (lgfs2_dirent_next(dip, bh, &dent) == 0);
849
850 errno = ENOSPC;
851 return -1;
852 }
853
854 void lgfs2_dirent2_del(struct lgfs2_inode *dip, struct lgfs2_buffer_head *bh,
855 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
856 {
857 uint16_t cur_rec_len, prev_rec_len;
858
859 lgfs2_bmodified(bh);
860 if (lgfs2_check_meta(bh->b_data, GFS2_METATYPE_LF) == 0) {
861 struct gfs2_leaf *lf = (struct gfs2_leaf *)bh->b_data;
862 uint16_t entries;
863
864 entries = be16_to_cpu(lf->lf_entries) - 1;
865 lf->lf_entries = cpu_to_be16(entries);
866 }
867
868 if (dip->i_entries) {
869 lgfs2_bmodified(dip->i_bh);
870 dip->i_entries--;
871 }
872 if (!prev) {
873 cur->de_inum.no_addr = 0;
874 cur->de_inum.no_formal_ino = 0;
875 return;
876 }
877
878 prev_rec_len = be16_to_cpu(prev->de_rec_len);
879 cur_rec_len = be16_to_cpu(cur->de_rec_len);
880
881 prev_rec_len += cur_rec_len;
882 prev->de_rec_len = cpu_to_be16(prev_rec_len);
883 }
884
885 int lgfs2_get_leaf_ptr(struct lgfs2_inode *dip, const uint32_t lindex, uint64_t *ptr)
886 {
887 __be64 leaf_no;
888 int count = lgfs2_readi(dip, (char *)&leaf_no, lindex * sizeof(__be64), sizeof(__be64));
889 if (count != sizeof(__be64))
890 return -1;
891
892 *ptr = be64_to_cpu(leaf_no);
893 return 0;
894 }
895
896 /**
897 * Split a directory leaf.
898 * Returns 0 on success and non-zero with errno set on failure.
899 */
900 int lgfs2_dir_split_leaf(struct lgfs2_inode *dip, uint32_t start, uint64_t leaf_no,
901 struct lgfs2_buffer_head *obh)
902 {
903 struct lgfs2_buffer_head *nbh;
904 struct gfs2_leaf *nleaf, *oleaf;
905 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
906 uint32_t len, half_len, divider;
907 uint16_t depth;
908 uint64_t bn;
909 __be64 *lp;
910 uint32_t name_len;
911 int x, moved = 0;
912 int count;
913
914 if (lgfs2_meta_alloc(dip, &bn)) {
915 errno = EIO;
916 return -1;
917 }
918 nbh = lgfs2_bget(dip->i_sbd, bn);
919 {
920 struct gfs2_meta_header mh = {
921 .mh_magic = cpu_to_be32(GFS2_MAGIC),
922 .mh_type = cpu_to_be32(GFS2_METATYPE_LF),
923 .mh_format = cpu_to_be32(GFS2_FORMAT_LF)
924 };
925 memcpy(nbh->b_data, &mh, sizeof(mh));
926 lgfs2_bmodified(nbh);
927 buffer_clear_tail(dip->i_sbd, nbh,
928 sizeof(struct gfs2_meta_header));
929 }
930
931 nleaf = (struct gfs2_leaf *)nbh->b_data;
932 nleaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
933
934 oleaf = (struct gfs2_leaf *)obh->b_data;
935
936 len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
937 half_len = len >> 1;
938
939 lp = calloc(1, half_len * sizeof(__be64));
940 if (lp == NULL) {
941 lgfs2_bfree(&nbh);
942 return -1;
943 }
944 for (x = 0; x < half_len; x++)
945 lp[x] = cpu_to_be64(bn);
946
947 count = lgfs2_writei(dip, (char *)lp, start * sizeof(uint64_t),
948 half_len * sizeof(uint64_t));
949 free(lp);
950 if (count != half_len * sizeof(uint64_t)) {
951 lgfs2_bfree(&nbh);
952 errno = EINVAL;
953 return -1;
954 }
955 divider = (start + half_len) << (32 - dip->i_depth);
956
957 lgfs2_dirent_first(dip, obh, &dent);
958
959 do {
960 next = dent;
961 if (lgfs2_dirent_next(dip, obh, &next))
962 next = NULL;
963
964 if (dent->de_inum.no_formal_ino &&
965 be32_to_cpu(dent->de_hash) < divider) {
966 uint16_t entries;
967
968 name_len = be16_to_cpu(dent->de_name_len);
969
970 if (dirent_alloc(dip, nbh, name_len, &new)) {
971 lgfs2_bfree(&nbh);
972 errno = ENOSPC;
973 return -1;
974 }
975
976 new->de_inum = dent->de_inum;
977 new->de_hash = dent->de_hash;
978 new->de_type = dent->de_type;
979 memcpy((char *)(new + 1), (char *)(dent + 1), name_len);
980
981 entries = be16_to_cpu(nleaf->lf_entries) + 1;
982 nleaf->lf_entries = cpu_to_be16(entries);
983
984 lgfs2_dirent2_del(dip, obh, prev, dent);
985
986 if (!prev)
987 prev = dent;
988
989 moved = 1;
990 } else
991 prev = dent;
992
993 dent = next;
994 } while (dent);
995
996 if (!moved) {
997 if (dirent_alloc(dip, nbh, 0, &new)) {
998 lgfs2_bfree(&nbh);
999 errno = ENOSPC;
1000 return -1;
1001 }
1002 new->de_inum.no_formal_ino = 0;
1003 /* Don't count the sentinel dirent as an entry */
1004 dip->i_entries--;
1005 }
1006
1007 depth = be16_to_cpu(oleaf->lf_depth) + 1;
1008 oleaf->lf_depth = cpu_to_be16(depth);
1009 nleaf->lf_depth = oleaf->lf_depth;
1010
1011 nleaf->lf_inode = cpu_to_be64(dip->i_num.in_addr);
1012 dip->i_blocks++;
1013 lgfs2_bmodified(dip->i_bh);
1014
1015 lgfs2_bmodified(obh); /* Need to do this in case nothing was moved */
1016 lgfs2_bmodified(nbh);
1017 lgfs2_brelse(nbh);
1018 return 0;
1019 }
1020
1021 static int dir_double_exhash(struct lgfs2_inode *dip)
1022 {
1023 struct lgfs2_sbd *sdp = dip->i_sbd;
1024 uint64_t *buf;
1025 uint64_t *from, *to;
1026 uint64_t block;
1027 int x;
1028 int count;
1029
1030 buf = calloc(1, 3 * sdp->sd_hash_bsize);
1031 if (buf == NULL)
1032 return -1;
1033
1034 for (block = dip->i_size >> sdp->sd_hash_bsize_shift; block--;) {
1035 count = lgfs2_readi(dip, (char *)buf,
1036 block * sdp->sd_hash_bsize,
1037 sdp->sd_hash_bsize);
1038 if (count != sdp->sd_hash_bsize) {
1039 free(buf);
1040 return -1;
1041 }
1042 from = buf;
1043 to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
1044
1045 for (x = sdp->sd_hash_ptrs; x--; from++) {
1046 *to++ = *from;
1047 *to++ = *from;
1048 }
1049 count = lgfs2_writei(dip, (char *)buf + sdp->sd_hash_bsize,
1050 block * sdp->sd_bsize, sdp->sd_bsize);
1051 if (count != sdp->sd_bsize) {
1052 free(buf);
1053 return -1;
1054 }
1055 }
1056
1057 free(buf);
1058
1059 dip->i_depth++;
1060 lgfs2_bmodified(dip->i_bh);
1061 return 0;
1062 }
1063
1064 /**
1065 * get_leaf - Get leaf
1066 * @dip:
1067 * @leaf_no:
1068 * @bh_out:
1069 *
1070 * Returns: 0 on success, error code otherwise
1071 */
1072
1073 int lgfs2_get_leaf(struct lgfs2_inode *dip, uint64_t leaf_no,
1074 struct lgfs2_buffer_head **bhp)
1075 {
1076 int error = 0;
1077
1078 *bhp = lgfs2_bread(dip->i_sbd, leaf_no);
1079 error = lgfs2_check_meta((*bhp)->b_data, GFS2_METATYPE_LF);
1080 if(error)
1081 lgfs2_brelse(*bhp);
1082 return error;
1083 }
1084
1085 /**
1086 * get_first_leaf - Get first leaf
1087 * @dip: The GFS2 inode
1088 * @index:
1089 * @bh_out:
1090 *
1091 * Returns: 0 on success, error code otherwise
1092 */
1093
1094 static int get_first_leaf(struct lgfs2_inode *dip, uint32_t lindex, struct lgfs2_buffer_head **bh_out)
1095 {
1096 uint64_t leaf_no;
1097
1098 if (lgfs2_get_leaf_ptr(dip, lindex, &leaf_no) != 0)
1099 return -1;
1100 *bh_out = lgfs2_bread(dip->i_sbd, leaf_no);
1101 if (*bh_out == NULL)
1102 return -1;
1103 return 0;
1104 }
1105
1106 /**
1107 * get_next_leaf - Get next leaf
1108 * @dip: The GFS2 inode
1109 * @bh_in: The buffer
1110 * @bh_out:
1111 *
1112 * Returns: 0 on success, error code otherwise
1113 */
1114
1115 static int get_next_leaf(struct lgfs2_inode *dip,struct lgfs2_buffer_head *bh_in,
1116 struct lgfs2_buffer_head **bh_out)
1117 {
1118 struct gfs2_leaf *leaf;
1119
1120 leaf = (struct gfs2_leaf *)bh_in->b_data;
1121
1122 if (!leaf->lf_next)
1123 return -1;
1124 /* Check for a leaf that points to itself as "next" */
1125 if (be64_to_cpu(leaf->lf_next) == bh_in->b_blocknr)
1126 return -1;
1127 *bh_out = lgfs2_bread(dip->i_sbd, be64_to_cpu(leaf->lf_next));
1128 if (*bh_out == NULL)
1129 return -ENOENT;
1130 /* Check for a leaf pointing to a non-leaf */
1131 if (lgfs2_check_meta((*bh_out)->b_data, GFS2_METATYPE_LF)) {
1132 lgfs2_brelse(*bh_out);
1133 *bh_out = NULL;
1134 return -ENOENT;
1135 }
1136 return 0;
1137 }
1138
1139 static int dir_e_add(struct lgfs2_inode *dip, const char *filename, int len,
1140 struct lgfs2_inum *inum, unsigned int type)
1141 {
1142 struct lgfs2_buffer_head *bh;
1143 struct gfs2_leaf *leaf, *nleaf;
1144 struct gfs2_dirent *dent;
1145 uint32_t lindex, llen;
1146 uint32_t hash;
1147 uint64_t leaf_no, bn;
1148 int err = 0;
1149
1150 hash = lgfs2_disk_hash(filename, len);
1151 restart:
1152 /* (hash >> 32) is undefined behaviour */
1153 if (dip->i_depth)
1154 lindex = hash >> (32 - dip->i_depth);
1155 else
1156 lindex = 0;
1157
1158 err = lgfs2_get_leaf_ptr(dip, lindex, &leaf_no);
1159 if (err)
1160 return err;
1161
1162 for (;;) {
1163 uint16_t entries;
1164
1165 bh = lgfs2_bread(dip->i_sbd, leaf_no);
1166 leaf = (struct gfs2_leaf *)bh->b_data;
1167
1168 if (dirent_alloc(dip, bh, len, &dent)) {
1169
1170 if (be16_to_cpu(leaf->lf_depth) < dip->i_depth) {
1171 llen = 1 << (dip->i_depth -
1172 be16_to_cpu(leaf->lf_depth));
1173 if (lgfs2_dir_split_leaf(dip, lindex & ~(llen - 1), leaf_no, bh)) {
1174 lgfs2_brelse(bh);
1175 return -1;
1176 }
1177 lgfs2_brelse(bh);
1178 goto restart;
1179
1180 } else if (dip->i_depth < GFS2_DIR_MAX_DEPTH) {
1181 lgfs2_brelse(bh);
1182 if (dir_double_exhash(dip))
1183 return -1;
1184 goto restart;
1185
1186 } else if (leaf->lf_next) {
1187 leaf_no = be64_to_cpu(leaf->lf_next);
1188 lgfs2_brelse(bh);
1189 continue;
1190
1191 } else {
1192 struct lgfs2_buffer_head *nbh;
1193 struct gfs2_meta_header mh = {
1194 .mh_magic = cpu_to_be32(GFS2_MAGIC),
1195 .mh_type = cpu_to_be32(GFS2_METATYPE_LF),
1196 .mh_format = cpu_to_be32(GFS2_FORMAT_LF)
1197 };
1198 if (lgfs2_meta_alloc(dip, &bn)) {
1199 lgfs2_bfree(&bh);
1200 return -1;
1201 }
1202 nbh = lgfs2_bget(dip->i_sbd, bn);
1203 memcpy(nbh->b_data, &mh, sizeof(mh));
1204 lgfs2_bmodified(nbh);
1205
1206 leaf->lf_next = cpu_to_be64(bn);
1207
1208 nleaf = (struct gfs2_leaf *)nbh->b_data;
1209 nleaf->lf_depth = leaf->lf_depth;
1210 nleaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
1211 nleaf->lf_inode = cpu_to_be64(dip->i_num.in_addr);
1212 err = dirent_alloc(dip, nbh, len, &dent);
1213 if (err) {
1214 lgfs2_bfree(&nbh);
1215 lgfs2_bfree(&bh);
1216 return err;
1217 }
1218 dip->i_blocks++;
1219 lgfs2_bmodified(dip->i_bh);
1220 lgfs2_bmodified(bh);
1221 lgfs2_brelse(bh);
1222 bh = nbh;
1223 leaf = nleaf;
1224 }
1225 }
1226
1227 lgfs2_inum_out(inum, &dent->de_inum);
1228 dent->de_hash = cpu_to_be32(hash);
1229 dent->de_type = cpu_to_be16(type);
1230 memcpy((char *)(dent + 1), filename, len);
1231
1232 entries = be16_to_cpu(leaf->lf_entries) + 1;
1233 leaf->lf_entries = cpu_to_be16(entries);
1234
1235 lgfs2_bmodified(bh);
1236 lgfs2_brelse(bh);
1237 return err;
1238 }
1239 }
1240
1241 static int dir_make_exhash(struct lgfs2_inode *dip)
1242 {
1243 struct lgfs2_sbd *sdp = dip->i_sbd;
1244 struct gfs2_dirent *dent;
1245 struct lgfs2_buffer_head *bh;
1246 struct gfs2_leaf *leaf;
1247 uint16_t rec_len;
1248 int y;
1249 uint32_t x;
1250 uint64_t bn;
1251 __be64 *lp;
1252
1253 if (lgfs2_meta_alloc(dip, &bn))
1254 return -1;
1255 bh = lgfs2_bget(sdp, bn);
1256 {
1257 struct gfs2_meta_header mh = {
1258 .mh_magic = cpu_to_be32(GFS2_MAGIC),
1259 .mh_type = cpu_to_be32(GFS2_METATYPE_LF),
1260 .mh_format = cpu_to_be32(GFS2_FORMAT_LF)
1261 };
1262 memcpy(bh->b_data, &mh, sizeof(mh));
1263 lgfs2_bmodified(bh);
1264 }
1265
1266 leaf = (struct gfs2_leaf *)bh->b_data;
1267 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
1268 leaf->lf_entries = cpu_to_be16(dip->i_entries);
1269 leaf->lf_inode = cpu_to_be64(dip->i_num.in_addr);
1270 buffer_copy_tail(sdp, bh, sizeof(struct gfs2_leaf),
1271 dip->i_bh, sizeof(struct gfs2_dinode));
1272
1273 x = 0;
1274 lgfs2_dirent_first(dip, bh, &dent);
1275
1276 do {
1277 if (!dent->de_inum.no_formal_ino)
1278 continue;
1279 if (++x == dip->i_entries)
1280 break;
1281 } while (lgfs2_dirent_next(dip, bh, &dent) == 0);
1282
1283 rec_len = be16_to_cpu(dent->de_rec_len) +
1284 sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf);
1285 dent->de_rec_len = cpu_to_be16(rec_len);
1286
1287 /* no need to: lgfs2_bmodified(bh); (buffer_copy_tail does it) */
1288 lgfs2_brelse(bh);
1289
1290 buffer_clear_tail(sdp, dip->i_bh, sizeof(struct gfs2_dinode));
1291
1292 lp = (__be64 *)(dip->i_bh->b_data + sizeof(struct gfs2_dinode));
1293
1294 for (x = sdp->sd_hash_ptrs; x--; lp++)
1295 *lp = cpu_to_be64(bn);
1296
1297 dip->i_size = sdp->sd_bsize / 2;
1298 dip->i_blocks++;
1299 dip->i_flags |= GFS2_DIF_EXHASH;
1300 dip->i_payload_format = 0;
1301 /* no need: lgfs2_bmodified(dip->i_bh); buffer_clear_tail does it. */
1302
1303 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
1304 dip->i_depth = y;
1305
1306 lgfs2_dinode_out(dip, dip->i_bh->b_data);
1307 lgfs2_bwrite(dip->i_bh);
1308 return 0;
1309 }
1310
1311 static int dir_l_add(struct lgfs2_inode *dip, const char *filename, int len,
1312 struct lgfs2_inum *inum, unsigned int type)
1313 {
1314 struct gfs2_dirent *dent;
1315 uint32_t de_hash;
1316 int err = 0;
1317
1318 if (dirent_alloc(dip, dip->i_bh, len, &dent)) {
1319 err = dir_make_exhash(dip);
1320 if (err != 0)
1321 return err;
1322 err = dir_e_add(dip, filename, len, inum, type);
1323 return err;
1324 }
1325
1326 lgfs2_inum_out(inum, &dent->de_inum);
1327 de_hash = lgfs2_disk_hash(filename, len);
1328 dent->de_hash = cpu_to_be32(de_hash);
1329 dent->de_type = cpu_to_be16(type);
1330 memcpy((char *)(dent + 1), filename, len);
1331 lgfs2_bmodified(dip->i_bh);
1332 return err;
1333 }
1334
1335 int lgfs2_dir_add(struct lgfs2_inode *dip, const char *filename, int len,
1336 struct lgfs2_inum *inum, unsigned int type)
1337 {
1338 int err = 0;
1339 if (dip->i_flags & GFS2_DIF_EXHASH)
1340 err = dir_e_add(dip, filename, len, inum, type);
1341 else
1342 err = dir_l_add(dip, filename, len, inum, type);
1343 return err;
1344 }
1345
1346 static int __init_dinode(struct lgfs2_sbd *sdp, struct lgfs2_buffer_head **bhp, struct lgfs2_inum *inum,
1347 unsigned int mode, uint32_t flags, struct lgfs2_inum *parent)
1348 {
1349 struct lgfs2_buffer_head *bh;
1350 struct gfs2_dinode *di;
1351 int is_dir = S_ISDIR(mode);
1352
1353 errno = EINVAL;
1354 if (bhp == NULL)
1355 return 1;
1356
1357 if (*bhp == NULL) {
1358 *bhp = lgfs2_bget(sdp, inum->in_addr);
1359 if (*bhp == NULL)
1360 return 1;
1361 }
1362
1363 bh = *bhp;
1364
1365 di = (struct gfs2_dinode *)bh->b_data;
1366
1367 di->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
1368 di->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
1369 di->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
1370 di->di_num.no_formal_ino = cpu_to_be64(inum->in_formal_ino);
1371 di->di_num.no_addr = cpu_to_be64(inum->in_addr);
1372 di->di_mode = cpu_to_be32(mode);
1373 di->di_nlink = cpu_to_be32(1);
1374 di->di_blocks = cpu_to_be64(1);
1375 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(sdp->sd_time);
1376 di->di_goal_meta = di->di_goal_data = cpu_to_be64(bh->b_blocknr);
1377 di->di_flags = cpu_to_be32(flags);
1378
1379 if (is_dir) {
1380 char *p = bh->b_data + sizeof(*di);
1381 struct gfs2_dirent de = {0};
1382 uint32_t hash;
1383 uint16_t len;
1384
1385 hash = lgfs2_disk_hash(".", 1);
1386 len = GFS2_DIRENT_SIZE(1);
1387 de.de_inum = di->di_num;
1388 de.de_hash = cpu_to_be32(hash);
1389 de.de_rec_len = cpu_to_be16(len);
1390 de.de_name_len = cpu_to_be16(1);
1391 de.de_type = cpu_to_be16(IF2DT(S_IFDIR));
1392 memcpy(p, &de, sizeof(de));
1393 p[sizeof(de)] = '.';
1394 p += len;
1395
1396 hash = lgfs2_disk_hash("..", 2);
1397 len = sdp->sd_bsize - (p - bh->b_data);
1398 de.de_inum.no_formal_ino = cpu_to_be64(parent->in_formal_ino);
1399 de.de_inum.no_addr = cpu_to_be64(parent->in_addr);
1400 de.de_hash = cpu_to_be32(hash);
1401 de.de_rec_len = cpu_to_be16(len);
1402 de.de_name_len = cpu_to_be16(2);
1403 de.de_type = cpu_to_be16(IF2DT(S_IFDIR));
1404 memcpy(p, &de, sizeof(de));
1405 p += sizeof(de);
1406 *p++ = '.';
1407 *p = '.';
1408
1409 di->di_nlink = cpu_to_be32(2);
1410 di->di_size = cpu_to_be64(sdp->sd_bsize - sizeof(struct gfs2_dinode));
1411 di->di_flags = cpu_to_be32(flags | GFS2_DIF_JDATA);
1412 di->di_payload_format = cpu_to_be32(GFS2_FORMAT_DE);
1413 di->di_entries = cpu_to_be32(2);
1414 }
1415 lgfs2_bmodified(bh);
1416 return 0;
1417 }
1418
1419 int lgfs2_init_dinode(struct lgfs2_sbd *sdp, struct lgfs2_buffer_head **bhp, struct lgfs2_inum *inum,
1420 unsigned int mode, uint32_t flags, struct lgfs2_inum *parent)
1421 {
1422 return __init_dinode(sdp, bhp, inum, mode, flags, parent);
1423 }
1424
1425 static void lgfs2_fill_indir(char *start, char *end, uint64_t ptr0, unsigned n, unsigned *p)
1426 {
1427 char *bp;
1428 memset(start, 0, end - start);
1429 for (bp = start; bp < end && *p < n; bp += sizeof(uint64_t)) {
1430 uint64_t pn = ptr0 + *p;
1431 *(__be64 *)bp = cpu_to_be64(pn);
1432 (*p)++;
1433 }
1434 }
1435
1436 /**
1437 * Calculate and write the indirect blocks for a single-extent file of a given
1438 * size.
1439 * ip: The inode for which to write indirect blocks, with fields already set
1440 * appropriately (see lgfs2_file_alloc).
1441 * Returns 0 on success or non-zero with errno set on failure.
1442 */
1443 int lgfs2_write_filemeta(struct lgfs2_inode *ip)
1444 {
1445 unsigned height = 0;
1446 struct lgfs2_metapath mp;
1447 struct lgfs2_sbd *sdp = ip->i_sbd;
1448 uint64_t dblocks = (ip->i_size + sdp->sd_bsize - 1) / sdp->sd_bsize;
1449 uint64_t ptr0 = ip->i_num.in_addr + 1;
1450 unsigned ptrs = 1;
1451 struct gfs2_meta_header mh = {
1452 .mh_magic = cpu_to_be32(GFS2_MAGIC),
1453 .mh_type = cpu_to_be32(GFS2_METATYPE_IN),
1454 .mh_format = cpu_to_be32(GFS2_FORMAT_IN)
1455 };
1456 struct lgfs2_buffer_head *bh = lgfs2_bget(sdp, ip->i_num.in_addr);
1457 if (bh == NULL)
1458 return 1;
1459
1460 /* Using lgfs2_find_metapath() to find the last data block in the file will
1461 effectively give a remainder for the number of pointers at each
1462 height. Just need to add 1 to convert ptr index to quantity later. */
1463 lgfs2_find_metapath(ip, dblocks - 1, &mp);
1464
1465 for (height = 0; height < ip->i_height; height++) {
1466 unsigned p;
1467 /* The number of pointers in this height will be the number of
1468 full indirect blocks pointed to by the previous height
1469 multiplied by the pointer capacity of an indirect block,
1470 plus the remainder which lgfs2_find_metapath() gave us. */
1471 ptrs = ((ptrs - 1) * sdp->sd_inptrs) + mp.mp_list[height] + 1;
1472
1473 for (p = 0; p < ptrs; bh->b_blocknr++) {
1474 char *start = bh->b_data;
1475 if (height == 0) {
1476 start += sizeof(struct gfs2_dinode);
1477 lgfs2_dinode_out(ip, bh->b_data);
1478 } else {
1479 start += sizeof(struct gfs2_meta_header);
1480 memcpy(bh->b_data, &mh, sizeof(mh));
1481 }
1482 lgfs2_fill_indir(start, bh->b_data + sdp->sd_bsize, ptr0, ptrs, &p);
1483 if (lgfs2_bwrite(bh)) {
1484 free(bh);
1485 return 1;
1486 }
1487 }
1488 ptr0 += ptrs;
1489 }
1490 free(bh);
1491 return 0;
1492 }
1493
1494 static struct lgfs2_inode *do_createi(struct lgfs2_inode *dip, const char *filename,
1495 unsigned int mode, uint32_t flags)
1496 {
1497 struct lgfs2_inum parent = dip->i_num;
1498 struct lgfs2_sbd *sdp = dip->i_sbd;
1499 uint64_t bn;
1500 struct lgfs2_inum inum;
1501 struct lgfs2_buffer_head *bh = NULL;
1502 struct lgfs2_inode *ip;
1503 int err = 0;
1504 int is_dir;
1505
1506 err = lgfs2_dinode_alloc(sdp, 1, &bn);
1507 if (err != 0)
1508 return NULL;
1509
1510 inum.in_formal_ino = sdp->md.next_inum++;
1511 inum.in_addr = bn;
1512
1513 err = lgfs2_dir_add(dip, filename, strlen(filename), &inum, IF2DT(mode));
1514 if (err)
1515 return NULL;
1516
1517 is_dir = S_ISDIR(mode);
1518 if (is_dir) {
1519 lgfs2_bmodified(dip->i_bh);
1520 dip->i_nlink++;
1521 }
1522 err = __init_dinode(sdp, &bh, &inum, mode, flags, &parent);
1523 if (err != 0)
1524 return NULL;
1525
1526 ip = lgfs2_inode_get(sdp, bh);
1527 if (ip == NULL)
1528 return NULL;
1529 lgfs2_bmodified(bh);
1530 ip->bh_owned = 1;
1531 return ip;
1532 }
1533
1534 /**
1535 * Create an inode and link it into a directory. If it already exists, return
1536 * the existing inode. To create gfs1 inodes, dip->i_sbd->gfs1 must be set.
1537 * @dip: The inode of the parent directory.
1538 * @filename: The new inode's filename.
1539 * @mode: The mode of the new inode.
1540 * @flags: The flags of the new inode.
1541 * Returns the new or existing inode on success or NULL on failure with errno set.
1542 */
1543 struct lgfs2_inode *lgfs2_createi(struct lgfs2_inode *dip, const char *filename,
1544 unsigned int mode, uint32_t flags)
1545 {
1546 struct lgfs2_inode *ip = lgfs2_lookupi(dip, filename, strlen(filename));
1547
1548 if (ip != NULL) {
1549 ip->bh_owned = 1;
1550 return ip;
1551 }
1552 return do_createi(dip, filename, mode, flags);
1553 }
1554
1555 /**
1556 * gfs2_filecmp - Compare two filenames
1557 * @file1: The first filename
1558 * @file2: The second filename
1559 * @len_of_file2: The length of the second file
1560 *
1561 * This routine compares two filenames and returns 1 if they are equal.
1562 *
1563 * Returns: 1 if the files are the same, otherwise 0.
1564 */
1565
1566 static int gfs2_filecmp(const char *file1, const char *file2, int len_of_file2)
1567 {
1568 if (strlen(file1) != len_of_file2)
1569 return 0;
1570 if (memcmp(file1, file2, len_of_file2))
1571 return 0;
1572 return 1;
1573 }
1574
1575 /**
1576 * leaf_search
1577 * @bh:
1578 * @id:
1579 * @dent_out:
1580 * @dent_prev:
1581 *
1582 * Returns:
1583 */
1584 static int leaf_search(struct lgfs2_inode *dip, struct lgfs2_buffer_head *bh,
1585 const char *filename, int len,
1586 struct gfs2_dirent **dent_out,
1587 struct gfs2_dirent **dent_prev)
1588 {
1589 uint32_t hash;
1590 struct gfs2_dirent *dent, *prev = NULL;
1591 unsigned int entries = 0, x = 0;
1592 int type;
1593
1594 type = lgfs2_dirent_first(dip, bh, &dent);
1595
1596 if (type == LGFS2_IS_LEAF){
1597 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1598 entries = be16_to_cpu(leaf->lf_entries);
1599 } else if (type == LGFS2_IS_DINODE)
1600 entries = dip->i_entries;
1601 else
1602 return -1;
1603
1604 hash = lgfs2_disk_hash(filename, len);
1605
1606 do{
1607 if (!dent->de_inum.no_formal_ino){
1608 prev = dent;
1609 continue;
1610 }
1611
1612 if (be32_to_cpu(dent->de_hash) == hash &&
1613 gfs2_filecmp(filename, (char *)(dent + 1),
1614 be16_to_cpu(dent->de_name_len))) {
1615 *dent_out = dent;
1616 if (dent_prev)
1617 *dent_prev = prev;
1618 return 0;
1619 }
1620
1621 if(x >= entries)
1622 return -1;
1623 x++;
1624 prev = dent;
1625 } while (lgfs2_dirent_next(dip, bh, &dent) == 0);
1626
1627 return -ENOENT;
1628 }
1629
1630 /**
1631 * linked_leaf_search - Linked leaf search
1632 * @dip: The GFS2 inode
1633 * @id:
1634 * @dent_out:
1635 * @dent_prev:
1636 * @bh_out:
1637 *
1638 * Returns: 0 on sucess, error code otherwise
1639 */
1640
1641 static int linked_leaf_search(struct lgfs2_inode *dip, const char *filename,
1642 int len, struct gfs2_dirent **dent_out,
1643 struct lgfs2_buffer_head **bh_out)
1644 {
1645 struct lgfs2_buffer_head *bh = NULL, *bh_next;
1646 uint32_t hsize, lindex;
1647 uint32_t hash;
1648 int error = 0;
1649
1650 hsize = 1 << dip->i_depth;
1651 if(hsize * sizeof(uint64_t) != dip->i_size)
1652 return -1;
1653
1654 /* Figure out the address of the leaf node. */
1655
1656 hash = lgfs2_disk_hash(filename, len);
1657 lindex = hash >> (32 - dip->i_depth);
1658
1659 error = get_first_leaf(dip, lindex, &bh_next);
1660 if (error)
1661 return error;
1662 if (bh_next == NULL)
1663 return errno;
1664
1665 /* Find the entry */
1666 do{
1667 if (bh && bh != dip->i_bh)
1668 lgfs2_brelse(bh);
1669
1670 bh = bh_next;
1671
1672 error = leaf_search(dip, bh, filename, len, dent_out, NULL);
1673 switch (error){
1674 case 0:
1675 *bh_out = bh;
1676 return 0;
1677
1678 case -ENOENT:
1679 break;
1680
1681 default:
1682 if (bh && bh != dip->i_bh)
1683 lgfs2_brelse(bh);
1684 return error;
1685 }
1686
1687 error = get_next_leaf(dip, bh, &bh_next);
1688 } while (!error && bh_next != NULL);
1689
1690 if (bh && bh != dip->i_bh)
1691 lgfs2_brelse(bh);
1692
1693 return error;
1694 }
1695
1696 /**
1697 * dir_e_search -
1698 * @dip: The GFS2 inode
1699 * @id:
1700 * @inode:
1701 *
1702 * Returns:
1703 */
1704 static int dir_e_search(struct lgfs2_inode *dip, const char *filename,
1705 int len, unsigned int *type, struct lgfs2_inum *inum)
1706 {
1707 struct lgfs2_buffer_head *bh = NULL;
1708 struct gfs2_dirent *dent;
1709 int error;
1710
1711 error = linked_leaf_search(dip, filename, len, &dent, &bh);
1712 if (error)
1713 return error;
1714
1715 lgfs2_inum_in(inum, &dent->de_inum);
1716 if (type)
1717 *type = be16_to_cpu(dent->de_type);
1718
1719 lgfs2_brelse(bh);
1720
1721 return 0;
1722 }
1723
1724
1725 /**
1726 * dir_l_search -
1727 * @dip: The GFS2 inode
1728 * @id:
1729 * @inode:
1730 *
1731 * Returns:
1732 */
1733 static int dir_l_search(struct lgfs2_inode *dip, const char *filename,
1734 int len, unsigned int *type, struct lgfs2_inum *inum)
1735 {
1736 struct gfs2_dirent *dent;
1737 int error;
1738
1739 if(!inode_is_stuffed(dip))
1740 return -1;
1741
1742 error = leaf_search(dip, dip->i_bh, filename, len, &dent, NULL);
1743 if (!error) {
1744 lgfs2_inum_in(inum, &dent->de_inum);
1745 if(type)
1746 *type = be16_to_cpu(dent->de_type);
1747 }
1748 return error;
1749 }
1750
1751 /**
1752 * lgfs2_dir_search - Search a directory
1753 * @dip: The GFS inode
1754 * @id
1755 * @type:
1756 *
1757 * This routine searches a directory for a file or another directory
1758 * given its filename. The component of the identifier that is
1759 * not being used to search will be filled in and must be freed by
1760 * the caller.
1761 *
1762 * Returns: 0 if found, -1 on failure, -ENOENT if not found.
1763 */
1764 int lgfs2_dir_search(struct lgfs2_inode *dip, const char *filename, int len,
1765 unsigned int *type, struct lgfs2_inum *inum)
1766 {
1767 int error;
1768
1769 if (!S_ISDIR(dip->i_mode))
1770 return -1;
1771
1772 if (dip->i_flags & GFS2_DIF_EXHASH)
1773 error = dir_e_search(dip, filename, len, type, inum);
1774 else
1775 error = dir_l_search(dip, filename, len, type, inum);
1776
1777 return error;
1778 }
1779
1780 static int dir_e_del(struct lgfs2_inode *dip, const char *filename, int len)
1781 {
1782 int lindex;
1783 int error;
1784 int found = 0;
1785 uint64_t leaf_no;
1786 struct lgfs2_buffer_head *bh = NULL;
1787 struct gfs2_dirent *cur, *prev;
1788
1789 lindex = (1 << (dip->i_depth))-1;
1790
1791 for(; (lindex >= 0) && !found; lindex--){
1792 error = lgfs2_get_leaf_ptr(dip, lindex, &leaf_no);
1793 if (error)
1794 return error;
1795
1796 while(leaf_no && !found){
1797 bh = lgfs2_bread(dip->i_sbd, leaf_no);
1798 error = leaf_search(dip, bh, filename, len, &cur, &prev);
1799 if (error) {
1800 if(error != -ENOENT){
1801 lgfs2_brelse(bh);
1802 return -1;
1803 }
1804 leaf_no = be64_to_cpu(((struct gfs2_leaf *)bh->b_data)->lf_next);
1805 lgfs2_brelse(bh);
1806 } else
1807 found = 1;
1808 }
1809 }
1810
1811 if(!found)
1812 return 1;
1813
1814 if (bh) {
1815 lgfs2_dirent2_del(dip, bh, prev, cur);
1816 lgfs2_brelse(bh);
1817 }
1818 return 0;
1819 }
1820
1821 static int dir_l_del(struct lgfs2_inode *dip, const char *filename, int len)
1822 {
1823 int error=0;
1824 struct gfs2_dirent *cur, *prev;
1825
1826 if(!inode_is_stuffed(dip))
1827 return -1;
1828
1829 error = leaf_search(dip, dip->i_bh, filename, len, &cur, &prev);
1830 if (error) {
1831 if (error == -ENOENT)
1832 return 1;
1833 else
1834 return -1;
1835 }
1836
1837 lgfs2_dirent2_del(dip, dip->i_bh, prev, cur);
1838 return 0;
1839 }
1840
1841
1842 /*
1843 * lgfs2_dirent_del
1844 * @dip
1845 * filename
1846 *
1847 * Delete a directory entry from a directory. This _only_
1848 * removes the directory entry - leaving the dinode in
1849 * place. (Likely without a link.)
1850 *
1851 * Returns: 0 on success (or if it doesn't already exist), -1 on failure
1852 */
1853 int lgfs2_dirent_del(struct lgfs2_inode *dip, const char *filename, int len)
1854 {
1855 int error;
1856
1857 if(!S_ISDIR(dip->i_mode))
1858 return -1;
1859
1860 if (dip->i_flags & GFS2_DIF_EXHASH)
1861 error = dir_e_del(dip, filename, len);
1862 else
1863 error = dir_l_del(dip, filename, len);
1864 lgfs2_bmodified(dip->i_bh);
1865 return error;
1866 }
1867
1868 /**
1869 * Look up a filename in a directory and return its inode, which can be the
1870 * the directory inode when "." is looked up.
1871 * @dip: The directory to search
1872 * @name: The name of the inode to look for
1873 * @len: The length of name
1874 *
1875 * Returns: The inode on success or NULL on failure with errno set.
1876 */
1877 struct lgfs2_inode *lgfs2_lookupi(struct lgfs2_inode *dip, const char *filename, int len)
1878 {
1879 struct lgfs2_sbd *sdp;
1880 struct lgfs2_inum inum;
1881 int error = 0;
1882
1883 errno = EINVAL;
1884 if (dip == NULL)
1885 return NULL;
1886 sdp = dip->i_sbd;
1887 errno = ENAMETOOLONG;
1888 if (!len || len > GFS2_FNAMESIZE)
1889 return NULL;
1890
1891 if (gfs2_filecmp(filename, ".", 1))
1892 return dip;
1893
1894 error = lgfs2_dir_search(dip, filename, len, NULL, &inum);
1895 if (error)
1896 return NULL;
1897 return lgfs2_inode_read(sdp, inum.in_addr);
1898 }
1899
1900 /**
1901 * lgfs2_free_block - free up a block given its block number
1902 */
1903 void lgfs2_free_block(struct lgfs2_sbd *sdp, uint64_t block)
1904 {
1905 struct lgfs2_rgrp_tree *rgd;
1906
1907 /* Adjust the free space count for the freed block */
1908 rgd = lgfs2_blk2rgrpd(sdp, block); /* find the rg for indir block */
1909 if (rgd) {
1910 lgfs2_set_bitmap(rgd, block, GFS2_BLKST_FREE);
1911 rgd->rt_free++; /* adjust the free count */
1912 lgfs2_rgrp_out(rgd, rgd->rt_bits[0].bi_data);
1913 rgd->rt_bits[0].bi_modified = 1;
1914 sdp->blks_alloced--;
1915 }
1916 }
1917