]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_dir2.c
Update copyright/license notices to match SGI legal prefered boilerplate.
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2.c
CommitLineData
2bd0ea18 1/*
da23017d
NS
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5000d01d 4 *
da23017d
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
2bd0ea18 7 * published by the Free Software Foundation.
5000d01d 8 *
da23017d
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.
5000d01d 13 *
da23017d
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
2bd0ea18
NS
17 */
18
19/*
20 * XFS v2 directory implmentation.
21 * Top-level and utility routines.
22 */
23
24#include <xfs.h>
25
26
27/*
28 * Initialize directory-related fields in the mount structure.
29 */
30void
31xfs_dir2_mount(
32 xfs_mount_t *mp) /* filesystem mount point */
33{
34 mp->m_dirversion = 2;
35 ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
36 XFS_MAX_BLOCKSIZE);
37 mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog);
38 mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog;
39 mp->m_dirdatablk = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_DATA_FIRSTDB(mp));
40 mp->m_dirleafblk = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_LEAF_FIRSTDB(mp));
41 mp->m_dirfreeblk = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_FREE_FIRSTDB(mp));
6bef826c
NS
42 mp->m_attr_node_ents =
43 (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) /
44 (uint)sizeof(xfs_da_node_entry_t);
45 mp->m_dir_node_ents =
2bd0ea18
NS
46 (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) /
47 (uint)sizeof(xfs_da_node_entry_t);
48 mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100;
49}
50
51/*
52 * Initialize a directory with its "." and ".." entries.
53 */
54int /* error */
55xfs_dir2_init(
56 xfs_trans_t *tp, /* transaction pointer */
57 xfs_inode_t *dp, /* incore directory inode */
58 xfs_inode_t *pdp) /* incore parent directory inode */
59{
60 xfs_da_args_t args; /* operation arguments */
61 int error; /* error return value */
62
32181a02 63 memset((char *)&args, 0, sizeof(args));
2bd0ea18
NS
64 args.dp = dp;
65 args.trans = tp;
322f2a29 66 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
0e266570 67 if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino))) {
2bd0ea18
NS
68 return error;
69 }
70 return xfs_dir2_sf_create(&args, pdp->i_ino);
71}
72
73/*
74 Enter a name in a directory.
75 */
76STATIC int /* error */
77xfs_dir2_createname(
78 xfs_trans_t *tp, /* transaction pointer */
79 xfs_inode_t *dp, /* incore directory inode */
80 char *name, /* new entry name */
81 int namelen, /* new entry name length */
82 xfs_ino_t inum, /* new entry inode number */
83 xfs_fsblock_t *first, /* bmap's firstblock */
84 xfs_bmap_free_t *flist, /* bmap's freeblock list */
85 xfs_extlen_t total) /* bmap's total block count */
86{
87 xfs_da_args_t args; /* operation arguments */
88 int rval; /* return value */
89 int v; /* type-checking value */
90
322f2a29 91 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
0e266570 92 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) {
2bd0ea18
NS
93 return rval;
94 }
32a82561 95 XFS_STATS_INC(xs_dir_create);
2bd0ea18
NS
96 /*
97 * Fill in the arg structure for this request.
98 */
99 args.name = name;
100 args.namelen = namelen;
101 args.hashval = xfs_da_hashname(name, namelen);
102 args.inumber = inum;
103 args.dp = dp;
104 args.firstblock = first;
105 args.flist = flist;
106 args.total = total;
107 args.whichfork = XFS_DATA_FORK;
108 args.trans = tp;
109 args.justcheck = 0;
110 args.addname = args.oknoent = 1;
111 /*
112 * Decide on what work routines to call based on the inode size.
113 */
114 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
115 rval = xfs_dir2_sf_addname(&args);
0e266570 116 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
2bd0ea18
NS
117 return rval;
118 } else if (v)
119 rval = xfs_dir2_block_addname(&args);
0e266570 120 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
2bd0ea18
NS
121 return rval;
122 } else if (v)
123 rval = xfs_dir2_leaf_addname(&args);
124 else
125 rval = xfs_dir2_node_addname(&args);
126 return rval;
127}
128
129/*
130 * Lookup a name in a directory, give back the inode number.
131 */
132STATIC int /* error */
133xfs_dir2_lookup(
134 xfs_trans_t *tp, /* transaction pointer */
135 xfs_inode_t *dp, /* incore directory inode */
136 char *name, /* lookup name */
137 int namelen, /* lookup name length */
138 xfs_ino_t *inum) /* out: inode number */
139{
140 xfs_da_args_t args; /* operation arguments */
141 int rval; /* return value */
142 int v; /* type-checking value */
143
322f2a29 144 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
32a82561 145 XFS_STATS_INC(xs_dir_lookup);
a95cf252 146
2bd0ea18
NS
147 /*
148 * Fill in the arg structure for this request.
149 */
150 args.name = name;
151 args.namelen = namelen;
152 args.hashval = xfs_da_hashname(name, namelen);
153 args.inumber = 0;
154 args.dp = dp;
155 args.firstblock = NULL;
156 args.flist = NULL;
157 args.total = 0;
158 args.whichfork = XFS_DATA_FORK;
159 args.trans = tp;
160 args.justcheck = args.addname = 0;
161 args.oknoent = 1;
162 /*
163 * Decide on what work routines to call based on the inode size.
164 */
165 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
166 rval = xfs_dir2_sf_lookup(&args);
0e266570 167 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
2bd0ea18
NS
168 return rval;
169 } else if (v)
170 rval = xfs_dir2_block_lookup(&args);
0e266570 171 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
2bd0ea18
NS
172 return rval;
173 } else if (v)
174 rval = xfs_dir2_leaf_lookup(&args);
175 else
176 rval = xfs_dir2_node_lookup(&args);
177 if (rval == EEXIST)
178 rval = 0;
179 if (rval == 0)
180 *inum = args.inumber;
181 return rval;
182}
183
184/*
185 * Remove an entry from a directory.
186 */
187STATIC int /* error */
188xfs_dir2_removename(
189 xfs_trans_t *tp, /* transaction pointer */
190 xfs_inode_t *dp, /* incore directory inode */
191 char *name, /* name of entry to remove */
192 int namelen, /* name length of entry to remove */
193 xfs_ino_t ino, /* inode number of entry to remove */
194 xfs_fsblock_t *first, /* bmap's firstblock */
dfc130f3 195 xfs_bmap_free_t *flist, /* bmap's freeblock list */
2bd0ea18
NS
196 xfs_extlen_t total) /* bmap's total block count */
197{
198 xfs_da_args_t args; /* operation arguments */
199 int rval; /* return value */
200 int v; /* type-checking value */
201
322f2a29 202 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
32a82561 203 XFS_STATS_INC(xs_dir_remove);
2bd0ea18
NS
204 /*
205 * Fill in the arg structure for this request.
206 */
207 args.name = name;
208 args.namelen = namelen;
209 args.hashval = xfs_da_hashname(name, namelen);
210 args.inumber = ino;
211 args.dp = dp;
212 args.firstblock = first;
213 args.flist = flist;
214 args.total = total;
215 args.whichfork = XFS_DATA_FORK;
216 args.trans = tp;
217 args.justcheck = args.addname = args.oknoent = 0;
218 /*
219 * Decide on what work routines to call based on the inode size.
220 */
221 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
222 rval = xfs_dir2_sf_removename(&args);
0e266570 223 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
2bd0ea18
NS
224 return rval;
225 } else if (v)
226 rval = xfs_dir2_block_removename(&args);
0e266570 227 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
2bd0ea18
NS
228 return rval;
229 } else if (v)
230 rval = xfs_dir2_leaf_removename(&args);
231 else
232 rval = xfs_dir2_node_removename(&args);
233 return rval;
234}
235
236/*
237 * Replace the inode number of a directory entry.
238 */
239STATIC int /* error */
240xfs_dir2_replace(
241 xfs_trans_t *tp, /* transaction pointer */
242 xfs_inode_t *dp, /* incore directory inode */
243 char *name, /* name of entry to replace */
244 int namelen, /* name length of entry to replace */
245 xfs_ino_t inum, /* new inode number */
246 xfs_fsblock_t *first, /* bmap's firstblock */
dfc130f3 247 xfs_bmap_free_t *flist, /* bmap's freeblock list */
2bd0ea18
NS
248 xfs_extlen_t total) /* bmap's total block count */
249{
250 xfs_da_args_t args; /* operation arguments */
251 int rval; /* return value */
252 int v; /* type-checking value */
253
322f2a29 254 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
a95cf252 255
0e266570 256 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) {
2bd0ea18
NS
257 return rval;
258 }
259 /*
260 * Fill in the arg structure for this request.
261 */
262 args.name = name;
263 args.namelen = namelen;
264 args.hashval = xfs_da_hashname(name, namelen);
265 args.inumber = inum;
266 args.dp = dp;
267 args.firstblock = first;
268 args.flist = flist;
269 args.total = total;
270 args.whichfork = XFS_DATA_FORK;
271 args.trans = tp;
272 args.justcheck = args.addname = args.oknoent = 0;
273 /*
274 * Decide on what work routines to call based on the inode size.
275 */
276 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
277 rval = xfs_dir2_sf_replace(&args);
0e266570 278 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
2bd0ea18
NS
279 return rval;
280 } else if (v)
281 rval = xfs_dir2_block_replace(&args);
0e266570 282 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
2bd0ea18
NS
283 return rval;
284 } else if (v)
285 rval = xfs_dir2_leaf_replace(&args);
286 else
287 rval = xfs_dir2_node_replace(&args);
288 return rval;
289}
290
291/*
292 * Utility routines.
293 */
294
295/*
296 * Add a block to the directory.
297 * This routine is for data and free blocks, not leaf/node blocks
298 * which are handled by xfs_da_grow_inode.
299 */
300int /* error */
301xfs_dir2_grow_inode(
302 xfs_da_args_t *args, /* operation arguments */
303 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
304 xfs_dir2_db_t *dbp) /* out: block number added */
305{
306 xfs_fileoff_t bno; /* directory offset of new block */
307 int count; /* count of filesystem blocks */
308 xfs_inode_t *dp; /* incore directory inode */
309 int error; /* error return value */
310 int got; /* blocks actually mapped */
311 int i; /* temp mapping index */
dfc130f3 312 xfs_bmbt_irec_t map; /* single structure for bmap */
2bd0ea18 313 int mapi; /* mapping index */
dfc130f3 314 xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */
2bd0ea18
NS
315 xfs_mount_t *mp; /* filesystem mount point */
316 int nmap; /* number of bmap entries */
317 xfs_trans_t *tp; /* transaction pointer */
318
319 xfs_dir2_trace_args_s("grow_inode", args, space);
320 dp = args->dp;
321 tp = args->trans;
322 mp = dp->i_mount;
323 /*
324 * Set lowest possible block in the space requested.
325 */
326 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
327 count = mp->m_dirblkfsbs;
328 /*
329 * Find the first hole for our block.
330 */
0e266570 331 if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK))) {
2bd0ea18
NS
332 return error;
333 }
334 nmap = 1;
335 ASSERT(args->firstblock != NULL);
336 /*
337 * Try mapping the new block contiguously (one extent).
338 */
0e266570 339 if ((error = xfs_bmapi(tp, dp, bno, count,
2bd0ea18
NS
340 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
341 args->firstblock, args->total, &map, &nmap,
0e266570 342 args->flist))) {
2bd0ea18
NS
343 return error;
344 }
345 ASSERT(nmap <= 1);
346 /*
347 * Got it in 1.
348 */
349 if (nmap == 1) {
350 mapp = &map;
351 mapi = 1;
352 }
353 /*
354 * Didn't work and this is a multiple-fsb directory block.
355 * Try again with contiguous flag turned on.
356 */
357 else if (nmap == 0 && count > 1) {
2bd0ea18
NS
358 xfs_fileoff_t b; /* current file offset */
359
360 /*
361 * Space for maximum number of mappings.
362 */
363 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
364 /*
365 * Iterate until we get to the end of our block.
366 */
367 for (b = bno, mapi = 0; b < bno + count; ) {
368 int c; /* current fsb count */
369
370 /*
371 * Can't map more than MAX_NMAP at once.
372 */
373 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
374 c = (int)(bno + count - b);
0e266570 375 if ((error = xfs_bmapi(tp, dp, b, c,
2bd0ea18
NS
376 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
377 args->firstblock, args->total,
0e266570 378 &mapp[mapi], &nmap, args->flist))) {
2bd0ea18
NS
379 kmem_free(mapp, sizeof(*mapp) * count);
380 return error;
381 }
382 if (nmap < 1)
383 break;
384 /*
385 * Add this bunch into our table, go to the next offset.
386 */
387 mapi += nmap;
388 b = mapp[mapi - 1].br_startoff +
389 mapp[mapi - 1].br_blockcount;
390 }
391 }
392 /*
393 * Didn't work.
394 */
395 else {
2bd0ea18
NS
396 mapi = 0;
397 mapp = NULL;
398 }
399 /*
400 * See how many fsb's we got.
401 */
402 for (i = 0, got = 0; i < mapi; i++)
403 got += mapp[i].br_blockcount;
404 /*
405 * Didn't get enough fsb's, or the first/last block's are wrong.
406 */
407 if (got != count || mapp[0].br_startoff != bno ||
408 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
409 bno + count) {
2bd0ea18
NS
410 if (mapp != &map)
411 kmem_free(mapp, sizeof(*mapp) * count);
412 return XFS_ERROR(ENOSPC);
413 }
414 /*
415 * Done with the temporary mapping table.
416 */
417 if (mapp != &map)
418 kmem_free(mapp, sizeof(*mapp) * count);
419 *dbp = XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)bno);
420 /*
421 * Update file's size if this is the data space and it grew.
422 */
423 if (space == XFS_DIR2_DATA_SPACE) {
424 xfs_fsize_t size; /* directory file (data) size */
425
426 size = XFS_FSB_TO_B(mp, bno + count);
427 if (size > dp->i_d.di_size) {
428 dp->i_d.di_size = size;
429 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
430 }
431 }
432 return 0;
433}
434
435/*
436 * See if the directory is a single-block form directory.
437 */
438int /* error */
439xfs_dir2_isblock(
440 xfs_trans_t *tp, /* transaction pointer */
441 xfs_inode_t *dp, /* incore directory inode */
442 int *vp) /* out: 1 is block, 0 is not block */
443{
444 xfs_fileoff_t last; /* last file offset */
445 xfs_mount_t *mp; /* filesystem mount point */
446 int rval; /* return value */
447
448 mp = dp->i_mount;
0e266570 449 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) {
2bd0ea18
NS
450 return rval;
451 }
452 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
453 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
454 *vp = rval;
455 return 0;
456}
457
458/*
459 * See if the directory is a single-leaf form directory.
460 */
461int /* error */
462xfs_dir2_isleaf(
463 xfs_trans_t *tp, /* transaction pointer */
464 xfs_inode_t *dp, /* incore directory inode */
465 int *vp) /* out: 1 is leaf, 0 is not leaf */
466{
467 xfs_fileoff_t last; /* last file offset */
468 xfs_mount_t *mp; /* filesystem mount point */
469 int rval; /* return value */
470
471 mp = dp->i_mount;
0e266570 472 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) {
2bd0ea18
NS
473 return rval;
474 }
475 *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
476 return 0;
477}
478
479/*
480 * Remove the given block from the directory.
481 * This routine is used for data and free blocks, leaf/node are done
482 * by xfs_da_shrink_inode.
483 */
484int
485xfs_dir2_shrink_inode(
486 xfs_da_args_t *args, /* operation arguments */
487 xfs_dir2_db_t db, /* directory block number */
488 xfs_dabuf_t *bp) /* block's buffer */
489{
490 xfs_fileoff_t bno; /* directory file offset */
491 xfs_dablk_t da; /* directory file offset */
492 int done; /* bunmap is finished */
493 xfs_inode_t *dp; /* incore directory inode */
494 int error; /* error return value */
495 xfs_mount_t *mp; /* filesystem mount point */
496 xfs_trans_t *tp; /* transaction pointer */
497
498 xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
499 dp = args->dp;
500 mp = dp->i_mount;
501 tp = args->trans;
502 da = XFS_DIR2_DB_TO_DA(mp, db);
503 /*
504 * Unmap the fsblock(s).
505 */
0e266570 506 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
2bd0ea18 507 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
0e266570 508 &done))) {
2bd0ea18
NS
509 /*
510 * ENOSPC actually can happen if we're in a removename with
511 * no space reservation, and the resulting block removal
512 * would cause a bmap btree split or conversion from extents
513 * to btree. This can only happen for un-fragmented
514 * directory blocks, since you need to be punching out
515 * the middle of an extent.
516 * In this case we need to leave the block in the file,
517 * and not binval it.
518 * So the block has to be in a consistent empty state
519 * and appropriately logged.
5000d01d 520 * We don't free up the buffer, the caller can tell it
2bd0ea18
NS
521 * hasn't happened since it got an error back.
522 */
523 return error;
524 }
525 ASSERT(done);
526 /*
527 * Invalidate the buffer from the transaction.
528 */
529 xfs_da_binval(tp, bp);
530 /*
531 * If it's not a data block, we're done.
532 */
5000d01d 533 if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
2bd0ea18
NS
534 return 0;
535 /*
536 * If the block isn't the last one in the directory, we're done.
537 */
538 if (dp->i_d.di_size > XFS_DIR2_DB_OFF_TO_BYTE(mp, db + 1, 0))
539 return 0;
540 bno = da;
0e266570 541 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
2bd0ea18
NS
542 /*
543 * This can't really happen unless there's kernel corruption.
544 */
545 return error;
546 }
547 if (db == mp->m_dirdatablk)
548 ASSERT(bno == 0);
549 else
550 ASSERT(bno > 0);
551 /*
552 * Set the size to the new last block.
553 */
554 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
555 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
556 return 0;
557}