[SCSI] qla2xxx: Refactor call to qla2xxx_read_sfp for thermal temperature.
[linux-block.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
bb8d8a6f
SW
38struct gfs2_inum_range_host {
39 u64 ir_start;
40 u64 ir_length;
41};
42
feaa7bba
SW
43static int iget_test(struct inode *inode, void *opaque)
44{
45 struct gfs2_inode *ip = GFS2_I(inode);
dbb7cae2 46 u64 *no_addr = opaque;
feaa7bba 47
009d8518 48 if (ip->i_no_addr == *no_addr)
feaa7bba 49 return 1;
b3b94faa 50
feaa7bba
SW
51 return 0;
52}
53
54static int iget_set(struct inode *inode, void *opaque)
b3b94faa 55{
feaa7bba 56 struct gfs2_inode *ip = GFS2_I(inode);
dbb7cae2 57 u64 *no_addr = opaque;
b3b94faa 58
dbb7cae2
SW
59 inode->i_ino = (unsigned long)*no_addr;
60 ip->i_no_addr = *no_addr;
feaa7bba
SW
61 return 0;
62}
b3b94faa 63
dbb7cae2 64struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
feaa7bba 65{
dbb7cae2
SW
66 unsigned long hash = (unsigned long)no_addr;
67 return ilookup5(sb, hash, iget_test, &no_addr);
feaa7bba 68}
b3b94faa 69
dbb7cae2 70static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
feaa7bba 71{
dbb7cae2
SW
72 unsigned long hash = (unsigned long)no_addr;
73 return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
b3b94faa
DT
74}
75
35dcc52e 76/**
24d9765f
SW
77 * gfs2_set_iop - Sets inode operations
78 * @inode: The inode with correct i_mode filled in
35dcc52e 79 *
24d9765f
SW
80 * GFS2 lookup code fills in vfs inode contents based on info obtained
81 * from directory entry inside gfs2_inode_lookup().
82 */
35dcc52e 83
24d9765f 84static void gfs2_set_iop(struct inode *inode)
35dcc52e 85{
c97bfe43 86 struct gfs2_sbd *sdp = GFS2_SB(inode);
35dcc52e
WC
87 umode_t mode = inode->i_mode;
88
89 if (S_ISREG(mode)) {
90 inode->i_op = &gfs2_file_iops;
f057f6cd 91 if (gfs2_localflocks(sdp))
10d21988 92 inode->i_fop = &gfs2_file_fops_nolock;
c97bfe43 93 else
10d21988 94 inode->i_fop = &gfs2_file_fops;
35dcc52e
WC
95 } else if (S_ISDIR(mode)) {
96 inode->i_op = &gfs2_dir_iops;
f057f6cd 97 if (gfs2_localflocks(sdp))
10d21988 98 inode->i_fop = &gfs2_dir_fops_nolock;
c97bfe43 99 else
10d21988 100 inode->i_fop = &gfs2_dir_fops;
35dcc52e
WC
101 } else if (S_ISLNK(mode)) {
102 inode->i_op = &gfs2_symlink_iops;
103 } else {
d83225d4 104 inode->i_op = &gfs2_file_iops;
43a33c53 105 init_special_inode(inode, inode->i_mode, inode->i_rdev);
35dcc52e 106 }
35dcc52e
WC
107}
108
b3b94faa 109/**
feaa7bba
SW
110 * gfs2_inode_lookup - Lookup an inode
111 * @sb: The super block
dbb7cae2 112 * @no_addr: The inode number
feaa7bba 113 * @type: The type of the inode
b3b94faa 114 *
feaa7bba 115 * Returns: A VFS inode, or an error
b3b94faa
DT
116 */
117
24d9765f
SW
118struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
119 u64 no_addr, u64 no_formal_ino)
b3b94faa 120{
7a9f53b3
BM
121 struct inode *inode;
122 struct gfs2_inode *ip;
b1becbde 123 struct gfs2_glock *io_gl = NULL;
feaa7bba 124 int error;
b3b94faa 125
1a0eae88 126 inode = gfs2_iget(sb, no_addr);
7a9f53b3
BM
127 ip = GFS2_I(inode);
128
26d83ded
SW
129 if (!inode)
130 return ERR_PTR(-ENOBUFS);
131
feaa7bba
SW
132 if (inode->i_state & I_NEW) {
133 struct gfs2_sbd *sdp = GFS2_SB(inode);
bb9bcf06 134 ip->i_no_formal_ino = no_formal_ino;
b3b94faa 135
dbb7cae2 136 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
feaa7bba
SW
137 if (unlikely(error))
138 goto fail;
139 ip->i_gl->gl_object = ip;
b3b94faa 140
dbb7cae2 141 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
feaa7bba
SW
142 if (unlikely(error))
143 goto fail_put;
b3b94faa 144
bfded27b 145 set_bit(GIF_INVALID, &ip->i_flags);
feaa7bba
SW
146 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
147 if (unlikely(error))
148 goto fail_iopen;
b3b94faa 149
24d9765f 150 ip->i_iopen_gh.gh_gl->gl_object = ip;
feaa7bba 151 gfs2_glock_put(io_gl);
b1becbde 152 io_gl = NULL;
c8cdf479 153
c8cdf479 154 if (type == DT_UNKNOWN) {
24d9765f
SW
155 /* Inode glock must be locked already */
156 error = gfs2_inode_refresh(GFS2_I(inode));
157 if (error)
158 goto fail_refresh;
159 } else {
160 inode->i_mode = DT2IF(type);
c8cdf479
SW
161 }
162
35dcc52e 163 gfs2_set_iop(inode);
24d9765f 164 unlock_new_inode(inode);
feaa7bba 165 }
b3b94faa
DT
166
167 return inode;
24d9765f
SW
168
169fail_refresh:
170 ip->i_iopen_gh.gh_gl->gl_object = NULL;
171 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
feaa7bba 172fail_iopen:
b1becbde
BP
173 if (io_gl)
174 gfs2_glock_put(io_gl);
feaa7bba 175fail_put:
24d9765f 176 ip->i_gl->gl_object = NULL;
feaa7bba
SW
177 gfs2_glock_put(ip->i_gl);
178fail:
24d9765f 179 iget_failed(inode);
feaa7bba 180 return ERR_PTR(error);
b3b94faa
DT
181}
182
044b9414
SW
183struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
184 u64 *no_formal_ino, unsigned int blktype)
1a0eae88 185{
044b9414
SW
186 struct super_block *sb = sdp->sd_vfs;
187 struct gfs2_holder i_gh;
ed4878e8 188 struct inode *inode;
044b9414 189 int error;
1a0eae88 190
044b9414
SW
191 error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
192 LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
193 if (error)
194 return ERR_PTR(error);
1a0eae88 195
044b9414
SW
196 error = gfs2_check_blk_type(sdp, no_addr, blktype);
197 if (error)
1a0eae88 198 goto fail;
1a0eae88 199
044b9414
SW
200 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0);
201 if (IS_ERR(inode))
202 goto fail;
ed4878e8 203
044b9414
SW
204 /* Two extra checks for NFS only */
205 if (no_formal_ino) {
206 error = -ESTALE;
207 if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
208 goto fail_iput;
ed4878e8 209
044b9414
SW
210 error = -EIO;
211 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
212 goto fail_iput;
ed4878e8 213
044b9414
SW
214 error = 0;
215 }
1a0eae88 216
1a0eae88 217fail:
044b9414
SW
218 gfs2_glock_dq_uninit(&i_gh);
219 return error ? ERR_PTR(error) : inode;
220fail_iput:
221 iput(inode);
222 goto fail;
1a0eae88
BP
223}
224
af339c02 225static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
ea744d01 226{
ea744d01 227 const struct gfs2_dinode *str = buf;
719ee344 228 struct timespec atime;
9a004508 229 u16 height, depth;
ea744d01 230
ecc30c79
SW
231 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
232 goto corrupt;
dbb7cae2 233 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
b60623c2 234 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
e7f14f4d 235 ip->i_inode.i_rdev = 0;
b60623c2 236 switch (ip->i_inode.i_mode & S_IFMT) {
e7f14f4d
SW
237 case S_IFBLK:
238 case S_IFCHR:
239 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
240 be32_to_cpu(str->di_minor));
241 break;
242 };
243
2933f925
SW
244 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
245 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
4f56110a
SW
246 /*
247 * We will need to review setting the nlink count here in the
248 * light of the forthcoming ro bind mount work. This is a reminder
249 * to do that.
250 */
251 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
a2e0f799 252 i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
77658aad 253 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
719ee344
SW
254 atime.tv_sec = be64_to_cpu(str->di_atime);
255 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
256 if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
257 ip->i_inode.i_atime = atime;
1a7b1eed 258 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
4bd91ba1 259 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
1a7b1eed 260 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
4bd91ba1 261 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
ea744d01 262
ce276b06 263 ip->i_goal = be64_to_cpu(str->di_goal_meta);
bcf0b5b3 264 ip->i_generation = be64_to_cpu(str->di_generation);
ea744d01 265
383f01fb 266 ip->i_diskflags = be32_to_cpu(str->di_flags);
6b124d8d 267 gfs2_set_inode_flags(&ip->i_inode);
ecc30c79
SW
268 height = be16_to_cpu(str->di_height);
269 if (unlikely(height > GFS2_MAX_META_HEIGHT))
270 goto corrupt;
271 ip->i_height = (u8)height;
ea744d01 272
9a004508
SW
273 depth = be16_to_cpu(str->di_depth);
274 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
275 goto corrupt;
276 ip->i_depth = (u8)depth;
ad6203f2 277 ip->i_entries = be32_to_cpu(str->di_entries);
ea744d01 278
3767ac21 279 ip->i_eattr = be64_to_cpu(str->di_eattr);
5561093e
SW
280 if (S_ISREG(ip->i_inode.i_mode))
281 gfs2_set_aops(&ip->i_inode);
282
af339c02 283 return 0;
ecc30c79
SW
284corrupt:
285 if (gfs2_consist_inode(ip))
286 gfs2_dinode_print(ip);
287 return -EIO;
ea744d01
SW
288}
289
b3b94faa
DT
290/**
291 * gfs2_inode_refresh - Refresh the incore copy of the dinode
292 * @ip: The GFS2 inode
293 *
294 * Returns: errno
295 */
296
297int gfs2_inode_refresh(struct gfs2_inode *ip)
298{
299 struct buffer_head *dibh;
300 int error;
301
302 error = gfs2_meta_inode_buffer(ip, &dibh);
303 if (error)
304 return error;
305
feaa7bba 306 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
b3b94faa
DT
307 brelse(dibh);
308 return -EIO;
309 }
310
af339c02 311 error = gfs2_dinode_in(ip, dibh->b_data);
b3b94faa 312 brelse(dibh);
bfded27b 313 clear_bit(GIF_INVALID, &ip->i_flags);
b3b94faa 314
af339c02 315 return error;
b3b94faa
DT
316}
317
feaa7bba 318int gfs2_dinode_dealloc(struct gfs2_inode *ip)
b3b94faa 319{
feaa7bba 320 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa
DT
321 struct gfs2_alloc *al;
322 struct gfs2_rgrpd *rgd;
323 int error;
324
77658aad 325 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
b3b94faa 326 if (gfs2_consist_inode(ip))
4cc14f0b 327 gfs2_dinode_print(ip);
b3b94faa
DT
328 return -EIO;
329 }
330
331 al = gfs2_alloc_get(ip);
182fe5ab
CG
332 if (!al)
333 return -ENOMEM;
b3b94faa
DT
334
335 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
336 if (error)
337 goto out;
338
339 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
340 if (error)
341 goto out_qs;
342
dbb7cae2 343 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
b3b94faa
DT
344 if (!rgd) {
345 gfs2_consist_inode(ip);
346 error = -EIO;
347 goto out_rindex_relse;
348 }
349
350 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
351 &al->al_rgd_gh);
352 if (error)
353 goto out_rindex_relse;
354
420b9e5e 355 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
b3b94faa
DT
356 if (error)
357 goto out_rg_gunlock;
358
2bcd610d
SW
359 set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
360 set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
b3b94faa
DT
361
362 gfs2_free_di(rgd, ip);
363
b3b94faa 364 gfs2_trans_end(sdp);
b3b94faa 365
feaa7bba 366out_rg_gunlock:
b3b94faa 367 gfs2_glock_dq_uninit(&al->al_rgd_gh);
feaa7bba 368out_rindex_relse:
b3b94faa 369 gfs2_glock_dq_uninit(&al->al_ri_gh);
feaa7bba 370out_qs:
b3b94faa 371 gfs2_quota_unhold(ip);
36327521 372out:
feaa7bba 373 gfs2_alloc_put(ip);
b3b94faa
DT
374 return error;
375}
376
b3b94faa 377/**
87d21e07 378 * gfs2_change_nlink - Change nlink count on inode
b3b94faa
DT
379 * @ip: The GFS2 inode
380 * @diff: The change in the nlink count required
381 *
382 * Returns: errno
383 */
87d21e07 384int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
b3b94faa
DT
385{
386 struct buffer_head *dibh;
cd915493 387 u32 nlink;
b3b94faa
DT
388 int error;
389
4f56110a
SW
390 BUG_ON(diff != 1 && diff != -1);
391 nlink = ip->i_inode.i_nlink + diff;
b3b94faa
DT
392
393 /* If we are reducing the nlink count, but the new value ends up being
394 bigger than the old one, we must have underflowed. */
4f56110a 395 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
b3b94faa 396 if (gfs2_consist_inode(ip))
4cc14f0b 397 gfs2_dinode_print(ip);
b3b94faa
DT
398 return -EIO;
399 }
400
401 error = gfs2_meta_inode_buffer(ip, &dibh);
402 if (error)
403 return error;
404
4f56110a
SW
405 if (diff > 0)
406 inc_nlink(&ip->i_inode);
407 else
408 drop_nlink(&ip->i_inode);
409
4bd91ba1 410 ip->i_inode.i_ctime = CURRENT_TIME;
b3b94faa 411
d4e9c4c3 412 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 413 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa 414 brelse(dibh);
feaa7bba 415 mark_inode_dirty(&ip->i_inode);
b3b94faa 416
87d21e07 417 if (ip->i_inode.i_nlink == 0)
ddee7608 418 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
87d21e07 419
5509826f
WC
420 return error;
421}
422
c752666c
SW
423struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
424{
425 struct qstr qstr;
6c93fd1e 426 struct inode *inode;
71b86f56 427 gfs2_str2qstr(&qstr, name);
a569c711 428 inode = gfs2_lookupi(dip, &qstr, 1);
6c93fd1e
RC
429 /* gfs2_lookupi has inconsistent callers: vfs
430 * related routines expect NULL for no entry found,
431 * gfs2_lookup_simple callers expect ENOENT
432 * and do not check for NULL.
433 */
434 if (inode == NULL)
435 return ERR_PTR(-ENOENT);
436 else
437 return inode;
c752666c
SW
438}
439
440
b3b94faa
DT
441/**
442 * gfs2_lookupi - Look up a filename in a directory and return its inode
443 * @d_gh: An initialized holder for the directory glock
444 * @name: The name of the inode to look for
445 * @is_root: If 1, ignore the caller's permissions
446 * @i_gh: An uninitialized holder for the new inode glock
447 *
d7c103d0
SW
448 * This can be called via the VFS filldir function when NFS is doing
449 * a readdirplus and the inode which its intending to stat isn't
450 * already in cache. In this case we must not take the directory glock
451 * again, since the readdir call will have already taken that lock.
b3b94faa
DT
452 *
453 * Returns: errno
454 */
455
feaa7bba 456struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
a569c711 457 int is_root)
b3b94faa 458{
c9fd4307 459 struct super_block *sb = dir->i_sb;
feaa7bba 460 struct gfs2_inode *dip = GFS2_I(dir);
b3b94faa 461 struct gfs2_holder d_gh;
037bcbb7 462 int error = 0;
c752666c 463 struct inode *inode = NULL;
d7c103d0 464 int unlock = 0;
b3b94faa
DT
465
466 if (!name->len || name->len > GFS2_FNAMESIZE)
c752666c 467 return ERR_PTR(-ENAMETOOLONG);
b3b94faa 468
c752666c
SW
469 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
470 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
471 dir == sb->s_root->d_inode)) {
320dd101
SW
472 igrab(dir);
473 return dir;
b3b94faa
DT
474 }
475
7afd88d9 476 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
d7c103d0
SW
477 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
478 if (error)
479 return ERR_PTR(error);
480 unlock = 1;
481 }
b3b94faa
DT
482
483 if (!is_root) {
b74c79e9 484 error = gfs2_permission(dir, MAY_EXEC, 0);
b3b94faa
DT
485 if (error)
486 goto out;
487 }
488
dbb7cae2
SW
489 inode = gfs2_dir_search(dir, name);
490 if (IS_ERR(inode))
491 error = PTR_ERR(inode);
7359a19c 492out:
d7c103d0
SW
493 if (unlock)
494 gfs2_glock_dq_uninit(&d_gh);
c752666c
SW
495 if (error == -ENOENT)
496 return NULL;
d7c103d0 497 return inode ? inode : ERR_PTR(error);
b3b94faa
DT
498}
499
b3b94faa
DT
500/**
501 * create_ok - OK to create a new on-disk inode here?
502 * @dip: Directory in which dinode is to be created
503 * @name: Name of new dinode
504 * @mode:
505 *
506 * Returns: errno
507 */
508
feaa7bba 509static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
b3b94faa
DT
510 unsigned int mode)
511{
512 int error;
513
b74c79e9 514 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
b3b94faa
DT
515 if (error)
516 return error;
517
518 /* Don't create entries in an unlinked directory */
4f56110a 519 if (!dip->i_inode.i_nlink)
b3b94faa
DT
520 return -EPERM;
521
dbb7cae2 522 error = gfs2_dir_check(&dip->i_inode, name, NULL);
b3b94faa
DT
523 switch (error) {
524 case -ENOENT:
525 error = 0;
526 break;
527 case 0:
528 return -EEXIST;
529 default:
530 return error;
531 }
532
ad6203f2 533 if (dip->i_entries == (u32)-1)
b3b94faa 534 return -EFBIG;
4f56110a 535 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
b3b94faa
DT
536 return -EMLINK;
537
538 return 0;
539}
540
541static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
542 unsigned int *uid, unsigned int *gid)
543{
feaa7bba 544 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
2933f925 545 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
b3b94faa
DT
546 if (S_ISDIR(*mode))
547 *mode |= S_ISUID;
3de7be33 548 else if (dip->i_inode.i_uid != current_fsuid())
b3b94faa 549 *mode &= ~07111;
2933f925 550 *uid = dip->i_inode.i_uid;
b3b94faa 551 } else
3de7be33 552 *uid = current_fsuid();
b3b94faa 553
b60623c2 554 if (dip->i_inode.i_mode & S_ISGID) {
b3b94faa
DT
555 if (S_ISDIR(*mode))
556 *mode |= S_ISGID;
2933f925 557 *gid = dip->i_inode.i_gid;
b3b94faa 558 } else
3de7be33 559 *gid = current_fsgid();
b3b94faa
DT
560}
561
dbb7cae2 562static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
b3b94faa 563{
feaa7bba 564 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
b3b94faa
DT
565 int error;
566
6dbd8224
SW
567 if (gfs2_alloc_get(dip) == NULL)
568 return -ENOMEM;
b3b94faa 569
6dbd8224 570 dip->i_alloc->al_requested = RES_DINODE;
b3b94faa
DT
571 error = gfs2_inplace_reserve(dip);
572 if (error)
573 goto out;
574
feaa7bba 575 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
b3b94faa
DT
576 if (error)
577 goto out_ipreserv;
578
6050b9c7 579 error = gfs2_alloc_di(dip, no_addr, generation);
b3b94faa
DT
580
581 gfs2_trans_end(sdp);
582
4340fe62 583out_ipreserv:
b3b94faa 584 gfs2_inplace_release(dip);
4340fe62 585out:
b3b94faa 586 gfs2_alloc_put(dip);
b3b94faa
DT
587 return error;
588}
589
590/**
591 * init_dinode - Fill in a new dinode structure
592 * @dip: the directory this inode is being created in
593 * @gl: The glock covering the new inode
594 * @inum: the inode number
595 * @mode: the file permissions
596 * @uid:
597 * @gid:
598 *
599 */
600
601static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
629a21e7 602 const struct gfs2_inum_host *inum, unsigned int mode,
4340fe62 603 unsigned int uid, unsigned int gid,
e9bd2b3b 604 const u64 *generation, dev_t dev, struct buffer_head **bhp)
b3b94faa 605{
feaa7bba 606 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
b96ca4fa 607 struct gfs2_dinode *di;
b3b94faa 608 struct buffer_head *dibh;
4bd91ba1 609 struct timespec tv = CURRENT_TIME;
b3b94faa
DT
610
611 dibh = gfs2_meta_new(gl, inum->no_addr);
d4e9c4c3 612 gfs2_trans_add_bh(gl, dibh, 1);
b3b94faa
DT
613 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
614 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
b96ca4fa
SW
615 di = (struct gfs2_dinode *)dibh->b_data;
616
2442a098
SW
617 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
618 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
b96ca4fa
SW
619 di->di_mode = cpu_to_be32(mode);
620 di->di_uid = cpu_to_be32(uid);
621 di->di_gid = cpu_to_be32(gid);
294caaa3
SW
622 di->di_nlink = 0;
623 di->di_size = 0;
b96ca4fa 624 di->di_blocks = cpu_to_be64(1);
4bd91ba1 625 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
e7f14f4d
SW
626 di->di_major = cpu_to_be32(MAJOR(dev));
627 di->di_minor = cpu_to_be32(MINOR(dev));
b96ca4fa 628 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
4340fe62 629 di->di_generation = cpu_to_be64(*generation);
294caaa3 630 di->di_flags = 0;
b3b94faa
DT
631
632 if (S_ISREG(mode)) {
383f01fb 633 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
b3b94faa 634 gfs2_tune_get(sdp, gt_new_files_jdata))
b96ca4fa 635 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
b3b94faa 636 } else if (S_ISDIR(mode)) {
383f01fb 637 di->di_flags |= cpu_to_be32(dip->i_diskflags &
568f4c96 638 GFS2_DIF_INHERIT_JDATA);
b3b94faa
DT
639 }
640
b96ca4fa 641 di->__pad1 = 0;
a9583c79 642 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
294caaa3 643 di->di_height = 0;
b96ca4fa
SW
644 di->__pad2 = 0;
645 di->__pad3 = 0;
294caaa3
SW
646 di->di_depth = 0;
647 di->di_entries = 0;
b96ca4fa 648 memset(&di->__pad4, 0, sizeof(di->__pad4));
294caaa3 649 di->di_eattr = 0;
4bd91ba1
SW
650 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
651 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
652 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
b96ca4fa 653 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
e9bd2b3b
WC
654
655 set_buffer_uptodate(dibh);
b96ca4fa 656
e9bd2b3b 657 *bhp = dibh;
b3b94faa
DT
658}
659
660static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
629a21e7 661 unsigned int mode, const struct gfs2_inum_host *inum,
e9bd2b3b 662 const u64 *generation, dev_t dev, struct buffer_head **bhp)
b3b94faa 663{
feaa7bba 664 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
b3b94faa
DT
665 unsigned int uid, gid;
666 int error;
667
668 munge_mode_uid_gid(dip, &mode, &uid, &gid);
182fe5ab
CG
669 if (!gfs2_alloc_get(dip))
670 return -ENOMEM;
b3b94faa
DT
671
672 error = gfs2_quota_lock(dip, uid, gid);
673 if (error)
674 goto out;
675
676 error = gfs2_quota_check(dip, uid, gid);
677 if (error)
678 goto out_quota;
679
feaa7bba 680 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
b3b94faa
DT
681 if (error)
682 goto out_quota;
683
e9bd2b3b 684 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
b3b94faa 685 gfs2_quota_change(dip, +1, uid, gid);
b3b94faa
DT
686 gfs2_trans_end(sdp);
687
feaa7bba 688out_quota:
b3b94faa 689 gfs2_quota_unlock(dip);
feaa7bba 690out:
b3b94faa 691 gfs2_alloc_put(dip);
b3b94faa
DT
692 return error;
693}
694
feaa7bba
SW
695static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
696 struct gfs2_inode *ip)
b3b94faa 697{
feaa7bba 698 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
b3b94faa
DT
699 struct gfs2_alloc *al;
700 int alloc_required;
701 struct buffer_head *dibh;
702 int error;
703
704 al = gfs2_alloc_get(dip);
182fe5ab
CG
705 if (!al)
706 return -ENOMEM;
b3b94faa
DT
707
708 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
709 if (error)
710 goto fail;
711
feaa7bba 712 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
c752666c 713 if (alloc_required < 0)
1b8177ec 714 goto fail_quota_locks;
b3b94faa 715 if (alloc_required) {
2933f925 716 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
b3b94faa
DT
717 if (error)
718 goto fail_quota_locks;
719
720 al->al_requested = sdp->sd_max_dirres;
721
722 error = gfs2_inplace_reserve(dip);
723 if (error)
724 goto fail_quota_locks;
725
320dd101 726 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
bb8d8a6f 727 al->al_rgd->rd_length +
907b9bce 728 2 * RES_DINODE +
b3b94faa
DT
729 RES_STATFS + RES_QUOTA, 0);
730 if (error)
731 goto fail_ipreserv;
732 } else {
feaa7bba 733 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
b3b94faa
DT
734 if (error)
735 goto fail_quota_locks;
736 }
737
dbb7cae2 738 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
b3b94faa
DT
739 if (error)
740 goto fail_end_trans;
741
742 error = gfs2_meta_inode_buffer(ip, &dibh);
743 if (error)
744 goto fail_end_trans;
4f56110a 745 ip->i_inode.i_nlink = 1;
d4e9c4c3 746 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 747 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa 748 brelse(dibh);
b3b94faa
DT
749 return 0;
750
320dd101 751fail_end_trans:
b3b94faa
DT
752 gfs2_trans_end(sdp);
753
320dd101 754fail_ipreserv:
6dbd8224 755 if (dip->i_alloc->al_rgd)
b3b94faa
DT
756 gfs2_inplace_release(dip);
757
320dd101 758fail_quota_locks:
b3b94faa
DT
759 gfs2_quota_unlock(dip);
760
320dd101 761fail:
b3b94faa 762 gfs2_alloc_put(dip);
b3b94faa
DT
763 return error;
764}
765
2a7dba39
EP
766static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
767 const struct qstr *qstr)
fcb47e0b
RH
768{
769 int err;
770 size_t len;
771 void *value;
772 char *name;
fcb47e0b 773
2a7dba39 774 err = security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
fcb47e0b
RH
775 &name, &value, &len);
776
777 if (err) {
778 if (err == -EOPNOTSUPP)
779 return 0;
780 return err;
781 }
782
431547b3
CH
783 err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
784 GFS2_EATYPE_SECURITY);
fcb47e0b
RH
785 kfree(value);
786 kfree(name);
787
788 return err;
789}
790
b3b94faa
DT
791/**
792 * gfs2_createi - Create a new inode
793 * @ghs: An array of two holders
794 * @name: The name of the new file
795 * @mode: the permissions on the new inode
796 *
797 * @ghs[0] is an initialized holder for the directory
798 * @ghs[1] is the holder for the inode lock
799 *
7359a19c 800 * If the return value is not NULL, the glocks on both the directory and the new
b3b94faa
DT
801 * file are held. A transaction has been started and an inplace reservation
802 * is held, as well.
803 *
7359a19c 804 * Returns: An inode
b3b94faa
DT
805 */
806
feaa7bba 807struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
e7f14f4d 808 unsigned int mode, dev_t dev)
b3b94faa 809{
e1cc8603 810 struct inode *inode = NULL;
5c676f6d 811 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
feaa7bba
SW
812 struct inode *dir = &dip->i_inode;
813 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
dbb7cae2 814 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
b3b94faa 815 int error;
4340fe62 816 u64 generation;
f91a0d3e 817 struct buffer_head *bh = NULL;
b3b94faa
DT
818
819 if (!name->len || name->len > GFS2_FNAMESIZE)
7359a19c 820 return ERR_PTR(-ENAMETOOLONG);
b3b94faa 821
b3b94faa
DT
822 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
823 error = gfs2_glock_nq(ghs);
824 if (error)
825 goto fail;
826
827 error = create_ok(dip, name, mode);
828 if (error)
829 goto fail_gunlock;
830
dbb7cae2 831 error = alloc_dinode(dip, &inum.no_addr, &generation);
b3b94faa
DT
832 if (error)
833 goto fail_gunlock;
8d8291ae 834 inum.no_formal_ino = generation;
b3b94faa 835
28626e20
SW
836 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
837 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
838 if (error)
839 goto fail_gunlock;
b3b94faa 840
e9bd2b3b 841 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
b3b94faa
DT
842 if (error)
843 goto fail_gunlock2;
844
8d8291ae 845 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
1a0eae88 846 inum.no_formal_ino);
feaa7bba 847 if (IS_ERR(inode))
b3b94faa
DT
848 goto fail_gunlock2;
849
feaa7bba 850 error = gfs2_inode_refresh(GFS2_I(inode));
b3b94faa 851 if (error)
e1cc8603 852 goto fail_gunlock2;
b3b94faa 853
479c427d 854 error = gfs2_acl_create(dip, inode);
b3b94faa 855 if (error)
e1cc8603 856 goto fail_gunlock2;
b3b94faa 857
2a7dba39 858 error = gfs2_security_init(dip, GFS2_I(inode), name);
fcb47e0b 859 if (error)
e1cc8603 860 goto fail_gunlock2;
fcb47e0b 861
feaa7bba 862 error = link_dinode(dip, name, GFS2_I(inode));
b3b94faa 863 if (error)
e1cc8603 864 goto fail_gunlock2;
b3b94faa 865
f91a0d3e
SW
866 if (bh)
867 brelse(bh);
7359a19c 868 return inode;
b3b94faa 869
320dd101 870fail_gunlock2:
b3b94faa 871 gfs2_glock_dq_uninit(ghs + 1);
bd1eb881 872 if (inode && !IS_ERR(inode))
e1cc8603 873 iput(inode);
320dd101 874fail_gunlock:
b3b94faa 875 gfs2_glock_dq(ghs);
320dd101 876fail:
f91a0d3e
SW
877 if (bh)
878 brelse(bh);
7359a19c 879 return ERR_PTR(error);
b3b94faa
DT
880}
881
536baf02 882static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
b3b94faa 883{
1025774c 884 struct inode *inode = &ip->i_inode;
b3b94faa
DT
885 struct buffer_head *dibh;
886 int error;
887
888 error = gfs2_meta_inode_buffer(ip, &dibh);
1025774c
CH
889 if (error)
890 return error;
891
1025774c
CH
892 setattr_copy(inode, attr);
893 mark_inode_dirty(inode);
1025774c
CH
894 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
895 gfs2_dinode_out(ip, dibh->b_data);
896 brelse(dibh);
897 return 0;
b3b94faa
DT
898}
899
900/**
901 * gfs2_setattr_simple -
902 * @ip:
903 * @attr:
904 *
905 * Called with a reference on the vnode.
906 *
907 * Returns: errno
908 */
909
910int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
911{
912 int error;
913
5c676f6d 914 if (current->journal_info)
b3b94faa
DT
915 return __gfs2_setattr_simple(ip, attr);
916
feaa7bba 917 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
b3b94faa
DT
918 if (error)
919 return error;
920
921 error = __gfs2_setattr_simple(ip, attr);
feaa7bba 922 gfs2_trans_end(GFS2_SB(&ip->i_inode));
b3b94faa
DT
923 return error;
924}
925
bb8d8a6f
SW
926void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
927{
bb8d8a6f
SW
928 struct gfs2_dinode *str = buf;
929
930 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
931 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
bb8d8a6f 932 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
bb8d8a6f
SW
933 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
934 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
935 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
936 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
937 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
938 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
a2e0f799 939 str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
77658aad 940 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
bb8d8a6f
SW
941 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
942 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
943 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
944
ce276b06
SW
945 str->di_goal_meta = cpu_to_be64(ip->i_goal);
946 str->di_goal_data = cpu_to_be64(ip->i_goal);
bcf0b5b3 947 str->di_generation = cpu_to_be64(ip->i_generation);
bb8d8a6f 948
383f01fb 949 str->di_flags = cpu_to_be32(ip->i_diskflags);
ecc30c79 950 str->di_height = cpu_to_be16(ip->i_height);
bb8d8a6f 951 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
383f01fb 952 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
bb8d8a6f 953 GFS2_FORMAT_DE : 0);
9a004508 954 str->di_depth = cpu_to_be16(ip->i_depth);
ad6203f2 955 str->di_entries = cpu_to_be32(ip->i_entries);
bb8d8a6f 956
3767ac21 957 str->di_eattr = cpu_to_be64(ip->i_eattr);
4bd91ba1
SW
958 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
959 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
960 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
bb8d8a6f
SW
961}
962
963void gfs2_dinode_print(const struct gfs2_inode *ip)
964{
bb8d8a6f
SW
965 printk(KERN_INFO " no_formal_ino = %llu\n",
966 (unsigned long long)ip->i_no_formal_ino);
967 printk(KERN_INFO " no_addr = %llu\n",
968 (unsigned long long)ip->i_no_addr);
a2e0f799
SW
969 printk(KERN_INFO " i_size = %llu\n",
970 (unsigned long long)i_size_read(&ip->i_inode));
77658aad
SW
971 printk(KERN_INFO " blocks = %llu\n",
972 (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
ce276b06
SW
973 printk(KERN_INFO " i_goal = %llu\n",
974 (unsigned long long)ip->i_goal);
383f01fb 975 printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
ca390601 976 printk(KERN_INFO " i_height = %u\n", ip->i_height);
9a004508 977 printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
ad6203f2 978 printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
3767ac21
SW
979 printk(KERN_INFO " i_eattr = %llu\n",
980 (unsigned long long)ip->i_eattr);
bb8d8a6f
SW
981}
982