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