Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0 2 : /* 3 : * Copyright (c) 2022-2023 Oracle, Inc. 4 : * All rights reserved. 5 : */ 6 : #include "xfs.h" 7 : #include "xfs_fs.h" 8 : #include "xfs_format.h" 9 : #include "xfs_log_format.h" 10 : #include "xfs_shared.h" 11 : #include "xfs_trans_resv.h" 12 : #include "xfs_mount.h" 13 : #include "xfs_bmap_btree.h" 14 : #include "xfs_inode.h" 15 : #include "xfs_error.h" 16 : #include "xfs_trace.h" 17 : #include "xfs_trans.h" 18 : #include "xfs_da_format.h" 19 : #include "xfs_da_btree.h" 20 : #include "xfs_attr.h" 21 : #include "xfs_ioctl.h" 22 : #include "xfs_parent.h" 23 : #include "xfs_da_btree.h" 24 : #include "xfs_parent_utils.h" 25 : #include "xfs_health.h" 26 : 27 : struct xfs_getparent_ctx { 28 : struct xfs_attr_list_context context; 29 : struct xfs_parent_name_irec pptr_irec; 30 : struct xfs_getparents *ppi; 31 : }; 32 : 33 : static inline unsigned int 34 : xfs_getparents_rec_sizeof( 35 : const struct xfs_parent_name_irec *irec) 36 : { 37 242074166 : return round_up(sizeof(struct xfs_getparents_rec) + irec->p_namelen + 1, 38 : sizeof(uint32_t)); 39 : } 40 : 41 : static void 42 5251354287 : xfs_getparent_listent( 43 : struct xfs_attr_list_context *context, 44 : int flags, 45 : unsigned char *name, 46 : int namelen, 47 : void *value, 48 : int valuelen) 49 : { 50 5251354287 : struct xfs_getparent_ctx *gp; 51 5251354287 : struct xfs_getparents *ppi; 52 5251354287 : struct xfs_getparents_rec *pptr; 53 5251354287 : struct xfs_parent_name_rec *rec = (void *)name; 54 5251354287 : struct xfs_parent_name_irec *irec; 55 5251354287 : struct xfs_mount *mp = context->dp->i_mount; 56 5251354287 : int arraytop; 57 : 58 5251354287 : gp = container_of(context, struct xfs_getparent_ctx, context); 59 5251354287 : ppi = gp->ppi; 60 5251354287 : irec = &gp->pptr_irec; 61 : 62 : /* Ignore non-parent xattrs */ 63 5251354287 : if (!(flags & XFS_ATTR_PARENT)) 64 : return; 65 : 66 : /* 67 : * Report corruption for anything that doesn't look like a parent 68 : * pointer. The attr list functions filtered out INCOMPLETE attrs. 69 : */ 70 242121322 : if (XFS_IS_CORRUPT(mp, 71 : !xfs_parent_namecheck(mp, rec, namelen, flags)) || 72 242110210 : XFS_IS_CORRUPT(mp, 73 : !xfs_parent_valuecheck(mp, value, valuelen))) { 74 0 : xfs_inode_mark_sick(context->dp, XFS_SICK_INO_PARENT); 75 0 : context->seen_enough = -EFSCORRUPTED; 76 0 : return; 77 : } 78 : 79 242153366 : xfs_parent_irec_from_disk(&gp->pptr_irec, rec, value, valuelen); 80 : 81 : /* 82 : * We found a parent pointer, but we've filled up the buffer. Signal 83 : * to the caller that we did /not/ reach the end of the parent pointer 84 : * recordset. 85 : */ 86 242074166 : arraytop = xfs_getparents_arraytop(ppi, ppi->gp_count + 1); 87 242074166 : context->firstu -= xfs_getparents_rec_sizeof(irec); 88 242074166 : if (context->firstu < arraytop) { 89 4 : context->seen_enough = 1; 90 4 : return; 91 : } 92 : 93 242074162 : trace_xfs_getparent_listent(context->dp, ppi, irec); 94 : 95 : /* Format the parent pointer directly into the caller buffer. */ 96 242106811 : ppi->gp_offsets[ppi->gp_count] = context->firstu; 97 242106811 : pptr = xfs_getparents_rec(ppi, ppi->gp_count); 98 242106811 : pptr->gpr_ino = irec->p_ino; 99 242106811 : pptr->gpr_gen = irec->p_gen; 100 242106811 : pptr->gpr_pad = 0; 101 242106811 : pptr->gpr_rsvd = 0; 102 : 103 484213622 : memcpy(pptr->gpr_name, irec->p_name, irec->p_namelen); 104 242106811 : pptr->gpr_name[irec->p_namelen] = 0; 105 242106811 : ppi->gp_count++; 106 : } 107 : 108 : /* Retrieve the parent pointers for a given inode. */ 109 : int 110 263547788 : xfs_getparent_pointers( 111 : struct xfs_inode *ip, 112 : struct xfs_getparents *ppi) 113 : { 114 263547788 : struct xfs_getparent_ctx *gp; 115 263547788 : int error; 116 : 117 263547788 : gp = kzalloc(sizeof(struct xfs_getparent_ctx), GFP_KERNEL); 118 263517904 : if (!gp) 119 : return -ENOMEM; 120 263517904 : gp->ppi = ppi; 121 263517904 : gp->context.dp = ip; 122 263517904 : gp->context.resynch = 1; 123 263517904 : gp->context.put_listent = xfs_getparent_listent; 124 263517904 : gp->context.bufsize = round_down(ppi->gp_bufsize, sizeof(uint32_t)); 125 263517904 : gp->context.firstu = gp->context.bufsize; 126 : 127 : /* Copy the cursor provided by caller */ 128 527035808 : memcpy(&gp->context.cursor, &ppi->gp_cursor, 129 : sizeof(struct xfs_attrlist_cursor)); 130 263517904 : ppi->gp_count = 0; 131 : 132 263517904 : trace_xfs_getparent_pointers(ip, ppi, &gp->context.cursor); 133 : 134 263562032 : error = xfs_attr_list(&gp->context); 135 263603817 : if (error) 136 0 : goto out_free; 137 263603817 : if (gp->context.seen_enough < 0) { 138 0 : error = gp->context.seen_enough; 139 0 : goto out_free; 140 : } 141 : 142 : /* Is this the root directory? */ 143 263603817 : if (ip->i_ino == ip->i_mount->m_sb.sb_rootino) 144 22322282 : ppi->gp_flags |= XFS_GETPARENTS_OFLAG_ROOT; 145 : 146 : /* 147 : * If we did not run out of buffer space, then we reached the end of 148 : * the pptr recordset, so set the DONE flag. 149 : */ 150 263603817 : if (gp->context.seen_enough == 0) 151 263614321 : ppi->gp_flags |= XFS_GETPARENTS_OFLAG_DONE; 152 : 153 : /* Update the caller with the current cursor position */ 154 527207634 : memcpy(&ppi->gp_cursor, &gp->context.cursor, 155 : sizeof(struct xfs_attrlist_cursor)); 156 263603817 : out_free: 157 263603817 : kfree(gp); 158 263603817 : return error; 159 : }