@@ -37,6 +37,7 @@ HFILES = \
xfs_ialloc_btree.h \
xfs_inode_buf.h \
xfs_inode_fork.h \
+ xfs_parent.h \
xfs_quota_defs.h \
xfs_refcount.h \
xfs_refcount_btree.h \
@@ -89,6 +90,7 @@ CFILES = cache.c \
xfs_inode_fork.c \
xfs_ialloc_btree.c \
xfs_log_rlimit.c \
+ xfs_parent.c \
xfs_refcount.c \
xfs_refcount_btree.c \
xfs_rmap.c \
new file mode 100644
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2015 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation
+ */
+#include "xfs.h"
+#include "xfs_fs.h"
+#include "platform_defs.h"
+#include "xfs_arch.h"
+#include "xfs_format.h"
+#include "xfs_log_format.h"
+#include "list.h"
+#include "xfs_shared.h"
+#include "xfs_trans_resv.h"
+#include "radix-tree.h"
+#include "atomic.h"
+#include "xfs_mount.h"
+#include "xfs_da_format.h"
+#include "xfs_bmap_btree.h"
+#include "cache.h"
+#include "xfs_cksum.h"
+#include "libxfs_io.h"
+#include "xfs_inode.h"
+#include "xfs_trace.h"
+#include "xfs_trans.h"
+#include "xfs_attr.h"
+#include "xfs_da_btree.h"
+#include "xfs_attr_sf.h"
+#include "libxfs_api_defs.h"
+
+/*
+ * Parent pointer attribute handling.
+ *
+ * Because the attribute value is a filename component, it will never be longer
+ * than 255 bytes. This means the attribute will always be a local format
+ * attribute as it is xfs_attr_leaf_entsize_local_max() for v5 filesystems will
+ * always be larger than this (max is 75% of block size).
+ *
+ * Creating a new parent attribute will always create a new attribute - there
+ * should never, ever be an existing attribute in the tree for a new inode.
+ * ENOSPC behaviour is problematic - creating the inode without the parent
+ * pointer is effectively a corruption, so we allow parent attribute creation
+ * to dip into the reserve block pool to avoid unexpected ENOSPC errors from
+ * occurring.
+ */
+
+
+/* Initializes a xfs_parent_name_rec to be stored as an attribute name */
+void
+xfs_init_parent_name_rec(
+ struct xfs_parent_name_rec *rec,
+ xfs_ino_t p_ino,
+ uint32_t p_gen,
+ uint32_t p_diroffset)
+{
+ rec->p_ino = cpu_to_be64(p_ino);
+ rec->p_gen = cpu_to_be32(p_gen);
+ rec->p_diroffset = cpu_to_be32(p_diroffset);
+}
+
+/* Initializes a xfs_parent_name_irec from an xfs_parent_name_rec */
+void
+xfs_init_parent_name_irec(
+ struct xfs_parent_name_irec *irec,
+ struct xfs_parent_name_rec *rec)
+{
+ irec->p_ino = be64_to_cpu(rec->p_ino);
+ irec->p_gen = be32_to_cpu(rec->p_gen);
+ irec->p_diroffset = be32_to_cpu(rec->p_diroffset);
+}
+
+/*
+ * Directly add a parent pointer instead of as a deferred operation
+ * Currently only used during protofile creation
+ */
+int
+xfs_parent_add(
+ struct xfs_inode *parent,
+ struct xfs_inode *child,
+ struct xfs_name *child_name,
+ uint32_t diroffset,
+ xfs_fsblock_t *firstblock,
+ struct xfs_defer_ops *dfops)
+{
+ struct xfs_parent_name_rec rec;
+ int error;
+ struct xfs_da_args args;
+ int flags = ATTR_PARENT;
+ int local = 0;
+ int rsvd = 0;
+ struct xfs_buf *leaf_bp = NULL;
+ struct xfs_trans_res tres;
+ struct xfs_mount *mp = child->i_mount;
+
+ xfs_init_parent_name_rec(&rec, parent->i_ino,
+ VFS_I(parent)->i_generation, diroffset);
+
+ error = xfs_attr_args_init(&args, child, (const unsigned char *)&rec,
+ sizeof(rec), flags);
+ if (error)
+ return error;
+
+ args.hashval = xfs_da_hashname(args.name, args.namelen);
+ args.value = (char *)child_name->name;
+ args.valuelen = child_name->len;
+ args.dfops = dfops;
+ args.op_flags = XFS_DA_OP_OKNOENT | XFS_DA_OP_ADDNAME;
+ args.firstblock = firstblock;
+ args.total = xfs_attr_calc_size(&args, &local);
+ args.trans = &tres;
+ ASSERT(local);
+
+ tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
+ M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
+ tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
+ tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
+
+ /*
+ * Root fork attributes can use reserved data blocks for this
+ * operation if necessary
+ */
+ error = xfs_trans_alloc(mp, &tres, args.total, 0,
+ rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
+ if (error)
+ goto out;
+
+
+ /*
+ * If the inode doesn't have an attribute fork, add one.
+ * (inode must not be locked when we call this routine)
+ */
+ if (XFS_IFORK_Q(child) == 0) {
+ int sf_size = sizeof(xfs_attr_sf_hdr_t) +
+ XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, args.valuelen);
+
+ error = xfs_bmap_add_attrfork(child, sf_size, rsvd);
+ if (error)
+ return error;
+ }
+
+ error = xfs_attr_set_args(&args, flags, leaf_bp, false);
+
+
+ if (error)
+ goto out;
+
+ xfs_trans_log_inode(args.trans, child, XFS_ILOG_CORE);
+
+ return error;
+
+out:
+ if (args.trans)
+ xfs_trans_cancel(args.trans);
+
+ return error;
+}
+
new file mode 100644
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017 Oracle, Inc.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation Inc.
+ */
+#ifndef __XFS_PARENT_H__
+#define __XFS_PARENT_H__
+
+#include "xfs_da_format.h"
+#include "xfs_format.h"
+
+/*
+ * Parent pointer attribute prototypes
+ */
+void xfs_init_parent_name_rec(struct xfs_parent_name_rec *rec,
+ xfs_ino_t p_ino, uint32_t p_gen,
+ uint32_t p_diroffset);
+void xfs_init_parent_name_irec(struct xfs_parent_name_irec *irec,
+ struct xfs_parent_name_rec *rec);
+
+int xfs_parent_add(struct xfs_trans *tp, struct xfs_inode *parent,
+ struct xfs_inode *child, struct xfs_name *child_name,
+ uint32_t diroffset, xfs_fsblock_t *firstblock,
+ struct xfs_defer_ops *dfops);
+#endif /* __XFS_PARENT_H__ */