OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [fs/] [xfs/] [xfs_dir2_sf.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
3
 *
4
 * This program is free software; you can redistribute it and/or modify it
5
 * under the terms of version 2 of the GNU General Public License as
6
 * published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it would be useful, but
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * Further, this software is distributed without any warranty that it is
13
 * free of the rightful claim of any third person regarding infringement
14
 * or the like.  Any license provided herein, whether implied or
15
 * otherwise, applies only to this software file.  Patent licenses, if
16
 * any, provided herein do not apply to combinations of this program with
17
 * other software, or any other product whatsoever.
18
 *
19
 * You should have received a copy of the GNU General Public License along
20
 * with this program; if not, write the Free Software Foundation, Inc., 59
21
 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22
 *
23
 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24
 * Mountain View, CA  94043, or:
25
 *
26
 * http://www.sgi.com
27
 *
28
 * For further information regarding this notice, see:
29
 *
30
 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31
 */
32
 
33
/*
34
 * xfs_dir2_sf.c
35
 * Shortform directory implementation for v2 directories.
36
 */
37
 
38
#include "xfs.h"
39
 
40
#include "xfs_macros.h"
41
#include "xfs_types.h"
42
#include "xfs_inum.h"
43
#include "xfs_log.h"
44
#include "xfs_trans.h"
45
#include "xfs_sb.h"
46
#include "xfs_dir.h"
47
#include "xfs_dir2.h"
48
#include "xfs_dmapi.h"
49
#include "xfs_mount.h"
50
#include "xfs_bmap_btree.h"
51
#include "xfs_attr_sf.h"
52
#include "xfs_dir_sf.h"
53
#include "xfs_dir2_sf.h"
54
#include "xfs_dinode.h"
55
#include "xfs_inode_item.h"
56
#include "xfs_inode.h"
57
#include "xfs_da_btree.h"
58
#include "xfs_dir_leaf.h"
59
#include "xfs_error.h"
60
#include "xfs_dir2_data.h"
61
#include "xfs_dir2_leaf.h"
62
#include "xfs_dir2_block.h"
63
#include "xfs_dir2_trace.h"
64
 
65
/*
66
 * Prototypes for internal functions.
67
 */
68
static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
69
                                     xfs_dir2_sf_entry_t *sfep,
70
                                     xfs_dir2_data_aoff_t offset,
71
                                     int new_isize);
72
static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
73
                                     int new_isize);
74
static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
75
                                    xfs_dir2_sf_entry_t **sfepp,
76
                                    xfs_dir2_data_aoff_t *offsetp);
77
#ifdef DEBUG
78
static void xfs_dir2_sf_check(xfs_da_args_t *args);
79
#else
80
#define xfs_dir2_sf_check(args)
81
#endif /* DEBUG */
82
#if XFS_BIG_INUMS
83
static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
84
static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
85
#endif /* XFS_BIG_INUMS */
86
 
87
/*
88
 * Given a block directory (dp/block), calculate its size as a shortform (sf)
89
 * directory and a header for the sf directory, if it will fit it the
90
 * space currently present in the inode.  If it won't fit, the output
91
 * size is too big (but not accurate).
92
 */
93
int                                             /* size for sf form */
94
xfs_dir2_block_sfsize(
95
        xfs_inode_t             *dp,            /* incore inode pointer */
96
        xfs_dir2_block_t        *block,         /* block directory data */
97
        xfs_dir2_sf_hdr_t       *sfhp)          /* output: header for sf form */
98
{
99
        xfs_dir2_dataptr_t      addr;           /* data entry address */
100
        xfs_dir2_leaf_entry_t   *blp;           /* leaf area of the block */
101
        xfs_dir2_block_tail_t   *btp;           /* tail area of the block */
102
        int                     count;          /* shortform entry count */
103
        xfs_dir2_data_entry_t   *dep;           /* data entry in the block */
104
        int                     i;              /* block entry index */
105
        int                     i8count;        /* count of big-inode entries */
106
        int                     isdot;          /* entry is "." */
107
        int                     isdotdot;       /* entry is ".." */
108
        xfs_mount_t             *mp;            /* mount structure pointer */
109
        int                     namelen;        /* total name bytes */
110
        xfs_ino_t               parent;         /* parent inode number */
111
        int                     size=0;          /* total computed size */
112
 
113
        mp = dp->i_mount;
114
 
115
        count = i8count = namelen = 0;
116
        btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
117
        blp = XFS_DIR2_BLOCK_LEAF_P_ARCH(btp, ARCH_CONVERT);
118
 
119
        /*
120
         * Iterate over the block's data entries by using the leaf pointers.
121
         */
122
        for (i = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
123
                if ((addr = INT_GET(blp[i].address, ARCH_CONVERT)) == XFS_DIR2_NULL_DATAPTR)
124
                        continue;
125
                /*
126
                 * Calculate the pointer to the entry at hand.
127
                 */
128
                dep = (xfs_dir2_data_entry_t *)
129
                      ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, addr));
130
                /*
131
                 * Detect . and .., so we can special-case them.
132
                 * . is not included in sf directories.
133
                 * .. is included by just the parent inode number.
134
                 */
135
                isdot = dep->namelen == 1 && dep->name[0] == '.';
136
                isdotdot =
137
                        dep->namelen == 2 &&
138
                        dep->name[0] == '.' && dep->name[1] == '.';
139
#if XFS_BIG_INUMS
140
                if (!isdot)
141
                        i8count += INT_GET(dep->inumber, ARCH_CONVERT) > XFS_DIR2_MAX_SHORT_INUM;
142
#endif
143
                if (!isdot && !isdotdot) {
144
                        count++;
145
                        namelen += dep->namelen;
146
                } else if (isdotdot)
147
                        parent = INT_GET(dep->inumber, ARCH_CONVERT);
148
                /*
149
                 * Calculate the new size, see if we should give up yet.
150
                 */
151
                size = XFS_DIR2_SF_HDR_SIZE(i8count) +          /* header */
152
                       count +                                  /* namelen */
153
                       count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
154
                       namelen +                                /* name */
155
                       (i8count ?                               /* inumber */
156
                                (uint)sizeof(xfs_dir2_ino8_t) * count :
157
                                (uint)sizeof(xfs_dir2_ino4_t) * count);
158
                if (size > XFS_IFORK_DSIZE(dp))
159
                        return size;            /* size value is a failure */
160
        }
161
        /*
162
         * Create the output header, if it worked.
163
         */
164
        sfhp->count = count;
165
        sfhp->i8count = i8count;
166
        XFS_DIR2_SF_PUT_INUMBER_ARCH((xfs_dir2_sf_t *)sfhp, &parent, &sfhp->parent, ARCH_CONVERT);
167
        return size;
168
}
169
 
170
/*
171
 * Convert a block format directory to shortform.
172
 * Caller has already checked that it will fit, and built us a header.
173
 */
174
int                                             /* error */
175
xfs_dir2_block_to_sf(
176
        xfs_da_args_t           *args,          /* operation arguments */
177
        xfs_dabuf_t             *bp,            /* block buffer */
178
        int                     size,           /* shortform directory size */
179
        xfs_dir2_sf_hdr_t       *sfhp)          /* shortform directory hdr */
180
{
181
        xfs_dir2_block_t        *block;         /* block structure */
182
        xfs_dir2_block_tail_t   *btp;           /* block tail pointer */
183
        xfs_dir2_data_entry_t   *dep;           /* data entry pointer */
184
        xfs_inode_t             *dp;            /* incore directory inode */
185
        xfs_dir2_data_unused_t  *dup;           /* unused data pointer */
186
        char                    *endptr;        /* end of data entries */
187
        int                     error;          /* error return value */
188
        int                     logflags;       /* inode logging flags */
189
        xfs_mount_t             *mp;            /* filesystem mount point */
190
        char                    *ptr;           /* current data pointer */
191
        xfs_dir2_sf_entry_t     *sfep;          /* shortform entry */
192
        xfs_dir2_sf_t           *sfp;           /* shortform structure */
193
        xfs_ino_t               temp;
194
 
195
        xfs_dir2_trace_args_sb("block_to_sf", args, size, bp);
196
        dp = args->dp;
197
        mp = dp->i_mount;
198
 
199
        /*
200
         * Make a copy of the block data, so we can shrink the inode
201
         * and add local data.
202
         */
203
        block = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
204
        memcpy(block, bp->data, mp->m_dirblksize);
205
        logflags = XFS_ILOG_CORE;
206
        if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
207
                ASSERT(error != ENOSPC);
208
                goto out;
209
        }
210
        /*
211
         * The buffer is now unconditionally gone, whether
212
         * xfs_dir2_shrink_inode worked or not.
213
         *
214
         * Convert the inode to local format.
215
         */
216
        dp->i_df.if_flags &= ~XFS_IFEXTENTS;
217
        dp->i_df.if_flags |= XFS_IFINLINE;
218
        dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
219
        ASSERT(dp->i_df.if_bytes == 0);
220
        xfs_idata_realloc(dp, size, XFS_DATA_FORK);
221
        logflags |= XFS_ILOG_DDATA;
222
        /*
223
         * Copy the header into the newly allocate local space.
224
         */
225
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
226
        memcpy(sfp, sfhp, XFS_DIR2_SF_HDR_SIZE(sfhp->i8count));
227
        dp->i_d.di_size = size;
228
        /*
229
         * Set up to loop over the block's entries.
230
         */
231
        btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
232
        ptr = (char *)block->u;
233
        endptr = (char *)XFS_DIR2_BLOCK_LEAF_P_ARCH(btp, ARCH_CONVERT);
234
        sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
235
        /*
236
         * Loop over the active and unused entries.
237
         * Stop when we reach the leaf/tail portion of the block.
238
         */
239
        while (ptr < endptr) {
240
                /*
241
                 * If it's unused, just skip over it.
242
                 */
243
                dup = (xfs_dir2_data_unused_t *)ptr;
244
                if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
245
                        ptr += INT_GET(dup->length, ARCH_CONVERT);
246
                        continue;
247
                }
248
                dep = (xfs_dir2_data_entry_t *)ptr;
249
                /*
250
                 * Skip .
251
                 */
252
                if (dep->namelen == 1 && dep->name[0] == '.')
253
                        ASSERT(INT_GET(dep->inumber, ARCH_CONVERT) == dp->i_ino);
254
                /*
255
                 * Skip .., but make sure the inode number is right.
256
                 */
257
                else if (dep->namelen == 2 &&
258
                         dep->name[0] == '.' && dep->name[1] == '.')
259
                        ASSERT(INT_GET(dep->inumber, ARCH_CONVERT) ==
260
                               XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, &sfp->hdr.parent, ARCH_CONVERT));
261
                /*
262
                 * Normal entry, copy it into shortform.
263
                 */
264
                else {
265
                        sfep->namelen = dep->namelen;
266
                        XFS_DIR2_SF_PUT_OFFSET_ARCH(sfep,
267
                                (xfs_dir2_data_aoff_t)
268
                                ((char *)dep - (char *)block), ARCH_CONVERT);
269
                        memcpy(sfep->name, dep->name, dep->namelen);
270
                        temp=INT_GET(dep->inumber, ARCH_CONVERT);
271
                        XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &temp,
272
                                XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT);
273
                        sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
274
                }
275
                ptr += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
276
        }
277
        ASSERT((char *)sfep - (char *)sfp == size);
278
        xfs_dir2_sf_check(args);
279
out:
280
        xfs_trans_log_inode(args->trans, dp, logflags);
281
        kmem_free(block, mp->m_dirblksize);
282
        return error;
283
}
284
 
285
/*
286
 * Add a name to a shortform directory.
287
 * There are two algorithms, "easy" and "hard" which we decide on
288
 * before changing anything.
289
 * Convert to block form if necessary, if the new entry won't fit.
290
 */
291
int                                             /* error */
292
xfs_dir2_sf_addname(
293
        xfs_da_args_t           *args)          /* operation arguments */
294
{
295
        int                     add_entsize;    /* size of the new entry */
296
        xfs_inode_t             *dp;            /* incore directory inode */
297
        int                     error;          /* error return value */
298
        int                     incr_isize;     /* total change in size */
299
        int                     new_isize;      /* di_size after adding name */
300
        int                     objchange;      /* changing to 8-byte inodes */
301
        xfs_dir2_data_aoff_t    offset;         /* offset for new entry */
302
        int                     old_isize;      /* di_size before adding name */
303
        int                     pick;           /* which algorithm to use */
304
        xfs_dir2_sf_t           *sfp;           /* shortform structure */
305
        xfs_dir2_sf_entry_t     *sfep;          /* shortform entry */
306
 
307
        xfs_dir2_trace_args("sf_addname", args);
308
        ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
309
        dp = args->dp;
310
        ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
311
        /*
312
         * Make sure the shortform value has some of its header.
313
         */
314
        if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
315
                ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
316
                return XFS_ERROR(EIO);
317
        }
318
        ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
319
        ASSERT(dp->i_df.if_u1.if_data != NULL);
320
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
321
        ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
322
        /*
323
         * Compute entry (and change in) size.
324
         */
325
        add_entsize = XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen);
326
        incr_isize = add_entsize;
327
        objchange = 0;
328
#if XFS_BIG_INUMS
329
        /*
330
         * Do we have to change to 8 byte inodes?
331
         */
332
        if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
333
                /*
334
                 * Yes, adjust the entry size and the total size.
335
                 */
336
                add_entsize +=
337
                        (uint)sizeof(xfs_dir2_ino8_t) -
338
                        (uint)sizeof(xfs_dir2_ino4_t);
339
                incr_isize +=
340
                        (sfp->hdr.count + 2) *
341
                        ((uint)sizeof(xfs_dir2_ino8_t) -
342
                         (uint)sizeof(xfs_dir2_ino4_t));
343
                objchange = 1;
344
        }
345
#endif
346
        old_isize = (int)dp->i_d.di_size;
347
        new_isize = old_isize + incr_isize;
348
        /*
349
         * Won't fit as shortform any more (due to size),
350
         * or the pick routine says it won't (due to offset values).
351
         */
352
        if (new_isize > XFS_IFORK_DSIZE(dp) ||
353
            (pick =
354
             xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
355
                /*
356
                 * Just checking or no space reservation, it doesn't fit.
357
                 */
358
                if (args->justcheck || args->total == 0)
359
                        return XFS_ERROR(ENOSPC);
360
                /*
361
                 * Convert to block form then add the name.
362
                 */
363
                error = xfs_dir2_sf_to_block(args);
364
                if (error)
365
                        return error;
366
                return xfs_dir2_block_addname(args);
367
        }
368
        /*
369
         * Just checking, it fits.
370
         */
371
        if (args->justcheck)
372
                return 0;
373
        /*
374
         * Do it the easy way - just add it at the end.
375
         */
376
        if (pick == 1)
377
                xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
378
        /*
379
         * Do it the hard way - look for a place to insert the new entry.
380
         * Convert to 8 byte inode numbers first if necessary.
381
         */
382
        else {
383
                ASSERT(pick == 2);
384
#if XFS_BIG_INUMS
385
                if (objchange)
386
                        xfs_dir2_sf_toino8(args);
387
#endif
388
                xfs_dir2_sf_addname_hard(args, objchange, new_isize);
389
        }
390
        xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
391
        return 0;
392
}
393
 
394
/*
395
 * Add the new entry the "easy" way.
396
 * This is copying the old directory and adding the new entry at the end.
397
 * Since it's sorted by "offset" we need room after the last offset
398
 * that's already there, and then room to convert to a block directory.
399
 * This is already checked by the pick routine.
400
 */
401
static void
402
xfs_dir2_sf_addname_easy(
403
        xfs_da_args_t           *args,          /* operation arguments */
404
        xfs_dir2_sf_entry_t     *sfep,          /* pointer to new entry */
405
        xfs_dir2_data_aoff_t    offset,         /* offset to use for new ent */
406
        int                     new_isize)      /* new directory size */
407
{
408
        int                     byteoff;        /* byte offset in sf dir */
409
        xfs_inode_t             *dp;            /* incore directory inode */
410
        xfs_dir2_sf_t           *sfp;           /* shortform structure */
411
 
412
        dp = args->dp;
413
 
414
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
415
        byteoff = (int)((char *)sfep - (char *)sfp);
416
        /*
417
         * Grow the in-inode space.
418
         */
419
        xfs_idata_realloc(dp, XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen),
420
                XFS_DATA_FORK);
421
        /*
422
         * Need to set up again due to realloc of the inode data.
423
         */
424
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
425
        sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
426
        /*
427
         * Fill in the new entry.
428
         */
429
        sfep->namelen = args->namelen;
430
        XFS_DIR2_SF_PUT_OFFSET_ARCH(sfep, offset, ARCH_CONVERT);
431
        memcpy(sfep->name, args->name, sfep->namelen);
432
        XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &args->inumber,
433
                XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT);
434
        /*
435
         * Update the header and inode.
436
         */
437
        sfp->hdr.count++;
438
#if XFS_BIG_INUMS
439
        if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
440
                sfp->hdr.i8count++;
441
#endif
442
        dp->i_d.di_size = new_isize;
443
        xfs_dir2_sf_check(args);
444
}
445
 
446
/*
447
 * Add the new entry the "hard" way.
448
 * The caller has already converted to 8 byte inode numbers if necessary,
449
 * in which case we need to leave the i8count at 1.
450
 * Find a hole that the new entry will fit into, and copy
451
 * the first part of the entries, the new entry, and the last part of
452
 * the entries.
453
 */
454
/* ARGSUSED */
455
static void
456
xfs_dir2_sf_addname_hard(
457
        xfs_da_args_t           *args,          /* operation arguments */
458
        int                     objchange,      /* changing inode number size */
459
        int                     new_isize)      /* new directory size */
460
{
461
        int                     add_datasize;   /* data size need for new ent */
462
        char                    *buf;           /* buffer for old */
463
        xfs_inode_t             *dp;            /* incore directory inode */
464
        int                     eof;            /* reached end of old dir */
465
        int                     nbytes;         /* temp for byte copies */
466
        xfs_dir2_data_aoff_t    new_offset;     /* next offset value */
467
        xfs_dir2_data_aoff_t    offset;         /* current offset value */
468
        int                     old_isize;      /* previous di_size */
469
        xfs_dir2_sf_entry_t     *oldsfep;       /* entry in original dir */
470
        xfs_dir2_sf_t           *oldsfp;        /* original shortform dir */
471
        xfs_dir2_sf_entry_t     *sfep;          /* entry in new dir */
472
        xfs_dir2_sf_t           *sfp;           /* new shortform dir */
473
 
474
        /*
475
         * Copy the old directory to the stack buffer.
476
         */
477
        dp = args->dp;
478
 
479
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
480
        old_isize = (int)dp->i_d.di_size;
481
        buf = kmem_alloc(old_isize, KM_SLEEP);
482
        oldsfp = (xfs_dir2_sf_t *)buf;
483
        memcpy(oldsfp, sfp, old_isize);
484
        /*
485
         * Loop over the old directory finding the place we're going
486
         * to insert the new entry.
487
         * If it's going to end up at the end then oldsfep will point there.
488
         */
489
        for (offset = XFS_DIR2_DATA_FIRST_OFFSET,
490
              oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp),
491
              add_datasize = XFS_DIR2_DATA_ENTSIZE(args->namelen),
492
              eof = (char *)oldsfep == &buf[old_isize];
493
             !eof;
494
             offset = new_offset + XFS_DIR2_DATA_ENTSIZE(oldsfep->namelen),
495
              oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep),
496
              eof = (char *)oldsfep == &buf[old_isize]) {
497
                new_offset = XFS_DIR2_SF_GET_OFFSET_ARCH(oldsfep, ARCH_CONVERT);
498
                if (offset + add_datasize <= new_offset)
499
                        break;
500
        }
501
        /*
502
         * Get rid of the old directory, then allocate space for
503
         * the new one.  We do this so xfs_idata_realloc won't copy
504
         * the data.
505
         */
506
        xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
507
        xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
508
        /*
509
         * Reset the pointer since the buffer was reallocated.
510
         */
511
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
512
        /*
513
         * Copy the first part of the directory, including the header.
514
         */
515
        nbytes = (int)((char *)oldsfep - (char *)oldsfp);
516
        memcpy(sfp, oldsfp, nbytes);
517
        sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
518
        /*
519
         * Fill in the new entry, and update the header counts.
520
         */
521
        sfep->namelen = args->namelen;
522
        XFS_DIR2_SF_PUT_OFFSET_ARCH(sfep, offset, ARCH_CONVERT);
523
        memcpy(sfep->name, args->name, sfep->namelen);
524
        XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &args->inumber,
525
                XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT);
526
        sfp->hdr.count++;
527
#if XFS_BIG_INUMS
528
        if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
529
                sfp->hdr.i8count++;
530
#endif
531
        /*
532
         * If there's more left to copy, do that.
533
         */
534
        if (!eof) {
535
                sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
536
                memcpy(sfep, oldsfep, old_isize - nbytes);
537
        }
538
        kmem_free(buf, old_isize);
539
        dp->i_d.di_size = new_isize;
540
        xfs_dir2_sf_check(args);
541
}
542
 
543
/*
544
 * Decide if the new entry will fit at all.
545
 * If it will fit, pick between adding the new entry to the end (easy)
546
 * or somewhere else (hard).
547
 * Return 0 (won't fit), 1 (easy), 2 (hard).
548
 */
549
/*ARGSUSED*/
550
static int                                      /* pick result */
551
xfs_dir2_sf_addname_pick(
552
        xfs_da_args_t           *args,          /* operation arguments */
553
        int                     objchange,      /* inode # size changes */
554
        xfs_dir2_sf_entry_t     **sfepp,        /* out(1): new entry ptr */
555
        xfs_dir2_data_aoff_t    *offsetp)       /* out(1): new offset */
556
{
557
        xfs_inode_t             *dp;            /* incore directory inode */
558
        int                     holefit;        /* found hole it will fit in */
559
        int                     i;              /* entry number */
560
        xfs_mount_t             *mp;            /* filesystem mount point */
561
        xfs_dir2_data_aoff_t    offset;         /* data block offset */
562
        xfs_dir2_sf_entry_t     *sfep;          /* shortform entry */
563
        xfs_dir2_sf_t           *sfp;           /* shortform structure */
564
        int                     size;           /* entry's data size */
565
        int                     used;           /* data bytes used */
566
 
567
        dp = args->dp;
568
        mp = dp->i_mount;
569
 
570
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
571
        size = XFS_DIR2_DATA_ENTSIZE(args->namelen);
572
        offset = XFS_DIR2_DATA_FIRST_OFFSET;
573
        sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
574
        holefit = 0;
575
        /*
576
         * Loop over sf entries.
577
         * Keep track of data offset and whether we've seen a place
578
         * to insert the new entry.
579
         */
580
        for (i = 0; i < sfp->hdr.count; i++) {
581
                if (!holefit)
582
                        holefit = offset + size <= XFS_DIR2_SF_GET_OFFSET_ARCH(sfep, ARCH_CONVERT);
583
                offset = XFS_DIR2_SF_GET_OFFSET_ARCH(sfep, ARCH_CONVERT) +
584
                         XFS_DIR2_DATA_ENTSIZE(sfep->namelen);
585
                sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
586
        }
587
        /*
588
         * Calculate data bytes used excluding the new entry, if this
589
         * was a data block (block form directory).
590
         */
591
        used = offset +
592
               (sfp->hdr.count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
593
               (uint)sizeof(xfs_dir2_block_tail_t);
594
        /*
595
         * If it won't fit in a block form then we can't insert it,
596
         * we'll go back, convert to block, then try the insert and convert
597
         * to leaf.
598
         */
599
        if (used + (holefit ? 0 : size) > mp->m_dirblksize)
600
                return 0;
601
        /*
602
         * If changing the inode number size, do it the hard way.
603
         */
604
#if XFS_BIG_INUMS
605
        if (objchange) {
606
                return 2;
607
        }
608
#else
609
        ASSERT(objchange == 0);
610
#endif
611
        /*
612
         * If it won't fit at the end then do it the hard way (use the hole).
613
         */
614
        if (used + size > mp->m_dirblksize)
615
                return 2;
616
        /*
617
         * Do it the easy way.
618
         */
619
        *sfepp = sfep;
620
        *offsetp = offset;
621
        return 1;
622
}
623
 
624
#ifdef DEBUG
625
/*
626
 * Check consistency of shortform directory, assert if bad.
627
 */
628
static void
629
xfs_dir2_sf_check(
630
        xfs_da_args_t           *args)          /* operation arguments */
631
{
632
        xfs_inode_t             *dp;            /* incore directory inode */
633
        int                     i;              /* entry number */
634
        int                     i8count;        /* number of big inode#s */
635
        xfs_ino_t               ino;            /* entry inode number */
636
        int                     offset;         /* data offset */
637
        xfs_dir2_sf_entry_t     *sfep;          /* shortform dir entry */
638
        xfs_dir2_sf_t           *sfp;           /* shortform structure */
639
 
640
        dp = args->dp;
641
 
642
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
643
        offset = XFS_DIR2_DATA_FIRST_OFFSET;
644
        ino = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, &sfp->hdr.parent, ARCH_CONVERT);
645
        i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
646
 
647
        for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
648
             i < sfp->hdr.count;
649
             i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
650
                ASSERT(XFS_DIR2_SF_GET_OFFSET_ARCH(sfep, ARCH_CONVERT) >= offset);
651
                ino = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT);
652
                i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
653
                offset =
654
                        XFS_DIR2_SF_GET_OFFSET_ARCH(sfep, ARCH_CONVERT) +
655
                        XFS_DIR2_DATA_ENTSIZE(sfep->namelen);
656
        }
657
        ASSERT(i8count == sfp->hdr.i8count);
658
        ASSERT(XFS_BIG_INUMS || i8count == 0);
659
        ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
660
        ASSERT(offset +
661
               (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
662
               (uint)sizeof(xfs_dir2_block_tail_t) <=
663
               dp->i_mount->m_dirblksize);
664
}
665
#endif  /* DEBUG */
666
 
667
/*
668
 * Create a new (shortform) directory.
669
 */
670
int                                     /* error, always 0 */
671
xfs_dir2_sf_create(
672
        xfs_da_args_t   *args,          /* operation arguments */
673
        xfs_ino_t       pino)           /* parent inode number */
674
{
675
        xfs_inode_t     *dp;            /* incore directory inode */
676
        int             i8count;        /* parent inode is an 8-byte number */
677
        xfs_dir2_sf_t   *sfp;           /* shortform structure */
678
        int             size;           /* directory size */
679
 
680
        xfs_dir2_trace_args_i("sf_create", args, pino);
681
        dp = args->dp;
682
 
683
        ASSERT(dp != NULL);
684
        ASSERT(dp->i_d.di_size == 0);
685
        /*
686
         * If it's currently a zero-length extent file,
687
         * convert it to local format.
688
         */
689
        if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
690
                dp->i_df.if_flags &= ~XFS_IFEXTENTS;    /* just in case */
691
                dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
692
                xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
693
                dp->i_df.if_flags |= XFS_IFINLINE;
694
        }
695
        ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
696
        ASSERT(dp->i_df.if_bytes == 0);
697
        i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
698
        size = XFS_DIR2_SF_HDR_SIZE(i8count);
699
        /*
700
         * Make a buffer for the data.
701
         */
702
        xfs_idata_realloc(dp, size, XFS_DATA_FORK);
703
        /*
704
         * Fill in the header,
705
         */
706
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
707
        sfp->hdr.i8count = i8count;
708
        /*
709
         * Now can put in the inode number, since i8count is set.
710
         */
711
        XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &pino, &sfp->hdr.parent, ARCH_CONVERT);
712
        sfp->hdr.count = 0;
713
        dp->i_d.di_size = size;
714
        xfs_dir2_sf_check(args);
715
        xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
716
        return 0;
717
}
718
 
719
int                                             /* error */
720
xfs_dir2_sf_getdents(
721
        xfs_inode_t             *dp,            /* incore directory inode */
722
        uio_t                   *uio,           /* caller's buffer control */
723
        int                     *eofp,          /* eof reached? (out) */
724
        xfs_dirent_t            *dbp,           /* caller's buffer */
725
        xfs_dir2_put_t          put)            /* abi's formatting function */
726
{
727
        int                     error;          /* error return value */
728
        int                     i;              /* shortform entry number */
729
        xfs_mount_t             *mp;            /* filesystem mount point */
730
        xfs_dir2_dataptr_t      off;            /* current entry's offset */
731
        xfs_dir2_put_args_t     p;              /* arg package for put rtn */
732
        xfs_dir2_sf_entry_t     *sfep;          /* shortform directory entry */
733
        xfs_dir2_sf_t           *sfp;           /* shortform structure */
734
        xfs_off_t                       dir_offset;
735
 
736
        mp = dp->i_mount;
737
 
738
        ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
739
        /*
740
         * Give up if the directory is way too short.
741
         */
742
        if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
743
                ASSERT(XFS_FORCED_SHUTDOWN(mp));
744
                return XFS_ERROR(EIO);
745
        }
746
 
747
        dir_offset = uio->uio_offset;
748
 
749
        ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
750
        ASSERT(dp->i_df.if_u1.if_data != NULL);
751
 
752
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
753
 
754
        ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
755
 
756
        /*
757
         * If the block number in the offset is out of range, we're done.
758
         */
759
        if (XFS_DIR2_DATAPTR_TO_DB(mp, dir_offset) > mp->m_dirdatablk) {
760
                *eofp = 1;
761
                return 0;
762
        }
763
 
764
        /*
765
         * Set up putargs structure.
766
         */
767
        p.dbp = dbp;
768
        p.put = put;
769
        p.uio = uio;
770
        /*
771
         * Put . entry unless we're starting past it.
772
         */
773
        if (dir_offset <=
774
                    XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
775
                                               XFS_DIR2_DATA_DOT_OFFSET)) {
776
                p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, 0,
777
                                                XFS_DIR2_DATA_DOTDOT_OFFSET);
778
                p.ino = dp->i_ino;
779
#if XFS_BIG_INUMS
780
                p.ino += mp->m_inoadd;
781
#endif
782
                p.name = ".";
783
                p.namelen = 1;
784
 
785
                error = p.put(&p);
786
 
787
                if (!p.done) {
788
                        uio->uio_offset =
789
                                XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
790
                                                XFS_DIR2_DATA_DOT_OFFSET);
791
                        return error;
792
                }
793
        }
794
 
795
        /*
796
         * Put .. entry unless we're starting past it.
797
         */
798
        if (dir_offset <=
799
                    XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
800
                                               XFS_DIR2_DATA_DOTDOT_OFFSET)) {
801
                p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
802
                                                XFS_DIR2_DATA_FIRST_OFFSET);
803
                p.ino = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, &sfp->hdr.parent,
804
                                                ARCH_CONVERT);
805
#if XFS_BIG_INUMS
806
                p.ino += mp->m_inoadd;
807
#endif
808
                p.name = "..";
809
                p.namelen = 2;
810
 
811
                error = p.put(&p);
812
 
813
                if (!p.done) {
814
                        uio->uio_offset =
815
                                XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
816
                                        XFS_DIR2_DATA_DOTDOT_OFFSET);
817
                        return error;
818
                }
819
        }
820
 
821
        /*
822
         * Loop while there are more entries and put'ing works.
823
         */
824
        for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
825
                     i < sfp->hdr.count;
826
                             i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
827
 
828
                off = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
829
                                XFS_DIR2_SF_GET_OFFSET_ARCH(sfep, ARCH_CONVERT));
830
 
831
                if (dir_offset > off)
832
                        continue;
833
 
834
                p.namelen = sfep->namelen;
835
 
836
                p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
837
                        XFS_DIR2_SF_GET_OFFSET_ARCH(sfep, ARCH_CONVERT) +
838
                        XFS_DIR2_DATA_ENTSIZE(p.namelen));
839
 
840
                p.ino = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp,
841
                                XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT);
842
#if XFS_BIG_INUMS
843
                p.ino += mp->m_inoadd;
844
#endif
845
                p.name = (char *)sfep->name;
846
 
847
                error = p.put(&p);
848
 
849
                if (!p.done) {
850
                        uio->uio_offset = off;
851
                        return error;
852
                }
853
        }
854
 
855
        /*
856
         * They all fit.
857
         */
858
        *eofp = 1;
859
 
860
        uio->uio_offset =
861
                XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk + 1, 0);
862
 
863
        return 0;
864
}
865
 
866
/*
867
 * Lookup an entry in a shortform directory.
868
 * Returns EEXIST if found, ENOENT if not found.
869
 */
870
int                                             /* error */
871
xfs_dir2_sf_lookup(
872
        xfs_da_args_t           *args)          /* operation arguments */
873
{
874
        xfs_inode_t             *dp;            /* incore directory inode */
875
        int                     i;              /* entry index */
876
        xfs_dir2_sf_entry_t     *sfep;          /* shortform directory entry */
877
        xfs_dir2_sf_t           *sfp;           /* shortform structure */
878
 
879
        xfs_dir2_trace_args("sf_lookup", args);
880
        xfs_dir2_sf_check(args);
881
        dp = args->dp;
882
 
883
        ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
884
        /*
885
         * Bail out if the directory is way too short.
886
         */
887
        if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
888
                ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
889
                return XFS_ERROR(EIO);
890
        }
891
        ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
892
        ASSERT(dp->i_df.if_u1.if_data != NULL);
893
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
894
        ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
895
        /*
896
         * Special case for .
897
         */
898
        if (args->namelen == 1 && args->name[0] == '.') {
899
                args->inumber = dp->i_ino;
900
                return XFS_ERROR(EEXIST);
901
        }
902
        /*
903
         * Special case for ..
904
         */
905
        if (args->namelen == 2 &&
906
            args->name[0] == '.' && args->name[1] == '.') {
907
                args->inumber = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, &sfp->hdr.parent, ARCH_CONVERT);
908
                return XFS_ERROR(EEXIST);
909
        }
910
        /*
911
         * Loop over all the entries trying to match ours.
912
         */
913
        for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
914
             i < sfp->hdr.count;
915
             i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
916
                if (sfep->namelen == args->namelen &&
917
                    sfep->name[0] == args->name[0] &&
918
                    memcmp(args->name, sfep->name, args->namelen) == 0) {
919
                        args->inumber =
920
                                XFS_DIR2_SF_GET_INUMBER_ARCH(sfp,
921
                                        XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT);
922
                        return XFS_ERROR(EEXIST);
923
                }
924
        }
925
        /*
926
         * Didn't find it.
927
         */
928
        ASSERT(args->oknoent);
929
        return XFS_ERROR(ENOENT);
930
}
931
 
932
/*
933
 * Remove an entry from a shortform directory.
934
 */
935
int                                             /* error */
936
xfs_dir2_sf_removename(
937
        xfs_da_args_t           *args)
938
{
939
        int                     byteoff;        /* offset of removed entry */
940
        xfs_inode_t             *dp;            /* incore directory inode */
941
        int                     entsize;        /* this entry's size */
942
        int                     i;              /* shortform entry index */
943
        int                     newsize;        /* new inode size */
944
        int                     oldsize;        /* old inode size */
945
        xfs_dir2_sf_entry_t     *sfep;          /* shortform directory entry */
946
        xfs_dir2_sf_t           *sfp;           /* shortform structure */
947
 
948
        xfs_dir2_trace_args("sf_removename", args);
949
        dp = args->dp;
950
 
951
        ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
952
        oldsize = (int)dp->i_d.di_size;
953
        /*
954
         * Bail out if the directory is way too short.
955
         */
956
        if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
957
                ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
958
                return XFS_ERROR(EIO);
959
        }
960
        ASSERT(dp->i_df.if_bytes == oldsize);
961
        ASSERT(dp->i_df.if_u1.if_data != NULL);
962
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
963
        ASSERT(oldsize >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
964
        /*
965
         * Loop over the old directory entries.
966
         * Find the one we're deleting.
967
         */
968
        for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
969
             i < sfp->hdr.count;
970
             i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
971
                if (sfep->namelen == args->namelen &&
972
                    sfep->name[0] == args->name[0] &&
973
                    memcmp(sfep->name, args->name, args->namelen) == 0) {
974
                        ASSERT(XFS_DIR2_SF_GET_INUMBER_ARCH(sfp,
975
                                        XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT) ==
976
                                args->inumber);
977
                        break;
978
                }
979
        }
980
        /*
981
         * Didn't find it.
982
         */
983
        if (i == sfp->hdr.count) {
984
                return XFS_ERROR(ENOENT);
985
        }
986
        /*
987
         * Calculate sizes.
988
         */
989
        byteoff = (int)((char *)sfep - (char *)sfp);
990
        entsize = XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen);
991
        newsize = oldsize - entsize;
992
        /*
993
         * Copy the part if any after the removed entry, sliding it down.
994
         */
995
        if (byteoff + entsize < oldsize)
996
                memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
997
                        oldsize - (byteoff + entsize));
998
        /*
999
         * Fix up the header and file size.
1000
         */
1001
        sfp->hdr.count--;
1002
        dp->i_d.di_size = newsize;
1003
        /*
1004
         * Reallocate, making it smaller.
1005
         */
1006
        xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
1007
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1008
#if XFS_BIG_INUMS
1009
        /*
1010
         * Are we changing inode number size?
1011
         */
1012
        if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1013
                if (sfp->hdr.i8count == 1)
1014
                        xfs_dir2_sf_toino4(args);
1015
                else
1016
                        sfp->hdr.i8count--;
1017
        }
1018
#endif
1019
        xfs_dir2_sf_check(args);
1020
        xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1021
        return 0;
1022
}
1023
 
1024
/*
1025
 * Replace the inode number of an entry in a shortform directory.
1026
 */
1027
int                                             /* error */
1028
xfs_dir2_sf_replace(
1029
        xfs_da_args_t           *args)          /* operation arguments */
1030
{
1031
        xfs_inode_t             *dp;            /* incore directory inode */
1032
        int                     i;              /* entry index */
1033
#if XFS_BIG_INUMS || defined(DEBUG)
1034
        xfs_ino_t               ino=0;           /* entry old inode number */
1035
#endif
1036
#if XFS_BIG_INUMS
1037
        int                     i8elevated;     /* sf_toino8 set i8count=1 */
1038
#endif
1039
        xfs_dir2_sf_entry_t     *sfep;          /* shortform directory entry */
1040
        xfs_dir2_sf_t           *sfp;           /* shortform structure */
1041
 
1042
        xfs_dir2_trace_args("sf_replace", args);
1043
        dp = args->dp;
1044
 
1045
        ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1046
        /*
1047
         * Bail out if the shortform directory is way too small.
1048
         */
1049
        if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1050
                ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
1051
                return XFS_ERROR(EIO);
1052
        }
1053
        ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1054
        ASSERT(dp->i_df.if_u1.if_data != NULL);
1055
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1056
        ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
1057
#if XFS_BIG_INUMS
1058
        /*
1059
         * New inode number is large, and need to convert to 8-byte inodes.
1060
         */
1061
        if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
1062
                int     error;                  /* error return value */
1063
                int     newsize;                /* new inode size */
1064
 
1065
                newsize =
1066
                        dp->i_df.if_bytes +
1067
                        (sfp->hdr.count + 1) *
1068
                        ((uint)sizeof(xfs_dir2_ino8_t) -
1069
                         (uint)sizeof(xfs_dir2_ino4_t));
1070
                /*
1071
                 * Won't fit as shortform, convert to block then do replace.
1072
                 */
1073
                if (newsize > XFS_IFORK_DSIZE(dp)) {
1074
                        error = xfs_dir2_sf_to_block(args);
1075
                        if (error) {
1076
                                return error;
1077
                        }
1078
                        return xfs_dir2_block_replace(args);
1079
                }
1080
                /*
1081
                 * Still fits, convert to 8-byte now.
1082
                 */
1083
                xfs_dir2_sf_toino8(args);
1084
                i8elevated = 1;
1085
                sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1086
        } else
1087
                i8elevated = 0;
1088
#endif
1089
        ASSERT(args->namelen != 1 || args->name[0] != '.');
1090
        /*
1091
         * Replace ..'s entry.
1092
         */
1093
        if (args->namelen == 2 &&
1094
            args->name[0] == '.' && args->name[1] == '.') {
1095
#if XFS_BIG_INUMS || defined(DEBUG)
1096
                ino = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, &sfp->hdr.parent, ARCH_CONVERT);
1097
                ASSERT(args->inumber != ino);
1098
#endif
1099
                XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &args->inumber, &sfp->hdr.parent, ARCH_CONVERT);
1100
        }
1101
        /*
1102
         * Normal entry, look for the name.
1103
         */
1104
        else {
1105
                for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
1106
                     i < sfp->hdr.count;
1107
                     i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
1108
                        if (sfep->namelen == args->namelen &&
1109
                            sfep->name[0] == args->name[0] &&
1110
                            memcmp(args->name, sfep->name, args->namelen) == 0) {
1111
#if XFS_BIG_INUMS || defined(DEBUG)
1112
                                ino = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp,
1113
                                        XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT);
1114
                                ASSERT(args->inumber != ino);
1115
#endif
1116
                                XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &args->inumber,
1117
                                        XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT);
1118
                                break;
1119
                        }
1120
                }
1121
                /*
1122
                 * Didn't find it.
1123
                 */
1124
                if (i == sfp->hdr.count) {
1125
                        ASSERT(args->oknoent);
1126
#if XFS_BIG_INUMS
1127
                        if (i8elevated)
1128
                                xfs_dir2_sf_toino4(args);
1129
#endif
1130
                        return XFS_ERROR(ENOENT);
1131
                }
1132
        }
1133
#if XFS_BIG_INUMS
1134
        /*
1135
         * See if the old number was large, the new number is small.
1136
         */
1137
        if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1138
            args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1139
                /*
1140
                 * And the old count was one, so need to convert to small.
1141
                 */
1142
                if (sfp->hdr.i8count == 1)
1143
                        xfs_dir2_sf_toino4(args);
1144
                else
1145
                        sfp->hdr.i8count--;
1146
        }
1147
        /*
1148
         * See if the old number was small, the new number is large.
1149
         */
1150
        if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1151
            args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1152
                /*
1153
                 * add to the i8count unless we just converted to 8-byte
1154
                 * inodes (which does an implied i8count = 1)
1155
                 */
1156
                ASSERT(sfp->hdr.i8count != 0);
1157
                if (!i8elevated)
1158
                        sfp->hdr.i8count++;
1159
        }
1160
#endif
1161
        xfs_dir2_sf_check(args);
1162
        xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1163
        return 0;
1164
}
1165
 
1166
#if XFS_BIG_INUMS
1167
/*
1168
 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1169
 * The last 8-byte inode number is gone, but the count is still 1.
1170
 */
1171
static void
1172
xfs_dir2_sf_toino4(
1173
        xfs_da_args_t           *args)          /* operation arguments */
1174
{
1175
        char                    *buf;           /* old dir's buffer */
1176
        xfs_inode_t             *dp;            /* incore directory inode */
1177
        int                     i;              /* entry index */
1178
        xfs_ino_t               ino;            /* entry inode number */
1179
        int                     newsize;        /* new inode size */
1180
        xfs_dir2_sf_entry_t     *oldsfep;       /* old sf entry */
1181
        xfs_dir2_sf_t           *oldsfp;        /* old sf directory */
1182
        int                     oldsize;        /* old inode size */
1183
        xfs_dir2_sf_entry_t     *sfep;          /* new sf entry */
1184
        xfs_dir2_sf_t           *sfp;           /* new sf directory */
1185
 
1186
        xfs_dir2_trace_args("sf_toino4", args);
1187
        dp = args->dp;
1188
 
1189
        /*
1190
         * Copy the old directory to the buffer.
1191
         * Then nuke it from the inode, and add the new buffer to the inode.
1192
         * Don't want xfs_idata_realloc copying the data here.
1193
         */
1194
        oldsize = dp->i_df.if_bytes;
1195
        buf = kmem_alloc(oldsize, KM_SLEEP);
1196
        oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1197
        ASSERT(oldsfp->hdr.i8count == 1);
1198
        memcpy(buf, oldsfp, oldsize);
1199
        /*
1200
         * Compute the new inode size.
1201
         */
1202
        newsize =
1203
                oldsize -
1204
                (oldsfp->hdr.count + 1) *
1205
                ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1206
        xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1207
        xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1208
        /*
1209
         * Reset our pointers, the data has moved.
1210
         */
1211
        oldsfp = (xfs_dir2_sf_t *)buf;
1212
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1213
        /*
1214
         * Fill in the new header.
1215
         */
1216
        sfp->hdr.count = oldsfp->hdr.count;
1217
        sfp->hdr.i8count = 0;
1218
        ino = XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp, &oldsfp->hdr.parent, ARCH_CONVERT);
1219
        XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &ino, &sfp->hdr.parent, ARCH_CONVERT);
1220
        /*
1221
         * Copy the entries field by field.
1222
         */
1223
        for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp),
1224
                    oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp);
1225
             i < sfp->hdr.count;
1226
             i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep),
1227
                  oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep)) {
1228
                sfep->namelen = oldsfep->namelen;
1229
                sfep->offset = oldsfep->offset;
1230
                memcpy(sfep->name, oldsfep->name, sfep->namelen);
1231
                ino = XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp,
1232
                        XFS_DIR2_SF_INUMBERP(oldsfep), ARCH_CONVERT);
1233
                XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &ino, XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT);
1234
        }
1235
        /*
1236
         * Clean up the inode.
1237
         */
1238
        kmem_free(buf, oldsize);
1239
        dp->i_d.di_size = newsize;
1240
        xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1241
}
1242
 
1243
/*
1244
 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1245
 * The new 8-byte inode number is not there yet, we leave with the
1246
 * count 1 but no corresponding entry.
1247
 */
1248
static void
1249
xfs_dir2_sf_toino8(
1250
        xfs_da_args_t           *args)          /* operation arguments */
1251
{
1252
        char                    *buf;           /* old dir's buffer */
1253
        xfs_inode_t             *dp;            /* incore directory inode */
1254
        int                     i;              /* entry index */
1255
        xfs_ino_t               ino;            /* entry inode number */
1256
        int                     newsize;        /* new inode size */
1257
        xfs_dir2_sf_entry_t     *oldsfep;       /* old sf entry */
1258
        xfs_dir2_sf_t           *oldsfp;        /* old sf directory */
1259
        int                     oldsize;        /* old inode size */
1260
        xfs_dir2_sf_entry_t     *sfep;          /* new sf entry */
1261
        xfs_dir2_sf_t           *sfp;           /* new sf directory */
1262
 
1263
        xfs_dir2_trace_args("sf_toino8", args);
1264
        dp = args->dp;
1265
 
1266
        /*
1267
         * Copy the old directory to the buffer.
1268
         * Then nuke it from the inode, and add the new buffer to the inode.
1269
         * Don't want xfs_idata_realloc copying the data here.
1270
         */
1271
        oldsize = dp->i_df.if_bytes;
1272
        buf = kmem_alloc(oldsize, KM_SLEEP);
1273
        oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1274
        ASSERT(oldsfp->hdr.i8count == 0);
1275
        memcpy(buf, oldsfp, oldsize);
1276
        /*
1277
         * Compute the new inode size.
1278
         */
1279
        newsize =
1280
                oldsize +
1281
                (oldsfp->hdr.count + 1) *
1282
                ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1283
        xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1284
        xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1285
        /*
1286
         * Reset our pointers, the data has moved.
1287
         */
1288
        oldsfp = (xfs_dir2_sf_t *)buf;
1289
        sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1290
        /*
1291
         * Fill in the new header.
1292
         */
1293
        sfp->hdr.count = oldsfp->hdr.count;
1294
        sfp->hdr.i8count = 1;
1295
        ino = XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp, &oldsfp->hdr.parent, ARCH_CONVERT);
1296
        XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &ino, &sfp->hdr.parent, ARCH_CONVERT);
1297
        /*
1298
         * Copy the entries field by field.
1299
         */
1300
        for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp),
1301
                    oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp);
1302
             i < sfp->hdr.count;
1303
             i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep),
1304
                  oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep)) {
1305
                sfep->namelen = oldsfep->namelen;
1306
                sfep->offset = oldsfep->offset;
1307
                memcpy(sfep->name, oldsfep->name, sfep->namelen);
1308
                ino = XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp,
1309
                        XFS_DIR2_SF_INUMBERP(oldsfep), ARCH_CONVERT);
1310
                XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &ino, XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT);
1311
        }
1312
        /*
1313
         * Clean up the inode.
1314
         */
1315
        kmem_free(buf, oldsize);
1316
        dp->i_d.di_size = newsize;
1317
        xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1318
}
1319
#endif  /* XFS_BIG_INUMS */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.