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