]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
0d3e0b37 | 2 | * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved. |
2bd0ea18 NS |
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 | ||
41 | /* | |
42 | * Given a block directory (dp/block), calculate its size as a shortform (sf) | |
43 | * directory and a header for the sf directory, if it will fit it the | |
44 | * space currently present in the inode. If it won't fit, the output | |
45 | * size is too big (but not accurate). | |
46 | */ | |
47 | int /* size for sf form */ | |
48 | xfs_dir2_block_sfsize( | |
49 | xfs_inode_t *dp, /* incore inode pointer */ | |
50 | xfs_dir2_block_t *block, /* block directory data */ | |
51 | xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */ | |
52 | { | |
53 | xfs_dir2_dataptr_t addr; /* data entry address */ | |
54 | xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */ | |
55 | xfs_dir2_block_tail_t *btp; /* tail area of the block */ | |
56 | int count; /* shortform entry count */ | |
57 | xfs_dir2_data_entry_t *dep; /* data entry in the block */ | |
58 | int i; /* block entry index */ | |
59 | int i8count; /* count of big-inode entries */ | |
60 | int isdot; /* entry is "." */ | |
61 | int isdotdot; /* entry is ".." */ | |
62 | xfs_mount_t *mp; /* mount structure pointer */ | |
63 | int namelen; /* total name bytes */ | |
64 | xfs_ino_t parent; /* parent inode number */ | |
0e266570 | 65 | int size=0; /* total computed size */ |
2bd0ea18 NS |
66 | |
67 | mp = dp->i_mount; | |
68 | ||
69 | count = i8count = namelen = 0; | |
70 | btp = XFS_DIR2_BLOCK_TAIL_P(mp, block); | |
71 | blp = XFS_DIR2_BLOCK_LEAF_P_ARCH(btp, ARCH_CONVERT); | |
72 | ||
73 | /* | |
74 | * Iterate over the block's data entries by using the leaf pointers. | |
75 | */ | |
76 | for (i = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) { | |
77 | if ((addr = INT_GET(blp[i].address, ARCH_CONVERT)) == XFS_DIR2_NULL_DATAPTR) | |
78 | continue; | |
79 | /* | |
80 | * Calculate the pointer to the entry at hand. | |
81 | */ | |
82 | dep = (xfs_dir2_data_entry_t *) | |
83 | ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, addr)); | |
84 | /* | |
85 | * Detect . and .., so we can special-case them. | |
86 | * . is not included in sf directories. | |
87 | * .. is included by just the parent inode number. | |
88 | */ | |
89 | isdot = dep->namelen == 1 && dep->name[0] == '.'; | |
90 | isdotdot = | |
91 | dep->namelen == 2 && | |
92 | dep->name[0] == '.' && dep->name[1] == '.'; | |
93 | #if XFS_BIG_FILESYSTEMS | |
94 | if (!isdot) | |
95 | i8count += INT_GET(dep->inumber, ARCH_CONVERT) > XFS_DIR2_MAX_SHORT_INUM; | |
96 | #endif | |
97 | if (!isdot && !isdotdot) { | |
98 | count++; | |
99 | namelen += dep->namelen; | |
100 | } else if (isdotdot) | |
101 | parent = INT_GET(dep->inumber, ARCH_CONVERT); | |
102 | /* | |
103 | * Calculate the new size, see if we should give up yet. | |
104 | */ | |
105 | size = XFS_DIR2_SF_HDR_SIZE(i8count) + /* header */ | |
106 | count + /* namelen */ | |
107 | count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */ | |
108 | namelen + /* name */ | |
109 | (i8count ? /* inumber */ | |
110 | (uint)sizeof(xfs_dir2_ino8_t) * count : | |
111 | (uint)sizeof(xfs_dir2_ino4_t) * count); | |
112 | if (size > XFS_IFORK_DSIZE(dp)) | |
113 | return size; /* size value is a failure */ | |
114 | } | |
115 | /* | |
116 | * Create the output header, if it worked. | |
117 | */ | |
118 | sfhp->count = count; | |
119 | sfhp->i8count = i8count; | |
120 | XFS_DIR2_SF_PUT_INUMBER_ARCH((xfs_dir2_sf_t *)sfhp, &parent, &sfhp->parent, ARCH_CONVERT); | |
121 | return size; | |
122 | } | |
123 | ||
124 | /* | |
125 | * Convert a block format directory to shortform. | |
126 | * Caller has already checked that it will fit, and built us a header. | |
127 | */ | |
128 | int /* error */ | |
129 | xfs_dir2_block_to_sf( | |
130 | xfs_da_args_t *args, /* operation arguments */ | |
131 | xfs_dabuf_t *bp, /* block buffer */ | |
132 | int size, /* shortform directory size */ | |
133 | xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */ | |
134 | { | |
135 | xfs_dir2_block_t *block; /* block structure */ | |
136 | xfs_dir2_block_tail_t *btp; /* block tail pointer */ | |
137 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ | |
138 | xfs_inode_t *dp; /* incore directory inode */ | |
139 | xfs_dir2_data_unused_t *dup; /* unused data pointer */ | |
140 | char *endptr; /* end of data entries */ | |
141 | int error; /* error return value */ | |
142 | int logflags; /* inode logging flags */ | |
143 | xfs_mount_t *mp; /* filesystem mount point */ | |
144 | char *ptr; /* current data pointer */ | |
145 | xfs_dir2_sf_entry_t *sfep; /* shortform entry */ | |
146 | xfs_dir2_sf_t *sfp; /* shortform structure */ | |
147 | xfs_ino_t temp; | |
148 | ||
149 | xfs_dir2_trace_args_sb("block_to_sf", args, size, bp); | |
150 | dp = args->dp; | |
151 | mp = dp->i_mount; | |
152 | ||
153 | /* | |
154 | * Make a copy of the block data, so we can shrink the inode | |
155 | * and add local data. | |
156 | */ | |
157 | block = kmem_alloc(mp->m_dirblksize, KM_SLEEP); | |
158 | bcopy(bp->data, block, mp->m_dirblksize); | |
159 | logflags = XFS_ILOG_CORE; | |
0e266570 | 160 | if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) { |
2bd0ea18 NS |
161 | ASSERT(error != ENOSPC); |
162 | goto out; | |
163 | } | |
164 | /* | |
165 | * The buffer is now unconditionally gone, whether | |
166 | * xfs_dir2_shrink_inode worked or not. | |
167 | * | |
168 | * Convert the inode to local format. | |
169 | */ | |
170 | dp->i_df.if_flags &= ~XFS_IFEXTENTS; | |
171 | dp->i_df.if_flags |= XFS_IFINLINE; | |
172 | dp->i_d.di_format = XFS_DINODE_FMT_LOCAL; | |
173 | ASSERT(dp->i_df.if_bytes == 0); | |
174 | xfs_idata_realloc(dp, size, XFS_DATA_FORK); | |
175 | logflags |= XFS_ILOG_DDATA; | |
176 | /* | |
177 | * Copy the header into the newly allocate local space. | |
178 | */ | |
179 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
180 | bcopy(sfhp, sfp, XFS_DIR2_SF_HDR_SIZE(sfhp->i8count)); | |
181 | dp->i_d.di_size = size; | |
182 | /* | |
183 | * Set up to loop over the block's entries. | |
184 | */ | |
185 | btp = XFS_DIR2_BLOCK_TAIL_P(mp, block); | |
186 | ptr = (char *)block->u; | |
187 | endptr = (char *)XFS_DIR2_BLOCK_LEAF_P_ARCH(btp, ARCH_CONVERT); | |
188 | sfep = XFS_DIR2_SF_FIRSTENTRY(sfp); | |
189 | /* | |
190 | * Loop over the active and unused entries. | |
191 | * Stop when we reach the leaf/tail portion of the block. | |
192 | */ | |
193 | while (ptr < endptr) { | |
194 | /* | |
195 | * If it's unused, just skip over it. | |
196 | */ | |
197 | dup = (xfs_dir2_data_unused_t *)ptr; | |
198 | if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) { | |
199 | ptr += INT_GET(dup->length, ARCH_CONVERT); | |
200 | continue; | |
201 | } | |
202 | dep = (xfs_dir2_data_entry_t *)ptr; | |
203 | /* | |
204 | * Skip . | |
205 | */ | |
206 | if (dep->namelen == 1 && dep->name[0] == '.') | |
207 | ASSERT(INT_GET(dep->inumber, ARCH_CONVERT) == dp->i_ino); | |
208 | /* | |
209 | * Skip .., but make sure the inode number is right. | |
210 | */ | |
211 | else if (dep->namelen == 2 && | |
212 | dep->name[0] == '.' && dep->name[1] == '.') | |
213 | ASSERT(INT_GET(dep->inumber, ARCH_CONVERT) == | |
214 | XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, &sfp->hdr.parent, ARCH_CONVERT)); | |
215 | /* | |
216 | * Normal entry, copy it into shortform. | |
217 | */ | |
218 | else { | |
219 | sfep->namelen = dep->namelen; | |
220 | XFS_DIR2_SF_PUT_OFFSET_ARCH(sfep, | |
221 | (xfs_dir2_data_aoff_t) | |
222 | ((char *)dep - (char *)block), ARCH_CONVERT); | |
223 | bcopy(dep->name, sfep->name, dep->namelen); | |
224 | temp=INT_GET(dep->inumber, ARCH_CONVERT); | |
225 | XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &temp, | |
226 | XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT); | |
227 | sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep); | |
228 | } | |
229 | ptr += XFS_DIR2_DATA_ENTSIZE(dep->namelen); | |
230 | } | |
231 | ASSERT((char *)sfep - (char *)sfp == size); | |
232 | xfs_dir2_sf_check(args); | |
233 | out: | |
234 | xfs_trans_log_inode(args->trans, dp, logflags); | |
235 | kmem_free(block, mp->m_dirblksize); | |
236 | return error; | |
237 | } | |
238 | ||
239 | /* | |
240 | * Add a name to a shortform directory. | |
241 | * There are two algorithms, "easy" and "hard" which we decide on | |
242 | * before changing anything. | |
243 | * Convert to block form if necessary, if the new entry won't fit. | |
244 | */ | |
245 | int /* error */ | |
246 | xfs_dir2_sf_addname( | |
247 | xfs_da_args_t *args) /* operation arguments */ | |
248 | { | |
249 | int add_entsize; /* size of the new entry */ | |
250 | xfs_inode_t *dp; /* incore directory inode */ | |
251 | int error; /* error return value */ | |
252 | int incr_isize; /* total change in size */ | |
253 | int new_isize; /* di_size after adding name */ | |
254 | int objchange; /* changing to 8-byte inodes */ | |
255 | xfs_dir2_data_aoff_t offset; /* offset for new entry */ | |
256 | int old_isize; /* di_size before adding name */ | |
257 | int pick; /* which algorithm to use */ | |
258 | xfs_dir2_sf_t *sfp; /* shortform structure */ | |
259 | xfs_dir2_sf_entry_t *sfep; /* shortform entry */ | |
260 | ||
261 | xfs_dir2_trace_args("sf_addname", args); | |
262 | ASSERT(xfs_dir2_sf_lookup(args) == ENOENT); | |
263 | dp = args->dp; | |
264 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); | |
265 | /* | |
266 | * Make sure the shortform value has some of its header. | |
267 | */ | |
268 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { | |
2bd0ea18 NS |
269 | ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount)); |
270 | return XFS_ERROR(EIO); | |
271 | } | |
272 | ASSERT(dp->i_df.if_bytes == dp->i_d.di_size); | |
273 | ASSERT(dp->i_df.if_u1.if_data != NULL); | |
274 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
275 | ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count)); | |
276 | /* | |
277 | * Compute entry (and change in) size. | |
278 | */ | |
279 | add_entsize = XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen); | |
280 | incr_isize = add_entsize; | |
281 | #if XFS_BIG_FILESYSTEMS | |
282 | /* | |
283 | * Do we have to change to 8 byte inodes? | |
284 | */ | |
285 | if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) { | |
2bd0ea18 NS |
286 | /* |
287 | * Yes, adjust the entry size and the total size. | |
288 | */ | |
289 | add_entsize += | |
290 | (uint)sizeof(xfs_dir2_ino8_t) - | |
291 | (uint)sizeof(xfs_dir2_ino4_t); | |
292 | incr_isize += | |
293 | (sfp->hdr.count + 2) * | |
294 | ((uint)sizeof(xfs_dir2_ino8_t) - | |
295 | (uint)sizeof(xfs_dir2_ino4_t)); | |
296 | objchange = 1; | |
297 | } else | |
298 | objchange = 0; | |
299 | #else | |
300 | objchange = 0; | |
301 | #endif | |
302 | old_isize = (int)dp->i_d.di_size; | |
303 | new_isize = old_isize + incr_isize; | |
304 | /* | |
305 | * Won't fit as shortform any more (due to size), | |
306 | * or the pick routine says it won't (due to offset values). | |
307 | */ | |
308 | if (new_isize > XFS_IFORK_DSIZE(dp) || | |
309 | (pick = | |
310 | xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) { | |
2bd0ea18 NS |
311 | /* |
312 | * Just checking or no space reservation, it doesn't fit. | |
313 | */ | |
314 | if (args->justcheck || args->total == 0) | |
315 | return XFS_ERROR(ENOSPC); | |
316 | /* | |
317 | * Convert to block form then add the name. | |
318 | */ | |
319 | error = xfs_dir2_sf_to_block(args); | |
320 | if (error) | |
321 | return error; | |
322 | return xfs_dir2_block_addname(args); | |
323 | } | |
324 | /* | |
325 | * Just checking, it fits. | |
326 | */ | |
327 | if (args->justcheck) | |
328 | return 0; | |
329 | /* | |
330 | * Do it the easy way - just add it at the end. | |
331 | */ | |
332 | if (pick == 1) | |
333 | xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize); | |
334 | /* | |
335 | * Do it the hard way - look for a place to insert the new entry. | |
336 | * Convert to 8 byte inode numbers first if necessary. | |
337 | */ | |
338 | else { | |
339 | ASSERT(pick == 2); | |
340 | #if XFS_BIG_FILESYSTEMS | |
341 | if (objchange) | |
342 | xfs_dir2_sf_toino8(args); | |
343 | #endif | |
344 | xfs_dir2_sf_addname_hard(args, objchange, new_isize); | |
345 | } | |
346 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA); | |
347 | return 0; | |
348 | } | |
349 | ||
350 | /* | |
351 | * Add the new entry the "easy" way. | |
352 | * This is copying the old directory and adding the new entry at the end. | |
353 | * Since it's sorted by "offset" we need room after the last offset | |
354 | * that's already there, and then room to convert to a block directory. | |
355 | * This is already checked by the pick routine. | |
356 | */ | |
357 | STATIC void | |
358 | xfs_dir2_sf_addname_easy( | |
359 | xfs_da_args_t *args, /* operation arguments */ | |
360 | xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */ | |
361 | xfs_dir2_data_aoff_t offset, /* offset to use for new ent */ | |
362 | int new_isize) /* new directory size */ | |
363 | { | |
364 | int byteoff; /* byte offset in sf dir */ | |
365 | xfs_inode_t *dp; /* incore directory inode */ | |
366 | xfs_dir2_sf_t *sfp; /* shortform structure */ | |
367 | ||
368 | dp = args->dp; | |
369 | ||
370 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
371 | byteoff = (int)((char *)sfep - (char *)sfp); | |
372 | /* | |
373 | * Grow the in-inode space. | |
374 | */ | |
375 | xfs_idata_realloc(dp, XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen), | |
376 | XFS_DATA_FORK); | |
377 | /* | |
378 | * Need to set up again due to realloc of the inode data. | |
379 | */ | |
380 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
381 | sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff); | |
382 | /* | |
383 | * Fill in the new entry. | |
384 | */ | |
385 | sfep->namelen = args->namelen; | |
386 | XFS_DIR2_SF_PUT_OFFSET_ARCH(sfep, offset, ARCH_CONVERT); | |
387 | bcopy(args->name, sfep->name, sfep->namelen); | |
388 | XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &args->inumber, | |
389 | XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT); | |
390 | /* | |
391 | * Update the header and inode. | |
392 | */ | |
393 | sfp->hdr.count++; | |
394 | #if XFS_BIG_FILESYSTEMS | |
395 | if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) | |
396 | sfp->hdr.i8count++; | |
397 | #endif | |
398 | dp->i_d.di_size = new_isize; | |
399 | xfs_dir2_sf_check(args); | |
400 | } | |
401 | ||
402 | /* | |
403 | * Add the new entry the "hard" way. | |
404 | * The caller has already converted to 8 byte inode numbers if necessary, | |
405 | * in which case we need to leave the i8count at 1. | |
406 | * Find a hole that the new entry will fit into, and copy | |
407 | * the first part of the entries, the new entry, and the last part of | |
408 | * the entries. | |
409 | */ | |
410 | /* ARGSUSED */ | |
411 | STATIC void | |
412 | xfs_dir2_sf_addname_hard( | |
413 | xfs_da_args_t *args, /* operation arguments */ | |
414 | int objchange, /* changing inode number size */ | |
415 | int new_isize) /* new directory size */ | |
416 | { | |
417 | int add_datasize; /* data size need for new ent */ | |
d663096d | 418 | char *buf; /* buffer for old */ |
2bd0ea18 NS |
419 | xfs_inode_t *dp; /* incore directory inode */ |
420 | int eof; /* reached end of old dir */ | |
421 | int nbytes; /* temp for byte copies */ | |
422 | xfs_dir2_data_aoff_t new_offset; /* next offset value */ | |
423 | xfs_dir2_data_aoff_t offset; /* current offset value */ | |
424 | int old_isize; /* previous di_size */ | |
425 | xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */ | |
426 | xfs_dir2_sf_t *oldsfp; /* original shortform dir */ | |
427 | xfs_dir2_sf_entry_t *sfep; /* entry in new dir */ | |
428 | xfs_dir2_sf_t *sfp; /* new shortform dir */ | |
429 | ||
430 | /* | |
431 | * Copy the old directory to the stack buffer. | |
432 | */ | |
433 | dp = args->dp; | |
434 | ||
435 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
436 | old_isize = (int)dp->i_d.di_size; | |
d663096d | 437 | buf = kmem_alloc(old_isize, KM_SLEEP); |
2bd0ea18 NS |
438 | oldsfp = (xfs_dir2_sf_t *)buf; |
439 | bcopy(sfp, oldsfp, old_isize); | |
440 | /* | |
441 | * Loop over the old directory finding the place we're going | |
442 | * to insert the new entry. | |
443 | * If it's going to end up at the end then oldsfep will point there. | |
444 | */ | |
445 | for (offset = XFS_DIR2_DATA_FIRST_OFFSET, | |
446 | oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp), | |
447 | add_datasize = XFS_DIR2_DATA_ENTSIZE(args->namelen), | |
448 | eof = (char *)oldsfep == &buf[old_isize]; | |
449 | !eof; | |
450 | offset = new_offset + XFS_DIR2_DATA_ENTSIZE(oldsfep->namelen), | |
451 | oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep), | |
452 | eof = (char *)oldsfep == &buf[old_isize]) { | |
453 | new_offset = XFS_DIR2_SF_GET_OFFSET_ARCH(oldsfep, ARCH_CONVERT); | |
454 | if (offset + add_datasize <= new_offset) | |
455 | break; | |
456 | } | |
457 | /* | |
458 | * Get rid of the old directory, then allocate space for | |
459 | * the new one. We do this so xfs_idata_realloc won't copy | |
460 | * the data. | |
461 | */ | |
462 | xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK); | |
463 | xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK); | |
464 | /* | |
465 | * Reset the pointer since the buffer was reallocated. | |
466 | */ | |
467 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
468 | /* | |
469 | * Copy the first part of the directory, including the header. | |
470 | */ | |
471 | nbytes = (int)((char *)oldsfep - (char *)oldsfp); | |
472 | bcopy(oldsfp, sfp, nbytes); | |
473 | sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes); | |
474 | /* | |
475 | * Fill in the new entry, and update the header counts. | |
476 | */ | |
477 | sfep->namelen = args->namelen; | |
478 | XFS_DIR2_SF_PUT_OFFSET_ARCH(sfep, offset, ARCH_CONVERT); | |
479 | bcopy(args->name, sfep->name, sfep->namelen); | |
480 | XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &args->inumber, | |
481 | XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT); | |
482 | sfp->hdr.count++; | |
483 | #if XFS_BIG_FILESYSTEMS | |
484 | if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange) | |
485 | sfp->hdr.i8count++; | |
486 | #endif | |
487 | /* | |
488 | * If there's more left to copy, do that. | |
489 | */ | |
490 | if (!eof) { | |
491 | sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep); | |
492 | bcopy(oldsfep, sfep, old_isize - nbytes); | |
493 | } | |
d663096d | 494 | kmem_free(buf, old_isize); |
2bd0ea18 NS |
495 | dp->i_d.di_size = new_isize; |
496 | xfs_dir2_sf_check(args); | |
497 | } | |
498 | ||
499 | /* | |
500 | * Decide if the new entry will fit at all. | |
501 | * If it will fit, pick between adding the new entry to the end (easy) | |
502 | * or somewhere else (hard). | |
503 | * Return 0 (won't fit), 1 (easy), 2 (hard). | |
504 | */ | |
505 | /*ARGSUSED*/ | |
506 | STATIC int /* pick result */ | |
507 | xfs_dir2_sf_addname_pick( | |
508 | xfs_da_args_t *args, /* operation arguments */ | |
509 | int objchange, /* inode # size changes */ | |
510 | xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */ | |
511 | xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */ | |
512 | { | |
513 | xfs_inode_t *dp; /* incore directory inode */ | |
514 | int holefit; /* found hole it will fit in */ | |
515 | int i; /* entry number */ | |
516 | xfs_mount_t *mp; /* filesystem mount point */ | |
517 | xfs_dir2_data_aoff_t offset; /* data block offset */ | |
518 | xfs_dir2_sf_entry_t *sfep; /* shortform entry */ | |
519 | xfs_dir2_sf_t *sfp; /* shortform structure */ | |
520 | int size; /* entry's data size */ | |
521 | int used; /* data bytes used */ | |
522 | ||
523 | dp = args->dp; | |
524 | mp = dp->i_mount; | |
525 | ||
526 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
527 | size = XFS_DIR2_DATA_ENTSIZE(args->namelen); | |
528 | offset = XFS_DIR2_DATA_FIRST_OFFSET; | |
529 | sfep = XFS_DIR2_SF_FIRSTENTRY(sfp); | |
530 | holefit = 0; | |
531 | /* | |
532 | * Loop over sf entries. | |
533 | * Keep track of data offset and whether we've seen a place | |
534 | * to insert the new entry. | |
535 | */ | |
536 | for (i = 0; i < sfp->hdr.count; i++) { | |
537 | if (!holefit) | |
538 | holefit = offset + size <= XFS_DIR2_SF_GET_OFFSET_ARCH(sfep, ARCH_CONVERT); | |
539 | offset = XFS_DIR2_SF_GET_OFFSET_ARCH(sfep, ARCH_CONVERT) + | |
540 | XFS_DIR2_DATA_ENTSIZE(sfep->namelen); | |
541 | sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep); | |
542 | } | |
543 | /* | |
544 | * Calculate data bytes used excluding the new entry, if this | |
545 | * was a data block (block form directory). | |
546 | */ | |
547 | used = offset + | |
548 | (sfp->hdr.count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) + | |
549 | (uint)sizeof(xfs_dir2_block_tail_t); | |
550 | /* | |
551 | * If it won't fit in a block form then we can't insert it, | |
552 | * we'll go back, convert to block, then try the insert and convert | |
553 | * to leaf. | |
554 | */ | |
555 | if (used + (holefit ? 0 : size) > mp->m_dirblksize) | |
556 | return 0; | |
557 | /* | |
558 | * If changing the inode number size, do it the hard way. | |
559 | */ | |
560 | #if XFS_BIG_FILESYSTEMS | |
561 | if (objchange) { | |
2bd0ea18 NS |
562 | return 2; |
563 | } | |
564 | #else | |
565 | ASSERT(objchange == 0); | |
566 | #endif | |
567 | /* | |
568 | * If it won't fit at the end then do it the hard way (use the hole). | |
569 | */ | |
570 | if (used + size > mp->m_dirblksize) | |
571 | return 2; | |
572 | /* | |
573 | * Do it the easy way. | |
574 | */ | |
575 | *sfepp = sfep; | |
576 | *offsetp = offset; | |
577 | return 1; | |
578 | } | |
579 | ||
580 | #ifdef DEBUG | |
581 | /* | |
582 | * Check consistency of shortform directory, assert if bad. | |
583 | */ | |
584 | STATIC void | |
585 | xfs_dir2_sf_check( | |
586 | xfs_da_args_t *args) /* operation arguments */ | |
587 | { | |
588 | xfs_inode_t *dp; /* incore directory inode */ | |
589 | int i; /* entry number */ | |
590 | int i8count; /* number of big inode#s */ | |
591 | xfs_ino_t ino; /* entry inode number */ | |
592 | int offset; /* data offset */ | |
593 | xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */ | |
594 | xfs_dir2_sf_t *sfp; /* shortform structure */ | |
595 | ||
596 | dp = args->dp; | |
597 | ||
598 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
599 | offset = XFS_DIR2_DATA_FIRST_OFFSET; | |
600 | ino = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, &sfp->hdr.parent, ARCH_CONVERT); | |
601 | i8count = ino > XFS_DIR2_MAX_SHORT_INUM; | |
602 | ||
603 | for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp); | |
604 | i < sfp->hdr.count; | |
605 | i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) { | |
606 | ASSERT(XFS_DIR2_SF_GET_OFFSET_ARCH(sfep, ARCH_CONVERT) >= offset); | |
607 | ino = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT); | |
608 | i8count += ino > XFS_DIR2_MAX_SHORT_INUM; | |
609 | offset = | |
610 | XFS_DIR2_SF_GET_OFFSET_ARCH(sfep, ARCH_CONVERT) + | |
611 | XFS_DIR2_DATA_ENTSIZE(sfep->namelen); | |
612 | } | |
613 | ASSERT(i8count == sfp->hdr.i8count); | |
614 | #if !XFS_BIG_FILESYSTEMS | |
615 | ASSERT(i8count == 0); | |
616 | #endif | |
617 | ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size); | |
618 | ASSERT(offset + | |
619 | (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) + | |
620 | (uint)sizeof(xfs_dir2_block_tail_t) <= | |
621 | dp->i_mount->m_dirblksize); | |
622 | } | |
623 | #endif /* DEBUG */ | |
624 | ||
625 | /* | |
626 | * Create a new (shortform) directory. | |
627 | */ | |
628 | int /* error, always 0 */ | |
629 | xfs_dir2_sf_create( | |
630 | xfs_da_args_t *args, /* operation arguments */ | |
631 | xfs_ino_t pino) /* parent inode number */ | |
632 | { | |
633 | xfs_inode_t *dp; /* incore directory inode */ | |
634 | int i8count; /* parent inode is an 8-byte number */ | |
635 | xfs_dir2_sf_t *sfp; /* shortform structure */ | |
636 | int size; /* directory size */ | |
637 | ||
638 | xfs_dir2_trace_args_i("sf_create", args, pino); | |
639 | dp = args->dp; | |
640 | ||
641 | ASSERT(dp != NULL); | |
642 | ASSERT(dp->i_d.di_size == 0); | |
643 | /* | |
644 | * If it's currently a zero-length extent file, | |
645 | * convert it to local format. | |
646 | */ | |
647 | if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) { | |
648 | dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */ | |
649 | dp->i_d.di_format = XFS_DINODE_FMT_LOCAL; | |
650 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE); | |
651 | dp->i_df.if_flags |= XFS_IFINLINE; | |
652 | } | |
653 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); | |
654 | ASSERT(dp->i_df.if_bytes == 0); | |
655 | i8count = pino > XFS_DIR2_MAX_SHORT_INUM; | |
656 | size = XFS_DIR2_SF_HDR_SIZE(i8count); | |
657 | /* | |
658 | * Make a buffer for the data. | |
659 | */ | |
660 | xfs_idata_realloc(dp, size, XFS_DATA_FORK); | |
661 | /* | |
662 | * Fill in the header, | |
663 | */ | |
664 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
665 | sfp->hdr.i8count = i8count; | |
666 | /* | |
667 | * Now can put in the inode number, since i8count is set. | |
668 | */ | |
669 | XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &pino, &sfp->hdr.parent, ARCH_CONVERT); | |
670 | sfp->hdr.count = 0; | |
671 | dp->i_d.di_size = size; | |
672 | xfs_dir2_sf_check(args); | |
673 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA); | |
674 | return 0; | |
675 | } | |
676 | ||
677 | /* | |
678 | * Lookup an entry in a shortform directory. | |
679 | * Returns EEXIST if found, ENOENT if not found. | |
680 | */ | |
681 | int /* error */ | |
682 | xfs_dir2_sf_lookup( | |
683 | xfs_da_args_t *args) /* operation arguments */ | |
684 | { | |
685 | xfs_inode_t *dp; /* incore directory inode */ | |
686 | int i; /* entry index */ | |
687 | xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */ | |
688 | xfs_dir2_sf_t *sfp; /* shortform structure */ | |
689 | ||
690 | xfs_dir2_trace_args("sf_lookup", args); | |
691 | xfs_dir2_sf_check(args); | |
692 | dp = args->dp; | |
693 | ||
694 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); | |
695 | /* | |
696 | * Bail out if the directory is way too short. | |
697 | */ | |
698 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { | |
2bd0ea18 NS |
699 | ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount)); |
700 | return XFS_ERROR(EIO); | |
701 | } | |
702 | ASSERT(dp->i_df.if_bytes == dp->i_d.di_size); | |
703 | ASSERT(dp->i_df.if_u1.if_data != NULL); | |
704 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
705 | ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count)); | |
706 | /* | |
707 | * Special case for . | |
708 | */ | |
709 | if (args->namelen == 1 && args->name[0] == '.') { | |
710 | args->inumber = dp->i_ino; | |
711 | return XFS_ERROR(EEXIST); | |
712 | } | |
713 | /* | |
714 | * Special case for .. | |
715 | */ | |
716 | if (args->namelen == 2 && | |
717 | args->name[0] == '.' && args->name[1] == '.') { | |
718 | args->inumber = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, &sfp->hdr.parent, ARCH_CONVERT); | |
719 | return XFS_ERROR(EEXIST); | |
720 | } | |
721 | /* | |
722 | * Loop over all the entries trying to match ours. | |
723 | */ | |
724 | for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp); | |
725 | i < sfp->hdr.count; | |
726 | i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) { | |
727 | if (sfep->namelen == args->namelen && | |
728 | sfep->name[0] == args->name[0] && | |
729 | bcmp(args->name, sfep->name, args->namelen) == 0) { | |
730 | args->inumber = | |
731 | XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, | |
732 | XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT); | |
733 | return XFS_ERROR(EEXIST); | |
734 | } | |
735 | } | |
736 | /* | |
737 | * Didn't find it. | |
738 | */ | |
739 | ASSERT(args->oknoent); | |
740 | return XFS_ERROR(ENOENT); | |
741 | } | |
742 | ||
743 | /* | |
744 | * Remove an entry from a shortform directory. | |
745 | */ | |
746 | int /* error */ | |
747 | xfs_dir2_sf_removename( | |
748 | xfs_da_args_t *args) | |
749 | { | |
750 | int byteoff; /* offset of removed entry */ | |
751 | xfs_inode_t *dp; /* incore directory inode */ | |
752 | int entsize; /* this entry's size */ | |
753 | int i; /* shortform entry index */ | |
754 | int newsize; /* new inode size */ | |
755 | int oldsize; /* old inode size */ | |
756 | xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */ | |
757 | xfs_dir2_sf_t *sfp; /* shortform structure */ | |
758 | ||
759 | xfs_dir2_trace_args("sf_removename", args); | |
760 | dp = args->dp; | |
761 | ||
762 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); | |
763 | oldsize = (int)dp->i_d.di_size; | |
764 | /* | |
765 | * Bail out if the directory is way too short. | |
766 | */ | |
767 | if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) { | |
2bd0ea18 NS |
768 | ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount)); |
769 | return XFS_ERROR(EIO); | |
770 | } | |
771 | ASSERT(dp->i_df.if_bytes == oldsize); | |
772 | ASSERT(dp->i_df.if_u1.if_data != NULL); | |
773 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
774 | ASSERT(oldsize >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count)); | |
775 | /* | |
776 | * Loop over the old directory entries. | |
777 | * Find the one we're deleting. | |
778 | */ | |
779 | for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp); | |
780 | i < sfp->hdr.count; | |
781 | i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) { | |
782 | if (sfep->namelen == args->namelen && | |
783 | sfep->name[0] == args->name[0] && | |
784 | bcmp(sfep->name, args->name, args->namelen) == 0) { | |
785 | ASSERT(XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, | |
786 | XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT) == | |
787 | args->inumber); | |
788 | break; | |
789 | } | |
790 | } | |
791 | /* | |
792 | * Didn't find it. | |
793 | */ | |
794 | if (i == sfp->hdr.count) { | |
2bd0ea18 NS |
795 | return XFS_ERROR(ENOENT); |
796 | } | |
797 | /* | |
798 | * Calculate sizes. | |
799 | */ | |
800 | byteoff = (int)((char *)sfep - (char *)sfp); | |
801 | entsize = XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen); | |
802 | newsize = oldsize - entsize; | |
803 | /* | |
804 | * Copy the part if any after the removed entry, sliding it down. | |
805 | */ | |
806 | if (byteoff + entsize < oldsize) | |
807 | ovbcopy((char *)sfp + byteoff + entsize, (char *)sfp + byteoff, | |
808 | oldsize - (byteoff + entsize)); | |
809 | /* | |
810 | * Fix up the header and file size. | |
811 | */ | |
812 | sfp->hdr.count--; | |
813 | dp->i_d.di_size = newsize; | |
814 | /* | |
815 | * Reallocate, making it smaller. | |
816 | */ | |
817 | xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK); | |
818 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
819 | #if XFS_BIG_FILESYSTEMS | |
820 | /* | |
821 | * Are we changing inode number size? | |
822 | */ | |
823 | if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) { | |
2bd0ea18 NS |
824 | if (sfp->hdr.i8count == 1) |
825 | xfs_dir2_sf_toino4(args); | |
826 | else | |
827 | sfp->hdr.i8count--; | |
828 | } | |
829 | #endif | |
830 | xfs_dir2_sf_check(args); | |
831 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA); | |
832 | return 0; | |
833 | } | |
834 | ||
835 | /* | |
836 | * Replace the inode number of an entry in a shortform directory. | |
837 | */ | |
838 | int /* error */ | |
839 | xfs_dir2_sf_replace( | |
840 | xfs_da_args_t *args) /* operation arguments */ | |
841 | { | |
842 | xfs_inode_t *dp; /* incore directory inode */ | |
843 | int i; /* entry index */ | |
844 | #if XFS_BIG_FILESYSTEMS || defined(DEBUG) | |
0e266570 | 845 | xfs_ino_t ino=0; /* entry old inode number */ |
2bd0ea18 NS |
846 | #endif |
847 | xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */ | |
848 | xfs_dir2_sf_t *sfp; /* shortform structure */ | |
849 | ||
850 | xfs_dir2_trace_args("sf_replace", args); | |
851 | dp = args->dp; | |
852 | ||
853 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); | |
854 | /* | |
855 | * Bail out if the shortform directory is way too small. | |
856 | */ | |
857 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { | |
2bd0ea18 NS |
858 | ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount)); |
859 | return XFS_ERROR(EIO); | |
860 | } | |
861 | ASSERT(dp->i_df.if_bytes == dp->i_d.di_size); | |
862 | ASSERT(dp->i_df.if_u1.if_data != NULL); | |
863 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
864 | ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count)); | |
865 | #if XFS_BIG_FILESYSTEMS | |
866 | /* | |
867 | * New inode number is large, and need to convert to 8-byte inodes. | |
868 | */ | |
869 | if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) { | |
2bd0ea18 NS |
870 | int error; /* error return value */ |
871 | int newsize; /* new inode size */ | |
872 | ||
873 | newsize = | |
874 | dp->i_df.if_bytes + | |
875 | (sfp->hdr.count + 1) * | |
876 | ((uint)sizeof(xfs_dir2_ino8_t) - | |
877 | (uint)sizeof(xfs_dir2_ino4_t)); | |
878 | /* | |
879 | * Won't fit as shortform, convert to block then do replace. | |
880 | */ | |
881 | if (newsize > XFS_IFORK_DSIZE(dp)) { | |
882 | error = xfs_dir2_sf_to_block(args); | |
883 | if (error) { | |
884 | return error; | |
885 | } | |
886 | return xfs_dir2_block_replace(args); | |
887 | } | |
888 | /* | |
889 | * Still fits, convert to 8-byte now. | |
890 | */ | |
891 | xfs_dir2_sf_toino8(args); | |
892 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
893 | } | |
894 | #endif | |
895 | ASSERT(args->namelen != 1 || args->name[0] != '.'); | |
896 | /* | |
897 | * Replace ..'s entry. | |
898 | */ | |
899 | if (args->namelen == 2 && | |
900 | args->name[0] == '.' && args->name[1] == '.') { | |
901 | #if XFS_BIG_FILESYSTEMS || defined(DEBUG) | |
902 | ino = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, &sfp->hdr.parent, ARCH_CONVERT); | |
903 | ASSERT(args->inumber != ino); | |
904 | #endif | |
905 | XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &args->inumber, &sfp->hdr.parent, ARCH_CONVERT); | |
906 | } | |
907 | /* | |
908 | * Normal entry, look for the name. | |
909 | */ | |
910 | else { | |
911 | for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp); | |
912 | i < sfp->hdr.count; | |
913 | i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) { | |
914 | if (sfep->namelen == args->namelen && | |
915 | sfep->name[0] == args->name[0] && | |
916 | bcmp(args->name, sfep->name, args->namelen) == 0) { | |
917 | #if XFS_BIG_FILESYSTEMS || defined(DEBUG) | |
918 | ino = XFS_DIR2_SF_GET_INUMBER_ARCH(sfp, | |
919 | XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT); | |
920 | ASSERT(args->inumber != ino); | |
921 | #endif | |
922 | XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &args->inumber, | |
923 | XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT); | |
924 | break; | |
925 | } | |
926 | } | |
927 | /* | |
928 | * Didn't find it. | |
929 | */ | |
930 | if (i == sfp->hdr.count) { | |
2bd0ea18 NS |
931 | ASSERT(args->oknoent); |
932 | return XFS_ERROR(ENOENT); | |
933 | } | |
934 | } | |
935 | #if XFS_BIG_FILESYSTEMS | |
936 | /* | |
937 | * See if the old number was large, the new number is small. | |
938 | */ | |
939 | if (ino > XFS_DIR2_MAX_SHORT_INUM && | |
940 | args->inumber <= XFS_DIR2_MAX_SHORT_INUM) { | |
2bd0ea18 NS |
941 | /* |
942 | * And the old count was one, so need to convert to small. | |
943 | */ | |
944 | if (sfp->hdr.i8count == 1) | |
945 | xfs_dir2_sf_toino4(args); | |
946 | else | |
947 | sfp->hdr.i8count--; | |
948 | } | |
949 | #endif | |
950 | xfs_dir2_sf_check(args); | |
951 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA); | |
952 | return 0; | |
953 | } | |
954 | ||
955 | #if XFS_BIG_FILESYSTEMS | |
956 | /* | |
957 | * Convert from 8-byte inode numbers to 4-byte inode numbers. | |
958 | * The last 8-byte inode number is gone, but the count is still 1. | |
959 | */ | |
960 | STATIC void | |
961 | xfs_dir2_sf_toino4( | |
962 | xfs_da_args_t *args) /* operation arguments */ | |
963 | { | |
964 | char *buf; /* old dir's buffer */ | |
965 | xfs_inode_t *dp; /* incore directory inode */ | |
966 | int i; /* entry index */ | |
967 | xfs_ino_t ino; /* entry inode number */ | |
968 | int newsize; /* new inode size */ | |
969 | xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */ | |
970 | xfs_dir2_sf_t *oldsfp; /* old sf directory */ | |
971 | int oldsize; /* old inode size */ | |
972 | xfs_dir2_sf_entry_t *sfep; /* new sf entry */ | |
973 | xfs_dir2_sf_t *sfp; /* new sf directory */ | |
974 | ||
975 | xfs_dir2_trace_args("sf_toino4", args); | |
976 | dp = args->dp; | |
977 | ||
978 | /* | |
979 | * Copy the old directory to the buffer. | |
980 | * Then nuke it from the inode, and add the new buffer to the inode. | |
981 | * Don't want xfs_idata_realloc copying the data here. | |
982 | */ | |
983 | oldsize = dp->i_df.if_bytes; | |
984 | buf = kmem_alloc(oldsize, KM_SLEEP); | |
985 | oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
986 | ASSERT(oldsfp->hdr.i8count == 1); | |
987 | bcopy(oldsfp, buf, oldsize); | |
988 | /* | |
989 | * Compute the new inode size. | |
990 | */ | |
991 | newsize = | |
992 | oldsize - | |
993 | (oldsfp->hdr.count + 1) * | |
994 | ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)); | |
995 | xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK); | |
996 | xfs_idata_realloc(dp, newsize, XFS_DATA_FORK); | |
997 | /* | |
998 | * Reset our pointers, the data has moved. | |
999 | */ | |
1000 | oldsfp = (xfs_dir2_sf_t *)buf; | |
1001 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
1002 | /* | |
1003 | * Fill in the new header. | |
1004 | */ | |
1005 | sfp->hdr.count = oldsfp->hdr.count; | |
1006 | sfp->hdr.i8count = 0; | |
1007 | ino = XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp, &oldsfp->hdr.parent, ARCH_CONVERT); | |
1008 | XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &ino, &sfp->hdr.parent, ARCH_CONVERT); | |
1009 | /* | |
1010 | * Copy the entries field by field. | |
1011 | */ | |
1012 | for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp), | |
1013 | oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp); | |
1014 | i < sfp->hdr.count; | |
1015 | i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep), | |
1016 | oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep)) { | |
1017 | sfep->namelen = oldsfep->namelen; | |
1018 | sfep->offset = oldsfep->offset; | |
1019 | bcopy(oldsfep->name, sfep->name, sfep->namelen); | |
1020 | ino = XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp, | |
1021 | XFS_DIR2_SF_INUMBERP(oldsfep), ARCH_CONVERT); | |
1022 | XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &ino, XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT); | |
1023 | } | |
1024 | /* | |
1025 | * Clean up the inode. | |
1026 | */ | |
1027 | kmem_free(buf, oldsize); | |
1028 | dp->i_d.di_size = newsize; | |
1029 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA); | |
1030 | } | |
1031 | ||
1032 | /* | |
1033 | * Convert from 4-byte inode numbers to 8-byte inode numbers. | |
1034 | * The new 8-byte inode number is not there yet, we leave with the | |
1035 | * count 1 but no corresponding entry. | |
1036 | */ | |
1037 | STATIC void | |
1038 | xfs_dir2_sf_toino8( | |
1039 | xfs_da_args_t *args) /* operation arguments */ | |
1040 | { | |
1041 | char *buf; /* old dir's buffer */ | |
1042 | xfs_inode_t *dp; /* incore directory inode */ | |
1043 | int i; /* entry index */ | |
1044 | xfs_ino_t ino; /* entry inode number */ | |
1045 | int newsize; /* new inode size */ | |
1046 | xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */ | |
1047 | xfs_dir2_sf_t *oldsfp; /* old sf directory */ | |
1048 | int oldsize; /* old inode size */ | |
1049 | xfs_dir2_sf_entry_t *sfep; /* new sf entry */ | |
1050 | xfs_dir2_sf_t *sfp; /* new sf directory */ | |
1051 | ||
1052 | xfs_dir2_trace_args("sf_toino8", args); | |
1053 | dp = args->dp; | |
1054 | ||
1055 | /* | |
1056 | * Copy the old directory to the buffer. | |
1057 | * Then nuke it from the inode, and add the new buffer to the inode. | |
1058 | * Don't want xfs_idata_realloc copying the data here. | |
1059 | */ | |
1060 | oldsize = dp->i_df.if_bytes; | |
1061 | buf = kmem_alloc(oldsize, KM_SLEEP); | |
1062 | oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
1063 | ASSERT(oldsfp->hdr.i8count == 0); | |
1064 | bcopy(oldsfp, buf, oldsize); | |
1065 | /* | |
1066 | * Compute the new inode size. | |
1067 | */ | |
1068 | newsize = | |
1069 | oldsize + | |
1070 | (oldsfp->hdr.count + 1) * | |
1071 | ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)); | |
1072 | xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK); | |
1073 | xfs_idata_realloc(dp, newsize, XFS_DATA_FORK); | |
1074 | /* | |
1075 | * Reset our pointers, the data has moved. | |
1076 | */ | |
1077 | oldsfp = (xfs_dir2_sf_t *)buf; | |
1078 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
1079 | /* | |
1080 | * Fill in the new header. | |
1081 | */ | |
1082 | sfp->hdr.count = oldsfp->hdr.count; | |
1083 | sfp->hdr.i8count = 1; | |
1084 | ino = XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp, &oldsfp->hdr.parent, ARCH_CONVERT); | |
1085 | XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &ino, &sfp->hdr.parent, ARCH_CONVERT); | |
1086 | /* | |
1087 | * Copy the entries field by field. | |
1088 | */ | |
1089 | for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp), | |
1090 | oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp); | |
1091 | i < sfp->hdr.count; | |
1092 | i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep), | |
1093 | oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep)) { | |
1094 | sfep->namelen = oldsfep->namelen; | |
1095 | sfep->offset = oldsfep->offset; | |
1096 | bcopy(oldsfep->name, sfep->name, sfep->namelen); | |
1097 | ino = XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp, | |
1098 | XFS_DIR2_SF_INUMBERP(oldsfep), ARCH_CONVERT); | |
1099 | XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp, &ino, XFS_DIR2_SF_INUMBERP(sfep), ARCH_CONVERT); | |
1100 | } | |
1101 | /* | |
1102 | * Clean up the inode. | |
1103 | */ | |
1104 | kmem_free(buf, oldsize); | |
1105 | dp->i_d.di_size = newsize; | |
1106 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA); | |
1107 | } | |
1108 | #endif /* XFS_BIG_FILESYSTEMS */ |