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