blob: be7c4251fa619e03a161cf3d66c60dd2e0518151 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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"
David Chinnera8272ce2007-11-23 16:28:09 +110045#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100048void
49xfs_dir_mount(
50 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100052 ASSERT(XFS_SB_VERSION_HASDIRV2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
54 XFS_MAX_BLOCKSIZE);
55 mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog);
56 mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100057 mp->m_dirdatablk = xfs_dir2_db_to_da(mp, XFS_DIR2_DATA_FIRSTDB(mp));
58 mp->m_dirleafblk = xfs_dir2_db_to_da(mp, XFS_DIR2_LEAF_FIRSTDB(mp));
59 mp->m_dirfreeblk = xfs_dir2_db_to_da(mp, XFS_DIR2_FREE_FIRSTDB(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 mp->m_attr_node_ents =
61 (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) /
62 (uint)sizeof(xfs_da_node_entry_t);
63 mp->m_dir_node_ents =
64 (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) /
65 (uint)sizeof(xfs_da_node_entry_t);
66 mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100;
67}
68
69/*
70 * Return 1 if directory contains only "." and "..".
71 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100072int
73xfs_dir_isempty(
74 xfs_inode_t *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100076 xfs_dir2_sf_t *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100079 if (dp->i_d.di_size == 0) /* might happen during shutdown. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
82 return 0;
83 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
84 return !sfp->hdr.count;
85}
86
87/*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100088 * Validate a given inode number.
89 */
90int
91xfs_dir_ino_validate(
92 xfs_mount_t *mp,
93 xfs_ino_t ino)
94{
95 xfs_agblock_t agblkno;
96 xfs_agino_t agino;
97 xfs_agnumber_t agno;
98 int ino_ok;
99 int ioff;
100
101 agno = XFS_INO_TO_AGNO(mp, ino);
102 agblkno = XFS_INO_TO_AGBNO(mp, ino);
103 ioff = XFS_INO_TO_OFFSET(mp, ino);
104 agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
105 ino_ok =
106 agno < mp->m_sb.sb_agcount &&
107 agblkno < mp->m_sb.sb_agblocks &&
108 agblkno != 0 &&
109 ioff < (1 << mp->m_sb.sb_inopblog) &&
110 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
111 if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
112 XFS_RANDOM_DIR_INO_VALIDATE))) {
113 xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx",
114 (unsigned long long) ino);
115 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
116 return XFS_ERROR(EFSCORRUPTED);
117 }
118 return 0;
119}
120
121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * Initialize a directory with its "." and ".." entries.
123 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000124int
125xfs_dir_init(
126 xfs_trans_t *tp,
127 xfs_inode_t *dp,
128 xfs_inode_t *pdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000130 xfs_da_args_t args;
131 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 memset((char *)&args, 0, sizeof(args));
134 args.dp = dp;
135 args.trans = tp;
136 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000137 if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return xfs_dir2_sf_create(&args, pdp->i_ino);
140}
141
142/*
143 Enter a name in a directory.
144 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000145int
146xfs_dir_createname(
147 xfs_trans_t *tp,
148 xfs_inode_t *dp,
149 char *name,
150 int namelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 xfs_ino_t inum, /* new entry inode number */
152 xfs_fsblock_t *first, /* bmap's firstblock */
153 xfs_bmap_free_t *flist, /* bmap's freeblock list */
154 xfs_extlen_t total) /* bmap's total block count */
155{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000156 xfs_da_args_t args;
157 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 int v; /* type-checking value */
159
160 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000161 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 XFS_STATS_INC(xs_dir_create);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 args.name = name;
166 args.namelen = namelen;
167 args.hashval = xfs_da_hashname(name, namelen);
168 args.inumber = inum;
169 args.dp = dp;
170 args.firstblock = first;
171 args.flist = flist;
172 args.total = total;
173 args.whichfork = XFS_DATA_FORK;
174 args.trans = tp;
175 args.justcheck = 0;
176 args.addname = args.oknoent = 1;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
179 rval = xfs_dir2_sf_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000180 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000182 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 rval = xfs_dir2_block_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000184 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000186 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 rval = xfs_dir2_leaf_addname(&args);
188 else
189 rval = xfs_dir2_node_addname(&args);
190 return rval;
191}
192
193/*
194 * Lookup a name in a directory, give back the inode number.
195 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000196int
197xfs_dir_lookup(
198 xfs_trans_t *tp,
199 xfs_inode_t *dp,
200 char *name,
201 int namelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 xfs_ino_t *inum) /* out: inode number */
203{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000204 xfs_da_args_t args;
205 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int v; /* type-checking value */
207
208 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
209 XFS_STATS_INC(xs_dir_lookup);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 args.name = name;
212 args.namelen = namelen;
213 args.hashval = xfs_da_hashname(name, namelen);
214 args.inumber = 0;
215 args.dp = dp;
216 args.firstblock = NULL;
217 args.flist = NULL;
218 args.total = 0;
219 args.whichfork = XFS_DATA_FORK;
220 args.trans = tp;
221 args.justcheck = args.addname = 0;
222 args.oknoent = 1;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
225 rval = xfs_dir2_sf_lookup(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000226 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000228 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 rval = xfs_dir2_block_lookup(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000230 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000232 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 rval = xfs_dir2_leaf_lookup(&args);
234 else
235 rval = xfs_dir2_node_lookup(&args);
236 if (rval == EEXIST)
237 rval = 0;
238 if (rval == 0)
239 *inum = args.inumber;
240 return rval;
241}
242
243/*
244 * Remove an entry from a directory.
245 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000246int
247xfs_dir_removename(
248 xfs_trans_t *tp,
249 xfs_inode_t *dp,
250 char *name,
251 int namelen,
252 xfs_ino_t ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 xfs_fsblock_t *first, /* bmap's firstblock */
254 xfs_bmap_free_t *flist, /* bmap's freeblock list */
255 xfs_extlen_t total) /* bmap's total block count */
256{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000257 xfs_da_args_t args;
258 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 int v; /* type-checking value */
260
261 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
262 XFS_STATS_INC(xs_dir_remove);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 args.name = name;
265 args.namelen = namelen;
266 args.hashval = xfs_da_hashname(name, namelen);
267 args.inumber = ino;
268 args.dp = dp;
269 args.firstblock = first;
270 args.flist = flist;
271 args.total = total;
272 args.whichfork = XFS_DATA_FORK;
273 args.trans = tp;
274 args.justcheck = args.addname = args.oknoent = 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
277 rval = xfs_dir2_sf_removename(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000278 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000280 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 rval = xfs_dir2_block_removename(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000282 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000284 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 rval = xfs_dir2_leaf_removename(&args);
286 else
287 rval = xfs_dir2_node_removename(&args);
288 return rval;
289}
290
291/*
292 * Read a directory.
293 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000294int
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000295xfs_readdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000296 xfs_inode_t *dp,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000297 void *dirent,
298 size_t bufsize,
299 xfs_off_t *offset,
300 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 int rval; /* return value */
303 int v; /* type-checking value */
304
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100305 xfs_itrace_entry(dp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000306
307 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
308 return XFS_ERROR(EIO);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
311 XFS_STATS_INC(xs_dir_getdents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000314 rval = xfs_dir2_sf_getdents(dp, dirent, offset, filldir);
315 else if ((rval = xfs_dir2_isblock(NULL, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 ;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000317 else if (v)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000318 rval = xfs_dir2_block_getdents(dp, dirent, offset, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 else
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000320 rval = xfs_dir2_leaf_getdents(dp, dirent, bufsize, offset,
321 filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return rval;
323}
324
325/*
326 * Replace the inode number of a directory entry.
327 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000328int
329xfs_dir_replace(
330 xfs_trans_t *tp,
331 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 char *name, /* name of entry to replace */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000333 int namelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 xfs_ino_t inum, /* new inode number */
335 xfs_fsblock_t *first, /* bmap's firstblock */
336 xfs_bmap_free_t *flist, /* bmap's freeblock list */
337 xfs_extlen_t total) /* bmap's total block count */
338{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000339 xfs_da_args_t args;
340 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int v; /* type-checking value */
342
343 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
344
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000345 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 args.name = name;
349 args.namelen = namelen;
350 args.hashval = xfs_da_hashname(name, namelen);
351 args.inumber = inum;
352 args.dp = dp;
353 args.firstblock = first;
354 args.flist = flist;
355 args.total = total;
356 args.whichfork = XFS_DATA_FORK;
357 args.trans = tp;
358 args.justcheck = args.addname = args.oknoent = 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
361 rval = xfs_dir2_sf_replace(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000362 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000364 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 rval = xfs_dir2_block_replace(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000366 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000368 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 rval = xfs_dir2_leaf_replace(&args);
370 else
371 rval = xfs_dir2_node_replace(&args);
372 return rval;
373}
374
375/*
376 * See if this entry can be added to the directory without allocating space.
377 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000378int
379xfs_dir_canenter(
380 xfs_trans_t *tp,
381 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 char *name, /* name of entry to add */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000383 int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000385 xfs_da_args_t args;
386 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 int v; /* type-checking value */
388
389 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 args.name = name;
392 args.namelen = namelen;
393 args.hashval = xfs_da_hashname(name, namelen);
394 args.inumber = 0;
395 args.dp = dp;
396 args.firstblock = NULL;
397 args.flist = NULL;
398 args.total = 0;
399 args.whichfork = XFS_DATA_FORK;
400 args.trans = tp;
401 args.justcheck = args.addname = args.oknoent = 1;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
404 rval = xfs_dir2_sf_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000405 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000407 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 rval = xfs_dir2_block_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000409 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000411 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 rval = xfs_dir2_leaf_addname(&args);
413 else
414 rval = xfs_dir2_node_addname(&args);
415 return rval;
416}
417
418/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * Utility routines.
420 */
421
422/*
423 * Add a block to the directory.
424 * This routine is for data and free blocks, not leaf/node blocks
425 * which are handled by xfs_da_grow_inode.
426 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000427int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428xfs_dir2_grow_inode(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000429 xfs_da_args_t *args,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
431 xfs_dir2_db_t *dbp) /* out: block number added */
432{
433 xfs_fileoff_t bno; /* directory offset of new block */
434 int count; /* count of filesystem blocks */
435 xfs_inode_t *dp; /* incore directory inode */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000436 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int got; /* blocks actually mapped */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000438 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 xfs_bmbt_irec_t map; /* single structure for bmap */
440 int mapi; /* mapping index */
441 xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000442 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 int nmap; /* number of bmap entries */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000444 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 xfs_dir2_trace_args_s("grow_inode", args, space);
447 dp = args->dp;
448 tp = args->trans;
449 mp = dp->i_mount;
450 /*
451 * Set lowest possible block in the space requested.
452 */
453 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
454 count = mp->m_dirblkfsbs;
455 /*
456 * Find the first hole for our block.
457 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000458 if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 nmap = 1;
461 ASSERT(args->firstblock != NULL);
462 /*
463 * Try mapping the new block contiguously (one extent).
464 */
465 if ((error = xfs_bmapi(tp, dp, bno, count,
466 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
467 args->firstblock, args->total, &map, &nmap,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000468 args->flist, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 ASSERT(nmap <= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (nmap == 1) {
472 mapp = &map;
473 mapi = 1;
474 }
475 /*
476 * Didn't work and this is a multiple-fsb directory block.
477 * Try again with contiguous flag turned on.
478 */
479 else if (nmap == 0 && count > 1) {
480 xfs_fileoff_t b; /* current file offset */
481
482 /*
483 * Space for maximum number of mappings.
484 */
485 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
486 /*
487 * Iterate until we get to the end of our block.
488 */
489 for (b = bno, mapi = 0; b < bno + count; ) {
490 int c; /* current fsb count */
491
492 /*
493 * Can't map more than MAX_NMAP at once.
494 */
495 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
496 c = (int)(bno + count - b);
497 if ((error = xfs_bmapi(tp, dp, b, c,
498 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
499 args->firstblock, args->total,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000500 &mapp[mapi], &nmap, args->flist,
501 NULL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 kmem_free(mapp, sizeof(*mapp) * count);
503 return error;
504 }
505 if (nmap < 1)
506 break;
507 /*
508 * Add this bunch into our table, go to the next offset.
509 */
510 mapi += nmap;
511 b = mapp[mapi - 1].br_startoff +
512 mapp[mapi - 1].br_blockcount;
513 }
514 }
515 /*
516 * Didn't work.
517 */
518 else {
519 mapi = 0;
520 mapp = NULL;
521 }
522 /*
523 * See how many fsb's we got.
524 */
525 for (i = 0, got = 0; i < mapi; i++)
526 got += mapp[i].br_blockcount;
527 /*
528 * Didn't get enough fsb's, or the first/last block's are wrong.
529 */
530 if (got != count || mapp[0].br_startoff != bno ||
531 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
532 bno + count) {
533 if (mapp != &map)
534 kmem_free(mapp, sizeof(*mapp) * count);
535 return XFS_ERROR(ENOSPC);
536 }
537 /*
538 * Done with the temporary mapping table.
539 */
540 if (mapp != &map)
541 kmem_free(mapp, sizeof(*mapp) * count);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000542 *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /*
544 * Update file's size if this is the data space and it grew.
545 */
546 if (space == XFS_DIR2_DATA_SPACE) {
547 xfs_fsize_t size; /* directory file (data) size */
548
549 size = XFS_FSB_TO_B(mp, bno + count);
550 if (size > dp->i_d.di_size) {
551 dp->i_d.di_size = size;
552 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
553 }
554 }
555 return 0;
556}
557
558/*
559 * See if the directory is a single-block form directory.
560 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000561int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562xfs_dir2_isblock(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000563 xfs_trans_t *tp,
564 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int *vp) /* out: 1 is block, 0 is not block */
566{
567 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000568 xfs_mount_t *mp;
569 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000572 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
575 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
576 *vp = rval;
577 return 0;
578}
579
580/*
581 * See if the directory is a single-leaf form directory.
582 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000583int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584xfs_dir2_isleaf(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000585 xfs_trans_t *tp,
586 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int *vp) /* out: 1 is leaf, 0 is not leaf */
588{
589 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000590 xfs_mount_t *mp;
591 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000594 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
597 return 0;
598}
599
600/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * Remove the given block from the directory.
602 * This routine is used for data and free blocks, leaf/node are done
603 * by xfs_da_shrink_inode.
604 */
605int
606xfs_dir2_shrink_inode(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000607 xfs_da_args_t *args,
608 xfs_dir2_db_t db,
609 xfs_dabuf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 xfs_fileoff_t bno; /* directory file offset */
612 xfs_dablk_t da; /* directory file offset */
613 int done; /* bunmap is finished */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000614 xfs_inode_t *dp;
615 int error;
616 xfs_mount_t *mp;
617 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
620 dp = args->dp;
621 mp = dp->i_mount;
622 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000623 da = xfs_dir2_db_to_da(mp, db);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /*
625 * Unmap the fsblock(s).
626 */
627 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
628 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000629 NULL, &done))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
631 * ENOSPC actually can happen if we're in a removename with
632 * no space reservation, and the resulting block removal
633 * would cause a bmap btree split or conversion from extents
634 * to btree. This can only happen for un-fragmented
635 * directory blocks, since you need to be punching out
636 * the middle of an extent.
637 * In this case we need to leave the block in the file,
638 * and not binval it.
639 * So the block has to be in a consistent empty state
640 * and appropriately logged.
641 * We don't free up the buffer, the caller can tell it
642 * hasn't happened since it got an error back.
643 */
644 return error;
645 }
646 ASSERT(done);
647 /*
648 * Invalidate the buffer from the transaction.
649 */
650 xfs_da_binval(tp, bp);
651 /*
652 * If it's not a data block, we're done.
653 */
654 if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
655 return 0;
656 /*
657 * If the block isn't the last one in the directory, we're done.
658 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000659 if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 0;
661 bno = da;
662 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
663 /*
664 * This can't really happen unless there's kernel corruption.
665 */
666 return error;
667 }
668 if (db == mp->m_dirdatablk)
669 ASSERT(bno == 0);
670 else
671 ASSERT(bno > 0);
672 /*
673 * Set the size to the new last block.
674 */
675 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
676 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
677 return 0;
678}