| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
| 4 |
* Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/slab.h> |
| 8 |
#include <linux/spinlock.h> |
| 9 |
#include <linux/completion.h> |
| 10 |
#include <linux/buffer_head.h> |
| 11 |
#include <linux/namei.h> |
| 12 |
#include <linux/mm.h> |
| 13 |
#include <linux/cred.h> |
| 14 |
#include <linux/xattr.h> |
| 15 |
#include <linux/posix_acl.h> |
| 16 |
#include <linux/gfs2_ondisk.h> |
| 17 |
#include <linux/crc32.h> |
| 18 |
#include <linux/iomap.h> |
| 19 |
#include <linux/security.h> |
| 20 |
#include <linux/fiemap.h> |
| 21 |
#include <linux/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 |
#include "glops.h" |
| 38 |
|
| 39 |
static const struct inode_operations gfs2_file_iops; |
| 40 |
static const struct inode_operations gfs2_dir_iops; |
| 41 |
static const struct inode_operations gfs2_symlink_iops; |
| 42 |
|
| 43 |
/** |
| 44 |
* gfs2_set_iop - Sets inode operations |
| 45 |
* @inode: The inode with correct i_mode filled in |
| 46 |
* |
| 47 |
* GFS2 lookup code fills in vfs inode contents based on info obtained |
| 48 |
* from directory entry inside gfs2_inode_lookup(). |
| 49 |
*/ |
| 50 |
|
| 51 |
static void gfs2_set_iop(struct inode *inode) |
| 52 |
{ |
| 53 |
struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 54 |
umode_t mode = inode->i_mode; |
| 55 |
|
| 56 |
if (S_ISREG(mode)) { |
| 57 |
inode->i_op = &gfs2_file_iops; |
| 58 |
if (gfs2_localflocks(sdp)) |
| 59 |
inode->i_fop = &gfs2_file_fops_nolock; |
| 60 |
else |
| 61 |
inode->i_fop = &gfs2_file_fops; |
| 62 |
} else if (S_ISDIR(mode)) { |
| 63 |
inode->i_op = &gfs2_dir_iops; |
| 64 |
if (gfs2_localflocks(sdp)) |
| 65 |
inode->i_fop = &gfs2_dir_fops_nolock; |
| 66 |
else |
| 67 |
inode->i_fop = &gfs2_dir_fops; |
| 68 |
} else if (S_ISLNK(mode)) { |
| 69 |
inode->i_op = &gfs2_symlink_iops; |
| 70 |
} else { |
| 71 |
inode->i_op = &gfs2_file_iops; |
| 72 |
init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
static int iget_test(struct inode *inode, void *opaque) |
| 77 |
{ |
| 78 |
u64 no_addr = *(u64 *)opaque; |
| 79 |
|
| 80 |
return GFS2_I(inode)->i_no_addr == no_addr; |
| 81 |
} |
| 82 |
|
| 83 |
static int iget_set(struct inode *inode, void *opaque) |
| 84 |
{ |
| 85 |
u64 no_addr = *(u64 *)opaque; |
| 86 |
|
| 87 |
GFS2_I(inode)->i_no_addr = no_addr; |
| 88 |
inode->i_ino = no_addr; |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
void gfs2_setup_inode(struct inode *inode) |
| 93 |
{ |
| 94 |
gfp_t gfp_mask; |
| 95 |
|
| 96 |
/* |
| 97 |
* Ensure all page cache allocations are done from GFP_NOFS context to |
| 98 |
* prevent direct reclaim recursion back into the filesystem and blowing |
| 99 |
* stacks or deadlocking. |
| 100 |
*/ |
| 101 |
gfp_mask = mapping_gfp_mask(inode->i_mapping); |
| 102 |
mapping_set_gfp_mask(inode->i_mapping, gfp_mask & ~__GFP_FS); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* gfs2_inode_lookup - Lookup an inode |
| 107 |
* @sb: The super block |
| 108 |
* @type: The type of the inode |
| 109 |
* @no_addr: The inode number |
| 110 |
* @no_formal_ino: The inode generation number |
| 111 |
* @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED; |
| 112 |
* GFS2_BLKST_FREE to indicate not to verify) |
| 113 |
* |
| 114 |
* If @type is DT_UNKNOWN, the inode type is fetched from disk. |
| 115 |
* |
| 116 |
* If @blktype is anything other than GFS2_BLKST_FREE (which is used as a |
| 117 |
* placeholder because it doesn't otherwise make sense), the on-disk block type |
| 118 |
* is verified to be @blktype. |
| 119 |
* |
| 120 |
* When @no_formal_ino is non-zero, this function will return ERR_PTR(-ESTALE) |
| 121 |
* if it detects that @no_formal_ino doesn't match the actual inode generation |
| 122 |
* number. However, it doesn't always know unless @type is DT_UNKNOWN. |
| 123 |
* |
| 124 |
* Returns: A VFS inode, or an error |
| 125 |
*/ |
| 126 |
|
| 127 |
struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type, |
| 128 |
u64 no_addr, u64 no_formal_ino, |
| 129 |
unsigned int blktype) |
| 130 |
{ |
| 131 |
struct inode *inode; |
| 132 |
struct gfs2_inode *ip; |
| 133 |
struct gfs2_holder i_gh; |
| 134 |
int error; |
| 135 |
|
| 136 |
gfs2_holder_mark_uninitialized(&i_gh); |
| 137 |
inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr); |
| 138 |
if (!inode) |
| 139 |
return ERR_PTR(-ENOMEM); |
| 140 |
|
| 141 |
ip = GFS2_I(inode); |
| 142 |
|
| 143 |
if (inode_state_read_once(inode) & I_NEW) { |
| 144 |
struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 145 |
struct gfs2_glock *io_gl; |
| 146 |
int extra_flags = 0; |
| 147 |
|
| 148 |
gfs2_setup_inode(inode); |
| 149 |
error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, |
| 150 |
&ip->i_gl); |
| 151 |
if (unlikely(error)) |
| 152 |
goto fail; |
| 153 |
|
| 154 |
error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, |
| 155 |
&io_gl); |
| 156 |
if (unlikely(error)) |
| 157 |
goto fail; |
| 158 |
|
| 159 |
/* |
| 160 |
* The only caller that sets @blktype to GFS2_BLKST_UNLINKED is |
| 161 |
* delete_work_func(). Make sure not to cancel the delete work |
| 162 |
* from within itself here. |
| 163 |
*/ |
| 164 |
if (blktype == GFS2_BLKST_UNLINKED) |
| 165 |
extra_flags |= LM_FLAG_TRY; |
| 166 |
else |
| 167 |
gfs2_cancel_delete_work(io_gl); |
| 168 |
error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, |
| 169 |
GL_EXACT | GL_NOPID | extra_flags, |
| 170 |
&ip->i_iopen_gh); |
| 171 |
gfs2_glock_put(io_gl); |
| 172 |
if (unlikely(error)) |
| 173 |
goto fail; |
| 174 |
|
| 175 |
if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) { |
| 176 |
/* |
| 177 |
* The GL_SKIP flag indicates to skip reading the inode |
| 178 |
* block. We read the inode when instantiating it |
| 179 |
* after possibly checking the block type. |
| 180 |
*/ |
| 181 |
error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, |
| 182 |
GL_SKIP, &i_gh); |
| 183 |
if (error) |
| 184 |
goto fail; |
| 185 |
|
| 186 |
error = -ESTALE; |
| 187 |
if (no_formal_ino && |
| 188 |
gfs2_inode_already_deleted(ip->i_gl, no_formal_ino)) |
| 189 |
goto fail; |
| 190 |
|
| 191 |
if (blktype != GFS2_BLKST_FREE) { |
| 192 |
error = gfs2_check_blk_type(sdp, no_addr, |
| 193 |
blktype); |
| 194 |
if (error) |
| 195 |
goto fail; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
set_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags); |
| 200 |
|
| 201 |
/* Lowest possible timestamp; will be overwritten in gfs2_dinode_in. */ |
| 202 |
inode_set_atime(inode, |
| 203 |
1LL << (8 * sizeof(inode_get_atime_sec(inode)) - 1), |
| 204 |
0); |
| 205 |
|
| 206 |
glock_set_object(ip->i_gl, ip); |
| 207 |
|
| 208 |
if (type == DT_UNKNOWN) { |
| 209 |
/* Inode glock must be locked already */ |
| 210 |
error = gfs2_instantiate(&i_gh); |
| 211 |
if (error) { |
| 212 |
glock_clear_object(ip->i_gl, ip); |
| 213 |
goto fail; |
| 214 |
} |
| 215 |
} else { |
| 216 |
ip->i_no_formal_ino = no_formal_ino; |
| 217 |
inode->i_mode = DT2IF(type); |
| 218 |
} |
| 219 |
|
| 220 |
if (gfs2_holder_initialized(&i_gh)) |
| 221 |
gfs2_glock_dq_uninit(&i_gh); |
| 222 |
glock_set_object(ip->i_iopen_gh.gh_gl, ip); |
| 223 |
|
| 224 |
gfs2_set_iop(inode); |
| 225 |
unlock_new_inode(inode); |
| 226 |
} |
| 227 |
|
| 228 |
if (no_formal_ino && ip->i_no_formal_ino && |
| 229 |
no_formal_ino != ip->i_no_formal_ino) { |
| 230 |
iput(inode); |
| 231 |
return ERR_PTR(-ESTALE); |
| 232 |
} |
| 233 |
|
| 234 |
return inode; |
| 235 |
|
| 236 |
fail: |
| 237 |
if (error == GLR_TRYFAILED) |
| 238 |
error = -EAGAIN; |
| 239 |
if (gfs2_holder_initialized(&ip->i_iopen_gh)) |
| 240 |
gfs2_glock_dq_uninit(&ip->i_iopen_gh); |
| 241 |
if (gfs2_holder_initialized(&i_gh)) |
| 242 |
gfs2_glock_dq_uninit(&i_gh); |
| 243 |
if (ip->i_gl) { |
| 244 |
gfs2_glock_put(ip->i_gl); |
| 245 |
ip->i_gl = NULL; |
| 246 |
} |
| 247 |
iget_failed(inode); |
| 248 |
return ERR_PTR(error); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* gfs2_lookup_by_inum - look up an inode by inode number |
| 253 |
* @sdp: The super block |
| 254 |
* @no_addr: The inode number |
| 255 |
* @no_formal_ino: The inode generation number (0 for any) |
| 256 |
* @blktype: Requested block type (see gfs2_inode_lookup) |
| 257 |
*/ |
| 258 |
struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr, |
| 259 |
u64 no_formal_ino, unsigned int blktype) |
| 260 |
{ |
| 261 |
struct super_block *sb = sdp->sd_vfs; |
| 262 |
struct inode *inode; |
| 263 |
int error; |
| 264 |
|
| 265 |
inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, no_formal_ino, |
| 266 |
blktype); |
| 267 |
if (IS_ERR(inode)) |
| 268 |
return inode; |
| 269 |
|
| 270 |
if (no_formal_ino) { |
| 271 |
error = -EIO; |
| 272 |
if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM) |
| 273 |
goto fail_iput; |
| 274 |
} |
| 275 |
return inode; |
| 276 |
|
| 277 |
fail_iput: |
| 278 |
iput(inode); |
| 279 |
return ERR_PTR(error); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
/** |
| 284 |
* gfs2_lookup_meta - Look up an inode in a metadata directory |
| 285 |
* @dip: The directory |
| 286 |
* @name: The name of the inode |
| 287 |
*/ |
| 288 |
struct inode *gfs2_lookup_meta(struct inode *dip, const char *name) |
| 289 |
{ |
| 290 |
struct qstr qstr; |
| 291 |
struct inode *inode; |
| 292 |
|
| 293 |
gfs2_str2qstr(&qstr, name); |
| 294 |
inode = gfs2_lookupi(dip, &qstr, 1); |
| 295 |
if (IS_ERR_OR_NULL(inode)) |
| 296 |
return inode ? inode : ERR_PTR(-ENOENT); |
| 297 |
|
| 298 |
/* |
| 299 |
* Must not call back into the filesystem when allocating |
| 300 |
* pages in the metadata inode's address space. |
| 301 |
*/ |
| 302 |
mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); |
| 303 |
|
| 304 |
return inode; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
/** |
| 309 |
* gfs2_lookupi - Look up a filename in a directory and return its inode |
| 310 |
* @dir: The inode of the directory containing the inode to look-up |
| 311 |
* @name: The name of the inode to look for |
| 312 |
* @is_root: If 1, ignore the caller's permissions |
| 313 |
* |
| 314 |
* This can be called via the VFS filldir function when NFS is doing |
| 315 |
* a readdirplus and the inode which its intending to stat isn't |
| 316 |
* already in cache. In this case we must not take the directory glock |
| 317 |
* again, since the readdir call will have already taken that lock. |
| 318 |
* |
| 319 |
* Returns: errno |
| 320 |
*/ |
| 321 |
|
| 322 |
struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name, |
| 323 |
int is_root) |
| 324 |
{ |
| 325 |
struct super_block *sb = dir->i_sb; |
| 326 |
struct gfs2_inode *dip = GFS2_I(dir); |
| 327 |
struct gfs2_holder d_gh; |
| 328 |
int error = 0; |
| 329 |
struct inode *inode = NULL; |
| 330 |
|
| 331 |
gfs2_holder_mark_uninitialized(&d_gh); |
| 332 |
if (!name->len || name->len > GFS2_FNAMESIZE) |
| 333 |
return ERR_PTR(-ENAMETOOLONG); |
| 334 |
|
| 335 |
if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) || |
| 336 |
(name->len == 2 && memcmp(name->name, "..", 2) == 0 && |
| 337 |
dir == d_inode(sb->s_root))) { |
| 338 |
igrab(dir); |
| 339 |
return dir; |
| 340 |
} |
| 341 |
|
| 342 |
if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) { |
| 343 |
error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh); |
| 344 |
if (error) |
| 345 |
return ERR_PTR(error); |
| 346 |
} |
| 347 |
|
| 348 |
if (!is_root) { |
| 349 |
error = gfs2_permission(&nop_mnt_idmap, dir, MAY_EXEC); |
| 350 |
if (error) |
| 351 |
goto out; |
| 352 |
} |
| 353 |
|
| 354 |
inode = gfs2_dir_search(dir, name, false); |
| 355 |
if (IS_ERR(inode)) |
| 356 |
error = PTR_ERR(inode); |
| 357 |
out: |
| 358 |
if (gfs2_holder_initialized(&d_gh)) |
| 359 |
gfs2_glock_dq_uninit(&d_gh); |
| 360 |
if (error == -ENOENT) |
| 361 |
return NULL; |
| 362 |
return inode ? inode : ERR_PTR(error); |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* create_ok - OK to create a new on-disk inode here? |
| 367 |
* @dip: Directory in which dinode is to be created |
| 368 |
* @name: Name of new dinode |
| 369 |
* @mode: |
| 370 |
* |
| 371 |
* Returns: errno |
| 372 |
*/ |
| 373 |
|
| 374 |
static int create_ok(struct gfs2_inode *dip, const struct qstr *name, |
| 375 |
umode_t mode) |
| 376 |
{ |
| 377 |
int error; |
| 378 |
|
| 379 |
error = gfs2_permission(&nop_mnt_idmap, &dip->i_inode, |
| 380 |
MAY_WRITE | MAY_EXEC); |
| 381 |
if (error) |
| 382 |
return error; |
| 383 |
|
| 384 |
/* Don't create entries in an unlinked directory */ |
| 385 |
if (!dip->i_inode.i_nlink) |
| 386 |
return -ENOENT; |
| 387 |
|
| 388 |
if (dip->i_entries == (u32)-1) |
| 389 |
return -EFBIG; |
| 390 |
if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1) |
| 391 |
return -EMLINK; |
| 392 |
|
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
static void munge_mode_uid_gid(const struct gfs2_inode *dip, |
| 397 |
struct inode *inode) |
| 398 |
{ |
| 399 |
if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir && |
| 400 |
(dip->i_inode.i_mode & S_ISUID) && |
| 401 |
!uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) { |
| 402 |
if (S_ISDIR(inode->i_mode)) |
| 403 |
inode->i_mode |= S_ISUID; |
| 404 |
else if (!uid_eq(dip->i_inode.i_uid, current_fsuid())) |
| 405 |
inode->i_mode &= ~07111; |
| 406 |
inode->i_uid = dip->i_inode.i_uid; |
| 407 |
} else |
| 408 |
inode->i_uid = current_fsuid(); |
| 409 |
|
| 410 |
if (dip->i_inode.i_mode & S_ISGID) { |
| 411 |
if (S_ISDIR(inode->i_mode)) |
| 412 |
inode->i_mode |= S_ISGID; |
| 413 |
inode->i_gid = dip->i_inode.i_gid; |
| 414 |
} else |
| 415 |
inode->i_gid = current_fsgid(); |
| 416 |
} |
| 417 |
|
| 418 |
static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks) |
| 419 |
{ |
| 420 |
struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 421 |
struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, }; |
| 422 |
int error; |
| 423 |
|
| 424 |
error = gfs2_quota_lock_check(ip, &ap); |
| 425 |
if (error) |
| 426 |
goto out; |
| 427 |
|
| 428 |
error = gfs2_inplace_reserve(ip, &ap); |
| 429 |
if (error) |
| 430 |
goto out_quota; |
| 431 |
|
| 432 |
error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0); |
| 433 |
if (error) |
| 434 |
goto out_ipreserv; |
| 435 |
|
| 436 |
error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1); |
| 437 |
if (error) |
| 438 |
goto out_trans_end; |
| 439 |
|
| 440 |
ip->i_no_formal_ino = ip->i_generation; |
| 441 |
ip->i_inode.i_ino = ip->i_no_addr; |
| 442 |
ip->i_goal = ip->i_no_addr; |
| 443 |
if (*dblocks > 1) |
| 444 |
ip->i_eattr = ip->i_no_addr + 1; |
| 445 |
|
| 446 |
out_trans_end: |
| 447 |
gfs2_trans_end(sdp); |
| 448 |
out_ipreserv: |
| 449 |
gfs2_inplace_release(ip); |
| 450 |
out_quota: |
| 451 |
gfs2_quota_unlock(ip); |
| 452 |
out: |
| 453 |
return error; |
| 454 |
} |
| 455 |
|
| 456 |
static void gfs2_final_release_pages(struct gfs2_inode *ip) |
| 457 |
{ |
| 458 |
struct inode *inode = &ip->i_inode; |
| 459 |
struct gfs2_glock *gl = ip->i_gl; |
| 460 |
|
| 461 |
/* This can only happen during incomplete inode creation. */ |
| 462 |
if (unlikely(!gl)) |
| 463 |
return; |
| 464 |
|
| 465 |
truncate_inode_pages(gfs2_glock2aspace(gl), 0); |
| 466 |
truncate_inode_pages(&inode->i_data, 0); |
| 467 |
|
| 468 |
if (atomic_read(&gl->gl_revokes) == 0) { |
| 469 |
clear_bit(GLF_LFLUSH, &gl->gl_flags); |
| 470 |
clear_bit(GLF_DIRTY, &gl->gl_flags); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
int gfs2_dinode_dealloc(struct gfs2_inode *ip) |
| 475 |
{ |
| 476 |
struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 477 |
struct gfs2_rgrpd *rgd; |
| 478 |
struct gfs2_holder gh; |
| 479 |
int error; |
| 480 |
|
| 481 |
if (gfs2_get_inode_blocks(&ip->i_inode) != 1) { |
| 482 |
gfs2_consist_inode(ip); |
| 483 |
return -EIO; |
| 484 |
} |
| 485 |
|
| 486 |
gfs2_rindex_update(sdp); |
| 487 |
|
| 488 |
error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE); |
| 489 |
if (error) |
| 490 |
return error; |
| 491 |
|
| 492 |
rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1); |
| 493 |
if (!rgd) { |
| 494 |
gfs2_consist_inode(ip); |
| 495 |
error = -EIO; |
| 496 |
goto out_qs; |
| 497 |
} |
| 498 |
|
| 499 |
error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, |
| 500 |
LM_FLAG_NODE_SCOPE, &gh); |
| 501 |
if (error) |
| 502 |
goto out_qs; |
| 503 |
|
| 504 |
error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, |
| 505 |
sdp->sd_jdesc->jd_blocks); |
| 506 |
if (error) |
| 507 |
goto out_rg_gunlock; |
| 508 |
|
| 509 |
gfs2_free_di(rgd, ip); |
| 510 |
|
| 511 |
gfs2_final_release_pages(ip); |
| 512 |
|
| 513 |
gfs2_trans_end(sdp); |
| 514 |
|
| 515 |
out_rg_gunlock: |
| 516 |
gfs2_glock_dq_uninit(&gh); |
| 517 |
out_qs: |
| 518 |
gfs2_quota_unhold(ip); |
| 519 |
return error; |
| 520 |
} |
| 521 |
|
| 522 |
static void gfs2_init_dir(struct buffer_head *dibh, |
| 523 |
const struct gfs2_inode *parent) |
| 524 |
{ |
| 525 |
struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data; |
| 526 |
struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1); |
| 527 |
|
| 528 |
gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent); |
| 529 |
dent->de_inum = di->di_num; /* already GFS2 endian */ |
| 530 |
dent->de_type = cpu_to_be16(DT_DIR); |
| 531 |
|
| 532 |
dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1)); |
| 533 |
gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent); |
| 534 |
gfs2_inum_out(parent, dent); |
| 535 |
dent->de_type = cpu_to_be16(DT_DIR); |
| 536 |
|
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* gfs2_init_xattr - Initialise an xattr block for a new inode |
| 541 |
* @ip: The inode in question |
| 542 |
* |
| 543 |
* This sets up an empty xattr block for a new inode, ready to |
| 544 |
* take any ACLs, LSM xattrs, etc. |
| 545 |
*/ |
| 546 |
|
| 547 |
static void gfs2_init_xattr(struct gfs2_inode *ip) |
| 548 |
{ |
| 549 |
struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 550 |
struct buffer_head *bh; |
| 551 |
struct gfs2_ea_header *ea; |
| 552 |
|
| 553 |
bh = gfs2_meta_new(ip->i_gl, ip->i_eattr); |
| 554 |
gfs2_trans_add_meta(ip->i_gl, bh); |
| 555 |
gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA); |
| 556 |
gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header)); |
| 557 |
|
| 558 |
ea = GFS2_EA_BH2FIRST(bh); |
| 559 |
ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize); |
| 560 |
ea->ea_type = GFS2_EATYPE_UNUSED; |
| 561 |
ea->ea_flags = GFS2_EAFLAG_LAST; |
| 562 |
|
| 563 |
brelse(bh); |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* init_dinode - Fill in a new dinode structure |
| 568 |
* @dip: The directory this inode is being created in |
| 569 |
* @ip: The inode |
| 570 |
* @symname: The symlink destination (if a symlink) |
| 571 |
* |
| 572 |
*/ |
| 573 |
|
| 574 |
static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip, |
| 575 |
const char *symname) |
| 576 |
{ |
| 577 |
struct gfs2_dinode *di; |
| 578 |
struct buffer_head *dibh; |
| 579 |
|
| 580 |
dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr); |
| 581 |
gfs2_trans_add_meta(ip->i_gl, dibh); |
| 582 |
di = (struct gfs2_dinode *)dibh->b_data; |
| 583 |
gfs2_dinode_out(ip, di); |
| 584 |
|
| 585 |
di->di_major = cpu_to_be32(imajor(&ip->i_inode)); |
| 586 |
di->di_minor = cpu_to_be32(iminor(&ip->i_inode)); |
| 587 |
di->__pad1 = 0; |
| 588 |
di->__pad2 = 0; |
| 589 |
di->__pad3 = 0; |
| 590 |
memset(&di->__pad4, 0, sizeof(di->__pad4)); |
| 591 |
memset(&di->di_reserved, 0, sizeof(di->di_reserved)); |
| 592 |
gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); |
| 593 |
|
| 594 |
switch(ip->i_inode.i_mode & S_IFMT) { |
| 595 |
case S_IFDIR: |
| 596 |
gfs2_init_dir(dibh, dip); |
| 597 |
break; |
| 598 |
case S_IFLNK: |
| 599 |
memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size); |
| 600 |
break; |
| 601 |
} |
| 602 |
|
| 603 |
set_buffer_uptodate(dibh); |
| 604 |
brelse(dibh); |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* gfs2_trans_da_blks - Calculate number of blocks to link inode |
| 609 |
* @dip: The directory we are linking into |
| 610 |
* @da: The dir add information |
| 611 |
* @nr_inodes: The number of inodes involved |
| 612 |
* |
| 613 |
* This calculate the number of blocks we need to reserve in a |
| 614 |
* transaction to link @nr_inodes into a directory. In most cases |
| 615 |
* @nr_inodes will be 2 (the directory plus the inode being linked in) |
| 616 |
* but in case of rename, 4 may be required. |
| 617 |
* |
| 618 |
* Returns: Number of blocks |
| 619 |
*/ |
| 620 |
|
| 621 |
static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip, |
| 622 |
const struct gfs2_diradd *da, |
| 623 |
unsigned nr_inodes) |
| 624 |
{ |
| 625 |
return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) + |
| 626 |
(nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS; |
| 627 |
} |
| 628 |
|
| 629 |
static int link_dinode(struct gfs2_inode *dip, const struct qstr *name, |
| 630 |
struct gfs2_inode *ip, struct gfs2_diradd *da) |
| 631 |
{ |
| 632 |
struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
| 633 |
struct gfs2_alloc_parms ap = { .target = da->nr_blocks, }; |
| 634 |
int error; |
| 635 |
|
| 636 |
if (da->nr_blocks) { |
| 637 |
error = gfs2_quota_lock_check(dip, &ap); |
| 638 |
if (error) |
| 639 |
goto fail_quota_locks; |
| 640 |
|
| 641 |
error = gfs2_inplace_reserve(dip, &ap); |
| 642 |
if (error) |
| 643 |
goto fail_quota_locks; |
| 644 |
|
| 645 |
error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0); |
| 646 |
if (error) |
| 647 |
goto fail_ipreserv; |
| 648 |
} else { |
| 649 |
error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0); |
| 650 |
if (error) |
| 651 |
goto fail_quota_locks; |
| 652 |
} |
| 653 |
|
| 654 |
error = gfs2_dir_add(&dip->i_inode, name, ip, da); |
| 655 |
|
| 656 |
gfs2_trans_end(sdp); |
| 657 |
fail_ipreserv: |
| 658 |
gfs2_inplace_release(dip); |
| 659 |
fail_quota_locks: |
| 660 |
gfs2_quota_unlock(dip); |
| 661 |
return error; |
| 662 |
} |
| 663 |
|
| 664 |
static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array, |
| 665 |
void *fs_info) |
| 666 |
{ |
| 667 |
const struct xattr *xattr; |
| 668 |
int err = 0; |
| 669 |
|
| 670 |
for (xattr = xattr_array; xattr->name != NULL; xattr++) { |
| 671 |
err = __gfs2_xattr_set(inode, xattr->name, xattr->value, |
| 672 |
xattr->value_len, 0, |
| 673 |
GFS2_EATYPE_SECURITY); |
| 674 |
if (err < 0) |
| 675 |
break; |
| 676 |
} |
| 677 |
return err; |
| 678 |
} |
| 679 |
|
| 680 |
/** |
| 681 |
* gfs2_create_inode - Create a new inode |
| 682 |
* @dir: The parent directory |
| 683 |
* @dentry: The new dentry |
| 684 |
* @file: If non-NULL, the file which is being opened |
| 685 |
* @mode: The permissions on the new inode |
| 686 |
* @dev: For device nodes, this is the device number |
| 687 |
* @symname: For symlinks, this is the link destination |
| 688 |
* @size: The initial size of the inode (ignored for directories) |
| 689 |
* @excl: Force fail if inode exists |
| 690 |
* |
| 691 |
* FIXME: Change to allocate the disk blocks and write them out in the same |
| 692 |
* transaction. That way, we can no longer end up in a situation in which an |
| 693 |
* inode is allocated, the node crashes, and the block looks like a valid |
| 694 |
* inode. (With atomic creates in place, we will also no longer need to zero |
| 695 |
* the link count and dirty the inode here on failure.) |
| 696 |
* |
| 697 |
* Returns: 0 on success, or error code |
| 698 |
*/ |
| 699 |
|
| 700 |
static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, |
| 701 |
struct file *file, |
| 702 |
umode_t mode, dev_t dev, const char *symname, |
| 703 |
unsigned int size, int excl) |
| 704 |
{ |
| 705 |
const struct qstr *name = &dentry->d_name; |
| 706 |
struct posix_acl *default_acl, *acl; |
| 707 |
struct gfs2_holder d_gh, gh; |
| 708 |
struct inode *inode = NULL; |
| 709 |
struct gfs2_inode *dip = GFS2_I(dir), *ip; |
| 710 |
struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
| 711 |
struct gfs2_glock *io_gl; |
| 712 |
int error, dealloc_error; |
| 713 |
u32 aflags = 0; |
| 714 |
unsigned blocks = 1; |
| 715 |
struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, }; |
| 716 |
bool xattr_initialized = false; |
| 717 |
|
| 718 |
if (!name->len || name->len > GFS2_FNAMESIZE) |
| 719 |
return -ENAMETOOLONG; |
| 720 |
|
| 721 |
error = gfs2_qa_get(dip); |
| 722 |
if (error) |
| 723 |
return error; |
| 724 |
|
| 725 |
error = gfs2_rindex_update(sdp); |
| 726 |
if (error) |
| 727 |
goto fail; |
| 728 |
|
| 729 |
error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh); |
| 730 |
if (error) |
| 731 |
goto fail; |
| 732 |
gfs2_holder_mark_uninitialized(&gh); |
| 733 |
|
| 734 |
error = create_ok(dip, name, mode); |
| 735 |
if (error) |
| 736 |
goto fail_gunlock; |
| 737 |
|
| 738 |
inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl); |
| 739 |
error = PTR_ERR(inode); |
| 740 |
if (!IS_ERR(inode)) { |
| 741 |
if (S_ISDIR(inode->i_mode)) { |
| 742 |
iput(inode); |
| 743 |
inode = NULL; |
| 744 |
error = -EISDIR; |
| 745 |
goto fail_gunlock; |
| 746 |
} |
| 747 |
d_instantiate(dentry, inode); |
| 748 |
error = 0; |
| 749 |
if (file) { |
| 750 |
if (S_ISREG(inode->i_mode)) |
| 751 |
error = finish_open(file, dentry, gfs2_open_common); |
| 752 |
else |
| 753 |
error = finish_no_open(file, NULL); |
| 754 |
} |
| 755 |
gfs2_glock_dq_uninit(&d_gh); |
| 756 |
goto fail; |
| 757 |
} else if (error != -ENOENT) { |
| 758 |
goto fail_gunlock; |
| 759 |
} |
| 760 |
|
| 761 |
error = gfs2_diradd_alloc_required(dir, name, &da); |
| 762 |
if (error < 0) |
| 763 |
goto fail_gunlock; |
| 764 |
|
| 765 |
inode = new_inode(sdp->sd_vfs); |
| 766 |
error = -ENOMEM; |
| 767 |
if (!inode) |
| 768 |
goto fail_gunlock; |
| 769 |
gfs2_setup_inode(inode); |
| 770 |
ip = GFS2_I(inode); |
| 771 |
|
| 772 |
error = posix_acl_create(dir, &mode, &default_acl, &acl); |
| 773 |
if (error) |
| 774 |
goto fail_gunlock; |
| 775 |
|
| 776 |
error = gfs2_qa_get(ip); |
| 777 |
if (error) |
| 778 |
goto fail_free_acls; |
| 779 |
|
| 780 |
inode->i_mode = mode; |
| 781 |
set_nlink(inode, S_ISDIR(mode) ? 2 : 1); |
| 782 |
inode->i_rdev = dev; |
| 783 |
inode->i_size = size; |
| 784 |
simple_inode_init_ts(inode); |
| 785 |
munge_mode_uid_gid(dip, inode); |
| 786 |
check_and_update_goal(dip); |
| 787 |
ip->i_goal = dip->i_goal; |
| 788 |
ip->i_diskflags = 0; |
| 789 |
ip->i_eattr = 0; |
| 790 |
ip->i_height = 0; |
| 791 |
ip->i_depth = 0; |
| 792 |
ip->i_entries = 0; |
| 793 |
ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */ |
| 794 |
|
| 795 |
switch(mode & S_IFMT) { |
| 796 |
case S_IFREG: |
| 797 |
if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) || |
| 798 |
gfs2_tune_get(sdp, gt_new_files_jdata)) |
| 799 |
ip->i_diskflags |= GFS2_DIF_JDATA; |
| 800 |
gfs2_set_aops(inode); |
| 801 |
break; |
| 802 |
case S_IFDIR: |
| 803 |
ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA); |
| 804 |
ip->i_diskflags |= GFS2_DIF_JDATA; |
| 805 |
ip->i_entries = 2; |
| 806 |
break; |
| 807 |
} |
| 808 |
|
| 809 |
/* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */ |
| 810 |
if (dip->i_diskflags & GFS2_DIF_SYSTEM) |
| 811 |
ip->i_diskflags |= GFS2_DIF_SYSTEM; |
| 812 |
|
| 813 |
gfs2_set_inode_flags(inode); |
| 814 |
|
| 815 |
if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) || |
| 816 |
(dip->i_diskflags & GFS2_DIF_TOPDIR)) |
| 817 |
aflags |= GFS2_AF_ORLOV; |
| 818 |
|
| 819 |
if (default_acl || acl) |
| 820 |
blocks++; |
| 821 |
|
| 822 |
error = alloc_dinode(ip, aflags, &blocks); |
| 823 |
if (error) |
| 824 |
goto fail_free_inode; |
| 825 |
|
| 826 |
gfs2_set_inode_blocks(inode, blocks); |
| 827 |
|
| 828 |
error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl); |
| 829 |
if (error) |
| 830 |
goto fail_dealloc_inode; |
| 831 |
|
| 832 |
error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl); |
| 833 |
if (error) |
| 834 |
goto fail_dealloc_inode; |
| 835 |
gfs2_cancel_delete_work(io_gl); |
| 836 |
io_gl->gl_no_formal_ino = ip->i_no_formal_ino; |
| 837 |
|
| 838 |
retry: |
| 839 |
error = insert_inode_locked4(inode, ip->i_no_addr, iget_test, &ip->i_no_addr); |
| 840 |
if (error == -EBUSY) |
| 841 |
goto retry; |
| 842 |
if (error) |
| 843 |
goto fail_gunlock2; |
| 844 |
|
| 845 |
error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT | GL_NOPID, |
| 846 |
&ip->i_iopen_gh); |
| 847 |
if (error) |
| 848 |
goto fail_gunlock2; |
| 849 |
|
| 850 |
error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, &gh); |
| 851 |
if (error) |
| 852 |
goto fail_gunlock3; |
| 853 |
clear_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags); |
| 854 |
|
| 855 |
error = gfs2_trans_begin(sdp, blocks, 0); |
| 856 |
if (error) |
| 857 |
goto fail_gunlock3; |
| 858 |
|
| 859 |
if (blocks > 1) { |
| 860 |
gfs2_init_xattr(ip); |
| 861 |
xattr_initialized = true; |
| 862 |
} |
| 863 |
init_dinode(dip, ip, symname); |
| 864 |
gfs2_trans_end(sdp); |
| 865 |
|
| 866 |
glock_set_object(ip->i_gl, ip); |
| 867 |
glock_set_object(io_gl, ip); |
| 868 |
gfs2_set_iop(inode); |
| 869 |
|
| 870 |
if (default_acl) { |
| 871 |
error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT); |
| 872 |
if (error) |
| 873 |
goto fail_gunlock4; |
| 874 |
posix_acl_release(default_acl); |
| 875 |
default_acl = NULL; |
| 876 |
} |
| 877 |
if (acl) { |
| 878 |
error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS); |
| 879 |
if (error) |
| 880 |
goto fail_gunlock4; |
| 881 |
posix_acl_release(acl); |
| 882 |
acl = NULL; |
| 883 |
} |
| 884 |
|
| 885 |
error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name, |
| 886 |
&gfs2_initxattrs, NULL); |
| 887 |
if (error) |
| 888 |
goto fail_gunlock4; |
| 889 |
|
| 890 |
error = link_dinode(dip, name, ip, &da); |
| 891 |
if (error) |
| 892 |
goto fail_gunlock4; |
| 893 |
|
| 894 |
mark_inode_dirty(inode); |
| 895 |
d_instantiate(dentry, inode); |
| 896 |
/* After instantiate, errors should result in evict which will destroy |
| 897 |
* both inode and iopen glocks properly. */ |
| 898 |
if (file) { |
| 899 |
file->f_mode |= FMODE_CREATED; |
| 900 |
error = finish_open(file, dentry, gfs2_open_common); |
| 901 |
} |
| 902 |
gfs2_glock_dq_uninit(&d_gh); |
| 903 |
gfs2_qa_put(ip); |
| 904 |
gfs2_glock_dq_uninit(&gh); |
| 905 |
gfs2_glock_put(io_gl); |
| 906 |
gfs2_qa_put(dip); |
| 907 |
unlock_new_inode(inode); |
| 908 |
return error; |
| 909 |
|
| 910 |
fail_gunlock4: |
| 911 |
glock_clear_object(ip->i_gl, ip); |
| 912 |
glock_clear_object(io_gl, ip); |
| 913 |
fail_gunlock3: |
| 914 |
gfs2_glock_dq_uninit(&ip->i_iopen_gh); |
| 915 |
fail_gunlock2: |
| 916 |
gfs2_glock_put(io_gl); |
| 917 |
fail_dealloc_inode: |
| 918 |
dealloc_error = 0; |
| 919 |
if (ip->i_eattr) |
| 920 |
dealloc_error = gfs2_ea_dealloc(ip, xattr_initialized); |
| 921 |
clear_nlink(inode); |
| 922 |
mark_inode_dirty(inode); |
| 923 |
if (!dealloc_error) |
| 924 |
dealloc_error = gfs2_dinode_dealloc(ip); |
| 925 |
if (dealloc_error) |
| 926 |
fs_warn(sdp, "%s: %d\n", __func__, dealloc_error); |
| 927 |
ip->i_no_addr = 0; |
| 928 |
fail_free_inode: |
| 929 |
if (ip->i_gl) { |
| 930 |
gfs2_glock_put(ip->i_gl); |
| 931 |
ip->i_gl = NULL; |
| 932 |
} |
| 933 |
gfs2_rs_deltree(&ip->i_res); |
| 934 |
gfs2_qa_put(ip); |
| 935 |
fail_free_acls: |
| 936 |
posix_acl_release(default_acl); |
| 937 |
posix_acl_release(acl); |
| 938 |
fail_gunlock: |
| 939 |
gfs2_dir_no_add(&da); |
| 940 |
gfs2_glock_dq_uninit(&d_gh); |
| 941 |
if (!IS_ERR_OR_NULL(inode)) { |
| 942 |
if (inode_state_read_once(inode) & I_NEW) |
| 943 |
iget_failed(inode); |
| 944 |
else |
| 945 |
iput(inode); |
| 946 |
} |
| 947 |
if (gfs2_holder_initialized(&gh)) |
| 948 |
gfs2_glock_dq_uninit(&gh); |
| 949 |
fail: |
| 950 |
gfs2_qa_put(dip); |
| 951 |
return error; |
| 952 |
} |
| 953 |
|
| 954 |
/** |
| 955 |
* gfs2_create - Create a file |
| 956 |
* @idmap: idmap of the mount the inode was found from |
| 957 |
* @dir: The directory in which to create the file |
| 958 |
* @dentry: The dentry of the new file |
| 959 |
* @mode: The mode of the new file |
| 960 |
* @excl: Force fail if inode exists |
| 961 |
* |
| 962 |
* Returns: errno |
| 963 |
*/ |
| 964 |
|
| 965 |
static int gfs2_create(struct mnt_idmap *idmap, struct inode *dir, |
| 966 |
struct dentry *dentry, umode_t mode, bool excl) |
| 967 |
{ |
| 968 |
return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl); |
| 969 |
} |
| 970 |
|
| 971 |
/** |
| 972 |
* __gfs2_lookup - Look up a filename in a directory and return its inode |
| 973 |
* @dir: The directory inode |
| 974 |
* @dentry: The dentry of the new inode |
| 975 |
* @file: File to be opened |
| 976 |
* |
| 977 |
* |
| 978 |
* Returns: errno |
| 979 |
*/ |
| 980 |
|
| 981 |
static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry, |
| 982 |
struct file *file) |
| 983 |
{ |
| 984 |
struct inode *inode; |
| 985 |
struct dentry *d; |
| 986 |
struct gfs2_holder gh; |
| 987 |
struct gfs2_glock *gl; |
| 988 |
int error; |
| 989 |
|
| 990 |
inode = gfs2_lookupi(dir, &dentry->d_name, 0); |
| 991 |
if (inode == NULL) { |
| 992 |
d_add(dentry, NULL); |
| 993 |
return NULL; |
| 994 |
} |
| 995 |
if (IS_ERR(inode)) |
| 996 |
return ERR_CAST(inode); |
| 997 |
|
| 998 |
gl = GFS2_I(inode)->i_gl; |
| 999 |
error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
| 1000 |
if (error) { |
| 1001 |
iput(inode); |
| 1002 |
return ERR_PTR(error); |
| 1003 |
} |
| 1004 |
|
| 1005 |
d = d_splice_alias(inode, dentry); |
| 1006 |
if (IS_ERR(d)) { |
| 1007 |
gfs2_glock_dq_uninit(&gh); |
| 1008 |
return d; |
| 1009 |
} |
| 1010 |
if (file && S_ISREG(inode->i_mode)) |
| 1011 |
error = finish_open(file, dentry, gfs2_open_common); |
| 1012 |
|
| 1013 |
gfs2_glock_dq_uninit(&gh); |
| 1014 |
if (error) { |
| 1015 |
dput(d); |
| 1016 |
return ERR_PTR(error); |
| 1017 |
} |
| 1018 |
return d; |
| 1019 |
} |
| 1020 |
|
| 1021 |
static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry, |
| 1022 |
unsigned flags) |
| 1023 |
{ |
| 1024 |
return __gfs2_lookup(dir, dentry, NULL); |
| 1025 |
} |
| 1026 |
|
| 1027 |
/** |
| 1028 |
* gfs2_link - Link to a file |
| 1029 |
* @old_dentry: The inode to link |
| 1030 |
* @dir: Add link to this directory |
| 1031 |
* @dentry: The name of the link |
| 1032 |
* |
| 1033 |
* Link the inode in "old_dentry" into the directory "dir" with the |
| 1034 |
* name in "dentry". |
| 1035 |
* |
| 1036 |
* Returns: errno |
| 1037 |
*/ |
| 1038 |
|
| 1039 |
static int gfs2_link(struct dentry *old_dentry, struct inode *dir, |
| 1040 |
struct dentry *dentry) |
| 1041 |
{ |
| 1042 |
struct gfs2_inode *dip = GFS2_I(dir); |
| 1043 |
struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 1044 |
struct inode *inode = d_inode(old_dentry); |
| 1045 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 1046 |
struct gfs2_holder d_gh, gh; |
| 1047 |
struct buffer_head *dibh; |
| 1048 |
struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, }; |
| 1049 |
int error; |
| 1050 |
|
| 1051 |
if (S_ISDIR(inode->i_mode)) |
| 1052 |
return -EPERM; |
| 1053 |
|
| 1054 |
error = gfs2_qa_get(dip); |
| 1055 |
if (error) |
| 1056 |
return error; |
| 1057 |
|
| 1058 |
gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh); |
| 1059 |
gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
| 1060 |
|
| 1061 |
error = gfs2_glock_nq(&d_gh); |
| 1062 |
if (error) |
| 1063 |
goto out_parent; |
| 1064 |
|
| 1065 |
error = gfs2_glock_nq(&gh); |
| 1066 |
if (error) |
| 1067 |
goto out_child; |
| 1068 |
|
| 1069 |
error = -ENOENT; |
| 1070 |
if (inode->i_nlink == 0) |
| 1071 |
goto out_gunlock; |
| 1072 |
|
| 1073 |
error = gfs2_permission(&nop_mnt_idmap, dir, MAY_WRITE | MAY_EXEC); |
| 1074 |
if (error) |
| 1075 |
goto out_gunlock; |
| 1076 |
|
| 1077 |
error = gfs2_dir_check(dir, &dentry->d_name, NULL); |
| 1078 |
switch (error) { |
| 1079 |
case -ENOENT: |
| 1080 |
break; |
| 1081 |
case 0: |
| 1082 |
error = -EEXIST; |
| 1083 |
goto out_gunlock; |
| 1084 |
default: |
| 1085 |
goto out_gunlock; |
| 1086 |
} |
| 1087 |
|
| 1088 |
error = -EINVAL; |
| 1089 |
if (!dip->i_inode.i_nlink) |
| 1090 |
goto out_gunlock; |
| 1091 |
error = -EFBIG; |
| 1092 |
if (dip->i_entries == (u32)-1) |
| 1093 |
goto out_gunlock; |
| 1094 |
error = -EPERM; |
| 1095 |
if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 1096 |
goto out_gunlock; |
| 1097 |
error = -EMLINK; |
| 1098 |
if (ip->i_inode.i_nlink == (u32)-1) |
| 1099 |
goto out_gunlock; |
| 1100 |
|
| 1101 |
error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da); |
| 1102 |
if (error < 0) |
| 1103 |
goto out_gunlock; |
| 1104 |
|
| 1105 |
if (da.nr_blocks) { |
| 1106 |
struct gfs2_alloc_parms ap = { .target = da.nr_blocks, }; |
| 1107 |
error = gfs2_quota_lock_check(dip, &ap); |
| 1108 |
if (error) |
| 1109 |
goto out_gunlock; |
| 1110 |
|
| 1111 |
error = gfs2_inplace_reserve(dip, &ap); |
| 1112 |
if (error) |
| 1113 |
goto out_gunlock_q; |
| 1114 |
|
| 1115 |
error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0); |
| 1116 |
if (error) |
| 1117 |
goto out_ipres; |
| 1118 |
} else { |
| 1119 |
error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0); |
| 1120 |
if (error) |
| 1121 |
goto out_ipres; |
| 1122 |
} |
| 1123 |
|
| 1124 |
error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1125 |
if (error) |
| 1126 |
goto out_end_trans; |
| 1127 |
|
| 1128 |
error = gfs2_dir_add(dir, &dentry->d_name, ip, &da); |
| 1129 |
if (error) |
| 1130 |
goto out_brelse; |
| 1131 |
|
| 1132 |
gfs2_trans_add_meta(ip->i_gl, dibh); |
| 1133 |
inc_nlink(&ip->i_inode); |
| 1134 |
inode_set_ctime_current(&ip->i_inode); |
| 1135 |
ihold(inode); |
| 1136 |
d_instantiate(dentry, inode); |
| 1137 |
mark_inode_dirty(inode); |
| 1138 |
|
| 1139 |
out_brelse: |
| 1140 |
brelse(dibh); |
| 1141 |
out_end_trans: |
| 1142 |
gfs2_trans_end(sdp); |
| 1143 |
out_ipres: |
| 1144 |
if (da.nr_blocks) |
| 1145 |
gfs2_inplace_release(dip); |
| 1146 |
out_gunlock_q: |
| 1147 |
if (da.nr_blocks) |
| 1148 |
gfs2_quota_unlock(dip); |
| 1149 |
out_gunlock: |
| 1150 |
gfs2_dir_no_add(&da); |
| 1151 |
gfs2_glock_dq(&gh); |
| 1152 |
out_child: |
| 1153 |
gfs2_glock_dq(&d_gh); |
| 1154 |
out_parent: |
| 1155 |
gfs2_qa_put(dip); |
| 1156 |
gfs2_holder_uninit(&d_gh); |
| 1157 |
gfs2_holder_uninit(&gh); |
| 1158 |
return error; |
| 1159 |
} |
| 1160 |
|
| 1161 |
/* |
| 1162 |
* gfs2_unlink_ok - check to see that a inode is still in a directory |
| 1163 |
* @dip: the directory |
| 1164 |
* @name: the name of the file |
| 1165 |
* @ip: the inode |
| 1166 |
* |
| 1167 |
* Assumes that the lock on (at least) @dip is held. |
| 1168 |
* |
| 1169 |
* Returns: 0 if the parent/child relationship is correct, errno if it isn't |
| 1170 |
*/ |
| 1171 |
|
| 1172 |
static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name, |
| 1173 |
const struct gfs2_inode *ip) |
| 1174 |
{ |
| 1175 |
int error; |
| 1176 |
|
| 1177 |
if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode)) |
| 1178 |
return -EPERM; |
| 1179 |
|
| 1180 |
if ((dip->i_inode.i_mode & S_ISVTX) && |
| 1181 |
!uid_eq(dip->i_inode.i_uid, current_fsuid()) && |
| 1182 |
!uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER)) |
| 1183 |
return -EPERM; |
| 1184 |
|
| 1185 |
if (IS_APPEND(&dip->i_inode)) |
| 1186 |
return -EPERM; |
| 1187 |
|
| 1188 |
error = gfs2_permission(&nop_mnt_idmap, &dip->i_inode, |
| 1189 |
MAY_WRITE | MAY_EXEC); |
| 1190 |
if (error) |
| 1191 |
return error; |
| 1192 |
|
| 1193 |
return gfs2_dir_check(&dip->i_inode, name, ip); |
| 1194 |
} |
| 1195 |
|
| 1196 |
/** |
| 1197 |
* gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it |
| 1198 |
* @dip: The parent directory |
| 1199 |
* @dentry: The dentry to unlink |
| 1200 |
* |
| 1201 |
* Called with all the locks and in a transaction. This will only be |
| 1202 |
* called for a directory after it has been checked to ensure it is empty. |
| 1203 |
* |
| 1204 |
* Returns: 0 on success, or an error |
| 1205 |
*/ |
| 1206 |
|
| 1207 |
static int gfs2_unlink_inode(struct gfs2_inode *dip, |
| 1208 |
const struct dentry *dentry) |
| 1209 |
{ |
| 1210 |
struct inode *inode = d_inode(dentry); |
| 1211 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 1212 |
int error; |
| 1213 |
|
| 1214 |
error = gfs2_dir_del(dip, dentry); |
| 1215 |
if (error) |
| 1216 |
return error; |
| 1217 |
|
| 1218 |
ip->i_entries = 0; |
| 1219 |
inode_set_ctime_current(inode); |
| 1220 |
if (S_ISDIR(inode->i_mode)) |
| 1221 |
clear_nlink(inode); |
| 1222 |
else |
| 1223 |
drop_nlink(inode); |
| 1224 |
mark_inode_dirty(inode); |
| 1225 |
if (inode->i_nlink == 0) |
| 1226 |
gfs2_unlink_di(inode); |
| 1227 |
return 0; |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
/** |
| 1232 |
* gfs2_unlink - Unlink an inode (this does rmdir as well) |
| 1233 |
* @dir: The inode of the directory containing the inode to unlink |
| 1234 |
* @dentry: The file itself |
| 1235 |
* |
| 1236 |
* This routine uses the type of the inode as a flag to figure out |
| 1237 |
* whether this is an unlink or an rmdir. |
| 1238 |
* |
| 1239 |
* Returns: errno |
| 1240 |
*/ |
| 1241 |
|
| 1242 |
static int gfs2_unlink(struct inode *dir, struct dentry *dentry) |
| 1243 |
{ |
| 1244 |
struct gfs2_inode *dip = GFS2_I(dir); |
| 1245 |
struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 1246 |
struct inode *inode = d_inode(dentry); |
| 1247 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 1248 |
struct gfs2_holder d_gh, r_gh, gh; |
| 1249 |
struct gfs2_rgrpd *rgd; |
| 1250 |
int error; |
| 1251 |
|
| 1252 |
error = gfs2_rindex_update(sdp); |
| 1253 |
if (error) |
| 1254 |
return error; |
| 1255 |
|
| 1256 |
error = -EROFS; |
| 1257 |
|
| 1258 |
gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh); |
| 1259 |
gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
| 1260 |
|
| 1261 |
rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1); |
| 1262 |
if (!rgd) |
| 1263 |
goto out_inodes; |
| 1264 |
|
| 1265 |
gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE, &r_gh); |
| 1266 |
|
| 1267 |
|
| 1268 |
error = gfs2_glock_nq(&d_gh); |
| 1269 |
if (error) |
| 1270 |
goto out_parent; |
| 1271 |
|
| 1272 |
error = gfs2_glock_nq(&gh); |
| 1273 |
if (error) |
| 1274 |
goto out_child; |
| 1275 |
|
| 1276 |
error = -ENOENT; |
| 1277 |
if (inode->i_nlink == 0) |
| 1278 |
goto out_rgrp; |
| 1279 |
|
| 1280 |
if (S_ISDIR(inode->i_mode)) { |
| 1281 |
error = -ENOTEMPTY; |
| 1282 |
if (ip->i_entries > 2 || inode->i_nlink > 2) |
| 1283 |
goto out_rgrp; |
| 1284 |
} |
| 1285 |
|
| 1286 |
error = gfs2_glock_nq(&r_gh); /* rgrp */ |
| 1287 |
if (error) |
| 1288 |
goto out_rgrp; |
| 1289 |
|
| 1290 |
error = gfs2_unlink_ok(dip, &dentry->d_name, ip); |
| 1291 |
if (error) |
| 1292 |
goto out_gunlock; |
| 1293 |
|
| 1294 |
error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0); |
| 1295 |
if (error) |
| 1296 |
goto out_gunlock; |
| 1297 |
|
| 1298 |
error = gfs2_unlink_inode(dip, dentry); |
| 1299 |
gfs2_trans_end(sdp); |
| 1300 |
|
| 1301 |
out_gunlock: |
| 1302 |
gfs2_glock_dq(&r_gh); |
| 1303 |
out_rgrp: |
| 1304 |
gfs2_glock_dq(&gh); |
| 1305 |
out_child: |
| 1306 |
gfs2_glock_dq(&d_gh); |
| 1307 |
out_parent: |
| 1308 |
gfs2_holder_uninit(&r_gh); |
| 1309 |
out_inodes: |
| 1310 |
gfs2_holder_uninit(&gh); |
| 1311 |
gfs2_holder_uninit(&d_gh); |
| 1312 |
return error; |
| 1313 |
} |
| 1314 |
|
| 1315 |
/** |
| 1316 |
* gfs2_symlink - Create a symlink |
| 1317 |
* @idmap: idmap of the mount the inode was found from |
| 1318 |
* @dir: The directory to create the symlink in |
| 1319 |
* @dentry: The dentry to put the symlink in |
| 1320 |
* @symname: The thing which the link points to |
| 1321 |
* |
| 1322 |
* Returns: errno |
| 1323 |
*/ |
| 1324 |
|
| 1325 |
static int gfs2_symlink(struct mnt_idmap *idmap, struct inode *dir, |
| 1326 |
struct dentry *dentry, const char *symname) |
| 1327 |
{ |
| 1328 |
unsigned int size; |
| 1329 |
|
| 1330 |
size = strlen(symname); |
| 1331 |
if (size >= gfs2_max_stuffed_size(GFS2_I(dir))) |
| 1332 |
return -ENAMETOOLONG; |
| 1333 |
|
| 1334 |
return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0); |
| 1335 |
} |
| 1336 |
|
| 1337 |
/** |
| 1338 |
* gfs2_mkdir - Make a directory |
| 1339 |
* @idmap: idmap of the mount the inode was found from |
| 1340 |
* @dir: The parent directory of the new one |
| 1341 |
* @dentry: The dentry of the new directory |
| 1342 |
* @mode: The mode of the new directory |
| 1343 |
* |
| 1344 |
* Returns: the dentry, or ERR_PTR(errno) |
| 1345 |
*/ |
| 1346 |
|
| 1347 |
static struct dentry *gfs2_mkdir(struct mnt_idmap *idmap, struct inode *dir, |
| 1348 |
struct dentry *dentry, umode_t mode) |
| 1349 |
{ |
| 1350 |
unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir)); |
| 1351 |
|
| 1352 |
return ERR_PTR(gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0)); |
| 1353 |
} |
| 1354 |
|
| 1355 |
/** |
| 1356 |
* gfs2_mknod - Make a special file |
| 1357 |
* @idmap: idmap of the mount the inode was found from |
| 1358 |
* @dir: The directory in which the special file will reside |
| 1359 |
* @dentry: The dentry of the special file |
| 1360 |
* @mode: The mode of the special file |
| 1361 |
* @dev: The device specification of the special file |
| 1362 |
* |
| 1363 |
*/ |
| 1364 |
|
| 1365 |
static int gfs2_mknod(struct mnt_idmap *idmap, struct inode *dir, |
| 1366 |
struct dentry *dentry, umode_t mode, dev_t dev) |
| 1367 |
{ |
| 1368 |
return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0); |
| 1369 |
} |
| 1370 |
|
| 1371 |
/** |
| 1372 |
* gfs2_atomic_open - Atomically open a file |
| 1373 |
* @dir: The directory |
| 1374 |
* @dentry: The proposed new entry |
| 1375 |
* @file: The proposed new struct file |
| 1376 |
* @flags: open flags |
| 1377 |
* @mode: File mode |
| 1378 |
* |
| 1379 |
* Returns: error code or 0 for success |
| 1380 |
*/ |
| 1381 |
|
| 1382 |
static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry, |
| 1383 |
struct file *file, unsigned flags, |
| 1384 |
umode_t mode) |
| 1385 |
{ |
| 1386 |
bool excl = !!(flags & O_EXCL); |
| 1387 |
|
| 1388 |
if (d_in_lookup(dentry)) { |
| 1389 |
struct dentry *d = __gfs2_lookup(dir, dentry, file); |
| 1390 |
if (file->f_mode & FMODE_OPENED) { |
| 1391 |
if (IS_ERR(d)) |
| 1392 |
return PTR_ERR(d); |
| 1393 |
dput(d); |
| 1394 |
return excl && (flags & O_CREAT) ? -EEXIST : 0; |
| 1395 |
} |
| 1396 |
if (d || d_really_is_positive(dentry)) |
| 1397 |
return finish_no_open(file, d); |
| 1398 |
} |
| 1399 |
if (!(flags & O_CREAT)) |
| 1400 |
return -ENOENT; |
| 1401 |
|
| 1402 |
return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl); |
| 1403 |
} |
| 1404 |
|
| 1405 |
/* |
| 1406 |
* gfs2_ok_to_move - check if it's ok to move a directory to another directory |
| 1407 |
* @this: move this |
| 1408 |
* @to: to here |
| 1409 |
* |
| 1410 |
* Follow @to back to the root and make sure we don't encounter @this |
| 1411 |
* Assumes we already hold the rename lock. |
| 1412 |
* |
| 1413 |
* Returns: errno |
| 1414 |
*/ |
| 1415 |
|
| 1416 |
static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to) |
| 1417 |
{ |
| 1418 |
struct inode *dir = &to->i_inode; |
| 1419 |
struct super_block *sb = dir->i_sb; |
| 1420 |
struct inode *tmp; |
| 1421 |
int error = 0; |
| 1422 |
|
| 1423 |
igrab(dir); |
| 1424 |
|
| 1425 |
for (;;) { |
| 1426 |
if (dir == &this->i_inode) { |
| 1427 |
error = -EINVAL; |
| 1428 |
break; |
| 1429 |
} |
| 1430 |
if (dir == d_inode(sb->s_root)) { |
| 1431 |
error = 0; |
| 1432 |
break; |
| 1433 |
} |
| 1434 |
|
| 1435 |
tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1); |
| 1436 |
if (!tmp) { |
| 1437 |
error = -ENOENT; |
| 1438 |
break; |
| 1439 |
} |
| 1440 |
if (IS_ERR(tmp)) { |
| 1441 |
error = PTR_ERR(tmp); |
| 1442 |
break; |
| 1443 |
} |
| 1444 |
|
| 1445 |
iput(dir); |
| 1446 |
dir = tmp; |
| 1447 |
} |
| 1448 |
|
| 1449 |
iput(dir); |
| 1450 |
|
| 1451 |
return error; |
| 1452 |
} |
| 1453 |
|
| 1454 |
/** |
| 1455 |
* update_moved_ino - Update an inode that's being moved |
| 1456 |
* @ip: The inode being moved |
| 1457 |
* @ndip: The parent directory of the new filename |
| 1458 |
* @dir_rename: True of ip is a directory |
| 1459 |
* |
| 1460 |
* Returns: errno |
| 1461 |
*/ |
| 1462 |
|
| 1463 |
static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip, |
| 1464 |
int dir_rename) |
| 1465 |
{ |
| 1466 |
if (dir_rename) |
| 1467 |
return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR); |
| 1468 |
|
| 1469 |
inode_set_ctime_current(&ip->i_inode); |
| 1470 |
mark_inode_dirty_sync(&ip->i_inode); |
| 1471 |
return 0; |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
/** |
| 1476 |
* gfs2_rename - Rename a file |
| 1477 |
* @odir: Parent directory of old file name |
| 1478 |
* @odentry: The old dentry of the file |
| 1479 |
* @ndir: Parent directory of new file name |
| 1480 |
* @ndentry: The new dentry of the file |
| 1481 |
* |
| 1482 |
* Returns: errno |
| 1483 |
*/ |
| 1484 |
|
| 1485 |
static int gfs2_rename(struct inode *odir, struct dentry *odentry, |
| 1486 |
struct inode *ndir, struct dentry *ndentry) |
| 1487 |
{ |
| 1488 |
struct gfs2_inode *odip = GFS2_I(odir); |
| 1489 |
struct gfs2_inode *ndip = GFS2_I(ndir); |
| 1490 |
struct gfs2_inode *ip = GFS2_I(d_inode(odentry)); |
| 1491 |
struct gfs2_inode *nip = NULL; |
| 1492 |
struct gfs2_sbd *sdp = GFS2_SB(odir); |
| 1493 |
struct gfs2_holder ghs[4], r_gh, rd_gh; |
| 1494 |
struct gfs2_rgrpd *nrgd; |
| 1495 |
unsigned int num_gh; |
| 1496 |
int dir_rename = 0; |
| 1497 |
struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, }; |
| 1498 |
unsigned int retries = 0, x; |
| 1499 |
int error; |
| 1500 |
|
| 1501 |
gfs2_holder_mark_uninitialized(&r_gh); |
| 1502 |
gfs2_holder_mark_uninitialized(&rd_gh); |
| 1503 |
if (d_really_is_positive(ndentry)) { |
| 1504 |
nip = GFS2_I(d_inode(ndentry)); |
| 1505 |
if (ip == nip) |
| 1506 |
return 0; |
| 1507 |
} |
| 1508 |
|
| 1509 |
error = gfs2_rindex_update(sdp); |
| 1510 |
if (error) |
| 1511 |
return error; |
| 1512 |
|
| 1513 |
error = gfs2_qa_get(ndip); |
| 1514 |
if (error) |
| 1515 |
return error; |
| 1516 |
|
| 1517 |
if (odip != ndip) { |
| 1518 |
error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, |
| 1519 |
0, &r_gh); |
| 1520 |
if (error) |
| 1521 |
goto out; |
| 1522 |
|
| 1523 |
if (S_ISDIR(ip->i_inode.i_mode)) { |
| 1524 |
dir_rename = 1; |
| 1525 |
/* don't move a directory into its subdir */ |
| 1526 |
error = gfs2_ok_to_move(ip, ndip); |
| 1527 |
if (error) |
| 1528 |
goto out_gunlock_r; |
| 1529 |
} |
| 1530 |
} |
| 1531 |
|
| 1532 |
num_gh = 1; |
| 1533 |
gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs); |
| 1534 |
if (odip != ndip) { |
| 1535 |
gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC, |
| 1536 |
ghs + num_gh); |
| 1537 |
num_gh++; |
| 1538 |
} |
| 1539 |
gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh); |
| 1540 |
num_gh++; |
| 1541 |
|
| 1542 |
if (nip) { |
| 1543 |
gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, |
| 1544 |
ghs + num_gh); |
| 1545 |
num_gh++; |
| 1546 |
} |
| 1547 |
|
| 1548 |
again: |
| 1549 |
for (x = 0; x < num_gh; x++) { |
| 1550 |
error = gfs2_glock_nq(ghs + x); |
| 1551 |
if (error) |
| 1552 |
goto out_gunlock; |
| 1553 |
} |
| 1554 |
error = gfs2_glock_async_wait(num_gh, ghs, retries); |
| 1555 |
if (error == -ESTALE) { |
| 1556 |
retries++; |
| 1557 |
goto again; |
| 1558 |
} |
| 1559 |
if (error) |
| 1560 |
goto out_gunlock; |
| 1561 |
|
| 1562 |
if (nip) { |
| 1563 |
/* Grab the resource group glock for unlink flag twiddling. |
| 1564 |
* This is the case where the target dinode already exists |
| 1565 |
* so we unlink before doing the rename. |
| 1566 |
*/ |
| 1567 |
nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1); |
| 1568 |
if (!nrgd) { |
| 1569 |
error = -ENOENT; |
| 1570 |
goto out_gunlock; |
| 1571 |
} |
| 1572 |
error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, |
| 1573 |
LM_FLAG_NODE_SCOPE, &rd_gh); |
| 1574 |
if (error) |
| 1575 |
goto out_gunlock; |
| 1576 |
} |
| 1577 |
|
| 1578 |
error = -ENOENT; |
| 1579 |
if (ip->i_inode.i_nlink == 0) |
| 1580 |
goto out_gunlock; |
| 1581 |
|
| 1582 |
/* Check out the old directory */ |
| 1583 |
|
| 1584 |
error = gfs2_unlink_ok(odip, &odentry->d_name, ip); |
| 1585 |
if (error) |
| 1586 |
goto out_gunlock; |
| 1587 |
|
| 1588 |
/* Check out the new directory */ |
| 1589 |
|
| 1590 |
if (nip) { |
| 1591 |
error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip); |
| 1592 |
if (error) |
| 1593 |
goto out_gunlock; |
| 1594 |
|
| 1595 |
if (nip->i_inode.i_nlink == 0) { |
| 1596 |
error = -EAGAIN; |
| 1597 |
goto out_gunlock; |
| 1598 |
} |
| 1599 |
|
| 1600 |
if (S_ISDIR(nip->i_inode.i_mode)) { |
| 1601 |
if (nip->i_entries < 2) { |
| 1602 |
gfs2_consist_inode(nip); |
| 1603 |
error = -EIO; |
| 1604 |
goto out_gunlock; |
| 1605 |
} |
| 1606 |
if (nip->i_entries > 2) { |
| 1607 |
error = -ENOTEMPTY; |
| 1608 |
goto out_gunlock; |
| 1609 |
} |
| 1610 |
} |
| 1611 |
} else { |
| 1612 |
error = gfs2_permission(&nop_mnt_idmap, ndir, |
| 1613 |
MAY_WRITE | MAY_EXEC); |
| 1614 |
if (error) |
| 1615 |
goto out_gunlock; |
| 1616 |
|
| 1617 |
error = gfs2_dir_check(ndir, &ndentry->d_name, NULL); |
| 1618 |
switch (error) { |
| 1619 |
case -ENOENT: |
| 1620 |
error = 0; |
| 1621 |
break; |
| 1622 |
case 0: |
| 1623 |
error = -EEXIST; |
| 1624 |
goto out_gunlock; |
| 1625 |
default: |
| 1626 |
goto out_gunlock; |
| 1627 |
} |
| 1628 |
|
| 1629 |
if (odip != ndip) { |
| 1630 |
if (!ndip->i_inode.i_nlink) { |
| 1631 |
error = -ENOENT; |
| 1632 |
goto out_gunlock; |
| 1633 |
} |
| 1634 |
if (ndip->i_entries == (u32)-1) { |
| 1635 |
error = -EFBIG; |
| 1636 |
goto out_gunlock; |
| 1637 |
} |
| 1638 |
if (S_ISDIR(ip->i_inode.i_mode) && |
| 1639 |
ndip->i_inode.i_nlink == (u32)-1) { |
| 1640 |
error = -EMLINK; |
| 1641 |
goto out_gunlock; |
| 1642 |
} |
| 1643 |
} |
| 1644 |
} |
| 1645 |
|
| 1646 |
/* Check out the dir to be renamed */ |
| 1647 |
|
| 1648 |
if (dir_rename) { |
| 1649 |
error = gfs2_permission(&nop_mnt_idmap, d_inode(odentry), |
| 1650 |
MAY_WRITE); |
| 1651 |
if (error) |
| 1652 |
goto out_gunlock; |
| 1653 |
} |
| 1654 |
|
| 1655 |
if (nip == NULL) { |
| 1656 |
error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da); |
| 1657 |
if (error) |
| 1658 |
goto out_gunlock; |
| 1659 |
} |
| 1660 |
|
| 1661 |
if (da.nr_blocks) { |
| 1662 |
struct gfs2_alloc_parms ap = { .target = da.nr_blocks, }; |
| 1663 |
error = gfs2_quota_lock_check(ndip, &ap); |
| 1664 |
if (error) |
| 1665 |
goto out_gunlock; |
| 1666 |
|
| 1667 |
error = gfs2_inplace_reserve(ndip, &ap); |
| 1668 |
if (error) |
| 1669 |
goto out_gunlock_q; |
| 1670 |
|
| 1671 |
error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) + |
| 1672 |
4 * RES_LEAF + 4, 0); |
| 1673 |
if (error) |
| 1674 |
goto out_ipreserv; |
| 1675 |
} else { |
| 1676 |
error = gfs2_trans_begin(sdp, 4 * RES_DINODE + |
| 1677 |
5 * RES_LEAF + 4, 0); |
| 1678 |
if (error) |
| 1679 |
goto out_gunlock; |
| 1680 |
} |
| 1681 |
|
| 1682 |
/* Remove the target file, if it exists */ |
| 1683 |
|
| 1684 |
if (nip) |
| 1685 |
error = gfs2_unlink_inode(ndip, ndentry); |
| 1686 |
|
| 1687 |
error = update_moved_ino(ip, ndip, dir_rename); |
| 1688 |
if (error) |
| 1689 |
goto out_end_trans; |
| 1690 |
|
| 1691 |
error = gfs2_dir_del(odip, odentry); |
| 1692 |
if (error) |
| 1693 |
goto out_end_trans; |
| 1694 |
|
| 1695 |
error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da); |
| 1696 |
if (error) |
| 1697 |
goto out_end_trans; |
| 1698 |
|
| 1699 |
out_end_trans: |
| 1700 |
gfs2_trans_end(sdp); |
| 1701 |
out_ipreserv: |
| 1702 |
if (da.nr_blocks) |
| 1703 |
gfs2_inplace_release(ndip); |
| 1704 |
out_gunlock_q: |
| 1705 |
if (da.nr_blocks) |
| 1706 |
gfs2_quota_unlock(ndip); |
| 1707 |
out_gunlock: |
| 1708 |
gfs2_dir_no_add(&da); |
| 1709 |
if (gfs2_holder_initialized(&rd_gh)) |
| 1710 |
gfs2_glock_dq_uninit(&rd_gh); |
| 1711 |
|
| 1712 |
while (x--) { |
| 1713 |
if (gfs2_holder_queued(ghs + x)) |
| 1714 |
gfs2_glock_dq(ghs + x); |
| 1715 |
gfs2_holder_uninit(ghs + x); |
| 1716 |
} |
| 1717 |
out_gunlock_r: |
| 1718 |
if (gfs2_holder_initialized(&r_gh)) |
| 1719 |
gfs2_glock_dq_uninit(&r_gh); |
| 1720 |
out: |
| 1721 |
gfs2_qa_put(ndip); |
| 1722 |
return error; |
| 1723 |
} |
| 1724 |
|
| 1725 |
/** |
| 1726 |
* gfs2_exchange - exchange two files |
| 1727 |
* @odir: Parent directory of old file name |
| 1728 |
* @odentry: The old dentry of the file |
| 1729 |
* @ndir: Parent directory of new file name |
| 1730 |
* @ndentry: The new dentry of the file |
| 1731 |
* @flags: The rename flags |
| 1732 |
* |
| 1733 |
* Returns: errno |
| 1734 |
*/ |
| 1735 |
|
| 1736 |
static int gfs2_exchange(struct inode *odir, struct dentry *odentry, |
| 1737 |
struct inode *ndir, struct dentry *ndentry, |
| 1738 |
unsigned int flags) |
| 1739 |
{ |
| 1740 |
struct gfs2_inode *odip = GFS2_I(odir); |
| 1741 |
struct gfs2_inode *ndip = GFS2_I(ndir); |
| 1742 |
struct gfs2_inode *oip = GFS2_I(odentry->d_inode); |
| 1743 |
struct gfs2_inode *nip = GFS2_I(ndentry->d_inode); |
| 1744 |
struct gfs2_sbd *sdp = GFS2_SB(odir); |
| 1745 |
struct gfs2_holder ghs[4], r_gh; |
| 1746 |
unsigned int num_gh; |
| 1747 |
unsigned int retries = 0, x; |
| 1748 |
umode_t old_mode = oip->i_inode.i_mode; |
| 1749 |
umode_t new_mode = nip->i_inode.i_mode; |
| 1750 |
int error; |
| 1751 |
|
| 1752 |
gfs2_holder_mark_uninitialized(&r_gh); |
| 1753 |
error = gfs2_rindex_update(sdp); |
| 1754 |
if (error) |
| 1755 |
return error; |
| 1756 |
|
| 1757 |
if (odip != ndip) { |
| 1758 |
error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, |
| 1759 |
0, &r_gh); |
| 1760 |
if (error) |
| 1761 |
goto out; |
| 1762 |
|
| 1763 |
if (S_ISDIR(old_mode)) { |
| 1764 |
/* don't move a directory into its subdir */ |
| 1765 |
error = gfs2_ok_to_move(oip, ndip); |
| 1766 |
if (error) |
| 1767 |
goto out_gunlock_r; |
| 1768 |
} |
| 1769 |
|
| 1770 |
if (S_ISDIR(new_mode)) { |
| 1771 |
/* don't move a directory into its subdir */ |
| 1772 |
error = gfs2_ok_to_move(nip, odip); |
| 1773 |
if (error) |
| 1774 |
goto out_gunlock_r; |
| 1775 |
} |
| 1776 |
} |
| 1777 |
|
| 1778 |
num_gh = 1; |
| 1779 |
gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs); |
| 1780 |
if (odip != ndip) { |
| 1781 |
gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, |
| 1782 |
ghs + num_gh); |
| 1783 |
num_gh++; |
| 1784 |
} |
| 1785 |
gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh); |
| 1786 |
num_gh++; |
| 1787 |
|
| 1788 |
gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh); |
| 1789 |
num_gh++; |
| 1790 |
|
| 1791 |
again: |
| 1792 |
for (x = 0; x < num_gh; x++) { |
| 1793 |
error = gfs2_glock_nq(ghs + x); |
| 1794 |
if (error) |
| 1795 |
goto out_gunlock; |
| 1796 |
} |
| 1797 |
|
| 1798 |
error = gfs2_glock_async_wait(num_gh, ghs, retries); |
| 1799 |
if (error == -ESTALE) { |
| 1800 |
retries++; |
| 1801 |
goto again; |
| 1802 |
} |
| 1803 |
if (error) |
| 1804 |
goto out_gunlock; |
| 1805 |
|
| 1806 |
error = -ENOENT; |
| 1807 |
if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0) |
| 1808 |
goto out_gunlock; |
| 1809 |
|
| 1810 |
error = gfs2_unlink_ok(odip, &odentry->d_name, oip); |
| 1811 |
if (error) |
| 1812 |
goto out_gunlock; |
| 1813 |
error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip); |
| 1814 |
if (error) |
| 1815 |
goto out_gunlock; |
| 1816 |
|
| 1817 |
if (S_ISDIR(old_mode)) { |
| 1818 |
error = gfs2_permission(&nop_mnt_idmap, odentry->d_inode, |
| 1819 |
MAY_WRITE); |
| 1820 |
if (error) |
| 1821 |
goto out_gunlock; |
| 1822 |
} |
| 1823 |
if (S_ISDIR(new_mode)) { |
| 1824 |
error = gfs2_permission(&nop_mnt_idmap, ndentry->d_inode, |
| 1825 |
MAY_WRITE); |
| 1826 |
if (error) |
| 1827 |
goto out_gunlock; |
| 1828 |
} |
| 1829 |
error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0); |
| 1830 |
if (error) |
| 1831 |
goto out_gunlock; |
| 1832 |
|
| 1833 |
error = update_moved_ino(oip, ndip, S_ISDIR(old_mode)); |
| 1834 |
if (error) |
| 1835 |
goto out_end_trans; |
| 1836 |
|
| 1837 |
error = update_moved_ino(nip, odip, S_ISDIR(new_mode)); |
| 1838 |
if (error) |
| 1839 |
goto out_end_trans; |
| 1840 |
|
| 1841 |
error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip, |
| 1842 |
IF2DT(old_mode)); |
| 1843 |
if (error) |
| 1844 |
goto out_end_trans; |
| 1845 |
|
| 1846 |
error = gfs2_dir_mvino(odip, &odentry->d_name, nip, |
| 1847 |
IF2DT(new_mode)); |
| 1848 |
if (error) |
| 1849 |
goto out_end_trans; |
| 1850 |
|
| 1851 |
if (odip != ndip) { |
| 1852 |
if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) { |
| 1853 |
inc_nlink(&odip->i_inode); |
| 1854 |
drop_nlink(&ndip->i_inode); |
| 1855 |
} else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) { |
| 1856 |
inc_nlink(&ndip->i_inode); |
| 1857 |
drop_nlink(&odip->i_inode); |
| 1858 |
} |
| 1859 |
} |
| 1860 |
mark_inode_dirty(&ndip->i_inode); |
| 1861 |
if (odip != ndip) |
| 1862 |
mark_inode_dirty(&odip->i_inode); |
| 1863 |
|
| 1864 |
out_end_trans: |
| 1865 |
gfs2_trans_end(sdp); |
| 1866 |
out_gunlock: |
| 1867 |
while (x--) { |
| 1868 |
if (gfs2_holder_queued(ghs + x)) |
| 1869 |
gfs2_glock_dq(ghs + x); |
| 1870 |
gfs2_holder_uninit(ghs + x); |
| 1871 |
} |
| 1872 |
out_gunlock_r: |
| 1873 |
if (gfs2_holder_initialized(&r_gh)) |
| 1874 |
gfs2_glock_dq_uninit(&r_gh); |
| 1875 |
out: |
| 1876 |
return error; |
| 1877 |
} |
| 1878 |
|
| 1879 |
static int gfs2_rename2(struct mnt_idmap *idmap, struct inode *odir, |
| 1880 |
struct dentry *odentry, struct inode *ndir, |
| 1881 |
struct dentry *ndentry, unsigned int flags) |
| 1882 |
{ |
| 1883 |
flags &= ~RENAME_NOREPLACE; |
| 1884 |
|
| 1885 |
if (flags & ~RENAME_EXCHANGE) |
| 1886 |
return -EINVAL; |
| 1887 |
|
| 1888 |
if (flags & RENAME_EXCHANGE) |
| 1889 |
return gfs2_exchange(odir, odentry, ndir, ndentry, flags); |
| 1890 |
|
| 1891 |
return gfs2_rename(odir, odentry, ndir, ndentry); |
| 1892 |
} |
| 1893 |
|
| 1894 |
/** |
| 1895 |
* gfs2_get_link - Follow a symbolic link |
| 1896 |
* @dentry: The dentry of the link |
| 1897 |
* @inode: The inode of the link |
| 1898 |
* @done: destructor for return value |
| 1899 |
* |
| 1900 |
* This can handle symlinks of any size. |
| 1901 |
* |
| 1902 |
* Returns: 0 on success or error code |
| 1903 |
*/ |
| 1904 |
|
| 1905 |
static const char *gfs2_get_link(struct dentry *dentry, |
| 1906 |
struct inode *inode, |
| 1907 |
struct delayed_call *done) |
| 1908 |
{ |
| 1909 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 1910 |
struct gfs2_holder i_gh; |
| 1911 |
struct buffer_head *dibh; |
| 1912 |
unsigned int size; |
| 1913 |
char *buf; |
| 1914 |
int error; |
| 1915 |
|
| 1916 |
if (!dentry) |
| 1917 |
return ERR_PTR(-ECHILD); |
| 1918 |
|
| 1919 |
gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh); |
| 1920 |
error = gfs2_glock_nq(&i_gh); |
| 1921 |
if (error) { |
| 1922 |
gfs2_holder_uninit(&i_gh); |
| 1923 |
return ERR_PTR(error); |
| 1924 |
} |
| 1925 |
|
| 1926 |
size = (unsigned int)i_size_read(&ip->i_inode); |
| 1927 |
if (size == 0) { |
| 1928 |
gfs2_consist_inode(ip); |
| 1929 |
buf = ERR_PTR(-EIO); |
| 1930 |
goto out; |
| 1931 |
} |
| 1932 |
|
| 1933 |
error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1934 |
if (error) { |
| 1935 |
buf = ERR_PTR(error); |
| 1936 |
goto out; |
| 1937 |
} |
| 1938 |
|
| 1939 |
buf = kzalloc(size + 1, GFP_NOFS); |
| 1940 |
if (!buf) |
| 1941 |
buf = ERR_PTR(-ENOMEM); |
| 1942 |
else |
| 1943 |
memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size); |
| 1944 |
brelse(dibh); |
| 1945 |
out: |
| 1946 |
gfs2_glock_dq_uninit(&i_gh); |
| 1947 |
if (!IS_ERR(buf)) |
| 1948 |
set_delayed_call(done, kfree_link, buf); |
| 1949 |
return buf; |
| 1950 |
} |
| 1951 |
|
| 1952 |
/** |
| 1953 |
* gfs2_permission |
| 1954 |
* @idmap: idmap of the mount the inode was found from |
| 1955 |
* @inode: The inode |
| 1956 |
* @mask: The mask to be tested |
| 1957 |
* |
| 1958 |
* This may be called from the VFS directly, or from within GFS2 with the |
| 1959 |
* inode locked, so we look to see if the glock is already locked and only |
| 1960 |
* lock the glock if its not already been done. |
| 1961 |
* |
| 1962 |
* Returns: errno |
| 1963 |
*/ |
| 1964 |
|
| 1965 |
int gfs2_permission(struct mnt_idmap *idmap, struct inode *inode, |
| 1966 |
int mask) |
| 1967 |
{ |
| 1968 |
int may_not_block = mask & MAY_NOT_BLOCK; |
| 1969 |
struct gfs2_inode *ip; |
| 1970 |
struct gfs2_holder i_gh; |
| 1971 |
struct gfs2_glock *gl; |
| 1972 |
int error; |
| 1973 |
|
| 1974 |
gfs2_holder_mark_uninitialized(&i_gh); |
| 1975 |
ip = GFS2_I(inode); |
| 1976 |
gl = rcu_dereference_check(ip->i_gl, !may_not_block); |
| 1977 |
if (unlikely(!gl)) { |
| 1978 |
/* inode is getting torn down, must be RCU mode */ |
| 1979 |
WARN_ON_ONCE(!may_not_block); |
| 1980 |
return -ECHILD; |
| 1981 |
} |
| 1982 |
if (gfs2_glock_is_locked_by_me(gl) == NULL) { |
| 1983 |
if (may_not_block) |
| 1984 |
return -ECHILD; |
| 1985 |
error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
| 1986 |
if (error) |
| 1987 |
return error; |
| 1988 |
} |
| 1989 |
|
| 1990 |
if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode)) |
| 1991 |
error = -EPERM; |
| 1992 |
else |
| 1993 |
error = generic_permission(&nop_mnt_idmap, inode, mask); |
| 1994 |
if (gfs2_holder_initialized(&i_gh)) |
| 1995 |
gfs2_glock_dq_uninit(&i_gh); |
| 1996 |
|
| 1997 |
return error; |
| 1998 |
} |
| 1999 |
|
| 2000 |
static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr) |
| 2001 |
{ |
| 2002 |
setattr_copy(&nop_mnt_idmap, inode, attr); |
| 2003 |
mark_inode_dirty(inode); |
| 2004 |
return 0; |
| 2005 |
} |
| 2006 |
|
| 2007 |
static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr) |
| 2008 |
{ |
| 2009 |
int error; |
| 2010 |
|
| 2011 |
if (current->journal_info) |
| 2012 |
return __gfs2_setattr_simple(inode, attr); |
| 2013 |
|
| 2014 |
error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0); |
| 2015 |
if (error) |
| 2016 |
return error; |
| 2017 |
|
| 2018 |
error = __gfs2_setattr_simple(inode, attr); |
| 2019 |
gfs2_trans_end(GFS2_SB(inode)); |
| 2020 |
return error; |
| 2021 |
} |
| 2022 |
|
| 2023 |
static int setattr_chown(struct inode *inode, struct iattr *attr) |
| 2024 |
{ |
| 2025 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 2026 |
struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 2027 |
kuid_t ouid, nuid; |
| 2028 |
kgid_t ogid, ngid; |
| 2029 |
int error; |
| 2030 |
struct gfs2_alloc_parms ap = {}; |
| 2031 |
|
| 2032 |
ouid = inode->i_uid; |
| 2033 |
ogid = inode->i_gid; |
| 2034 |
nuid = attr->ia_uid; |
| 2035 |
ngid = attr->ia_gid; |
| 2036 |
|
| 2037 |
if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid)) |
| 2038 |
ouid = nuid = NO_UID_QUOTA_CHANGE; |
| 2039 |
if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid)) |
| 2040 |
ogid = ngid = NO_GID_QUOTA_CHANGE; |
| 2041 |
error = gfs2_qa_get(ip); |
| 2042 |
if (error) |
| 2043 |
return error; |
| 2044 |
|
| 2045 |
error = gfs2_rindex_update(sdp); |
| 2046 |
if (error) |
| 2047 |
goto out; |
| 2048 |
|
| 2049 |
error = gfs2_quota_lock(ip, nuid, ngid); |
| 2050 |
if (error) |
| 2051 |
goto out; |
| 2052 |
|
| 2053 |
ap.target = gfs2_get_inode_blocks(&ip->i_inode); |
| 2054 |
|
| 2055 |
if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) || |
| 2056 |
!gid_eq(ogid, NO_GID_QUOTA_CHANGE)) { |
| 2057 |
error = gfs2_quota_check(ip, nuid, ngid, &ap); |
| 2058 |
if (error) |
| 2059 |
goto out_gunlock_q; |
| 2060 |
} |
| 2061 |
|
| 2062 |
error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0); |
| 2063 |
if (error) |
| 2064 |
goto out_gunlock_q; |
| 2065 |
|
| 2066 |
error = gfs2_setattr_simple(inode, attr); |
| 2067 |
if (error) |
| 2068 |
goto out_end_trans; |
| 2069 |
|
| 2070 |
if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) || |
| 2071 |
!gid_eq(ogid, NO_GID_QUOTA_CHANGE)) { |
| 2072 |
gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid); |
| 2073 |
gfs2_quota_change(ip, ap.target, nuid, ngid); |
| 2074 |
} |
| 2075 |
|
| 2076 |
out_end_trans: |
| 2077 |
gfs2_trans_end(sdp); |
| 2078 |
out_gunlock_q: |
| 2079 |
gfs2_quota_unlock(ip); |
| 2080 |
out: |
| 2081 |
gfs2_qa_put(ip); |
| 2082 |
return error; |
| 2083 |
} |
| 2084 |
|
| 2085 |
/** |
| 2086 |
* gfs2_setattr - Change attributes on an inode |
| 2087 |
* @idmap: idmap of the mount the inode was found from |
| 2088 |
* @dentry: The dentry which is changing |
| 2089 |
* @attr: The structure describing the change |
| 2090 |
* |
| 2091 |
* The VFS layer wants to change one or more of an inodes attributes. Write |
| 2092 |
* that change out to disk. |
| 2093 |
* |
| 2094 |
* Returns: errno |
| 2095 |
*/ |
| 2096 |
|
| 2097 |
static int gfs2_setattr(struct mnt_idmap *idmap, |
| 2098 |
struct dentry *dentry, struct iattr *attr) |
| 2099 |
{ |
| 2100 |
struct inode *inode = d_inode(dentry); |
| 2101 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 2102 |
struct gfs2_holder i_gh; |
| 2103 |
int error; |
| 2104 |
|
| 2105 |
error = gfs2_qa_get(ip); |
| 2106 |
if (error) |
| 2107 |
return error; |
| 2108 |
|
| 2109 |
error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
| 2110 |
if (error) |
| 2111 |
goto out; |
| 2112 |
|
| 2113 |
error = may_setattr(&nop_mnt_idmap, inode, attr->ia_valid); |
| 2114 |
if (error) |
| 2115 |
goto error; |
| 2116 |
|
| 2117 |
error = setattr_prepare(&nop_mnt_idmap, dentry, attr); |
| 2118 |
if (error) |
| 2119 |
goto error; |
| 2120 |
|
| 2121 |
if (attr->ia_valid & ATTR_SIZE) |
| 2122 |
error = gfs2_setattr_size(inode, attr->ia_size); |
| 2123 |
else if (attr->ia_valid & (ATTR_UID | ATTR_GID)) |
| 2124 |
error = setattr_chown(inode, attr); |
| 2125 |
else { |
| 2126 |
error = gfs2_setattr_simple(inode, attr); |
| 2127 |
if (!error && attr->ia_valid & ATTR_MODE) |
| 2128 |
error = posix_acl_chmod(&nop_mnt_idmap, dentry, |
| 2129 |
inode->i_mode); |
| 2130 |
} |
| 2131 |
|
| 2132 |
error: |
| 2133 |
if (!error) |
| 2134 |
mark_inode_dirty(inode); |
| 2135 |
gfs2_glock_dq_uninit(&i_gh); |
| 2136 |
out: |
| 2137 |
gfs2_qa_put(ip); |
| 2138 |
return error; |
| 2139 |
} |
| 2140 |
|
| 2141 |
/** |
| 2142 |
* gfs2_getattr - Read out an inode's attributes |
| 2143 |
* @idmap: idmap of the mount the inode was found from |
| 2144 |
* @path: Object to query |
| 2145 |
* @stat: The inode's stats |
| 2146 |
* @request_mask: Mask of STATX_xxx flags indicating the caller's interests |
| 2147 |
* @flags: AT_STATX_xxx setting |
| 2148 |
* |
| 2149 |
* This may be called from the VFS directly, or from within GFS2 with the |
| 2150 |
* inode locked, so we look to see if the glock is already locked and only |
| 2151 |
* lock the glock if its not already been done. Note that its the NFS |
| 2152 |
* readdirplus operation which causes this to be called (from filldir) |
| 2153 |
* with the glock already held. |
| 2154 |
* |
| 2155 |
* Returns: errno |
| 2156 |
*/ |
| 2157 |
|
| 2158 |
static int gfs2_getattr(struct mnt_idmap *idmap, |
| 2159 |
const struct path *path, struct kstat *stat, |
| 2160 |
u32 request_mask, unsigned int flags) |
| 2161 |
{ |
| 2162 |
struct inode *inode = d_inode(path->dentry); |
| 2163 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 2164 |
struct gfs2_holder gh; |
| 2165 |
u32 gfsflags; |
| 2166 |
int error; |
| 2167 |
|
| 2168 |
gfs2_holder_mark_uninitialized(&gh); |
| 2169 |
if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) { |
| 2170 |
error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
| 2171 |
if (error) |
| 2172 |
return error; |
| 2173 |
} |
| 2174 |
|
| 2175 |
gfsflags = ip->i_diskflags; |
| 2176 |
if (gfsflags & GFS2_DIF_APPENDONLY) |
| 2177 |
stat->attributes |= STATX_ATTR_APPEND; |
| 2178 |
if (gfsflags & GFS2_DIF_IMMUTABLE) |
| 2179 |
stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 2180 |
|
| 2181 |
stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 2182 |
STATX_ATTR_COMPRESSED | |
| 2183 |
STATX_ATTR_ENCRYPTED | |
| 2184 |
STATX_ATTR_IMMUTABLE | |
| 2185 |
STATX_ATTR_NODUMP); |
| 2186 |
|
| 2187 |
generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat); |
| 2188 |
|
| 2189 |
if (gfs2_holder_initialized(&gh)) |
| 2190 |
gfs2_glock_dq_uninit(&gh); |
| 2191 |
|
| 2192 |
return 0; |
| 2193 |
} |
| 2194 |
|
| 2195 |
static bool fault_in_fiemap(struct fiemap_extent_info *fi) |
| 2196 |
{ |
| 2197 |
struct fiemap_extent __user *dest = fi->fi_extents_start; |
| 2198 |
size_t size = sizeof(*dest) * fi->fi_extents_max; |
| 2199 |
|
| 2200 |
return fault_in_safe_writeable((char __user *)dest, size) == 0; |
| 2201 |
} |
| 2202 |
|
| 2203 |
static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 2204 |
u64 start, u64 len) |
| 2205 |
{ |
| 2206 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 2207 |
struct gfs2_holder gh; |
| 2208 |
int ret; |
| 2209 |
|
| 2210 |
inode_lock_shared(inode); |
| 2211 |
|
| 2212 |
retry: |
| 2213 |
ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); |
| 2214 |
if (ret) |
| 2215 |
goto out; |
| 2216 |
|
| 2217 |
pagefault_disable(); |
| 2218 |
ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops); |
| 2219 |
pagefault_enable(); |
| 2220 |
|
| 2221 |
gfs2_glock_dq_uninit(&gh); |
| 2222 |
|
| 2223 |
if (ret == -EFAULT && fault_in_fiemap(fieinfo)) { |
| 2224 |
fieinfo->fi_extents_mapped = 0; |
| 2225 |
goto retry; |
| 2226 |
} |
| 2227 |
|
| 2228 |
out: |
| 2229 |
inode_unlock_shared(inode); |
| 2230 |
return ret; |
| 2231 |
} |
| 2232 |
|
| 2233 |
loff_t gfs2_seek_data(struct file *file, loff_t offset) |
| 2234 |
{ |
| 2235 |
struct inode *inode = file->f_mapping->host; |
| 2236 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 2237 |
struct gfs2_holder gh; |
| 2238 |
loff_t ret; |
| 2239 |
|
| 2240 |
inode_lock_shared(inode); |
| 2241 |
ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); |
| 2242 |
if (!ret) |
| 2243 |
ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops); |
| 2244 |
gfs2_glock_dq_uninit(&gh); |
| 2245 |
inode_unlock_shared(inode); |
| 2246 |
|
| 2247 |
if (ret < 0) |
| 2248 |
return ret; |
| 2249 |
return vfs_setpos(file, ret, inode->i_sb->s_maxbytes); |
| 2250 |
} |
| 2251 |
|
| 2252 |
loff_t gfs2_seek_hole(struct file *file, loff_t offset) |
| 2253 |
{ |
| 2254 |
struct inode *inode = file->f_mapping->host; |
| 2255 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 2256 |
struct gfs2_holder gh; |
| 2257 |
loff_t ret; |
| 2258 |
|
| 2259 |
inode_lock_shared(inode); |
| 2260 |
ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); |
| 2261 |
if (!ret) |
| 2262 |
ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops); |
| 2263 |
gfs2_glock_dq_uninit(&gh); |
| 2264 |
inode_unlock_shared(inode); |
| 2265 |
|
| 2266 |
if (ret < 0) |
| 2267 |
return ret; |
| 2268 |
return vfs_setpos(file, ret, inode->i_sb->s_maxbytes); |
| 2269 |
} |
| 2270 |
|
| 2271 |
static int gfs2_update_time(struct inode *inode, enum fs_update_time type, |
| 2272 |
unsigned int flags) |
| 2273 |
{ |
| 2274 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 2275 |
struct gfs2_glock *gl = ip->i_gl; |
| 2276 |
struct gfs2_holder *gh; |
| 2277 |
int error; |
| 2278 |
|
| 2279 |
if (flags & IOCB_NOWAIT) |
| 2280 |
return -EAGAIN; |
| 2281 |
|
| 2282 |
gh = gfs2_glock_is_locked_by_me(gl); |
| 2283 |
if (gh && gl->gl_state != LM_ST_EXCLUSIVE) { |
| 2284 |
gfs2_glock_dq(gh); |
| 2285 |
gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, gh); |
| 2286 |
error = gfs2_glock_nq(gh); |
| 2287 |
if (error) |
| 2288 |
return error; |
| 2289 |
} |
| 2290 |
return generic_update_time(inode, type, flags); |
| 2291 |
} |
| 2292 |
|
| 2293 |
static const struct inode_operations gfs2_file_iops = { |
| 2294 |
.permission = gfs2_permission, |
| 2295 |
.setattr = gfs2_setattr, |
| 2296 |
.getattr = gfs2_getattr, |
| 2297 |
.listxattr = gfs2_listxattr, |
| 2298 |
.fiemap = gfs2_fiemap, |
| 2299 |
.get_inode_acl = gfs2_get_acl, |
| 2300 |
.set_acl = gfs2_set_acl, |
| 2301 |
.update_time = gfs2_update_time, |
| 2302 |
.fileattr_get = gfs2_fileattr_get, |
| 2303 |
.fileattr_set = gfs2_fileattr_set, |
| 2304 |
}; |
| 2305 |
|
| 2306 |
static const struct inode_operations gfs2_dir_iops = { |
| 2307 |
.create = gfs2_create, |
| 2308 |
.lookup = gfs2_lookup, |
| 2309 |
.link = gfs2_link, |
| 2310 |
.unlink = gfs2_unlink, |
| 2311 |
.symlink = gfs2_symlink, |
| 2312 |
.mkdir = gfs2_mkdir, |
| 2313 |
.rmdir = gfs2_unlink, |
| 2314 |
.mknod = gfs2_mknod, |
| 2315 |
.rename = gfs2_rename2, |
| 2316 |
.permission = gfs2_permission, |
| 2317 |
.setattr = gfs2_setattr, |
| 2318 |
.getattr = gfs2_getattr, |
| 2319 |
.listxattr = gfs2_listxattr, |
| 2320 |
.fiemap = gfs2_fiemap, |
| 2321 |
.get_inode_acl = gfs2_get_acl, |
| 2322 |
.set_acl = gfs2_set_acl, |
| 2323 |
.update_time = gfs2_update_time, |
| 2324 |
.atomic_open = gfs2_atomic_open, |
| 2325 |
.fileattr_get = gfs2_fileattr_get, |
| 2326 |
.fileattr_set = gfs2_fileattr_set, |
| 2327 |
}; |
| 2328 |
|
| 2329 |
static const struct inode_operations gfs2_symlink_iops = { |
| 2330 |
.get_link = gfs2_get_link, |
| 2331 |
.permission = gfs2_permission, |
| 2332 |
.setattr = gfs2_setattr, |
| 2333 |
.getattr = gfs2_getattr, |
| 2334 |
.listxattr = gfs2_listxattr, |
| 2335 |
.fiemap = gfs2_fiemap, |
| 2336 |
}; |
| 2337 |
|