Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
1da177e4 | 20 | #include "xfs_types.h" |
1da177e4 | 21 | #include "xfs_log.h" |
a844f451 | 22 | #include "xfs_inum.h" |
1da177e4 LT |
23 | #include "xfs_trans.h" |
24 | #include "xfs_sb.h" | |
25 | #include "xfs_ag.h" | |
1da177e4 | 26 | #include "xfs_mount.h" |
a844f451 | 27 | #include "xfs_da_btree.h" |
1da177e4 | 28 | #include "xfs_bmap_btree.h" |
a844f451 | 29 | #include "xfs_alloc_btree.h" |
1da177e4 | 30 | #include "xfs_dinode.h" |
1da177e4 | 31 | #include "xfs_inode.h" |
a844f451 | 32 | #include "xfs_inode_item.h" |
1da177e4 | 33 | #include "xfs_bmap.h" |
57926640 | 34 | #include "xfs_dir2_format.h" |
2b9ab5ab | 35 | #include "xfs_dir2.h" |
57926640 | 36 | #include "xfs_dir2_priv.h" |
1da177e4 | 37 | #include "xfs_error.h" |
0b1b213f | 38 | #include "xfs_trace.h" |
1da177e4 | 39 | |
4a24cb71 | 40 | struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2}; |
1da177e4 | 41 | |
189f4bf2 BN |
42 | /* |
43 | * ASCII case-insensitive (ie. A-Z) support for directories that was | |
44 | * used in IRIX. | |
45 | */ | |
46 | STATIC xfs_dahash_t | |
47 | xfs_ascii_ci_hashname( | |
48 | struct xfs_name *name) | |
49 | { | |
50 | xfs_dahash_t hash; | |
51 | int i; | |
52 | ||
53 | for (i = 0, hash = 0; i < name->len; i++) | |
54 | hash = tolower(name->name[i]) ^ rol32(hash, 7); | |
55 | ||
56 | return hash; | |
57 | } | |
58 | ||
59 | STATIC enum xfs_dacmp | |
60 | xfs_ascii_ci_compname( | |
61 | struct xfs_da_args *args, | |
2bc75421 DC |
62 | const unsigned char *name, |
63 | int len) | |
189f4bf2 BN |
64 | { |
65 | enum xfs_dacmp result; | |
66 | int i; | |
67 | ||
68 | if (args->namelen != len) | |
69 | return XFS_CMP_DIFFERENT; | |
70 | ||
71 | result = XFS_CMP_EXACT; | |
72 | for (i = 0; i < len; i++) { | |
73 | if (args->name[i] == name[i]) | |
74 | continue; | |
75 | if (tolower(args->name[i]) != tolower(name[i])) | |
76 | return XFS_CMP_DIFFERENT; | |
77 | result = XFS_CMP_CASE; | |
78 | } | |
79 | ||
80 | return result; | |
81 | } | |
82 | ||
83 | static struct xfs_nameops xfs_ascii_ci_nameops = { | |
84 | .hashname = xfs_ascii_ci_hashname, | |
85 | .compname = xfs_ascii_ci_compname, | |
86 | }; | |
87 | ||
f6c2d1fa NS |
88 | void |
89 | xfs_dir_mount( | |
90 | xfs_mount_t *mp) | |
1da177e4 | 91 | { |
62118709 | 92 | ASSERT(xfs_sb_version_hasdirv2(&mp->m_sb)); |
1da177e4 LT |
93 | ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <= |
94 | XFS_MAX_BLOCKSIZE); | |
95 | mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog); | |
96 | mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog; | |
bbaaf538 CH |
97 | mp->m_dirdatablk = xfs_dir2_db_to_da(mp, XFS_DIR2_DATA_FIRSTDB(mp)); |
98 | mp->m_dirleafblk = xfs_dir2_db_to_da(mp, XFS_DIR2_LEAF_FIRSTDB(mp)); | |
99 | mp->m_dirfreeblk = xfs_dir2_db_to_da(mp, XFS_DIR2_FREE_FIRSTDB(mp)); | |
1da177e4 LT |
100 | mp->m_attr_node_ents = |
101 | (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) / | |
102 | (uint)sizeof(xfs_da_node_entry_t); | |
103 | mp->m_dir_node_ents = | |
104 | (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) / | |
105 | (uint)sizeof(xfs_da_node_entry_t); | |
106 | mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100; | |
189f4bf2 BN |
107 | if (xfs_sb_version_hasasciici(&mp->m_sb)) |
108 | mp->m_dirnameops = &xfs_ascii_ci_nameops; | |
109 | else | |
110 | mp->m_dirnameops = &xfs_default_nameops; | |
1da177e4 LT |
111 | } |
112 | ||
113 | /* | |
114 | * Return 1 if directory contains only "." and "..". | |
115 | */ | |
f6c2d1fa NS |
116 | int |
117 | xfs_dir_isempty( | |
118 | xfs_inode_t *dp) | |
1da177e4 | 119 | { |
ac8ba50f | 120 | xfs_dir2_sf_hdr_t *sfp; |
1da177e4 | 121 | |
abbede1b | 122 | ASSERT(S_ISDIR(dp->i_d.di_mode)); |
f6c2d1fa | 123 | if (dp->i_d.di_size == 0) /* might happen during shutdown. */ |
1da177e4 | 124 | return 1; |
1da177e4 LT |
125 | if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp)) |
126 | return 0; | |
ac8ba50f CH |
127 | sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data; |
128 | return !sfp->count; | |
1da177e4 LT |
129 | } |
130 | ||
f6c2d1fa NS |
131 | /* |
132 | * Validate a given inode number. | |
133 | */ | |
134 | int | |
135 | xfs_dir_ino_validate( | |
136 | xfs_mount_t *mp, | |
137 | xfs_ino_t ino) | |
138 | { | |
139 | xfs_agblock_t agblkno; | |
140 | xfs_agino_t agino; | |
141 | xfs_agnumber_t agno; | |
142 | int ino_ok; | |
143 | int ioff; | |
144 | ||
145 | agno = XFS_INO_TO_AGNO(mp, ino); | |
146 | agblkno = XFS_INO_TO_AGBNO(mp, ino); | |
147 | ioff = XFS_INO_TO_OFFSET(mp, ino); | |
148 | agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff); | |
149 | ino_ok = | |
150 | agno < mp->m_sb.sb_agcount && | |
151 | agblkno < mp->m_sb.sb_agblocks && | |
152 | agblkno != 0 && | |
153 | ioff < (1 << mp->m_sb.sb_inopblog) && | |
154 | XFS_AGINO_TO_INO(mp, agno, agino) == ino; | |
155 | if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE, | |
156 | XFS_RANDOM_DIR_INO_VALIDATE))) { | |
53487786 | 157 | xfs_warn(mp, "Invalid inode number 0x%Lx", |
f6c2d1fa NS |
158 | (unsigned long long) ino); |
159 | XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp); | |
160 | return XFS_ERROR(EFSCORRUPTED); | |
161 | } | |
162 | return 0; | |
163 | } | |
164 | ||
1da177e4 LT |
165 | /* |
166 | * Initialize a directory with its "." and ".." entries. | |
167 | */ | |
f6c2d1fa NS |
168 | int |
169 | xfs_dir_init( | |
170 | xfs_trans_t *tp, | |
171 | xfs_inode_t *dp, | |
172 | xfs_inode_t *pdp) | |
1da177e4 | 173 | { |
f6c2d1fa NS |
174 | xfs_da_args_t args; |
175 | int error; | |
1da177e4 LT |
176 | |
177 | memset((char *)&args, 0, sizeof(args)); | |
178 | args.dp = dp; | |
179 | args.trans = tp; | |
abbede1b | 180 | ASSERT(S_ISDIR(dp->i_d.di_mode)); |
f6c2d1fa | 181 | if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino))) |
1da177e4 | 182 | return error; |
1da177e4 LT |
183 | return xfs_dir2_sf_create(&args, pdp->i_ino); |
184 | } | |
185 | ||
186 | /* | |
187 | Enter a name in a directory. | |
188 | */ | |
f6c2d1fa NS |
189 | int |
190 | xfs_dir_createname( | |
191 | xfs_trans_t *tp, | |
192 | xfs_inode_t *dp, | |
556b8b16 | 193 | struct xfs_name *name, |
1da177e4 LT |
194 | xfs_ino_t inum, /* new entry inode number */ |
195 | xfs_fsblock_t *first, /* bmap's firstblock */ | |
196 | xfs_bmap_free_t *flist, /* bmap's freeblock list */ | |
197 | xfs_extlen_t total) /* bmap's total block count */ | |
198 | { | |
f6c2d1fa NS |
199 | xfs_da_args_t args; |
200 | int rval; | |
1da177e4 LT |
201 | int v; /* type-checking value */ |
202 | ||
abbede1b | 203 | ASSERT(S_ISDIR(dp->i_d.di_mode)); |
f6c2d1fa | 204 | if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) |
1da177e4 | 205 | return rval; |
1da177e4 | 206 | XFS_STATS_INC(xs_dir_create); |
f6c2d1fa | 207 | |
87affd08 | 208 | memset(&args, 0, sizeof(xfs_da_args_t)); |
556b8b16 BN |
209 | args.name = name->name; |
210 | args.namelen = name->len; | |
5163f95a | 211 | args.hashval = dp->i_mount->m_dirnameops->hashname(name); |
1da177e4 LT |
212 | args.inumber = inum; |
213 | args.dp = dp; | |
214 | args.firstblock = first; | |
215 | args.flist = flist; | |
216 | args.total = total; | |
217 | args.whichfork = XFS_DATA_FORK; | |
218 | args.trans = tp; | |
6a178100 | 219 | args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT; |
f6c2d1fa | 220 | |
1da177e4 LT |
221 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
222 | rval = xfs_dir2_sf_addname(&args); | |
f6c2d1fa | 223 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) |
1da177e4 | 224 | return rval; |
f6c2d1fa | 225 | else if (v) |
1da177e4 | 226 | rval = xfs_dir2_block_addname(&args); |
f6c2d1fa | 227 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) |
1da177e4 | 228 | return rval; |
f6c2d1fa | 229 | else if (v) |
1da177e4 LT |
230 | rval = xfs_dir2_leaf_addname(&args); |
231 | else | |
232 | rval = xfs_dir2_node_addname(&args); | |
233 | return rval; | |
234 | } | |
235 | ||
384f3ced BN |
236 | /* |
237 | * If doing a CI lookup and case-insensitive match, dup actual name into | |
238 | * args.value. Return EEXIST for success (ie. name found) or an error. | |
239 | */ | |
240 | int | |
241 | xfs_dir_cilookup_result( | |
242 | struct xfs_da_args *args, | |
a3380ae3 | 243 | const unsigned char *name, |
384f3ced BN |
244 | int len) |
245 | { | |
246 | if (args->cmpresult == XFS_CMP_DIFFERENT) | |
247 | return ENOENT; | |
248 | if (args->cmpresult != XFS_CMP_CASE || | |
249 | !(args->op_flags & XFS_DA_OP_CILOOKUP)) | |
250 | return EEXIST; | |
251 | ||
3f52c2f0 | 252 | args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL); |
384f3ced BN |
253 | if (!args->value) |
254 | return ENOMEM; | |
255 | ||
256 | memcpy(args->value, name, len); | |
257 | args->valuelen = len; | |
258 | return EEXIST; | |
259 | } | |
260 | ||
1da177e4 LT |
261 | /* |
262 | * Lookup a name in a directory, give back the inode number. | |
384f3ced BN |
263 | * If ci_name is not NULL, returns the actual name in ci_name if it differs |
264 | * to name, or ci_name->name is set to NULL for an exact match. | |
1da177e4 | 265 | */ |
384f3ced | 266 | |
f6c2d1fa NS |
267 | int |
268 | xfs_dir_lookup( | |
269 | xfs_trans_t *tp, | |
270 | xfs_inode_t *dp, | |
556b8b16 | 271 | struct xfs_name *name, |
384f3ced BN |
272 | xfs_ino_t *inum, /* out: inode number */ |
273 | struct xfs_name *ci_name) /* out: actual name if CI match */ | |
1da177e4 | 274 | { |
f6c2d1fa NS |
275 | xfs_da_args_t args; |
276 | int rval; | |
1da177e4 LT |
277 | int v; /* type-checking value */ |
278 | ||
abbede1b | 279 | ASSERT(S_ISDIR(dp->i_d.di_mode)); |
1da177e4 LT |
280 | XFS_STATS_INC(xs_dir_lookup); |
281 | ||
87affd08 | 282 | memset(&args, 0, sizeof(xfs_da_args_t)); |
556b8b16 BN |
283 | args.name = name->name; |
284 | args.namelen = name->len; | |
5163f95a | 285 | args.hashval = dp->i_mount->m_dirnameops->hashname(name); |
1da177e4 | 286 | args.dp = dp; |
1da177e4 LT |
287 | args.whichfork = XFS_DATA_FORK; |
288 | args.trans = tp; | |
6a178100 | 289 | args.op_flags = XFS_DA_OP_OKNOENT; |
384f3ced BN |
290 | if (ci_name) |
291 | args.op_flags |= XFS_DA_OP_CILOOKUP; | |
f6c2d1fa | 292 | |
1da177e4 LT |
293 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
294 | rval = xfs_dir2_sf_lookup(&args); | |
f6c2d1fa | 295 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) |
1da177e4 | 296 | return rval; |
f6c2d1fa | 297 | else if (v) |
1da177e4 | 298 | rval = xfs_dir2_block_lookup(&args); |
f6c2d1fa | 299 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) |
1da177e4 | 300 | return rval; |
f6c2d1fa | 301 | else if (v) |
1da177e4 LT |
302 | rval = xfs_dir2_leaf_lookup(&args); |
303 | else | |
304 | rval = xfs_dir2_node_lookup(&args); | |
305 | if (rval == EEXIST) | |
306 | rval = 0; | |
384f3ced | 307 | if (!rval) { |
1da177e4 | 308 | *inum = args.inumber; |
384f3ced BN |
309 | if (ci_name) { |
310 | ci_name->name = args.value; | |
311 | ci_name->len = args.valuelen; | |
312 | } | |
313 | } | |
1da177e4 LT |
314 | return rval; |
315 | } | |
316 | ||
317 | /* | |
318 | * Remove an entry from a directory. | |
319 | */ | |
f6c2d1fa NS |
320 | int |
321 | xfs_dir_removename( | |
322 | xfs_trans_t *tp, | |
323 | xfs_inode_t *dp, | |
556b8b16 | 324 | struct xfs_name *name, |
f6c2d1fa | 325 | xfs_ino_t ino, |
1da177e4 LT |
326 | xfs_fsblock_t *first, /* bmap's firstblock */ |
327 | xfs_bmap_free_t *flist, /* bmap's freeblock list */ | |
328 | xfs_extlen_t total) /* bmap's total block count */ | |
329 | { | |
f6c2d1fa NS |
330 | xfs_da_args_t args; |
331 | int rval; | |
1da177e4 LT |
332 | int v; /* type-checking value */ |
333 | ||
abbede1b | 334 | ASSERT(S_ISDIR(dp->i_d.di_mode)); |
1da177e4 | 335 | XFS_STATS_INC(xs_dir_remove); |
f6c2d1fa | 336 | |
87affd08 | 337 | memset(&args, 0, sizeof(xfs_da_args_t)); |
556b8b16 BN |
338 | args.name = name->name; |
339 | args.namelen = name->len; | |
5163f95a | 340 | args.hashval = dp->i_mount->m_dirnameops->hashname(name); |
1da177e4 LT |
341 | args.inumber = ino; |
342 | args.dp = dp; | |
343 | args.firstblock = first; | |
344 | args.flist = flist; | |
345 | args.total = total; | |
346 | args.whichfork = XFS_DATA_FORK; | |
347 | args.trans = tp; | |
f6c2d1fa | 348 | |
1da177e4 LT |
349 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
350 | rval = xfs_dir2_sf_removename(&args); | |
f6c2d1fa | 351 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) |
1da177e4 | 352 | return rval; |
f6c2d1fa | 353 | else if (v) |
1da177e4 | 354 | rval = xfs_dir2_block_removename(&args); |
f6c2d1fa | 355 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) |
1da177e4 | 356 | return rval; |
f6c2d1fa | 357 | else if (v) |
1da177e4 LT |
358 | rval = xfs_dir2_leaf_removename(&args); |
359 | else | |
360 | rval = xfs_dir2_node_removename(&args); | |
361 | return rval; | |
362 | } | |
363 | ||
1da177e4 LT |
364 | /* |
365 | * Replace the inode number of a directory entry. | |
366 | */ | |
f6c2d1fa NS |
367 | int |
368 | xfs_dir_replace( | |
369 | xfs_trans_t *tp, | |
370 | xfs_inode_t *dp, | |
556b8b16 | 371 | struct xfs_name *name, /* name of entry to replace */ |
1da177e4 LT |
372 | xfs_ino_t inum, /* new inode number */ |
373 | xfs_fsblock_t *first, /* bmap's firstblock */ | |
374 | xfs_bmap_free_t *flist, /* bmap's freeblock list */ | |
375 | xfs_extlen_t total) /* bmap's total block count */ | |
376 | { | |
f6c2d1fa NS |
377 | xfs_da_args_t args; |
378 | int rval; | |
1da177e4 LT |
379 | int v; /* type-checking value */ |
380 | ||
abbede1b | 381 | ASSERT(S_ISDIR(dp->i_d.di_mode)); |
1da177e4 | 382 | |
f6c2d1fa | 383 | if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) |
1da177e4 | 384 | return rval; |
f6c2d1fa | 385 | |
87affd08 | 386 | memset(&args, 0, sizeof(xfs_da_args_t)); |
556b8b16 BN |
387 | args.name = name->name; |
388 | args.namelen = name->len; | |
5163f95a | 389 | args.hashval = dp->i_mount->m_dirnameops->hashname(name); |
1da177e4 LT |
390 | args.inumber = inum; |
391 | args.dp = dp; | |
392 | args.firstblock = first; | |
393 | args.flist = flist; | |
394 | args.total = total; | |
395 | args.whichfork = XFS_DATA_FORK; | |
396 | args.trans = tp; | |
f6c2d1fa | 397 | |
1da177e4 LT |
398 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
399 | rval = xfs_dir2_sf_replace(&args); | |
f6c2d1fa | 400 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) |
1da177e4 | 401 | return rval; |
f6c2d1fa | 402 | else if (v) |
1da177e4 | 403 | rval = xfs_dir2_block_replace(&args); |
f6c2d1fa | 404 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) |
1da177e4 | 405 | return rval; |
f6c2d1fa | 406 | else if (v) |
1da177e4 LT |
407 | rval = xfs_dir2_leaf_replace(&args); |
408 | else | |
409 | rval = xfs_dir2_node_replace(&args); | |
410 | return rval; | |
411 | } | |
412 | ||
413 | /* | |
414 | * See if this entry can be added to the directory without allocating space. | |
556b8b16 | 415 | * First checks that the caller couldn't reserve enough space (resblks = 0). |
1da177e4 | 416 | */ |
f6c2d1fa NS |
417 | int |
418 | xfs_dir_canenter( | |
419 | xfs_trans_t *tp, | |
420 | xfs_inode_t *dp, | |
556b8b16 BN |
421 | struct xfs_name *name, /* name of entry to add */ |
422 | uint resblks) | |
1da177e4 | 423 | { |
f6c2d1fa NS |
424 | xfs_da_args_t args; |
425 | int rval; | |
1da177e4 LT |
426 | int v; /* type-checking value */ |
427 | ||
556b8b16 BN |
428 | if (resblks) |
429 | return 0; | |
430 | ||
abbede1b | 431 | ASSERT(S_ISDIR(dp->i_d.di_mode)); |
f6c2d1fa | 432 | |
87affd08 | 433 | memset(&args, 0, sizeof(xfs_da_args_t)); |
556b8b16 BN |
434 | args.name = name->name; |
435 | args.namelen = name->len; | |
5163f95a | 436 | args.hashval = dp->i_mount->m_dirnameops->hashname(name); |
1da177e4 | 437 | args.dp = dp; |
1da177e4 LT |
438 | args.whichfork = XFS_DATA_FORK; |
439 | args.trans = tp; | |
6a178100 BN |
440 | args.op_flags = XFS_DA_OP_JUSTCHECK | XFS_DA_OP_ADDNAME | |
441 | XFS_DA_OP_OKNOENT; | |
f6c2d1fa | 442 | |
1da177e4 LT |
443 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
444 | rval = xfs_dir2_sf_addname(&args); | |
f6c2d1fa | 445 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) |
1da177e4 | 446 | return rval; |
f6c2d1fa | 447 | else if (v) |
1da177e4 | 448 | rval = xfs_dir2_block_addname(&args); |
f6c2d1fa | 449 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) |
1da177e4 | 450 | return rval; |
f6c2d1fa | 451 | else if (v) |
1da177e4 LT |
452 | rval = xfs_dir2_leaf_addname(&args); |
453 | else | |
454 | rval = xfs_dir2_node_addname(&args); | |
455 | return rval; | |
456 | } | |
457 | ||
1da177e4 LT |
458 | /* |
459 | * Utility routines. | |
460 | */ | |
461 | ||
462 | /* | |
463 | * Add a block to the directory. | |
77936d02 CH |
464 | * |
465 | * This routine is for data and free blocks, not leaf/node blocks which are | |
466 | * handled by xfs_da_grow_inode. | |
1da177e4 | 467 | */ |
f6c2d1fa | 468 | int |
1da177e4 | 469 | xfs_dir2_grow_inode( |
77936d02 CH |
470 | struct xfs_da_args *args, |
471 | int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */ | |
472 | xfs_dir2_db_t *dbp) /* out: block number added */ | |
1da177e4 | 473 | { |
77936d02 CH |
474 | struct xfs_inode *dp = args->dp; |
475 | struct xfs_mount *mp = dp->i_mount; | |
476 | xfs_fileoff_t bno; /* directory offset of new block */ | |
477 | int count; /* count of filesystem blocks */ | |
478 | int error; | |
1da177e4 | 479 | |
0b1b213f CH |
480 | trace_xfs_dir2_grow_inode(args, space); |
481 | ||
1da177e4 LT |
482 | /* |
483 | * Set lowest possible block in the space requested. | |
484 | */ | |
485 | bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE); | |
486 | count = mp->m_dirblkfsbs; | |
77936d02 CH |
487 | |
488 | error = xfs_da_grow_inode_int(args, &bno, count); | |
489 | if (error) | |
1da177e4 | 490 | return error; |
a7444053 | 491 | |
bbaaf538 | 492 | *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno); |
a7444053 | 493 | |
1da177e4 LT |
494 | /* |
495 | * Update file's size if this is the data space and it grew. | |
496 | */ | |
497 | if (space == XFS_DIR2_DATA_SPACE) { | |
498 | xfs_fsize_t size; /* directory file (data) size */ | |
499 | ||
500 | size = XFS_FSB_TO_B(mp, bno + count); | |
501 | if (size > dp->i_d.di_size) { | |
502 | dp->i_d.di_size = size; | |
77936d02 | 503 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE); |
1da177e4 LT |
504 | } |
505 | } | |
506 | return 0; | |
507 | } | |
508 | ||
509 | /* | |
510 | * See if the directory is a single-block form directory. | |
511 | */ | |
f6c2d1fa | 512 | int |
1da177e4 | 513 | xfs_dir2_isblock( |
f6c2d1fa NS |
514 | xfs_trans_t *tp, |
515 | xfs_inode_t *dp, | |
1da177e4 LT |
516 | int *vp) /* out: 1 is block, 0 is not block */ |
517 | { | |
518 | xfs_fileoff_t last; /* last file offset */ | |
f6c2d1fa NS |
519 | xfs_mount_t *mp; |
520 | int rval; | |
1da177e4 LT |
521 | |
522 | mp = dp->i_mount; | |
f6c2d1fa | 523 | if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) |
1da177e4 | 524 | return rval; |
1da177e4 LT |
525 | rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize; |
526 | ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize); | |
527 | *vp = rval; | |
528 | return 0; | |
529 | } | |
530 | ||
531 | /* | |
532 | * See if the directory is a single-leaf form directory. | |
533 | */ | |
f6c2d1fa | 534 | int |
1da177e4 | 535 | xfs_dir2_isleaf( |
f6c2d1fa NS |
536 | xfs_trans_t *tp, |
537 | xfs_inode_t *dp, | |
1da177e4 LT |
538 | int *vp) /* out: 1 is leaf, 0 is not leaf */ |
539 | { | |
540 | xfs_fileoff_t last; /* last file offset */ | |
f6c2d1fa NS |
541 | xfs_mount_t *mp; |
542 | int rval; | |
1da177e4 LT |
543 | |
544 | mp = dp->i_mount; | |
f6c2d1fa | 545 | if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) |
1da177e4 | 546 | return rval; |
1da177e4 LT |
547 | *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog); |
548 | return 0; | |
549 | } | |
550 | ||
1da177e4 LT |
551 | /* |
552 | * Remove the given block from the directory. | |
553 | * This routine is used for data and free blocks, leaf/node are done | |
554 | * by xfs_da_shrink_inode. | |
555 | */ | |
556 | int | |
557 | xfs_dir2_shrink_inode( | |
f6c2d1fa NS |
558 | xfs_da_args_t *args, |
559 | xfs_dir2_db_t db, | |
1d9025e5 | 560 | struct xfs_buf *bp) |
1da177e4 LT |
561 | { |
562 | xfs_fileoff_t bno; /* directory file offset */ | |
563 | xfs_dablk_t da; /* directory file offset */ | |
564 | int done; /* bunmap is finished */ | |
f6c2d1fa NS |
565 | xfs_inode_t *dp; |
566 | int error; | |
567 | xfs_mount_t *mp; | |
568 | xfs_trans_t *tp; | |
1da177e4 | 569 | |
0b1b213f CH |
570 | trace_xfs_dir2_shrink_inode(args, db); |
571 | ||
1da177e4 LT |
572 | dp = args->dp; |
573 | mp = dp->i_mount; | |
574 | tp = args->trans; | |
bbaaf538 | 575 | da = xfs_dir2_db_to_da(mp, db); |
1da177e4 LT |
576 | /* |
577 | * Unmap the fsblock(s). | |
578 | */ | |
579 | if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs, | |
580 | XFS_BMAPI_METADATA, 0, args->firstblock, args->flist, | |
b4e9181e | 581 | &done))) { |
1da177e4 LT |
582 | /* |
583 | * ENOSPC actually can happen if we're in a removename with | |
584 | * no space reservation, and the resulting block removal | |
585 | * would cause a bmap btree split or conversion from extents | |
586 | * to btree. This can only happen for un-fragmented | |
587 | * directory blocks, since you need to be punching out | |
588 | * the middle of an extent. | |
589 | * In this case we need to leave the block in the file, | |
590 | * and not binval it. | |
591 | * So the block has to be in a consistent empty state | |
592 | * and appropriately logged. | |
593 | * We don't free up the buffer, the caller can tell it | |
594 | * hasn't happened since it got an error back. | |
595 | */ | |
596 | return error; | |
597 | } | |
598 | ASSERT(done); | |
599 | /* | |
600 | * Invalidate the buffer from the transaction. | |
601 | */ | |
1d9025e5 | 602 | xfs_trans_binval(tp, bp); |
1da177e4 LT |
603 | /* |
604 | * If it's not a data block, we're done. | |
605 | */ | |
606 | if (db >= XFS_DIR2_LEAF_FIRSTDB(mp)) | |
607 | return 0; | |
608 | /* | |
609 | * If the block isn't the last one in the directory, we're done. | |
610 | */ | |
bbaaf538 | 611 | if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0)) |
1da177e4 LT |
612 | return 0; |
613 | bno = da; | |
614 | if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) { | |
615 | /* | |
616 | * This can't really happen unless there's kernel corruption. | |
617 | */ | |
618 | return error; | |
619 | } | |
620 | if (db == mp->m_dirdatablk) | |
621 | ASSERT(bno == 0); | |
622 | else | |
623 | ASSERT(bno > 0); | |
624 | /* | |
625 | * Set the size to the new last block. | |
626 | */ | |
627 | dp->i_d.di_size = XFS_FSB_TO_B(mp, bno); | |
628 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
629 | return 0; | |
630 | } |