Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
| 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_mount.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 26 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 27 | #include "xfs_da_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_alloc.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 31 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 34 | #include "xfs_bmap_util.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 35 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_attr.h" |
| 37 | #include "xfs_attr_leaf.h" |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 38 | #include "xfs_attr_remote.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_quota.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_trans_space.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 42 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * xfs_attr.c |
| 46 | * |
| 47 | * Provide the external interfaces to manage attribute lists. |
| 48 | */ |
| 49 | |
| 50 | /*======================================================================== |
| 51 | * Function prototypes for the kernel. |
| 52 | *========================================================================*/ |
| 53 | |
| 54 | /* |
| 55 | * Internal routines when attribute list fits inside the inode. |
| 56 | */ |
| 57 | STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args); |
| 58 | |
| 59 | /* |
| 60 | * Internal routines when attribute list is one block. |
| 61 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 62 | STATIC int xfs_attr_leaf_get(xfs_da_args_t *args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args); |
| 64 | STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Internal routines when attribute list is more than one block. |
| 68 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 69 | STATIC int xfs_attr_node_get(xfs_da_args_t *args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | STATIC int xfs_attr_node_addname(xfs_da_args_t *args); |
| 71 | STATIC int xfs_attr_node_removename(xfs_da_args_t *args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | STATIC int xfs_attr_fillstate(xfs_da_state_t *state); |
| 73 | STATIC int xfs_attr_refillstate(xfs_da_state_t *state); |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 76 | STATIC int |
Christoph Hellwig | 67fd718 | 2014-05-13 16:34:43 +1000 | [diff] [blame] | 77 | xfs_attr_args_init( |
| 78 | struct xfs_da_args *args, |
| 79 | struct xfs_inode *dp, |
| 80 | const unsigned char *name, |
| 81 | int flags) |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 82 | { |
Christoph Hellwig | 67fd718 | 2014-05-13 16:34:43 +1000 | [diff] [blame] | 83 | |
| 84 | if (!name) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 85 | return -EINVAL; |
Christoph Hellwig | 67fd718 | 2014-05-13 16:34:43 +1000 | [diff] [blame] | 86 | |
| 87 | memset(args, 0, sizeof(*args)); |
Dave Chinner | 0650b55 | 2014-06-06 15:01:58 +1000 | [diff] [blame] | 88 | args->geo = dp->i_mount->m_attr_geo; |
Christoph Hellwig | 67fd718 | 2014-05-13 16:34:43 +1000 | [diff] [blame] | 89 | args->whichfork = XFS_ATTR_FORK; |
| 90 | args->dp = dp; |
| 91 | args->flags = flags; |
| 92 | args->name = name; |
| 93 | args->namelen = strlen((const char *)name); |
| 94 | if (args->namelen >= MAXNAMELEN) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 95 | return -EFAULT; /* match IRIX behaviour */ |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 96 | |
Christoph Hellwig | 67fd718 | 2014-05-13 16:34:43 +1000 | [diff] [blame] | 97 | args->hashval = xfs_da_hashname(args->name, args->namelen); |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 98 | return 0; |
| 99 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Dave Chinner | abec5f2 | 2013-08-12 20:49:38 +1000 | [diff] [blame] | 101 | int |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 102 | xfs_inode_hasattr( |
| 103 | struct xfs_inode *ip) |
| 104 | { |
| 105 | if (!XFS_IFORK_Q(ip) || |
| 106 | (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS && |
| 107 | ip->i_d.di_anextents == 0)) |
| 108 | return 0; |
| 109 | return 1; |
| 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /*======================================================================== |
| 113 | * Overall external interface routines. |
| 114 | *========================================================================*/ |
| 115 | |
Christoph Hellwig | b87d022 | 2014-05-13 16:34:24 +1000 | [diff] [blame] | 116 | int |
| 117 | xfs_attr_get( |
Christoph Hellwig | e82fa0c | 2009-11-14 16:17:20 +0000 | [diff] [blame] | 118 | struct xfs_inode *ip, |
Christoph Hellwig | b87d022 | 2014-05-13 16:34:24 +1000 | [diff] [blame] | 119 | const unsigned char *name, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 120 | unsigned char *value, |
Christoph Hellwig | e82fa0c | 2009-11-14 16:17:20 +0000 | [diff] [blame] | 121 | int *valuelenp, |
| 122 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Christoph Hellwig | b87d022 | 2014-05-13 16:34:24 +1000 | [diff] [blame] | 124 | struct xfs_da_args args; |
Christoph Hellwig | b87d022 | 2014-05-13 16:34:24 +1000 | [diff] [blame] | 125 | uint lock_mode; |
| 126 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 128 | XFS_STATS_INC(ip->i_mount, xs_attr_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 131 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | if (!xfs_inode_hasattr(ip)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 134 | return -ENOATTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Christoph Hellwig | 67fd718 | 2014-05-13 16:34:43 +1000 | [diff] [blame] | 136 | error = xfs_attr_args_init(&args, ip, name, flags); |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 137 | if (error) |
| 138 | return error; |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | args.value = value; |
| 141 | args.valuelen = *valuelenp; |
Eric Sandeen | c400ee3 | 2015-08-19 10:30:48 +1000 | [diff] [blame] | 142 | /* Entirely possible to look up a name which doesn't exist */ |
| 143 | args.op_flags = XFS_DA_OP_OKNOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Christoph Hellwig | 683cb94 | 2013-12-06 12:30:15 -0800 | [diff] [blame] | 145 | lock_mode = xfs_ilock_attr_map_shared(ip); |
Christoph Hellwig | b87d022 | 2014-05-13 16:34:24 +1000 | [diff] [blame] | 146 | if (!xfs_inode_hasattr(ip)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 147 | error = -ENOATTR; |
Christoph Hellwig | b87d022 | 2014-05-13 16:34:24 +1000 | [diff] [blame] | 148 | else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) |
| 149 | error = xfs_attr_shortform_getvalue(&args); |
| 150 | else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) |
| 151 | error = xfs_attr_leaf_get(&args); |
| 152 | else |
| 153 | error = xfs_attr_node_get(&args); |
Christoph Hellwig | 683cb94 | 2013-12-06 12:30:15 -0800 | [diff] [blame] | 154 | xfs_iunlock(ip, lock_mode); |
Christoph Hellwig | b87d022 | 2014-05-13 16:34:24 +1000 | [diff] [blame] | 155 | |
| 156 | *valuelenp = args.valuelen; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 157 | return error == -EEXIST ? 0 : error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 160 | /* |
| 161 | * Calculate how many blocks we need for the new attribute, |
| 162 | */ |
Eric Sandeen | 5d77c0d | 2009-11-19 15:52:00 +0000 | [diff] [blame] | 163 | STATIC int |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 164 | xfs_attr_calc_size( |
Christoph Hellwig | 6c888af | 2014-05-13 16:40:19 +1000 | [diff] [blame] | 165 | struct xfs_da_args *args, |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 166 | int *local) |
| 167 | { |
Christoph Hellwig | 6c888af | 2014-05-13 16:40:19 +1000 | [diff] [blame] | 168 | struct xfs_mount *mp = args->dp->i_mount; |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 169 | int size; |
| 170 | int nblks; |
| 171 | |
| 172 | /* |
| 173 | * Determine space new attribute will use, and if it would be |
| 174 | * "local" or "remote" (note: local != inline). |
| 175 | */ |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 176 | size = xfs_attr_leaf_newentsize(args, local); |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 177 | nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK); |
| 178 | if (*local) { |
Dave Chinner | 33a6039 | 2014-06-06 15:21:10 +1000 | [diff] [blame] | 179 | if (size > (args->geo->blksize / 2)) { |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 180 | /* Double split possible */ |
| 181 | nblks *= 2; |
| 182 | } |
| 183 | } else { |
| 184 | /* |
| 185 | * Out of line attribute, cannot double split, but |
| 186 | * make room for the attribute value itself. |
| 187 | */ |
Dave Chinner | 2d6dcc6 | 2014-05-15 09:39:28 +1000 | [diff] [blame] | 188 | uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen); |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 189 | nblks += dblocks; |
| 190 | nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK); |
| 191 | } |
| 192 | |
| 193 | return nblks; |
| 194 | } |
| 195 | |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 196 | int |
| 197 | xfs_attr_set( |
| 198 | struct xfs_inode *dp, |
| 199 | const unsigned char *name, |
| 200 | unsigned char *value, |
| 201 | int valuelen, |
| 202 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 204 | struct xfs_mount *mp = dp->i_mount; |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 205 | struct xfs_da_args args; |
| 206 | struct xfs_bmap_free flist; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 207 | struct xfs_trans_res tres; |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 208 | xfs_fsblock_t firstblock; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 209 | int rsvd = (flags & ATTR_ROOT) != 0; |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 210 | int error, err2, local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 212 | XFS_STATS_INC(mp, xs_attr_set); |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 213 | |
| 214 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 215 | return -EIO; |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 216 | |
Christoph Hellwig | 67fd718 | 2014-05-13 16:34:43 +1000 | [diff] [blame] | 217 | error = xfs_attr_args_init(&args, dp, name, flags); |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 218 | if (error) |
| 219 | return error; |
| 220 | |
Christoph Hellwig | 67fd718 | 2014-05-13 16:34:43 +1000 | [diff] [blame] | 221 | args.value = value; |
| 222 | args.valuelen = valuelen; |
| 223 | args.firstblock = &firstblock; |
| 224 | args.flist = &flist; |
| 225 | args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT; |
Christoph Hellwig | 6c888af | 2014-05-13 16:40:19 +1000 | [diff] [blame] | 226 | args.total = xfs_attr_calc_size(&args, &local); |
Christoph Hellwig | 67fd718 | 2014-05-13 16:34:43 +1000 | [diff] [blame] | 227 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 228 | error = xfs_qm_dqattach(dp, 0); |
| 229 | if (error) |
| 230 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * If the inode doesn't have an attribute fork, add one. |
| 234 | * (inode must not be locked when we call this routine) |
| 235 | */ |
| 236 | if (XFS_IFORK_Q(dp) == 0) { |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 237 | int sf_size = sizeof(xfs_attr_sf_hdr_t) + |
Christoph Hellwig | 67fd718 | 2014-05-13 16:34:43 +1000 | [diff] [blame] | 238 | XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen); |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 239 | |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 240 | error = xfs_bmap_add_attrfork(dp, sf_size, rsvd); |
| 241 | if (error) |
| 242 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | * Start our first transaction of the day. |
| 247 | * |
| 248 | * All future transactions during this code must be "chained" off |
| 249 | * this one via the trans_dup() call. All transactions will contain |
| 250 | * the inode, and the inode will always be marked with trans_ihold(). |
| 251 | * Since the inode will be locked in all transactions, we must log |
| 252 | * the inode in every transaction to let it float upward through |
| 253 | * the log. |
| 254 | */ |
| 255 | args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET); |
| 256 | |
| 257 | /* |
| 258 | * Root fork attributes can use reserved data blocks for this |
| 259 | * operation if necessary |
| 260 | */ |
| 261 | |
| 262 | if (rsvd) |
| 263 | args.trans->t_flags |= XFS_TRANS_RESERVE; |
| 264 | |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 265 | tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres + |
| 266 | M_RES(mp)->tr_attrsetrt.tr_logres * args.total; |
| 267 | tres.tr_logcount = XFS_ATTRSET_LOG_COUNT; |
| 268 | tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; |
| 269 | error = xfs_trans_reserve(args.trans, &tres, args.total, 0); |
Jeff Liu | a21cd50 | 2013-01-28 21:27:53 +0800 | [diff] [blame] | 270 | if (error) { |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 271 | xfs_trans_cancel(args.trans); |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 272 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | xfs_ilock(dp, XFS_ILOCK_EXCL); |
| 275 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 276 | error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0, |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 277 | rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 278 | XFS_QMOPT_RES_REGBLKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | if (error) { |
| 280 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 281 | xfs_trans_cancel(args.trans); |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 282 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 285 | xfs_trans_ijoin(args.trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 288 | * If the attribute list is non-existent or a shortform list, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | * upgrade it to a single-leaf-block attribute list. |
| 290 | */ |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 291 | if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL || |
| 292 | (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS && |
| 293 | dp->i_d.di_anextents == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | /* |
| 296 | * Build initial attribute list (if required). |
| 297 | */ |
| 298 | if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 299 | xfs_attr_shortform_create(&args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * Try to add the attr to the attribute list in |
| 303 | * the inode. |
| 304 | */ |
| 305 | error = xfs_attr_shortform_addname(&args); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 306 | if (error != -ENOSPC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | /* |
| 308 | * Commit the shortform mods, and we're done. |
| 309 | * NOTE: this is also the error path (EEXIST, etc). |
| 310 | */ |
| 311 | ASSERT(args.trans != NULL); |
| 312 | |
| 313 | /* |
| 314 | * If this is a synchronous mount, make sure that |
| 315 | * the transaction goes to disk before returning |
| 316 | * to the user. |
| 317 | */ |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 318 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | xfs_trans_set_sync(args.trans); |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 320 | |
| 321 | if (!error && (flags & ATTR_KERNOTIME) == 0) { |
| 322 | xfs_trans_ichgtime(args.trans, dp, |
| 323 | XFS_ICHGTIME_CHG); |
| 324 | } |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 325 | err2 = xfs_trans_commit(args.trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 327 | |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 328 | return error ? error : err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
| 332 | * It won't fit in the shortform, transform to a leaf block. |
| 333 | * GROT: another possible req'mt for a double-split btree op. |
| 334 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 335 | xfs_bmap_init(args.flist, args.firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | error = xfs_attr_shortform_to_leaf(&args); |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 337 | if (!error) |
| 338 | error = xfs_bmap_finish(&args.trans, args.flist, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | args.trans = NULL; |
| 341 | xfs_bmap_cancel(&flist); |
| 342 | goto out; |
| 343 | } |
| 344 | |
| 345 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | * Commit the leaf transformation. We'll need another (linked) |
| 347 | * transaction to add the new attribute to the leaf. |
| 348 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 349 | |
| 350 | error = xfs_trans_roll(&args.trans, dp); |
| 351 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | goto out; |
| 353 | |
| 354 | } |
| 355 | |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 356 | if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | error = xfs_attr_leaf_addname(&args); |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 358 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | error = xfs_attr_node_addname(&args); |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 360 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | /* |
| 364 | * If this is a synchronous mount, make sure that the |
| 365 | * transaction goes to disk before returning to the user. |
| 366 | */ |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 367 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | xfs_trans_set_sync(args.trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 370 | if ((flags & ATTR_KERNOTIME) == 0) |
| 371 | xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG); |
| 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | /* |
| 374 | * Commit the last in the sequence of transactions. |
| 375 | */ |
| 376 | xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE); |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 377 | error = xfs_trans_commit(args.trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 379 | |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 380 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
| 382 | out: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 383 | if (args.trans) |
| 384 | xfs_trans_cancel(args.trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 386 | return error; |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Generic handler routine to remove a name from an attribute list. |
| 391 | * Transitions attribute list from Btree to shortform as necessary. |
| 392 | */ |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 393 | int |
| 394 | xfs_attr_remove( |
| 395 | struct xfs_inode *dp, |
| 396 | const unsigned char *name, |
| 397 | int flags) |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 398 | { |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 399 | struct xfs_mount *mp = dp->i_mount; |
| 400 | struct xfs_da_args args; |
| 401 | struct xfs_bmap_free flist; |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 402 | xfs_fsblock_t firstblock; |
| 403 | int error; |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 404 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 405 | XFS_STATS_INC(mp, xs_attr_remove); |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 406 | |
| 407 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 408 | return -EIO; |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 409 | |
| 410 | if (!xfs_inode_hasattr(dp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 411 | return -ENOATTR; |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 412 | |
Christoph Hellwig | 67fd718 | 2014-05-13 16:34:43 +1000 | [diff] [blame] | 413 | error = xfs_attr_args_init(&args, dp, name, flags); |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 414 | if (error) |
| 415 | return error; |
| 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | args.firstblock = &firstblock; |
| 418 | args.flist = &flist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | /* |
Dave Chinner | 4a33821 | 2011-06-23 01:35:01 +0000 | [diff] [blame] | 421 | * we have no control over the attribute names that userspace passes us |
| 422 | * to remove, so we have to allow the name lookup prior to attribute |
| 423 | * removal to fail. |
| 424 | */ |
| 425 | args.op_flags = XFS_DA_OP_OKNOENT; |
| 426 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 427 | error = xfs_qm_dqattach(dp, 0); |
| 428 | if (error) |
| 429 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | /* |
| 432 | * Start our first transaction of the day. |
| 433 | * |
| 434 | * All future transactions during this code must be "chained" off |
| 435 | * this one via the trans_dup() call. All transactions will contain |
| 436 | * the inode, and the inode will always be marked with trans_ihold(). |
| 437 | * Since the inode will be locked in all transactions, we must log |
| 438 | * the inode in every transaction to let it float upward through |
| 439 | * the log. |
| 440 | */ |
| 441 | args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM); |
| 442 | |
| 443 | /* |
| 444 | * Root fork attributes can use reserved data blocks for this |
| 445 | * operation if necessary |
| 446 | */ |
| 447 | |
| 448 | if (flags & ATTR_ROOT) |
| 449 | args.trans->t_flags |= XFS_TRANS_RESERVE; |
| 450 | |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 451 | error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm, |
| 452 | XFS_ATTRRM_SPACE_RES(mp), 0); |
| 453 | if (error) { |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 454 | xfs_trans_cancel(args.trans); |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 455 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | xfs_ilock(dp, XFS_ILOCK_EXCL); |
| 459 | /* |
| 460 | * No need to make quota reservations here. We expect to release some |
| 461 | * blocks not allocate in the common case. |
| 462 | */ |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 463 | xfs_trans_ijoin(args.trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 465 | if (!xfs_inode_hasattr(dp)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 466 | error = -ENOATTR; |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 467 | } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | ASSERT(dp->i_afp->if_flags & XFS_IFINLINE); |
| 469 | error = xfs_attr_shortform_remove(&args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) { |
| 471 | error = xfs_attr_leaf_removename(&args); |
| 472 | } else { |
| 473 | error = xfs_attr_node_removename(&args); |
| 474 | } |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 475 | |
| 476 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * If this is a synchronous mount, make sure that the |
| 481 | * transaction goes to disk before returning to the user. |
| 482 | */ |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 483 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | xfs_trans_set_sync(args.trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 486 | if ((flags & ATTR_KERNOTIME) == 0) |
| 487 | xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG); |
| 488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | /* |
| 490 | * Commit the last in the sequence of transactions. |
| 491 | */ |
| 492 | xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE); |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 493 | error = xfs_trans_commit(args.trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 495 | |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 496 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | out: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 499 | if (args.trans) |
| 500 | xfs_trans_cancel(args.trans); |
Christoph Hellwig | 1bc426a | 2014-05-13 16:34:33 +1000 | [diff] [blame] | 501 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 502 | return error; |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 503 | } |
| 504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | /*======================================================================== |
| 506 | * External routines when attribute list is inside the inode |
| 507 | *========================================================================*/ |
| 508 | |
| 509 | /* |
| 510 | * Add a name to the shortform attribute list structure |
| 511 | * This is the external routine. |
| 512 | */ |
| 513 | STATIC int |
| 514 | xfs_attr_shortform_addname(xfs_da_args_t *args) |
| 515 | { |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 516 | int newsize, forkoff, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 518 | trace_xfs_attr_sf_addname(args); |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | retval = xfs_attr_shortform_lookup(args); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 521 | if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) { |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 522 | return retval; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 523 | } else if (retval == -EEXIST) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | if (args->flags & ATTR_CREATE) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 525 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | retval = xfs_attr_shortform_remove(args); |
| 527 | ASSERT(retval == 0); |
| 528 | } |
| 529 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 530 | if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX || |
| 531 | args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 532 | return -ENOSPC; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | newsize = XFS_ATTR_SF_TOTSIZE(args->dp); |
| 535 | newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 536 | |
| 537 | forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize); |
| 538 | if (!forkoff) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 539 | return -ENOSPC; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 540 | |
| 541 | xfs_attr_shortform_add(args, forkoff); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 542 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | |
| 546 | /*======================================================================== |
| 547 | * External routines when attribute list is one block |
| 548 | *========================================================================*/ |
| 549 | |
| 550 | /* |
| 551 | * Add a name to the leaf attribute list structure |
| 552 | * |
| 553 | * This leaf block cannot have a "remote" value, we only call this routine |
| 554 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 555 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 556 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | xfs_attr_leaf_addname(xfs_da_args_t *args) |
| 558 | { |
| 559 | xfs_inode_t *dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 560 | struct xfs_buf *bp; |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 561 | int retval, error, forkoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 563 | trace_xfs_attr_leaf_addname(args); |
| 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | /* |
| 566 | * Read the (only) block in the attribute list in. |
| 567 | */ |
| 568 | dp = args->dp; |
| 569 | args->blkno = 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 570 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | if (error) |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 572 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
| 574 | /* |
| 575 | * Look up the given attribute in the leaf block. Figure out if |
| 576 | * the given flags produce an error or call for an atomic rename. |
| 577 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 578 | retval = xfs_attr3_leaf_lookup_int(bp, args); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 579 | if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 580 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 581 | return retval; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 582 | } else if (retval == -EEXIST) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | if (args->flags & ATTR_CREATE) { /* pure create op */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 584 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 585 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 587 | |
| 588 | trace_xfs_attr_leaf_replace(args); |
| 589 | |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 590 | /* save the attribute state for later removal*/ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 591 | args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | args->blkno2 = args->blkno; /* set 2nd entry info*/ |
| 593 | args->index2 = args->index; |
| 594 | args->rmtblkno2 = args->rmtblkno; |
| 595 | args->rmtblkcnt2 = args->rmtblkcnt; |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 596 | args->rmtvaluelen2 = args->rmtvaluelen; |
| 597 | |
| 598 | /* |
| 599 | * clear the remote attr state now that it is saved so that the |
| 600 | * values reflect the state of the attribute we are about to |
| 601 | * add, not the attribute we just found and will remove later. |
| 602 | */ |
| 603 | args->rmtblkno = 0; |
| 604 | args->rmtblkcnt = 0; |
| 605 | args->rmtvaluelen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* |
| 609 | * Add the attribute to the leaf block, transitioning to a Btree |
| 610 | * if required. |
| 611 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 612 | retval = xfs_attr3_leaf_add(bp, args); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 613 | if (retval == -ENOSPC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | /* |
| 615 | * Promote the attribute list to the Btree format, then |
| 616 | * Commit that transaction so that the node_addname() call |
| 617 | * can manage its own transactions. |
| 618 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 619 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 620 | error = xfs_attr3_leaf_to_node(args); |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 621 | if (!error) |
| 622 | error = xfs_bmap_finish(&args->trans, args->flist, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | args->trans = NULL; |
| 625 | xfs_bmap_cancel(args->flist); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 626 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | * Commit the current trans (including the inode) and start |
| 631 | * a new one. |
| 632 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 633 | error = xfs_trans_roll(&args->trans, dp); |
| 634 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 635 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
| 637 | /* |
| 638 | * Fob the whole rest of the problem off on the Btree code. |
| 639 | */ |
| 640 | error = xfs_attr_node_addname(args); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 641 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Commit the transaction that added the attr name so that |
| 646 | * later routines can manage their own transactions. |
| 647 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 648 | error = xfs_trans_roll(&args->trans, dp); |
| 649 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 650 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
| 652 | /* |
| 653 | * If there was an out-of-line value, allocate the blocks we |
| 654 | * identified for its storage and copy the value. This is done |
| 655 | * after we create the attribute so that we don't overflow the |
| 656 | * maximum size of a transaction and/or hit a deadlock. |
| 657 | */ |
| 658 | if (args->rmtblkno > 0) { |
| 659 | error = xfs_attr_rmtval_set(args); |
| 660 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 661 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | * If this is an atomic rename operation, we must "flip" the |
| 666 | * incomplete flags on the "new" and "old" attribute/value pairs |
| 667 | * so that one disappears and one appears atomically. Then we |
| 668 | * must remove the "old" attribute/value pair. |
| 669 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 670 | if (args->op_flags & XFS_DA_OP_RENAME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | /* |
| 672 | * In a separate transaction, set the incomplete flag on the |
| 673 | * "old" attr and clear the incomplete flag on the "new" attr. |
| 674 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 675 | error = xfs_attr3_leaf_flipflags(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 677 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
| 679 | /* |
| 680 | * Dismantle the "old" attribute/value pair by removing |
| 681 | * a "remote" value (if it exists). |
| 682 | */ |
| 683 | args->index = args->index2; |
| 684 | args->blkno = args->blkno2; |
| 685 | args->rmtblkno = args->rmtblkno2; |
| 686 | args->rmtblkcnt = args->rmtblkcnt2; |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 687 | args->rmtvaluelen = args->rmtvaluelen2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | if (args->rmtblkno) { |
| 689 | error = xfs_attr_rmtval_remove(args); |
| 690 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 691 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* |
| 695 | * Read in the block containing the "old" attr, then |
| 696 | * remove the "old" attr from that block (neat, huh!) |
| 697 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 698 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 699 | -1, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | if (error) |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 701 | return error; |
| 702 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 703 | xfs_attr3_leaf_remove(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
| 705 | /* |
| 706 | * If the result is small enough, shrink it all into the inode. |
| 707 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 708 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 709 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 710 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | /* bp is gone due to xfs_da_shrink_inode */ |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 712 | if (!error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | error = xfs_bmap_finish(&args->trans, |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 714 | args->flist, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | args->trans = NULL; |
| 717 | xfs_bmap_cancel(args->flist); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 718 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 720 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
| 722 | /* |
| 723 | * Commit the remove and start the next trans in series. |
| 724 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 725 | error = xfs_trans_roll(&args->trans, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
| 727 | } else if (args->rmtblkno > 0) { |
| 728 | /* |
| 729 | * Added a "remote" value, just clear the incomplete flag. |
| 730 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 731 | error = xfs_attr3_leaf_clearflag(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 733 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /* |
| 737 | * Remove a name from the leaf attribute list structure |
| 738 | * |
| 739 | * This leaf block cannot have a "remote" value, we only call this routine |
| 740 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 741 | */ |
| 742 | STATIC int |
| 743 | xfs_attr_leaf_removename(xfs_da_args_t *args) |
| 744 | { |
| 745 | xfs_inode_t *dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 746 | struct xfs_buf *bp; |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 747 | int error, forkoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 749 | trace_xfs_attr_leaf_removename(args); |
| 750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | /* |
| 752 | * Remove the attribute. |
| 753 | */ |
| 754 | dp = args->dp; |
| 755 | args->blkno = 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 756 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 757 | if (error) |
| 758 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 760 | error = xfs_attr3_leaf_lookup_int(bp, args); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 761 | if (error == -ENOATTR) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 762 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 763 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 766 | xfs_attr3_leaf_remove(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
| 768 | /* |
| 769 | * If the result is small enough, shrink it all into the inode. |
| 770 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 771 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 772 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 773 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | /* bp is gone due to xfs_da_shrink_inode */ |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 775 | if (!error) |
| 776 | error = xfs_bmap_finish(&args->trans, args->flist, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | args->trans = NULL; |
| 779 | xfs_bmap_cancel(args->flist); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 780 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 782 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 783 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /* |
| 787 | * Look up a name in a leaf attribute list structure. |
| 788 | * |
| 789 | * This leaf block cannot have a "remote" value, we only call this routine |
| 790 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 791 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 792 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | xfs_attr_leaf_get(xfs_da_args_t *args) |
| 794 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 795 | struct xfs_buf *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | int error; |
| 797 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 798 | trace_xfs_attr_leaf_get(args); |
| 799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | args->blkno = 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 801 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | if (error) |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 803 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 805 | error = xfs_attr3_leaf_lookup_int(bp, args); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 806 | if (error != -EEXIST) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 807 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 808 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 810 | error = xfs_attr3_leaf_getvalue(bp, args); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 811 | xfs_trans_brelse(args->trans, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) { |
| 813 | error = xfs_attr_rmtval_get(args); |
| 814 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 815 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | /*======================================================================== |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 819 | * External routines when attribute list size > geo->blksize |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | *========================================================================*/ |
| 821 | |
| 822 | /* |
| 823 | * Add a name to a Btree-format attribute list. |
| 824 | * |
| 825 | * This will involve walking down the Btree, and may involve splitting |
| 826 | * leaf nodes and even splitting intermediate nodes up to and including |
| 827 | * the root node (a special case of an intermediate node). |
| 828 | * |
| 829 | * "Remote" attribute values confuse the issue and atomic rename operations |
| 830 | * add a whole extra layer of confusion on top of that. |
| 831 | */ |
| 832 | STATIC int |
| 833 | xfs_attr_node_addname(xfs_da_args_t *args) |
| 834 | { |
| 835 | xfs_da_state_t *state; |
| 836 | xfs_da_state_blk_t *blk; |
| 837 | xfs_inode_t *dp; |
| 838 | xfs_mount_t *mp; |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 839 | int retval, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 841 | trace_xfs_attr_node_addname(args); |
| 842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | /* |
| 844 | * Fill in bucket of arguments/results/context to carry around. |
| 845 | */ |
| 846 | dp = args->dp; |
| 847 | mp = dp->i_mount; |
| 848 | restart: |
| 849 | state = xfs_da_state_alloc(); |
| 850 | state->args = args; |
| 851 | state->mp = mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
| 853 | /* |
| 854 | * Search to see if name already exists, and get back a pointer |
| 855 | * to where it should go. |
| 856 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 857 | error = xfs_da3_node_lookup_int(state, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | if (error) |
| 859 | goto out; |
| 860 | blk = &state->path.blk[ state->path.active-1 ]; |
| 861 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 862 | if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | goto out; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 864 | } else if (retval == -EEXIST) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | if (args->flags & ATTR_CREATE) |
| 866 | goto out; |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 867 | |
| 868 | trace_xfs_attr_node_replace(args); |
| 869 | |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 870 | /* save the attribute state for later removal*/ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 871 | args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | args->blkno2 = args->blkno; /* set 2nd entry info*/ |
| 873 | args->index2 = args->index; |
| 874 | args->rmtblkno2 = args->rmtblkno; |
| 875 | args->rmtblkcnt2 = args->rmtblkcnt; |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 876 | args->rmtvaluelen2 = args->rmtvaluelen; |
| 877 | |
| 878 | /* |
| 879 | * clear the remote attr state now that it is saved so that the |
| 880 | * values reflect the state of the attribute we are about to |
| 881 | * add, not the attribute we just found and will remove later. |
| 882 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | args->rmtblkno = 0; |
| 884 | args->rmtblkcnt = 0; |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 885 | args->rmtvaluelen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 888 | retval = xfs_attr3_leaf_add(blk->bp, state->args); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 889 | if (retval == -ENOSPC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | if (state->path.active == 1) { |
| 891 | /* |
| 892 | * Its really a single leaf node, but it had |
| 893 | * out-of-line values so it looked like it *might* |
| 894 | * have been a b-tree. |
| 895 | */ |
| 896 | xfs_da_state_free(state); |
Eric Sandeen | 6dd93e9 | 2013-07-31 20:18:54 -0500 | [diff] [blame] | 897 | state = NULL; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 898 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 899 | error = xfs_attr3_leaf_to_node(args); |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 900 | if (!error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | error = xfs_bmap_finish(&args->trans, |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 902 | args->flist, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | args->trans = NULL; |
| 905 | xfs_bmap_cancel(args->flist); |
| 906 | goto out; |
| 907 | } |
| 908 | |
| 909 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | * Commit the node conversion and start the next |
| 911 | * trans in the chain. |
| 912 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 913 | error = xfs_trans_roll(&args->trans, dp); |
| 914 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | goto out; |
| 916 | |
| 917 | goto restart; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * Split as many Btree elements as required. |
| 922 | * This code tracks the new and old attr's location |
| 923 | * in the index/blkno/rmtblkno/rmtblkcnt fields and |
| 924 | * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields. |
| 925 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 926 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 927 | error = xfs_da3_split(state); |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 928 | if (!error) |
| 929 | error = xfs_bmap_finish(&args->trans, args->flist, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | args->trans = NULL; |
| 932 | xfs_bmap_cancel(args->flist); |
| 933 | goto out; |
| 934 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } else { |
| 936 | /* |
| 937 | * Addition succeeded, update Btree hashvals. |
| 938 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 939 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* |
| 943 | * Kill the state structure, we're done with it and need to |
| 944 | * allow the buffers to come back later. |
| 945 | */ |
| 946 | xfs_da_state_free(state); |
| 947 | state = NULL; |
| 948 | |
| 949 | /* |
| 950 | * Commit the leaf addition or btree split and start the next |
| 951 | * trans in the chain. |
| 952 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 953 | error = xfs_trans_roll(&args->trans, dp); |
| 954 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | goto out; |
| 956 | |
| 957 | /* |
| 958 | * If there was an out-of-line value, allocate the blocks we |
| 959 | * identified for its storage and copy the value. This is done |
| 960 | * after we create the attribute so that we don't overflow the |
| 961 | * maximum size of a transaction and/or hit a deadlock. |
| 962 | */ |
| 963 | if (args->rmtblkno > 0) { |
| 964 | error = xfs_attr_rmtval_set(args); |
| 965 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 966 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /* |
| 970 | * If this is an atomic rename operation, we must "flip" the |
| 971 | * incomplete flags on the "new" and "old" attribute/value pairs |
| 972 | * so that one disappears and one appears atomically. Then we |
| 973 | * must remove the "old" attribute/value pair. |
| 974 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 975 | if (args->op_flags & XFS_DA_OP_RENAME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | /* |
| 977 | * In a separate transaction, set the incomplete flag on the |
| 978 | * "old" attr and clear the incomplete flag on the "new" attr. |
| 979 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 980 | error = xfs_attr3_leaf_flipflags(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | if (error) |
| 982 | goto out; |
| 983 | |
| 984 | /* |
| 985 | * Dismantle the "old" attribute/value pair by removing |
| 986 | * a "remote" value (if it exists). |
| 987 | */ |
| 988 | args->index = args->index2; |
| 989 | args->blkno = args->blkno2; |
| 990 | args->rmtblkno = args->rmtblkno2; |
| 991 | args->rmtblkcnt = args->rmtblkcnt2; |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 992 | args->rmtvaluelen = args->rmtvaluelen2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | if (args->rmtblkno) { |
| 994 | error = xfs_attr_rmtval_remove(args); |
| 995 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 996 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /* |
| 1000 | * Re-find the "old" attribute entry after any split ops. |
| 1001 | * The INCOMPLETE flag means that we will find the "old" |
| 1002 | * attr, not the "new" one. |
| 1003 | */ |
| 1004 | args->flags |= XFS_ATTR_INCOMPLETE; |
| 1005 | state = xfs_da_state_alloc(); |
| 1006 | state->args = args; |
| 1007 | state->mp = mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | state->inleaf = 0; |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1009 | error = xfs_da3_node_lookup_int(state, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | if (error) |
| 1011 | goto out; |
| 1012 | |
| 1013 | /* |
| 1014 | * Remove the name and update the hashvals in the tree. |
| 1015 | */ |
| 1016 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1017 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1018 | error = xfs_attr3_leaf_remove(blk->bp, args); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1019 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
| 1021 | /* |
| 1022 | * Check to see if the tree needs to be collapsed. |
| 1023 | */ |
| 1024 | if (retval && (state->path.active > 1)) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1025 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1026 | error = xfs_da3_join(state); |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 1027 | if (!error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | error = xfs_bmap_finish(&args->trans, |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 1029 | args->flist, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | args->trans = NULL; |
| 1032 | xfs_bmap_cancel(args->flist); |
| 1033 | goto out; |
| 1034 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Commit and start the next trans in the chain. |
| 1039 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1040 | error = xfs_trans_roll(&args->trans, dp); |
| 1041 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | goto out; |
| 1043 | |
| 1044 | } else if (args->rmtblkno > 0) { |
| 1045 | /* |
| 1046 | * Added a "remote" value, just clear the incomplete flag. |
| 1047 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1048 | error = xfs_attr3_leaf_clearflag(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | if (error) |
| 1050 | goto out; |
| 1051 | } |
| 1052 | retval = error = 0; |
| 1053 | |
| 1054 | out: |
| 1055 | if (state) |
| 1056 | xfs_da_state_free(state); |
| 1057 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1058 | return error; |
| 1059 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * Remove a name from a B-tree attribute list. |
| 1064 | * |
| 1065 | * This will involve walking down the Btree, and may involve joining |
| 1066 | * leaf nodes and even joining intermediate nodes up to and including |
| 1067 | * the root node (a special case of an intermediate node). |
| 1068 | */ |
| 1069 | STATIC int |
| 1070 | xfs_attr_node_removename(xfs_da_args_t *args) |
| 1071 | { |
| 1072 | xfs_da_state_t *state; |
| 1073 | xfs_da_state_blk_t *blk; |
| 1074 | xfs_inode_t *dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1075 | struct xfs_buf *bp; |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 1076 | int retval, error, forkoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1078 | trace_xfs_attr_node_removename(args); |
| 1079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | /* |
| 1081 | * Tie a string around our finger to remind us where we are. |
| 1082 | */ |
| 1083 | dp = args->dp; |
| 1084 | state = xfs_da_state_alloc(); |
| 1085 | state->args = args; |
| 1086 | state->mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
| 1088 | /* |
| 1089 | * Search to see if name exists, and get back a pointer to it. |
| 1090 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1091 | error = xfs_da3_node_lookup_int(state, &retval); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1092 | if (error || (retval != -EEXIST)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | if (error == 0) |
| 1094 | error = retval; |
| 1095 | goto out; |
| 1096 | } |
| 1097 | |
| 1098 | /* |
| 1099 | * If there is an out-of-line value, de-allocate the blocks. |
| 1100 | * This is done before we remove the attribute so that we don't |
| 1101 | * overflow the maximum size of a transaction and/or hit a deadlock. |
| 1102 | */ |
| 1103 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1104 | ASSERT(blk->bp != NULL); |
| 1105 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1106 | if (args->rmtblkno > 0) { |
| 1107 | /* |
| 1108 | * Fill in disk block numbers in the state structure |
| 1109 | * so that we can get the buffers back after we commit |
| 1110 | * several transactions in the following calls. |
| 1111 | */ |
| 1112 | error = xfs_attr_fillstate(state); |
| 1113 | if (error) |
| 1114 | goto out; |
| 1115 | |
| 1116 | /* |
| 1117 | * Mark the attribute as INCOMPLETE, then bunmapi() the |
| 1118 | * remote value. |
| 1119 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1120 | error = xfs_attr3_leaf_setflag(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | if (error) |
| 1122 | goto out; |
| 1123 | error = xfs_attr_rmtval_remove(args); |
| 1124 | if (error) |
| 1125 | goto out; |
| 1126 | |
| 1127 | /* |
| 1128 | * Refill the state structure with buffers, the prior calls |
| 1129 | * released our buffers. |
| 1130 | */ |
| 1131 | error = xfs_attr_refillstate(state); |
| 1132 | if (error) |
| 1133 | goto out; |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * Remove the name and update the hashvals in the tree. |
| 1138 | */ |
| 1139 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1140 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1141 | retval = xfs_attr3_leaf_remove(blk->bp, args); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1142 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | |
| 1144 | /* |
| 1145 | * Check to see if the tree needs to be collapsed. |
| 1146 | */ |
| 1147 | if (retval && (state->path.active > 1)) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1148 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1149 | error = xfs_da3_join(state); |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 1150 | if (!error) |
| 1151 | error = xfs_bmap_finish(&args->trans, args->flist, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | args->trans = NULL; |
| 1154 | xfs_bmap_cancel(args->flist); |
| 1155 | goto out; |
| 1156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | /* |
| 1158 | * Commit the Btree join operation and start a new trans. |
| 1159 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1160 | error = xfs_trans_roll(&args->trans, dp); |
| 1161 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | goto out; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * If the result is small enough, push it all into the inode. |
| 1167 | */ |
| 1168 | if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) { |
| 1169 | /* |
| 1170 | * Have to get rid of the copy of this dabuf in the state. |
| 1171 | */ |
| 1172 | ASSERT(state->path.active == 1); |
| 1173 | ASSERT(state->path.blk[0].bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | state->path.blk[0].bp = NULL; |
| 1175 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1176 | error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | if (error) |
| 1178 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 1180 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1181 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1182 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | /* bp is gone due to xfs_da_shrink_inode */ |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 1184 | if (!error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | error = xfs_bmap_finish(&args->trans, |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 1186 | args->flist, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | args->trans = NULL; |
| 1189 | xfs_bmap_cancel(args->flist); |
| 1190 | goto out; |
| 1191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | } else |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1193 | xfs_trans_brelse(args->trans, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | } |
| 1195 | error = 0; |
| 1196 | |
| 1197 | out: |
| 1198 | xfs_da_state_free(state); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1199 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * Fill in the disk block numbers in the state structure for the buffers |
| 1204 | * that are attached to the state structure. |
| 1205 | * This is done so that we can quickly reattach ourselves to those buffers |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1206 | * after some set of transaction commits have released these buffers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | */ |
| 1208 | STATIC int |
| 1209 | xfs_attr_fillstate(xfs_da_state_t *state) |
| 1210 | { |
| 1211 | xfs_da_state_path_t *path; |
| 1212 | xfs_da_state_blk_t *blk; |
| 1213 | int level; |
| 1214 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1215 | trace_xfs_attr_fillstate(state->args); |
| 1216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | /* |
| 1218 | * Roll down the "path" in the state structure, storing the on-disk |
| 1219 | * block number for those buffers in the "path". |
| 1220 | */ |
| 1221 | path = &state->path; |
| 1222 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1223 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1224 | if (blk->bp) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1225 | blk->disk_blkno = XFS_BUF_ADDR(blk->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | blk->bp = NULL; |
| 1227 | } else { |
| 1228 | blk->disk_blkno = 0; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * Roll down the "altpath" in the state structure, storing the on-disk |
| 1234 | * block number for those buffers in the "altpath". |
| 1235 | */ |
| 1236 | path = &state->altpath; |
| 1237 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1238 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1239 | if (blk->bp) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1240 | blk->disk_blkno = XFS_BUF_ADDR(blk->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | blk->bp = NULL; |
| 1242 | } else { |
| 1243 | blk->disk_blkno = 0; |
| 1244 | } |
| 1245 | } |
| 1246 | |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1247 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * Reattach the buffers to the state structure based on the disk block |
| 1252 | * numbers stored in the state structure. |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1253 | * This is done after some set of transaction commits have released those |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | * buffers from our grip. |
| 1255 | */ |
| 1256 | STATIC int |
| 1257 | xfs_attr_refillstate(xfs_da_state_t *state) |
| 1258 | { |
| 1259 | xfs_da_state_path_t *path; |
| 1260 | xfs_da_state_blk_t *blk; |
| 1261 | int level, error; |
| 1262 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1263 | trace_xfs_attr_refillstate(state->args); |
| 1264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | /* |
| 1266 | * Roll down the "path" in the state structure, storing the on-disk |
| 1267 | * block number for those buffers in the "path". |
| 1268 | */ |
| 1269 | path = &state->path; |
| 1270 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1271 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1272 | if (blk->disk_blkno) { |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1273 | error = xfs_da3_node_read(state->args->trans, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | state->args->dp, |
| 1275 | blk->blkno, blk->disk_blkno, |
Dave Chinner | d9392a4 | 2012-11-12 22:54:17 +1100 | [diff] [blame] | 1276 | &blk->bp, XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1278 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | } else { |
| 1280 | blk->bp = NULL; |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Roll down the "altpath" in the state structure, storing the on-disk |
| 1286 | * block number for those buffers in the "altpath". |
| 1287 | */ |
| 1288 | path = &state->altpath; |
| 1289 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1290 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1291 | if (blk->disk_blkno) { |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1292 | error = xfs_da3_node_read(state->args->trans, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | state->args->dp, |
| 1294 | blk->blkno, blk->disk_blkno, |
Dave Chinner | d9392a4 | 2012-11-12 22:54:17 +1100 | [diff] [blame] | 1295 | &blk->bp, XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1297 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | } else { |
| 1299 | blk->bp = NULL; |
| 1300 | } |
| 1301 | } |
| 1302 | |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1303 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * Look up a filename in a node attribute list. |
| 1308 | * |
| 1309 | * This routine gets called for any attribute fork that has more than one |
| 1310 | * block, ie: both true Btree attr lists and for single-leaf-blocks with |
| 1311 | * "remote" values taking up more blocks. |
| 1312 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1313 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | xfs_attr_node_get(xfs_da_args_t *args) |
| 1315 | { |
| 1316 | xfs_da_state_t *state; |
| 1317 | xfs_da_state_blk_t *blk; |
| 1318 | int error, retval; |
| 1319 | int i; |
| 1320 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1321 | trace_xfs_attr_node_get(args); |
| 1322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | state = xfs_da_state_alloc(); |
| 1324 | state->args = args; |
| 1325 | state->mp = args->dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | |
| 1327 | /* |
| 1328 | * Search to see if name exists, and get back a pointer to it. |
| 1329 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1330 | error = xfs_da3_node_lookup_int(state, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | if (error) { |
| 1332 | retval = error; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1333 | } else if (retval == -EEXIST) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1335 | ASSERT(blk->bp != NULL); |
| 1336 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1337 | |
| 1338 | /* |
| 1339 | * Get the value, local or "remote" |
| 1340 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1341 | retval = xfs_attr3_leaf_getvalue(blk->bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | if (!retval && (args->rmtblkno > 0) |
| 1343 | && !(args->flags & ATTR_KERNOVAL)) { |
| 1344 | retval = xfs_attr_rmtval_get(args); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * If not in a transaction, we have to release all the buffers. |
| 1350 | */ |
| 1351 | for (i = 0; i < state->path.active; i++) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1352 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | state->path.blk[i].bp = NULL; |
| 1354 | } |
| 1355 | |
| 1356 | xfs_da_state_free(state); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1357 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | } |