1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2018-2024 Oracle. All Rights Reserved.
* Author: Darrick J. Wong <djwong@kernel.org>
*/
#include "libxfs_priv.h"
#include "xfs_fs.h"
#include "xfs_shared.h"
#include "xfs_format.h"
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
#include "xfs_bit.h"
#include "xfs_sb.h"
#include "xfs_mount.h"
#include "xfs_defer.h"
#include "xfs_trans.h"
#include "xfs_metafile.h"
#include "xfs_metadir.h"
#include "xfs_trace.h"
#include "xfs_inode.h"
#include "xfs_ialloc.h"
#include "xfs_bmap_btree.h"
#include "xfs_da_format.h"
#include "xfs_da_btree.h"
#include "xfs_trans_space.h"
#include "xfs_ag.h"
#include "xfs_dir2.h"
#include "xfs_dir2_priv.h"
#include "xfs_parent.h"
#include "xfs_health.h"
#include "xfs_errortag.h"
#include "xfs_btree.h"
#include "xfs_alloc.h"
/*
* Metadata Directory Tree
* =======================
*
* These functions provide an abstraction layer for looking up, creating, and
* deleting metadata inodes that live within a special metadata directory tree.
*
* This code does not manage the five existing metadata inodes: real time
* bitmap & summary; and the user, group, and quotas. All other metadata
* inodes must use only the xfs_meta{dir,file}_* functions.
*
* Callers wishing to create or hardlink a metadata inode must create an
* xfs_metadir_update structure, call the appropriate xfs_metadir* function,
* and then call xfs_metadir_commit or xfs_metadir_cancel to commit or cancel
* the update. Files in the metadata directory tree currently cannot be
* unlinked.
*
* When the metadir feature is enabled, all metadata inodes must have the
* "metadata" inode flag set to prevent them from being exposed to the outside
* world.
*
* Callers must take the ILOCK of any inode in the metadata directory tree to
* synchronize access to that inode. It is never necessary to take the IOLOCK
* or the MMAPLOCK since metadata inodes must not be exposed to user space.
*/
static inline void
xfs_metadir_set_xname(
struct xfs_name *xname,
const char *path,
unsigned char ftype)
{
xname->name = (const unsigned char *)path;
xname->len = strlen(path);
xname->type = ftype;
}
/*
* Given a parent directory @dp and a metadata inode path component @xname,
* Look up the inode number in the directory, returning it in @ino.
* @xname.type must match the directory entry's ftype.
*
* Caller must hold ILOCK_EXCL.
*/
static inline int
xfs_metadir_lookup(
struct xfs_trans *tp,
struct xfs_inode *dp,
struct xfs_name *xname,
xfs_ino_t *ino)
{
struct xfs_mount *mp = dp->i_mount;
struct xfs_da_args args = {
.trans = tp,
.dp = dp,
.geo = mp->m_dir_geo,
.name = xname->name,
.namelen = xname->len,
.hashval = xfs_dir2_hashname(mp, xname),
.whichfork = XFS_DATA_FORK,
.op_flags = XFS_DA_OP_OKNOENT,
.owner = dp->i_ino,
};
int error;
if (!S_ISDIR(VFS_I(dp)->i_mode)) {
xfs_fs_mark_sick(mp, XFS_SICK_FS_METADIR);
return -EFSCORRUPTED;
}
if (xfs_is_shutdown(mp))
return -EIO;
error = xfs_dir_lookup_args(&args);
if (error)
return error;
if (!xfs_verify_ino(mp, args.inumber)) {
xfs_fs_mark_sick(mp, XFS_SICK_FS_METADIR);
return -EFSCORRUPTED;
}
if (xname->type != XFS_DIR3_FT_UNKNOWN && xname->type != args.filetype) {
xfs_fs_mark_sick(mp, XFS_SICK_FS_METADIR);
return -EFSCORRUPTED;
}
trace_xfs_metadir_lookup(dp, xname, args.inumber);
*ino = args.inumber;
return 0;
}
/*
* Look up and read a metadata inode from the metadata directory. If the path
* component doesn't exist, return -ENOENT.
*/
int
xfs_metadir_load(
struct xfs_trans *tp,
struct xfs_inode *dp,
const char *path,
enum xfs_metafile_type metafile_type,
struct xfs_inode **ipp)
{
struct xfs_name xname;
xfs_ino_t ino;
int error;
xfs_metadir_set_xname(&xname, path, XFS_DIR3_FT_UNKNOWN);
xfs_ilock(dp, XFS_ILOCK_EXCL);
error = xfs_metadir_lookup(tp, dp, &xname, &ino);
xfs_iunlock(dp, XFS_ILOCK_EXCL);
if (error)
return error;
return xfs_trans_metafile_iget(tp, ino, metafile_type, ipp);
}
/*
* Unlock and release resources after committing (or cancelling) a metadata
* directory tree operation. The caller retains its reference to @upd->ip
* and must release it explicitly.
*/
static inline void
xfs_metadir_teardown(
struct xfs_metadir_update *upd,
int error)
{
trace_xfs_metadir_teardown(upd, error);
if (upd->ppargs) {
xfs_parent_finish(upd->dp->i_mount, upd->ppargs);
upd->ppargs = NULL;
}
if (upd->ip) {
if (upd->ip_locked)
xfs_iunlock(upd->ip, XFS_ILOCK_EXCL);
upd->ip_locked = false;
}
if (upd->dp_locked)
xfs_iunlock(upd->dp, XFS_ILOCK_EXCL);
upd->dp_locked = false;
}
/*
* Begin the process of creating a metadata file by allocating transactions
* and taking whatever resources we're going to need.
*/
int
xfs_metadir_start_create(
struct xfs_metadir_update *upd)
{
struct xfs_mount *mp = upd->dp->i_mount;
int error;
ASSERT(upd->dp != NULL);
ASSERT(upd->ip == NULL);
ASSERT(xfs_has_metadir(mp));
ASSERT(upd->metafile_type != XFS_METAFILE_UNKNOWN);
error = xfs_parent_start(mp, &upd->ppargs);
if (error)
return error;
/*
* If we ever need the ability to create rt metadata files on a
* pre-metadir filesystem, we'll need to dqattach the parent here.
* Currently we assume that mkfs will create the files and quotacheck
* will account for them.
*/
error = xfs_trans_alloc(mp, &M_RES(mp)->tr_create,
xfs_create_space_res(mp, MAXNAMELEN), 0, 0, &upd->tp);
if (error)
goto out_teardown;
/*
* Lock the parent directory if there is one. We can't ijoin it to
* the transaction until after the child file has been created.
*/
xfs_ilock(upd->dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
upd->dp_locked = true;
trace_xfs_metadir_start_create(upd);
return 0;
out_teardown:
xfs_metadir_teardown(upd, error);
return error;
}
/*
* Create a metadata inode with the given @mode, and insert it into the
* metadata directory tree at the given @upd->path. The path up to the final
* component must already exist. The final path component must not exist.
*
* The new metadata inode will be attached to the update structure @upd->ip,
* with the ILOCK held until the caller releases it.
*
* NOTE: This function may return a new inode to the caller even if it returns
* a negative error code. If an inode is passed back, the caller must finish
* setting up the inode before releasing it.
*/
int
xfs_metadir_create(
struct xfs_metadir_update *upd,
umode_t mode)
{
struct xfs_icreate_args args = {
.pip = upd->dp,
.mode = mode,
};
struct xfs_name xname;
struct xfs_dir_update du = {
.dp = upd->dp,
.name = &xname,
.ppargs = upd->ppargs,
};
struct xfs_mount *mp = upd->dp->i_mount;
xfs_ino_t ino;
unsigned int resblks;
int error;
xfs_assert_ilocked(upd->dp, XFS_ILOCK_EXCL);
/* Check that the name does not already exist in the directory. */
xfs_metadir_set_xname(&xname, upd->path, XFS_DIR3_FT_UNKNOWN);
error = xfs_metadir_lookup(upd->tp, upd->dp, &xname, &ino);
switch (error) {
case -ENOENT:
break;
case 0:
error = -EEXIST;
fallthrough;
default:
return error;
}
/*
* A newly created regular or special file just has one directory
* entry pointing to them, but a directory also the "." entry
* pointing to itself.
*/
error = xfs_dialloc(&upd->tp, &args, &ino);
if (error)
return error;
error = xfs_icreate(upd->tp, ino, &args, &upd->ip);
if (error)
return error;
du.ip = upd->ip;
xfs_metafile_set_iflag(upd->tp, upd->ip, upd->metafile_type);
upd->ip_locked = true;
/*
* Join the directory inode to the transaction. We do not do it
* earlier because xfs_dialloc rolls the transaction.
*/
xfs_trans_ijoin(upd->tp, upd->dp, 0);
/* Create the entry. */
if (S_ISDIR(args.mode))
resblks = xfs_mkdir_space_res(mp, xname.len);
else
resblks = xfs_create_space_res(mp, xname.len);
xname.type = xfs_mode_to_ftype(args.mode);
trace_xfs_metadir_try_create(upd);
error = xfs_dir_create_child(upd->tp, resblks, &du);
if (error)
return error;
/* Metadir files are not accounted to quota. */
trace_xfs_metadir_create(upd);
return 0;
}
#ifndef __KERNEL__
/*
* Begin the process of linking a metadata file by allocating transactions
* and locking whatever resources we're going to need.
*/
int
xfs_metadir_start_link(
struct xfs_metadir_update *upd)
{
struct xfs_mount *mp = upd->dp->i_mount;
unsigned int resblks;
int nospace_error = 0;
int error;
ASSERT(upd->dp != NULL);
ASSERT(upd->ip != NULL);
ASSERT(xfs_has_metadir(mp));
error = xfs_parent_start(mp, &upd->ppargs);
if (error)
return error;
resblks = xfs_link_space_res(mp, MAXNAMELEN);
error = xfs_trans_alloc_dir(upd->dp, &M_RES(mp)->tr_link, upd->ip,
&resblks, &upd->tp, &nospace_error);
if (error)
goto out_teardown;
if (!resblks) {
/* We don't allow reservationless updates. */
xfs_trans_cancel(upd->tp);
upd->tp = NULL;
xfs_iunlock(upd->dp, XFS_ILOCK_EXCL);
xfs_iunlock(upd->ip, XFS_ILOCK_EXCL);
error = nospace_error;
goto out_teardown;
}
upd->dp_locked = true;
upd->ip_locked = true;
trace_xfs_metadir_start_link(upd);
return 0;
out_teardown:
xfs_metadir_teardown(upd, error);
return error;
}
/*
* Link the metadata directory given by @path to the inode @upd->ip.
* The path (up to the final component) must already exist, but the final
* component must not already exist.
*/
int
xfs_metadir_link(
struct xfs_metadir_update *upd)
{
struct xfs_name xname;
struct xfs_dir_update du = {
.dp = upd->dp,
.name = &xname,
.ip = upd->ip,
.ppargs = upd->ppargs,
};
struct xfs_mount *mp = upd->dp->i_mount;
xfs_ino_t ino;
unsigned int resblks;
int error;
xfs_assert_ilocked(upd->dp, XFS_ILOCK_EXCL);
xfs_assert_ilocked(upd->ip, XFS_ILOCK_EXCL);
/* Look up the name in the current directory. */
xfs_metadir_set_xname(&xname, upd->path,
xfs_mode_to_ftype(VFS_I(upd->ip)->i_mode));
error = xfs_metadir_lookup(upd->tp, upd->dp, &xname, &ino);
switch (error) {
case -ENOENT:
break;
case 0:
error = -EEXIST;
fallthrough;
default:
return error;
}
resblks = xfs_link_space_res(mp, xname.len);
error = xfs_dir_add_child(upd->tp, resblks, &du);
if (error)
return error;
trace_xfs_metadir_link(upd);
return 0;
}
#endif /* ! __KERNEL__ */
/* Commit a metadir update and unlock/drop all resources. */
int
xfs_metadir_commit(
struct xfs_metadir_update *upd)
{
int error;
trace_xfs_metadir_commit(upd);
error = xfs_trans_commit(upd->tp);
upd->tp = NULL;
xfs_metadir_teardown(upd, error);
return error;
}
/* Cancel a metadir update and unlock/drop all resources. */
void
xfs_metadir_cancel(
struct xfs_metadir_update *upd,
int error)
{
trace_xfs_metadir_cancel(upd);
xfs_trans_cancel(upd->tp);
upd->tp = NULL;
xfs_metadir_teardown(upd, error);
}
/* Create a metadata for the last component of the path. */
int
xfs_metadir_mkdir(
struct xfs_inode *dp,
const char *path,
struct xfs_inode **ipp)
{
struct xfs_metadir_update upd = {
.dp = dp,
.path = path,
.metafile_type = XFS_METAFILE_DIR,
};
int error;
if (xfs_is_shutdown(dp->i_mount))
return -EIO;
/* Allocate a transaction to create the last directory. */
error = xfs_metadir_start_create(&upd);
if (error)
return error;
/* Create the subdirectory and take our reference. */
error = xfs_metadir_create(&upd, S_IFDIR);
if (error)
goto out_cancel;
error = xfs_metadir_commit(&upd);
if (error)
goto out_irele;
xfs_finish_inode_setup(upd.ip);
*ipp = upd.ip;
return 0;
out_cancel:
xfs_metadir_cancel(&upd, error);
out_irele:
/* Have to finish setting up the inode to ensure it's deleted. */
if (upd.ip) {
xfs_finish_inode_setup(upd.ip);
xfs_irele(upd.ip);
}
return error;
}
|