blob: 77d6882c46b05e80ccc76801e0b9ed891ebfc5a7 [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"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110030#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100033#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020034#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110037#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Dave Chinner0cb97762013-08-12 20:50:09 +100039struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2, XFS_DIR3_FT_DIR };
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Barry Naujok189f4bf2008-05-21 16:58:55 +100042/*
43 * ASCII case-insensitive (ie. A-Z) support for directories that was
44 * used in IRIX.
45 */
46STATIC xfs_dahash_t
47xfs_ascii_ci_hashname(
48 struct xfs_name *name)
49{
50 xfs_dahash_t hash;
51 int i;
52
53 for (i = 0, hash = 0; i < name->len; i++)
54 hash = tolower(name->name[i]) ^ rol32(hash, 7);
55
56 return hash;
57}
58
59STATIC enum xfs_dacmp
60xfs_ascii_ci_compname(
61 struct xfs_da_args *args,
Dave Chinner2bc75422010-01-20 10:47:17 +110062 const unsigned char *name,
63 int len)
Barry Naujok189f4bf2008-05-21 16:58:55 +100064{
65 enum xfs_dacmp result;
66 int i;
67
68 if (args->namelen != len)
69 return XFS_CMP_DIFFERENT;
70
71 result = XFS_CMP_EXACT;
72 for (i = 0; i < len; i++) {
73 if (args->name[i] == name[i])
74 continue;
75 if (tolower(args->name[i]) != tolower(name[i]))
76 return XFS_CMP_DIFFERENT;
77 result = XFS_CMP_CASE;
78 }
79
80 return result;
81}
82
83static struct xfs_nameops xfs_ascii_ci_nameops = {
84 .hashname = xfs_ascii_ci_hashname,
85 .compname = xfs_ascii_ci_compname,
86};
87
Dave Chinner0650b552014-06-06 15:01:58 +100088int
89xfs_da_mount(
90 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Dave Chinner0650b552014-06-06 15:01:58 +100092 struct xfs_da_geometry *dageo;
93 int nodehdr_size;
Dave Chinner37804372013-08-26 14:13:30 +100094
95
Eric Sandeen62118702008-03-06 13:44:28 +110096 ASSERT(xfs_sb_version_hasdirv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
98 XFS_MAX_BLOCKSIZE);
Dave Chinner4bceb182013-10-29 22:11:51 +110099
100 mp->m_dir_inode_ops = xfs_dir_get_ops(mp, NULL);
101 mp->m_nondir_inode_ops = xfs_nondir_get_ops(mp, NULL);
102
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100103 nodehdr_size = mp->m_dir_inode_ops->node_hdr_size;
Dave Chinner0650b552014-06-06 15:01:58 +1000104 mp->m_dir_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
105 KM_SLEEP | KM_MAYFAIL);
106 mp->m_attr_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
107 KM_SLEEP | KM_MAYFAIL);
108 if (!mp->m_dir_geo || !mp->m_attr_geo) {
109 kmem_free(mp->m_dir_geo);
110 kmem_free(mp->m_attr_geo);
111 return ENOMEM;
112 }
Dave Chinner37804372013-08-26 14:13:30 +1000113
Dave Chinner0650b552014-06-06 15:01:58 +1000114 /* set up directory geometry */
115 dageo = mp->m_dir_geo;
116 dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
117 dageo->fsblog = mp->m_sb.sb_blocklog;
118 dageo->blksize = 1 << dageo->blklog;
119 dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
Dave Chinner30028032014-06-06 15:08:18 +1000120
121 /*
122 * Now we've set up the block conversion variables, we can calculate the
123 * segment block constants using the geometry structure.
124 */
125 dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
126 dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
127 dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
Dave Chinner0650b552014-06-06 15:01:58 +1000128 dageo->node_ents = (dageo->blksize - nodehdr_size) /
129 (uint)sizeof(xfs_da_node_entry_t);
130 dageo->magicpct = (dageo->blksize * 37) / 100;
131
132 /* set up attribute geometry - single fsb only */
133 dageo = mp->m_attr_geo;
134 dageo->blklog = mp->m_sb.sb_blocklog;
135 dageo->fsblog = mp->m_sb.sb_blocklog;
136 dageo->blksize = 1 << dageo->blklog;
137 dageo->fsbcount = 1;
138 dageo->node_ents = (dageo->blksize - nodehdr_size) /
139 (uint)sizeof(xfs_da_node_entry_t);
140 dageo->magicpct = (dageo->blksize * 37) / 100;
141
Barry Naujok189f4bf2008-05-21 16:58:55 +1000142 if (xfs_sb_version_hasasciici(&mp->m_sb))
143 mp->m_dirnameops = &xfs_ascii_ci_nameops;
144 else
145 mp->m_dirnameops = &xfs_default_nameops;
Dave Chinner32c54832013-10-29 22:11:46 +1100146
Dave Chinner0650b552014-06-06 15:01:58 +1000147 /* XXX: these are to be removed as code is converted to use geo */
148 mp->m_dirblksize = mp->m_dir_geo->blksize;
149 mp->m_dirblkfsbs = mp->m_dir_geo->fsbcount;
Dave Chinner0650b552014-06-06 15:01:58 +1000150 mp->m_dir_node_ents = mp->m_dir_geo->node_ents;
151 mp->m_dir_magicpct = mp->m_dir_geo->magicpct;
152 mp->m_attr_node_ents = mp->m_attr_geo->node_ents;
153 mp->m_attr_magicpct = mp->m_attr_geo->magicpct;
154 return 0;
155}
156
157void
158xfs_da_unmount(
159 struct xfs_mount *mp)
160{
161 kmem_free(mp->m_dir_geo);
162 kmem_free(mp->m_attr_geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165/*
166 * Return 1 if directory contains only "." and "..".
167 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000168int
169xfs_dir_isempty(
170 xfs_inode_t *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200172 xfs_dir2_sf_hdr_t *sfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Al Viroabbede12011-07-26 02:31:30 -0400174 ASSERT(S_ISDIR(dp->i_d.di_mode));
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000175 if (dp->i_d.di_size == 0) /* might happen during shutdown. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
178 return 0;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200179 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
180 return !sfp->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000184 * Validate a given inode number.
185 */
186int
187xfs_dir_ino_validate(
188 xfs_mount_t *mp,
189 xfs_ino_t ino)
190{
191 xfs_agblock_t agblkno;
192 xfs_agino_t agino;
193 xfs_agnumber_t agno;
194 int ino_ok;
195 int ioff;
196
197 agno = XFS_INO_TO_AGNO(mp, ino);
198 agblkno = XFS_INO_TO_AGBNO(mp, ino);
199 ioff = XFS_INO_TO_OFFSET(mp, ino);
200 agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
201 ino_ok =
202 agno < mp->m_sb.sb_agcount &&
203 agblkno < mp->m_sb.sb_agblocks &&
204 agblkno != 0 &&
205 ioff < (1 << mp->m_sb.sb_inopblog) &&
206 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
207 if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
208 XFS_RANDOM_DIR_INO_VALIDATE))) {
Dave Chinner53487782011-03-07 10:05:35 +1100209 xfs_warn(mp, "Invalid inode number 0x%Lx",
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000210 (unsigned long long) ino);
211 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
212 return XFS_ERROR(EFSCORRUPTED);
213 }
214 return 0;
215}
216
217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Initialize a directory with its "." and ".." entries.
219 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000220int
221xfs_dir_init(
222 xfs_trans_t *tp,
223 xfs_inode_t *dp,
224 xfs_inode_t *pdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Dave Chinnera1358aa2014-02-27 16:51:26 +1100226 struct xfs_da_args *args;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000227 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Al Viroabbede12011-07-26 02:31:30 -0400229 ASSERT(S_ISDIR(dp->i_d.di_mode));
Dave Chinnera1358aa2014-02-27 16:51:26 +1100230 error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino);
231 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return error;
Dave Chinnera1358aa2014-02-27 16:51:26 +1100233
234 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
235 if (!args)
236 return ENOMEM;
237
Dave Chinner0650b552014-06-06 15:01:58 +1000238 args->geo = dp->i_mount->m_dir_geo;
Dave Chinnera1358aa2014-02-27 16:51:26 +1100239 args->dp = dp;
240 args->trans = tp;
241 error = xfs_dir2_sf_create(args, pdp->i_ino);
242 kmem_free(args);
243 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/*
247 Enter a name in a directory.
248 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000249int
250xfs_dir_createname(
251 xfs_trans_t *tp,
252 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000253 struct xfs_name *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 xfs_ino_t inum, /* new entry inode number */
255 xfs_fsblock_t *first, /* bmap's firstblock */
256 xfs_bmap_free_t *flist, /* bmap's freeblock list */
257 xfs_extlen_t total) /* bmap's total block count */
258{
Dave Chinnera1358aa2014-02-27 16:51:26 +1100259 struct xfs_da_args *args;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000260 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int v; /* type-checking value */
262
Al Viroabbede12011-07-26 02:31:30 -0400263 ASSERT(S_ISDIR(dp->i_d.di_mode));
Dave Chinnera1358aa2014-02-27 16:51:26 +1100264 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
265 if (rval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 XFS_STATS_INC(xs_dir_create);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000268
Dave Chinnera1358aa2014-02-27 16:51:26 +1100269 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
270 if (!args)
271 return ENOMEM;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000272
Dave Chinner0650b552014-06-06 15:01:58 +1000273 args->geo = dp->i_mount->m_dir_geo;
Dave Chinnera1358aa2014-02-27 16:51:26 +1100274 args->name = name->name;
275 args->namelen = name->len;
276 args->filetype = name->type;
277 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
278 args->inumber = inum;
279 args->dp = dp;
280 args->firstblock = first;
281 args->flist = flist;
282 args->total = total;
283 args->whichfork = XFS_DATA_FORK;
284 args->trans = tp;
285 args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
286
287 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
288 rval = xfs_dir2_sf_addname(args);
289 goto out_free;
290 }
291
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000292 rval = xfs_dir2_isblock(dp, &v);
Dave Chinnera1358aa2014-02-27 16:51:26 +1100293 if (rval)
294 goto out_free;
295 if (v) {
296 rval = xfs_dir2_block_addname(args);
297 goto out_free;
298 }
299
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000300 rval = xfs_dir2_isleaf(dp, &v);
Dave Chinnera1358aa2014-02-27 16:51:26 +1100301 if (rval)
302 goto out_free;
303 if (v)
304 rval = xfs_dir2_leaf_addname(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 else
Dave Chinnera1358aa2014-02-27 16:51:26 +1100306 rval = xfs_dir2_node_addname(args);
307
308out_free:
309 kmem_free(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return rval;
311}
312
313/*
Barry Naujok384f3ce2008-05-21 16:58:22 +1000314 * If doing a CI lookup and case-insensitive match, dup actual name into
315 * args.value. Return EEXIST for success (ie. name found) or an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000317int
Barry Naujok384f3ce2008-05-21 16:58:22 +1000318xfs_dir_cilookup_result(
319 struct xfs_da_args *args,
Dave Chinnera3380ae2010-01-20 10:47:25 +1100320 const unsigned char *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000321 int len)
322{
323 if (args->cmpresult == XFS_CMP_DIFFERENT)
324 return ENOENT;
325 if (args->cmpresult != XFS_CMP_CASE ||
326 !(args->op_flags & XFS_DA_OP_CILOOKUP))
327 return EEXIST;
328
Christoph Hellwig3f52c2f2009-07-18 18:14:57 -0400329 args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000330 if (!args->value)
331 return ENOMEM;
332
333 memcpy(args->value, name, len);
334 args->valuelen = len;
335 return EEXIST;
336}
337
338/*
339 * Lookup a name in a directory, give back the inode number.
340 * If ci_name is not NULL, returns the actual name in ci_name if it differs
341 * to name, or ci_name->name is set to NULL for an exact match.
342 */
343
344int
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000345xfs_dir_lookup(
346 xfs_trans_t *tp,
347 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000348 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000349 xfs_ino_t *inum, /* out: inode number */
350 struct xfs_name *ci_name) /* out: actual name if CI match */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Dave Chinnera1358aa2014-02-27 16:51:26 +1100352 struct xfs_da_args *args;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000353 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int v; /* type-checking value */
355
Al Viroabbede12011-07-26 02:31:30 -0400356 ASSERT(S_ISDIR(dp->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 XFS_STATS_INC(xs_dir_lookup);
358
Dave Chinnera1358aa2014-02-27 16:51:26 +1100359 /*
360 * We need to use KM_NOFS here so that lockdep will not throw false
361 * positive deadlock warnings on a non-transactional lookup path. It is
362 * safe to recurse into inode recalim in that case, but lockdep can't
363 * easily be taught about it. Hence KM_NOFS avoids having to add more
364 * lockdep Doing this avoids having to add a bunch of lockdep class
365 * annotations into the reclaim path for the ilock.
366 */
367 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
Dave Chinner0650b552014-06-06 15:01:58 +1000368 args->geo = dp->i_mount->m_dir_geo;
Dave Chinnera1358aa2014-02-27 16:51:26 +1100369 args->name = name->name;
370 args->namelen = name->len;
371 args->filetype = name->type;
372 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
373 args->dp = dp;
374 args->whichfork = XFS_DATA_FORK;
375 args->trans = tp;
376 args->op_flags = XFS_DA_OP_OKNOENT;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000377 if (ci_name)
Dave Chinnera1358aa2014-02-27 16:51:26 +1100378 args->op_flags |= XFS_DA_OP_CILOOKUP;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000379
Dave Chinnera1358aa2014-02-27 16:51:26 +1100380 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
381 rval = xfs_dir2_sf_lookup(args);
382 goto out_check_rval;
383 }
384
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000385 rval = xfs_dir2_isblock(dp, &v);
Dave Chinnera1358aa2014-02-27 16:51:26 +1100386 if (rval)
387 goto out_free;
388 if (v) {
389 rval = xfs_dir2_block_lookup(args);
390 goto out_check_rval;
391 }
392
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000393 rval = xfs_dir2_isleaf(dp, &v);
Dave Chinnera1358aa2014-02-27 16:51:26 +1100394 if (rval)
395 goto out_free;
396 if (v)
397 rval = xfs_dir2_leaf_lookup(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 else
Dave Chinnera1358aa2014-02-27 16:51:26 +1100399 rval = xfs_dir2_node_lookup(args);
400
401out_check_rval:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (rval == EEXIST)
403 rval = 0;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000404 if (!rval) {
Dave Chinnera1358aa2014-02-27 16:51:26 +1100405 *inum = args->inumber;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000406 if (ci_name) {
Dave Chinnera1358aa2014-02-27 16:51:26 +1100407 ci_name->name = args->value;
408 ci_name->len = args->valuelen;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000409 }
410 }
Dave Chinnera1358aa2014-02-27 16:51:26 +1100411out_free:
412 kmem_free(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return rval;
414}
415
416/*
417 * Remove an entry from a directory.
418 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000419int
420xfs_dir_removename(
421 xfs_trans_t *tp,
422 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000423 struct xfs_name *name,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000424 xfs_ino_t ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 xfs_fsblock_t *first, /* bmap's firstblock */
426 xfs_bmap_free_t *flist, /* bmap's freeblock list */
427 xfs_extlen_t total) /* bmap's total block count */
428{
Dave Chinnera1358aa2014-02-27 16:51:26 +1100429 struct xfs_da_args *args;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000430 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int v; /* type-checking value */
432
Al Viroabbede12011-07-26 02:31:30 -0400433 ASSERT(S_ISDIR(dp->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 XFS_STATS_INC(xs_dir_remove);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000435
Dave Chinnera1358aa2014-02-27 16:51:26 +1100436 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
437 if (!args)
438 return ENOMEM;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000439
Dave Chinner0650b552014-06-06 15:01:58 +1000440 args->geo = dp->i_mount->m_dir_geo;
Dave Chinnera1358aa2014-02-27 16:51:26 +1100441 args->name = name->name;
442 args->namelen = name->len;
443 args->filetype = name->type;
444 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
445 args->inumber = ino;
446 args->dp = dp;
447 args->firstblock = first;
448 args->flist = flist;
449 args->total = total;
450 args->whichfork = XFS_DATA_FORK;
451 args->trans = tp;
452
453 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
454 rval = xfs_dir2_sf_removename(args);
455 goto out_free;
456 }
457
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000458 rval = xfs_dir2_isblock(dp, &v);
Dave Chinnera1358aa2014-02-27 16:51:26 +1100459 if (rval)
460 goto out_free;
461 if (v) {
462 rval = xfs_dir2_block_removename(args);
463 goto out_free;
464 }
465
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000466 rval = xfs_dir2_isleaf(dp, &v);
Dave Chinnera1358aa2014-02-27 16:51:26 +1100467 if (rval)
468 goto out_free;
469 if (v)
470 rval = xfs_dir2_leaf_removename(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 else
Dave Chinnera1358aa2014-02-27 16:51:26 +1100472 rval = xfs_dir2_node_removename(args);
473out_free:
474 kmem_free(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return rval;
476}
477
478/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * Replace the inode number of a directory entry.
480 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000481int
482xfs_dir_replace(
483 xfs_trans_t *tp,
484 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000485 struct xfs_name *name, /* name of entry to replace */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 xfs_ino_t inum, /* new inode number */
487 xfs_fsblock_t *first, /* bmap's firstblock */
488 xfs_bmap_free_t *flist, /* bmap's freeblock list */
489 xfs_extlen_t total) /* bmap's total block count */
490{
Dave Chinnera1358aa2014-02-27 16:51:26 +1100491 struct xfs_da_args *args;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000492 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int v; /* type-checking value */
494
Al Viroabbede12011-07-26 02:31:30 -0400495 ASSERT(S_ISDIR(dp->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Dave Chinnera1358aa2014-02-27 16:51:26 +1100497 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
498 if (rval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return rval;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000500
Dave Chinnera1358aa2014-02-27 16:51:26 +1100501 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
502 if (!args)
503 return ENOMEM;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000504
Dave Chinner0650b552014-06-06 15:01:58 +1000505 args->geo = dp->i_mount->m_dir_geo;
Dave Chinnera1358aa2014-02-27 16:51:26 +1100506 args->name = name->name;
507 args->namelen = name->len;
508 args->filetype = name->type;
509 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
510 args->inumber = inum;
511 args->dp = dp;
512 args->firstblock = first;
513 args->flist = flist;
514 args->total = total;
515 args->whichfork = XFS_DATA_FORK;
516 args->trans = tp;
517
518 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
519 rval = xfs_dir2_sf_replace(args);
520 goto out_free;
521 }
522
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000523 rval = xfs_dir2_isblock(dp, &v);
Dave Chinnera1358aa2014-02-27 16:51:26 +1100524 if (rval)
525 goto out_free;
526 if (v) {
527 rval = xfs_dir2_block_replace(args);
528 goto out_free;
529 }
530
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000531 rval = xfs_dir2_isleaf(dp, &v);
Dave Chinnera1358aa2014-02-27 16:51:26 +1100532 if (rval)
533 goto out_free;
534 if (v)
535 rval = xfs_dir2_leaf_replace(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 else
Dave Chinnera1358aa2014-02-27 16:51:26 +1100537 rval = xfs_dir2_node_replace(args);
538out_free:
539 kmem_free(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return rval;
541}
542
543/*
544 * See if this entry can be added to the directory without allocating space.
Barry Naujok556b8b12008-04-10 12:22:07 +1000545 * First checks that the caller couldn't reserve enough space (resblks = 0).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000547int
548xfs_dir_canenter(
549 xfs_trans_t *tp,
550 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000551 struct xfs_name *name, /* name of entry to add */
552 uint resblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Dave Chinnera1358aa2014-02-27 16:51:26 +1100554 struct xfs_da_args *args;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000555 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int v; /* type-checking value */
557
Barry Naujok556b8b12008-04-10 12:22:07 +1000558 if (resblks)
559 return 0;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000560
Al Viroabbede12011-07-26 02:31:30 -0400561 ASSERT(S_ISDIR(dp->i_d.di_mode));
Barry Naujok556b8b12008-04-10 12:22:07 +1000562
Dave Chinnera1358aa2014-02-27 16:51:26 +1100563 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
564 if (!args)
565 return ENOMEM;
566
Dave Chinner0650b552014-06-06 15:01:58 +1000567 args->geo = dp->i_mount->m_dir_geo;
Dave Chinnera1358aa2014-02-27 16:51:26 +1100568 args->name = name->name;
569 args->namelen = name->len;
570 args->filetype = name->type;
571 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
572 args->dp = dp;
573 args->whichfork = XFS_DATA_FORK;
574 args->trans = tp;
575 args->op_flags = XFS_DA_OP_JUSTCHECK | XFS_DA_OP_ADDNAME |
Barry Naujok6a178102008-05-21 16:42:05 +1000576 XFS_DA_OP_OKNOENT;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000577
Dave Chinnera1358aa2014-02-27 16:51:26 +1100578 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
579 rval = xfs_dir2_sf_addname(args);
580 goto out_free;
581 }
582
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000583 rval = xfs_dir2_isblock(dp, &v);
Dave Chinnera1358aa2014-02-27 16:51:26 +1100584 if (rval)
585 goto out_free;
586 if (v) {
587 rval = xfs_dir2_block_addname(args);
588 goto out_free;
589 }
590
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000591 rval = xfs_dir2_isleaf(dp, &v);
Dave Chinnera1358aa2014-02-27 16:51:26 +1100592 if (rval)
593 goto out_free;
594 if (v)
595 rval = xfs_dir2_leaf_addname(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 else
Dave Chinnera1358aa2014-02-27 16:51:26 +1100597 rval = xfs_dir2_node_addname(args);
598out_free:
599 kmem_free(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return rval;
601}
602
603/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * Utility routines.
605 */
606
607/*
608 * Add a block to the directory.
Christoph Hellwig77936d02011-07-13 13:43:49 +0200609 *
610 * This routine is for data and free blocks, not leaf/node blocks which are
611 * handled by xfs_da_grow_inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000613int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614xfs_dir2_grow_inode(
Christoph Hellwig77936d02011-07-13 13:43:49 +0200615 struct xfs_da_args *args,
616 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
617 xfs_dir2_db_t *dbp) /* out: block number added */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Christoph Hellwig77936d02011-07-13 13:43:49 +0200619 struct xfs_inode *dp = args->dp;
620 struct xfs_mount *mp = dp->i_mount;
621 xfs_fileoff_t bno; /* directory offset of new block */
622 int count; /* count of filesystem blocks */
623 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000625 trace_xfs_dir2_grow_inode(args, space);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /*
628 * Set lowest possible block in the space requested.
629 */
630 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
631 count = mp->m_dirblkfsbs;
Christoph Hellwig77936d02011-07-13 13:43:49 +0200632
633 error = xfs_da_grow_inode_int(args, &bno, count);
634 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000637 *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
David Chinnera7444052008-10-30 17:38:12 +1100638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /*
640 * Update file's size if this is the data space and it grew.
641 */
642 if (space == XFS_DIR2_DATA_SPACE) {
643 xfs_fsize_t size; /* directory file (data) size */
644
645 size = XFS_FSB_TO_B(mp, bno + count);
646 if (size > dp->i_d.di_size) {
647 dp->i_d.di_size = size;
Christoph Hellwig77936d02011-07-13 13:43:49 +0200648 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 }
651 return 0;
652}
653
654/*
655 * See if the directory is a single-block form directory.
656 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000657int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658xfs_dir2_isblock(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000659 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int *vp) /* out: 1 is block, 0 is not block */
661{
662 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000663 xfs_mount_t *mp;
664 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 mp = dp->i_mount;
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000667 if ((rval = xfs_bmap_last_offset(dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
670 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
671 *vp = rval;
672 return 0;
673}
674
675/*
676 * See if the directory is a single-leaf form directory.
677 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000678int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679xfs_dir2_isleaf(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000680 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int *vp) /* out: 1 is leaf, 0 is not leaf */
682{
683 xfs_fileoff_t last; /* last file offset */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000684 xfs_mount_t *mp;
685 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 mp = dp->i_mount;
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000688 if ((rval = xfs_bmap_last_offset(dp, &last, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return rval;
Dave Chinner7dda6e82014-06-06 15:11:18 +1000690 *vp = last == mp->m_dir_geo->leafblk + (1 << mp->m_sb.sb_dirblklog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return 0;
692}
693
694/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * Remove the given block from the directory.
696 * This routine is used for data and free blocks, leaf/node are done
697 * by xfs_da_shrink_inode.
698 */
699int
700xfs_dir2_shrink_inode(
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000701 xfs_da_args_t *args,
702 xfs_dir2_db_t db,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000703 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 xfs_fileoff_t bno; /* directory file offset */
706 xfs_dablk_t da; /* directory file offset */
707 int done; /* bunmap is finished */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000708 xfs_inode_t *dp;
709 int error;
710 xfs_mount_t *mp;
711 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000713 trace_xfs_dir2_shrink_inode(args, db);
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 dp = args->dp;
716 mp = dp->i_mount;
717 tp = args->trans;
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000718 da = xfs_dir2_db_to_da(args->geo, db);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /*
720 * Unmap the fsblock(s).
721 */
722 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
723 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000724 &done))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /*
726 * ENOSPC actually can happen if we're in a removename with
727 * no space reservation, and the resulting block removal
728 * would cause a bmap btree split or conversion from extents
729 * to btree. This can only happen for un-fragmented
730 * directory blocks, since you need to be punching out
731 * the middle of an extent.
732 * In this case we need to leave the block in the file,
733 * and not binval it.
734 * So the block has to be in a consistent empty state
735 * and appropriately logged.
736 * We don't free up the buffer, the caller can tell it
737 * hasn't happened since it got an error back.
738 */
739 return error;
740 }
741 ASSERT(done);
742 /*
743 * Invalidate the buffer from the transaction.
744 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000745 xfs_trans_binval(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /*
747 * If it's not a data block, we're done.
748 */
Dave Chinner30028032014-06-06 15:08:18 +1000749 if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751 /*
752 * If the block isn't the last one in the directory, we're done.
753 */
Dave Chinner9b3b5522014-06-06 15:06:53 +1000754 if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
756 bno = da;
757 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
758 /*
759 * This can't really happen unless there's kernel corruption.
760 */
761 return error;
762 }
Dave Chinner7dda6e82014-06-06 15:11:18 +1000763 if (db == args->geo->datablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 ASSERT(bno == 0);
765 else
766 ASSERT(bno > 0);
767 /*
768 * Set the size to the new last block.
769 */
770 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
771 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
772 return 0;
773}