Coverage Report

Created: 2024-09-08 06:27

/src/e2fsprogs/lib/ext2fs/link.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * link.c --- create links in a ext2fs directory
3
 *
4
 * Copyright (C) 1993, 1994 Theodore Ts'o.
5
 *
6
 * %Begin-Header%
7
 * This file may be redistributed under the terms of the GNU Library
8
 * General Public License, version 2.
9
 * %End-Header%
10
 */
11
12
#include "config.h"
13
#include <stdio.h>
14
#include <string.h>
15
#if HAVE_UNISTD_H
16
#include <unistd.h>
17
#endif
18
19
#include "ext2_fs.h"
20
#include "ext2fs.h"
21
#include "ext2fsP.h"
22
23
0
#define EXT2_DX_ROOT_OFF 24
24
25
struct dx_frame {
26
  void *buf;
27
  blk64_t pblock;
28
  struct ext2_dx_countlimit *head;
29
  struct ext2_dx_entry *entries;
30
  struct ext2_dx_entry *at;
31
};
32
33
struct dx_lookup_info {
34
  const char *name;
35
  int namelen;
36
  int hash_alg;
37
  __u32 hash;
38
  unsigned levels;
39
  struct dx_frame frames[EXT4_HTREE_LEVEL];
40
};
41
42
static errcode_t alloc_dx_frame(ext2_filsys fs, struct dx_frame *frame)
43
0
{
44
0
  return ext2fs_get_mem(fs->blocksize, &frame->buf);
45
0
}
46
47
static void dx_release(struct dx_lookup_info *info)
48
0
{
49
0
  unsigned level;
50
51
0
  for (level = 0; level < info->levels; level++) {
52
0
    if (info->frames[level].buf == NULL)
53
0
      break;
54
0
    ext2fs_free_mem(&(info->frames[level].buf));
55
0
  }
56
0
  info->levels = 0;
57
0
}
58
59
static void dx_search_entry(struct dx_frame *frame, int count, __u32 hash)
60
0
{
61
0
  struct ext2_dx_entry *p, *q, *m;
62
63
0
  p = frame->entries + 1;
64
0
  q = frame->entries + count - 1;
65
0
  while (p <= q) {
66
0
    m = p + (q - p) / 2;
67
0
    if (ext2fs_le32_to_cpu(m->hash) > hash)
68
0
      q = m - 1;
69
0
    else
70
0
      p = m + 1;
71
0
  }
72
0
  frame->at = p - 1;
73
0
}
74
75
static errcode_t load_logical_dir_block(ext2_filsys fs, ext2_ino_t dir,
76
          struct ext2_inode *diri, blk64_t block,
77
          blk64_t *pblk, void *buf)
78
0
{
79
0
  errcode_t errcode;
80
0
  int ret_flags;
81
82
0
  errcode = ext2fs_bmap2(fs, dir, diri, NULL, 0, block, &ret_flags,
83
0
             pblk);
84
0
  if (errcode)
85
0
    return errcode;
86
0
  if (ret_flags & BMAP_RET_UNINIT)
87
0
    return EXT2_ET_DIR_CORRUPTED;
88
0
  return ext2fs_read_dir_block4(fs, *pblk, buf, 0, dir);
89
0
}
90
91
static errcode_t dx_lookup(ext2_filsys fs, ext2_ino_t dir,
92
         struct ext2_inode *diri, struct dx_lookup_info *info)
93
0
{
94
0
  struct ext2_dx_root_info *root;
95
0
  errcode_t errcode;
96
0
  int level = 0;
97
0
  int count, limit;
98
0
  int hash_alg;
99
0
  int hash_flags = diri->i_flags & EXT4_CASEFOLD_FL;
100
0
  __u32 minor_hash;
101
0
  struct dx_frame *frame;
102
103
0
  errcode = alloc_dx_frame(fs, &(info->frames[0]));
104
0
  if (errcode)
105
0
    return errcode;
106
0
  info->levels = 1;
107
108
0
  errcode = load_logical_dir_block(fs, dir, diri, 0,
109
0
           &(info->frames[0].pblock),
110
0
           info->frames[0].buf);
111
0
  if (errcode)
112
0
    goto out_err;
113
0
  root = (struct ext2_dx_root_info *) ((char *)info->frames[0].buf +
114
0
               EXT2_DX_ROOT_OFF);
115
0
  hash_alg = root->hash_version;
116
0
  if (hash_alg != EXT2_HASH_TEA && hash_alg != EXT2_HASH_HALF_MD4 &&
117
0
      hash_alg != EXT2_HASH_LEGACY) {
118
0
    errcode = EXT2_ET_DIRHASH_UNSUPP;
119
0
    goto out_err;
120
0
  }
121
0
  if (hash_alg <= EXT2_HASH_TEA &&
122
0
      fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH)
123
0
    hash_alg += 3;
124
0
  if (root->indirect_levels >= ext2_dir_htree_level(fs)) {
125
0
    errcode = EXT2_ET_DIR_CORRUPTED;
126
0
    goto out_err;
127
0
  }
128
0
  info->hash_alg = hash_alg;
129
130
0
  errcode = ext2fs_dirhash2(hash_alg, info->name, info->namelen,
131
0
          fs->encoding, hash_flags,
132
0
          fs->super->s_hash_seed, &info->hash,
133
0
          &minor_hash);
134
0
  if (errcode)
135
0
    goto out_err;
136
137
0
  for (level = 0; level <= root->indirect_levels; level++) {
138
0
    frame = &(info->frames[level]);
139
0
    if (level > 0) {
140
0
      errcode = alloc_dx_frame(fs, frame);
141
0
      if (errcode)
142
0
        goto out_err;
143
0
      info->levels++;
144
145
0
      errcode = load_logical_dir_block(fs, dir, diri,
146
0
        ext2fs_le32_to_cpu(info->frames[level-1].at->block) & 0x0fffffff,
147
0
        &(frame->pblock), frame->buf);
148
0
      if (errcode)
149
0
        goto out_err;
150
0
    }
151
0
    errcode = ext2fs_get_dx_countlimit(fs, frame->buf,
152
0
               &(frame->head), NULL);
153
0
    if (errcode)
154
0
      goto out_err;
155
0
    count = ext2fs_le16_to_cpu(frame->head->count);
156
0
    limit = ext2fs_le16_to_cpu(frame->head->limit);
157
0
    frame->entries = (struct ext2_dx_entry *)(frame->head);
158
0
    if (!count || count > limit) {
159
0
      errcode = EXT2_ET_DIR_CORRUPTED;
160
0
      goto out_err;
161
0
    }
162
163
0
    dx_search_entry(frame, count, info->hash);
164
0
  }
165
0
  return 0;
166
0
out_err:
167
0
  dx_release(info);
168
0
  return errcode;
169
0
}
170
171
struct link_struct  {
172
  ext2_filsys fs;
173
  const char  *name;
174
  int   namelen;
175
  ext2_ino_t  inode;
176
  int   flags;
177
  int   done;
178
  unsigned int  blocksize;
179
  errcode_t err;
180
  struct ext2_super_block *sb;
181
};
182
183
static int link_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
184
         int entru EXT2FS_ATTR((unused)),
185
         struct ext2_dir_entry *dirent,
186
         int  offset,
187
         int  blocksize,
188
         char *buf,
189
         void *priv_data)
190
0
{
191
0
  struct link_struct *ls = (struct link_struct *) priv_data;
192
0
  struct ext2_dir_entry *next;
193
0
  unsigned int rec_len, min_rec_len, curr_rec_len;
194
0
  int ret = 0;
195
0
  int csum_size = 0;
196
197
0
  if (ls->done)
198
0
    return DIRENT_ABORT;
199
200
0
  rec_len = EXT2_DIR_REC_LEN(ls->namelen);
201
202
0
  ls->err = ext2fs_get_rec_len(ls->fs, dirent, &curr_rec_len);
203
0
  if (ls->err)
204
0
    return DIRENT_ABORT;
205
206
0
  if (ext2fs_has_feature_metadata_csum(ls->fs->super))
207
0
    csum_size = sizeof(struct ext2_dir_entry_tail);
208
  /*
209
   * See if the following directory entry (if any) is unused;
210
   * if so, absorb it into this one.
211
   */
212
0
  next = (struct ext2_dir_entry *) (buf + offset + curr_rec_len);
213
0
  if ((offset + (int) curr_rec_len < blocksize - (8 + csum_size)) &&
214
0
      (next->inode == 0) &&
215
0
      (offset + (int) curr_rec_len + (int) next->rec_len <= blocksize)) {
216
0
    curr_rec_len += next->rec_len;
217
0
    ls->err = ext2fs_set_rec_len(ls->fs, curr_rec_len, dirent);
218
0
    if (ls->err)
219
0
      return DIRENT_ABORT;
220
0
    ret = DIRENT_CHANGED;
221
0
  }
222
223
  /*
224
   * If the directory entry is used, see if we can split the
225
   * directory entry to make room for the new name.  If so,
226
   * truncate it and return.
227
   */
228
0
  if (dirent->inode) {
229
0
    min_rec_len = EXT2_DIR_REC_LEN(ext2fs_dirent_name_len(dirent));
230
0
    if (curr_rec_len < (min_rec_len + rec_len))
231
0
      return ret;
232
0
    rec_len = curr_rec_len - min_rec_len;
233
0
    ls->err = ext2fs_set_rec_len(ls->fs, min_rec_len, dirent);
234
0
    if (ls->err)
235
0
      return DIRENT_ABORT;
236
0
    next = (struct ext2_dir_entry *) (buf + offset +
237
0
              dirent->rec_len);
238
0
    next->inode = 0;
239
0
    ext2fs_dirent_set_name_len(next, 0);
240
0
    ext2fs_dirent_set_file_type(next, 0);
241
0
    ls->err = ext2fs_set_rec_len(ls->fs, rec_len, next);
242
0
    if (ls->err)
243
0
      return DIRENT_ABORT;
244
0
    return DIRENT_CHANGED;
245
0
  }
246
247
  /*
248
   * If we get this far, then the directory entry is not used.
249
   * See if we can fit the request entry in.  If so, do it.
250
   */
251
0
  if (curr_rec_len < rec_len)
252
0
    return ret;
253
0
  dirent->inode = ls->inode;
254
0
  ext2fs_dirent_set_name_len(dirent, ls->namelen);
255
0
  strncpy(dirent->name, ls->name, ls->namelen);
256
0
  if (ext2fs_has_feature_filetype(ls->sb))
257
0
    ext2fs_dirent_set_file_type(dirent, ls->flags & 0x7);
258
259
0
  ls->done++;
260
0
  return DIRENT_ABORT|DIRENT_CHANGED;
261
0
}
262
263
static errcode_t add_dirent_to_buf(ext2_filsys fs, e2_blkcnt_t blockcnt,
264
           char *buf, ext2_ino_t dir,
265
           struct ext2_inode *diri, const char *name,
266
           ext2_ino_t ino, int flags, blk64_t *pblkp)
267
0
{
268
0
  struct dir_context ctx;
269
0
  struct link_struct ls;
270
0
  errcode_t retval;
271
272
0
  retval = load_logical_dir_block(fs, dir, diri, blockcnt, pblkp, buf);
273
0
  if (retval)
274
0
    return retval;
275
0
  ctx.errcode = 0;
276
0
  ctx.func = link_proc;
277
0
  ctx.dir = dir;
278
0
  ctx.flags = DIRENT_FLAG_INCLUDE_EMPTY;
279
0
  ctx.buf = buf;
280
0
  ctx.priv_data = &ls;
281
282
0
  ls.fs = fs;
283
0
  ls.name = name;
284
0
  ls.namelen = strlen(name);
285
0
  ls.inode = ino;
286
0
  ls.flags = flags;
287
0
  ls.done = 0;
288
0
  ls.sb = fs->super;
289
0
  ls.blocksize = fs->blocksize;
290
0
  ls.err = 0;
291
292
0
  ext2fs_process_dir_block(fs, pblkp, blockcnt, 0, 0, &ctx);
293
0
  if (ctx.errcode)
294
0
    return ctx.errcode;
295
0
  if (ls.err)
296
0
    return ls.err;
297
0
  if (!ls.done)
298
0
    return EXT2_ET_DIR_NO_SPACE;
299
0
  return 0;
300
0
}
301
302
struct dx_hash_map {
303
  __u32 hash;
304
  int size;
305
  int off;
306
};
307
308
static EXT2_QSORT_TYPE dx_hash_map_cmp(const void *ap, const void *bp)
309
0
{
310
0
  const struct dx_hash_map *a = ap, *b = bp;
311
312
0
  if (a->hash < b->hash)
313
0
    return -1;
314
0
  if (a->hash > b->hash)
315
0
    return 1;
316
0
  return 0;
317
0
}
318
319
static errcode_t dx_move_dirents(ext2_filsys fs, struct dx_hash_map *map,
320
         int count, void *from, void *to)
321
0
{
322
0
  struct ext2_dir_entry *de;
323
0
  int i;
324
0
  int rec_len = 0;
325
0
  errcode_t retval;
326
0
  int csum_size = 0;
327
0
  void *base = to;
328
329
0
  if (ext2fs_has_feature_metadata_csum(fs->super))
330
0
    csum_size = sizeof(struct ext2_dir_entry_tail);
331
332
0
  for (i = 0; i < count; i++) {
333
0
    de = (struct ext2_dir_entry *) ((char *)from + map[i].off);
334
0
    rec_len = EXT2_DIR_REC_LEN(ext2fs_dirent_name_len(de));
335
0
    memcpy(to, de, rec_len);
336
0
    retval = ext2fs_set_rec_len(fs, rec_len, to);
337
0
    if (retval)
338
0
      return retval;
339
0
    to = (char *)to + rec_len;
340
0
  }
341
  /*
342
   * Update rec_len of the last dir entry to stretch to the end of block
343
   */
344
0
  to = (char *)to - rec_len;
345
0
  rec_len = fs->blocksize - ((char *)to - (char *)base) - csum_size;
346
0
  retval = ext2fs_set_rec_len(fs, rec_len, to);
347
0
  if (retval)
348
0
    return retval;
349
0
  if (csum_size)
350
0
    ext2fs_initialize_dirent_tail(fs,
351
0
        EXT2_DIRENT_TAIL(base, fs->blocksize));
352
0
  return 0;
353
0
}
354
355
static errcode_t dx_insert_entry(ext2_filsys fs, ext2_ino_t dir,
356
         struct dx_lookup_info *info, int level,
357
         __u32 hash, blk64_t lblk)
358
0
{
359
0
  int pcount;
360
0
  struct ext2_dx_entry *top, *new;
361
362
0
  pcount = ext2fs_le16_to_cpu(info->frames[level].head->count);
363
0
  top = info->frames[level].entries + pcount;
364
0
  new = info->frames[level].at + 1;
365
0
  memmove(new + 1, new, (char *)top - (char *)new);
366
0
  new->hash = ext2fs_cpu_to_le32(hash);
367
0
  new->block = ext2fs_cpu_to_le32(lblk);
368
0
  info->frames[level].head->count = ext2fs_cpu_to_le16(pcount + 1);
369
0
  return ext2fs_write_dir_block4(fs, info->frames[level].pblock,
370
0
               info->frames[level].buf, 0, dir);
371
0
}
372
373
static errcode_t dx_split_leaf(ext2_filsys fs, ext2_ino_t dir,
374
             struct ext2_inode *diri,
375
             struct dx_lookup_info *info, void *buf,
376
             blk64_t leaf_pblk, blk64_t new_lblk,
377
             blk64_t new_pblk)
378
0
{
379
0
  int hash_flags = diri->i_flags & EXT4_CASEFOLD_FL;
380
0
  struct ext2_dir_entry *de;
381
0
  void *buf2;
382
0
  errcode_t retval = 0;
383
0
  unsigned int rec_len;
384
0
  unsigned int offset, move_size;
385
0
  int i, count = 0;
386
0
  struct dx_hash_map *map;
387
0
  int continued;
388
0
  __u32 minor_hash;
389
390
0
  retval = ext2fs_get_mem(fs->blocksize, &buf2);
391
0
  if (retval)
392
0
    return retval;
393
0
  retval = ext2fs_get_array(fs->blocksize / 12,
394
0
          sizeof(struct dx_hash_map), &map);
395
0
  if (retval) {
396
0
    ext2fs_free_mem(&buf2);
397
0
    return retval;
398
0
  }
399
0
  for (offset = 0; offset < fs->blocksize; offset += rec_len) {
400
0
    de = (struct ext2_dir_entry *) ((char *)buf + offset);
401
0
    retval = ext2fs_get_rec_len(fs, de, &rec_len);
402
0
    if (retval)
403
0
      goto out;
404
0
    if (ext2fs_dirent_name_len(de) > 0 && de->inode) {
405
0
      map[count].off = offset;
406
0
      map[count].size = rec_len;
407
0
      retval = ext2fs_dirhash2(info->hash_alg, de->name,
408
0
          ext2fs_dirent_name_len(de),
409
0
          fs->encoding, hash_flags,
410
0
          fs->super->s_hash_seed,
411
0
          &(map[count].hash),
412
0
          &minor_hash);
413
0
      if (retval)
414
0
        goto out;
415
0
      count++;
416
0
    }
417
0
  }
418
0
  qsort(map, count, sizeof(struct dx_hash_map), dx_hash_map_cmp);
419
0
  move_size = 0;
420
  /* Find place to split block */
421
0
  for (i = count - 1; i >= 0; i--) {
422
0
    if (move_size + map[i].size / 2 > fs->blocksize / 2)
423
0
      break;
424
0
    move_size += map[i].size;
425
0
  }
426
  /* Let i be the first entry to move */
427
0
  i++;
428
  /* Move selected directory entries to new block */
429
0
  retval = dx_move_dirents(fs, map + i, count - i, buf, buf2);
430
0
  if (retval)
431
0
    goto out;
432
0
  retval = ext2fs_write_dir_block4(fs, new_pblk, buf2, 0, dir);
433
0
  if (retval)
434
0
    goto out;
435
  /* Repack remaining entries in the old block */
436
0
  retval = dx_move_dirents(fs, map, i, buf, buf2);
437
0
  if (retval)
438
0
    goto out;
439
0
  retval = ext2fs_write_dir_block4(fs, leaf_pblk, buf2, 0, dir);
440
0
  if (retval)
441
0
    goto out;
442
  /* Update parent node */
443
0
  continued = map[i].hash == map[i-1].hash;
444
0
  retval = dx_insert_entry(fs, dir, info, info->levels - 1,
445
0
         map[i].hash + continued, new_lblk);
446
0
out:
447
0
  ext2fs_free_mem(&buf2);
448
0
  ext2fs_free_mem(&map);
449
0
  return retval;
450
0
}
451
452
static errcode_t dx_grow_tree(ext2_filsys fs, ext2_ino_t dir,
453
            struct ext2_inode *diri,
454
            struct dx_lookup_info *info, void *buf,
455
            blk64_t leaf_pblk)
456
0
{
457
0
  int i;
458
0
  errcode_t retval;
459
0
  ext2_off64_t size = EXT2_I_SIZE(diri);
460
0
  blk64_t lblk, pblk;
461
0
  struct ext2_dir_entry *de;
462
0
  struct ext2_dx_countlimit *head;
463
0
  int csum_size = 0;
464
0
  int count;
465
466
0
  if (ext2fs_has_feature_metadata_csum(fs->super))
467
0
    csum_size = sizeof(struct ext2_dx_tail);
468
469
  /* Find level which can accommodate new child */
470
0
  for (i = info->levels - 1; i >= 0; i--)
471
0
    if (ext2fs_le16_to_cpu(info->frames[i].head->count) <
472
0
        ext2fs_le16_to_cpu(info->frames[i].head->limit))
473
0
      break;
474
  /* Need to grow tree depth? */
475
0
  if (i < 0 && info->levels >= ext2_dir_htree_level(fs))
476
0
    return EXT2_ET_DIR_NO_SPACE;
477
0
  lblk = size / fs->blocksize;
478
0
  size += fs->blocksize;
479
0
  retval = ext2fs_inode_size_set(fs, diri, size);
480
0
  if (retval)
481
0
    return retval;
482
0
  retval = ext2fs_fallocate(fs,
483
0
      EXT2_FALLOCATE_FORCE_INIT | EXT2_FALLOCATE_ZERO_BLOCKS,
484
0
      dir, diri, 0, lblk, 1);
485
0
  if (retval)
486
0
    return retval;
487
0
  retval = ext2fs_write_inode(fs, dir, diri);
488
0
  if (retval)
489
0
    return retval;
490
0
  retval = ext2fs_bmap2(fs, dir, diri, NULL, 0, lblk, NULL, &pblk);
491
0
  if (retval)
492
0
    return retval;
493
  /* Only leaf addition needed? */
494
0
  if (i == (int)info->levels - 1)
495
0
    return dx_split_leaf(fs, dir, diri, info, buf, leaf_pblk,
496
0
             lblk, pblk);
497
498
0
  de = buf;
499
0
  de->inode = 0;
500
0
  ext2fs_dirent_set_name_len(de, 0);
501
0
  ext2fs_dirent_set_file_type(de, 0);
502
0
  retval = ext2fs_set_rec_len(fs, fs->blocksize, de);
503
0
  if (retval)
504
0
    return retval;
505
0
  head = (struct ext2_dx_countlimit *) ((char *)buf + 8);
506
0
  count = ext2fs_le16_to_cpu(info->frames[i+1].head->count);
507
  /* Growing tree depth? */
508
0
  if (i < 0) {
509
0
    struct ext2_dx_root_info *root;
510
511
0
    memcpy(head, info->frames[0].entries,
512
0
           count * sizeof(struct ext2_dx_entry));
513
0
    head->limit = ext2fs_cpu_to_le16(
514
0
        (fs->blocksize - (8 + csum_size)) /
515
0
        sizeof(struct ext2_dx_entry));
516
    /* head->count gets set by memcpy above to correct value */
517
518
    /* Now update tree root */
519
0
    info->frames[0].head->count = ext2fs_cpu_to_le16(1);
520
0
    info->frames[0].entries[0].block = ext2fs_cpu_to_le32(lblk);
521
0
    root = (struct ext2_dx_root_info *)
522
0
      ((char *)info->frames[0].buf + EXT2_DX_ROOT_OFF);
523
0
    root->indirect_levels++;
524
0
  } else {
525
    /* Splitting internal node in two */
526
0
    int count1 = count / 2;
527
0
    int count2 = count - count1;
528
0
    __u32 split_hash = ext2fs_le32_to_cpu(info->frames[i+1].entries[count1].hash);
529
530
0
    memcpy(head, info->frames[i+1].entries + count1,
531
0
           count2 * sizeof(struct ext2_dx_entry));
532
0
    head->count = ext2fs_cpu_to_le16(count2);
533
0
    head->limit = ext2fs_cpu_to_le16(
534
0
        (fs->blocksize - (8 + csum_size)) /
535
0
        sizeof(struct ext2_dx_entry));
536
0
    info->frames[i+1].head->count = ext2fs_cpu_to_le16(count1);
537
538
    /* Update parent node */
539
0
    retval = dx_insert_entry(fs, dir, info, i, split_hash, lblk);
540
0
    if (retval)
541
0
      return retval;
542
543
0
  }
544
  /* Writeout split block / updated root */
545
0
  retval = ext2fs_write_dir_block4(fs, info->frames[i+1].pblock,
546
0
           info->frames[i+1].buf, 0, dir);
547
0
  if (retval)
548
0
    return retval;
549
  /* Writeout new tree block */
550
0
  retval = ext2fs_write_dir_block4(fs, pblk, buf, 0, dir);
551
0
  if (retval)
552
0
    return retval;
553
0
  return 0;
554
0
}
555
556
static errcode_t dx_link(ext2_filsys fs, ext2_ino_t dir,
557
       struct ext2_inode *diri, const char *name,
558
       ext2_ino_t ino, int flags)
559
0
{
560
0
  struct dx_lookup_info dx_info;
561
0
  errcode_t retval;
562
0
  void *blockbuf;
563
0
  unsigned restart = 0;
564
0
  blk64_t leaf_pblk;
565
566
0
  retval = ext2fs_get_mem(fs->blocksize, &blockbuf);
567
0
  if (retval)
568
0
    return retval;
569
570
0
  dx_info.name = name;
571
0
  dx_info.namelen = strlen(name);
572
0
again:
573
0
  retval = dx_lookup(fs, dir, diri, &dx_info);
574
0
  if (retval)
575
0
    goto free_buf;
576
577
0
  retval = add_dirent_to_buf(fs,
578
0
    ext2fs_le32_to_cpu(dx_info.frames[dx_info.levels-1].at->block) & 0x0fffffff,
579
0
    blockbuf, dir, diri, name, ino, flags, &leaf_pblk);
580
  /*
581
   * Success or error other than ENOSPC...? We are done. We may need upto
582
   * two tries to add entry. One to split htree node and another to add
583
   * new leaf block.
584
   */
585
0
  if (restart >= dx_info.levels || retval != EXT2_ET_DIR_NO_SPACE)
586
0
    goto free_frames;
587
0
  retval = dx_grow_tree(fs, dir, diri, &dx_info, blockbuf, leaf_pblk);
588
0
  if (retval)
589
0
    goto free_frames;
590
  /* Restart everything now that the tree is larger */
591
0
  restart++;
592
0
  dx_release(&dx_info);
593
0
  goto again;
594
0
free_frames:
595
0
  dx_release(&dx_info);
596
0
free_buf:
597
0
  ext2fs_free_mem(&blockbuf);
598
0
  return retval;
599
0
}
600
601
/*
602
 * Note: the low 3 bits of the flags field are used as the directory
603
 * entry filetype.
604
 */
605
#ifdef __TURBOC__
606
 #pragma argsused
607
#endif
608
errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,
609
          ext2_ino_t ino, int flags)
610
0
{
611
0
  errcode_t   retval;
612
0
  struct link_struct  ls;
613
0
  struct ext2_inode inode;
614
615
0
  EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
616
617
0
  if (!(fs->flags & EXT2_FLAG_RW))
618
0
    return EXT2_ET_RO_FILSYS;
619
620
0
  if ((retval = ext2fs_read_inode(fs, dir, &inode)) != 0)
621
0
    return retval;
622
623
0
  if (inode.i_flags & EXT2_INDEX_FL)
624
0
    return dx_link(fs, dir, &inode, name, ino, flags);
625
626
0
  ls.fs = fs;
627
0
  ls.name = name;
628
0
  ls.namelen = name ? strlen(name) : 0;
629
0
  ls.inode = ino;
630
0
  ls.flags = flags;
631
0
  ls.done = 0;
632
0
  ls.sb = fs->super;
633
0
  ls.blocksize = fs->blocksize;
634
0
  ls.err = 0;
635
636
0
  retval = ext2fs_dir_iterate2(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY,
637
0
             NULL, link_proc, &ls);
638
0
  if (retval)
639
0
    return retval;
640
0
  if (ls.err)
641
0
    return ls.err;
642
643
0
  if (!ls.done)
644
0
    return EXT2_ET_DIR_NO_SPACE;
645
0
  return 0;
646
0
}