| 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
| 3 | * Copyright (C) 2004-2006 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 version 2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/completion.h> |
| 13 | #include <linux/buffer_head.h> |
| 14 | #include <linux/namei.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/xattr.h> |
| 17 | #include <linux/posix_acl.h> |
| 18 | #include <linux/gfs2_ondisk.h> |
| 19 | #include <linux/crc32.h> |
| 20 | #include <linux/fiemap.h> |
| 21 | #include <asm/uaccess.h> |
| 22 | |
| 23 | #include "gfs2.h" |
| 24 | #include "incore.h" |
| 25 | #include "acl.h" |
| 26 | #include "bmap.h" |
| 27 | #include "dir.h" |
| 28 | #include "xattr.h" |
| 29 | #include "glock.h" |
| 30 | #include "inode.h" |
| 31 | #include "meta_io.h" |
| 32 | #include "quota.h" |
| 33 | #include "rgrp.h" |
| 34 | #include "trans.h" |
| 35 | #include "util.h" |
| 36 | #include "super.h" |
| 37 | |
| 38 | /** |
| 39 | * gfs2_create - Create a file |
| 40 | * @dir: The directory in which to create the file |
| 41 | * @dentry: The dentry of the new file |
| 42 | * @mode: The mode of the new file |
| 43 | * |
| 44 | * Returns: errno |
| 45 | */ |
| 46 | |
| 47 | static int gfs2_create(struct inode *dir, struct dentry *dentry, |
| 48 | int mode, struct nameidata *nd) |
| 49 | { |
| 50 | struct gfs2_inode *dip = GFS2_I(dir); |
| 51 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 52 | struct gfs2_holder ghs[2]; |
| 53 | struct inode *inode; |
| 54 | |
| 55 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 56 | |
| 57 | for (;;) { |
| 58 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0); |
| 59 | if (!IS_ERR(inode)) { |
| 60 | gfs2_trans_end(sdp); |
| 61 | if (dip->i_alloc->al_rgd) |
| 62 | gfs2_inplace_release(dip); |
| 63 | gfs2_quota_unlock(dip); |
| 64 | gfs2_alloc_put(dip); |
| 65 | gfs2_glock_dq_uninit_m(2, ghs); |
| 66 | mark_inode_dirty(inode); |
| 67 | break; |
| 68 | } else if (PTR_ERR(inode) != -EEXIST || |
| 69 | (nd && nd->flags & LOOKUP_EXCL)) { |
| 70 | gfs2_holder_uninit(ghs); |
| 71 | return PTR_ERR(inode); |
| 72 | } |
| 73 | |
| 74 | inode = gfs2_lookupi(dir, &dentry->d_name, 0); |
| 75 | if (inode) { |
| 76 | if (!IS_ERR(inode)) { |
| 77 | gfs2_holder_uninit(ghs); |
| 78 | break; |
| 79 | } else { |
| 80 | gfs2_holder_uninit(ghs); |
| 81 | return PTR_ERR(inode); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | d_instantiate(dentry, inode); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * gfs2_lookup - Look up a filename in a directory and return its inode |
| 93 | * @dir: The directory inode |
| 94 | * @dentry: The dentry of the new inode |
| 95 | * @nd: passed from Linux VFS, ignored by us |
| 96 | * |
| 97 | * Called by the VFS layer. Lock dir and call gfs2_lookupi() |
| 98 | * |
| 99 | * Returns: errno |
| 100 | */ |
| 101 | |
| 102 | static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry, |
| 103 | struct nameidata *nd) |
| 104 | { |
| 105 | struct inode *inode = NULL; |
| 106 | |
| 107 | inode = gfs2_lookupi(dir, &dentry->d_name, 0); |
| 108 | if (inode && IS_ERR(inode)) |
| 109 | return ERR_CAST(inode); |
| 110 | |
| 111 | if (inode) { |
| 112 | struct gfs2_glock *gl = GFS2_I(inode)->i_gl; |
| 113 | struct gfs2_holder gh; |
| 114 | int error; |
| 115 | error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
| 116 | if (error) { |
| 117 | iput(inode); |
| 118 | return ERR_PTR(error); |
| 119 | } |
| 120 | gfs2_glock_dq_uninit(&gh); |
| 121 | return d_splice_alias(inode, dentry); |
| 122 | } |
| 123 | d_add(dentry, inode); |
| 124 | |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * gfs2_link - Link to a file |
| 130 | * @old_dentry: The inode to link |
| 131 | * @dir: Add link to this directory |
| 132 | * @dentry: The name of the link |
| 133 | * |
| 134 | * Link the inode in "old_dentry" into the directory "dir" with the |
| 135 | * name in "dentry". |
| 136 | * |
| 137 | * Returns: errno |
| 138 | */ |
| 139 | |
| 140 | static int gfs2_link(struct dentry *old_dentry, struct inode *dir, |
| 141 | struct dentry *dentry) |
| 142 | { |
| 143 | struct gfs2_inode *dip = GFS2_I(dir); |
| 144 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 145 | struct inode *inode = old_dentry->d_inode; |
| 146 | struct gfs2_inode *ip = GFS2_I(inode); |
| 147 | struct gfs2_holder ghs[2]; |
| 148 | struct buffer_head *dibh; |
| 149 | int alloc_required; |
| 150 | int error; |
| 151 | |
| 152 | if (S_ISDIR(inode->i_mode)) |
| 153 | return -EPERM; |
| 154 | |
| 155 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
| 156 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); |
| 157 | |
| 158 | error = gfs2_glock_nq(ghs); /* parent */ |
| 159 | if (error) |
| 160 | goto out_parent; |
| 161 | |
| 162 | error = gfs2_glock_nq(ghs + 1); /* child */ |
| 163 | if (error) |
| 164 | goto out_child; |
| 165 | |
| 166 | error = -ENOENT; |
| 167 | if (inode->i_nlink == 0) |
| 168 | goto out_gunlock; |
| 169 | |
| 170 | error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC, 0); |
| 171 | if (error) |
| 172 | goto out_gunlock; |
| 173 | |
| 174 | error = gfs2_dir_check(dir, &dentry->d_name, NULL); |
| 175 | switch (error) { |
| 176 | case -ENOENT: |
| 177 | break; |
| 178 | case 0: |
| 179 | error = -EEXIST; |
| 180 | default: |
| 181 | goto out_gunlock; |
| 182 | } |
| 183 | |
| 184 | error = -EINVAL; |
| 185 | if (!dip->i_inode.i_nlink) |
| 186 | goto out_gunlock; |
| 187 | error = -EFBIG; |
| 188 | if (dip->i_entries == (u32)-1) |
| 189 | goto out_gunlock; |
| 190 | error = -EPERM; |
| 191 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 192 | goto out_gunlock; |
| 193 | error = -EINVAL; |
| 194 | if (!ip->i_inode.i_nlink) |
| 195 | goto out_gunlock; |
| 196 | error = -EMLINK; |
| 197 | if (ip->i_inode.i_nlink == (u32)-1) |
| 198 | goto out_gunlock; |
| 199 | |
| 200 | alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name); |
| 201 | if (error < 0) |
| 202 | goto out_gunlock; |
| 203 | error = 0; |
| 204 | |
| 205 | if (alloc_required) { |
| 206 | struct gfs2_alloc *al = gfs2_alloc_get(dip); |
| 207 | if (!al) { |
| 208 | error = -ENOMEM; |
| 209 | goto out_gunlock; |
| 210 | } |
| 211 | |
| 212 | error = gfs2_quota_lock_check(dip); |
| 213 | if (error) |
| 214 | goto out_alloc; |
| 215 | |
| 216 | al->al_requested = sdp->sd_max_dirres; |
| 217 | |
| 218 | error = gfs2_inplace_reserve(dip); |
| 219 | if (error) |
| 220 | goto out_gunlock_q; |
| 221 | |
| 222 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
| 223 | gfs2_rg_blocks(al) + |
| 224 | 2 * RES_DINODE + RES_STATFS + |
| 225 | RES_QUOTA, 0); |
| 226 | if (error) |
| 227 | goto out_ipres; |
| 228 | } else { |
| 229 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0); |
| 230 | if (error) |
| 231 | goto out_ipres; |
| 232 | } |
| 233 | |
| 234 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 235 | if (error) |
| 236 | goto out_end_trans; |
| 237 | |
| 238 | error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode)); |
| 239 | if (error) |
| 240 | goto out_brelse; |
| 241 | |
| 242 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
| 243 | inc_nlink(&ip->i_inode); |
| 244 | ip->i_inode.i_ctime = CURRENT_TIME; |
| 245 | gfs2_dinode_out(ip, dibh->b_data); |
| 246 | mark_inode_dirty(&ip->i_inode); |
| 247 | |
| 248 | out_brelse: |
| 249 | brelse(dibh); |
| 250 | out_end_trans: |
| 251 | gfs2_trans_end(sdp); |
| 252 | out_ipres: |
| 253 | if (alloc_required) |
| 254 | gfs2_inplace_release(dip); |
| 255 | out_gunlock_q: |
| 256 | if (alloc_required) |
| 257 | gfs2_quota_unlock(dip); |
| 258 | out_alloc: |
| 259 | if (alloc_required) |
| 260 | gfs2_alloc_put(dip); |
| 261 | out_gunlock: |
| 262 | gfs2_glock_dq(ghs + 1); |
| 263 | out_child: |
| 264 | gfs2_glock_dq(ghs); |
| 265 | out_parent: |
| 266 | gfs2_holder_uninit(ghs); |
| 267 | gfs2_holder_uninit(ghs + 1); |
| 268 | if (!error) { |
| 269 | ihold(inode); |
| 270 | d_instantiate(dentry, inode); |
| 271 | mark_inode_dirty(inode); |
| 272 | } |
| 273 | return error; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * gfs2_unlink_ok - check to see that a inode is still in a directory |
| 278 | * @dip: the directory |
| 279 | * @name: the name of the file |
| 280 | * @ip: the inode |
| 281 | * |
| 282 | * Assumes that the lock on (at least) @dip is held. |
| 283 | * |
| 284 | * Returns: 0 if the parent/child relationship is correct, errno if it isn't |
| 285 | */ |
| 286 | |
| 287 | static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name, |
| 288 | const struct gfs2_inode *ip) |
| 289 | { |
| 290 | int error; |
| 291 | |
| 292 | if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode)) |
| 293 | return -EPERM; |
| 294 | |
| 295 | if ((dip->i_inode.i_mode & S_ISVTX) && |
| 296 | dip->i_inode.i_uid != current_fsuid() && |
| 297 | ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER)) |
| 298 | return -EPERM; |
| 299 | |
| 300 | if (IS_APPEND(&dip->i_inode)) |
| 301 | return -EPERM; |
| 302 | |
| 303 | error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0); |
| 304 | if (error) |
| 305 | return error; |
| 306 | |
| 307 | error = gfs2_dir_check(&dip->i_inode, name, ip); |
| 308 | if (error) |
| 309 | return error; |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * gfs2_unlink - Unlink a file |
| 316 | * @dir: The inode of the directory containing the file to unlink |
| 317 | * @dentry: The file itself |
| 318 | * |
| 319 | * Unlink a file. Call gfs2_unlinki() |
| 320 | * |
| 321 | * Returns: errno |
| 322 | */ |
| 323 | |
| 324 | static int gfs2_unlink(struct inode *dir, struct dentry *dentry) |
| 325 | { |
| 326 | struct gfs2_inode *dip = GFS2_I(dir); |
| 327 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 328 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
| 329 | struct gfs2_holder ghs[3]; |
| 330 | struct gfs2_rgrpd *rgd; |
| 331 | struct gfs2_holder ri_gh; |
| 332 | int error; |
| 333 | |
| 334 | error = gfs2_rindex_hold(sdp, &ri_gh); |
| 335 | if (error) |
| 336 | return error; |
| 337 | |
| 338 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
| 339 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); |
| 340 | |
| 341 | rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr); |
| 342 | gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2); |
| 343 | |
| 344 | |
| 345 | error = gfs2_glock_nq(ghs); /* parent */ |
| 346 | if (error) |
| 347 | goto out_parent; |
| 348 | |
| 349 | error = gfs2_glock_nq(ghs + 1); /* child */ |
| 350 | if (error) |
| 351 | goto out_child; |
| 352 | |
| 353 | error = -ENOENT; |
| 354 | if (ip->i_inode.i_nlink == 0) |
| 355 | goto out_rgrp; |
| 356 | |
| 357 | error = gfs2_glock_nq(ghs + 2); /* rgrp */ |
| 358 | if (error) |
| 359 | goto out_rgrp; |
| 360 | |
| 361 | error = gfs2_unlink_ok(dip, &dentry->d_name, ip); |
| 362 | if (error) |
| 363 | goto out_gunlock; |
| 364 | |
| 365 | error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0); |
| 366 | if (error) |
| 367 | goto out_gunlock; |
| 368 | |
| 369 | error = gfs2_dir_del(dip, &dentry->d_name); |
| 370 | if (error) |
| 371 | goto out_end_trans; |
| 372 | |
| 373 | error = gfs2_change_nlink(ip, -1); |
| 374 | |
| 375 | out_end_trans: |
| 376 | gfs2_trans_end(sdp); |
| 377 | out_gunlock: |
| 378 | gfs2_glock_dq(ghs + 2); |
| 379 | out_rgrp: |
| 380 | gfs2_holder_uninit(ghs + 2); |
| 381 | gfs2_glock_dq(ghs + 1); |
| 382 | out_child: |
| 383 | gfs2_holder_uninit(ghs + 1); |
| 384 | gfs2_glock_dq(ghs); |
| 385 | out_parent: |
| 386 | gfs2_holder_uninit(ghs); |
| 387 | gfs2_glock_dq_uninit(&ri_gh); |
| 388 | return error; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * gfs2_symlink - Create a symlink |
| 393 | * @dir: The directory to create the symlink in |
| 394 | * @dentry: The dentry to put the symlink in |
| 395 | * @symname: The thing which the link points to |
| 396 | * |
| 397 | * Returns: errno |
| 398 | */ |
| 399 | |
| 400 | static int gfs2_symlink(struct inode *dir, struct dentry *dentry, |
| 401 | const char *symname) |
| 402 | { |
| 403 | struct gfs2_inode *dip = GFS2_I(dir), *ip; |
| 404 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 405 | struct gfs2_holder ghs[2]; |
| 406 | struct inode *inode; |
| 407 | struct buffer_head *dibh; |
| 408 | int size; |
| 409 | int error; |
| 410 | |
| 411 | /* Must be stuffed with a null terminator for gfs2_follow_link() */ |
| 412 | size = strlen(symname); |
| 413 | if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1) |
| 414 | return -ENAMETOOLONG; |
| 415 | |
| 416 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 417 | |
| 418 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0); |
| 419 | if (IS_ERR(inode)) { |
| 420 | gfs2_holder_uninit(ghs); |
| 421 | return PTR_ERR(inode); |
| 422 | } |
| 423 | |
| 424 | ip = ghs[1].gh_gl->gl_object; |
| 425 | |
| 426 | i_size_write(inode, size); |
| 427 | |
| 428 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 429 | |
| 430 | if (!gfs2_assert_withdraw(sdp, !error)) { |
| 431 | gfs2_dinode_out(ip, dibh->b_data); |
| 432 | memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, |
| 433 | size); |
| 434 | brelse(dibh); |
| 435 | } |
| 436 | |
| 437 | gfs2_trans_end(sdp); |
| 438 | if (dip->i_alloc->al_rgd) |
| 439 | gfs2_inplace_release(dip); |
| 440 | gfs2_quota_unlock(dip); |
| 441 | gfs2_alloc_put(dip); |
| 442 | |
| 443 | gfs2_glock_dq_uninit_m(2, ghs); |
| 444 | |
| 445 | d_instantiate(dentry, inode); |
| 446 | mark_inode_dirty(inode); |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * gfs2_mkdir - Make a directory |
| 453 | * @dir: The parent directory of the new one |
| 454 | * @dentry: The dentry of the new directory |
| 455 | * @mode: The mode of the new directory |
| 456 | * |
| 457 | * Returns: errno |
| 458 | */ |
| 459 | |
| 460 | static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 461 | { |
| 462 | struct gfs2_inode *dip = GFS2_I(dir), *ip; |
| 463 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 464 | struct gfs2_holder ghs[2]; |
| 465 | struct inode *inode; |
| 466 | struct buffer_head *dibh; |
| 467 | int error; |
| 468 | |
| 469 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 470 | |
| 471 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0); |
| 472 | if (IS_ERR(inode)) { |
| 473 | gfs2_holder_uninit(ghs); |
| 474 | return PTR_ERR(inode); |
| 475 | } |
| 476 | |
| 477 | ip = ghs[1].gh_gl->gl_object; |
| 478 | |
| 479 | ip->i_inode.i_nlink = 2; |
| 480 | i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)); |
| 481 | ip->i_diskflags |= GFS2_DIF_JDATA; |
| 482 | ip->i_entries = 2; |
| 483 | |
| 484 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 485 | |
| 486 | if (!gfs2_assert_withdraw(sdp, !error)) { |
| 487 | struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data; |
| 488 | struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1); |
| 489 | |
| 490 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
| 491 | gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent); |
| 492 | dent->de_inum = di->di_num; /* already GFS2 endian */ |
| 493 | dent->de_type = cpu_to_be16(DT_DIR); |
| 494 | di->di_entries = cpu_to_be32(1); |
| 495 | |
| 496 | dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1)); |
| 497 | gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent); |
| 498 | |
| 499 | gfs2_inum_out(dip, dent); |
| 500 | dent->de_type = cpu_to_be16(DT_DIR); |
| 501 | |
| 502 | gfs2_dinode_out(ip, di); |
| 503 | |
| 504 | brelse(dibh); |
| 505 | } |
| 506 | |
| 507 | error = gfs2_change_nlink(dip, +1); |
| 508 | gfs2_assert_withdraw(sdp, !error); /* dip already pinned */ |
| 509 | |
| 510 | gfs2_trans_end(sdp); |
| 511 | if (dip->i_alloc->al_rgd) |
| 512 | gfs2_inplace_release(dip); |
| 513 | gfs2_quota_unlock(dip); |
| 514 | gfs2_alloc_put(dip); |
| 515 | |
| 516 | gfs2_glock_dq_uninit_m(2, ghs); |
| 517 | |
| 518 | d_instantiate(dentry, inode); |
| 519 | mark_inode_dirty(inode); |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * gfs2_rmdiri - Remove a directory |
| 526 | * @dip: The parent directory of the directory to be removed |
| 527 | * @name: The name of the directory to be removed |
| 528 | * @ip: The GFS2 inode of the directory to be removed |
| 529 | * |
| 530 | * Assumes Glocks on dip and ip are held |
| 531 | * |
| 532 | * Returns: errno |
| 533 | */ |
| 534 | |
| 535 | static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name, |
| 536 | struct gfs2_inode *ip) |
| 537 | { |
| 538 | int error; |
| 539 | |
| 540 | if (ip->i_entries != 2) { |
| 541 | if (gfs2_consist_inode(ip)) |
| 542 | gfs2_dinode_print(ip); |
| 543 | return -EIO; |
| 544 | } |
| 545 | |
| 546 | error = gfs2_dir_del(dip, name); |
| 547 | if (error) |
| 548 | return error; |
| 549 | |
| 550 | error = gfs2_change_nlink(dip, -1); |
| 551 | if (error) |
| 552 | return error; |
| 553 | |
| 554 | error = gfs2_dir_del(ip, &gfs2_qdot); |
| 555 | if (error) |
| 556 | return error; |
| 557 | |
| 558 | error = gfs2_dir_del(ip, &gfs2_qdotdot); |
| 559 | if (error) |
| 560 | return error; |
| 561 | |
| 562 | /* It looks odd, but it really should be done twice */ |
| 563 | error = gfs2_change_nlink(ip, -1); |
| 564 | if (error) |
| 565 | return error; |
| 566 | |
| 567 | error = gfs2_change_nlink(ip, -1); |
| 568 | if (error) |
| 569 | return error; |
| 570 | |
| 571 | return error; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * gfs2_rmdir - Remove a directory |
| 576 | * @dir: The parent directory of the directory to be removed |
| 577 | * @dentry: The dentry of the directory to remove |
| 578 | * |
| 579 | * Remove a directory. Call gfs2_rmdiri() |
| 580 | * |
| 581 | * Returns: errno |
| 582 | */ |
| 583 | |
| 584 | static int gfs2_rmdir(struct inode *dir, struct dentry *dentry) |
| 585 | { |
| 586 | struct gfs2_inode *dip = GFS2_I(dir); |
| 587 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 588 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
| 589 | struct gfs2_holder ghs[3]; |
| 590 | struct gfs2_rgrpd *rgd; |
| 591 | struct gfs2_holder ri_gh; |
| 592 | int error; |
| 593 | |
| 594 | error = gfs2_rindex_hold(sdp, &ri_gh); |
| 595 | if (error) |
| 596 | return error; |
| 597 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
| 598 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); |
| 599 | |
| 600 | rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr); |
| 601 | gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2); |
| 602 | |
| 603 | error = gfs2_glock_nq(ghs); /* parent */ |
| 604 | if (error) |
| 605 | goto out_parent; |
| 606 | |
| 607 | error = gfs2_glock_nq(ghs + 1); /* child */ |
| 608 | if (error) |
| 609 | goto out_child; |
| 610 | |
| 611 | error = -ENOENT; |
| 612 | if (ip->i_inode.i_nlink == 0) |
| 613 | goto out_rgrp; |
| 614 | |
| 615 | error = gfs2_glock_nq(ghs + 2); /* rgrp */ |
| 616 | if (error) |
| 617 | goto out_rgrp; |
| 618 | |
| 619 | error = gfs2_unlink_ok(dip, &dentry->d_name, ip); |
| 620 | if (error) |
| 621 | goto out_gunlock; |
| 622 | |
| 623 | if (ip->i_entries < 2) { |
| 624 | if (gfs2_consist_inode(ip)) |
| 625 | gfs2_dinode_print(ip); |
| 626 | error = -EIO; |
| 627 | goto out_gunlock; |
| 628 | } |
| 629 | if (ip->i_entries > 2) { |
| 630 | error = -ENOTEMPTY; |
| 631 | goto out_gunlock; |
| 632 | } |
| 633 | |
| 634 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0); |
| 635 | if (error) |
| 636 | goto out_gunlock; |
| 637 | |
| 638 | error = gfs2_rmdiri(dip, &dentry->d_name, ip); |
| 639 | |
| 640 | gfs2_trans_end(sdp); |
| 641 | |
| 642 | out_gunlock: |
| 643 | gfs2_glock_dq(ghs + 2); |
| 644 | out_rgrp: |
| 645 | gfs2_holder_uninit(ghs + 2); |
| 646 | gfs2_glock_dq(ghs + 1); |
| 647 | out_child: |
| 648 | gfs2_holder_uninit(ghs + 1); |
| 649 | gfs2_glock_dq(ghs); |
| 650 | out_parent: |
| 651 | gfs2_holder_uninit(ghs); |
| 652 | gfs2_glock_dq_uninit(&ri_gh); |
| 653 | return error; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * gfs2_mknod - Make a special file |
| 658 | * @dir: The directory in which the special file will reside |
| 659 | * @dentry: The dentry of the special file |
| 660 | * @mode: The mode of the special file |
| 661 | * @rdev: The device specification of the special file |
| 662 | * |
| 663 | */ |
| 664 | |
| 665 | static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode, |
| 666 | dev_t dev) |
| 667 | { |
| 668 | struct gfs2_inode *dip = GFS2_I(dir); |
| 669 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 670 | struct gfs2_holder ghs[2]; |
| 671 | struct inode *inode; |
| 672 | |
| 673 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 674 | |
| 675 | inode = gfs2_createi(ghs, &dentry->d_name, mode, dev); |
| 676 | if (IS_ERR(inode)) { |
| 677 | gfs2_holder_uninit(ghs); |
| 678 | return PTR_ERR(inode); |
| 679 | } |
| 680 | |
| 681 | gfs2_trans_end(sdp); |
| 682 | if (dip->i_alloc->al_rgd) |
| 683 | gfs2_inplace_release(dip); |
| 684 | gfs2_quota_unlock(dip); |
| 685 | gfs2_alloc_put(dip); |
| 686 | |
| 687 | gfs2_glock_dq_uninit_m(2, ghs); |
| 688 | |
| 689 | d_instantiate(dentry, inode); |
| 690 | mark_inode_dirty(inode); |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * gfs2_ok_to_move - check if it's ok to move a directory to another directory |
| 697 | * @this: move this |
| 698 | * @to: to here |
| 699 | * |
| 700 | * Follow @to back to the root and make sure we don't encounter @this |
| 701 | * Assumes we already hold the rename lock. |
| 702 | * |
| 703 | * Returns: errno |
| 704 | */ |
| 705 | |
| 706 | static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to) |
| 707 | { |
| 708 | struct inode *dir = &to->i_inode; |
| 709 | struct super_block *sb = dir->i_sb; |
| 710 | struct inode *tmp; |
| 711 | int error = 0; |
| 712 | |
| 713 | igrab(dir); |
| 714 | |
| 715 | for (;;) { |
| 716 | if (dir == &this->i_inode) { |
| 717 | error = -EINVAL; |
| 718 | break; |
| 719 | } |
| 720 | if (dir == sb->s_root->d_inode) { |
| 721 | error = 0; |
| 722 | break; |
| 723 | } |
| 724 | |
| 725 | tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1); |
| 726 | if (IS_ERR(tmp)) { |
| 727 | error = PTR_ERR(tmp); |
| 728 | break; |
| 729 | } |
| 730 | |
| 731 | iput(dir); |
| 732 | dir = tmp; |
| 733 | } |
| 734 | |
| 735 | iput(dir); |
| 736 | |
| 737 | return error; |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * gfs2_rename - Rename a file |
| 742 | * @odir: Parent directory of old file name |
| 743 | * @odentry: The old dentry of the file |
| 744 | * @ndir: Parent directory of new file name |
| 745 | * @ndentry: The new dentry of the file |
| 746 | * |
| 747 | * Returns: errno |
| 748 | */ |
| 749 | |
| 750 | static int gfs2_rename(struct inode *odir, struct dentry *odentry, |
| 751 | struct inode *ndir, struct dentry *ndentry) |
| 752 | { |
| 753 | struct gfs2_inode *odip = GFS2_I(odir); |
| 754 | struct gfs2_inode *ndip = GFS2_I(ndir); |
| 755 | struct gfs2_inode *ip = GFS2_I(odentry->d_inode); |
| 756 | struct gfs2_inode *nip = NULL; |
| 757 | struct gfs2_sbd *sdp = GFS2_SB(odir); |
| 758 | struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh; |
| 759 | struct gfs2_rgrpd *nrgd; |
| 760 | unsigned int num_gh; |
| 761 | int dir_rename = 0; |
| 762 | int alloc_required = 0; |
| 763 | unsigned int x; |
| 764 | int error; |
| 765 | |
| 766 | if (ndentry->d_inode) { |
| 767 | nip = GFS2_I(ndentry->d_inode); |
| 768 | if (ip == nip) |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | error = gfs2_rindex_hold(sdp, &ri_gh); |
| 773 | if (error) |
| 774 | return error; |
| 775 | |
| 776 | if (odip != ndip) { |
| 777 | error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, |
| 778 | 0, &r_gh); |
| 779 | if (error) |
| 780 | goto out; |
| 781 | |
| 782 | if (S_ISDIR(ip->i_inode.i_mode)) { |
| 783 | dir_rename = 1; |
| 784 | /* don't move a dirctory into it's subdir */ |
| 785 | error = gfs2_ok_to_move(ip, ndip); |
| 786 | if (error) |
| 787 | goto out_gunlock_r; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | num_gh = 1; |
| 792 | gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
| 793 | if (odip != ndip) { |
| 794 | gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); |
| 795 | num_gh++; |
| 796 | } |
| 797 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); |
| 798 | num_gh++; |
| 799 | |
| 800 | if (nip) { |
| 801 | gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); |
| 802 | num_gh++; |
| 803 | /* grab the resource lock for unlink flag twiddling |
| 804 | * this is the case of the target file already existing |
| 805 | * so we unlink before doing the rename |
| 806 | */ |
| 807 | nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr); |
| 808 | if (nrgd) |
| 809 | gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++); |
| 810 | } |
| 811 | |
| 812 | for (x = 0; x < num_gh; x++) { |
| 813 | error = gfs2_glock_nq(ghs + x); |
| 814 | if (error) |
| 815 | goto out_gunlock; |
| 816 | } |
| 817 | |
| 818 | error = -ENOENT; |
| 819 | if (ip->i_inode.i_nlink == 0) |
| 820 | goto out_gunlock; |
| 821 | |
| 822 | /* Check out the old directory */ |
| 823 | |
| 824 | error = gfs2_unlink_ok(odip, &odentry->d_name, ip); |
| 825 | if (error) |
| 826 | goto out_gunlock; |
| 827 | |
| 828 | /* Check out the new directory */ |
| 829 | |
| 830 | if (nip) { |
| 831 | error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip); |
| 832 | if (error) |
| 833 | goto out_gunlock; |
| 834 | |
| 835 | if (nip->i_inode.i_nlink == 0) { |
| 836 | error = -EAGAIN; |
| 837 | goto out_gunlock; |
| 838 | } |
| 839 | |
| 840 | if (S_ISDIR(nip->i_inode.i_mode)) { |
| 841 | if (nip->i_entries < 2) { |
| 842 | if (gfs2_consist_inode(nip)) |
| 843 | gfs2_dinode_print(nip); |
| 844 | error = -EIO; |
| 845 | goto out_gunlock; |
| 846 | } |
| 847 | if (nip->i_entries > 2) { |
| 848 | error = -ENOTEMPTY; |
| 849 | goto out_gunlock; |
| 850 | } |
| 851 | } |
| 852 | } else { |
| 853 | error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0); |
| 854 | if (error) |
| 855 | goto out_gunlock; |
| 856 | |
| 857 | error = gfs2_dir_check(ndir, &ndentry->d_name, NULL); |
| 858 | switch (error) { |
| 859 | case -ENOENT: |
| 860 | error = 0; |
| 861 | break; |
| 862 | case 0: |
| 863 | error = -EEXIST; |
| 864 | default: |
| 865 | goto out_gunlock; |
| 866 | }; |
| 867 | |
| 868 | if (odip != ndip) { |
| 869 | if (!ndip->i_inode.i_nlink) { |
| 870 | error = -ENOENT; |
| 871 | goto out_gunlock; |
| 872 | } |
| 873 | if (ndip->i_entries == (u32)-1) { |
| 874 | error = -EFBIG; |
| 875 | goto out_gunlock; |
| 876 | } |
| 877 | if (S_ISDIR(ip->i_inode.i_mode) && |
| 878 | ndip->i_inode.i_nlink == (u32)-1) { |
| 879 | error = -EMLINK; |
| 880 | goto out_gunlock; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | /* Check out the dir to be renamed */ |
| 886 | |
| 887 | if (dir_rename) { |
| 888 | error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0); |
| 889 | if (error) |
| 890 | goto out_gunlock; |
| 891 | } |
| 892 | |
| 893 | if (nip == NULL) |
| 894 | alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name); |
| 895 | error = alloc_required; |
| 896 | if (error < 0) |
| 897 | goto out_gunlock; |
| 898 | error = 0; |
| 899 | |
| 900 | if (alloc_required) { |
| 901 | struct gfs2_alloc *al = gfs2_alloc_get(ndip); |
| 902 | if (!al) { |
| 903 | error = -ENOMEM; |
| 904 | goto out_gunlock; |
| 905 | } |
| 906 | |
| 907 | error = gfs2_quota_lock_check(ndip); |
| 908 | if (error) |
| 909 | goto out_alloc; |
| 910 | |
| 911 | al->al_requested = sdp->sd_max_dirres; |
| 912 | |
| 913 | error = gfs2_inplace_reserve_ri(ndip); |
| 914 | if (error) |
| 915 | goto out_gunlock_q; |
| 916 | |
| 917 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
| 918 | gfs2_rg_blocks(al) + |
| 919 | 4 * RES_DINODE + 4 * RES_LEAF + |
| 920 | RES_STATFS + RES_QUOTA + 4, 0); |
| 921 | if (error) |
| 922 | goto out_ipreserv; |
| 923 | } else { |
| 924 | error = gfs2_trans_begin(sdp, 4 * RES_DINODE + |
| 925 | 5 * RES_LEAF + 4, 0); |
| 926 | if (error) |
| 927 | goto out_gunlock; |
| 928 | } |
| 929 | |
| 930 | /* Remove the target file, if it exists */ |
| 931 | |
| 932 | if (nip) { |
| 933 | if (S_ISDIR(nip->i_inode.i_mode)) |
| 934 | error = gfs2_rmdiri(ndip, &ndentry->d_name, nip); |
| 935 | else { |
| 936 | error = gfs2_dir_del(ndip, &ndentry->d_name); |
| 937 | if (error) |
| 938 | goto out_end_trans; |
| 939 | error = gfs2_change_nlink(nip, -1); |
| 940 | } |
| 941 | if (error) |
| 942 | goto out_end_trans; |
| 943 | } |
| 944 | |
| 945 | if (dir_rename) { |
| 946 | error = gfs2_change_nlink(ndip, +1); |
| 947 | if (error) |
| 948 | goto out_end_trans; |
| 949 | error = gfs2_change_nlink(odip, -1); |
| 950 | if (error) |
| 951 | goto out_end_trans; |
| 952 | |
| 953 | error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR); |
| 954 | if (error) |
| 955 | goto out_end_trans; |
| 956 | } else { |
| 957 | struct buffer_head *dibh; |
| 958 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 959 | if (error) |
| 960 | goto out_end_trans; |
| 961 | ip->i_inode.i_ctime = CURRENT_TIME; |
| 962 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
| 963 | gfs2_dinode_out(ip, dibh->b_data); |
| 964 | brelse(dibh); |
| 965 | } |
| 966 | |
| 967 | error = gfs2_dir_del(odip, &odentry->d_name); |
| 968 | if (error) |
| 969 | goto out_end_trans; |
| 970 | |
| 971 | error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode)); |
| 972 | if (error) |
| 973 | goto out_end_trans; |
| 974 | |
| 975 | out_end_trans: |
| 976 | gfs2_trans_end(sdp); |
| 977 | out_ipreserv: |
| 978 | if (alloc_required) |
| 979 | gfs2_inplace_release(ndip); |
| 980 | out_gunlock_q: |
| 981 | if (alloc_required) |
| 982 | gfs2_quota_unlock(ndip); |
| 983 | out_alloc: |
| 984 | if (alloc_required) |
| 985 | gfs2_alloc_put(ndip); |
| 986 | out_gunlock: |
| 987 | while (x--) { |
| 988 | gfs2_glock_dq(ghs + x); |
| 989 | gfs2_holder_uninit(ghs + x); |
| 990 | } |
| 991 | out_gunlock_r: |
| 992 | if (r_gh.gh_gl) |
| 993 | gfs2_glock_dq_uninit(&r_gh); |
| 994 | out: |
| 995 | gfs2_glock_dq_uninit(&ri_gh); |
| 996 | return error; |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * gfs2_follow_link - Follow a symbolic link |
| 1001 | * @dentry: The dentry of the link |
| 1002 | * @nd: Data that we pass to vfs_follow_link() |
| 1003 | * |
| 1004 | * This can handle symlinks of any size. |
| 1005 | * |
| 1006 | * Returns: 0 on success or error code |
| 1007 | */ |
| 1008 | |
| 1009 | static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 1010 | { |
| 1011 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
| 1012 | struct gfs2_holder i_gh; |
| 1013 | struct buffer_head *dibh; |
| 1014 | unsigned int x, size; |
| 1015 | char *buf; |
| 1016 | int error; |
| 1017 | |
| 1018 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh); |
| 1019 | error = gfs2_glock_nq(&i_gh); |
| 1020 | if (error) { |
| 1021 | gfs2_holder_uninit(&i_gh); |
| 1022 | nd_set_link(nd, ERR_PTR(error)); |
| 1023 | return NULL; |
| 1024 | } |
| 1025 | |
| 1026 | size = (unsigned int)i_size_read(&ip->i_inode); |
| 1027 | if (size == 0) { |
| 1028 | gfs2_consist_inode(ip); |
| 1029 | buf = ERR_PTR(-EIO); |
| 1030 | goto out; |
| 1031 | } |
| 1032 | |
| 1033 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1034 | if (error) { |
| 1035 | buf = ERR_PTR(error); |
| 1036 | goto out; |
| 1037 | } |
| 1038 | |
| 1039 | x = size + 1; |
| 1040 | buf = kmalloc(x, GFP_NOFS); |
| 1041 | if (!buf) |
| 1042 | buf = ERR_PTR(-ENOMEM); |
| 1043 | else |
| 1044 | memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x); |
| 1045 | brelse(dibh); |
| 1046 | out: |
| 1047 | gfs2_glock_dq_uninit(&i_gh); |
| 1048 | nd_set_link(nd, buf); |
| 1049 | return NULL; |
| 1050 | } |
| 1051 | |
| 1052 | static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p) |
| 1053 | { |
| 1054 | char *s = nd_get_link(nd); |
| 1055 | if (!IS_ERR(s)) |
| 1056 | kfree(s); |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * gfs2_permission - |
| 1061 | * @inode: The inode |
| 1062 | * @mask: The mask to be tested |
| 1063 | * @flags: Indicates whether this is an RCU path walk or not |
| 1064 | * |
| 1065 | * This may be called from the VFS directly, or from within GFS2 with the |
| 1066 | * inode locked, so we look to see if the glock is already locked and only |
| 1067 | * lock the glock if its not already been done. |
| 1068 | * |
| 1069 | * Returns: errno |
| 1070 | */ |
| 1071 | |
| 1072 | int gfs2_permission(struct inode *inode, int mask, unsigned int flags) |
| 1073 | { |
| 1074 | struct gfs2_inode *ip; |
| 1075 | struct gfs2_holder i_gh; |
| 1076 | int error; |
| 1077 | int unlock = 0; |
| 1078 | |
| 1079 | |
| 1080 | ip = GFS2_I(inode); |
| 1081 | if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) { |
| 1082 | if (flags & IPERM_FLAG_RCU) |
| 1083 | return -ECHILD; |
| 1084 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
| 1085 | if (error) |
| 1086 | return error; |
| 1087 | unlock = 1; |
| 1088 | } |
| 1089 | |
| 1090 | if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode)) |
| 1091 | error = -EACCES; |
| 1092 | else |
| 1093 | error = generic_permission(inode, mask, flags, gfs2_check_acl); |
| 1094 | if (unlock) |
| 1095 | gfs2_glock_dq_uninit(&i_gh); |
| 1096 | |
| 1097 | return error; |
| 1098 | } |
| 1099 | |
| 1100 | static int setattr_chown(struct inode *inode, struct iattr *attr) |
| 1101 | { |
| 1102 | struct gfs2_inode *ip = GFS2_I(inode); |
| 1103 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 1104 | u32 ouid, ogid, nuid, ngid; |
| 1105 | int error; |
| 1106 | |
| 1107 | ouid = inode->i_uid; |
| 1108 | ogid = inode->i_gid; |
| 1109 | nuid = attr->ia_uid; |
| 1110 | ngid = attr->ia_gid; |
| 1111 | |
| 1112 | if (!(attr->ia_valid & ATTR_UID) || ouid == nuid) |
| 1113 | ouid = nuid = NO_QUOTA_CHANGE; |
| 1114 | if (!(attr->ia_valid & ATTR_GID) || ogid == ngid) |
| 1115 | ogid = ngid = NO_QUOTA_CHANGE; |
| 1116 | |
| 1117 | if (!gfs2_alloc_get(ip)) |
| 1118 | return -ENOMEM; |
| 1119 | |
| 1120 | error = gfs2_quota_lock(ip, nuid, ngid); |
| 1121 | if (error) |
| 1122 | goto out_alloc; |
| 1123 | |
| 1124 | if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) { |
| 1125 | error = gfs2_quota_check(ip, nuid, ngid); |
| 1126 | if (error) |
| 1127 | goto out_gunlock_q; |
| 1128 | } |
| 1129 | |
| 1130 | error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0); |
| 1131 | if (error) |
| 1132 | goto out_gunlock_q; |
| 1133 | |
| 1134 | error = gfs2_setattr_simple(ip, attr); |
| 1135 | if (error) |
| 1136 | goto out_end_trans; |
| 1137 | |
| 1138 | if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) { |
| 1139 | u64 blocks = gfs2_get_inode_blocks(&ip->i_inode); |
| 1140 | gfs2_quota_change(ip, -blocks, ouid, ogid); |
| 1141 | gfs2_quota_change(ip, blocks, nuid, ngid); |
| 1142 | } |
| 1143 | |
| 1144 | out_end_trans: |
| 1145 | gfs2_trans_end(sdp); |
| 1146 | out_gunlock_q: |
| 1147 | gfs2_quota_unlock(ip); |
| 1148 | out_alloc: |
| 1149 | gfs2_alloc_put(ip); |
| 1150 | return error; |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * gfs2_setattr - Change attributes on an inode |
| 1155 | * @dentry: The dentry which is changing |
| 1156 | * @attr: The structure describing the change |
| 1157 | * |
| 1158 | * The VFS layer wants to change one or more of an inodes attributes. Write |
| 1159 | * that change out to disk. |
| 1160 | * |
| 1161 | * Returns: errno |
| 1162 | */ |
| 1163 | |
| 1164 | static int gfs2_setattr(struct dentry *dentry, struct iattr *attr) |
| 1165 | { |
| 1166 | struct inode *inode = dentry->d_inode; |
| 1167 | struct gfs2_inode *ip = GFS2_I(inode); |
| 1168 | struct gfs2_holder i_gh; |
| 1169 | int error; |
| 1170 | |
| 1171 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
| 1172 | if (error) |
| 1173 | return error; |
| 1174 | |
| 1175 | error = -EPERM; |
| 1176 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 1177 | goto out; |
| 1178 | |
| 1179 | error = inode_change_ok(inode, attr); |
| 1180 | if (error) |
| 1181 | goto out; |
| 1182 | |
| 1183 | if (attr->ia_valid & ATTR_SIZE) |
| 1184 | error = gfs2_setattr_size(inode, attr->ia_size); |
| 1185 | else if (attr->ia_valid & (ATTR_UID | ATTR_GID)) |
| 1186 | error = setattr_chown(inode, attr); |
| 1187 | else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode)) |
| 1188 | error = gfs2_acl_chmod(ip, attr); |
| 1189 | else |
| 1190 | error = gfs2_setattr_simple(ip, attr); |
| 1191 | |
| 1192 | out: |
| 1193 | gfs2_glock_dq_uninit(&i_gh); |
| 1194 | if (!error) |
| 1195 | mark_inode_dirty(inode); |
| 1196 | return error; |
| 1197 | } |
| 1198 | |
| 1199 | /** |
| 1200 | * gfs2_getattr - Read out an inode's attributes |
| 1201 | * @mnt: The vfsmount the inode is being accessed from |
| 1202 | * @dentry: The dentry to stat |
| 1203 | * @stat: The inode's stats |
| 1204 | * |
| 1205 | * This may be called from the VFS directly, or from within GFS2 with the |
| 1206 | * inode locked, so we look to see if the glock is already locked and only |
| 1207 | * lock the glock if its not already been done. Note that its the NFS |
| 1208 | * readdirplus operation which causes this to be called (from filldir) |
| 1209 | * with the glock already held. |
| 1210 | * |
| 1211 | * Returns: errno |
| 1212 | */ |
| 1213 | |
| 1214 | static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 1215 | struct kstat *stat) |
| 1216 | { |
| 1217 | struct inode *inode = dentry->d_inode; |
| 1218 | struct gfs2_inode *ip = GFS2_I(inode); |
| 1219 | struct gfs2_holder gh; |
| 1220 | int error; |
| 1221 | int unlock = 0; |
| 1222 | |
| 1223 | if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) { |
| 1224 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
| 1225 | if (error) |
| 1226 | return error; |
| 1227 | unlock = 1; |
| 1228 | } |
| 1229 | |
| 1230 | generic_fillattr(inode, stat); |
| 1231 | if (unlock) |
| 1232 | gfs2_glock_dq_uninit(&gh); |
| 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | static int gfs2_setxattr(struct dentry *dentry, const char *name, |
| 1238 | const void *data, size_t size, int flags) |
| 1239 | { |
| 1240 | struct inode *inode = dentry->d_inode; |
| 1241 | struct gfs2_inode *ip = GFS2_I(inode); |
| 1242 | struct gfs2_holder gh; |
| 1243 | int ret; |
| 1244 | |
| 1245 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
| 1246 | ret = gfs2_glock_nq(&gh); |
| 1247 | if (ret == 0) { |
| 1248 | ret = generic_setxattr(dentry, name, data, size, flags); |
| 1249 | gfs2_glock_dq(&gh); |
| 1250 | } |
| 1251 | gfs2_holder_uninit(&gh); |
| 1252 | return ret; |
| 1253 | } |
| 1254 | |
| 1255 | static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name, |
| 1256 | void *data, size_t size) |
| 1257 | { |
| 1258 | struct inode *inode = dentry->d_inode; |
| 1259 | struct gfs2_inode *ip = GFS2_I(inode); |
| 1260 | struct gfs2_holder gh; |
| 1261 | int ret; |
| 1262 | |
| 1263 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
| 1264 | ret = gfs2_glock_nq(&gh); |
| 1265 | if (ret == 0) { |
| 1266 | ret = generic_getxattr(dentry, name, data, size); |
| 1267 | gfs2_glock_dq(&gh); |
| 1268 | } |
| 1269 | gfs2_holder_uninit(&gh); |
| 1270 | return ret; |
| 1271 | } |
| 1272 | |
| 1273 | static int gfs2_removexattr(struct dentry *dentry, const char *name) |
| 1274 | { |
| 1275 | struct inode *inode = dentry->d_inode; |
| 1276 | struct gfs2_inode *ip = GFS2_I(inode); |
| 1277 | struct gfs2_holder gh; |
| 1278 | int ret; |
| 1279 | |
| 1280 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
| 1281 | ret = gfs2_glock_nq(&gh); |
| 1282 | if (ret == 0) { |
| 1283 | ret = generic_removexattr(dentry, name); |
| 1284 | gfs2_glock_dq(&gh); |
| 1285 | } |
| 1286 | gfs2_holder_uninit(&gh); |
| 1287 | return ret; |
| 1288 | } |
| 1289 | |
| 1290 | static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 1291 | u64 start, u64 len) |
| 1292 | { |
| 1293 | struct gfs2_inode *ip = GFS2_I(inode); |
| 1294 | struct gfs2_holder gh; |
| 1295 | int ret; |
| 1296 | |
| 1297 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); |
| 1298 | if (ret) |
| 1299 | return ret; |
| 1300 | |
| 1301 | mutex_lock(&inode->i_mutex); |
| 1302 | |
| 1303 | ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); |
| 1304 | if (ret) |
| 1305 | goto out; |
| 1306 | |
| 1307 | if (gfs2_is_stuffed(ip)) { |
| 1308 | u64 phys = ip->i_no_addr << inode->i_blkbits; |
| 1309 | u64 size = i_size_read(inode); |
| 1310 | u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED| |
| 1311 | FIEMAP_EXTENT_DATA_INLINE; |
| 1312 | phys += sizeof(struct gfs2_dinode); |
| 1313 | phys += start; |
| 1314 | if (start + len > size) |
| 1315 | len = size - start; |
| 1316 | if (start < size) |
| 1317 | ret = fiemap_fill_next_extent(fieinfo, start, phys, |
| 1318 | len, flags); |
| 1319 | if (ret == 1) |
| 1320 | ret = 0; |
| 1321 | } else { |
| 1322 | ret = __generic_block_fiemap(inode, fieinfo, start, len, |
| 1323 | gfs2_block_map); |
| 1324 | } |
| 1325 | |
| 1326 | gfs2_glock_dq_uninit(&gh); |
| 1327 | out: |
| 1328 | mutex_unlock(&inode->i_mutex); |
| 1329 | return ret; |
| 1330 | } |
| 1331 | |
| 1332 | const struct inode_operations gfs2_file_iops = { |
| 1333 | .permission = gfs2_permission, |
| 1334 | .setattr = gfs2_setattr, |
| 1335 | .getattr = gfs2_getattr, |
| 1336 | .setxattr = gfs2_setxattr, |
| 1337 | .getxattr = gfs2_getxattr, |
| 1338 | .listxattr = gfs2_listxattr, |
| 1339 | .removexattr = gfs2_removexattr, |
| 1340 | .fiemap = gfs2_fiemap, |
| 1341 | }; |
| 1342 | |
| 1343 | const struct inode_operations gfs2_dir_iops = { |
| 1344 | .create = gfs2_create, |
| 1345 | .lookup = gfs2_lookup, |
| 1346 | .link = gfs2_link, |
| 1347 | .unlink = gfs2_unlink, |
| 1348 | .symlink = gfs2_symlink, |
| 1349 | .mkdir = gfs2_mkdir, |
| 1350 | .rmdir = gfs2_rmdir, |
| 1351 | .mknod = gfs2_mknod, |
| 1352 | .rename = gfs2_rename, |
| 1353 | .permission = gfs2_permission, |
| 1354 | .setattr = gfs2_setattr, |
| 1355 | .getattr = gfs2_getattr, |
| 1356 | .setxattr = gfs2_setxattr, |
| 1357 | .getxattr = gfs2_getxattr, |
| 1358 | .listxattr = gfs2_listxattr, |
| 1359 | .removexattr = gfs2_removexattr, |
| 1360 | .fiemap = gfs2_fiemap, |
| 1361 | }; |
| 1362 | |
| 1363 | const struct inode_operations gfs2_symlink_iops = { |
| 1364 | .readlink = generic_readlink, |
| 1365 | .follow_link = gfs2_follow_link, |
| 1366 | .put_link = gfs2_put_link, |
| 1367 | .permission = gfs2_permission, |
| 1368 | .setattr = gfs2_setattr, |
| 1369 | .getattr = gfs2_getattr, |
| 1370 | .setxattr = gfs2_setxattr, |
| 1371 | .getxattr = gfs2_getxattr, |
| 1372 | .listxattr = gfs2_listxattr, |
| 1373 | .removexattr = gfs2_removexattr, |
| 1374 | .fiemap = gfs2_fiemap, |
| 1375 | }; |
| 1376 | |