[XFS] Remove xfs_macros.c, xfs_macros.h, rework headers a whole lot.
[linux-2.6-block.git] / fs / xfs / xfs_dir2.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2000-2001 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 */
1da177e4 32#include "xfs.h"
a844f451 33#include "xfs_fs.h"
1da177e4 34#include "xfs_types.h"
a844f451 35#include "xfs_bit.h"
1da177e4 36#include "xfs_log.h"
a844f451 37#include "xfs_inum.h"
1da177e4
LT
38#include "xfs_trans.h"
39#include "xfs_sb.h"
40#include "xfs_ag.h"
41#include "xfs_dir.h"
42#include "xfs_dir2.h"
43#include "xfs_dmapi.h"
44#include "xfs_mount.h"
a844f451 45#include "xfs_da_btree.h"
1da177e4 46#include "xfs_bmap_btree.h"
a844f451 47#include "xfs_alloc_btree.h"
1da177e4
LT
48#include "xfs_dir_sf.h"
49#include "xfs_dir2_sf.h"
a844f451 50#include "xfs_attr_sf.h"
1da177e4 51#include "xfs_dinode.h"
1da177e4 52#include "xfs_inode.h"
a844f451 53#include "xfs_inode_item.h"
1da177e4 54#include "xfs_bmap.h"
1da177e4
LT
55#include "xfs_dir_leaf.h"
56#include "xfs_dir2_data.h"
57#include "xfs_dir2_leaf.h"
58#include "xfs_dir2_block.h"
59#include "xfs_dir2_node.h"
60#include "xfs_dir2_trace.h"
61#include "xfs_error.h"
1da177e4
LT
62
63/*
64 * Declarations for interface routines.
65 */
66static void xfs_dir2_mount(xfs_mount_t *mp);
67static int xfs_dir2_isempty(xfs_inode_t *dp);
68static int xfs_dir2_init(xfs_trans_t *tp, xfs_inode_t *dp,
69 xfs_inode_t *pdp);
70static int xfs_dir2_createname(xfs_trans_t *tp, xfs_inode_t *dp,
71 char *name, int namelen, xfs_ino_t inum,
72 xfs_fsblock_t *first,
73 xfs_bmap_free_t *flist, xfs_extlen_t total);
74static int xfs_dir2_lookup(xfs_trans_t *tp, xfs_inode_t *dp, char *name,
75 int namelen, xfs_ino_t *inum);
76static int xfs_dir2_removename(xfs_trans_t *tp, xfs_inode_t *dp,
77 char *name, int namelen, xfs_ino_t ino,
78 xfs_fsblock_t *first,
79 xfs_bmap_free_t *flist, xfs_extlen_t total);
80static int xfs_dir2_getdents(xfs_trans_t *tp, xfs_inode_t *dp, uio_t *uio,
81 int *eofp);
82static int xfs_dir2_replace(xfs_trans_t *tp, xfs_inode_t *dp, char *name,
83 int namelen, xfs_ino_t inum,
84 xfs_fsblock_t *first, xfs_bmap_free_t *flist,
85 xfs_extlen_t total);
86static int xfs_dir2_canenter(xfs_trans_t *tp, xfs_inode_t *dp, char *name,
87 int namelen);
88static int xfs_dir2_shortform_validate_ondisk(xfs_mount_t *mp,
89 xfs_dinode_t *dip);
90
91/*
92 * Utility routine declarations.
93 */
94static int xfs_dir2_put_dirent64_direct(xfs_dir2_put_args_t *pa);
95static int xfs_dir2_put_dirent64_uio(xfs_dir2_put_args_t *pa);
96
97/*
98 * Directory operations vector.
99 */
100xfs_dirops_t xfsv2_dirops = {
101 .xd_mount = xfs_dir2_mount,
102 .xd_isempty = xfs_dir2_isempty,
103 .xd_init = xfs_dir2_init,
104 .xd_createname = xfs_dir2_createname,
105 .xd_lookup = xfs_dir2_lookup,
106 .xd_removename = xfs_dir2_removename,
107 .xd_getdents = xfs_dir2_getdents,
108 .xd_replace = xfs_dir2_replace,
109 .xd_canenter = xfs_dir2_canenter,
110 .xd_shortform_validate_ondisk = xfs_dir2_shortform_validate_ondisk,
111 .xd_shortform_to_single = xfs_dir2_sf_to_block,
112};
113
114/*
115 * Interface routines.
116 */
117
118/*
119 * Initialize directory-related fields in the mount structure.
120 */
121static void
122xfs_dir2_mount(
123 xfs_mount_t *mp) /* filesystem mount point */
124{
125 mp->m_dirversion = 2;
126 ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
127 XFS_MAX_BLOCKSIZE);
128 mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog);
129 mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog;
130 mp->m_dirdatablk = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_DATA_FIRSTDB(mp));
131 mp->m_dirleafblk = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_LEAF_FIRSTDB(mp));
132 mp->m_dirfreeblk = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_FREE_FIRSTDB(mp));
133 mp->m_attr_node_ents =
134 (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) /
135 (uint)sizeof(xfs_da_node_entry_t);
136 mp->m_dir_node_ents =
137 (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) /
138 (uint)sizeof(xfs_da_node_entry_t);
139 mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100;
140}
141
142/*
143 * Return 1 if directory contains only "." and "..".
144 */
145static int /* return code */
146xfs_dir2_isempty(
147 xfs_inode_t *dp) /* incore inode structure */
148{
149 xfs_dir2_sf_t *sfp; /* shortform directory structure */
150
151 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
152 /*
153 * Might happen during shutdown.
154 */
155 if (dp->i_d.di_size == 0) {
156 return 1;
157 }
158 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
159 return 0;
160 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
161 return !sfp->hdr.count;
162}
163
164/*
165 * Initialize a directory with its "." and ".." entries.
166 */
167static int /* error */
168xfs_dir2_init(
169 xfs_trans_t *tp, /* transaction pointer */
170 xfs_inode_t *dp, /* incore directory inode */
171 xfs_inode_t *pdp) /* incore parent directory inode */
172{
173 xfs_da_args_t args; /* operation arguments */
174 int error; /* error return value */
175
176 memset((char *)&args, 0, sizeof(args));
177 args.dp = dp;
178 args.trans = tp;
179 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
180 if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino))) {
181 return error;
182 }
183 return xfs_dir2_sf_create(&args, pdp->i_ino);
184}
185
186/*
187 Enter a name in a directory.
188 */
189static int /* error */
190xfs_dir2_createname(
191 xfs_trans_t *tp, /* transaction pointer */
192 xfs_inode_t *dp, /* incore directory inode */
193 char *name, /* new entry name */
194 int namelen, /* new entry name length */
195 xfs_ino_t inum, /* new entry inode number */
196 xfs_fsblock_t *first, /* bmap's firstblock */
197 xfs_bmap_free_t *flist, /* bmap's freeblock list */
198 xfs_extlen_t total) /* bmap's total block count */
199{
200 xfs_da_args_t args; /* operation arguments */
201 int rval; /* return value */
202 int v; /* type-checking value */
203
204 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
205 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) {
206 return rval;
207 }
208 XFS_STATS_INC(xs_dir_create);
209 /*
210 * Fill in the arg structure for this request.
211 */
212 args.name = name;
213 args.namelen = namelen;
214 args.hashval = xfs_da_hashname(name, namelen);
215 args.inumber = inum;
216 args.dp = dp;
217 args.firstblock = first;
218 args.flist = flist;
219 args.total = total;
220 args.whichfork = XFS_DATA_FORK;
221 args.trans = tp;
222 args.justcheck = 0;
223 args.addname = args.oknoent = 1;
224 /*
225 * Decide on what work routines to call based on the inode size.
226 */
227 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
228 rval = xfs_dir2_sf_addname(&args);
229 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
230 return rval;
231 } else if (v)
232 rval = xfs_dir2_block_addname(&args);
233 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
234 return rval;
235 } else if (v)
236 rval = xfs_dir2_leaf_addname(&args);
237 else
238 rval = xfs_dir2_node_addname(&args);
239 return rval;
240}
241
242/*
243 * Lookup a name in a directory, give back the inode number.
244 */
245static int /* error */
246xfs_dir2_lookup(
247 xfs_trans_t *tp, /* transaction pointer */
248 xfs_inode_t *dp, /* incore directory inode */
249 char *name, /* lookup name */
250 int namelen, /* lookup name length */
251 xfs_ino_t *inum) /* out: inode number */
252{
253 xfs_da_args_t args; /* operation arguments */
254 int rval; /* return value */
255 int v; /* type-checking value */
256
257 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
258 XFS_STATS_INC(xs_dir_lookup);
259
260 /*
261 * Fill in the arg structure for this request.
262 */
263 args.name = name;
264 args.namelen = namelen;
265 args.hashval = xfs_da_hashname(name, namelen);
266 args.inumber = 0;
267 args.dp = dp;
268 args.firstblock = NULL;
269 args.flist = NULL;
270 args.total = 0;
271 args.whichfork = XFS_DATA_FORK;
272 args.trans = tp;
273 args.justcheck = args.addname = 0;
274 args.oknoent = 1;
275 /*
276 * Decide on what work routines to call based on the inode size.
277 */
278 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
279 rval = xfs_dir2_sf_lookup(&args);
280 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
281 return rval;
282 } else if (v)
283 rval = xfs_dir2_block_lookup(&args);
284 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
285 return rval;
286 } else if (v)
287 rval = xfs_dir2_leaf_lookup(&args);
288 else
289 rval = xfs_dir2_node_lookup(&args);
290 if (rval == EEXIST)
291 rval = 0;
292 if (rval == 0)
293 *inum = args.inumber;
294 return rval;
295}
296
297/*
298 * Remove an entry from a directory.
299 */
300static int /* error */
301xfs_dir2_removename(
302 xfs_trans_t *tp, /* transaction pointer */
303 xfs_inode_t *dp, /* incore directory inode */
304 char *name, /* name of entry to remove */
305 int namelen, /* name length of entry to remove */
306 xfs_ino_t ino, /* inode number of entry to remove */
307 xfs_fsblock_t *first, /* bmap's firstblock */
308 xfs_bmap_free_t *flist, /* bmap's freeblock list */
309 xfs_extlen_t total) /* bmap's total block count */
310{
311 xfs_da_args_t args; /* operation arguments */
312 int rval; /* return value */
313 int v; /* type-checking value */
314
315 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
316 XFS_STATS_INC(xs_dir_remove);
317 /*
318 * Fill in the arg structure for this request.
319 */
320 args.name = name;
321 args.namelen = namelen;
322 args.hashval = xfs_da_hashname(name, namelen);
323 args.inumber = ino;
324 args.dp = dp;
325 args.firstblock = first;
326 args.flist = flist;
327 args.total = total;
328 args.whichfork = XFS_DATA_FORK;
329 args.trans = tp;
330 args.justcheck = args.addname = args.oknoent = 0;
331 /*
332 * Decide on what work routines to call based on the inode size.
333 */
334 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
335 rval = xfs_dir2_sf_removename(&args);
336 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
337 return rval;
338 } else if (v)
339 rval = xfs_dir2_block_removename(&args);
340 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
341 return rval;
342 } else if (v)
343 rval = xfs_dir2_leaf_removename(&args);
344 else
345 rval = xfs_dir2_node_removename(&args);
346 return rval;
347}
348
349/*
350 * Read a directory.
351 */
352static int /* error */
353xfs_dir2_getdents(
354 xfs_trans_t *tp, /* transaction pointer */
355 xfs_inode_t *dp, /* incore directory inode */
356 uio_t *uio, /* caller's buffer control */
357 int *eofp) /* out: eof reached */
358{
359 int alignment; /* alignment required for ABI */
360 xfs_dirent_t *dbp; /* malloc'ed buffer */
361 xfs_dir2_put_t put; /* entry formatting routine */
362 int rval; /* return value */
363 int v; /* type-checking value */
364
365 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
366 XFS_STATS_INC(xs_dir_getdents);
367 /*
368 * If our caller has given us a single contiguous aligned memory buffer,
369 * just work directly within that buffer. If it's in user memory,
370 * lock it down first.
371 */
372 alignment = sizeof(xfs_off_t) - 1;
373 if ((uio->uio_iovcnt == 1) &&
374 (((__psint_t)uio->uio_iov[0].iov_base & alignment) == 0) &&
375 ((uio->uio_iov[0].iov_len & alignment) == 0)) {
376 dbp = NULL;
377 put = xfs_dir2_put_dirent64_direct;
378 } else {
379 dbp = kmem_alloc(sizeof(*dbp) + MAXNAMELEN, KM_SLEEP);
380 put = xfs_dir2_put_dirent64_uio;
381 }
382
383 *eofp = 0;
384 /*
385 * Decide on what work routines to call based on the inode size.
386 */
387 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
388 rval = xfs_dir2_sf_getdents(dp, uio, eofp, dbp, put);
389 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
390 ;
391 } else if (v)
392 rval = xfs_dir2_block_getdents(tp, dp, uio, eofp, dbp, put);
393 else
394 rval = xfs_dir2_leaf_getdents(tp, dp, uio, eofp, dbp, put);
395 if (dbp != NULL)
396 kmem_free(dbp, sizeof(*dbp) + MAXNAMELEN);
397 return rval;
398}
399
400/*
401 * Replace the inode number of a directory entry.
402 */
403static int /* error */
404xfs_dir2_replace(
405 xfs_trans_t *tp, /* transaction pointer */
406 xfs_inode_t *dp, /* incore directory inode */
407 char *name, /* name of entry to replace */
408 int namelen, /* name length of entry to replace */
409 xfs_ino_t inum, /* new inode number */
410 xfs_fsblock_t *first, /* bmap's firstblock */
411 xfs_bmap_free_t *flist, /* bmap's freeblock list */
412 xfs_extlen_t total) /* bmap's total block count */
413{
414 xfs_da_args_t args; /* operation arguments */
415 int rval; /* return value */
416 int v; /* type-checking value */
417
418 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
419
420 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) {
421 return rval;
422 }
423 /*
424 * Fill in the arg structure for this request.
425 */
426 args.name = name;
427 args.namelen = namelen;
428 args.hashval = xfs_da_hashname(name, namelen);
429 args.inumber = inum;
430 args.dp = dp;
431 args.firstblock = first;
432 args.flist = flist;
433 args.total = total;
434 args.whichfork = XFS_DATA_FORK;
435 args.trans = tp;
436 args.justcheck = args.addname = args.oknoent = 0;
437 /*
438 * Decide on what work routines to call based on the inode size.
439 */
440 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
441 rval = xfs_dir2_sf_replace(&args);
442 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
443 return rval;
444 } else if (v)
445 rval = xfs_dir2_block_replace(&args);
446 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
447 return rval;
448 } else if (v)
449 rval = xfs_dir2_leaf_replace(&args);
450 else
451 rval = xfs_dir2_node_replace(&args);
452 return rval;
453}
454
455/*
456 * See if this entry can be added to the directory without allocating space.
457 */
458static int /* error */
459xfs_dir2_canenter(
460 xfs_trans_t *tp, /* transaction pointer */
461 xfs_inode_t *dp, /* incore directory inode */
462 char *name, /* name of entry to add */
463 int namelen) /* name length of entry to add */
464{
465 xfs_da_args_t args; /* operation arguments */
466 int rval; /* return value */
467 int v; /* type-checking value */
468
469 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
470 /*
471 * Fill in the arg structure for this request.
472 */
473 args.name = name;
474 args.namelen = namelen;
475 args.hashval = xfs_da_hashname(name, namelen);
476 args.inumber = 0;
477 args.dp = dp;
478 args.firstblock = NULL;
479 args.flist = NULL;
480 args.total = 0;
481 args.whichfork = XFS_DATA_FORK;
482 args.trans = tp;
483 args.justcheck = args.addname = args.oknoent = 1;
484 /*
485 * Decide on what work routines to call based on the inode size.
486 */
487 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
488 rval = xfs_dir2_sf_addname(&args);
489 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
490 return rval;
491 } else if (v)
492 rval = xfs_dir2_block_addname(&args);
493 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
494 return rval;
495 } else if (v)
496 rval = xfs_dir2_leaf_addname(&args);
497 else
498 rval = xfs_dir2_node_addname(&args);
499 return rval;
500}
501
502/*
503 * Dummy routine for shortform inode validation.
504 * Can't really do this.
505 */
506/* ARGSUSED */
507static int /* error */
508xfs_dir2_shortform_validate_ondisk(
509 xfs_mount_t *mp, /* filesystem mount point */
510 xfs_dinode_t *dip) /* ondisk inode */
511{
512 return 0;
513}
514
515/*
516 * Utility routines.
517 */
518
519/*
520 * Add a block to the directory.
521 * This routine is for data and free blocks, not leaf/node blocks
522 * which are handled by xfs_da_grow_inode.
523 */
524int /* error */
525xfs_dir2_grow_inode(
526 xfs_da_args_t *args, /* operation arguments */
527 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
528 xfs_dir2_db_t *dbp) /* out: block number added */
529{
530 xfs_fileoff_t bno; /* directory offset of new block */
531 int count; /* count of filesystem blocks */
532 xfs_inode_t *dp; /* incore directory inode */
533 int error; /* error return value */
534 int got; /* blocks actually mapped */
535 int i; /* temp mapping index */
536 xfs_bmbt_irec_t map; /* single structure for bmap */
537 int mapi; /* mapping index */
538 xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */
539 xfs_mount_t *mp; /* filesystem mount point */
540 int nmap; /* number of bmap entries */
541 xfs_trans_t *tp; /* transaction pointer */
542
543 xfs_dir2_trace_args_s("grow_inode", args, space);
544 dp = args->dp;
545 tp = args->trans;
546 mp = dp->i_mount;
547 /*
548 * Set lowest possible block in the space requested.
549 */
550 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
551 count = mp->m_dirblkfsbs;
552 /*
553 * Find the first hole for our block.
554 */
555 if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK))) {
556 return error;
557 }
558 nmap = 1;
559 ASSERT(args->firstblock != NULL);
560 /*
561 * Try mapping the new block contiguously (one extent).
562 */
563 if ((error = xfs_bmapi(tp, dp, bno, count,
564 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
565 args->firstblock, args->total, &map, &nmap,
566 args->flist))) {
567 return error;
568 }
569 ASSERT(nmap <= 1);
570 /*
571 * Got it in 1.
572 */
573 if (nmap == 1) {
574 mapp = &map;
575 mapi = 1;
576 }
577 /*
578 * Didn't work and this is a multiple-fsb directory block.
579 * Try again with contiguous flag turned on.
580 */
581 else if (nmap == 0 && count > 1) {
582 xfs_fileoff_t b; /* current file offset */
583
584 /*
585 * Space for maximum number of mappings.
586 */
587 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
588 /*
589 * Iterate until we get to the end of our block.
590 */
591 for (b = bno, mapi = 0; b < bno + count; ) {
592 int c; /* current fsb count */
593
594 /*
595 * Can't map more than MAX_NMAP at once.
596 */
597 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
598 c = (int)(bno + count - b);
599 if ((error = xfs_bmapi(tp, dp, b, c,
600 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
601 args->firstblock, args->total,
602 &mapp[mapi], &nmap, args->flist))) {
603 kmem_free(mapp, sizeof(*mapp) * count);
604 return error;
605 }
606 if (nmap < 1)
607 break;
608 /*
609 * Add this bunch into our table, go to the next offset.
610 */
611 mapi += nmap;
612 b = mapp[mapi - 1].br_startoff +
613 mapp[mapi - 1].br_blockcount;
614 }
615 }
616 /*
617 * Didn't work.
618 */
619 else {
620 mapi = 0;
621 mapp = NULL;
622 }
623 /*
624 * See how many fsb's we got.
625 */
626 for (i = 0, got = 0; i < mapi; i++)
627 got += mapp[i].br_blockcount;
628 /*
629 * Didn't get enough fsb's, or the first/last block's are wrong.
630 */
631 if (got != count || mapp[0].br_startoff != bno ||
632 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
633 bno + count) {
634 if (mapp != &map)
635 kmem_free(mapp, sizeof(*mapp) * count);
636 return XFS_ERROR(ENOSPC);
637 }
638 /*
639 * Done with the temporary mapping table.
640 */
641 if (mapp != &map)
642 kmem_free(mapp, sizeof(*mapp) * count);
643 *dbp = XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)bno);
644 /*
645 * Update file's size if this is the data space and it grew.
646 */
647 if (space == XFS_DIR2_DATA_SPACE) {
648 xfs_fsize_t size; /* directory file (data) size */
649
650 size = XFS_FSB_TO_B(mp, bno + count);
651 if (size > dp->i_d.di_size) {
652 dp->i_d.di_size = size;
653 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
654 }
655 }
656 return 0;
657}
658
659/*
660 * See if the directory is a single-block form directory.
661 */
662int /* error */
663xfs_dir2_isblock(
664 xfs_trans_t *tp, /* transaction pointer */
665 xfs_inode_t *dp, /* incore directory inode */
666 int *vp) /* out: 1 is block, 0 is not block */
667{
668 xfs_fileoff_t last; /* last file offset */
669 xfs_mount_t *mp; /* filesystem mount point */
670 int rval; /* return value */
671
672 mp = dp->i_mount;
673 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) {
674 return rval;
675 }
676 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
677 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
678 *vp = rval;
679 return 0;
680}
681
682/*
683 * See if the directory is a single-leaf form directory.
684 */
685int /* error */
686xfs_dir2_isleaf(
687 xfs_trans_t *tp, /* transaction pointer */
688 xfs_inode_t *dp, /* incore directory inode */
689 int *vp) /* out: 1 is leaf, 0 is not leaf */
690{
691 xfs_fileoff_t last; /* last file offset */
692 xfs_mount_t *mp; /* filesystem mount point */
693 int rval; /* return value */
694
695 mp = dp->i_mount;
696 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) {
697 return rval;
698 }
699 *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
700 return 0;
701}
702
703/*
704 * Getdents put routine for 64-bit ABI, direct form.
705 */
706static int /* error */
707xfs_dir2_put_dirent64_direct(
708 xfs_dir2_put_args_t *pa) /* argument bundle */
709{
710 xfs_dirent_t *idbp; /* dirent pointer */
711 iovec_t *iovp; /* io vector */
712 int namelen; /* entry name length */
713 int reclen; /* entry total length */
714 uio_t *uio; /* I/O control */
715
716 namelen = pa->namelen;
717 reclen = DIRENTSIZE(namelen);
718 uio = pa->uio;
719 /*
720 * Won't fit in the remaining space.
721 */
722 if (reclen > uio->uio_resid) {
723 pa->done = 0;
724 return 0;
725 }
726 iovp = uio->uio_iov;
727 idbp = (xfs_dirent_t *)iovp->iov_base;
728 iovp->iov_base = (char *)idbp + reclen;
729 iovp->iov_len -= reclen;
730 uio->uio_resid -= reclen;
731 idbp->d_reclen = reclen;
732 idbp->d_ino = pa->ino;
733 idbp->d_off = pa->cook;
734 idbp->d_name[namelen] = '\0';
735 pa->done = 1;
736 memcpy(idbp->d_name, pa->name, namelen);
737 return 0;
738}
739
740/*
741 * Getdents put routine for 64-bit ABI, uio form.
742 */
743static int /* error */
744xfs_dir2_put_dirent64_uio(
745 xfs_dir2_put_args_t *pa) /* argument bundle */
746{
747 xfs_dirent_t *idbp; /* dirent pointer */
748 int namelen; /* entry name length */
749 int reclen; /* entry total length */
750 int rval; /* return value */
751 uio_t *uio; /* I/O control */
752
753 namelen = pa->namelen;
754 reclen = DIRENTSIZE(namelen);
755 uio = pa->uio;
756 /*
757 * Won't fit in the remaining space.
758 */
759 if (reclen > uio->uio_resid) {
760 pa->done = 0;
761 return 0;
762 }
763 idbp = pa->dbp;
764 idbp->d_reclen = reclen;
765 idbp->d_ino = pa->ino;
766 idbp->d_off = pa->cook;
767 idbp->d_name[namelen] = '\0';
768 memcpy(idbp->d_name, pa->name, namelen);
769 rval = uio_read((caddr_t)idbp, reclen, uio);
770 pa->done = (rval == 0);
771 return rval;
772}
773
774/*
775 * Remove the given block from the directory.
776 * This routine is used for data and free blocks, leaf/node are done
777 * by xfs_da_shrink_inode.
778 */
779int
780xfs_dir2_shrink_inode(
781 xfs_da_args_t *args, /* operation arguments */
782 xfs_dir2_db_t db, /* directory block number */
783 xfs_dabuf_t *bp) /* block's buffer */
784{
785 xfs_fileoff_t bno; /* directory file offset */
786 xfs_dablk_t da; /* directory file offset */
787 int done; /* bunmap is finished */
788 xfs_inode_t *dp; /* incore directory inode */
789 int error; /* error return value */
790 xfs_mount_t *mp; /* filesystem mount point */
791 xfs_trans_t *tp; /* transaction pointer */
792
793 xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
794 dp = args->dp;
795 mp = dp->i_mount;
796 tp = args->trans;
797 da = XFS_DIR2_DB_TO_DA(mp, db);
798 /*
799 * Unmap the fsblock(s).
800 */
801 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
802 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
803 &done))) {
804 /*
805 * ENOSPC actually can happen if we're in a removename with
806 * no space reservation, and the resulting block removal
807 * would cause a bmap btree split or conversion from extents
808 * to btree. This can only happen for un-fragmented
809 * directory blocks, since you need to be punching out
810 * the middle of an extent.
811 * In this case we need to leave the block in the file,
812 * and not binval it.
813 * So the block has to be in a consistent empty state
814 * and appropriately logged.
815 * We don't free up the buffer, the caller can tell it
816 * hasn't happened since it got an error back.
817 */
818 return error;
819 }
820 ASSERT(done);
821 /*
822 * Invalidate the buffer from the transaction.
823 */
824 xfs_da_binval(tp, bp);
825 /*
826 * If it's not a data block, we're done.
827 */
828 if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
829 return 0;
830 /*
831 * If the block isn't the last one in the directory, we're done.
832 */
833 if (dp->i_d.di_size > XFS_DIR2_DB_OFF_TO_BYTE(mp, db + 1, 0))
834 return 0;
835 bno = da;
836 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
837 /*
838 * This can't really happen unless there's kernel corruption.
839 */
840 return error;
841 }
842 if (db == mp->m_dirdatablk)
843 ASSERT(bno == 0);
844 else
845 ASSERT(bno > 0);
846 /*
847 * Set the size to the new last block.
848 */
849 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
850 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
851 return 0;
852}