blob: 3387acd3e4719107962a66503af744efbb596379 [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
Barry Naujok6a178102008-05-21 16:42:05 +100049extern const struct xfs_nameops xfs_default_nameops;
50
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100051void
52xfs_dir_mount(
53 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Eric Sandeen62118702008-03-06 13:44:28 +110055 ASSERT(xfs_sb_version_hasdirv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 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;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100060 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 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;
Barry Naujok5163f952008-05-21 16:41:01 +100070 mp->m_dirnameops = &xfs_default_nameops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73/*
74 * Return 1 if directory contains only "." and "..".
75 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100076int
77xfs_dir_isempty(
78 xfs_inode_t *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100080 xfs_dir2_sf_t *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100083 if (dp->i_d.di_size == 0) /* might happen during shutdown. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 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
91/*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100092 * 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
125/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * Initialize a directory with its "." and ".." entries.
127 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000128int
129xfs_dir_init(
130 xfs_trans_t *tp,
131 xfs_inode_t *dp,
132 xfs_inode_t *pdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000134 xfs_da_args_t args;
135 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
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);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000141 if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return xfs_dir2_sf_create(&args, pdp->i_ino);
144}
145
146/*
147 Enter a name in a directory.
148 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000149int
150xfs_dir_createname(
151 xfs_trans_t *tp,
152 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000153 struct xfs_name *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 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{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000159 xfs_da_args_t args;
160 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int v; /* type-checking value */
162
163 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000164 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 XFS_STATS_INC(xs_dir_create);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000167
Barry Naujok556b8b12008-04-10 12:22:07 +1000168 args.name = name->name;
169 args.namelen = name->len;
Barry Naujok5163f952008-05-21 16:41:01 +1000170 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 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;
Barry Naujok6a178102008-05-21 16:42:05 +1000178 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
181 rval = xfs_dir2_sf_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000182 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000184 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 rval = xfs_dir2_block_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000186 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000188 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 rval = xfs_dir2_leaf_addname(&args);
190 else
191 rval = xfs_dir2_node_addname(&args);
192 return rval;
193}
194
195/*
196 * Lookup a name in a directory, give back the inode number.
197 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000198int
199xfs_dir_lookup(
200 xfs_trans_t *tp,
201 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000202 struct xfs_name *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 xfs_ino_t *inum) /* out: inode number */
204{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000205 xfs_da_args_t args;
206 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int v; /* type-checking value */
208
209 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
210 XFS_STATS_INC(xs_dir_lookup);
Barry Naujok556b8b12008-04-10 12:22:07 +1000211 memset(&args, 0, sizeof(xfs_da_args_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Barry Naujok556b8b12008-04-10 12:22:07 +1000213 args.name = name->name;
214 args.namelen = name->len;
Barry Naujok5163f952008-05-21 16:41:01 +1000215 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 args.dp = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 args.whichfork = XFS_DATA_FORK;
218 args.trans = tp;
Barry Naujok6a178102008-05-21 16:42:05 +1000219 args.op_flags = XFS_DA_OP_OKNOENT;
Barry Naujok5163f952008-05-21 16:41:01 +1000220 args.cmpresult = XFS_CMP_DIFFERENT;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
223 rval = xfs_dir2_sf_lookup(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000224 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000226 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 rval = xfs_dir2_block_lookup(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000228 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000230 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 rval = xfs_dir2_leaf_lookup(&args);
232 else
233 rval = xfs_dir2_node_lookup(&args);
234 if (rval == EEXIST)
235 rval = 0;
236 if (rval == 0)
237 *inum = args.inumber;
238 return rval;
239}
240
241/*
242 * Remove an entry from a directory.
243 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000244int
245xfs_dir_removename(
246 xfs_trans_t *tp,
247 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000248 struct xfs_name *name,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000249 xfs_ino_t ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 xfs_fsblock_t *first, /* bmap's firstblock */
251 xfs_bmap_free_t *flist, /* bmap's freeblock list */
252 xfs_extlen_t total) /* bmap's total block count */
253{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000254 xfs_da_args_t args;
255 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int v; /* type-checking value */
257
258 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
259 XFS_STATS_INC(xs_dir_remove);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000260
Barry Naujok556b8b12008-04-10 12:22:07 +1000261 args.name = name->name;
262 args.namelen = name->len;
Barry Naujok5163f952008-05-21 16:41:01 +1000263 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 args.inumber = ino;
265 args.dp = dp;
266 args.firstblock = first;
267 args.flist = flist;
268 args.total = total;
269 args.whichfork = XFS_DATA_FORK;
270 args.trans = tp;
Barry Naujok6a178102008-05-21 16:42:05 +1000271 args.op_flags = 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
274 rval = xfs_dir2_sf_removename(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000275 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000277 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 rval = xfs_dir2_block_removename(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000279 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000281 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 rval = xfs_dir2_leaf_removename(&args);
283 else
284 rval = xfs_dir2_node_removename(&args);
285 return rval;
286}
287
288/*
289 * Read a directory.
290 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000291int
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000292xfs_readdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000293 xfs_inode_t *dp,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000294 void *dirent,
295 size_t bufsize,
296 xfs_off_t *offset,
297 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int rval; /* return value */
300 int v; /* type-checking value */
301
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100302 xfs_itrace_entry(dp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000303
304 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
305 return XFS_ERROR(EIO);
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
308 XFS_STATS_INC(xs_dir_getdents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000311 rval = xfs_dir2_sf_getdents(dp, dirent, offset, filldir);
312 else if ((rval = xfs_dir2_isblock(NULL, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 ;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000314 else if (v)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000315 rval = xfs_dir2_block_getdents(dp, dirent, offset, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 else
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000317 rval = xfs_dir2_leaf_getdents(dp, dirent, bufsize, offset,
318 filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return rval;
320}
321
322/*
323 * Replace the inode number of a directory entry.
324 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000325int
326xfs_dir_replace(
327 xfs_trans_t *tp,
328 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000329 struct xfs_name *name, /* name of entry to replace */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 xfs_ino_t inum, /* new inode number */
331 xfs_fsblock_t *first, /* bmap's firstblock */
332 xfs_bmap_free_t *flist, /* bmap's freeblock list */
333 xfs_extlen_t total) /* bmap's total block count */
334{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000335 xfs_da_args_t args;
336 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int v; /* type-checking value */
338
339 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
340
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000341 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000343
Barry Naujok556b8b12008-04-10 12:22:07 +1000344 args.name = name->name;
345 args.namelen = name->len;
Barry Naujok5163f952008-05-21 16:41:01 +1000346 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 args.inumber = inum;
348 args.dp = dp;
349 args.firstblock = first;
350 args.flist = flist;
351 args.total = total;
352 args.whichfork = XFS_DATA_FORK;
353 args.trans = tp;
Barry Naujok6a178102008-05-21 16:42:05 +1000354 args.op_flags = 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
357 rval = xfs_dir2_sf_replace(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000358 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000360 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 rval = xfs_dir2_block_replace(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000362 else if ((rval = xfs_dir2_isleaf(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_leaf_replace(&args);
366 else
367 rval = xfs_dir2_node_replace(&args);
368 return rval;
369}
370
371/*
372 * See if this entry can be added to the directory without allocating space.
Barry Naujok556b8b12008-04-10 12:22:07 +1000373 * First checks that the caller couldn't reserve enough space (resblks = 0).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000375int
376xfs_dir_canenter(
377 xfs_trans_t *tp,
378 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000379 struct xfs_name *name, /* name of entry to add */
380 uint resblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000382 xfs_da_args_t args;
383 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 int v; /* type-checking value */
385
Barry Naujok556b8b12008-04-10 12:22:07 +1000386 if (resblks)
387 return 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000388
Barry Naujok556b8b12008-04-10 12:22:07 +1000389 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
390 memset(&args, 0, sizeof(xfs_da_args_t));
391
392 args.name = name->name;
393 args.namelen = name->len;
Barry Naujok5163f952008-05-21 16:41:01 +1000394 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 args.dp = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 args.whichfork = XFS_DATA_FORK;
397 args.trans = tp;
Barry Naujok6a178102008-05-21 16:42:05 +1000398 args.op_flags = XFS_DA_OP_JUSTCHECK | XFS_DA_OP_ADDNAME |
399 XFS_DA_OP_OKNOENT;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
402 rval = xfs_dir2_sf_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000403 else if ((rval = xfs_dir2_isblock(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_block_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000407 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000409 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 rval = xfs_dir2_leaf_addname(&args);
411 else
412 rval = xfs_dir2_node_addname(&args);
413 return rval;
414}
415
416/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * Utility routines.
418 */
419
420/*
421 * Add a block to the directory.
422 * This routine is for data and free blocks, not leaf/node blocks
423 * which are handled by xfs_da_grow_inode.
424 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000425int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426xfs_dir2_grow_inode(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000427 xfs_da_args_t *args,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
429 xfs_dir2_db_t *dbp) /* out: block number added */
430{
431 xfs_fileoff_t bno; /* directory offset of new block */
432 int count; /* count of filesystem blocks */
433 xfs_inode_t *dp; /* incore directory inode */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000434 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int got; /* blocks actually mapped */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000436 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 xfs_bmbt_irec_t map; /* single structure for bmap */
438 int mapi; /* mapping index */
439 xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000440 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int nmap; /* number of bmap entries */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000442 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 xfs_dir2_trace_args_s("grow_inode", args, space);
445 dp = args->dp;
446 tp = args->trans;
447 mp = dp->i_mount;
448 /*
449 * Set lowest possible block in the space requested.
450 */
451 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
452 count = mp->m_dirblkfsbs;
453 /*
454 * Find the first hole for our block.
455 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000456 if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 nmap = 1;
459 ASSERT(args->firstblock != NULL);
460 /*
461 * Try mapping the new block contiguously (one extent).
462 */
463 if ((error = xfs_bmapi(tp, dp, bno, count,
464 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
465 args->firstblock, args->total, &map, &nmap,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000466 args->flist, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 ASSERT(nmap <= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (nmap == 1) {
470 mapp = &map;
471 mapi = 1;
472 }
473 /*
474 * Didn't work and this is a multiple-fsb directory block.
475 * Try again with contiguous flag turned on.
476 */
477 else if (nmap == 0 && count > 1) {
478 xfs_fileoff_t b; /* current file offset */
479
480 /*
481 * Space for maximum number of mappings.
482 */
483 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
484 /*
485 * Iterate until we get to the end of our block.
486 */
487 for (b = bno, mapi = 0; b < bno + count; ) {
488 int c; /* current fsb count */
489
490 /*
491 * Can't map more than MAX_NMAP at once.
492 */
493 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
494 c = (int)(bno + count - b);
495 if ((error = xfs_bmapi(tp, dp, b, c,
496 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
497 args->firstblock, args->total,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000498 &mapp[mapi], &nmap, args->flist,
499 NULL))) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000500 kmem_free(mapp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return error;
502 }
503 if (nmap < 1)
504 break;
505 /*
506 * Add this bunch into our table, go to the next offset.
507 */
508 mapi += nmap;
509 b = mapp[mapi - 1].br_startoff +
510 mapp[mapi - 1].br_blockcount;
511 }
512 }
513 /*
514 * Didn't work.
515 */
516 else {
517 mapi = 0;
518 mapp = NULL;
519 }
520 /*
521 * See how many fsb's we got.
522 */
523 for (i = 0, got = 0; i < mapi; i++)
524 got += mapp[i].br_blockcount;
525 /*
526 * Didn't get enough fsb's, or the first/last block's are wrong.
527 */
528 if (got != count || mapp[0].br_startoff != bno ||
529 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
530 bno + count) {
531 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000532 kmem_free(mapp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return XFS_ERROR(ENOSPC);
534 }
535 /*
536 * Done with the temporary mapping table.
537 */
538 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000539 kmem_free(mapp);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000540 *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /*
542 * Update file's size if this is the data space and it grew.
543 */
544 if (space == XFS_DIR2_DATA_SPACE) {
545 xfs_fsize_t size; /* directory file (data) size */
546
547 size = XFS_FSB_TO_B(mp, bno + count);
548 if (size > dp->i_d.di_size) {
549 dp->i_d.di_size = size;
550 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
551 }
552 }
553 return 0;
554}
555
556/*
557 * See if the directory is a single-block form directory.
558 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000559int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560xfs_dir2_isblock(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000561 xfs_trans_t *tp,
562 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 int *vp) /* out: 1 is block, 0 is not block */
564{
565 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000566 xfs_mount_t *mp;
567 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000570 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
573 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
574 *vp = rval;
575 return 0;
576}
577
578/*
579 * See if the directory is a single-leaf form directory.
580 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000581int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582xfs_dir2_isleaf(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000583 xfs_trans_t *tp,
584 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int *vp) /* out: 1 is leaf, 0 is not leaf */
586{
587 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000588 xfs_mount_t *mp;
589 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000592 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
595 return 0;
596}
597
598/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * Remove the given block from the directory.
600 * This routine is used for data and free blocks, leaf/node are done
601 * by xfs_da_shrink_inode.
602 */
603int
604xfs_dir2_shrink_inode(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000605 xfs_da_args_t *args,
606 xfs_dir2_db_t db,
607 xfs_dabuf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 xfs_fileoff_t bno; /* directory file offset */
610 xfs_dablk_t da; /* directory file offset */
611 int done; /* bunmap is finished */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000612 xfs_inode_t *dp;
613 int error;
614 xfs_mount_t *mp;
615 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
618 dp = args->dp;
619 mp = dp->i_mount;
620 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000621 da = xfs_dir2_db_to_da(mp, db);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
623 * Unmap the fsblock(s).
624 */
625 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
626 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000627 NULL, &done))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /*
629 * ENOSPC actually can happen if we're in a removename with
630 * no space reservation, and the resulting block removal
631 * would cause a bmap btree split or conversion from extents
632 * to btree. This can only happen for un-fragmented
633 * directory blocks, since you need to be punching out
634 * the middle of an extent.
635 * In this case we need to leave the block in the file,
636 * and not binval it.
637 * So the block has to be in a consistent empty state
638 * and appropriately logged.
639 * We don't free up the buffer, the caller can tell it
640 * hasn't happened since it got an error back.
641 */
642 return error;
643 }
644 ASSERT(done);
645 /*
646 * Invalidate the buffer from the transaction.
647 */
648 xfs_da_binval(tp, bp);
649 /*
650 * If it's not a data block, we're done.
651 */
652 if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
653 return 0;
654 /*
655 * If the block isn't the last one in the directory, we're done.
656 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000657 if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return 0;
659 bno = da;
660 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
661 /*
662 * This can't really happen unless there's kernel corruption.
663 */
664 return error;
665 }
666 if (db == mp->m_dirdatablk)
667 ASSERT(bno == 0);
668 else
669 ASSERT(bno > 0);
670 /*
671 * Set the size to the new last block.
672 */
673 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
674 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
675 return 0;
676}