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