blob: 7cb26529766b7759c2ed743273d0942d71765a15 [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;
68}
69
70/*
71 * Return 1 if directory contains only "." and "..".
72 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100073int
74xfs_dir_isempty(
75 xfs_inode_t *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100077 xfs_dir2_sf_t *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100080 if (dp->i_d.di_size == 0) /* might happen during shutdown. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
83 return 0;
84 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
85 return !sfp->hdr.count;
86}
87
88/*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100089 * Validate a given inode number.
90 */
91int
92xfs_dir_ino_validate(
93 xfs_mount_t *mp,
94 xfs_ino_t ino)
95{
96 xfs_agblock_t agblkno;
97 xfs_agino_t agino;
98 xfs_agnumber_t agno;
99 int ino_ok;
100 int ioff;
101
102 agno = XFS_INO_TO_AGNO(mp, ino);
103 agblkno = XFS_INO_TO_AGBNO(mp, ino);
104 ioff = XFS_INO_TO_OFFSET(mp, ino);
105 agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
106 ino_ok =
107 agno < mp->m_sb.sb_agcount &&
108 agblkno < mp->m_sb.sb_agblocks &&
109 agblkno != 0 &&
110 ioff < (1 << mp->m_sb.sb_inopblog) &&
111 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
112 if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
113 XFS_RANDOM_DIR_INO_VALIDATE))) {
114 xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx",
115 (unsigned long long) ino);
116 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
117 return XFS_ERROR(EFSCORRUPTED);
118 }
119 return 0;
120}
121
122/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * Initialize a directory with its "." and ".." entries.
124 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000125int
126xfs_dir_init(
127 xfs_trans_t *tp,
128 xfs_inode_t *dp,
129 xfs_inode_t *pdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000131 xfs_da_args_t args;
132 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 memset((char *)&args, 0, sizeof(args));
135 args.dp = dp;
136 args.trans = tp;
137 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000138 if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return xfs_dir2_sf_create(&args, pdp->i_ino);
141}
142
143/*
144 Enter a name in a directory.
145 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000146int
147xfs_dir_createname(
148 xfs_trans_t *tp,
149 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000150 struct xfs_name *name,
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
Barry Naujok556b8b12008-04-10 12:22:07 +1000165 args.name = name->name;
166 args.namelen = name->len;
167 args.hashval = xfs_da_hashname(name->name, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 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,
Barry Naujok556b8b12008-04-10 12:22:07 +1000200 struct xfs_name *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 xfs_ino_t *inum) /* out: inode number */
202{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000203 xfs_da_args_t args;
204 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int v; /* type-checking value */
206
207 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
208 XFS_STATS_INC(xs_dir_lookup);
Barry Naujok556b8b12008-04-10 12:22:07 +1000209 memset(&args, 0, sizeof(xfs_da_args_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Barry Naujok556b8b12008-04-10 12:22:07 +1000211 args.name = name->name;
212 args.namelen = name->len;
213 args.hashval = xfs_da_hashname(name->name, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 args.dp = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 args.whichfork = XFS_DATA_FORK;
216 args.trans = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 args.oknoent = 1;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
220 rval = xfs_dir2_sf_lookup(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000221 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000223 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 rval = xfs_dir2_block_lookup(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000225 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000227 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 rval = xfs_dir2_leaf_lookup(&args);
229 else
230 rval = xfs_dir2_node_lookup(&args);
231 if (rval == EEXIST)
232 rval = 0;
233 if (rval == 0)
234 *inum = args.inumber;
235 return rval;
236}
237
238/*
239 * Remove an entry from a directory.
240 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000241int
242xfs_dir_removename(
243 xfs_trans_t *tp,
244 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000245 struct xfs_name *name,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000246 xfs_ino_t ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 xfs_fsblock_t *first, /* bmap's firstblock */
248 xfs_bmap_free_t *flist, /* bmap's freeblock list */
249 xfs_extlen_t total) /* bmap's total block count */
250{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000251 xfs_da_args_t args;
252 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int v; /* type-checking value */
254
255 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
256 XFS_STATS_INC(xs_dir_remove);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000257
Barry Naujok556b8b12008-04-10 12:22:07 +1000258 args.name = name->name;
259 args.namelen = name->len;
260 args.hashval = xfs_da_hashname(name->name, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 args.inumber = ino;
262 args.dp = dp;
263 args.firstblock = first;
264 args.flist = flist;
265 args.total = total;
266 args.whichfork = XFS_DATA_FORK;
267 args.trans = tp;
268 args.justcheck = args.addname = args.oknoent = 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
271 rval = xfs_dir2_sf_removename(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000272 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000274 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 rval = xfs_dir2_block_removename(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000276 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000278 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 rval = xfs_dir2_leaf_removename(&args);
280 else
281 rval = xfs_dir2_node_removename(&args);
282 return rval;
283}
284
285/*
286 * Read a directory.
287 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000288int
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000289xfs_readdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000290 xfs_inode_t *dp,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000291 void *dirent,
292 size_t bufsize,
293 xfs_off_t *offset,
294 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int rval; /* return value */
297 int v; /* type-checking value */
298
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100299 xfs_itrace_entry(dp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000300
301 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
302 return XFS_ERROR(EIO);
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
305 XFS_STATS_INC(xs_dir_getdents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000308 rval = xfs_dir2_sf_getdents(dp, dirent, offset, filldir);
309 else if ((rval = xfs_dir2_isblock(NULL, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 ;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000311 else if (v)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000312 rval = xfs_dir2_block_getdents(dp, dirent, offset, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 else
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000314 rval = xfs_dir2_leaf_getdents(dp, dirent, bufsize, offset,
315 filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return rval;
317}
318
319/*
320 * Replace the inode number of a directory entry.
321 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000322int
323xfs_dir_replace(
324 xfs_trans_t *tp,
325 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000326 struct xfs_name *name, /* name of entry to replace */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 xfs_ino_t inum, /* new inode number */
328 xfs_fsblock_t *first, /* bmap's firstblock */
329 xfs_bmap_free_t *flist, /* bmap's freeblock list */
330 xfs_extlen_t total) /* bmap's total block count */
331{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000332 xfs_da_args_t args;
333 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int v; /* type-checking value */
335
336 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
337
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000338 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000340
Barry Naujok556b8b12008-04-10 12:22:07 +1000341 args.name = name->name;
342 args.namelen = name->len;
343 args.hashval = xfs_da_hashname(name->name, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 args.inumber = inum;
345 args.dp = dp;
346 args.firstblock = first;
347 args.flist = flist;
348 args.total = total;
349 args.whichfork = XFS_DATA_FORK;
350 args.trans = tp;
351 args.justcheck = args.addname = args.oknoent = 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
354 rval = xfs_dir2_sf_replace(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000355 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000357 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 rval = xfs_dir2_block_replace(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000359 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000361 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 rval = xfs_dir2_leaf_replace(&args);
363 else
364 rval = xfs_dir2_node_replace(&args);
365 return rval;
366}
367
368/*
369 * See if this entry can be added to the directory without allocating space.
Barry Naujok556b8b12008-04-10 12:22:07 +1000370 * First checks that the caller couldn't reserve enough space (resblks = 0).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000372int
373xfs_dir_canenter(
374 xfs_trans_t *tp,
375 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000376 struct xfs_name *name, /* name of entry to add */
377 uint resblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000379 xfs_da_args_t args;
380 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int v; /* type-checking value */
382
Barry Naujok556b8b12008-04-10 12:22:07 +1000383 if (resblks)
384 return 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000385
Barry Naujok556b8b12008-04-10 12:22:07 +1000386 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
387 memset(&args, 0, sizeof(xfs_da_args_t));
388
389 args.name = name->name;
390 args.namelen = name->len;
391 args.hashval = xfs_da_hashname(name->name, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 args.dp = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 args.whichfork = XFS_DATA_FORK;
394 args.trans = tp;
395 args.justcheck = args.addname = args.oknoent = 1;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
398 rval = xfs_dir2_sf_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000399 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000401 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 rval = xfs_dir2_block_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000403 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000405 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 rval = xfs_dir2_leaf_addname(&args);
407 else
408 rval = xfs_dir2_node_addname(&args);
409 return rval;
410}
411
412/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * Utility routines.
414 */
415
416/*
417 * Add a block to the directory.
418 * This routine is for data and free blocks, not leaf/node blocks
419 * which are handled by xfs_da_grow_inode.
420 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000421int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422xfs_dir2_grow_inode(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000423 xfs_da_args_t *args,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
425 xfs_dir2_db_t *dbp) /* out: block number added */
426{
427 xfs_fileoff_t bno; /* directory offset of new block */
428 int count; /* count of filesystem blocks */
429 xfs_inode_t *dp; /* incore directory inode */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000430 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int got; /* blocks actually mapped */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000432 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 xfs_bmbt_irec_t map; /* single structure for bmap */
434 int mapi; /* mapping index */
435 xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000436 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int nmap; /* number of bmap entries */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000438 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 xfs_dir2_trace_args_s("grow_inode", args, space);
441 dp = args->dp;
442 tp = args->trans;
443 mp = dp->i_mount;
444 /*
445 * Set lowest possible block in the space requested.
446 */
447 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
448 count = mp->m_dirblkfsbs;
449 /*
450 * Find the first hole for our block.
451 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000452 if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 nmap = 1;
455 ASSERT(args->firstblock != NULL);
456 /*
457 * Try mapping the new block contiguously (one extent).
458 */
459 if ((error = xfs_bmapi(tp, dp, bno, count,
460 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
461 args->firstblock, args->total, &map, &nmap,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000462 args->flist, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ASSERT(nmap <= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (nmap == 1) {
466 mapp = &map;
467 mapi = 1;
468 }
469 /*
470 * Didn't work and this is a multiple-fsb directory block.
471 * Try again with contiguous flag turned on.
472 */
473 else if (nmap == 0 && count > 1) {
474 xfs_fileoff_t b; /* current file offset */
475
476 /*
477 * Space for maximum number of mappings.
478 */
479 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
480 /*
481 * Iterate until we get to the end of our block.
482 */
483 for (b = bno, mapi = 0; b < bno + count; ) {
484 int c; /* current fsb count */
485
486 /*
487 * Can't map more than MAX_NMAP at once.
488 */
489 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
490 c = (int)(bno + count - b);
491 if ((error = xfs_bmapi(tp, dp, b, c,
492 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
493 args->firstblock, args->total,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000494 &mapp[mapi], &nmap, args->flist,
495 NULL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 kmem_free(mapp, sizeof(*mapp) * count);
497 return error;
498 }
499 if (nmap < 1)
500 break;
501 /*
502 * Add this bunch into our table, go to the next offset.
503 */
504 mapi += nmap;
505 b = mapp[mapi - 1].br_startoff +
506 mapp[mapi - 1].br_blockcount;
507 }
508 }
509 /*
510 * Didn't work.
511 */
512 else {
513 mapi = 0;
514 mapp = NULL;
515 }
516 /*
517 * See how many fsb's we got.
518 */
519 for (i = 0, got = 0; i < mapi; i++)
520 got += mapp[i].br_blockcount;
521 /*
522 * Didn't get enough fsb's, or the first/last block's are wrong.
523 */
524 if (got != count || mapp[0].br_startoff != bno ||
525 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
526 bno + count) {
527 if (mapp != &map)
528 kmem_free(mapp, sizeof(*mapp) * count);
529 return XFS_ERROR(ENOSPC);
530 }
531 /*
532 * Done with the temporary mapping table.
533 */
534 if (mapp != &map)
535 kmem_free(mapp, sizeof(*mapp) * count);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000536 *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * Update file's size if this is the data space and it grew.
539 */
540 if (space == XFS_DIR2_DATA_SPACE) {
541 xfs_fsize_t size; /* directory file (data) size */
542
543 size = XFS_FSB_TO_B(mp, bno + count);
544 if (size > dp->i_d.di_size) {
545 dp->i_d.di_size = size;
546 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
547 }
548 }
549 return 0;
550}
551
552/*
553 * See if the directory is a single-block form directory.
554 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000555int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556xfs_dir2_isblock(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000557 xfs_trans_t *tp,
558 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int *vp) /* out: 1 is block, 0 is not block */
560{
561 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000562 xfs_mount_t *mp;
563 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000566 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
569 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
570 *vp = rval;
571 return 0;
572}
573
574/*
575 * See if the directory is a single-leaf form directory.
576 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000577int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578xfs_dir2_isleaf(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000579 xfs_trans_t *tp,
580 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 int *vp) /* out: 1 is leaf, 0 is not leaf */
582{
583 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000584 xfs_mount_t *mp;
585 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000588 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
591 return 0;
592}
593
594/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * Remove the given block from the directory.
596 * This routine is used for data and free blocks, leaf/node are done
597 * by xfs_da_shrink_inode.
598 */
599int
600xfs_dir2_shrink_inode(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000601 xfs_da_args_t *args,
602 xfs_dir2_db_t db,
603 xfs_dabuf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 xfs_fileoff_t bno; /* directory file offset */
606 xfs_dablk_t da; /* directory file offset */
607 int done; /* bunmap is finished */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000608 xfs_inode_t *dp;
609 int error;
610 xfs_mount_t *mp;
611 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
614 dp = args->dp;
615 mp = dp->i_mount;
616 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000617 da = xfs_dir2_db_to_da(mp, db);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /*
619 * Unmap the fsblock(s).
620 */
621 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
622 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000623 NULL, &done))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /*
625 * ENOSPC actually can happen if we're in a removename with
626 * no space reservation, and the resulting block removal
627 * would cause a bmap btree split or conversion from extents
628 * to btree. This can only happen for un-fragmented
629 * directory blocks, since you need to be punching out
630 * the middle of an extent.
631 * In this case we need to leave the block in the file,
632 * and not binval it.
633 * So the block has to be in a consistent empty state
634 * and appropriately logged.
635 * We don't free up the buffer, the caller can tell it
636 * hasn't happened since it got an error back.
637 */
638 return error;
639 }
640 ASSERT(done);
641 /*
642 * Invalidate the buffer from the transaction.
643 */
644 xfs_da_binval(tp, bp);
645 /*
646 * If it's not a data block, we're done.
647 */
648 if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
649 return 0;
650 /*
651 * If the block isn't the last one in the directory, we're done.
652 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000653 if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
655 bno = da;
656 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
657 /*
658 * This can't really happen unless there's kernel corruption.
659 */
660 return error;
661 }
662 if (db == mp->m_dirdatablk)
663 ASSERT(bno == 0);
664 else
665 ASSERT(bno > 0);
666 /*
667 * Set the size to the new last block.
668 */
669 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
670 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
671 return 0;
672}