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" |
a844f451 | 21 | #include "xfs_bit.h" |
1da177e4 | 22 | #include "xfs_log.h" |
a844f451 | 23 | #include "xfs_inum.h" |
1da177e4 LT |
24 | #include "xfs_trans.h" |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_ag.h" | |
1da177e4 LT |
27 | #include "xfs_dir2.h" |
28 | #include "xfs_dmapi.h" | |
29 | #include "xfs_mount.h" | |
a844f451 | 30 | #include "xfs_da_btree.h" |
1da177e4 | 31 | #include "xfs_bmap_btree.h" |
a844f451 | 32 | #include "xfs_alloc_btree.h" |
1da177e4 | 33 | #include "xfs_dir2_sf.h" |
a844f451 | 34 | #include "xfs_attr_sf.h" |
1da177e4 | 35 | #include "xfs_dinode.h" |
1da177e4 | 36 | #include "xfs_inode.h" |
a844f451 | 37 | #include "xfs_inode_item.h" |
1da177e4 | 38 | #include "xfs_bmap.h" |
1da177e4 LT |
39 | #include "xfs_dir2_data.h" |
40 | #include "xfs_dir2_leaf.h" | |
41 | #include "xfs_dir2_block.h" | |
42 | #include "xfs_dir2_node.h" | |
43 | #include "xfs_dir2_trace.h" | |
44 | #include "xfs_error.h" | |
1da177e4 | 45 | |
1da177e4 | 46 | |
f6c2d1fa NS |
47 | void |
48 | xfs_dir_mount( | |
49 | xfs_mount_t *mp) | |
1da177e4 | 50 | { |
f6c2d1fa | 51 | ASSERT(XFS_SB_VERSION_HASDIRV2(&mp->m_sb)); |
1da177e4 LT |
52 | ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <= |
53 | XFS_MAX_BLOCKSIZE); | |
54 | mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog); | |
55 | mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog; | |
bbaaf538 CH |
56 | mp->m_dirdatablk = xfs_dir2_db_to_da(mp, XFS_DIR2_DATA_FIRSTDB(mp)); |
57 | mp->m_dirleafblk = xfs_dir2_db_to_da(mp, XFS_DIR2_LEAF_FIRSTDB(mp)); | |
58 | mp->m_dirfreeblk = xfs_dir2_db_to_da(mp, XFS_DIR2_FREE_FIRSTDB(mp)); | |
1da177e4 LT |
59 | mp->m_attr_node_ents = |
60 | (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) / | |
61 | (uint)sizeof(xfs_da_node_entry_t); | |
62 | mp->m_dir_node_ents = | |
63 | (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) / | |
64 | (uint)sizeof(xfs_da_node_entry_t); | |
65 | mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100; | |
66 | } | |
67 | ||
68 | /* | |
69 | * Return 1 if directory contains only "." and "..". | |
70 | */ | |
f6c2d1fa NS |
71 | int |
72 | xfs_dir_isempty( | |
73 | xfs_inode_t *dp) | |
1da177e4 | 74 | { |
f6c2d1fa | 75 | xfs_dir2_sf_t *sfp; |
1da177e4 LT |
76 | |
77 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | |
f6c2d1fa | 78 | if (dp->i_d.di_size == 0) /* might happen during shutdown. */ |
1da177e4 | 79 | return 1; |
1da177e4 LT |
80 | if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp)) |
81 | return 0; | |
82 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | |
83 | return !sfp->hdr.count; | |
84 | } | |
85 | ||
f6c2d1fa NS |
86 | /* |
87 | * Validate a given inode number. | |
88 | */ | |
89 | int | |
90 | xfs_dir_ino_validate( | |
91 | xfs_mount_t *mp, | |
92 | xfs_ino_t ino) | |
93 | { | |
94 | xfs_agblock_t agblkno; | |
95 | xfs_agino_t agino; | |
96 | xfs_agnumber_t agno; | |
97 | int ino_ok; | |
98 | int ioff; | |
99 | ||
100 | agno = XFS_INO_TO_AGNO(mp, ino); | |
101 | agblkno = XFS_INO_TO_AGBNO(mp, ino); | |
102 | ioff = XFS_INO_TO_OFFSET(mp, ino); | |
103 | agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff); | |
104 | ino_ok = | |
105 | agno < mp->m_sb.sb_agcount && | |
106 | agblkno < mp->m_sb.sb_agblocks && | |
107 | agblkno != 0 && | |
108 | ioff < (1 << mp->m_sb.sb_inopblog) && | |
109 | XFS_AGINO_TO_INO(mp, agno, agino) == ino; | |
110 | if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE, | |
111 | XFS_RANDOM_DIR_INO_VALIDATE))) { | |
112 | xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx", | |
113 | (unsigned long long) ino); | |
114 | XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp); | |
115 | return XFS_ERROR(EFSCORRUPTED); | |
116 | } | |
117 | return 0; | |
118 | } | |
119 | ||
1da177e4 LT |
120 | /* |
121 | * Initialize a directory with its "." and ".." entries. | |
122 | */ | |
f6c2d1fa NS |
123 | int |
124 | xfs_dir_init( | |
125 | xfs_trans_t *tp, | |
126 | xfs_inode_t *dp, | |
127 | xfs_inode_t *pdp) | |
1da177e4 | 128 | { |
f6c2d1fa NS |
129 | xfs_da_args_t args; |
130 | int error; | |
1da177e4 LT |
131 | |
132 | memset((char *)&args, 0, sizeof(args)); | |
133 | args.dp = dp; | |
134 | args.trans = tp; | |
135 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | |
f6c2d1fa | 136 | if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino))) |
1da177e4 | 137 | return error; |
1da177e4 LT |
138 | return xfs_dir2_sf_create(&args, pdp->i_ino); |
139 | } | |
140 | ||
141 | /* | |
142 | Enter a name in a directory. | |
143 | */ | |
f6c2d1fa NS |
144 | int |
145 | xfs_dir_createname( | |
146 | xfs_trans_t *tp, | |
147 | xfs_inode_t *dp, | |
148 | char *name, | |
149 | int namelen, | |
1da177e4 LT |
150 | xfs_ino_t inum, /* new entry inode number */ |
151 | xfs_fsblock_t *first, /* bmap's firstblock */ | |
152 | xfs_bmap_free_t *flist, /* bmap's freeblock list */ | |
153 | xfs_extlen_t total) /* bmap's total block count */ | |
154 | { | |
f6c2d1fa NS |
155 | xfs_da_args_t args; |
156 | int rval; | |
1da177e4 LT |
157 | int v; /* type-checking value */ |
158 | ||
159 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | |
f6c2d1fa | 160 | if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) |
1da177e4 | 161 | return rval; |
1da177e4 | 162 | XFS_STATS_INC(xs_dir_create); |
f6c2d1fa | 163 | |
1da177e4 LT |
164 | args.name = name; |
165 | args.namelen = namelen; | |
166 | args.hashval = xfs_da_hashname(name, namelen); | |
167 | args.inumber = inum; | |
168 | args.dp = dp; | |
169 | args.firstblock = first; | |
170 | args.flist = flist; | |
171 | args.total = total; | |
172 | args.whichfork = XFS_DATA_FORK; | |
173 | args.trans = tp; | |
174 | args.justcheck = 0; | |
175 | args.addname = args.oknoent = 1; | |
f6c2d1fa | 176 | |
1da177e4 LT |
177 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
178 | rval = xfs_dir2_sf_addname(&args); | |
f6c2d1fa | 179 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) |
1da177e4 | 180 | return rval; |
f6c2d1fa | 181 | else if (v) |
1da177e4 | 182 | rval = xfs_dir2_block_addname(&args); |
f6c2d1fa | 183 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) |
1da177e4 | 184 | return rval; |
f6c2d1fa | 185 | else if (v) |
1da177e4 LT |
186 | rval = xfs_dir2_leaf_addname(&args); |
187 | else | |
188 | rval = xfs_dir2_node_addname(&args); | |
189 | return rval; | |
190 | } | |
191 | ||
192 | /* | |
193 | * Lookup a name in a directory, give back the inode number. | |
194 | */ | |
f6c2d1fa NS |
195 | int |
196 | xfs_dir_lookup( | |
197 | xfs_trans_t *tp, | |
198 | xfs_inode_t *dp, | |
199 | char *name, | |
200 | int namelen, | |
1da177e4 LT |
201 | xfs_ino_t *inum) /* out: inode number */ |
202 | { | |
f6c2d1fa NS |
203 | xfs_da_args_t args; |
204 | int rval; | |
1da177e4 LT |
205 | int v; /* type-checking value */ |
206 | ||
207 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | |
208 | XFS_STATS_INC(xs_dir_lookup); | |
209 | ||
1da177e4 LT |
210 | args.name = name; |
211 | args.namelen = namelen; | |
212 | args.hashval = xfs_da_hashname(name, namelen); | |
213 | args.inumber = 0; | |
214 | args.dp = dp; | |
215 | args.firstblock = NULL; | |
216 | args.flist = NULL; | |
217 | args.total = 0; | |
218 | args.whichfork = XFS_DATA_FORK; | |
219 | args.trans = tp; | |
220 | args.justcheck = args.addname = 0; | |
221 | args.oknoent = 1; | |
f6c2d1fa | 222 | |
1da177e4 LT |
223 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
224 | rval = xfs_dir2_sf_lookup(&args); | |
f6c2d1fa | 225 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) |
1da177e4 | 226 | return rval; |
f6c2d1fa | 227 | else if (v) |
1da177e4 | 228 | rval = xfs_dir2_block_lookup(&args); |
f6c2d1fa | 229 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) |
1da177e4 | 230 | return rval; |
f6c2d1fa | 231 | else if (v) |
1da177e4 LT |
232 | rval = xfs_dir2_leaf_lookup(&args); |
233 | else | |
234 | rval = xfs_dir2_node_lookup(&args); | |
235 | if (rval == EEXIST) | |
236 | rval = 0; | |
237 | if (rval == 0) | |
238 | *inum = args.inumber; | |
239 | return rval; | |
240 | } | |
241 | ||
242 | /* | |
243 | * Remove an entry from a directory. | |
244 | */ | |
f6c2d1fa NS |
245 | int |
246 | xfs_dir_removename( | |
247 | xfs_trans_t *tp, | |
248 | xfs_inode_t *dp, | |
249 | char *name, | |
250 | int namelen, | |
251 | xfs_ino_t ino, | |
1da177e4 LT |
252 | xfs_fsblock_t *first, /* bmap's firstblock */ |
253 | xfs_bmap_free_t *flist, /* bmap's freeblock list */ | |
254 | xfs_extlen_t total) /* bmap's total block count */ | |
255 | { | |
f6c2d1fa NS |
256 | xfs_da_args_t args; |
257 | int rval; | |
1da177e4 LT |
258 | int v; /* type-checking value */ |
259 | ||
260 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | |
261 | XFS_STATS_INC(xs_dir_remove); | |
f6c2d1fa | 262 | |
1da177e4 LT |
263 | args.name = name; |
264 | args.namelen = namelen; | |
265 | args.hashval = xfs_da_hashname(name, namelen); | |
266 | args.inumber = ino; | |
267 | args.dp = dp; | |
268 | args.firstblock = first; | |
269 | args.flist = flist; | |
270 | args.total = total; | |
271 | args.whichfork = XFS_DATA_FORK; | |
272 | args.trans = tp; | |
273 | args.justcheck = args.addname = args.oknoent = 0; | |
f6c2d1fa | 274 | |
1da177e4 LT |
275 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
276 | rval = xfs_dir2_sf_removename(&args); | |
f6c2d1fa | 277 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) |
1da177e4 | 278 | return rval; |
f6c2d1fa | 279 | else if (v) |
1da177e4 | 280 | rval = xfs_dir2_block_removename(&args); |
f6c2d1fa | 281 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) |
1da177e4 | 282 | return rval; |
f6c2d1fa | 283 | else if (v) |
1da177e4 LT |
284 | rval = xfs_dir2_leaf_removename(&args); |
285 | else | |
286 | rval = xfs_dir2_node_removename(&args); | |
287 | return rval; | |
288 | } | |
289 | ||
290 | /* | |
291 | * Read a directory. | |
292 | */ | |
f6c2d1fa | 293 | int |
051e7cd4 CH |
294 | xfs_readdir( |
295 | bhv_desc_t *dir_bdp, | |
296 | void *dirent, | |
297 | size_t bufsize, | |
298 | xfs_off_t *offset, | |
299 | filldir_t filldir) | |
1da177e4 | 300 | { |
051e7cd4 | 301 | xfs_inode_t *dp = XFS_BHVTOI(dir_bdp); |
1da177e4 LT |
302 | int rval; /* return value */ |
303 | int v; /* type-checking value */ | |
304 | ||
051e7cd4 CH |
305 | vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__, |
306 | (inst_t *)__return_address); | |
307 | ||
308 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) | |
309 | return XFS_ERROR(EIO); | |
310 | ||
1da177e4 LT |
311 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); |
312 | XFS_STATS_INC(xs_dir_getdents); | |
1da177e4 | 313 | |
1da177e4 | 314 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
051e7cd4 CH |
315 | rval = xfs_dir2_sf_getdents(dp, dirent, offset, filldir); |
316 | else if ((rval = xfs_dir2_isblock(NULL, dp, &v))) | |
1da177e4 | 317 | ; |
f6c2d1fa | 318 | else if (v) |
051e7cd4 | 319 | rval = xfs_dir2_block_getdents(dp, dirent, offset, filldir); |
1da177e4 | 320 | else |
051e7cd4 CH |
321 | rval = xfs_dir2_leaf_getdents(dp, dirent, bufsize, offset, |
322 | filldir); | |
1da177e4 LT |
323 | return rval; |
324 | } | |
325 | ||
326 | /* | |
327 | * Replace the inode number of a directory entry. | |
328 | */ | |
f6c2d1fa NS |
329 | int |
330 | xfs_dir_replace( | |
331 | xfs_trans_t *tp, | |
332 | xfs_inode_t *dp, | |
1da177e4 | 333 | char *name, /* name of entry to replace */ |
f6c2d1fa | 334 | int namelen, |
1da177e4 LT |
335 | xfs_ino_t inum, /* new inode number */ |
336 | xfs_fsblock_t *first, /* bmap's firstblock */ | |
337 | xfs_bmap_free_t *flist, /* bmap's freeblock list */ | |
338 | xfs_extlen_t total) /* bmap's total block count */ | |
339 | { | |
f6c2d1fa NS |
340 | xfs_da_args_t args; |
341 | int rval; | |
1da177e4 LT |
342 | int v; /* type-checking value */ |
343 | ||
344 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | |
345 | ||
f6c2d1fa | 346 | if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) |
1da177e4 | 347 | return rval; |
f6c2d1fa | 348 | |
1da177e4 LT |
349 | args.name = name; |
350 | args.namelen = namelen; | |
351 | args.hashval = xfs_da_hashname(name, namelen); | |
352 | args.inumber = inum; | |
353 | args.dp = dp; | |
354 | args.firstblock = first; | |
355 | args.flist = flist; | |
356 | args.total = total; | |
357 | args.whichfork = XFS_DATA_FORK; | |
358 | args.trans = tp; | |
359 | args.justcheck = args.addname = args.oknoent = 0; | |
f6c2d1fa | 360 | |
1da177e4 LT |
361 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
362 | rval = xfs_dir2_sf_replace(&args); | |
f6c2d1fa | 363 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) |
1da177e4 | 364 | return rval; |
f6c2d1fa | 365 | else if (v) |
1da177e4 | 366 | rval = xfs_dir2_block_replace(&args); |
f6c2d1fa | 367 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) |
1da177e4 | 368 | return rval; |
f6c2d1fa | 369 | else if (v) |
1da177e4 LT |
370 | rval = xfs_dir2_leaf_replace(&args); |
371 | else | |
372 | rval = xfs_dir2_node_replace(&args); | |
373 | return rval; | |
374 | } | |
375 | ||
376 | /* | |
377 | * See if this entry can be added to the directory without allocating space. | |
378 | */ | |
f6c2d1fa NS |
379 | int |
380 | xfs_dir_canenter( | |
381 | xfs_trans_t *tp, | |
382 | xfs_inode_t *dp, | |
1da177e4 | 383 | char *name, /* name of entry to add */ |
f6c2d1fa | 384 | int namelen) |
1da177e4 | 385 | { |
f6c2d1fa NS |
386 | xfs_da_args_t args; |
387 | int rval; | |
1da177e4 LT |
388 | int v; /* type-checking value */ |
389 | ||
390 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | |
f6c2d1fa | 391 | |
1da177e4 LT |
392 | args.name = name; |
393 | args.namelen = namelen; | |
394 | args.hashval = xfs_da_hashname(name, namelen); | |
395 | args.inumber = 0; | |
396 | args.dp = dp; | |
397 | args.firstblock = NULL; | |
398 | args.flist = NULL; | |
399 | args.total = 0; | |
400 | args.whichfork = XFS_DATA_FORK; | |
401 | args.trans = tp; | |
402 | args.justcheck = args.addname = args.oknoent = 1; | |
f6c2d1fa | 403 | |
1da177e4 LT |
404 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
405 | rval = xfs_dir2_sf_addname(&args); | |
f6c2d1fa | 406 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) |
1da177e4 | 407 | return rval; |
f6c2d1fa | 408 | else if (v) |
1da177e4 | 409 | rval = xfs_dir2_block_addname(&args); |
f6c2d1fa | 410 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) |
1da177e4 | 411 | return rval; |
f6c2d1fa | 412 | else if (v) |
1da177e4 LT |
413 | rval = xfs_dir2_leaf_addname(&args); |
414 | else | |
415 | rval = xfs_dir2_node_addname(&args); | |
416 | return rval; | |
417 | } | |
418 | ||
1da177e4 LT |
419 | /* |
420 | * Utility routines. | |
421 | */ | |
422 | ||
423 | /* | |
424 | * Add a block to the directory. | |
425 | * This routine is for data and free blocks, not leaf/node blocks | |
426 | * which are handled by xfs_da_grow_inode. | |
427 | */ | |
f6c2d1fa | 428 | int |
1da177e4 | 429 | xfs_dir2_grow_inode( |
f6c2d1fa | 430 | xfs_da_args_t *args, |
1da177e4 LT |
431 | int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */ |
432 | xfs_dir2_db_t *dbp) /* out: block number added */ | |
433 | { | |
434 | xfs_fileoff_t bno; /* directory offset of new block */ | |
435 | int count; /* count of filesystem blocks */ | |
436 | xfs_inode_t *dp; /* incore directory inode */ | |
f6c2d1fa | 437 | int error; |
1da177e4 | 438 | int got; /* blocks actually mapped */ |
f6c2d1fa | 439 | int i; |
1da177e4 LT |
440 | xfs_bmbt_irec_t map; /* single structure for bmap */ |
441 | int mapi; /* mapping index */ | |
442 | xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */ | |
f6c2d1fa | 443 | xfs_mount_t *mp; |
1da177e4 | 444 | int nmap; /* number of bmap entries */ |
f6c2d1fa | 445 | xfs_trans_t *tp; |
1da177e4 LT |
446 | |
447 | xfs_dir2_trace_args_s("grow_inode", args, space); | |
448 | dp = args->dp; | |
449 | tp = args->trans; | |
450 | mp = dp->i_mount; | |
451 | /* | |
452 | * Set lowest possible block in the space requested. | |
453 | */ | |
454 | bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE); | |
455 | count = mp->m_dirblkfsbs; | |
456 | /* | |
457 | * Find the first hole for our block. | |
458 | */ | |
f6c2d1fa | 459 | if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK))) |
1da177e4 | 460 | return error; |
1da177e4 LT |
461 | nmap = 1; |
462 | ASSERT(args->firstblock != NULL); | |
463 | /* | |
464 | * Try mapping the new block contiguously (one extent). | |
465 | */ | |
466 | if ((error = xfs_bmapi(tp, dp, bno, count, | |
467 | XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG, | |
468 | args->firstblock, args->total, &map, &nmap, | |
f6c2d1fa | 469 | args->flist, NULL))) |
1da177e4 | 470 | return error; |
1da177e4 | 471 | ASSERT(nmap <= 1); |
1da177e4 LT |
472 | if (nmap == 1) { |
473 | mapp = ↦ | |
474 | mapi = 1; | |
475 | } | |
476 | /* | |
477 | * Didn't work and this is a multiple-fsb directory block. | |
478 | * Try again with contiguous flag turned on. | |
479 | */ | |
480 | else if (nmap == 0 && count > 1) { | |
481 | xfs_fileoff_t b; /* current file offset */ | |
482 | ||
483 | /* | |
484 | * Space for maximum number of mappings. | |
485 | */ | |
486 | mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP); | |
487 | /* | |
488 | * Iterate until we get to the end of our block. | |
489 | */ | |
490 | for (b = bno, mapi = 0; b < bno + count; ) { | |
491 | int c; /* current fsb count */ | |
492 | ||
493 | /* | |
494 | * Can't map more than MAX_NMAP at once. | |
495 | */ | |
496 | nmap = MIN(XFS_BMAP_MAX_NMAP, count); | |
497 | c = (int)(bno + count - b); | |
498 | if ((error = xfs_bmapi(tp, dp, b, c, | |
499 | XFS_BMAPI_WRITE|XFS_BMAPI_METADATA, | |
500 | args->firstblock, args->total, | |
3e57ecf6 OW |
501 | &mapp[mapi], &nmap, args->flist, |
502 | NULL))) { | |
1da177e4 LT |
503 | kmem_free(mapp, sizeof(*mapp) * count); |
504 | return error; | |
505 | } | |
506 | if (nmap < 1) | |
507 | break; | |
508 | /* | |
509 | * Add this bunch into our table, go to the next offset. | |
510 | */ | |
511 | mapi += nmap; | |
512 | b = mapp[mapi - 1].br_startoff + | |
513 | mapp[mapi - 1].br_blockcount; | |
514 | } | |
515 | } | |
516 | /* | |
517 | * Didn't work. | |
518 | */ | |
519 | else { | |
520 | mapi = 0; | |
521 | mapp = NULL; | |
522 | } | |
523 | /* | |
524 | * See how many fsb's we got. | |
525 | */ | |
526 | for (i = 0, got = 0; i < mapi; i++) | |
527 | got += mapp[i].br_blockcount; | |
528 | /* | |
529 | * Didn't get enough fsb's, or the first/last block's are wrong. | |
530 | */ | |
531 | if (got != count || mapp[0].br_startoff != bno || | |
532 | mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount != | |
533 | bno + count) { | |
534 | if (mapp != &map) | |
535 | kmem_free(mapp, sizeof(*mapp) * count); | |
536 | return XFS_ERROR(ENOSPC); | |
537 | } | |
538 | /* | |
539 | * Done with the temporary mapping table. | |
540 | */ | |
541 | if (mapp != &map) | |
542 | kmem_free(mapp, sizeof(*mapp) * count); | |
bbaaf538 | 543 | *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno); |
1da177e4 LT |
544 | /* |
545 | * Update file's size if this is the data space and it grew. | |
546 | */ | |
547 | if (space == XFS_DIR2_DATA_SPACE) { | |
548 | xfs_fsize_t size; /* directory file (data) size */ | |
549 | ||
550 | size = XFS_FSB_TO_B(mp, bno + count); | |
551 | if (size > dp->i_d.di_size) { | |
552 | dp->i_d.di_size = size; | |
553 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
554 | } | |
555 | } | |
556 | return 0; | |
557 | } | |
558 | ||
559 | /* | |
560 | * See if the directory is a single-block form directory. | |
561 | */ | |
f6c2d1fa | 562 | int |
1da177e4 | 563 | xfs_dir2_isblock( |
f6c2d1fa NS |
564 | xfs_trans_t *tp, |
565 | xfs_inode_t *dp, | |
1da177e4 LT |
566 | int *vp) /* out: 1 is block, 0 is not block */ |
567 | { | |
568 | xfs_fileoff_t last; /* last file offset */ | |
f6c2d1fa NS |
569 | xfs_mount_t *mp; |
570 | int rval; | |
1da177e4 LT |
571 | |
572 | mp = dp->i_mount; | |
f6c2d1fa | 573 | if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) |
1da177e4 | 574 | return rval; |
1da177e4 LT |
575 | rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize; |
576 | ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize); | |
577 | *vp = rval; | |
578 | return 0; | |
579 | } | |
580 | ||
581 | /* | |
582 | * See if the directory is a single-leaf form directory. | |
583 | */ | |
f6c2d1fa | 584 | int |
1da177e4 | 585 | xfs_dir2_isleaf( |
f6c2d1fa NS |
586 | xfs_trans_t *tp, |
587 | xfs_inode_t *dp, | |
1da177e4 LT |
588 | int *vp) /* out: 1 is leaf, 0 is not leaf */ |
589 | { | |
590 | xfs_fileoff_t last; /* last file offset */ | |
f6c2d1fa NS |
591 | xfs_mount_t *mp; |
592 | int rval; | |
1da177e4 LT |
593 | |
594 | mp = dp->i_mount; | |
f6c2d1fa | 595 | if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) |
1da177e4 | 596 | return rval; |
1da177e4 LT |
597 | *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog); |
598 | return 0; | |
599 | } | |
600 | ||
1da177e4 LT |
601 | /* |
602 | * Remove the given block from the directory. | |
603 | * This routine is used for data and free blocks, leaf/node are done | |
604 | * by xfs_da_shrink_inode. | |
605 | */ | |
606 | int | |
607 | xfs_dir2_shrink_inode( | |
f6c2d1fa NS |
608 | xfs_da_args_t *args, |
609 | xfs_dir2_db_t db, | |
610 | xfs_dabuf_t *bp) | |
1da177e4 LT |
611 | { |
612 | xfs_fileoff_t bno; /* directory file offset */ | |
613 | xfs_dablk_t da; /* directory file offset */ | |
614 | int done; /* bunmap is finished */ | |
f6c2d1fa NS |
615 | xfs_inode_t *dp; |
616 | int error; | |
617 | xfs_mount_t *mp; | |
618 | xfs_trans_t *tp; | |
1da177e4 LT |
619 | |
620 | xfs_dir2_trace_args_db("shrink_inode", args, db, bp); | |
621 | dp = args->dp; | |
622 | mp = dp->i_mount; | |
623 | tp = args->trans; | |
bbaaf538 | 624 | da = xfs_dir2_db_to_da(mp, db); |
1da177e4 LT |
625 | /* |
626 | * Unmap the fsblock(s). | |
627 | */ | |
628 | if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs, | |
629 | XFS_BMAPI_METADATA, 0, args->firstblock, args->flist, | |
3e57ecf6 | 630 | NULL, &done))) { |
1da177e4 LT |
631 | /* |
632 | * ENOSPC actually can happen if we're in a removename with | |
633 | * no space reservation, and the resulting block removal | |
634 | * would cause a bmap btree split or conversion from extents | |
635 | * to btree. This can only happen for un-fragmented | |
636 | * directory blocks, since you need to be punching out | |
637 | * the middle of an extent. | |
638 | * In this case we need to leave the block in the file, | |
639 | * and not binval it. | |
640 | * So the block has to be in a consistent empty state | |
641 | * and appropriately logged. | |
642 | * We don't free up the buffer, the caller can tell it | |
643 | * hasn't happened since it got an error back. | |
644 | */ | |
645 | return error; | |
646 | } | |
647 | ASSERT(done); | |
648 | /* | |
649 | * Invalidate the buffer from the transaction. | |
650 | */ | |
651 | xfs_da_binval(tp, bp); | |
652 | /* | |
653 | * If it's not a data block, we're done. | |
654 | */ | |
655 | if (db >= XFS_DIR2_LEAF_FIRSTDB(mp)) | |
656 | return 0; | |
657 | /* | |
658 | * If the block isn't the last one in the directory, we're done. | |
659 | */ | |
bbaaf538 | 660 | if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0)) |
1da177e4 LT |
661 | return 0; |
662 | bno = da; | |
663 | if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) { | |
664 | /* | |
665 | * This can't really happen unless there's kernel corruption. | |
666 | */ | |
667 | return error; | |
668 | } | |
669 | if (db == mp->m_dirdatablk) | |
670 | ASSERT(bno == 0); | |
671 | else | |
672 | ASSERT(bno > 0); | |
673 | /* | |
674 | * Set the size to the new last block. | |
675 | */ | |
676 | dp->i_d.di_size = XFS_FSB_TO_B(mp, bno); | |
677 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
678 | return 0; | |
679 | } |