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