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