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