From: Darrick J. Wong <djwong@xxxxxxxxxx> Since xfs_imeta_create can create new metadata files arbitrarily deep in the metadata directory tree, we must supply a function that can ensure that all directories in a path exist, and call it before the quota functions create the quota inodes. Signed-off-by: Darrick J. Wong <djwong@xxxxxxxxxx> --- fs/xfs/libxfs/xfs_imeta.h | 2 + fs/xfs/xfs_inode.c | 103 +++++++++++++++++++++++++++++++++++++++++++++ fs/xfs/xfs_qm.c | 16 +++++++ 3 files changed, 121 insertions(+) diff --git a/fs/xfs/libxfs/xfs_imeta.h b/fs/xfs/libxfs/xfs_imeta.h index 9b139f6809f0..741f426c6a4a 100644 --- a/fs/xfs/libxfs/xfs_imeta.h +++ b/fs/xfs/libxfs/xfs_imeta.h @@ -80,5 +80,7 @@ unsigned int xfs_imeta_unlink_space_res(struct xfs_mount *mp); int xfs_imeta_iget(struct xfs_mount *mp, xfs_ino_t ino, unsigned char ftype, struct xfs_inode **ipp); void xfs_imeta_irele(struct xfs_inode *ip); +int xfs_imeta_ensure_dirpath(struct xfs_mount *mp, + const struct xfs_imeta_path *path); #endif /* __XFS_IMETA_H__ */ diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c index 3830e03ceb0a..1eb53ed0097d 100644 --- a/fs/xfs/xfs_inode.c +++ b/fs/xfs/xfs_inode.c @@ -1031,6 +1031,109 @@ xfs_create_tmpfile( return error; } +/* Create a metadata for the last component of the path. */ +STATIC int +xfs_imeta_mkdir( + struct xfs_mount *mp, + const struct xfs_imeta_path *path) +{ + struct xfs_imeta_update upd; + struct xfs_inode *ip = NULL; + struct xfs_trans *tp = NULL; + struct xfs_dquot *udqp = NULL; + struct xfs_dquot *gdqp = NULL; + struct xfs_dquot *pdqp = NULL; + unsigned int resblks; + int error; + + if (xfs_is_shutdown(mp)) + return -EIO; + + error = xfs_imeta_start_update(mp, path, &upd); + if (error) + return error; + + /* Grab all the root dquots. */ + error = xfs_qm_vop_dqalloc(mp->m_metadirip, GLOBAL_ROOT_UID, + GLOBAL_ROOT_GID, 0, XFS_QMOPT_QUOTALL, &udqp, &gdqp, + &pdqp); + if (error) + goto out_end; + + /* Allocate a transaction to create the last directory. */ + resblks = xfs_imeta_create_space_res(mp); + error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_imeta_create, udqp, + gdqp, pdqp, resblks, &tp); + if (error) + goto out_dqrele; + + /* Create the subdirectory. */ + error = xfs_imeta_create(&tp, path, S_IFDIR, 0, &ip, &upd); + if (error) + goto out_trans_cancel; + + /* + * Attach the dquot(s) to the inodes and modify them incore. + * These ids of the inode couldn't have changed since the new + * inode has been locked ever since it was created. + */ + xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); + + error = xfs_trans_commit(tp); + + /* + * We don't pass the directory we just created to the caller, so finish + * setting up the inode, then release the dir and the dquots. + */ + goto out_irele; + +out_trans_cancel: + xfs_trans_cancel(tp); +out_irele: + /* Have to finish setting up the inode to ensure it's deleted. */ + if (ip) { + xfs_finish_inode_setup(ip); + xfs_irele(ip); + } + +out_dqrele: + xfs_qm_dqrele(udqp); + xfs_qm_dqrele(gdqp); + xfs_qm_dqrele(pdqp); +out_end: + xfs_imeta_end_update(mp, &upd, error); + return error; +} + +/* + * Make sure that every metadata directory path component exists and is a + * directory. + */ +int +xfs_imeta_ensure_dirpath( + struct xfs_mount *mp, + const struct xfs_imeta_path *path) +{ + struct xfs_imeta_path temp_path = { + .im_path = path->im_path, + .im_depth = 1, + .im_ftype = XFS_DIR3_FT_DIR, + }; + unsigned int i; + int error = 0; + + if (!xfs_has_metadir(mp)) + return 0; + + for (i = 0; i < path->im_depth - 1; i++, temp_path.im_depth++) { + error = xfs_imeta_mkdir(mp, &temp_path); + if (error && error != -EEXIST) + break; + } + + return error == -EEXIST ? 0 : error; +} + int xfs_link( xfs_inode_t *tdp, diff --git a/fs/xfs/xfs_qm.c b/fs/xfs/xfs_qm.c index 8828e8cafca5..905765eedcb0 100644 --- a/fs/xfs/xfs_qm.c +++ b/fs/xfs/xfs_qm.c @@ -829,6 +829,22 @@ xfs_qm_qino_alloc( if (error) return error; + /* + * Ensure the quota directory exists, being careful to disable quotas + * while we do this. We'll have to quotacheck anyway, so the temporary + * undercount of the directory tree shouldn't affect the quota count. + */ + if (xfs_has_metadir(mp)) { + unsigned int old_qflags; + + old_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT; + mp->m_qflags &= ~XFS_ALL_QUOTA_ACCT; + error = xfs_imeta_ensure_dirpath(mp, path); + mp->m_qflags |= old_qflags; + if (error) + return error; + } + error = xfs_imeta_start_update(mp, path, &upd); if (error) return error;