GFS2: Move gfs2_refresh_inode() and friends into glops.c
[deliverable/linux.git] / fs / gfs2 / inode.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
ca390601 3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
b3b94faa
DT
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/posix_acl.h>
16#include <linux/sort.h>
5c676f6d 17#include <linux/gfs2_ondisk.h>
71b86f56 18#include <linux/crc32.h>
fcb47e0b 19#include <linux/security.h>
719ee344 20#include <linux/time.h>
b3b94faa
DT
21
22#include "gfs2.h"
5c676f6d 23#include "incore.h"
b3b94faa
DT
24#include "acl.h"
25#include "bmap.h"
26#include "dir.h"
307cf6e6 27#include "xattr.h"
b3b94faa
DT
28#include "glock.h"
29#include "glops.h"
30#include "inode.h"
31#include "log.h"
32#include "meta_io.h"
b3b94faa
DT
33#include "quota.h"
34#include "rgrp.h"
35#include "trans.h"
5c676f6d 36#include "util.h"
b3b94faa 37
44ad37d6
BP
38struct gfs2_skip_data {
39 u64 no_addr;
40 int skipped;
41 int non_block;
42};
43
feaa7bba
SW
44static int iget_test(struct inode *inode, void *opaque)
45{
46 struct gfs2_inode *ip = GFS2_I(inode);
44ad37d6 47 struct gfs2_skip_data *data = opaque;
feaa7bba 48
44ad37d6
BP
49 if (ip->i_no_addr == data->no_addr) {
50 if (data->non_block &&
51 inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
52 data->skipped = 1;
53 return 0;
54 }
feaa7bba 55 return 1;
44ad37d6 56 }
feaa7bba
SW
57 return 0;
58}
59
60static int iget_set(struct inode *inode, void *opaque)
b3b94faa 61{
feaa7bba 62 struct gfs2_inode *ip = GFS2_I(inode);
44ad37d6 63 struct gfs2_skip_data *data = opaque;
b3b94faa 64
44ad37d6
BP
65 if (data->skipped)
66 return -ENOENT;
67 inode->i_ino = (unsigned long)(data->no_addr);
68 ip->i_no_addr = data->no_addr;
feaa7bba
SW
69 return 0;
70}
b3b94faa 71
4667a0ec 72struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
feaa7bba 73{
dbb7cae2 74 unsigned long hash = (unsigned long)no_addr;
44ad37d6
BP
75 struct gfs2_skip_data data;
76
77 data.no_addr = no_addr;
78 data.skipped = 0;
4667a0ec 79 data.non_block = non_block;
44ad37d6 80 return ilookup5(sb, hash, iget_test, &data);
feaa7bba 81}
b3b94faa 82
44ad37d6
BP
83static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
84 int non_block)
feaa7bba 85{
44ad37d6 86 struct gfs2_skip_data data;
dbb7cae2 87 unsigned long hash = (unsigned long)no_addr;
44ad37d6
BP
88
89 data.no_addr = no_addr;
90 data.skipped = 0;
91 data.non_block = non_block;
92 return iget5_locked(sb, hash, iget_test, iget_set, &data);
b3b94faa
DT
93}
94
35dcc52e 95/**
24d9765f
SW
96 * gfs2_set_iop - Sets inode operations
97 * @inode: The inode with correct i_mode filled in
35dcc52e 98 *
24d9765f
SW
99 * GFS2 lookup code fills in vfs inode contents based on info obtained
100 * from directory entry inside gfs2_inode_lookup().
101 */
35dcc52e 102
24d9765f 103static void gfs2_set_iop(struct inode *inode)
35dcc52e 104{
c97bfe43 105 struct gfs2_sbd *sdp = GFS2_SB(inode);
35dcc52e
WC
106 umode_t mode = inode->i_mode;
107
108 if (S_ISREG(mode)) {
109 inode->i_op = &gfs2_file_iops;
f057f6cd 110 if (gfs2_localflocks(sdp))
10d21988 111 inode->i_fop = &gfs2_file_fops_nolock;
c97bfe43 112 else
10d21988 113 inode->i_fop = &gfs2_file_fops;
35dcc52e
WC
114 } else if (S_ISDIR(mode)) {
115 inode->i_op = &gfs2_dir_iops;
f057f6cd 116 if (gfs2_localflocks(sdp))
10d21988 117 inode->i_fop = &gfs2_dir_fops_nolock;
c97bfe43 118 else
10d21988 119 inode->i_fop = &gfs2_dir_fops;
35dcc52e
WC
120 } else if (S_ISLNK(mode)) {
121 inode->i_op = &gfs2_symlink_iops;
122 } else {
d83225d4 123 inode->i_op = &gfs2_file_iops;
43a33c53 124 init_special_inode(inode, inode->i_mode, inode->i_rdev);
35dcc52e 125 }
35dcc52e
WC
126}
127
b3b94faa 128/**
feaa7bba
SW
129 * gfs2_inode_lookup - Lookup an inode
130 * @sb: The super block
dbb7cae2 131 * @no_addr: The inode number
feaa7bba 132 * @type: The type of the inode
44ad37d6 133 * non_block: Can we block on inodes that are being freed?
b3b94faa 134 *
feaa7bba 135 * Returns: A VFS inode, or an error
b3b94faa
DT
136 */
137
24d9765f 138struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
44ad37d6 139 u64 no_addr, u64 no_formal_ino, int non_block)
b3b94faa 140{
7a9f53b3
BM
141 struct inode *inode;
142 struct gfs2_inode *ip;
b1becbde 143 struct gfs2_glock *io_gl = NULL;
feaa7bba 144 int error;
b3b94faa 145
44ad37d6 146 inode = gfs2_iget(sb, no_addr, non_block);
7a9f53b3
BM
147 ip = GFS2_I(inode);
148
26d83ded
SW
149 if (!inode)
150 return ERR_PTR(-ENOBUFS);
151
feaa7bba
SW
152 if (inode->i_state & I_NEW) {
153 struct gfs2_sbd *sdp = GFS2_SB(inode);
bb9bcf06 154 ip->i_no_formal_ino = no_formal_ino;
b3b94faa 155
dbb7cae2 156 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
feaa7bba
SW
157 if (unlikely(error))
158 goto fail;
159 ip->i_gl->gl_object = ip;
b3b94faa 160
dbb7cae2 161 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
feaa7bba
SW
162 if (unlikely(error))
163 goto fail_put;
b3b94faa 164
bfded27b 165 set_bit(GIF_INVALID, &ip->i_flags);
feaa7bba
SW
166 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
167 if (unlikely(error))
168 goto fail_iopen;
b3b94faa 169
24d9765f 170 ip->i_iopen_gh.gh_gl->gl_object = ip;
feaa7bba 171 gfs2_glock_put(io_gl);
b1becbde 172 io_gl = NULL;
c8cdf479 173
c8cdf479 174 if (type == DT_UNKNOWN) {
24d9765f
SW
175 /* Inode glock must be locked already */
176 error = gfs2_inode_refresh(GFS2_I(inode));
177 if (error)
178 goto fail_refresh;
179 } else {
180 inode->i_mode = DT2IF(type);
c8cdf479
SW
181 }
182
35dcc52e 183 gfs2_set_iop(inode);
24d9765f 184 unlock_new_inode(inode);
feaa7bba 185 }
b3b94faa
DT
186
187 return inode;
24d9765f
SW
188
189fail_refresh:
190 ip->i_iopen_gh.gh_gl->gl_object = NULL;
191 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
feaa7bba 192fail_iopen:
b1becbde
BP
193 if (io_gl)
194 gfs2_glock_put(io_gl);
feaa7bba 195fail_put:
24d9765f 196 ip->i_gl->gl_object = NULL;
feaa7bba
SW
197 gfs2_glock_put(ip->i_gl);
198fail:
24d9765f 199 iget_failed(inode);
feaa7bba 200 return ERR_PTR(error);
b3b94faa
DT
201}
202
044b9414
SW
203struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
204 u64 *no_formal_ino, unsigned int blktype)
1a0eae88 205{
044b9414
SW
206 struct super_block *sb = sdp->sd_vfs;
207 struct gfs2_holder i_gh;
44ad37d6 208 struct inode *inode = NULL;
044b9414 209 int error;
1a0eae88 210
44ad37d6 211 /* Must not read in block until block type is verified */
044b9414 212 error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
44ad37d6 213 LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
044b9414
SW
214 if (error)
215 return ERR_PTR(error);
1a0eae88 216
044b9414
SW
217 error = gfs2_check_blk_type(sdp, no_addr, blktype);
218 if (error)
1a0eae88 219 goto fail;
1a0eae88 220
44ad37d6 221 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
044b9414
SW
222 if (IS_ERR(inode))
223 goto fail;
ed4878e8 224
044b9414
SW
225 /* Two extra checks for NFS only */
226 if (no_formal_ino) {
227 error = -ESTALE;
228 if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
229 goto fail_iput;
ed4878e8 230
044b9414
SW
231 error = -EIO;
232 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
233 goto fail_iput;
ed4878e8 234
044b9414
SW
235 error = 0;
236 }
1a0eae88 237
1a0eae88 238fail:
044b9414
SW
239 gfs2_glock_dq_uninit(&i_gh);
240 return error ? ERR_PTR(error) : inode;
241fail_iput:
242 iput(inode);
243 goto fail;
1a0eae88
BP
244}
245
b3b94faa 246
c752666c
SW
247struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
248{
249 struct qstr qstr;
6c93fd1e 250 struct inode *inode;
71b86f56 251 gfs2_str2qstr(&qstr, name);
a569c711 252 inode = gfs2_lookupi(dip, &qstr, 1);
6c93fd1e
RC
253 /* gfs2_lookupi has inconsistent callers: vfs
254 * related routines expect NULL for no entry found,
255 * gfs2_lookup_simple callers expect ENOENT
256 * and do not check for NULL.
257 */
258 if (inode == NULL)
259 return ERR_PTR(-ENOENT);
260 else
261 return inode;
c752666c
SW
262}
263
264
b3b94faa
DT
265/**
266 * gfs2_lookupi - Look up a filename in a directory and return its inode
267 * @d_gh: An initialized holder for the directory glock
268 * @name: The name of the inode to look for
269 * @is_root: If 1, ignore the caller's permissions
270 * @i_gh: An uninitialized holder for the new inode glock
271 *
d7c103d0
SW
272 * This can be called via the VFS filldir function when NFS is doing
273 * a readdirplus and the inode which its intending to stat isn't
274 * already in cache. In this case we must not take the directory glock
275 * again, since the readdir call will have already taken that lock.
b3b94faa
DT
276 *
277 * Returns: errno
278 */
279
feaa7bba 280struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
a569c711 281 int is_root)
b3b94faa 282{
c9fd4307 283 struct super_block *sb = dir->i_sb;
feaa7bba 284 struct gfs2_inode *dip = GFS2_I(dir);
b3b94faa 285 struct gfs2_holder d_gh;
037bcbb7 286 int error = 0;
c752666c 287 struct inode *inode = NULL;
d7c103d0 288 int unlock = 0;
b3b94faa
DT
289
290 if (!name->len || name->len > GFS2_FNAMESIZE)
c752666c 291 return ERR_PTR(-ENAMETOOLONG);
b3b94faa 292
c752666c
SW
293 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
294 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
295 dir == sb->s_root->d_inode)) {
320dd101
SW
296 igrab(dir);
297 return dir;
b3b94faa
DT
298 }
299
7afd88d9 300 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
d7c103d0
SW
301 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
302 if (error)
303 return ERR_PTR(error);
304 unlock = 1;
305 }
b3b94faa
DT
306
307 if (!is_root) {
b74c79e9 308 error = gfs2_permission(dir, MAY_EXEC, 0);
b3b94faa
DT
309 if (error)
310 goto out;
311 }
312
dbb7cae2
SW
313 inode = gfs2_dir_search(dir, name);
314 if (IS_ERR(inode))
315 error = PTR_ERR(inode);
7359a19c 316out:
d7c103d0
SW
317 if (unlock)
318 gfs2_glock_dq_uninit(&d_gh);
c752666c
SW
319 if (error == -ENOENT)
320 return NULL;
d7c103d0 321 return inode ? inode : ERR_PTR(error);
b3b94faa
DT
322}
323
b3b94faa
DT
324/**
325 * create_ok - OK to create a new on-disk inode here?
326 * @dip: Directory in which dinode is to be created
327 * @name: Name of new dinode
328 * @mode:
329 *
330 * Returns: errno
331 */
332
feaa7bba 333static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
b3b94faa
DT
334 unsigned int mode)
335{
336 int error;
337
b74c79e9 338 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
b3b94faa
DT
339 if (error)
340 return error;
341
342 /* Don't create entries in an unlinked directory */
4f56110a 343 if (!dip->i_inode.i_nlink)
d192a8e5 344 return -ENOENT;
b3b94faa 345
dbb7cae2 346 error = gfs2_dir_check(&dip->i_inode, name, NULL);
b3b94faa
DT
347 switch (error) {
348 case -ENOENT:
349 error = 0;
350 break;
351 case 0:
352 return -EEXIST;
353 default:
354 return error;
355 }
356
ad6203f2 357 if (dip->i_entries == (u32)-1)
b3b94faa 358 return -EFBIG;
4f56110a 359 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
b3b94faa
DT
360 return -EMLINK;
361
362 return 0;
363}
364
365static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
366 unsigned int *uid, unsigned int *gid)
367{
feaa7bba 368 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
2933f925 369 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
b3b94faa
DT
370 if (S_ISDIR(*mode))
371 *mode |= S_ISUID;
3de7be33 372 else if (dip->i_inode.i_uid != current_fsuid())
b3b94faa 373 *mode &= ~07111;
2933f925 374 *uid = dip->i_inode.i_uid;
b3b94faa 375 } else
3de7be33 376 *uid = current_fsuid();
b3b94faa 377
b60623c2 378 if (dip->i_inode.i_mode & S_ISGID) {
b3b94faa
DT
379 if (S_ISDIR(*mode))
380 *mode |= S_ISGID;
2933f925 381 *gid = dip->i_inode.i_gid;
b3b94faa 382 } else
3de7be33 383 *gid = current_fsgid();
b3b94faa
DT
384}
385
dbb7cae2 386static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
b3b94faa 387{
feaa7bba 388 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
b3b94faa
DT
389 int error;
390
6dbd8224
SW
391 if (gfs2_alloc_get(dip) == NULL)
392 return -ENOMEM;
b3b94faa 393
6dbd8224 394 dip->i_alloc->al_requested = RES_DINODE;
b3b94faa
DT
395 error = gfs2_inplace_reserve(dip);
396 if (error)
397 goto out;
398
feaa7bba 399 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
b3b94faa
DT
400 if (error)
401 goto out_ipreserv;
402
6050b9c7 403 error = gfs2_alloc_di(dip, no_addr, generation);
b3b94faa
DT
404
405 gfs2_trans_end(sdp);
406
4340fe62 407out_ipreserv:
b3b94faa 408 gfs2_inplace_release(dip);
4340fe62 409out:
b3b94faa 410 gfs2_alloc_put(dip);
b3b94faa
DT
411 return error;
412}
413
414/**
415 * init_dinode - Fill in a new dinode structure
416 * @dip: the directory this inode is being created in
417 * @gl: The glock covering the new inode
418 * @inum: the inode number
419 * @mode: the file permissions
420 * @uid:
421 * @gid:
422 *
423 */
424
425static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
629a21e7 426 const struct gfs2_inum_host *inum, unsigned int mode,
4340fe62 427 unsigned int uid, unsigned int gid,
e9bd2b3b 428 const u64 *generation, dev_t dev, struct buffer_head **bhp)
b3b94faa 429{
feaa7bba 430 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
b96ca4fa 431 struct gfs2_dinode *di;
b3b94faa 432 struct buffer_head *dibh;
4bd91ba1 433 struct timespec tv = CURRENT_TIME;
b3b94faa
DT
434
435 dibh = gfs2_meta_new(gl, inum->no_addr);
d4e9c4c3 436 gfs2_trans_add_bh(gl, dibh, 1);
b3b94faa
DT
437 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
438 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
b96ca4fa
SW
439 di = (struct gfs2_dinode *)dibh->b_data;
440
2442a098
SW
441 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
442 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
b96ca4fa
SW
443 di->di_mode = cpu_to_be32(mode);
444 di->di_uid = cpu_to_be32(uid);
445 di->di_gid = cpu_to_be32(gid);
294caaa3
SW
446 di->di_nlink = 0;
447 di->di_size = 0;
b96ca4fa 448 di->di_blocks = cpu_to_be64(1);
4bd91ba1 449 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
e7f14f4d
SW
450 di->di_major = cpu_to_be32(MAJOR(dev));
451 di->di_minor = cpu_to_be32(MINOR(dev));
b96ca4fa 452 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
4340fe62 453 di->di_generation = cpu_to_be64(*generation);
294caaa3 454 di->di_flags = 0;
b3b94faa
DT
455
456 if (S_ISREG(mode)) {
383f01fb 457 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
b3b94faa 458 gfs2_tune_get(sdp, gt_new_files_jdata))
b96ca4fa 459 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
b3b94faa 460 } else if (S_ISDIR(mode)) {
383f01fb 461 di->di_flags |= cpu_to_be32(dip->i_diskflags &
568f4c96 462 GFS2_DIF_INHERIT_JDATA);
b3b94faa
DT
463 }
464
b96ca4fa 465 di->__pad1 = 0;
a9583c79 466 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
294caaa3 467 di->di_height = 0;
b96ca4fa
SW
468 di->__pad2 = 0;
469 di->__pad3 = 0;
294caaa3
SW
470 di->di_depth = 0;
471 di->di_entries = 0;
b96ca4fa 472 memset(&di->__pad4, 0, sizeof(di->__pad4));
294caaa3 473 di->di_eattr = 0;
4bd91ba1
SW
474 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
475 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
476 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
b96ca4fa 477 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
e9bd2b3b
WC
478
479 set_buffer_uptodate(dibh);
b96ca4fa 480
e9bd2b3b 481 *bhp = dibh;
b3b94faa
DT
482}
483
484static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
629a21e7 485 unsigned int mode, const struct gfs2_inum_host *inum,
e9bd2b3b 486 const u64 *generation, dev_t dev, struct buffer_head **bhp)
b3b94faa 487{
feaa7bba 488 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
b3b94faa
DT
489 unsigned int uid, gid;
490 int error;
491
492 munge_mode_uid_gid(dip, &mode, &uid, &gid);
182fe5ab
CG
493 if (!gfs2_alloc_get(dip))
494 return -ENOMEM;
b3b94faa
DT
495
496 error = gfs2_quota_lock(dip, uid, gid);
497 if (error)
498 goto out;
499
500 error = gfs2_quota_check(dip, uid, gid);
501 if (error)
502 goto out_quota;
503
feaa7bba 504 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
b3b94faa
DT
505 if (error)
506 goto out_quota;
507
e9bd2b3b 508 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
b3b94faa 509 gfs2_quota_change(dip, +1, uid, gid);
b3b94faa
DT
510 gfs2_trans_end(sdp);
511
feaa7bba 512out_quota:
b3b94faa 513 gfs2_quota_unlock(dip);
feaa7bba 514out:
b3b94faa 515 gfs2_alloc_put(dip);
b3b94faa
DT
516 return error;
517}
518
feaa7bba
SW
519static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
520 struct gfs2_inode *ip)
b3b94faa 521{
feaa7bba 522 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
b3b94faa
DT
523 struct gfs2_alloc *al;
524 int alloc_required;
525 struct buffer_head *dibh;
526 int error;
527
528 al = gfs2_alloc_get(dip);
182fe5ab
CG
529 if (!al)
530 return -ENOMEM;
b3b94faa
DT
531
532 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
533 if (error)
534 goto fail;
535
feaa7bba 536 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
c752666c 537 if (alloc_required < 0)
1b8177ec 538 goto fail_quota_locks;
b3b94faa 539 if (alloc_required) {
2933f925 540 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
b3b94faa
DT
541 if (error)
542 goto fail_quota_locks;
543
544 al->al_requested = sdp->sd_max_dirres;
545
546 error = gfs2_inplace_reserve(dip);
547 if (error)
548 goto fail_quota_locks;
549
320dd101 550 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
bb8d8a6f 551 al->al_rgd->rd_length +
907b9bce 552 2 * RES_DINODE +
b3b94faa
DT
553 RES_STATFS + RES_QUOTA, 0);
554 if (error)
555 goto fail_ipreserv;
556 } else {
feaa7bba 557 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
b3b94faa
DT
558 if (error)
559 goto fail_quota_locks;
560 }
561
3d6ecb7d 562 error = gfs2_dir_add(&dip->i_inode, name, ip);
b3b94faa
DT
563 if (error)
564 goto fail_end_trans;
565
566 error = gfs2_meta_inode_buffer(ip, &dibh);
567 if (error)
568 goto fail_end_trans;
4f56110a 569 ip->i_inode.i_nlink = 1;
d4e9c4c3 570 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 571 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa 572 brelse(dibh);
b3b94faa
DT
573 return 0;
574
320dd101 575fail_end_trans:
b3b94faa
DT
576 gfs2_trans_end(sdp);
577
320dd101 578fail_ipreserv:
6dbd8224 579 if (dip->i_alloc->al_rgd)
b3b94faa
DT
580 gfs2_inplace_release(dip);
581
320dd101 582fail_quota_locks:
b3b94faa
DT
583 gfs2_quota_unlock(dip);
584
320dd101 585fail:
b3b94faa 586 gfs2_alloc_put(dip);
b3b94faa
DT
587 return error;
588}
589
2a7dba39
EP
590static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
591 const struct qstr *qstr)
fcb47e0b
RH
592{
593 int err;
594 size_t len;
595 void *value;
596 char *name;
fcb47e0b 597
2a7dba39 598 err = security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
fcb47e0b
RH
599 &name, &value, &len);
600
601 if (err) {
602 if (err == -EOPNOTSUPP)
603 return 0;
604 return err;
605 }
606
431547b3
CH
607 err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
608 GFS2_EATYPE_SECURITY);
fcb47e0b
RH
609 kfree(value);
610 kfree(name);
611
612 return err;
613}
614
b3b94faa
DT
615/**
616 * gfs2_createi - Create a new inode
617 * @ghs: An array of two holders
618 * @name: The name of the new file
619 * @mode: the permissions on the new inode
620 *
621 * @ghs[0] is an initialized holder for the directory
622 * @ghs[1] is the holder for the inode lock
623 *
7359a19c 624 * If the return value is not NULL, the glocks on both the directory and the new
b3b94faa
DT
625 * file are held. A transaction has been started and an inplace reservation
626 * is held, as well.
627 *
7359a19c 628 * Returns: An inode
b3b94faa
DT
629 */
630
feaa7bba 631struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
e7f14f4d 632 unsigned int mode, dev_t dev)
b3b94faa 633{
e1cc8603 634 struct inode *inode = NULL;
5c676f6d 635 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
feaa7bba
SW
636 struct inode *dir = &dip->i_inode;
637 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
dbb7cae2 638 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
b3b94faa 639 int error;
4340fe62 640 u64 generation;
f91a0d3e 641 struct buffer_head *bh = NULL;
b3b94faa
DT
642
643 if (!name->len || name->len > GFS2_FNAMESIZE)
7359a19c 644 return ERR_PTR(-ENAMETOOLONG);
b3b94faa 645
b3b94faa
DT
646 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
647 error = gfs2_glock_nq(ghs);
648 if (error)
649 goto fail;
650
651 error = create_ok(dip, name, mode);
652 if (error)
653 goto fail_gunlock;
654
dbb7cae2 655 error = alloc_dinode(dip, &inum.no_addr, &generation);
b3b94faa
DT
656 if (error)
657 goto fail_gunlock;
8d8291ae 658 inum.no_formal_ino = generation;
b3b94faa 659
28626e20
SW
660 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
661 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
662 if (error)
663 goto fail_gunlock;
b3b94faa 664
e9bd2b3b 665 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
b3b94faa
DT
666 if (error)
667 goto fail_gunlock2;
668
8d8291ae 669 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
44ad37d6 670 inum.no_formal_ino, 0);
feaa7bba 671 if (IS_ERR(inode))
b3b94faa
DT
672 goto fail_gunlock2;
673
feaa7bba 674 error = gfs2_inode_refresh(GFS2_I(inode));
b3b94faa 675 if (error)
e1cc8603 676 goto fail_gunlock2;
b3b94faa 677
479c427d 678 error = gfs2_acl_create(dip, inode);
b3b94faa 679 if (error)
e1cc8603 680 goto fail_gunlock2;
b3b94faa 681
2a7dba39 682 error = gfs2_security_init(dip, GFS2_I(inode), name);
fcb47e0b 683 if (error)
e1cc8603 684 goto fail_gunlock2;
fcb47e0b 685
feaa7bba 686 error = link_dinode(dip, name, GFS2_I(inode));
b3b94faa 687 if (error)
e1cc8603 688 goto fail_gunlock2;
b3b94faa 689
f91a0d3e
SW
690 if (bh)
691 brelse(bh);
7359a19c 692 return inode;
b3b94faa 693
320dd101 694fail_gunlock2:
b3b94faa 695 gfs2_glock_dq_uninit(ghs + 1);
bd1eb881 696 if (inode && !IS_ERR(inode))
e1cc8603 697 iput(inode);
320dd101 698fail_gunlock:
b3b94faa 699 gfs2_glock_dq(ghs);
320dd101 700fail:
f91a0d3e
SW
701 if (bh)
702 brelse(bh);
7359a19c 703 return ERR_PTR(error);
b3b94faa
DT
704}
705
536baf02 706static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
b3b94faa 707{
1025774c 708 struct inode *inode = &ip->i_inode;
b3b94faa
DT
709 struct buffer_head *dibh;
710 int error;
711
712 error = gfs2_meta_inode_buffer(ip, &dibh);
1025774c
CH
713 if (error)
714 return error;
715
1025774c
CH
716 setattr_copy(inode, attr);
717 mark_inode_dirty(inode);
1025774c
CH
718 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
719 gfs2_dinode_out(ip, dibh->b_data);
720 brelse(dibh);
721 return 0;
b3b94faa
DT
722}
723
724/**
725 * gfs2_setattr_simple -
726 * @ip:
727 * @attr:
728 *
729 * Called with a reference on the vnode.
730 *
731 * Returns: errno
732 */
733
734int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
735{
736 int error;
737
5c676f6d 738 if (current->journal_info)
b3b94faa
DT
739 return __gfs2_setattr_simple(ip, attr);
740
feaa7bba 741 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
b3b94faa
DT
742 if (error)
743 return error;
744
745 error = __gfs2_setattr_simple(ip, attr);
feaa7bba 746 gfs2_trans_end(GFS2_SB(&ip->i_inode));
b3b94faa
DT
747 return error;
748}
749
bb8d8a6f
SW
750void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
751{
bb8d8a6f
SW
752 struct gfs2_dinode *str = buf;
753
754 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
755 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
bb8d8a6f 756 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
bb8d8a6f
SW
757 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
758 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
759 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
760 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
761 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
762 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
a2e0f799 763 str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
77658aad 764 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
bb8d8a6f
SW
765 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
766 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
767 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
768
ce276b06
SW
769 str->di_goal_meta = cpu_to_be64(ip->i_goal);
770 str->di_goal_data = cpu_to_be64(ip->i_goal);
bcf0b5b3 771 str->di_generation = cpu_to_be64(ip->i_generation);
bb8d8a6f 772
383f01fb 773 str->di_flags = cpu_to_be32(ip->i_diskflags);
ecc30c79 774 str->di_height = cpu_to_be16(ip->i_height);
bb8d8a6f 775 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
383f01fb 776 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
bb8d8a6f 777 GFS2_FORMAT_DE : 0);
9a004508 778 str->di_depth = cpu_to_be16(ip->i_depth);
ad6203f2 779 str->di_entries = cpu_to_be32(ip->i_entries);
bb8d8a6f 780
3767ac21 781 str->di_eattr = cpu_to_be64(ip->i_eattr);
4bd91ba1
SW
782 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
783 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
784 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
bb8d8a6f 785}
This page took 0.679949 seconds and 5 git commands to generate.