blob: 29e091914df440cdf26fbcd5833b86d8e98f9fdb [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static int xfs_dir2_put_dirent64_direct(xfs_dir2_put_args_t *pa);
47static int xfs_dir2_put_dirent64_uio(xfs_dir2_put_args_t *pa);
48
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100049void
50xfs_dir_mount(
51 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100053 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,
150 char *name,
151 int namelen,
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 args.name = name;
167 args.namelen = namelen;
168 args.hashval = xfs_da_hashname(name, namelen);
169 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,
201 char *name,
202 int namelen,
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);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 args.name = name;
213 args.namelen = namelen;
214 args.hashval = xfs_da_hashname(name, namelen);
215 args.inumber = 0;
216 args.dp = dp;
217 args.firstblock = NULL;
218 args.flist = NULL;
219 args.total = 0;
220 args.whichfork = XFS_DATA_FORK;
221 args.trans = tp;
222 args.justcheck = args.addname = 0;
223 args.oknoent = 1;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
226 rval = xfs_dir2_sf_lookup(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000227 else if ((rval = xfs_dir2_isblock(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_block_lookup(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000231 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000233 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 rval = xfs_dir2_leaf_lookup(&args);
235 else
236 rval = xfs_dir2_node_lookup(&args);
237 if (rval == EEXIST)
238 rval = 0;
239 if (rval == 0)
240 *inum = args.inumber;
241 return rval;
242}
243
244/*
245 * Remove an entry from a directory.
246 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000247int
248xfs_dir_removename(
249 xfs_trans_t *tp,
250 xfs_inode_t *dp,
251 char *name,
252 int namelen,
253 xfs_ino_t ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 xfs_fsblock_t *first, /* bmap's firstblock */
255 xfs_bmap_free_t *flist, /* bmap's freeblock list */
256 xfs_extlen_t total) /* bmap's total block count */
257{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000258 xfs_da_args_t args;
259 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 int v; /* type-checking value */
261
262 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
263 XFS_STATS_INC(xs_dir_remove);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 args.name = name;
266 args.namelen = namelen;
267 args.hashval = xfs_da_hashname(name, namelen);
268 args.inumber = ino;
269 args.dp = dp;
270 args.firstblock = first;
271 args.flist = flist;
272 args.total = total;
273 args.whichfork = XFS_DATA_FORK;
274 args.trans = tp;
275 args.justcheck = args.addname = args.oknoent = 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
278 rval = xfs_dir2_sf_removename(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000279 else if ((rval = xfs_dir2_isblock(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_block_removename(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000283 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000285 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 rval = xfs_dir2_leaf_removename(&args);
287 else
288 rval = xfs_dir2_node_removename(&args);
289 return rval;
290}
291
292/*
293 * Read a directory.
294 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000295int
296xfs_dir_getdents(
297 xfs_trans_t *tp,
298 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 uio_t *uio, /* caller's buffer control */
300 int *eofp) /* out: eof reached */
301{
302 int alignment; /* alignment required for ABI */
303 xfs_dirent_t *dbp; /* malloc'ed buffer */
304 xfs_dir2_put_t put; /* entry formatting routine */
305 int rval; /* return value */
306 int v; /* type-checking value */
307
308 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
309 XFS_STATS_INC(xs_dir_getdents);
310 /*
311 * If our caller has given us a single contiguous aligned memory buffer,
312 * just work directly within that buffer. If it's in user memory,
313 * lock it down first.
314 */
315 alignment = sizeof(xfs_off_t) - 1;
316 if ((uio->uio_iovcnt == 1) &&
317 (((__psint_t)uio->uio_iov[0].iov_base & alignment) == 0) &&
318 ((uio->uio_iov[0].iov_len & alignment) == 0)) {
319 dbp = NULL;
320 put = xfs_dir2_put_dirent64_direct;
321 } else {
322 dbp = kmem_alloc(sizeof(*dbp) + MAXNAMELEN, KM_SLEEP);
323 put = xfs_dir2_put_dirent64_uio;
324 }
325
326 *eofp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
328 rval = xfs_dir2_sf_getdents(dp, uio, eofp, dbp, put);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000329 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 ;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000331 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 rval = xfs_dir2_block_getdents(tp, dp, uio, eofp, dbp, put);
333 else
334 rval = xfs_dir2_leaf_getdents(tp, dp, uio, eofp, dbp, put);
335 if (dbp != NULL)
336 kmem_free(dbp, sizeof(*dbp) + MAXNAMELEN);
337 return rval;
338}
339
340/*
341 * Replace the inode number of a directory entry.
342 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000343int
344xfs_dir_replace(
345 xfs_trans_t *tp,
346 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 char *name, /* name of entry to replace */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000348 int namelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 xfs_ino_t inum, /* new inode number */
350 xfs_fsblock_t *first, /* bmap's firstblock */
351 xfs_bmap_free_t *flist, /* bmap's freeblock list */
352 xfs_extlen_t total) /* bmap's total block count */
353{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000354 xfs_da_args_t args;
355 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int v; /* type-checking value */
357
358 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
359
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000360 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 args.name = name;
364 args.namelen = namelen;
365 args.hashval = xfs_da_hashname(name, namelen);
366 args.inumber = inum;
367 args.dp = dp;
368 args.firstblock = first;
369 args.flist = flist;
370 args.total = total;
371 args.whichfork = XFS_DATA_FORK;
372 args.trans = tp;
373 args.justcheck = args.addname = args.oknoent = 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
376 rval = xfs_dir2_sf_replace(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000377 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000379 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 rval = xfs_dir2_block_replace(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000381 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000383 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 rval = xfs_dir2_leaf_replace(&args);
385 else
386 rval = xfs_dir2_node_replace(&args);
387 return rval;
388}
389
390/*
391 * See if this entry can be added to the directory without allocating space.
392 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000393int
394xfs_dir_canenter(
395 xfs_trans_t *tp,
396 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 char *name, /* name of entry to add */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000398 int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000400 xfs_da_args_t args;
401 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int v; /* type-checking value */
403
404 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 args.name = name;
407 args.namelen = namelen;
408 args.hashval = xfs_da_hashname(name, namelen);
409 args.inumber = 0;
410 args.dp = dp;
411 args.firstblock = NULL;
412 args.flist = NULL;
413 args.total = 0;
414 args.whichfork = XFS_DATA_FORK;
415 args.trans = tp;
416 args.justcheck = args.addname = args.oknoent = 1;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
419 rval = xfs_dir2_sf_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000420 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000422 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 rval = xfs_dir2_block_addname(&args);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000424 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000426 else if (v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 rval = xfs_dir2_leaf_addname(&args);
428 else
429 rval = xfs_dir2_node_addname(&args);
430 return rval;
431}
432
433/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * Utility routines.
435 */
436
437/*
438 * Add a block to the directory.
439 * This routine is for data and free blocks, not leaf/node blocks
440 * which are handled by xfs_da_grow_inode.
441 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000442int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443xfs_dir2_grow_inode(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000444 xfs_da_args_t *args,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
446 xfs_dir2_db_t *dbp) /* out: block number added */
447{
448 xfs_fileoff_t bno; /* directory offset of new block */
449 int count; /* count of filesystem blocks */
450 xfs_inode_t *dp; /* incore directory inode */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000451 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int got; /* blocks actually mapped */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000453 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 xfs_bmbt_irec_t map; /* single structure for bmap */
455 int mapi; /* mapping index */
456 xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000457 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int nmap; /* number of bmap entries */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000459 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 xfs_dir2_trace_args_s("grow_inode", args, space);
462 dp = args->dp;
463 tp = args->trans;
464 mp = dp->i_mount;
465 /*
466 * Set lowest possible block in the space requested.
467 */
468 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
469 count = mp->m_dirblkfsbs;
470 /*
471 * Find the first hole for our block.
472 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000473 if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 nmap = 1;
476 ASSERT(args->firstblock != NULL);
477 /*
478 * Try mapping the new block contiguously (one extent).
479 */
480 if ((error = xfs_bmapi(tp, dp, bno, count,
481 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
482 args->firstblock, args->total, &map, &nmap,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000483 args->flist, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 ASSERT(nmap <= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (nmap == 1) {
487 mapp = &map;
488 mapi = 1;
489 }
490 /*
491 * Didn't work and this is a multiple-fsb directory block.
492 * Try again with contiguous flag turned on.
493 */
494 else if (nmap == 0 && count > 1) {
495 xfs_fileoff_t b; /* current file offset */
496
497 /*
498 * Space for maximum number of mappings.
499 */
500 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
501 /*
502 * Iterate until we get to the end of our block.
503 */
504 for (b = bno, mapi = 0; b < bno + count; ) {
505 int c; /* current fsb count */
506
507 /*
508 * Can't map more than MAX_NMAP at once.
509 */
510 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
511 c = (int)(bno + count - b);
512 if ((error = xfs_bmapi(tp, dp, b, c,
513 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
514 args->firstblock, args->total,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000515 &mapp[mapi], &nmap, args->flist,
516 NULL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 kmem_free(mapp, sizeof(*mapp) * count);
518 return error;
519 }
520 if (nmap < 1)
521 break;
522 /*
523 * Add this bunch into our table, go to the next offset.
524 */
525 mapi += nmap;
526 b = mapp[mapi - 1].br_startoff +
527 mapp[mapi - 1].br_blockcount;
528 }
529 }
530 /*
531 * Didn't work.
532 */
533 else {
534 mapi = 0;
535 mapp = NULL;
536 }
537 /*
538 * See how many fsb's we got.
539 */
540 for (i = 0, got = 0; i < mapi; i++)
541 got += mapp[i].br_blockcount;
542 /*
543 * Didn't get enough fsb's, or the first/last block's are wrong.
544 */
545 if (got != count || mapp[0].br_startoff != bno ||
546 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
547 bno + count) {
548 if (mapp != &map)
549 kmem_free(mapp, sizeof(*mapp) * count);
550 return XFS_ERROR(ENOSPC);
551 }
552 /*
553 * Done with the temporary mapping table.
554 */
555 if (mapp != &map)
556 kmem_free(mapp, sizeof(*mapp) * count);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000557 *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * Update file's size if this is the data space and it grew.
560 */
561 if (space == XFS_DIR2_DATA_SPACE) {
562 xfs_fsize_t size; /* directory file (data) size */
563
564 size = XFS_FSB_TO_B(mp, bno + count);
565 if (size > dp->i_d.di_size) {
566 dp->i_d.di_size = size;
567 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
568 }
569 }
570 return 0;
571}
572
573/*
574 * See if the directory is a single-block form directory.
575 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000576int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577xfs_dir2_isblock(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000578 xfs_trans_t *tp,
579 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int *vp) /* out: 1 is block, 0 is not block */
581{
582 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000583 xfs_mount_t *mp;
584 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000587 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
590 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
591 *vp = rval;
592 return 0;
593}
594
595/*
596 * See if the directory is a single-leaf form directory.
597 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000598int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599xfs_dir2_isleaf(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000600 xfs_trans_t *tp,
601 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int *vp) /* out: 1 is leaf, 0 is not leaf */
603{
604 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000605 xfs_mount_t *mp;
606 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000609 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
612 return 0;
613}
614
615/*
616 * Getdents put routine for 64-bit ABI, direct form.
617 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000618static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619xfs_dir2_put_dirent64_direct(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000620 xfs_dir2_put_args_t *pa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 xfs_dirent_t *idbp; /* dirent pointer */
623 iovec_t *iovp; /* io vector */
624 int namelen; /* entry name length */
625 int reclen; /* entry total length */
626 uio_t *uio; /* I/O control */
627
628 namelen = pa->namelen;
629 reclen = DIRENTSIZE(namelen);
630 uio = pa->uio;
631 /*
632 * Won't fit in the remaining space.
633 */
634 if (reclen > uio->uio_resid) {
635 pa->done = 0;
636 return 0;
637 }
638 iovp = uio->uio_iov;
639 idbp = (xfs_dirent_t *)iovp->iov_base;
640 iovp->iov_base = (char *)idbp + reclen;
641 iovp->iov_len -= reclen;
642 uio->uio_resid -= reclen;
643 idbp->d_reclen = reclen;
644 idbp->d_ino = pa->ino;
645 idbp->d_off = pa->cook;
646 idbp->d_name[namelen] = '\0';
647 pa->done = 1;
648 memcpy(idbp->d_name, pa->name, namelen);
649 return 0;
650}
651
652/*
653 * Getdents put routine for 64-bit ABI, uio form.
654 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000655static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656xfs_dir2_put_dirent64_uio(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000657 xfs_dir2_put_args_t *pa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 xfs_dirent_t *idbp; /* dirent pointer */
660 int namelen; /* entry name length */
661 int reclen; /* entry total length */
662 int rval; /* return value */
663 uio_t *uio; /* I/O control */
664
665 namelen = pa->namelen;
666 reclen = DIRENTSIZE(namelen);
667 uio = pa->uio;
668 /*
669 * Won't fit in the remaining space.
670 */
671 if (reclen > uio->uio_resid) {
672 pa->done = 0;
673 return 0;
674 }
675 idbp = pa->dbp;
676 idbp->d_reclen = reclen;
677 idbp->d_ino = pa->ino;
678 idbp->d_off = pa->cook;
679 idbp->d_name[namelen] = '\0';
680 memcpy(idbp->d_name, pa->name, namelen);
Vlad Apostolov70a50522006-11-11 18:04:41 +1100681 rval = xfs_uio_read((caddr_t)idbp, reclen, uio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 pa->done = (rval == 0);
683 return rval;
684}
685
686/*
687 * Remove the given block from the directory.
688 * This routine is used for data and free blocks, leaf/node are done
689 * by xfs_da_shrink_inode.
690 */
691int
692xfs_dir2_shrink_inode(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000693 xfs_da_args_t *args,
694 xfs_dir2_db_t db,
695 xfs_dabuf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 xfs_fileoff_t bno; /* directory file offset */
698 xfs_dablk_t da; /* directory file offset */
699 int done; /* bunmap is finished */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000700 xfs_inode_t *dp;
701 int error;
702 xfs_mount_t *mp;
703 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
706 dp = args->dp;
707 mp = dp->i_mount;
708 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000709 da = xfs_dir2_db_to_da(mp, db);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
711 * Unmap the fsblock(s).
712 */
713 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
714 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000715 NULL, &done))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /*
717 * ENOSPC actually can happen if we're in a removename with
718 * no space reservation, and the resulting block removal
719 * would cause a bmap btree split or conversion from extents
720 * to btree. This can only happen for un-fragmented
721 * directory blocks, since you need to be punching out
722 * the middle of an extent.
723 * In this case we need to leave the block in the file,
724 * and not binval it.
725 * So the block has to be in a consistent empty state
726 * and appropriately logged.
727 * We don't free up the buffer, the caller can tell it
728 * hasn't happened since it got an error back.
729 */
730 return error;
731 }
732 ASSERT(done);
733 /*
734 * Invalidate the buffer from the transaction.
735 */
736 xfs_da_binval(tp, bp);
737 /*
738 * If it's not a data block, we're done.
739 */
740 if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
741 return 0;
742 /*
743 * If the block isn't the last one in the directory, we're done.
744 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000745 if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
747 bno = da;
748 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
749 /*
750 * This can't really happen unless there's kernel corruption.
751 */
752 return error;
753 }
754 if (db == mp->m_dirdatablk)
755 ASSERT(bno == 0);
756 else
757 ASSERT(bno > 0);
758 /*
759 * Set the size to the new last block.
760 */
761 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
762 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
763 return 0;
764}