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" |
Steven Whitehouse | 307cf6e | 2009-08-26 18:51:04 +0100 | [diff] [blame] | 21 | #include "xattr.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 22 | #include "glock.h" |
| 23 | #include "inode.h" |
| 24 | #include "meta_io.h" |
| 25 | #include "quota.h" |
| 26 | #include "rgrp.h" |
| 27 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 28 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * ea_calc_size - returns the acutal number of bytes the request will take up |
| 32 | * (not counting any unstuffed data blocks) |
| 33 | * @sdp: |
| 34 | * @er: |
| 35 | * @size: |
| 36 | * |
| 37 | * Returns: 1 if the EA should be stuffed |
| 38 | */ |
| 39 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 40 | static int ea_calc_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 41 | unsigned int *size) |
| 42 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 43 | unsigned int jbsize = sdp->sd_jbsize; |
| 44 | |
| 45 | /* Stuffed */ |
| 46 | *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize + dsize, 8); |
| 47 | |
| 48 | if (*size <= jbsize) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 49 | return 1; |
| 50 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 51 | /* Unstuffed */ |
| 52 | *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize + |
| 53 | (sizeof(__be64) * DIV_ROUND_UP(dsize, jbsize)), 8); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 58 | static int ea_check_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 59 | { |
| 60 | unsigned int size; |
| 61 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 62 | if (dsize > GFS2_EA_MAX_DATA_LEN) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 63 | return -ERANGE; |
| 64 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 65 | ea_calc_size(sdp, nsize, dsize, &size); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 66 | |
| 67 | /* This can only happen with 512 byte blocks */ |
| 68 | if (size > sdp->sd_jbsize) |
| 69 | return -ERANGE; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 74 | 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] | 75 | struct gfs2_ea_header *ea, |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 76 | struct gfs2_ea_header *prev, void *private); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 77 | |
| 78 | static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh, |
| 79 | ea_call_t ea_call, void *data) |
| 80 | { |
| 81 | struct gfs2_ea_header *ea, *prev = NULL; |
| 82 | int error = 0; |
| 83 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 84 | 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] | 85 | return -EIO; |
| 86 | |
| 87 | for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) { |
| 88 | if (!GFS2_EA_REC_LEN(ea)) |
| 89 | goto fail; |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 90 | if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <= |
| 91 | bh->b_data + bh->b_size)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 92 | goto fail; |
| 93 | if (!GFS2_EATYPE_VALID(ea->ea_type)) |
| 94 | goto fail; |
| 95 | |
| 96 | error = ea_call(ip, bh, ea, prev, data); |
| 97 | if (error) |
| 98 | return error; |
| 99 | |
| 100 | if (GFS2_EA_IS_LAST(ea)) { |
| 101 | if ((char *)GFS2_EA2NEXT(ea) != |
| 102 | bh->b_data + bh->b_size) |
| 103 | goto fail; |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return error; |
| 109 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 110 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 111 | gfs2_consist_inode(ip); |
| 112 | return -EIO; |
| 113 | } |
| 114 | |
| 115 | static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data) |
| 116 | { |
| 117 | struct buffer_head *bh, *eabh; |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 118 | __be64 *eablk, *end; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 119 | int error; |
| 120 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 121 | 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] | 122 | if (error) |
| 123 | return error; |
| 124 | |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 125 | if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 126 | error = ea_foreach_i(ip, bh, ea_call, data); |
| 127 | goto out; |
| 128 | } |
| 129 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 130 | 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] | 131 | error = -EIO; |
| 132 | goto out; |
| 133 | } |
| 134 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 135 | eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header)); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 136 | end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 137 | |
| 138 | for (; eablk < end; eablk++) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 139 | u64 bn; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 140 | |
| 141 | if (!*eablk) |
| 142 | break; |
| 143 | bn = be64_to_cpu(*eablk); |
| 144 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 145 | error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, &eabh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 146 | if (error) |
| 147 | break; |
| 148 | error = ea_foreach_i(ip, eabh, ea_call, data); |
| 149 | brelse(eabh); |
| 150 | if (error) |
| 151 | break; |
| 152 | } |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 153 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 154 | brelse(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 155 | return error; |
| 156 | } |
| 157 | |
| 158 | struct ea_find { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 159 | int type; |
| 160 | const char *name; |
| 161 | size_t namel; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 162 | struct gfs2_ea_location *ef_el; |
| 163 | }; |
| 164 | |
| 165 | static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh, |
| 166 | struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, |
| 167 | void *private) |
| 168 | { |
| 169 | struct ea_find *ef = private; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 170 | |
| 171 | if (ea->ea_type == GFS2_EATYPE_UNUSED) |
| 172 | return 0; |
| 173 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 174 | if (ea->ea_type == ef->type) { |
| 175 | if (ea->ea_name_len == ef->namel && |
| 176 | !memcmp(GFS2_EA2NAME(ea), ef->name, ea->ea_name_len)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 177 | struct gfs2_ea_location *el = ef->ef_el; |
| 178 | get_bh(bh); |
| 179 | el->el_bh = bh; |
| 180 | el->el_ea = ea; |
| 181 | el->el_prev = prev; |
| 182 | return 1; |
| 183 | } |
| 184 | } |
| 185 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 189 | int gfs2_ea_find(struct gfs2_inode *ip, int type, const char *name, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 190 | struct gfs2_ea_location *el) |
| 191 | { |
| 192 | struct ea_find ef; |
| 193 | int error; |
| 194 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 195 | ef.type = type; |
| 196 | ef.name = name; |
| 197 | ef.namel = strlen(name); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 198 | ef.ef_el = el; |
| 199 | |
| 200 | memset(el, 0, sizeof(struct gfs2_ea_location)); |
| 201 | |
| 202 | error = ea_foreach(ip, ea_find_i, &ef); |
| 203 | if (error > 0) |
| 204 | return 0; |
| 205 | |
| 206 | return error; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * ea_dealloc_unstuffed - |
| 211 | * @ip: |
| 212 | * @bh: |
| 213 | * @ea: |
| 214 | * @prev: |
| 215 | * @private: |
| 216 | * |
| 217 | * Take advantage of the fact that all unstuffed blocks are |
| 218 | * allocated from the same RG. But watch, this may not always |
| 219 | * be true. |
| 220 | * |
| 221 | * Returns: errno |
| 222 | */ |
| 223 | |
| 224 | static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh, |
| 225 | struct gfs2_ea_header *ea, |
| 226 | struct gfs2_ea_header *prev, void *private) |
| 227 | { |
| 228 | int *leave = private; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 229 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 230 | struct gfs2_rgrpd *rgd; |
| 231 | struct gfs2_holder rg_gh; |
| 232 | struct buffer_head *dibh; |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 233 | __be64 *dataptrs; |
| 234 | u64 bn = 0; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 235 | u64 bstart = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 236 | unsigned int blen = 0; |
| 237 | unsigned int blks = 0; |
| 238 | unsigned int x; |
| 239 | int error; |
| 240 | |
| 241 | if (GFS2_EA_IS_STUFFED(ea)) |
| 242 | return 0; |
| 243 | |
| 244 | dataptrs = GFS2_EA2DATAPTRS(ea); |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 245 | for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 246 | if (*dataptrs) { |
| 247 | blks++; |
| 248 | bn = be64_to_cpu(*dataptrs); |
| 249 | } |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 250 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 251 | if (!blks) |
| 252 | return 0; |
| 253 | |
| 254 | rgd = gfs2_blk2rgrpd(sdp, bn); |
| 255 | if (!rgd) { |
| 256 | gfs2_consist_inode(ip); |
| 257 | return -EIO; |
| 258 | } |
| 259 | |
| 260 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh); |
| 261 | if (error) |
| 262 | return error; |
| 263 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 264 | error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE + |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 265 | RES_EATTR + RES_STATFS + RES_QUOTA, blks); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 266 | if (error) |
| 267 | goto out_gunlock; |
| 268 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 269 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 270 | |
| 271 | dataptrs = GFS2_EA2DATAPTRS(ea); |
| 272 | for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) { |
| 273 | if (!*dataptrs) |
| 274 | break; |
| 275 | bn = be64_to_cpu(*dataptrs); |
| 276 | |
| 277 | if (bstart + blen == bn) |
| 278 | blen++; |
| 279 | else { |
| 280 | if (bstart) |
| 281 | gfs2_free_meta(ip, bstart, blen); |
| 282 | bstart = bn; |
| 283 | blen = 1; |
| 284 | } |
| 285 | |
| 286 | *dataptrs = 0; |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 287 | gfs2_add_inode_blocks(&ip->i_inode, -1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 288 | } |
| 289 | if (bstart) |
| 290 | gfs2_free_meta(ip, bstart, blen); |
| 291 | |
| 292 | if (prev && !leave) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 293 | u32 len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 294 | |
| 295 | len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea); |
| 296 | prev->ea_rec_len = cpu_to_be32(len); |
| 297 | |
| 298 | if (GFS2_EA_IS_LAST(ea)) |
| 299 | prev->ea_flags |= GFS2_EAFLAG_LAST; |
| 300 | } else { |
| 301 | ea->ea_type = GFS2_EATYPE_UNUSED; |
| 302 | ea->ea_num_ptrs = 0; |
| 303 | } |
| 304 | |
| 305 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 306 | if (!error) { |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 307 | ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 308 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 309 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 310 | brelse(dibh); |
| 311 | } |
| 312 | |
| 313 | gfs2_trans_end(sdp); |
| 314 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 315 | out_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 316 | gfs2_glock_dq_uninit(&rg_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 317 | return error; |
| 318 | } |
| 319 | |
| 320 | static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh, |
| 321 | struct gfs2_ea_header *ea, |
| 322 | struct gfs2_ea_header *prev, int leave) |
| 323 | { |
| 324 | struct gfs2_alloc *al; |
| 325 | int error; |
| 326 | |
| 327 | al = gfs2_alloc_get(ip); |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 328 | if (!al) |
| 329 | return -ENOMEM; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 330 | |
| 331 | error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 332 | if (error) |
| 333 | goto out_alloc; |
| 334 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 335 | 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] | 336 | if (error) |
| 337 | goto out_quota; |
| 338 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 339 | error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 340 | |
| 341 | gfs2_glock_dq_uninit(&al->al_ri_gh); |
| 342 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 343 | out_quota: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 344 | gfs2_quota_unhold(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 345 | out_alloc: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 346 | gfs2_alloc_put(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 347 | return error; |
| 348 | } |
| 349 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 350 | struct ea_list { |
| 351 | struct gfs2_ea_request *ei_er; |
| 352 | unsigned int ei_size; |
| 353 | }; |
| 354 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 355 | static inline unsigned int gfs2_ea_strlen(struct gfs2_ea_header *ea) |
| 356 | { |
| 357 | switch (ea->ea_type) { |
| 358 | case GFS2_EATYPE_USR: |
| 359 | return 5 + ea->ea_name_len + 1; |
| 360 | case GFS2_EATYPE_SYS: |
| 361 | return 7 + ea->ea_name_len + 1; |
| 362 | case GFS2_EATYPE_SECURITY: |
| 363 | return 9 + ea->ea_name_len + 1; |
| 364 | default: |
| 365 | return 0; |
| 366 | } |
| 367 | } |
| 368 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 369 | static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh, |
| 370 | struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, |
| 371 | void *private) |
| 372 | { |
| 373 | struct ea_list *ei = private; |
| 374 | struct gfs2_ea_request *er = ei->ei_er; |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 375 | unsigned int ea_size = gfs2_ea_strlen(ea); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 376 | |
| 377 | if (ea->ea_type == GFS2_EATYPE_UNUSED) |
| 378 | return 0; |
| 379 | |
| 380 | if (er->er_data_len) { |
Steven Whitehouse | 01eb7c0 | 2006-06-06 17:31:30 -0400 | [diff] [blame] | 381 | char *prefix = NULL; |
| 382 | unsigned int l = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 383 | char c = 0; |
| 384 | |
| 385 | if (ei->ei_size + ea_size > er->er_data_len) |
| 386 | return -ERANGE; |
| 387 | |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 388 | switch (ea->ea_type) { |
| 389 | case GFS2_EATYPE_USR: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 390 | prefix = "user."; |
| 391 | l = 5; |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 392 | break; |
| 393 | case GFS2_EATYPE_SYS: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 394 | prefix = "system."; |
| 395 | l = 7; |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 396 | break; |
| 397 | case GFS2_EATYPE_SECURITY: |
| 398 | prefix = "security."; |
| 399 | l = 9; |
| 400 | break; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Steven Whitehouse | 01eb7c0 | 2006-06-06 17:31:30 -0400 | [diff] [blame] | 403 | BUG_ON(l == 0); |
| 404 | |
Steven Whitehouse | 90cdd20 | 2006-05-22 10:36:25 -0400 | [diff] [blame] | 405 | memcpy(er->er_data + ei->ei_size, prefix, l); |
| 406 | memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea), |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 407 | ea->ea_name_len); |
Steven Whitehouse | 90cdd20 | 2006-05-22 10:36:25 -0400 | [diff] [blame] | 408 | memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | ei->ei_size += ea_size; |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | /** |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 417 | * gfs2_listxattr - List gfs2 extended attributes |
| 418 | * @dentry: The dentry whose inode we are interested in |
| 419 | * @buffer: The buffer to write the results |
| 420 | * @size: The size of the buffer |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 421 | * |
| 422 | * Returns: actual size of data on success, -errno on error |
| 423 | */ |
| 424 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 425 | ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 426 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 427 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
| 428 | struct gfs2_ea_request er; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 429 | struct gfs2_holder i_gh; |
| 430 | int error; |
| 431 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 432 | memset(&er, 0, sizeof(struct gfs2_ea_request)); |
| 433 | if (size) { |
| 434 | er.er_data = buffer; |
| 435 | er.er_data_len = size; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 438 | 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] | 439 | if (error) |
| 440 | return error; |
| 441 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 442 | if (ip->i_eattr) { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 443 | struct ea_list ei = { .ei_er = &er, .ei_size = 0 }; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 444 | |
| 445 | error = ea_foreach(ip, ea_list_i, &ei); |
| 446 | if (!error) |
| 447 | error = ei.ei_size; |
| 448 | } |
| 449 | |
| 450 | gfs2_glock_dq_uninit(&i_gh); |
| 451 | |
| 452 | return error; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * ea_get_unstuffed - actually copies the unstuffed data into the |
| 457 | * request buffer |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 458 | * @ip: The GFS2 inode |
| 459 | * @ea: The extended attribute header structure |
| 460 | * @data: The data to be copied |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 461 | * |
| 462 | * Returns: errno |
| 463 | */ |
| 464 | |
| 465 | static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea, |
| 466 | char *data) |
| 467 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 468 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 469 | struct buffer_head **bh; |
| 470 | unsigned int amount = GFS2_EA_DATA_LEN(ea); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 471 | unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize); |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 472 | __be64 *dataptrs = GFS2_EA2DATAPTRS(ea); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 473 | unsigned int x; |
| 474 | int error = 0; |
| 475 | |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 476 | bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 477 | if (!bh) |
| 478 | return -ENOMEM; |
| 479 | |
| 480 | for (x = 0; x < nptrs; x++) { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 481 | error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0, |
| 482 | bh + x); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 483 | if (error) { |
| 484 | while (x--) |
| 485 | brelse(bh[x]); |
| 486 | goto out; |
| 487 | } |
| 488 | dataptrs++; |
| 489 | } |
| 490 | |
| 491 | for (x = 0; x < nptrs; x++) { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 492 | error = gfs2_meta_wait(sdp, bh[x]); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 493 | if (error) { |
| 494 | for (; x < nptrs; x++) |
| 495 | brelse(bh[x]); |
| 496 | goto out; |
| 497 | } |
| 498 | if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) { |
| 499 | for (; x < nptrs; x++) |
| 500 | brelse(bh[x]); |
| 501 | error = -EIO; |
| 502 | goto out; |
| 503 | } |
| 504 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 505 | memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header), |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 506 | (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize); |
| 507 | |
| 508 | amount -= sdp->sd_jbsize; |
| 509 | data += sdp->sd_jbsize; |
| 510 | |
| 511 | brelse(bh[x]); |
| 512 | } |
| 513 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 514 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 515 | kfree(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 516 | return error; |
| 517 | } |
| 518 | |
| 519 | int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el, |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 520 | char *data, size_t size) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 521 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 522 | int ret; |
| 523 | size_t len = GFS2_EA_DATA_LEN(el->el_ea); |
| 524 | if (len > size) |
| 525 | return -ERANGE; |
| 526 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 527 | if (GFS2_EA_IS_STUFFED(el->el_ea)) { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 528 | memcpy(data, GFS2_EA2DATA(el->el_ea), len); |
| 529 | return len; |
| 530 | } |
| 531 | ret = ea_get_unstuffed(ip, el->el_ea, data); |
| 532 | if (ret < 0) |
| 533 | return ret; |
| 534 | return len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /** |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 538 | * gfs2_xattr_get - Get a GFS2 extended attribute |
| 539 | * @inode: The inode |
| 540 | * @type: The type of extended attribute |
| 541 | * @name: The name of the extended attribute |
| 542 | * @buffer: The buffer to write the result into |
| 543 | * @size: The size of the buffer |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 544 | * |
| 545 | * Returns: actual size of data on success, -errno on error |
| 546 | */ |
| 547 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 548 | int gfs2_xattr_get(struct inode *inode, int type, const char *name, |
| 549 | void *buffer, size_t size) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 550 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 551 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 552 | struct gfs2_ea_location el; |
| 553 | int error; |
| 554 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 555 | if (!ip->i_eattr) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 556 | return -ENODATA; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 557 | if (strlen(name) > GFS2_EA_MAX_NAME_LEN) |
| 558 | return -EINVAL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 559 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 560 | error = gfs2_ea_find(ip, type, name, &el); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 561 | if (error) |
| 562 | return error; |
| 563 | if (!el.el_ea) |
| 564 | return -ENODATA; |
Steven Whitehouse | 86d0063 | 2009-09-14 09:50:57 +0100 | [diff] [blame] | 565 | if (size) |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 566 | error = gfs2_ea_get_copy(ip, &el, buffer, size); |
| 567 | else |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 568 | error = GFS2_EA_DATA_LEN(el.el_ea); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 569 | brelse(el.el_bh); |
| 570 | |
| 571 | return error; |
| 572 | } |
| 573 | |
| 574 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 575 | * ea_alloc_blk - allocates a new block for extended attributes. |
| 576 | * @ip: A pointer to the inode that's getting extended attributes |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 577 | * @bhp: Pointer to pointer to a struct buffer_head |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 578 | * |
| 579 | * Returns: errno |
| 580 | */ |
| 581 | |
| 582 | static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp) |
| 583 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 584 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 585 | struct gfs2_ea_header *ea; |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 586 | unsigned int n = 1; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 587 | u64 block; |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 588 | int error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 589 | |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 590 | error = gfs2_alloc_block(ip, &block, &n); |
| 591 | if (error) |
| 592 | return error; |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 593 | gfs2_trans_add_unrevoke(sdp, block, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 594 | *bhp = gfs2_meta_new(ip->i_gl, block); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 595 | gfs2_trans_add_bh(ip->i_gl, *bhp, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 596 | gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA); |
| 597 | gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header)); |
| 598 | |
| 599 | ea = GFS2_EA_BH2FIRST(*bhp); |
| 600 | ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize); |
| 601 | ea->ea_type = GFS2_EATYPE_UNUSED; |
| 602 | ea->ea_flags = GFS2_EAFLAG_LAST; |
| 603 | ea->ea_num_ptrs = 0; |
| 604 | |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 605 | gfs2_add_inode_blocks(&ip->i_inode, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * ea_write - writes the request info to an ea, creating new blocks if |
| 612 | * necessary |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 613 | * @ip: inode that is being modified |
| 614 | * @ea: the location of the new ea in a block |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 615 | * @er: the write request |
| 616 | * |
| 617 | * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags |
| 618 | * |
| 619 | * returns : errno |
| 620 | */ |
| 621 | |
| 622 | static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea, |
| 623 | struct gfs2_ea_request *er) |
| 624 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 625 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 626 | int error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 627 | |
| 628 | ea->ea_data_len = cpu_to_be32(er->er_data_len); |
| 629 | ea->ea_name_len = er->er_name_len; |
| 630 | ea->ea_type = er->er_type; |
| 631 | ea->__pad = 0; |
| 632 | |
| 633 | memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len); |
| 634 | |
| 635 | if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) { |
| 636 | ea->ea_num_ptrs = 0; |
| 637 | memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len); |
| 638 | } else { |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 639 | __be64 *dataptr = GFS2_EA2DATAPTRS(ea); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 640 | const char *data = er->er_data; |
| 641 | unsigned int data_len = er->er_data_len; |
| 642 | unsigned int copy; |
| 643 | unsigned int x; |
| 644 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 645 | 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] | 646 | for (x = 0; x < ea->ea_num_ptrs; x++) { |
| 647 | struct buffer_head *bh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 648 | u64 block; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 649 | int mh_size = sizeof(struct gfs2_meta_header); |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 650 | unsigned int n = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 651 | |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 652 | error = gfs2_alloc_block(ip, &block, &n); |
| 653 | if (error) |
| 654 | return error; |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 655 | gfs2_trans_add_unrevoke(sdp, block, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 656 | bh = gfs2_meta_new(ip->i_gl, block); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 657 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 658 | gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED); |
| 659 | |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 660 | gfs2_add_inode_blocks(&ip->i_inode, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 661 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 662 | copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize : |
| 663 | data_len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 664 | memcpy(bh->b_data + mh_size, data, copy); |
| 665 | if (copy < sdp->sd_jbsize) |
| 666 | memset(bh->b_data + mh_size + copy, 0, |
| 667 | sdp->sd_jbsize - copy); |
| 668 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 669 | *dataptr++ = cpu_to_be64(bh->b_blocknr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 670 | data += copy; |
| 671 | data_len -= copy; |
| 672 | |
| 673 | brelse(bh); |
| 674 | } |
| 675 | |
| 676 | gfs2_assert_withdraw(sdp, !data_len); |
| 677 | } |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip, |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 683 | struct gfs2_ea_request *er, void *private); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 684 | |
| 685 | static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er, |
| 686 | unsigned int blks, |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 687 | ea_skeleton_call_t skeleton_call, void *private) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 688 | { |
| 689 | struct gfs2_alloc *al; |
| 690 | struct buffer_head *dibh; |
| 691 | int error; |
| 692 | |
| 693 | al = gfs2_alloc_get(ip); |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 694 | if (!al) |
| 695 | return -ENOMEM; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 696 | |
Steven Whitehouse | d82661d | 2008-03-10 15:34:50 +0000 | [diff] [blame] | 697 | error = gfs2_quota_lock_check(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 698 | if (error) |
| 699 | goto out; |
| 700 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 701 | al->al_requested = blks; |
| 702 | |
| 703 | error = gfs2_inplace_reserve(ip); |
| 704 | if (error) |
| 705 | goto out_gunlock_q; |
| 706 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 707 | error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 708 | blks + al->al_rgd->rd_length + |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 709 | RES_DINODE + RES_STATFS + RES_QUOTA, 0); |
| 710 | if (error) |
| 711 | goto out_ipres; |
| 712 | |
| 713 | error = skeleton_call(ip, er, private); |
| 714 | if (error) |
| 715 | goto out_end_trans; |
| 716 | |
| 717 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 718 | if (!error) { |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 719 | ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 720 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 721 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 722 | brelse(dibh); |
| 723 | } |
| 724 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 725 | out_end_trans: |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 726 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 727 | out_ipres: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 728 | gfs2_inplace_release(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 729 | out_gunlock_q: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 730 | gfs2_quota_unlock(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 731 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 732 | gfs2_alloc_put(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 733 | return error; |
| 734 | } |
| 735 | |
| 736 | static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er, |
| 737 | void *private) |
| 738 | { |
| 739 | struct buffer_head *bh; |
| 740 | int error; |
| 741 | |
| 742 | error = ea_alloc_blk(ip, &bh); |
| 743 | if (error) |
| 744 | return error; |
| 745 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 746 | ip->i_eattr = bh->b_blocknr; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 747 | error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er); |
| 748 | |
| 749 | brelse(bh); |
| 750 | |
| 751 | return error; |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * ea_init - initializes a new eattr block |
| 756 | * @ip: |
| 757 | * @er: |
| 758 | * |
| 759 | * Returns: errno |
| 760 | */ |
| 761 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 762 | static int ea_init(struct gfs2_inode *ip, int type, const char *name, |
| 763 | const void *data, size_t size) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 764 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 765 | struct gfs2_ea_request er; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 766 | unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 767 | unsigned int blks = 1; |
| 768 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 769 | er.er_type = type; |
| 770 | er.er_name = name; |
| 771 | er.er_name_len = strlen(name); |
| 772 | er.er_data = (void *)data; |
| 773 | er.er_data_len = size; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 774 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 775 | if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize) |
| 776 | blks += DIV_ROUND_UP(er.er_data_len, jbsize); |
| 777 | |
| 778 | return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea) |
| 782 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 783 | u32 ea_size = GFS2_EA_SIZE(ea); |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 784 | struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea + |
| 785 | ea_size); |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 786 | u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 787 | int last = ea->ea_flags & GFS2_EAFLAG_LAST; |
| 788 | |
| 789 | ea->ea_rec_len = cpu_to_be32(ea_size); |
| 790 | ea->ea_flags ^= last; |
| 791 | |
| 792 | new->ea_rec_len = cpu_to_be32(new_size); |
| 793 | new->ea_flags = last; |
| 794 | |
| 795 | return new; |
| 796 | } |
| 797 | |
| 798 | static void ea_set_remove_stuffed(struct gfs2_inode *ip, |
| 799 | struct gfs2_ea_location *el) |
| 800 | { |
| 801 | struct gfs2_ea_header *ea = el->el_ea; |
| 802 | struct gfs2_ea_header *prev = el->el_prev; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 803 | u32 len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 804 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 805 | gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 806 | |
| 807 | if (!prev || !GFS2_EA_IS_STUFFED(ea)) { |
| 808 | ea->ea_type = GFS2_EATYPE_UNUSED; |
| 809 | return; |
| 810 | } else if (GFS2_EA2NEXT(prev) != ea) { |
| 811 | prev = GFS2_EA2NEXT(prev); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 812 | gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea); |
| 816 | prev->ea_rec_len = cpu_to_be32(len); |
| 817 | |
| 818 | if (GFS2_EA_IS_LAST(ea)) |
| 819 | prev->ea_flags |= GFS2_EAFLAG_LAST; |
| 820 | } |
| 821 | |
| 822 | struct ea_set { |
| 823 | int ea_split; |
| 824 | |
| 825 | struct gfs2_ea_request *es_er; |
| 826 | struct gfs2_ea_location *es_el; |
| 827 | |
| 828 | struct buffer_head *es_bh; |
| 829 | struct gfs2_ea_header *es_ea; |
| 830 | }; |
| 831 | |
| 832 | static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh, |
| 833 | struct gfs2_ea_header *ea, struct ea_set *es) |
| 834 | { |
| 835 | struct gfs2_ea_request *er = es->es_er; |
| 836 | struct buffer_head *dibh; |
| 837 | int error; |
| 838 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 839 | 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] | 840 | if (error) |
| 841 | return error; |
| 842 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 843 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 844 | |
| 845 | if (es->ea_split) |
| 846 | ea = ea_split_ea(ea); |
| 847 | |
| 848 | ea_write(ip, ea, er); |
| 849 | |
| 850 | if (es->es_el) |
| 851 | ea_set_remove_stuffed(ip, es->es_el); |
| 852 | |
| 853 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 854 | if (error) |
| 855 | goto out; |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 856 | ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 857 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 858 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 859 | brelse(dibh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 860 | out: |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 861 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 862 | return error; |
| 863 | } |
| 864 | |
| 865 | static int ea_set_simple_alloc(struct gfs2_inode *ip, |
| 866 | struct gfs2_ea_request *er, void *private) |
| 867 | { |
| 868 | struct ea_set *es = private; |
| 869 | struct gfs2_ea_header *ea = es->es_ea; |
| 870 | int error; |
| 871 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 872 | gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 873 | |
| 874 | if (es->ea_split) |
| 875 | ea = ea_split_ea(ea); |
| 876 | |
| 877 | error = ea_write(ip, ea, er); |
| 878 | if (error) |
| 879 | return error; |
| 880 | |
| 881 | if (es->es_el) |
| 882 | ea_set_remove_stuffed(ip, es->es_el); |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh, |
| 888 | struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, |
| 889 | void *private) |
| 890 | { |
| 891 | struct ea_set *es = private; |
| 892 | unsigned int size; |
| 893 | int stuffed; |
| 894 | int error; |
| 895 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 896 | stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len, |
| 897 | es->es_er->er_data_len, &size); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 898 | |
| 899 | if (ea->ea_type == GFS2_EATYPE_UNUSED) { |
| 900 | if (GFS2_EA_REC_LEN(ea) < size) |
| 901 | return 0; |
| 902 | if (!GFS2_EA_IS_STUFFED(ea)) { |
| 903 | error = ea_remove_unstuffed(ip, bh, ea, prev, 1); |
| 904 | if (error) |
| 905 | return error; |
| 906 | } |
| 907 | es->ea_split = 0; |
| 908 | } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size) |
| 909 | es->ea_split = 1; |
| 910 | else |
| 911 | return 0; |
| 912 | |
| 913 | if (stuffed) { |
| 914 | error = ea_set_simple_noalloc(ip, bh, ea, es); |
| 915 | if (error) |
| 916 | return error; |
| 917 | } else { |
| 918 | unsigned int blks; |
| 919 | |
| 920 | es->es_bh = bh; |
| 921 | es->es_ea = ea; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 922 | blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len, |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 923 | GFS2_SB(&ip->i_inode)->sd_jbsize); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 924 | |
| 925 | error = ea_alloc_skeleton(ip, es->es_er, blks, |
| 926 | ea_set_simple_alloc, es); |
| 927 | if (error) |
| 928 | return error; |
| 929 | } |
| 930 | |
| 931 | return 1; |
| 932 | } |
| 933 | |
| 934 | static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er, |
| 935 | void *private) |
| 936 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 937 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 938 | struct buffer_head *indbh, *newbh; |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 939 | __be64 *eablk; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 940 | int error; |
| 941 | int mh_size = sizeof(struct gfs2_meta_header); |
| 942 | |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 943 | if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) { |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 944 | __be64 *end; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 945 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 946 | error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 947 | &indbh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 948 | if (error) |
| 949 | return error; |
| 950 | |
| 951 | if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) { |
| 952 | error = -EIO; |
| 953 | goto out; |
| 954 | } |
| 955 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 956 | eablk = (__be64 *)(indbh->b_data + mh_size); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 957 | end = eablk + sdp->sd_inptrs; |
| 958 | |
| 959 | for (; eablk < end; eablk++) |
| 960 | if (!*eablk) |
| 961 | break; |
| 962 | |
| 963 | if (eablk == end) { |
| 964 | error = -ENOSPC; |
| 965 | goto out; |
| 966 | } |
| 967 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 968 | gfs2_trans_add_bh(ip->i_gl, indbh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 969 | } else { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 970 | u64 blk; |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 971 | unsigned int n = 1; |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 972 | error = gfs2_alloc_block(ip, &blk, &n); |
| 973 | if (error) |
| 974 | return error; |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 975 | gfs2_trans_add_unrevoke(sdp, blk, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 976 | indbh = gfs2_meta_new(ip->i_gl, blk); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 977 | gfs2_trans_add_bh(ip->i_gl, indbh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 978 | gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN); |
| 979 | gfs2_buffer_clear_tail(indbh, mh_size); |
| 980 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 981 | eablk = (__be64 *)(indbh->b_data + mh_size); |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 982 | *eablk = cpu_to_be64(ip->i_eattr); |
| 983 | ip->i_eattr = blk; |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 984 | ip->i_diskflags |= GFS2_DIF_EA_INDIRECT; |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 985 | gfs2_add_inode_blocks(&ip->i_inode, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 986 | |
| 987 | eablk++; |
| 988 | } |
| 989 | |
| 990 | error = ea_alloc_blk(ip, &newbh); |
| 991 | if (error) |
| 992 | goto out; |
| 993 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 994 | *eablk = cpu_to_be64((u64)newbh->b_blocknr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 995 | error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er); |
| 996 | brelse(newbh); |
| 997 | if (error) |
| 998 | goto out; |
| 999 | |
| 1000 | if (private) |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 1001 | ea_set_remove_stuffed(ip, private); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1002 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1003 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1004 | brelse(indbh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1005 | return error; |
| 1006 | } |
| 1007 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1008 | static int ea_set_i(struct gfs2_inode *ip, int type, const char *name, |
| 1009 | const void *value, size_t size, struct gfs2_ea_location *el) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1010 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1011 | struct gfs2_ea_request er; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1012 | struct ea_set es; |
| 1013 | unsigned int blks = 2; |
| 1014 | int error; |
| 1015 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1016 | er.er_type = type; |
| 1017 | er.er_name = name; |
| 1018 | er.er_data = (void *)value; |
| 1019 | er.er_name_len = strlen(name); |
| 1020 | er.er_data_len = size; |
| 1021 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1022 | memset(&es, 0, sizeof(struct ea_set)); |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1023 | es.es_er = &er; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1024 | es.es_el = el; |
| 1025 | |
| 1026 | error = ea_foreach(ip, ea_set_simple, &es); |
| 1027 | if (error > 0) |
| 1028 | return 0; |
| 1029 | if (error) |
| 1030 | return error; |
| 1031 | |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 1032 | if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1033 | blks++; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1034 | if (GFS2_EAREQ_SIZE_STUFFED(&er) > GFS2_SB(&ip->i_inode)->sd_jbsize) |
| 1035 | 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] | 1036 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1037 | return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | static int ea_set_remove_unstuffed(struct gfs2_inode *ip, |
| 1041 | struct gfs2_ea_location *el) |
| 1042 | { |
| 1043 | if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) { |
| 1044 | el->el_prev = GFS2_EA2NEXT(el->el_prev); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1045 | gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1046 | GFS2_EA2NEXT(el->el_prev) == el->el_ea); |
| 1047 | } |
| 1048 | |
Steven Whitehouse | 86d0063 | 2009-09-14 09:50:57 +0100 | [diff] [blame] | 1049 | return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1052 | static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el) |
| 1053 | { |
| 1054 | struct gfs2_ea_header *ea = el->el_ea; |
| 1055 | struct gfs2_ea_header *prev = el->el_prev; |
| 1056 | struct buffer_head *dibh; |
| 1057 | int error; |
| 1058 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1059 | 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] | 1060 | if (error) |
| 1061 | return error; |
| 1062 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1063 | gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1064 | |
| 1065 | if (prev) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1066 | u32 len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1067 | |
| 1068 | len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea); |
| 1069 | prev->ea_rec_len = cpu_to_be32(len); |
| 1070 | |
| 1071 | if (GFS2_EA_IS_LAST(ea)) |
| 1072 | prev->ea_flags |= GFS2_EAFLAG_LAST; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1073 | } else { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1074 | ea->ea_type = GFS2_EATYPE_UNUSED; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1075 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1076 | |
| 1077 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1078 | if (!error) { |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 1079 | ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1080 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1081 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1082 | brelse(dibh); |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 1083 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1084 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1085 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1086 | |
| 1087 | return error; |
| 1088 | } |
| 1089 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1090 | /** |
| 1091 | * gfs2_xattr_remove - Remove a GFS2 extended attribute |
| 1092 | * @inode: The inode |
| 1093 | * @type: The type of the extended attribute |
| 1094 | * @name: The name of the extended attribute |
| 1095 | * |
| 1096 | * This is not called directly by the VFS since we use the (common) |
| 1097 | * scheme of making a "set with NULL data" mean a remove request. Note |
| 1098 | * that this is different from a set with zero length data. |
| 1099 | * |
| 1100 | * Returns: 0, or errno on failure |
| 1101 | */ |
| 1102 | |
| 1103 | static int gfs2_xattr_remove(struct inode *inode, int type, const char *name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1104 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1105 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1106 | struct gfs2_ea_location el; |
| 1107 | int error; |
| 1108 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 1109 | if (!ip->i_eattr) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1110 | return -ENODATA; |
| 1111 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1112 | error = gfs2_ea_find(ip, type, name, &el); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1113 | if (error) |
| 1114 | return error; |
| 1115 | if (!el.el_ea) |
| 1116 | return -ENODATA; |
| 1117 | |
| 1118 | if (GFS2_EA_IS_STUFFED(el.el_ea)) |
| 1119 | error = ea_remove_stuffed(ip, &el); |
| 1120 | else |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1121 | error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1122 | |
| 1123 | brelse(el.el_bh); |
| 1124 | |
| 1125 | return error; |
| 1126 | } |
| 1127 | |
| 1128 | /** |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1129 | * gfs2_xattr_set - Set (or remove) a GFS2 extended attribute |
| 1130 | * @inode: The inode |
| 1131 | * @type: The type of the extended attribute |
| 1132 | * @name: The name of the extended attribute |
| 1133 | * @value: The value of the extended attribute (NULL for remove) |
| 1134 | * @size: The size of the @value argument |
| 1135 | * @flags: Create or Replace |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1136 | * |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1137 | * See gfs2_xattr_remove() for details of the removal of xattrs. |
| 1138 | * |
| 1139 | * Returns: 0 or errno on failure |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1140 | */ |
| 1141 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1142 | int gfs2_xattr_set(struct inode *inode, int type, const char *name, |
| 1143 | const void *value, size_t size, int flags) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1144 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1145 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 1146 | struct gfs2_inode *ip = GFS2_I(inode); |
| 1147 | struct gfs2_ea_location el; |
| 1148 | unsigned int namel = strlen(name); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1149 | int error; |
| 1150 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1151 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 1152 | return -EPERM; |
| 1153 | if (namel > GFS2_EA_MAX_NAME_LEN) |
| 1154 | return -ERANGE; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1155 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1156 | if (value == NULL) |
| 1157 | return gfs2_xattr_remove(inode, type, name); |
| 1158 | |
| 1159 | if (ea_check_size(sdp, namel, size)) |
| 1160 | return -ERANGE; |
| 1161 | |
| 1162 | if (!ip->i_eattr) { |
| 1163 | if (flags & XATTR_REPLACE) |
| 1164 | return -ENODATA; |
| 1165 | return ea_init(ip, type, name, value, size); |
| 1166 | } |
| 1167 | |
| 1168 | error = gfs2_ea_find(ip, type, name, &el); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1169 | if (error) |
| 1170 | return error; |
| 1171 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1172 | if (el.el_ea) { |
| 1173 | if (ip->i_diskflags & GFS2_DIF_APPENDONLY) { |
| 1174 | brelse(el.el_bh); |
| 1175 | return -EPERM; |
| 1176 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1177 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1178 | error = -EEXIST; |
| 1179 | if (!(flags & XATTR_CREATE)) { |
| 1180 | int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea); |
| 1181 | error = ea_set_i(ip, type, name, value, size, &el); |
| 1182 | if (!error && unstuffed) |
| 1183 | ea_set_remove_unstuffed(ip, &el); |
| 1184 | } |
| 1185 | |
| 1186 | brelse(el.el_bh); |
| 1187 | return error; |
| 1188 | } |
| 1189 | |
| 1190 | error = -ENODATA; |
| 1191 | if (!(flags & XATTR_REPLACE)) |
| 1192 | error = ea_set_i(ip, type, name, value, size, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 1498 | static int gfs2_xattr_user_get(struct inode *inode, const char *name, |
| 1499 | void *buffer, size_t size) |
| 1500 | { |
| 1501 | return gfs2_xattr_get(inode, GFS2_EATYPE_USR, name, buffer, size); |
| 1502 | } |
| 1503 | |
| 1504 | static int gfs2_xattr_user_set(struct inode *inode, const char *name, |
| 1505 | const void *value, size_t size, int flags) |
| 1506 | { |
| 1507 | return gfs2_xattr_set(inode, GFS2_EATYPE_USR, name, value, size, flags); |
| 1508 | } |
| 1509 | |
| 1510 | static int gfs2_xattr_system_get(struct inode *inode, const char *name, |
| 1511 | void *buffer, size_t size) |
| 1512 | { |
| 1513 | return gfs2_xattr_get(inode, GFS2_EATYPE_SYS, name, buffer, size); |
| 1514 | } |
| 1515 | |
| 1516 | static int gfs2_xattr_system_set(struct inode *inode, const char *name, |
| 1517 | const void *value, size_t size, int flags) |
| 1518 | { |
| 1519 | return gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, value, size, flags); |
| 1520 | } |
| 1521 | |
| 1522 | static int gfs2_xattr_security_get(struct inode *inode, const char *name, |
| 1523 | void *buffer, size_t size) |
| 1524 | { |
| 1525 | return gfs2_xattr_get(inode, GFS2_EATYPE_SECURITY, name, buffer, size); |
| 1526 | } |
| 1527 | |
| 1528 | static int gfs2_xattr_security_set(struct inode *inode, const char *name, |
| 1529 | const void *value, size_t size, int flags) |
| 1530 | { |
| 1531 | return gfs2_xattr_set(inode, GFS2_EATYPE_SECURITY, name, value, size, flags); |
| 1532 | } |
| 1533 | |
| 1534 | static struct xattr_handler gfs2_xattr_user_handler = { |
| 1535 | .prefix = XATTR_USER_PREFIX, |
| 1536 | .get = gfs2_xattr_user_get, |
| 1537 | .set = gfs2_xattr_user_set, |
| 1538 | }; |
| 1539 | |
| 1540 | static struct xattr_handler gfs2_xattr_security_handler = { |
| 1541 | .prefix = XATTR_SECURITY_PREFIX, |
| 1542 | .get = gfs2_xattr_security_get, |
| 1543 | .set = gfs2_xattr_security_set, |
| 1544 | }; |
| 1545 | |
| 1546 | static struct xattr_handler gfs2_xattr_system_handler = { |
| 1547 | .prefix = XATTR_SYSTEM_PREFIX, |
| 1548 | .get = gfs2_xattr_system_get, |
| 1549 | .set = gfs2_xattr_system_set, |
| 1550 | }; |
| 1551 | |
| 1552 | struct xattr_handler *gfs2_xattr_handlers[] = { |
| 1553 | &gfs2_xattr_user_handler, |
| 1554 | &gfs2_xattr_security_handler, |
| 1555 | &gfs2_xattr_system_handler, |
| 1556 | NULL, |
| 1557 | }; |
| 1558 | |