David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 4 | * |
| 5 | * This copyrighted material is made available to anyone wishing to use, |
| 6 | * modify, copy, or redistribute it subject to the terms and conditions |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/completion.h> |
| 13 | #include <linux/buffer_head.h> |
| 14 | #include <linux/xattr.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 15 | #include <linux/gfs2_ondisk.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 16 | #include <asm/uaccess.h> |
| 17 | |
| 18 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 19 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 20 | #include "acl.h" |
| 21 | #include "eaops.h" |
| 22 | #include "eattr.h" |
| 23 | #include "glock.h" |
| 24 | #include "inode.h" |
| 25 | #include "meta_io.h" |
| 26 | #include "quota.h" |
| 27 | #include "rgrp.h" |
| 28 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 29 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * ea_calc_size - returns the acutal number of bytes the request will take up |
| 33 | * (not counting any unstuffed data blocks) |
| 34 | * @sdp: |
| 35 | * @er: |
| 36 | * @size: |
| 37 | * |
| 38 | * Returns: 1 if the EA should be stuffed |
| 39 | */ |
| 40 | |
| 41 | static int ea_calc_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er, |
| 42 | unsigned int *size) |
| 43 | { |
| 44 | *size = GFS2_EAREQ_SIZE_STUFFED(er); |
| 45 | if (*size <= sdp->sd_jbsize) |
| 46 | return 1; |
| 47 | |
| 48 | *size = GFS2_EAREQ_SIZE_UNSTUFFED(sdp, er); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static int ea_check_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er) |
| 54 | { |
| 55 | unsigned int size; |
| 56 | |
| 57 | if (er->er_data_len > GFS2_EA_MAX_DATA_LEN) |
| 58 | return -ERANGE; |
| 59 | |
| 60 | ea_calc_size(sdp, er, &size); |
| 61 | |
| 62 | /* This can only happen with 512 byte blocks */ |
| 63 | if (size > sdp->sd_jbsize) |
| 64 | return -ERANGE; |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 69 | typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 70 | struct gfs2_ea_header *ea, |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 71 | struct gfs2_ea_header *prev, void *private); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 72 | |
| 73 | static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh, |
| 74 | ea_call_t ea_call, void *data) |
| 75 | { |
| 76 | struct gfs2_ea_header *ea, *prev = NULL; |
| 77 | int error = 0; |
| 78 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 79 | if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 80 | return -EIO; |
| 81 | |
| 82 | for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) { |
| 83 | if (!GFS2_EA_REC_LEN(ea)) |
| 84 | goto fail; |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 85 | if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <= |
| 86 | bh->b_data + bh->b_size)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 87 | goto fail; |
| 88 | if (!GFS2_EATYPE_VALID(ea->ea_type)) |
| 89 | goto fail; |
| 90 | |
| 91 | error = ea_call(ip, bh, ea, prev, data); |
| 92 | if (error) |
| 93 | return error; |
| 94 | |
| 95 | if (GFS2_EA_IS_LAST(ea)) { |
| 96 | if ((char *)GFS2_EA2NEXT(ea) != |
| 97 | bh->b_data + bh->b_size) |
| 98 | goto fail; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return error; |
| 104 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 105 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 106 | gfs2_consist_inode(ip); |
| 107 | return -EIO; |
| 108 | } |
| 109 | |
| 110 | static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data) |
| 111 | { |
| 112 | struct buffer_head *bh, *eabh; |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 113 | __be64 *eablk, *end; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 114 | int error; |
| 115 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 116 | error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, &bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 117 | if (error) |
| 118 | return error; |
| 119 | |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 120 | if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 121 | error = ea_foreach_i(ip, bh, ea_call, data); |
| 122 | goto out; |
| 123 | } |
| 124 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 125 | if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 126 | error = -EIO; |
| 127 | goto out; |
| 128 | } |
| 129 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 130 | eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header)); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 131 | end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 132 | |
| 133 | for (; eablk < end; eablk++) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 134 | u64 bn; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 135 | |
| 136 | if (!*eablk) |
| 137 | break; |
| 138 | bn = be64_to_cpu(*eablk); |
| 139 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 140 | error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, &eabh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 141 | if (error) |
| 142 | break; |
| 143 | error = ea_foreach_i(ip, eabh, ea_call, data); |
| 144 | brelse(eabh); |
| 145 | if (error) |
| 146 | break; |
| 147 | } |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 148 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 149 | brelse(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 150 | return error; |
| 151 | } |
| 152 | |
| 153 | struct ea_find { |
| 154 | struct gfs2_ea_request *ef_er; |
| 155 | struct gfs2_ea_location *ef_el; |
| 156 | }; |
| 157 | |
| 158 | static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh, |
| 159 | struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, |
| 160 | void *private) |
| 161 | { |
| 162 | struct ea_find *ef = private; |
| 163 | struct gfs2_ea_request *er = ef->ef_er; |
| 164 | |
| 165 | if (ea->ea_type == GFS2_EATYPE_UNUSED) |
| 166 | return 0; |
| 167 | |
| 168 | if (ea->ea_type == er->er_type) { |
| 169 | if (ea->ea_name_len == er->er_name_len && |
| 170 | !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) { |
| 171 | struct gfs2_ea_location *el = ef->ef_el; |
| 172 | get_bh(bh); |
| 173 | el->el_bh = bh; |
| 174 | el->el_ea = ea; |
| 175 | el->el_prev = prev; |
| 176 | return 1; |
| 177 | } |
| 178 | } |
| 179 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er, |
| 184 | struct gfs2_ea_location *el) |
| 185 | { |
| 186 | struct ea_find ef; |
| 187 | int error; |
| 188 | |
| 189 | ef.ef_er = er; |
| 190 | ef.ef_el = el; |
| 191 | |
| 192 | memset(el, 0, sizeof(struct gfs2_ea_location)); |
| 193 | |
| 194 | error = ea_foreach(ip, ea_find_i, &ef); |
| 195 | if (error > 0) |
| 196 | return 0; |
| 197 | |
| 198 | return error; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * ea_dealloc_unstuffed - |
| 203 | * @ip: |
| 204 | * @bh: |
| 205 | * @ea: |
| 206 | * @prev: |
| 207 | * @private: |
| 208 | * |
| 209 | * Take advantage of the fact that all unstuffed blocks are |
| 210 | * allocated from the same RG. But watch, this may not always |
| 211 | * be true. |
| 212 | * |
| 213 | * Returns: errno |
| 214 | */ |
| 215 | |
| 216 | static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh, |
| 217 | struct gfs2_ea_header *ea, |
| 218 | struct gfs2_ea_header *prev, void *private) |
| 219 | { |
| 220 | int *leave = private; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 221 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 222 | struct gfs2_rgrpd *rgd; |
| 223 | struct gfs2_holder rg_gh; |
| 224 | struct buffer_head *dibh; |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 225 | __be64 *dataptrs; |
| 226 | u64 bn = 0; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 227 | u64 bstart = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 228 | unsigned int blen = 0; |
| 229 | unsigned int blks = 0; |
| 230 | unsigned int x; |
| 231 | int error; |
| 232 | |
| 233 | if (GFS2_EA_IS_STUFFED(ea)) |
| 234 | return 0; |
| 235 | |
| 236 | dataptrs = GFS2_EA2DATAPTRS(ea); |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 237 | for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 238 | if (*dataptrs) { |
| 239 | blks++; |
| 240 | bn = be64_to_cpu(*dataptrs); |
| 241 | } |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 242 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 243 | if (!blks) |
| 244 | return 0; |
| 245 | |
| 246 | rgd = gfs2_blk2rgrpd(sdp, bn); |
| 247 | if (!rgd) { |
| 248 | gfs2_consist_inode(ip); |
| 249 | return -EIO; |
| 250 | } |
| 251 | |
| 252 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh); |
| 253 | if (error) |
| 254 | return error; |
| 255 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 256 | error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE + |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 257 | RES_EATTR + RES_STATFS + RES_QUOTA, blks); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 258 | if (error) |
| 259 | goto out_gunlock; |
| 260 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 261 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 262 | |
| 263 | dataptrs = GFS2_EA2DATAPTRS(ea); |
| 264 | for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) { |
| 265 | if (!*dataptrs) |
| 266 | break; |
| 267 | bn = be64_to_cpu(*dataptrs); |
| 268 | |
| 269 | if (bstart + blen == bn) |
| 270 | blen++; |
| 271 | else { |
| 272 | if (bstart) |
| 273 | gfs2_free_meta(ip, bstart, blen); |
| 274 | bstart = bn; |
| 275 | blen = 1; |
| 276 | } |
| 277 | |
| 278 | *dataptrs = 0; |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 279 | gfs2_add_inode_blocks(&ip->i_inode, -1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 280 | } |
| 281 | if (bstart) |
| 282 | gfs2_free_meta(ip, bstart, blen); |
| 283 | |
| 284 | if (prev && !leave) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 285 | u32 len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 286 | |
| 287 | len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea); |
| 288 | prev->ea_rec_len = cpu_to_be32(len); |
| 289 | |
| 290 | if (GFS2_EA_IS_LAST(ea)) |
| 291 | prev->ea_flags |= GFS2_EAFLAG_LAST; |
| 292 | } else { |
| 293 | ea->ea_type = GFS2_EATYPE_UNUSED; |
| 294 | ea->ea_num_ptrs = 0; |
| 295 | } |
| 296 | |
| 297 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 298 | if (!error) { |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 299 | ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 300 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 301 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 302 | brelse(dibh); |
| 303 | } |
| 304 | |
| 305 | gfs2_trans_end(sdp); |
| 306 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 307 | out_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 308 | gfs2_glock_dq_uninit(&rg_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 309 | return error; |
| 310 | } |
| 311 | |
| 312 | static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh, |
| 313 | struct gfs2_ea_header *ea, |
| 314 | struct gfs2_ea_header *prev, int leave) |
| 315 | { |
| 316 | struct gfs2_alloc *al; |
| 317 | int error; |
| 318 | |
| 319 | al = gfs2_alloc_get(ip); |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 320 | if (!al) |
| 321 | return -ENOMEM; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 322 | |
| 323 | error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 324 | if (error) |
| 325 | goto out_alloc; |
| 326 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 327 | error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 328 | if (error) |
| 329 | goto out_quota; |
| 330 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 331 | error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 332 | |
| 333 | gfs2_glock_dq_uninit(&al->al_ri_gh); |
| 334 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 335 | out_quota: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 336 | gfs2_quota_unhold(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 337 | out_alloc: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 338 | gfs2_alloc_put(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 339 | return error; |
| 340 | } |
| 341 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 342 | struct ea_list { |
| 343 | struct gfs2_ea_request *ei_er; |
| 344 | unsigned int ei_size; |
| 345 | }; |
| 346 | |
| 347 | static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh, |
| 348 | struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, |
| 349 | void *private) |
| 350 | { |
| 351 | struct ea_list *ei = private; |
| 352 | struct gfs2_ea_request *er = ei->ei_er; |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 353 | unsigned int ea_size = gfs2_ea_strlen(ea); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 354 | |
| 355 | if (ea->ea_type == GFS2_EATYPE_UNUSED) |
| 356 | return 0; |
| 357 | |
| 358 | if (er->er_data_len) { |
Steven Whitehouse | 01eb7c0 | 2006-06-06 17:31:30 -0400 | [diff] [blame] | 359 | char *prefix = NULL; |
| 360 | unsigned int l = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 361 | char c = 0; |
| 362 | |
| 363 | if (ei->ei_size + ea_size > er->er_data_len) |
| 364 | return -ERANGE; |
| 365 | |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 366 | switch (ea->ea_type) { |
| 367 | case GFS2_EATYPE_USR: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 368 | prefix = "user."; |
| 369 | l = 5; |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 370 | break; |
| 371 | case GFS2_EATYPE_SYS: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 372 | prefix = "system."; |
| 373 | l = 7; |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 374 | break; |
| 375 | case GFS2_EATYPE_SECURITY: |
| 376 | prefix = "security."; |
| 377 | l = 9; |
| 378 | break; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Steven Whitehouse | 01eb7c0 | 2006-06-06 17:31:30 -0400 | [diff] [blame] | 381 | BUG_ON(l == 0); |
| 382 | |
Steven Whitehouse | 90cdd20 | 2006-05-22 10:36:25 -0400 | [diff] [blame] | 383 | memcpy(er->er_data + ei->ei_size, prefix, l); |
| 384 | memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea), |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 385 | ea->ea_name_len); |
Steven Whitehouse | 90cdd20 | 2006-05-22 10:36:25 -0400 | [diff] [blame] | 386 | memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | ei->ei_size += ea_size; |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * gfs2_ea_list - |
| 396 | * @ip: |
| 397 | * @er: |
| 398 | * |
| 399 | * Returns: actual size of data on success, -errno on error |
| 400 | */ |
| 401 | |
| 402 | int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 403 | { |
| 404 | struct gfs2_holder i_gh; |
| 405 | int error; |
| 406 | |
| 407 | if (!er->er_data || !er->er_data_len) { |
| 408 | er->er_data = NULL; |
| 409 | er->er_data_len = 0; |
| 410 | } |
| 411 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 412 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 413 | if (error) |
| 414 | return error; |
| 415 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 416 | if (ip->i_eattr) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 417 | struct ea_list ei = { .ei_er = er, .ei_size = 0 }; |
| 418 | |
| 419 | error = ea_foreach(ip, ea_list_i, &ei); |
| 420 | if (!error) |
| 421 | error = ei.ei_size; |
| 422 | } |
| 423 | |
| 424 | gfs2_glock_dq_uninit(&i_gh); |
| 425 | |
| 426 | return error; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * ea_get_unstuffed - actually copies the unstuffed data into the |
| 431 | * request buffer |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 432 | * @ip: The GFS2 inode |
| 433 | * @ea: The extended attribute header structure |
| 434 | * @data: The data to be copied |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 435 | * |
| 436 | * Returns: errno |
| 437 | */ |
| 438 | |
| 439 | static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea, |
| 440 | char *data) |
| 441 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 442 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 443 | struct buffer_head **bh; |
| 444 | unsigned int amount = GFS2_EA_DATA_LEN(ea); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 445 | unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize); |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 446 | __be64 *dataptrs = GFS2_EA2DATAPTRS(ea); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 447 | unsigned int x; |
| 448 | int error = 0; |
| 449 | |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 450 | bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 451 | if (!bh) |
| 452 | return -ENOMEM; |
| 453 | |
| 454 | for (x = 0; x < nptrs; x++) { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 455 | error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0, |
| 456 | bh + x); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 457 | if (error) { |
| 458 | while (x--) |
| 459 | brelse(bh[x]); |
| 460 | goto out; |
| 461 | } |
| 462 | dataptrs++; |
| 463 | } |
| 464 | |
| 465 | for (x = 0; x < nptrs; x++) { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 466 | error = gfs2_meta_wait(sdp, bh[x]); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 467 | if (error) { |
| 468 | for (; x < nptrs; x++) |
| 469 | brelse(bh[x]); |
| 470 | goto out; |
| 471 | } |
| 472 | if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) { |
| 473 | for (; x < nptrs; x++) |
| 474 | brelse(bh[x]); |
| 475 | error = -EIO; |
| 476 | goto out; |
| 477 | } |
| 478 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 479 | memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header), |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 480 | (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize); |
| 481 | |
| 482 | amount -= sdp->sd_jbsize; |
| 483 | data += sdp->sd_jbsize; |
| 484 | |
| 485 | brelse(bh[x]); |
| 486 | } |
| 487 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 488 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 489 | kfree(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 490 | return error; |
| 491 | } |
| 492 | |
| 493 | int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el, |
| 494 | char *data) |
| 495 | { |
| 496 | if (GFS2_EA_IS_STUFFED(el->el_ea)) { |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 497 | memcpy(data, GFS2_EA2DATA(el->el_ea), GFS2_EA_DATA_LEN(el->el_ea)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 498 | return 0; |
| 499 | } else |
| 500 | return ea_get_unstuffed(ip, el->el_ea, data); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * gfs2_ea_get_i - |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 505 | * @ip: The GFS2 inode |
| 506 | * @er: The request structure |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 507 | * |
| 508 | * Returns: actual size of data on success, -errno on error |
| 509 | */ |
| 510 | |
| 511 | int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 512 | { |
| 513 | struct gfs2_ea_location el; |
| 514 | int error; |
| 515 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 516 | if (!ip->i_eattr) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 517 | return -ENODATA; |
| 518 | |
| 519 | error = gfs2_ea_find(ip, er, &el); |
| 520 | if (error) |
| 521 | return error; |
| 522 | if (!el.el_ea) |
| 523 | return -ENODATA; |
| 524 | |
| 525 | if (er->er_data_len) { |
| 526 | if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len) |
| 527 | error = -ERANGE; |
| 528 | else |
| 529 | error = gfs2_ea_get_copy(ip, &el, er->er_data); |
| 530 | } |
| 531 | if (!error) |
| 532 | error = GFS2_EA_DATA_LEN(el.el_ea); |
| 533 | |
| 534 | brelse(el.el_bh); |
| 535 | |
| 536 | return error; |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * gfs2_ea_get - |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 541 | * @ip: The GFS2 inode |
| 542 | * @er: The request structure |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 543 | * |
| 544 | * Returns: actual size of data on success, -errno on error |
| 545 | */ |
| 546 | |
| 547 | int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 548 | { |
| 549 | struct gfs2_holder i_gh; |
| 550 | int error; |
| 551 | |
| 552 | if (!er->er_name_len || |
| 553 | er->er_name_len > GFS2_EA_MAX_NAME_LEN) |
| 554 | return -EINVAL; |
| 555 | if (!er->er_data || !er->er_data_len) { |
| 556 | er->er_data = NULL; |
| 557 | er->er_data_len = 0; |
| 558 | } |
| 559 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 560 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 561 | if (error) |
| 562 | return error; |
| 563 | |
| 564 | error = gfs2_ea_ops[er->er_type]->eo_get(ip, er); |
| 565 | |
| 566 | gfs2_glock_dq_uninit(&i_gh); |
| 567 | |
| 568 | return error; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * ea_alloc_blk - allocates a new block for extended attributes. |
| 573 | * @ip: A pointer to the inode that's getting extended attributes |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 574 | * @bhp: Pointer to pointer to a struct buffer_head |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 575 | * |
| 576 | * Returns: errno |
| 577 | */ |
| 578 | |
| 579 | static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp) |
| 580 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 581 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 582 | struct gfs2_ea_header *ea; |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 583 | unsigned int n = 1; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 584 | u64 block; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 585 | |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 586 | block = gfs2_alloc_block(ip, &n); |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 587 | gfs2_trans_add_unrevoke(sdp, block, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 588 | *bhp = gfs2_meta_new(ip->i_gl, block); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 589 | gfs2_trans_add_bh(ip->i_gl, *bhp, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 590 | gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA); |
| 591 | gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header)); |
| 592 | |
| 593 | ea = GFS2_EA_BH2FIRST(*bhp); |
| 594 | ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize); |
| 595 | ea->ea_type = GFS2_EATYPE_UNUSED; |
| 596 | ea->ea_flags = GFS2_EAFLAG_LAST; |
| 597 | ea->ea_num_ptrs = 0; |
| 598 | |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 599 | gfs2_add_inode_blocks(&ip->i_inode, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * ea_write - writes the request info to an ea, creating new blocks if |
| 606 | * necessary |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 607 | * @ip: inode that is being modified |
| 608 | * @ea: the location of the new ea in a block |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 609 | * @er: the write request |
| 610 | * |
| 611 | * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags |
| 612 | * |
| 613 | * returns : errno |
| 614 | */ |
| 615 | |
| 616 | static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea, |
| 617 | struct gfs2_ea_request *er) |
| 618 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 619 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 620 | |
| 621 | ea->ea_data_len = cpu_to_be32(er->er_data_len); |
| 622 | ea->ea_name_len = er->er_name_len; |
| 623 | ea->ea_type = er->er_type; |
| 624 | ea->__pad = 0; |
| 625 | |
| 626 | memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len); |
| 627 | |
| 628 | if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) { |
| 629 | ea->ea_num_ptrs = 0; |
| 630 | memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len); |
| 631 | } else { |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 632 | __be64 *dataptr = GFS2_EA2DATAPTRS(ea); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 633 | const char *data = er->er_data; |
| 634 | unsigned int data_len = er->er_data_len; |
| 635 | unsigned int copy; |
| 636 | unsigned int x; |
| 637 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 638 | ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 639 | for (x = 0; x < ea->ea_num_ptrs; x++) { |
| 640 | struct buffer_head *bh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 641 | u64 block; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 642 | int mh_size = sizeof(struct gfs2_meta_header); |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 643 | unsigned int n = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 644 | |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 645 | block = gfs2_alloc_block(ip, &n); |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 646 | gfs2_trans_add_unrevoke(sdp, block, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 647 | bh = gfs2_meta_new(ip->i_gl, block); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 648 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 649 | gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED); |
| 650 | |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 651 | gfs2_add_inode_blocks(&ip->i_inode, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 652 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 653 | copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize : |
| 654 | data_len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 655 | memcpy(bh->b_data + mh_size, data, copy); |
| 656 | if (copy < sdp->sd_jbsize) |
| 657 | memset(bh->b_data + mh_size + copy, 0, |
| 658 | sdp->sd_jbsize - copy); |
| 659 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 660 | *dataptr++ = cpu_to_be64(bh->b_blocknr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 661 | data += copy; |
| 662 | data_len -= copy; |
| 663 | |
| 664 | brelse(bh); |
| 665 | } |
| 666 | |
| 667 | gfs2_assert_withdraw(sdp, !data_len); |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip, |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 674 | struct gfs2_ea_request *er, void *private); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 675 | |
| 676 | static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er, |
| 677 | unsigned int blks, |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 678 | ea_skeleton_call_t skeleton_call, void *private) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 679 | { |
| 680 | struct gfs2_alloc *al; |
| 681 | struct buffer_head *dibh; |
| 682 | int error; |
| 683 | |
| 684 | al = gfs2_alloc_get(ip); |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 685 | if (!al) |
| 686 | return -ENOMEM; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 687 | |
Steven Whitehouse | d82661d | 2008-03-10 15:34:50 +0000 | [diff] [blame] | 688 | error = gfs2_quota_lock_check(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 689 | if (error) |
| 690 | goto out; |
| 691 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 692 | al->al_requested = blks; |
| 693 | |
| 694 | error = gfs2_inplace_reserve(ip); |
| 695 | if (error) |
| 696 | goto out_gunlock_q; |
| 697 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 698 | error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 699 | blks + al->al_rgd->rd_length + |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 700 | RES_DINODE + RES_STATFS + RES_QUOTA, 0); |
| 701 | if (error) |
| 702 | goto out_ipres; |
| 703 | |
| 704 | error = skeleton_call(ip, er, private); |
| 705 | if (error) |
| 706 | goto out_end_trans; |
| 707 | |
| 708 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 709 | if (!error) { |
| 710 | if (er->er_flags & GFS2_ERF_MODE) { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 711 | gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 712 | (ip->i_inode.i_mode & S_IFMT) == |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 713 | (er->er_mode & S_IFMT)); |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 714 | ip->i_inode.i_mode = er->er_mode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 715 | } |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 716 | ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 717 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 718 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 719 | brelse(dibh); |
| 720 | } |
| 721 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 722 | out_end_trans: |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 723 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 724 | out_ipres: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 725 | gfs2_inplace_release(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 726 | out_gunlock_q: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 727 | gfs2_quota_unlock(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 728 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 729 | gfs2_alloc_put(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 730 | return error; |
| 731 | } |
| 732 | |
| 733 | static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er, |
| 734 | void *private) |
| 735 | { |
| 736 | struct buffer_head *bh; |
| 737 | int error; |
| 738 | |
| 739 | error = ea_alloc_blk(ip, &bh); |
| 740 | if (error) |
| 741 | return error; |
| 742 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 743 | ip->i_eattr = bh->b_blocknr; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 744 | error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er); |
| 745 | |
| 746 | brelse(bh); |
| 747 | |
| 748 | return error; |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * ea_init - initializes a new eattr block |
| 753 | * @ip: |
| 754 | * @er: |
| 755 | * |
| 756 | * Returns: errno |
| 757 | */ |
| 758 | |
| 759 | static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 760 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 761 | unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 762 | unsigned int blks = 1; |
| 763 | |
| 764 | if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize) |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 765 | blks += DIV_ROUND_UP(er->er_data_len, jbsize); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 766 | |
| 767 | return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL); |
| 768 | } |
| 769 | |
| 770 | static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea) |
| 771 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 772 | u32 ea_size = GFS2_EA_SIZE(ea); |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 773 | struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea + |
| 774 | ea_size); |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 775 | u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 776 | int last = ea->ea_flags & GFS2_EAFLAG_LAST; |
| 777 | |
| 778 | ea->ea_rec_len = cpu_to_be32(ea_size); |
| 779 | ea->ea_flags ^= last; |
| 780 | |
| 781 | new->ea_rec_len = cpu_to_be32(new_size); |
| 782 | new->ea_flags = last; |
| 783 | |
| 784 | return new; |
| 785 | } |
| 786 | |
| 787 | static void ea_set_remove_stuffed(struct gfs2_inode *ip, |
| 788 | struct gfs2_ea_location *el) |
| 789 | { |
| 790 | struct gfs2_ea_header *ea = el->el_ea; |
| 791 | struct gfs2_ea_header *prev = el->el_prev; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 792 | u32 len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 793 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 794 | gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 795 | |
| 796 | if (!prev || !GFS2_EA_IS_STUFFED(ea)) { |
| 797 | ea->ea_type = GFS2_EATYPE_UNUSED; |
| 798 | return; |
| 799 | } else if (GFS2_EA2NEXT(prev) != ea) { |
| 800 | prev = GFS2_EA2NEXT(prev); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 801 | gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea); |
| 805 | prev->ea_rec_len = cpu_to_be32(len); |
| 806 | |
| 807 | if (GFS2_EA_IS_LAST(ea)) |
| 808 | prev->ea_flags |= GFS2_EAFLAG_LAST; |
| 809 | } |
| 810 | |
| 811 | struct ea_set { |
| 812 | int ea_split; |
| 813 | |
| 814 | struct gfs2_ea_request *es_er; |
| 815 | struct gfs2_ea_location *es_el; |
| 816 | |
| 817 | struct buffer_head *es_bh; |
| 818 | struct gfs2_ea_header *es_ea; |
| 819 | }; |
| 820 | |
| 821 | static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh, |
| 822 | struct gfs2_ea_header *ea, struct ea_set *es) |
| 823 | { |
| 824 | struct gfs2_ea_request *er = es->es_er; |
| 825 | struct buffer_head *dibh; |
| 826 | int error; |
| 827 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 828 | error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 829 | if (error) |
| 830 | return error; |
| 831 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 832 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 833 | |
| 834 | if (es->ea_split) |
| 835 | ea = ea_split_ea(ea); |
| 836 | |
| 837 | ea_write(ip, ea, er); |
| 838 | |
| 839 | if (es->es_el) |
| 840 | ea_set_remove_stuffed(ip, es->es_el); |
| 841 | |
| 842 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 843 | if (error) |
| 844 | goto out; |
| 845 | |
| 846 | if (er->er_flags & GFS2_ERF_MODE) { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 847 | gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 848 | (ip->i_inode.i_mode & S_IFMT) == (er->er_mode & S_IFMT)); |
| 849 | ip->i_inode.i_mode = er->er_mode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 850 | } |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 851 | ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 852 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 853 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 854 | brelse(dibh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 855 | out: |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 856 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 857 | return error; |
| 858 | } |
| 859 | |
| 860 | static int ea_set_simple_alloc(struct gfs2_inode *ip, |
| 861 | struct gfs2_ea_request *er, void *private) |
| 862 | { |
| 863 | struct ea_set *es = private; |
| 864 | struct gfs2_ea_header *ea = es->es_ea; |
| 865 | int error; |
| 866 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 867 | gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 868 | |
| 869 | if (es->ea_split) |
| 870 | ea = ea_split_ea(ea); |
| 871 | |
| 872 | error = ea_write(ip, ea, er); |
| 873 | if (error) |
| 874 | return error; |
| 875 | |
| 876 | if (es->es_el) |
| 877 | ea_set_remove_stuffed(ip, es->es_el); |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh, |
| 883 | struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, |
| 884 | void *private) |
| 885 | { |
| 886 | struct ea_set *es = private; |
| 887 | unsigned int size; |
| 888 | int stuffed; |
| 889 | int error; |
| 890 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 891 | stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er, &size); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 892 | |
| 893 | if (ea->ea_type == GFS2_EATYPE_UNUSED) { |
| 894 | if (GFS2_EA_REC_LEN(ea) < size) |
| 895 | return 0; |
| 896 | if (!GFS2_EA_IS_STUFFED(ea)) { |
| 897 | error = ea_remove_unstuffed(ip, bh, ea, prev, 1); |
| 898 | if (error) |
| 899 | return error; |
| 900 | } |
| 901 | es->ea_split = 0; |
| 902 | } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size) |
| 903 | es->ea_split = 1; |
| 904 | else |
| 905 | return 0; |
| 906 | |
| 907 | if (stuffed) { |
| 908 | error = ea_set_simple_noalloc(ip, bh, ea, es); |
| 909 | if (error) |
| 910 | return error; |
| 911 | } else { |
| 912 | unsigned int blks; |
| 913 | |
| 914 | es->es_bh = bh; |
| 915 | es->es_ea = ea; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 916 | blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len, |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 917 | GFS2_SB(&ip->i_inode)->sd_jbsize); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 918 | |
| 919 | error = ea_alloc_skeleton(ip, es->es_er, blks, |
| 920 | ea_set_simple_alloc, es); |
| 921 | if (error) |
| 922 | return error; |
| 923 | } |
| 924 | |
| 925 | return 1; |
| 926 | } |
| 927 | |
| 928 | static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er, |
| 929 | void *private) |
| 930 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 931 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 932 | struct buffer_head *indbh, *newbh; |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 933 | __be64 *eablk; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 934 | int error; |
| 935 | int mh_size = sizeof(struct gfs2_meta_header); |
| 936 | |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 937 | if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) { |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 938 | __be64 *end; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 939 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 940 | error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 941 | &indbh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 942 | if (error) |
| 943 | return error; |
| 944 | |
| 945 | if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) { |
| 946 | error = -EIO; |
| 947 | goto out; |
| 948 | } |
| 949 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 950 | eablk = (__be64 *)(indbh->b_data + mh_size); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 951 | end = eablk + sdp->sd_inptrs; |
| 952 | |
| 953 | for (; eablk < end; eablk++) |
| 954 | if (!*eablk) |
| 955 | break; |
| 956 | |
| 957 | if (eablk == end) { |
| 958 | error = -ENOSPC; |
| 959 | goto out; |
| 960 | } |
| 961 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 962 | gfs2_trans_add_bh(ip->i_gl, indbh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 963 | } else { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 964 | u64 blk; |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 965 | unsigned int n = 1; |
| 966 | blk = gfs2_alloc_block(ip, &n); |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 967 | gfs2_trans_add_unrevoke(sdp, blk, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 968 | indbh = gfs2_meta_new(ip->i_gl, blk); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 969 | gfs2_trans_add_bh(ip->i_gl, indbh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 970 | gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN); |
| 971 | gfs2_buffer_clear_tail(indbh, mh_size); |
| 972 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 973 | eablk = (__be64 *)(indbh->b_data + mh_size); |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 974 | *eablk = cpu_to_be64(ip->i_eattr); |
| 975 | ip->i_eattr = blk; |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 976 | ip->i_diskflags |= GFS2_DIF_EA_INDIRECT; |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 977 | gfs2_add_inode_blocks(&ip->i_inode, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 978 | |
| 979 | eablk++; |
| 980 | } |
| 981 | |
| 982 | error = ea_alloc_blk(ip, &newbh); |
| 983 | if (error) |
| 984 | goto out; |
| 985 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 986 | *eablk = cpu_to_be64((u64)newbh->b_blocknr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 987 | error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er); |
| 988 | brelse(newbh); |
| 989 | if (error) |
| 990 | goto out; |
| 991 | |
| 992 | if (private) |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 993 | ea_set_remove_stuffed(ip, private); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 994 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 995 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 996 | brelse(indbh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 997 | return error; |
| 998 | } |
| 999 | |
| 1000 | static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er, |
| 1001 | struct gfs2_ea_location *el) |
| 1002 | { |
| 1003 | struct ea_set es; |
| 1004 | unsigned int blks = 2; |
| 1005 | int error; |
| 1006 | |
| 1007 | memset(&es, 0, sizeof(struct ea_set)); |
| 1008 | es.es_er = er; |
| 1009 | es.es_el = el; |
| 1010 | |
| 1011 | error = ea_foreach(ip, ea_set_simple, &es); |
| 1012 | if (error > 0) |
| 1013 | return 0; |
| 1014 | if (error) |
| 1015 | return error; |
| 1016 | |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 1017 | if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1018 | blks++; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1019 | if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize) |
| 1020 | blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1021 | |
| 1022 | return ea_alloc_skeleton(ip, er, blks, ea_set_block, el); |
| 1023 | } |
| 1024 | |
| 1025 | static int ea_set_remove_unstuffed(struct gfs2_inode *ip, |
| 1026 | struct gfs2_ea_location *el) |
| 1027 | { |
| 1028 | if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) { |
| 1029 | el->el_prev = GFS2_EA2NEXT(el->el_prev); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1030 | gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1031 | GFS2_EA2NEXT(el->el_prev) == el->el_ea); |
| 1032 | } |
| 1033 | |
| 1034 | return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0); |
| 1035 | } |
| 1036 | |
| 1037 | int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 1038 | { |
| 1039 | struct gfs2_ea_location el; |
| 1040 | int error; |
| 1041 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 1042 | if (!ip->i_eattr) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1043 | if (er->er_flags & XATTR_REPLACE) |
| 1044 | return -ENODATA; |
| 1045 | return ea_init(ip, er); |
| 1046 | } |
| 1047 | |
| 1048 | error = gfs2_ea_find(ip, er, &el); |
| 1049 | if (error) |
| 1050 | return error; |
| 1051 | |
| 1052 | if (el.el_ea) { |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 1053 | if (ip->i_diskflags & GFS2_DIF_APPENDONLY) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1054 | brelse(el.el_bh); |
| 1055 | return -EPERM; |
| 1056 | } |
| 1057 | |
| 1058 | error = -EEXIST; |
| 1059 | if (!(er->er_flags & XATTR_CREATE)) { |
| 1060 | int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea); |
| 1061 | error = ea_set_i(ip, er, &el); |
| 1062 | if (!error && unstuffed) |
| 1063 | ea_set_remove_unstuffed(ip, &el); |
| 1064 | } |
| 1065 | |
| 1066 | brelse(el.el_bh); |
| 1067 | } else { |
| 1068 | error = -ENODATA; |
| 1069 | if (!(er->er_flags & XATTR_REPLACE)) |
| 1070 | error = ea_set_i(ip, er, NULL); |
| 1071 | } |
| 1072 | |
| 1073 | return error; |
| 1074 | } |
| 1075 | |
| 1076 | int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 1077 | { |
| 1078 | struct gfs2_holder i_gh; |
| 1079 | int error; |
| 1080 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 1081 | if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1082 | return -EINVAL; |
| 1083 | if (!er->er_data || !er->er_data_len) { |
| 1084 | er->er_data = NULL; |
| 1085 | er->er_data_len = 0; |
| 1086 | } |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1087 | error = ea_check_size(GFS2_SB(&ip->i_inode), er); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1088 | if (error) |
| 1089 | return error; |
| 1090 | |
| 1091 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
| 1092 | if (error) |
| 1093 | return error; |
| 1094 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1095 | if (IS_IMMUTABLE(&ip->i_inode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1096 | error = -EPERM; |
| 1097 | else |
| 1098 | error = gfs2_ea_ops[er->er_type]->eo_set(ip, er); |
| 1099 | |
| 1100 | gfs2_glock_dq_uninit(&i_gh); |
| 1101 | |
| 1102 | return error; |
| 1103 | } |
| 1104 | |
| 1105 | static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el) |
| 1106 | { |
| 1107 | struct gfs2_ea_header *ea = el->el_ea; |
| 1108 | struct gfs2_ea_header *prev = el->el_prev; |
| 1109 | struct buffer_head *dibh; |
| 1110 | int error; |
| 1111 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1112 | error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1113 | if (error) |
| 1114 | return error; |
| 1115 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1116 | gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1117 | |
| 1118 | if (prev) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1119 | u32 len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1120 | |
| 1121 | len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea); |
| 1122 | prev->ea_rec_len = cpu_to_be32(len); |
| 1123 | |
| 1124 | if (GFS2_EA_IS_LAST(ea)) |
| 1125 | prev->ea_flags |= GFS2_EAFLAG_LAST; |
| 1126 | } else |
| 1127 | ea->ea_type = GFS2_EATYPE_UNUSED; |
| 1128 | |
| 1129 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1130 | if (!error) { |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 1131 | ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1132 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1133 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1134 | brelse(dibh); |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 1135 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1136 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1137 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1138 | |
| 1139 | return error; |
| 1140 | } |
| 1141 | |
| 1142 | int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 1143 | { |
| 1144 | struct gfs2_ea_location el; |
| 1145 | int error; |
| 1146 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 1147 | if (!ip->i_eattr) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1148 | return -ENODATA; |
| 1149 | |
| 1150 | error = gfs2_ea_find(ip, er, &el); |
| 1151 | if (error) |
| 1152 | return error; |
| 1153 | if (!el.el_ea) |
| 1154 | return -ENODATA; |
| 1155 | |
| 1156 | if (GFS2_EA_IS_STUFFED(el.el_ea)) |
| 1157 | error = ea_remove_stuffed(ip, &el); |
| 1158 | else |
| 1159 | error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, |
| 1160 | 0); |
| 1161 | |
| 1162 | brelse(el.el_bh); |
| 1163 | |
| 1164 | return error; |
| 1165 | } |
| 1166 | |
| 1167 | /** |
| 1168 | * gfs2_ea_remove - sets (or creates or replaces) an extended attribute |
| 1169 | * @ip: pointer to the inode of the target file |
| 1170 | * @er: request information |
| 1171 | * |
| 1172 | * Returns: errno |
| 1173 | */ |
| 1174 | |
| 1175 | int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 1176 | { |
| 1177 | struct gfs2_holder i_gh; |
| 1178 | int error; |
| 1179 | |
| 1180 | if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN) |
| 1181 | return -EINVAL; |
| 1182 | |
| 1183 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
| 1184 | if (error) |
| 1185 | return error; |
| 1186 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1187 | if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1188 | error = -EPERM; |
| 1189 | else |
| 1190 | error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er); |
| 1191 | |
| 1192 | gfs2_glock_dq_uninit(&i_gh); |
| 1193 | |
| 1194 | return error; |
| 1195 | } |
| 1196 | |
| 1197 | static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip, |
| 1198 | struct gfs2_ea_header *ea, char *data) |
| 1199 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1200 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1201 | struct buffer_head **bh; |
| 1202 | unsigned int amount = GFS2_EA_DATA_LEN(ea); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1203 | unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize); |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 1204 | __be64 *dataptrs = GFS2_EA2DATAPTRS(ea); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1205 | unsigned int x; |
| 1206 | int error; |
| 1207 | |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 1208 | bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1209 | if (!bh) |
| 1210 | return -ENOMEM; |
| 1211 | |
| 1212 | error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0); |
| 1213 | if (error) |
| 1214 | goto out; |
| 1215 | |
| 1216 | for (x = 0; x < nptrs; x++) { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 1217 | error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0, |
| 1218 | bh + x); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1219 | if (error) { |
| 1220 | while (x--) |
| 1221 | brelse(bh[x]); |
| 1222 | goto fail; |
| 1223 | } |
| 1224 | dataptrs++; |
| 1225 | } |
| 1226 | |
| 1227 | for (x = 0; x < nptrs; x++) { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 1228 | error = gfs2_meta_wait(sdp, bh[x]); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1229 | if (error) { |
| 1230 | for (; x < nptrs; x++) |
| 1231 | brelse(bh[x]); |
| 1232 | goto fail; |
| 1233 | } |
| 1234 | if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) { |
| 1235 | for (; x < nptrs; x++) |
| 1236 | brelse(bh[x]); |
| 1237 | error = -EIO; |
| 1238 | goto fail; |
| 1239 | } |
| 1240 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1241 | gfs2_trans_add_bh(ip->i_gl, bh[x], 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1242 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 1243 | memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1244 | (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize); |
| 1245 | |
| 1246 | amount -= sdp->sd_jbsize; |
| 1247 | data += sdp->sd_jbsize; |
| 1248 | |
| 1249 | brelse(bh[x]); |
| 1250 | } |
| 1251 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1252 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1253 | kfree(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1254 | return error; |
| 1255 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1256 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1257 | gfs2_trans_end(sdp); |
| 1258 | kfree(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1259 | return error; |
| 1260 | } |
| 1261 | |
| 1262 | int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el, |
| 1263 | struct iattr *attr, char *data) |
| 1264 | { |
| 1265 | struct buffer_head *dibh; |
| 1266 | int error; |
| 1267 | |
| 1268 | if (GFS2_EA_IS_STUFFED(el->el_ea)) { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1269 | error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1270 | if (error) |
| 1271 | return error; |
| 1272 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1273 | gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1); |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 1274 | memcpy(GFS2_EA2DATA(el->el_ea), data, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1275 | GFS2_EA_DATA_LEN(el->el_ea)); |
| 1276 | } else |
| 1277 | error = ea_acl_chmod_unstuffed(ip, el->el_ea, data); |
| 1278 | |
| 1279 | if (error) |
| 1280 | return error; |
| 1281 | |
| 1282 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1283 | if (!error) { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1284 | error = inode_setattr(&ip->i_inode, attr); |
| 1285 | gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1286 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1287 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1288 | brelse(dibh); |
| 1289 | } |
| 1290 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1291 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1292 | |
| 1293 | return error; |
| 1294 | } |
| 1295 | |
| 1296 | static int ea_dealloc_indirect(struct gfs2_inode *ip) |
| 1297 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1298 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1299 | struct gfs2_rgrp_list rlist; |
| 1300 | struct buffer_head *indbh, *dibh; |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 1301 | __be64 *eablk, *end; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1302 | unsigned int rg_blocks = 0; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1303 | u64 bstart = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1304 | unsigned int blen = 0; |
| 1305 | unsigned int blks = 0; |
| 1306 | unsigned int x; |
| 1307 | int error; |
| 1308 | |
| 1309 | memset(&rlist, 0, sizeof(struct gfs2_rgrp_list)); |
| 1310 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 1311 | error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, &indbh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1312 | if (error) |
| 1313 | return error; |
| 1314 | |
| 1315 | if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) { |
| 1316 | error = -EIO; |
| 1317 | goto out; |
| 1318 | } |
| 1319 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 1320 | eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1321 | end = eablk + sdp->sd_inptrs; |
| 1322 | |
| 1323 | for (; eablk < end; eablk++) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1324 | u64 bn; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1325 | |
| 1326 | if (!*eablk) |
| 1327 | break; |
| 1328 | bn = be64_to_cpu(*eablk); |
| 1329 | |
| 1330 | if (bstart + blen == bn) |
| 1331 | blen++; |
| 1332 | else { |
| 1333 | if (bstart) |
| 1334 | gfs2_rlist_add(sdp, &rlist, bstart); |
| 1335 | bstart = bn; |
| 1336 | blen = 1; |
| 1337 | } |
| 1338 | blks++; |
| 1339 | } |
| 1340 | if (bstart) |
| 1341 | gfs2_rlist_add(sdp, &rlist, bstart); |
| 1342 | else |
| 1343 | goto out; |
| 1344 | |
Bob Peterson | fe6c991 | 2008-01-28 11:13:02 -0600 | [diff] [blame] | 1345 | gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1346 | |
| 1347 | for (x = 0; x < rlist.rl_rgrps; x++) { |
| 1348 | struct gfs2_rgrpd *rgd; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1349 | rgd = rlist.rl_ghs[x].gh_gl->gl_object; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1350 | rg_blocks += rgd->rd_length; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs); |
| 1354 | if (error) |
| 1355 | goto out_rlist_free; |
| 1356 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 1357 | error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT + |
| 1358 | RES_STATFS + RES_QUOTA, blks); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1359 | if (error) |
| 1360 | goto out_gunlock; |
| 1361 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1362 | gfs2_trans_add_bh(ip->i_gl, indbh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1363 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 1364 | eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1365 | bstart = 0; |
| 1366 | blen = 0; |
| 1367 | |
| 1368 | for (; eablk < end; eablk++) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1369 | u64 bn; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1370 | |
| 1371 | if (!*eablk) |
| 1372 | break; |
| 1373 | bn = be64_to_cpu(*eablk); |
| 1374 | |
| 1375 | if (bstart + blen == bn) |
| 1376 | blen++; |
| 1377 | else { |
| 1378 | if (bstart) |
| 1379 | gfs2_free_meta(ip, bstart, blen); |
| 1380 | bstart = bn; |
| 1381 | blen = 1; |
| 1382 | } |
| 1383 | |
| 1384 | *eablk = 0; |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 1385 | gfs2_add_inode_blocks(&ip->i_inode, -1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1386 | } |
| 1387 | if (bstart) |
| 1388 | gfs2_free_meta(ip, bstart, blen); |
| 1389 | |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 1390 | ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1391 | |
| 1392 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1393 | if (!error) { |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1394 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1395 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1396 | brelse(dibh); |
| 1397 | } |
| 1398 | |
| 1399 | gfs2_trans_end(sdp); |
| 1400 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1401 | out_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1402 | gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1403 | out_rlist_free: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1404 | gfs2_rlist_free(&rlist); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1405 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1406 | brelse(indbh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1407 | return error; |
| 1408 | } |
| 1409 | |
| 1410 | static int ea_dealloc_block(struct gfs2_inode *ip) |
| 1411 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1412 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 1413 | struct gfs2_alloc *al = ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1414 | struct gfs2_rgrpd *rgd; |
| 1415 | struct buffer_head *dibh; |
| 1416 | int error; |
| 1417 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 1418 | rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1419 | if (!rgd) { |
| 1420 | gfs2_consist_inode(ip); |
| 1421 | return -EIO; |
| 1422 | } |
| 1423 | |
| 1424 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, |
| 1425 | &al->al_rgd_gh); |
| 1426 | if (error) |
| 1427 | return error; |
| 1428 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 1429 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS + |
| 1430 | RES_QUOTA, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1431 | if (error) |
| 1432 | goto out_gunlock; |
| 1433 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 1434 | gfs2_free_meta(ip, ip->i_eattr, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1435 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 1436 | ip->i_eattr = 0; |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 1437 | gfs2_add_inode_blocks(&ip->i_inode, -1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1438 | |
| 1439 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1440 | if (!error) { |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1441 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1442 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1443 | brelse(dibh); |
| 1444 | } |
| 1445 | |
| 1446 | gfs2_trans_end(sdp); |
| 1447 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1448 | out_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1449 | gfs2_glock_dq_uninit(&al->al_rgd_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1450 | return error; |
| 1451 | } |
| 1452 | |
| 1453 | /** |
| 1454 | * gfs2_ea_dealloc - deallocate the extended attribute fork |
| 1455 | * @ip: the inode |
| 1456 | * |
| 1457 | * Returns: errno |
| 1458 | */ |
| 1459 | |
| 1460 | int gfs2_ea_dealloc(struct gfs2_inode *ip) |
| 1461 | { |
| 1462 | struct gfs2_alloc *al; |
| 1463 | int error; |
| 1464 | |
| 1465 | al = gfs2_alloc_get(ip); |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 1466 | if (!al) |
| 1467 | return -ENOMEM; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1468 | |
| 1469 | error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 1470 | if (error) |
| 1471 | goto out_alloc; |
| 1472 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1473 | error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1474 | if (error) |
| 1475 | goto out_quota; |
| 1476 | |
| 1477 | error = ea_foreach(ip, ea_dealloc_unstuffed, NULL); |
| 1478 | if (error) |
| 1479 | goto out_rindex; |
| 1480 | |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 1481 | if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1482 | error = ea_dealloc_indirect(ip); |
| 1483 | if (error) |
| 1484 | goto out_rindex; |
| 1485 | } |
| 1486 | |
| 1487 | error = ea_dealloc_block(ip); |
| 1488 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1489 | out_rindex: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1490 | gfs2_glock_dq_uninit(&al->al_ri_gh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1491 | out_quota: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1492 | gfs2_quota_unhold(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1493 | out_alloc: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1494 | gfs2_alloc_put(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1495 | return error; |
| 1496 | } |
| 1497 | |