inode.c 18.8 KB
Newer Older
D
David Teigland 已提交
1 2
/*
 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
B
Bob Peterson 已提交
3
 * Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
D
David Teigland 已提交
4 5 6
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
7
 * of the GNU General Public License version 2.
D
David Teigland 已提交
8 9 10 11 12 13 14 15 16
 */

#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/buffer_head.h>
#include <linux/posix_acl.h>
#include <linux/sort.h>
17
#include <linux/gfs2_ondisk.h>
18
#include <linux/crc32.h>
19
#include <linux/security.h>
20
#include <linux/time.h>
D
David Teigland 已提交
21 22

#include "gfs2.h"
23
#include "incore.h"
D
David Teigland 已提交
24 25 26
#include "acl.h"
#include "bmap.h"
#include "dir.h"
27
#include "xattr.h"
D
David Teigland 已提交
28 29 30 31 32 33 34 35
#include "glock.h"
#include "glops.h"
#include "inode.h"
#include "log.h"
#include "meta_io.h"
#include "quota.h"
#include "rgrp.h"
#include "trans.h"
36
#include "util.h"
D
David Teigland 已提交
37

38 39 40 41 42 43
struct gfs2_skip_data {
	u64 no_addr;
	int skipped;
	int non_block;
};

44 45 46
static int iget_test(struct inode *inode, void *opaque)
{
	struct gfs2_inode *ip = GFS2_I(inode);
47
	struct gfs2_skip_data *data = opaque;
48

49 50 51 52 53 54
	if (ip->i_no_addr == data->no_addr) {
		if (data->non_block &&
		    inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
			data->skipped = 1;
			return 0;
		}
55
		return 1;
56
	}
57 58 59 60
	return 0;
}

static int iget_set(struct inode *inode, void *opaque)
D
David Teigland 已提交
61
{
62
	struct gfs2_inode *ip = GFS2_I(inode);
63
	struct gfs2_skip_data *data = opaque;
D
David Teigland 已提交
64

65 66 67 68
	if (data->skipped)
		return -ENOENT;
	inode->i_ino = (unsigned long)(data->no_addr);
	ip->i_no_addr = data->no_addr;
69 70
	return 0;
}
D
David Teigland 已提交
71

72
struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
73
{
74
	unsigned long hash = (unsigned long)no_addr;
75 76 77 78
	struct gfs2_skip_data data;

	data.no_addr = no_addr;
	data.skipped = 0;
79
	data.non_block = non_block;
80
	return ilookup5(sb, hash, iget_test, &data);
81
}
D
David Teigland 已提交
82

83 84
static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
			       int non_block)
85
{
86
	struct gfs2_skip_data data;
87
	unsigned long hash = (unsigned long)no_addr;
88 89 90 91 92

	data.no_addr = no_addr;
	data.skipped = 0;
	data.non_block = non_block;
	return iget5_locked(sb, hash, iget_test, iget_set, &data);
D
David Teigland 已提交
93 94
}

95
/**
96 97
 * gfs2_set_iop - Sets inode operations
 * @inode: The inode with correct i_mode filled in
98
 *
99 100 101
 * GFS2 lookup code fills in vfs inode contents based on info obtained
 * from directory entry inside gfs2_inode_lookup().
 */
102

103
static void gfs2_set_iop(struct inode *inode)
104
{
105
	struct gfs2_sbd *sdp = GFS2_SB(inode);
106 107 108 109
	umode_t mode = inode->i_mode;

	if (S_ISREG(mode)) {
		inode->i_op = &gfs2_file_iops;
110
		if (gfs2_localflocks(sdp))
111
			inode->i_fop = &gfs2_file_fops_nolock;
112
		else
113
			inode->i_fop = &gfs2_file_fops;
114 115
	} else if (S_ISDIR(mode)) {
		inode->i_op = &gfs2_dir_iops;
116
		if (gfs2_localflocks(sdp))
117
			inode->i_fop = &gfs2_dir_fops_nolock;
118
		else
119
			inode->i_fop = &gfs2_dir_fops;
120 121 122
	} else if (S_ISLNK(mode)) {
		inode->i_op = &gfs2_symlink_iops;
	} else {
D
Denis Cheng 已提交
123
		inode->i_op = &gfs2_file_iops;
D
Denis Cheng 已提交
124
		init_special_inode(inode, inode->i_mode, inode->i_rdev);
125 126 127
	}
}

D
David Teigland 已提交
128
/**
129 130
 * gfs2_inode_lookup - Lookup an inode
 * @sb: The super block
131
 * @no_addr: The inode number
132
 * @type: The type of the inode
133
 * non_block: Can we block on inodes that are being freed?
D
David Teigland 已提交
134
 *
135
 * Returns: A VFS inode, or an error
D
David Teigland 已提交
136 137
 */

138
struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
139
				u64 no_addr, u64 no_formal_ino, int non_block)
D
David Teigland 已提交
140
{
141 142
	struct inode *inode;
	struct gfs2_inode *ip;
143
	struct gfs2_glock *io_gl = NULL;
144
	int error;
D
David Teigland 已提交
145

146
	inode = gfs2_iget(sb, no_addr, non_block);
147 148
	ip = GFS2_I(inode);

S
Steven Whitehouse 已提交
149 150 151
	if (!inode)
		return ERR_PTR(-ENOBUFS);

152 153
	if (inode->i_state & I_NEW) {
		struct gfs2_sbd *sdp = GFS2_SB(inode);
154
		ip->i_no_formal_ino = no_formal_ino;
D
David Teigland 已提交
155

156
		error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
157 158 159
		if (unlikely(error))
			goto fail;
		ip->i_gl->gl_object = ip;
D
David Teigland 已提交
160

161
		error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
162 163
		if (unlikely(error))
			goto fail_put;
D
David Teigland 已提交
164

165
		set_bit(GIF_INVALID, &ip->i_flags);
166 167 168
		error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
		if (unlikely(error))
			goto fail_iopen;
D
David Teigland 已提交
169

170
		ip->i_iopen_gh.gh_gl->gl_object = ip;
171
		gfs2_glock_put(io_gl);
172
		io_gl = NULL;
173 174

		if (type == DT_UNKNOWN) {
175 176 177 178 179 180
			/* Inode glock must be locked already */
			error = gfs2_inode_refresh(GFS2_I(inode));
			if (error)
				goto fail_refresh;
		} else {
			inode->i_mode = DT2IF(type);
181 182
		}

183
		gfs2_set_iop(inode);
184
		unlock_new_inode(inode);
185
	}
D
David Teigland 已提交
186 187

	return inode;
188 189 190 191

fail_refresh:
	ip->i_iopen_gh.gh_gl->gl_object = NULL;
	gfs2_glock_dq_uninit(&ip->i_iopen_gh);
192
fail_iopen:
193 194
	if (io_gl)
		gfs2_glock_put(io_gl);
195
fail_put:
196
	ip->i_gl->gl_object = NULL;
197 198
	gfs2_glock_put(ip->i_gl);
fail:
199
	iget_failed(inode);
200
	return ERR_PTR(error);
D
David Teigland 已提交
201 202
}

203 204
struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
				  u64 *no_formal_ino, unsigned int blktype)
B
Bob Peterson 已提交
205
{
206 207
	struct super_block *sb = sdp->sd_vfs;
	struct gfs2_holder i_gh;
208
	struct inode *inode = NULL;
209
	int error;
B
Bob Peterson 已提交
210

211
	/* Must not read in block until block type is verified */
212
	error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
213
				  LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
214 215
	if (error)
		return ERR_PTR(error);
B
Bob Peterson 已提交
216

217 218
	error = gfs2_check_blk_type(sdp, no_addr, blktype);
	if (error)
B
Bob Peterson 已提交
219 220
		goto fail;

221
	inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
222 223
	if (IS_ERR(inode))
		goto fail;
224

225 226 227 228 229
	/* Two extra checks for NFS only */
	if (no_formal_ino) {
		error = -ESTALE;
		if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
			goto fail_iput;
230

231 232 233
		error = -EIO;
		if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
			goto fail_iput;
234

235 236
		error = 0;
	}
B
Bob Peterson 已提交
237 238

fail:
239 240 241 242 243
	gfs2_glock_dq_uninit(&i_gh);
	return error ? ERR_PTR(error) : inode;
fail_iput:
	iput(inode);
	goto fail;
B
Bob Peterson 已提交
244 245
}

D
David Teigland 已提交
246

247 248 249
struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
{
	struct qstr qstr;
250
	struct inode *inode;
251
	gfs2_str2qstr(&qstr, name);
252
	inode = gfs2_lookupi(dip, &qstr, 1);
253 254 255 256 257 258 259 260 261
	/* gfs2_lookupi has inconsistent callers: vfs
	 * related routines expect NULL for no entry found,
	 * gfs2_lookup_simple callers expect ENOENT
	 * and do not check for NULL.
	 */
	if (inode == NULL)
		return ERR_PTR(-ENOENT);
	else
		return inode;
262 263 264
}


D
David Teigland 已提交
265 266 267 268 269 270 271
/**
 * gfs2_lookupi - Look up a filename in a directory and return its inode
 * @d_gh: An initialized holder for the directory glock
 * @name: The name of the inode to look for
 * @is_root: If 1, ignore the caller's permissions
 * @i_gh: An uninitialized holder for the new inode glock
 *
272 273 274 275
 * This can be called via the VFS filldir function when NFS is doing
 * a readdirplus and the inode which its intending to stat isn't
 * already in cache. In this case we must not take the directory glock
 * again, since the readdir call will have already taken that lock.
D
David Teigland 已提交
276 277 278 279
 *
 * Returns: errno
 */

280
struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
281
			   int is_root)
D
David Teigland 已提交
282
{
S
Steven Whitehouse 已提交
283
	struct super_block *sb = dir->i_sb;
284
	struct gfs2_inode *dip = GFS2_I(dir);
D
David Teigland 已提交
285
	struct gfs2_holder d_gh;
286
	int error = 0;
287
	struct inode *inode = NULL;
288
	int unlock = 0;
D
David Teigland 已提交
289 290

	if (!name->len || name->len > GFS2_FNAMESIZE)
291
		return ERR_PTR(-ENAMETOOLONG);
D
David Teigland 已提交
292

293 294 295
	if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
	    (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
	     dir == sb->s_root->d_inode)) {
296 297
		igrab(dir);
		return dir;
D
David Teigland 已提交
298 299
	}

300
	if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
301 302 303 304 305
		error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
		if (error)
			return ERR_PTR(error);
		unlock = 1;
	}
D
David Teigland 已提交
306 307

	if (!is_root) {
308
		error = gfs2_permission(dir, MAY_EXEC, 0);
D
David Teigland 已提交
309 310 311 312
		if (error)
			goto out;
	}

313 314 315
	inode = gfs2_dir_search(dir, name);
	if (IS_ERR(inode))
		error = PTR_ERR(inode);
316
out:
317 318
	if (unlock)
		gfs2_glock_dq_uninit(&d_gh);
319 320
	if (error == -ENOENT)
		return NULL;
321
	return inode ? inode : ERR_PTR(error);
D
David Teigland 已提交
322 323 324 325 326 327 328 329 330 331 332
}

/**
 * create_ok - OK to create a new on-disk inode here?
 * @dip:  Directory in which dinode is to be created
 * @name:  Name of new dinode
 * @mode:
 *
 * Returns: errno
 */

333
static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
D
David Teigland 已提交
334 335 336 337
		     unsigned int mode)
{
	int error;

338
	error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
D
David Teigland 已提交
339 340 341 342
	if (error)
		return error;

	/*  Don't create entries in an unlinked directory  */
343
	if (!dip->i_inode.i_nlink)
344
		return -ENOENT;
D
David Teigland 已提交
345

346
	error = gfs2_dir_check(&dip->i_inode, name, NULL);
D
David Teigland 已提交
347 348 349 350 351 352 353 354 355 356
	switch (error) {
	case -ENOENT:
		error = 0;
		break;
	case 0:
		return -EEXIST;
	default:
		return error;
	}

357
	if (dip->i_entries == (u32)-1)
D
David Teigland 已提交
358
		return -EFBIG;
359
	if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
D
David Teigland 已提交
360 361 362 363 364 365 366 367
		return -EMLINK;

	return 0;
}

static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
			       unsigned int *uid, unsigned int *gid)
{
368
	if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
369
	    (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
D
David Teigland 已提交
370 371
		if (S_ISDIR(*mode))
			*mode |= S_ISUID;
372
		else if (dip->i_inode.i_uid != current_fsuid())
D
David Teigland 已提交
373
			*mode &= ~07111;
374
		*uid = dip->i_inode.i_uid;
D
David Teigland 已提交
375
	} else
376
		*uid = current_fsuid();
D
David Teigland 已提交
377

378
	if (dip->i_inode.i_mode & S_ISGID) {
D
David Teigland 已提交
379 380
		if (S_ISDIR(*mode))
			*mode |= S_ISGID;
381
		*gid = dip->i_inode.i_gid;
D
David Teigland 已提交
382
	} else
383
		*gid = current_fsgid();
D
David Teigland 已提交
384 385
}

386
static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
D
David Teigland 已提交
387
{
388
	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
D
David Teigland 已提交
389 390
	int error;

391 392
	if (gfs2_alloc_get(dip) == NULL)
		return -ENOMEM;
D
David Teigland 已提交
393

394
	dip->i_alloc->al_requested = RES_DINODE;
D
David Teigland 已提交
395 396 397 398
	error = gfs2_inplace_reserve(dip);
	if (error)
		goto out;

399
	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
D
David Teigland 已提交
400 401 402
	if (error)
		goto out_ipreserv;

403
	error = gfs2_alloc_di(dip, no_addr, generation);
D
David Teigland 已提交
404 405 406

	gfs2_trans_end(sdp);

S
Steven Whitehouse 已提交
407
out_ipreserv:
D
David Teigland 已提交
408
	gfs2_inplace_release(dip);
S
Steven Whitehouse 已提交
409
out:
D
David Teigland 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	gfs2_alloc_put(dip);
	return error;
}

/**
 * init_dinode - Fill in a new dinode structure
 * @dip: the directory this inode is being created in
 * @gl: The glock covering the new inode
 * @inum: the inode number
 * @mode: the file permissions
 * @uid:
 * @gid:
 *
 */

static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
A
Al Viro 已提交
426
			const struct gfs2_inum_host *inum, unsigned int mode,
S
Steven Whitehouse 已提交
427
			unsigned int uid, unsigned int gid,
428
			const u64 *generation, dev_t dev, struct buffer_head **bhp)
D
David Teigland 已提交
429
{
430
	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
431
	struct gfs2_dinode *di;
D
David Teigland 已提交
432
	struct buffer_head *dibh;
433
	struct timespec tv = CURRENT_TIME;
D
David Teigland 已提交
434 435

	dibh = gfs2_meta_new(gl, inum->no_addr);
436
	gfs2_trans_add_bh(gl, dibh, 1);
D
David Teigland 已提交
437 438
	gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
	gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
439 440
	di = (struct gfs2_dinode *)dibh->b_data;

441 442
	di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
	di->di_num.no_addr = cpu_to_be64(inum->no_addr);
443 444 445
	di->di_mode = cpu_to_be32(mode);
	di->di_uid = cpu_to_be32(uid);
	di->di_gid = cpu_to_be32(gid);
446 447
	di->di_nlink = 0;
	di->di_size = 0;
448
	di->di_blocks = cpu_to_be64(1);
449
	di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
450 451
	di->di_major = cpu_to_be32(MAJOR(dev));
	di->di_minor = cpu_to_be32(MINOR(dev));
452
	di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
S
Steven Whitehouse 已提交
453
	di->di_generation = cpu_to_be64(*generation);
454
	di->di_flags = 0;
D
David Teigland 已提交
455 456

	if (S_ISREG(mode)) {
457
		if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
D
David Teigland 已提交
458
		    gfs2_tune_get(sdp, gt_new_files_jdata))
459
			di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
D
David Teigland 已提交
460
	} else if (S_ISDIR(mode)) {
461
		di->di_flags |= cpu_to_be32(dip->i_diskflags &
462
					    GFS2_DIF_INHERIT_JDATA);
D
David Teigland 已提交
463 464
	}

465
	di->__pad1 = 0;
466
	di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
467
	di->di_height = 0;
468 469
	di->__pad2 = 0;
	di->__pad3 = 0;
470 471
	di->di_depth = 0;
	di->di_entries = 0;
472
	memset(&di->__pad4, 0, sizeof(di->__pad4));
473
	di->di_eattr = 0;
474 475 476
	di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
	di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
	di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
477
	memset(&di->di_reserved, 0, sizeof(di->di_reserved));
478 479
	
	set_buffer_uptodate(dibh);
480

481
	*bhp = dibh;
D
David Teigland 已提交
482 483 484
}

static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
A
Al Viro 已提交
485
		       unsigned int mode, const struct gfs2_inum_host *inum,
486
		       const u64 *generation, dev_t dev, struct buffer_head **bhp)
D
David Teigland 已提交
487
{
488
	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
D
David Teigland 已提交
489 490 491 492
	unsigned int uid, gid;
	int error;

	munge_mode_uid_gid(dip, &mode, &uid, &gid);
493 494
	if (!gfs2_alloc_get(dip))
		return -ENOMEM;
D
David Teigland 已提交
495 496 497 498 499 500 501 502 503

	error = gfs2_quota_lock(dip, uid, gid);
	if (error)
		goto out;

	error = gfs2_quota_check(dip, uid, gid);
	if (error)
		goto out_quota;

504
	error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
D
David Teigland 已提交
505 506 507
	if (error)
		goto out_quota;

508
	init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
D
David Teigland 已提交
509 510 511
	gfs2_quota_change(dip, +1, uid, gid);
	gfs2_trans_end(sdp);

512
out_quota:
D
David Teigland 已提交
513
	gfs2_quota_unlock(dip);
514
out:
D
David Teigland 已提交
515 516 517 518
	gfs2_alloc_put(dip);
	return error;
}

519 520
static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
		       struct gfs2_inode *ip)
D
David Teigland 已提交
521
{
522
	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
D
David Teigland 已提交
523 524 525 526 527 528
	struct gfs2_alloc *al;
	int alloc_required;
	struct buffer_head *dibh;
	int error;

	al = gfs2_alloc_get(dip);
529 530
	if (!al)
		return -ENOMEM;
D
David Teigland 已提交
531 532 533 534 535

	error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
	if (error)
		goto fail;

536
	error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
537
	if (alloc_required < 0)
B
Bob Peterson 已提交
538
		goto fail_quota_locks;
D
David Teigland 已提交
539
	if (alloc_required) {
540
		error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
D
David Teigland 已提交
541 542 543 544 545 546 547 548 549
		if (error)
			goto fail_quota_locks;

		al->al_requested = sdp->sd_max_dirres;

		error = gfs2_inplace_reserve(dip);
		if (error)
			goto fail_quota_locks;

550
		error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
551
					 al->al_rgd->rd_length +
552
					 2 * RES_DINODE +
D
David Teigland 已提交
553 554 555 556
					 RES_STATFS + RES_QUOTA, 0);
		if (error)
			goto fail_ipreserv;
	} else {
557
		error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
D
David Teigland 已提交
558 559 560 561
		if (error)
			goto fail_quota_locks;
	}

562
	error = gfs2_dir_add(&dip->i_inode, name, ip);
D
David Teigland 已提交
563 564 565 566 567 568
	if (error)
		goto fail_end_trans;

	error = gfs2_meta_inode_buffer(ip, &dibh);
	if (error)
		goto fail_end_trans;
569
	ip->i_inode.i_nlink = 1;
570
	gfs2_trans_add_bh(ip->i_gl, dibh, 1);
571
	gfs2_dinode_out(ip, dibh->b_data);
D
David Teigland 已提交
572 573 574
	brelse(dibh);
	return 0;

575
fail_end_trans:
D
David Teigland 已提交
576 577
	gfs2_trans_end(sdp);

578
fail_ipreserv:
579
	if (dip->i_alloc->al_rgd)
D
David Teigland 已提交
580 581
		gfs2_inplace_release(dip);

582
fail_quota_locks:
D
David Teigland 已提交
583 584
	gfs2_quota_unlock(dip);

585
fail:
D
David Teigland 已提交
586 587 588 589
	gfs2_alloc_put(dip);
	return error;
}

590 591
static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
			      const struct qstr *qstr)
592 593 594 595 596 597
{
	int err;
	size_t len;
	void *value;
	char *name;

598
	err = security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
599 600 601 602 603 604 605 606
					   &name, &value, &len);

	if (err) {
		if (err == -EOPNOTSUPP)
			return 0;
		return err;
	}

607 608
	err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
			       GFS2_EATYPE_SECURITY);
609 610 611 612 613 614
	kfree(value);
	kfree(name);

	return err;
}

D
David Teigland 已提交
615 616 617 618 619 620 621 622 623
/**
 * gfs2_createi - Create a new inode
 * @ghs: An array of two holders
 * @name: The name of the new file
 * @mode: the permissions on the new inode
 *
 * @ghs[0] is an initialized holder for the directory
 * @ghs[1] is the holder for the inode lock
 *
624
 * If the return value is not NULL, the glocks on both the directory and the new
D
David Teigland 已提交
625 626 627
 * file are held.  A transaction has been started and an inplace reservation
 * is held, as well.
 *
628
 * Returns: An inode
D
David Teigland 已提交
629 630
 */

631
struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
632
			   unsigned int mode, dev_t dev)
D
David Teigland 已提交
633
{
634
	struct inode *inode = NULL;
635
	struct gfs2_inode *dip = ghs->gh_gl->gl_object;
636 637
	struct inode *dir = &dip->i_inode;
	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
638
	struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
D
David Teigland 已提交
639
	int error;
S
Steven Whitehouse 已提交
640
	u64 generation;
641
	struct buffer_head *bh = NULL;
D
David Teigland 已提交
642 643

	if (!name->len || name->len > GFS2_FNAMESIZE)
644
		return ERR_PTR(-ENAMETOOLONG);
D
David Teigland 已提交
645 646 647 648 649 650 651 652 653 654

	gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
	error = gfs2_glock_nq(ghs);
	if (error)
		goto fail;

	error = create_ok(dip, name, mode);
	if (error)
		goto fail_gunlock;

655
	error = alloc_dinode(dip, &inum.no_addr, &generation);
D
David Teigland 已提交
656 657
	if (error)
		goto fail_gunlock;
658
	inum.no_formal_ino = generation;
D
David Teigland 已提交
659

660 661 662 663
	error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
				  LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
	if (error)
		goto fail_gunlock;
D
David Teigland 已提交
664

665
	error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
D
David Teigland 已提交
666 667 668
	if (error)
		goto fail_gunlock2;

669
	inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
670
				  inum.no_formal_ino, 0);
671
	if (IS_ERR(inode))
D
David Teigland 已提交
672 673
		goto fail_gunlock2;

674
	error = gfs2_inode_refresh(GFS2_I(inode));
D
David Teigland 已提交
675
	if (error)
676
		goto fail_gunlock2;
D
David Teigland 已提交
677

S
Steven Whitehouse 已提交
678
	error = gfs2_acl_create(dip, inode);
D
David Teigland 已提交
679
	if (error)
680
		goto fail_gunlock2;
D
David Teigland 已提交
681

682
	error = gfs2_security_init(dip, GFS2_I(inode), name);
683
	if (error)
684
		goto fail_gunlock2;
685

686
	error = link_dinode(dip, name, GFS2_I(inode));
D
David Teigland 已提交
687
	if (error)
688
		goto fail_gunlock2;
D
David Teigland 已提交
689

690 691
	if (bh)
		brelse(bh);
692
	return inode;
D
David Teigland 已提交
693

694
fail_gunlock2:
D
David Teigland 已提交
695
	gfs2_glock_dq_uninit(ghs + 1);
696
	if (inode && !IS_ERR(inode))
697
		iput(inode);
698
fail_gunlock:
D
David Teigland 已提交
699
	gfs2_glock_dq(ghs);
700
fail:
701 702
	if (bh)
		brelse(bh);
703
	return ERR_PTR(error);
D
David Teigland 已提交
704 705
}

706
static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
D
David Teigland 已提交
707
{
C
Christoph Hellwig 已提交
708
	struct inode *inode = &ip->i_inode;
D
David Teigland 已提交
709 710 711 712
	struct buffer_head *dibh;
	int error;

	error = gfs2_meta_inode_buffer(ip, &dibh);
C
Christoph Hellwig 已提交
713 714 715 716 717 718 719 720 721
	if (error)
		return error;

	setattr_copy(inode, attr);
	mark_inode_dirty(inode);
	gfs2_trans_add_bh(ip->i_gl, dibh, 1);
	gfs2_dinode_out(ip, dibh->b_data);
	brelse(dibh);
	return 0;
D
David Teigland 已提交
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
}

/**
 * gfs2_setattr_simple -
 * @ip:
 * @attr:
 *
 * Called with a reference on the vnode.
 *
 * Returns: errno
 */

int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
{
	int error;

738
	if (current->journal_info)
D
David Teigland 已提交
739 740
		return __gfs2_setattr_simple(ip, attr);

741
	error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
D
David Teigland 已提交
742 743 744 745
	if (error)
		return error;

	error = __gfs2_setattr_simple(ip, attr);
746
	gfs2_trans_end(GFS2_SB(&ip->i_inode));
D
David Teigland 已提交
747 748 749
	return error;
}

750 751 752 753 754 755 756 757 758 759 760 761 762
void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
{
	struct gfs2_dinode *str = buf;

	str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
	str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
	str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
	str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
	str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
	str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
	str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
	str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
	str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
S
Steven Whitehouse 已提交
763
	str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
764
	str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
765 766 767 768
	str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
	str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
	str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);

769 770
	str->di_goal_meta = cpu_to_be64(ip->i_goal);
	str->di_goal_data = cpu_to_be64(ip->i_goal);
771
	str->di_generation = cpu_to_be64(ip->i_generation);
772

773
	str->di_flags = cpu_to_be32(ip->i_diskflags);
774
	str->di_height = cpu_to_be16(ip->i_height);
775
	str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
776
					     !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
777
					     GFS2_FORMAT_DE : 0);
778
	str->di_depth = cpu_to_be16(ip->i_depth);
779
	str->di_entries = cpu_to_be32(ip->i_entries);
780

781
	str->di_eattr = cpu_to_be64(ip->i_eattr);
782 783 784
	str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
	str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
	str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
785
}