blob: 675899bb70486576d0e335c348d97a1628fcc457 [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
Barry Naujok556b8b12008-04-10 12:22:07 +100047struct xfs_name xfs_name_dotdot = {"..", 2};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100049void
50xfs_dir_mount(
51 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Eric Sandeen62118702008-03-06 13:44:28 +110053 ASSERT(xfs_sb_version_hasdirv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
55 XFS_MAX_BLOCKSIZE);
56 mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog);
57 mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100058 mp->m_dirdatablk = xfs_dir2_db_to_da(mp, XFS_DIR2_DATA_FIRSTDB(mp));
59 mp->m_dirleafblk = xfs_dir2_db_to_da(mp, XFS_DIR2_LEAF_FIRSTDB(mp));
60 mp->m_dirfreeblk = xfs_dir2_db_to_da(mp, XFS_DIR2_FREE_FIRSTDB(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 mp->m_attr_node_ents =
62 (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) /
63 (uint)sizeof(xfs_da_node_entry_t);
64 mp->m_dir_node_ents =
65 (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) /
66 (uint)sizeof(xfs_da_node_entry_t);
67 mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100;
Barry Naujok5163f952008-05-21 16:41:01 +100068 mp->m_dirnameops = &xfs_default_nameops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71/*
72 * Return 1 if directory contains only "." and "..".
73 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100074int
75xfs_dir_isempty(
76 xfs_inode_t *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100078 xfs_dir2_sf_t *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100081 if (dp->i_d.di_size == 0) /* might happen during shutdown. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
84 return 0;
85 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
86 return !sfp->hdr.count;
87}
88
89/*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100090 * Validate a given inode number.
91 */
92int
93xfs_dir_ino_validate(
94 xfs_mount_t *mp,
95 xfs_ino_t ino)
96{
97 xfs_agblock_t agblkno;
98 xfs_agino_t agino;
99 xfs_agnumber_t agno;
100 int ino_ok;
101 int ioff;
102
103 agno = XFS_INO_TO_AGNO(mp, ino);
104 agblkno = XFS_INO_TO_AGBNO(mp, ino);
105 ioff = XFS_INO_TO_OFFSET(mp, ino);
106 agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
107 ino_ok =
108 agno < mp->m_sb.sb_agcount &&
109 agblkno < mp->m_sb.sb_agblocks &&
110 agblkno != 0 &&
111 ioff < (1 << mp->m_sb.sb_inopblog) &&
112 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
113 if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
114 XFS_RANDOM_DIR_INO_VALIDATE))) {
115 xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx",
116 (unsigned long long) ino);
117 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
118 return XFS_ERROR(EFSCORRUPTED);
119 }
120 return 0;
121}
122
123/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * Initialize a directory with its "." and ".." entries.
125 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000126int
127xfs_dir_init(
128 xfs_trans_t *tp,
129 xfs_inode_t *dp,
130 xfs_inode_t *pdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000132 xfs_da_args_t args;
133 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 memset((char *)&args, 0, sizeof(args));
136 args.dp = dp;
137 args.trans = tp;
138 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000139 if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return xfs_dir2_sf_create(&args, pdp->i_ino);
142}
143
144/*
145 Enter a name in a directory.
146 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000147int
148xfs_dir_createname(
149 xfs_trans_t *tp,
150 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000151 struct xfs_name *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 xfs_ino_t inum, /* new entry inode number */
153 xfs_fsblock_t *first, /* bmap's firstblock */
154 xfs_bmap_free_t *flist, /* bmap's freeblock list */
155 xfs_extlen_t total) /* bmap's total block count */
156{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000157 xfs_da_args_t args;
158 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int v; /* type-checking value */
160
161 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000162 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 XFS_STATS_INC(xs_dir_create);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000165
Barry Naujok556b8b12008-04-10 12:22:07 +1000166 args.name = name->name;
167 args.namelen = name->len;
Barry Naujok5163f952008-05-21 16:41:01 +1000168 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 args.inumber = inum;
170 args.dp = dp;
171 args.firstblock = first;
172 args.flist = flist;
173 args.total = total;
174 args.whichfork = XFS_DATA_FORK;
175 args.trans = tp;
176 args.justcheck = 0;
177 args.addname = args.oknoent = 1;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
180 rval = xfs_dir2_sf_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000181 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000183 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 rval = xfs_dir2_block_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000185 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000187 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 rval = xfs_dir2_leaf_addname(&args);
189 else
190 rval = xfs_dir2_node_addname(&args);
191 return rval;
192}
193
194/*
195 * Lookup a name in a directory, give back the inode number.
196 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000197int
198xfs_dir_lookup(
199 xfs_trans_t *tp,
200 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000201 struct xfs_name *name,
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);
Barry Naujok556b8b12008-04-10 12:22:07 +1000210 memset(&args, 0, sizeof(xfs_da_args_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Barry Naujok556b8b12008-04-10 12:22:07 +1000212 args.name = name->name;
213 args.namelen = name->len;
Barry Naujok5163f952008-05-21 16:41:01 +1000214 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 args.dp = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 args.whichfork = XFS_DATA_FORK;
217 args.trans = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 args.oknoent = 1;
Barry Naujok5163f952008-05-21 16:41:01 +1000219 args.cmpresult = XFS_CMP_DIFFERENT;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
222 rval = xfs_dir2_sf_lookup(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000223 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000225 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 rval = xfs_dir2_block_lookup(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000227 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000229 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 rval = xfs_dir2_leaf_lookup(&args);
231 else
232 rval = xfs_dir2_node_lookup(&args);
233 if (rval == EEXIST)
234 rval = 0;
235 if (rval == 0)
236 *inum = args.inumber;
237 return rval;
238}
239
240/*
241 * Remove an entry from a directory.
242 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000243int
244xfs_dir_removename(
245 xfs_trans_t *tp,
246 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000247 struct xfs_name *name,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000248 xfs_ino_t ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 xfs_fsblock_t *first, /* bmap's firstblock */
250 xfs_bmap_free_t *flist, /* bmap's freeblock list */
251 xfs_extlen_t total) /* bmap's total block count */
252{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000253 xfs_da_args_t args;
254 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int v; /* type-checking value */
256
257 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
258 XFS_STATS_INC(xs_dir_remove);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000259
Barry Naujok556b8b12008-04-10 12:22:07 +1000260 args.name = name->name;
261 args.namelen = name->len;
Barry Naujok5163f952008-05-21 16:41:01 +1000262 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 args.inumber = ino;
264 args.dp = dp;
265 args.firstblock = first;
266 args.flist = flist;
267 args.total = total;
268 args.whichfork = XFS_DATA_FORK;
269 args.trans = tp;
270 args.justcheck = args.addname = args.oknoent = 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
273 rval = xfs_dir2_sf_removename(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000274 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000276 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 rval = xfs_dir2_block_removename(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000278 else if ((rval = xfs_dir2_isleaf(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_leaf_removename(&args);
282 else
283 rval = xfs_dir2_node_removename(&args);
284 return rval;
285}
286
287/*
288 * Read a directory.
289 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000290int
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000291xfs_readdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000292 xfs_inode_t *dp,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000293 void *dirent,
294 size_t bufsize,
295 xfs_off_t *offset,
296 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int rval; /* return value */
299 int v; /* type-checking value */
300
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100301 xfs_itrace_entry(dp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000302
303 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
304 return XFS_ERROR(EIO);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
307 XFS_STATS_INC(xs_dir_getdents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000310 rval = xfs_dir2_sf_getdents(dp, dirent, offset, filldir);
311 else if ((rval = xfs_dir2_isblock(NULL, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 ;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000313 else if (v)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000314 rval = xfs_dir2_block_getdents(dp, dirent, offset, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 else
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000316 rval = xfs_dir2_leaf_getdents(dp, dirent, bufsize, offset,
317 filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return rval;
319}
320
321/*
322 * Replace the inode number of a directory entry.
323 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000324int
325xfs_dir_replace(
326 xfs_trans_t *tp,
327 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000328 struct xfs_name *name, /* name of entry to replace */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 xfs_ino_t inum, /* new inode number */
330 xfs_fsblock_t *first, /* bmap's firstblock */
331 xfs_bmap_free_t *flist, /* bmap's freeblock list */
332 xfs_extlen_t total) /* bmap's total block count */
333{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000334 xfs_da_args_t args;
335 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int v; /* type-checking value */
337
338 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
339
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000340 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000342
Barry Naujok556b8b12008-04-10 12:22:07 +1000343 args.name = name->name;
344 args.namelen = name->len;
Barry Naujok5163f952008-05-21 16:41:01 +1000345 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 args.inumber = inum;
347 args.dp = dp;
348 args.firstblock = first;
349 args.flist = flist;
350 args.total = total;
351 args.whichfork = XFS_DATA_FORK;
352 args.trans = tp;
353 args.justcheck = args.addname = args.oknoent = 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
356 rval = xfs_dir2_sf_replace(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000357 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000359 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 rval = xfs_dir2_block_replace(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000361 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000363 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 rval = xfs_dir2_leaf_replace(&args);
365 else
366 rval = xfs_dir2_node_replace(&args);
367 return rval;
368}
369
370/*
371 * See if this entry can be added to the directory without allocating space.
Barry Naujok556b8b12008-04-10 12:22:07 +1000372 * First checks that the caller couldn't reserve enough space (resblks = 0).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000374int
375xfs_dir_canenter(
376 xfs_trans_t *tp,
377 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000378 struct xfs_name *name, /* name of entry to add */
379 uint resblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000381 xfs_da_args_t args;
382 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int v; /* type-checking value */
384
Barry Naujok556b8b12008-04-10 12:22:07 +1000385 if (resblks)
386 return 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000387
Barry Naujok556b8b12008-04-10 12:22:07 +1000388 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
389 memset(&args, 0, sizeof(xfs_da_args_t));
390
391 args.name = name->name;
392 args.namelen = name->len;
Barry Naujok5163f952008-05-21 16:41:01 +1000393 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 args.dp = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 args.whichfork = XFS_DATA_FORK;
396 args.trans = tp;
397 args.justcheck = args.addname = args.oknoent = 1;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
400 rval = xfs_dir2_sf_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000401 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000403 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 rval = xfs_dir2_block_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000405 else if ((rval = xfs_dir2_isleaf(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_leaf_addname(&args);
409 else
410 rval = xfs_dir2_node_addname(&args);
411 return rval;
412}
413
414/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * Utility routines.
416 */
417
418/*
419 * Add a block to the directory.
420 * This routine is for data and free blocks, not leaf/node blocks
421 * which are handled by xfs_da_grow_inode.
422 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000423int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424xfs_dir2_grow_inode(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000425 xfs_da_args_t *args,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
427 xfs_dir2_db_t *dbp) /* out: block number added */
428{
429 xfs_fileoff_t bno; /* directory offset of new block */
430 int count; /* count of filesystem blocks */
431 xfs_inode_t *dp; /* incore directory inode */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000432 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int got; /* blocks actually mapped */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000434 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 xfs_bmbt_irec_t map; /* single structure for bmap */
436 int mapi; /* mapping index */
437 xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000438 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int nmap; /* number of bmap entries */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000440 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 xfs_dir2_trace_args_s("grow_inode", args, space);
443 dp = args->dp;
444 tp = args->trans;
445 mp = dp->i_mount;
446 /*
447 * Set lowest possible block in the space requested.
448 */
449 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
450 count = mp->m_dirblkfsbs;
451 /*
452 * Find the first hole for our block.
453 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000454 if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 nmap = 1;
457 ASSERT(args->firstblock != NULL);
458 /*
459 * Try mapping the new block contiguously (one extent).
460 */
461 if ((error = xfs_bmapi(tp, dp, bno, count,
462 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
463 args->firstblock, args->total, &map, &nmap,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000464 args->flist, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ASSERT(nmap <= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (nmap == 1) {
468 mapp = &map;
469 mapi = 1;
470 }
471 /*
472 * Didn't work and this is a multiple-fsb directory block.
473 * Try again with contiguous flag turned on.
474 */
475 else if (nmap == 0 && count > 1) {
476 xfs_fileoff_t b; /* current file offset */
477
478 /*
479 * Space for maximum number of mappings.
480 */
481 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
482 /*
483 * Iterate until we get to the end of our block.
484 */
485 for (b = bno, mapi = 0; b < bno + count; ) {
486 int c; /* current fsb count */
487
488 /*
489 * Can't map more than MAX_NMAP at once.
490 */
491 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
492 c = (int)(bno + count - b);
493 if ((error = xfs_bmapi(tp, dp, b, c,
494 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
495 args->firstblock, args->total,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000496 &mapp[mapi], &nmap, args->flist,
497 NULL))) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000498 kmem_free(mapp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return error;
500 }
501 if (nmap < 1)
502 break;
503 /*
504 * Add this bunch into our table, go to the next offset.
505 */
506 mapi += nmap;
507 b = mapp[mapi - 1].br_startoff +
508 mapp[mapi - 1].br_blockcount;
509 }
510 }
511 /*
512 * Didn't work.
513 */
514 else {
515 mapi = 0;
516 mapp = NULL;
517 }
518 /*
519 * See how many fsb's we got.
520 */
521 for (i = 0, got = 0; i < mapi; i++)
522 got += mapp[i].br_blockcount;
523 /*
524 * Didn't get enough fsb's, or the first/last block's are wrong.
525 */
526 if (got != count || mapp[0].br_startoff != bno ||
527 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
528 bno + count) {
529 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000530 kmem_free(mapp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return XFS_ERROR(ENOSPC);
532 }
533 /*
534 * Done with the temporary mapping table.
535 */
536 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000537 kmem_free(mapp);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000538 *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /*
540 * Update file's size if this is the data space and it grew.
541 */
542 if (space == XFS_DIR2_DATA_SPACE) {
543 xfs_fsize_t size; /* directory file (data) size */
544
545 size = XFS_FSB_TO_B(mp, bno + count);
546 if (size > dp->i_d.di_size) {
547 dp->i_d.di_size = size;
548 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
549 }
550 }
551 return 0;
552}
553
554/*
555 * See if the directory is a single-block form directory.
556 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000557int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558xfs_dir2_isblock(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000559 xfs_trans_t *tp,
560 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int *vp) /* out: 1 is block, 0 is not block */
562{
563 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000564 xfs_mount_t *mp;
565 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000568 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
571 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
572 *vp = rval;
573 return 0;
574}
575
576/*
577 * See if the directory is a single-leaf form directory.
578 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000579int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580xfs_dir2_isleaf(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000581 xfs_trans_t *tp,
582 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int *vp) /* out: 1 is leaf, 0 is not leaf */
584{
585 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000586 xfs_mount_t *mp;
587 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000590 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
593 return 0;
594}
595
596/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * Remove the given block from the directory.
598 * This routine is used for data and free blocks, leaf/node are done
599 * by xfs_da_shrink_inode.
600 */
601int
602xfs_dir2_shrink_inode(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000603 xfs_da_args_t *args,
604 xfs_dir2_db_t db,
605 xfs_dabuf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 xfs_fileoff_t bno; /* directory file offset */
608 xfs_dablk_t da; /* directory file offset */
609 int done; /* bunmap is finished */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000610 xfs_inode_t *dp;
611 int error;
612 xfs_mount_t *mp;
613 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
616 dp = args->dp;
617 mp = dp->i_mount;
618 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000619 da = xfs_dir2_db_to_da(mp, db);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /*
621 * Unmap the fsblock(s).
622 */
623 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
624 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000625 NULL, &done))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /*
627 * ENOSPC actually can happen if we're in a removename with
628 * no space reservation, and the resulting block removal
629 * would cause a bmap btree split or conversion from extents
630 * to btree. This can only happen for un-fragmented
631 * directory blocks, since you need to be punching out
632 * the middle of an extent.
633 * In this case we need to leave the block in the file,
634 * and not binval it.
635 * So the block has to be in a consistent empty state
636 * and appropriately logged.
637 * We don't free up the buffer, the caller can tell it
638 * hasn't happened since it got an error back.
639 */
640 return error;
641 }
642 ASSERT(done);
643 /*
644 * Invalidate the buffer from the transaction.
645 */
646 xfs_da_binval(tp, bp);
647 /*
648 * If it's not a data block, we're done.
649 */
650 if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
651 return 0;
652 /*
653 * If the block isn't the last one in the directory, we're done.
654 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000655 if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
657 bno = da;
658 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
659 /*
660 * This can't really happen unless there's kernel corruption.
661 */
662 return error;
663 }
664 if (db == mp->m_dirdatablk)
665 ASSERT(bno == 0);
666 else
667 ASSERT(bno > 0);
668 /*
669 * Set the size to the new last block.
670 */
671 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
672 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
673 return 0;
674}