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