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