Merge branch 'v6v7' into devel
[linux/fpc-iii.git] / fs / gfs2 / inode.c
blob7aa7d4f8984aa3271eac65e268cd4e5e0db05564
1 /*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
8 */
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>
17 #include <linux/gfs2_ondisk.h>
18 #include <linux/crc32.h>
19 #include <linux/security.h>
20 #include <linux/time.h>
22 #include "gfs2.h"
23 #include "incore.h"
24 #include "acl.h"
25 #include "bmap.h"
26 #include "dir.h"
27 #include "xattr.h"
28 #include "glock.h"
29 #include "glops.h"
30 #include "inode.h"
31 #include "log.h"
32 #include "meta_io.h"
33 #include "quota.h"
34 #include "rgrp.h"
35 #include "trans.h"
36 #include "util.h"
38 struct gfs2_inum_range_host {
39 u64 ir_start;
40 u64 ir_length;
43 static int iget_test(struct inode *inode, void *opaque)
45 struct gfs2_inode *ip = GFS2_I(inode);
46 u64 *no_addr = opaque;
48 if (ip->i_no_addr == *no_addr)
49 return 1;
51 return 0;
54 static int iget_set(struct inode *inode, void *opaque)
56 struct gfs2_inode *ip = GFS2_I(inode);
57 u64 *no_addr = opaque;
59 inode->i_ino = (unsigned long)*no_addr;
60 ip->i_no_addr = *no_addr;
61 return 0;
64 struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
66 unsigned long hash = (unsigned long)no_addr;
67 return ilookup5(sb, hash, iget_test, &no_addr);
70 static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
72 unsigned long hash = (unsigned long)no_addr;
73 return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
76 /**
77 * gfs2_set_iop - Sets inode operations
78 * @inode: The inode with correct i_mode filled in
80 * GFS2 lookup code fills in vfs inode contents based on info obtained
81 * from directory entry inside gfs2_inode_lookup().
84 static void gfs2_set_iop(struct inode *inode)
86 struct gfs2_sbd *sdp = GFS2_SB(inode);
87 umode_t mode = inode->i_mode;
89 if (S_ISREG(mode)) {
90 inode->i_op = &gfs2_file_iops;
91 if (gfs2_localflocks(sdp))
92 inode->i_fop = &gfs2_file_fops_nolock;
93 else
94 inode->i_fop = &gfs2_file_fops;
95 } else if (S_ISDIR(mode)) {
96 inode->i_op = &gfs2_dir_iops;
97 if (gfs2_localflocks(sdp))
98 inode->i_fop = &gfs2_dir_fops_nolock;
99 else
100 inode->i_fop = &gfs2_dir_fops;
101 } else if (S_ISLNK(mode)) {
102 inode->i_op = &gfs2_symlink_iops;
103 } else {
104 inode->i_op = &gfs2_file_iops;
105 init_special_inode(inode, inode->i_mode, inode->i_rdev);
110 * gfs2_inode_lookup - Lookup an inode
111 * @sb: The super block
112 * @no_addr: The inode number
113 * @type: The type of the inode
115 * Returns: A VFS inode, or an error
118 struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
119 u64 no_addr, u64 no_formal_ino)
121 struct inode *inode;
122 struct gfs2_inode *ip;
123 struct gfs2_glock *io_gl = NULL;
124 int error;
126 inode = gfs2_iget(sb, no_addr);
127 ip = GFS2_I(inode);
129 if (!inode)
130 return ERR_PTR(-ENOBUFS);
132 if (inode->i_state & I_NEW) {
133 struct gfs2_sbd *sdp = GFS2_SB(inode);
134 ip->i_no_formal_ino = no_formal_ino;
136 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
137 if (unlikely(error))
138 goto fail;
139 ip->i_gl->gl_object = ip;
141 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
142 if (unlikely(error))
143 goto fail_put;
145 set_bit(GIF_INVALID, &ip->i_flags);
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;
150 ip->i_iopen_gh.gh_gl->gl_object = ip;
151 gfs2_glock_put(io_gl);
152 io_gl = NULL;
154 if (type == DT_UNKNOWN) {
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);
163 gfs2_set_iop(inode);
164 unlock_new_inode(inode);
167 return inode;
169 fail_refresh:
170 ip->i_iopen_gh.gh_gl->gl_object = NULL;
171 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
172 fail_iopen:
173 if (io_gl)
174 gfs2_glock_put(io_gl);
175 fail_put:
176 ip->i_gl->gl_object = NULL;
177 gfs2_glock_put(ip->i_gl);
178 fail:
179 iget_failed(inode);
180 return ERR_PTR(error);
183 struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
184 u64 *no_formal_ino, unsigned int blktype)
186 struct super_block *sb = sdp->sd_vfs;
187 struct gfs2_holder i_gh;
188 struct inode *inode;
189 int error;
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);
196 error = gfs2_check_blk_type(sdp, no_addr, blktype);
197 if (error)
198 goto fail;
200 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0);
201 if (IS_ERR(inode))
202 goto fail;
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;
210 error = -EIO;
211 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
212 goto fail_iput;
214 error = 0;
217 fail:
218 gfs2_glock_dq_uninit(&i_gh);
219 return error ? ERR_PTR(error) : inode;
220 fail_iput:
221 iput(inode);
222 goto fail;
225 static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
227 const struct gfs2_dinode *str = buf;
228 struct timespec atime;
229 u16 height, depth;
231 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
232 goto corrupt;
233 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
234 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
235 ip->i_inode.i_rdev = 0;
236 switch (ip->i_inode.i_mode & S_IFMT) {
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;
244 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
245 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
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.
251 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
252 i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
253 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
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;
258 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
259 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
260 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
261 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
263 ip->i_goal = be64_to_cpu(str->di_goal_meta);
264 ip->i_generation = be64_to_cpu(str->di_generation);
266 ip->i_diskflags = be32_to_cpu(str->di_flags);
267 gfs2_set_inode_flags(&ip->i_inode);
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;
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;
277 ip->i_entries = be32_to_cpu(str->di_entries);
279 ip->i_eattr = be64_to_cpu(str->di_eattr);
280 if (S_ISREG(ip->i_inode.i_mode))
281 gfs2_set_aops(&ip->i_inode);
283 return 0;
284 corrupt:
285 if (gfs2_consist_inode(ip))
286 gfs2_dinode_print(ip);
287 return -EIO;
291 * gfs2_inode_refresh - Refresh the incore copy of the dinode
292 * @ip: The GFS2 inode
294 * Returns: errno
297 int gfs2_inode_refresh(struct gfs2_inode *ip)
299 struct buffer_head *dibh;
300 int error;
302 error = gfs2_meta_inode_buffer(ip, &dibh);
303 if (error)
304 return error;
306 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
307 brelse(dibh);
308 return -EIO;
311 error = gfs2_dinode_in(ip, dibh->b_data);
312 brelse(dibh);
313 clear_bit(GIF_INVALID, &ip->i_flags);
315 return error;
318 int gfs2_dinode_dealloc(struct gfs2_inode *ip)
320 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
321 struct gfs2_alloc *al;
322 struct gfs2_rgrpd *rgd;
323 int error;
325 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
326 if (gfs2_consist_inode(ip))
327 gfs2_dinode_print(ip);
328 return -EIO;
331 al = gfs2_alloc_get(ip);
332 if (!al)
333 return -ENOMEM;
335 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
336 if (error)
337 goto out;
339 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
340 if (error)
341 goto out_qs;
343 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
344 if (!rgd) {
345 gfs2_consist_inode(ip);
346 error = -EIO;
347 goto out_rindex_relse;
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;
355 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
356 if (error)
357 goto out_rg_gunlock;
359 set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
360 set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
362 gfs2_free_di(rgd, ip);
364 gfs2_trans_end(sdp);
366 out_rg_gunlock:
367 gfs2_glock_dq_uninit(&al->al_rgd_gh);
368 out_rindex_relse:
369 gfs2_glock_dq_uninit(&al->al_ri_gh);
370 out_qs:
371 gfs2_quota_unhold(ip);
372 out:
373 gfs2_alloc_put(ip);
374 return error;
378 * gfs2_change_nlink - Change nlink count on inode
379 * @ip: The GFS2 inode
380 * @diff: The change in the nlink count required
382 * Returns: errno
384 int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
386 struct buffer_head *dibh;
387 u32 nlink;
388 int error;
390 BUG_ON(diff != 1 && diff != -1);
391 nlink = ip->i_inode.i_nlink + diff;
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. */
395 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
396 if (gfs2_consist_inode(ip))
397 gfs2_dinode_print(ip);
398 return -EIO;
401 error = gfs2_meta_inode_buffer(ip, &dibh);
402 if (error)
403 return error;
405 if (diff > 0)
406 inc_nlink(&ip->i_inode);
407 else
408 drop_nlink(&ip->i_inode);
410 ip->i_inode.i_ctime = CURRENT_TIME;
412 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
413 gfs2_dinode_out(ip, dibh->b_data);
414 brelse(dibh);
415 mark_inode_dirty(&ip->i_inode);
417 if (ip->i_inode.i_nlink == 0)
418 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
420 return error;
423 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
425 struct qstr qstr;
426 struct inode *inode;
427 gfs2_str2qstr(&qstr, name);
428 inode = gfs2_lookupi(dip, &qstr, 1);
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.
434 if (inode == NULL)
435 return ERR_PTR(-ENOENT);
436 else
437 return inode;
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
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.
453 * Returns: errno
456 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
457 int is_root)
459 struct super_block *sb = dir->i_sb;
460 struct gfs2_inode *dip = GFS2_I(dir);
461 struct gfs2_holder d_gh;
462 int error = 0;
463 struct inode *inode = NULL;
464 int unlock = 0;
466 if (!name->len || name->len > GFS2_FNAMESIZE)
467 return ERR_PTR(-ENAMETOOLONG);
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)) {
472 igrab(dir);
473 return dir;
476 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
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;
483 if (!is_root) {
484 error = gfs2_permission(dir, MAY_EXEC, 0);
485 if (error)
486 goto out;
489 inode = gfs2_dir_search(dir, name);
490 if (IS_ERR(inode))
491 error = PTR_ERR(inode);
492 out:
493 if (unlock)
494 gfs2_glock_dq_uninit(&d_gh);
495 if (error == -ENOENT)
496 return NULL;
497 return inode ? inode : ERR_PTR(error);
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:
506 * Returns: errno
509 static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
510 unsigned int mode)
512 int error;
514 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
515 if (error)
516 return error;
518 /* Don't create entries in an unlinked directory */
519 if (!dip->i_inode.i_nlink)
520 return -EPERM;
522 error = gfs2_dir_check(&dip->i_inode, name, NULL);
523 switch (error) {
524 case -ENOENT:
525 error = 0;
526 break;
527 case 0:
528 return -EEXIST;
529 default:
530 return error;
533 if (dip->i_entries == (u32)-1)
534 return -EFBIG;
535 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
536 return -EMLINK;
538 return 0;
541 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
542 unsigned int *uid, unsigned int *gid)
544 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
545 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
546 if (S_ISDIR(*mode))
547 *mode |= S_ISUID;
548 else if (dip->i_inode.i_uid != current_fsuid())
549 *mode &= ~07111;
550 *uid = dip->i_inode.i_uid;
551 } else
552 *uid = current_fsuid();
554 if (dip->i_inode.i_mode & S_ISGID) {
555 if (S_ISDIR(*mode))
556 *mode |= S_ISGID;
557 *gid = dip->i_inode.i_gid;
558 } else
559 *gid = current_fsgid();
562 static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
564 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
565 int error;
567 if (gfs2_alloc_get(dip) == NULL)
568 return -ENOMEM;
570 dip->i_alloc->al_requested = RES_DINODE;
571 error = gfs2_inplace_reserve(dip);
572 if (error)
573 goto out;
575 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
576 if (error)
577 goto out_ipreserv;
579 error = gfs2_alloc_di(dip, no_addr, generation);
581 gfs2_trans_end(sdp);
583 out_ipreserv:
584 gfs2_inplace_release(dip);
585 out:
586 gfs2_alloc_put(dip);
587 return error;
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:
601 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
602 const struct gfs2_inum_host *inum, unsigned int mode,
603 unsigned int uid, unsigned int gid,
604 const u64 *generation, dev_t dev, struct buffer_head **bhp)
606 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
607 struct gfs2_dinode *di;
608 struct buffer_head *dibh;
609 struct timespec tv = CURRENT_TIME;
611 dibh = gfs2_meta_new(gl, inum->no_addr);
612 gfs2_trans_add_bh(gl, dibh, 1);
613 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
614 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
615 di = (struct gfs2_dinode *)dibh->b_data;
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);
619 di->di_mode = cpu_to_be32(mode);
620 di->di_uid = cpu_to_be32(uid);
621 di->di_gid = cpu_to_be32(gid);
622 di->di_nlink = 0;
623 di->di_size = 0;
624 di->di_blocks = cpu_to_be64(1);
625 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
626 di->di_major = cpu_to_be32(MAJOR(dev));
627 di->di_minor = cpu_to_be32(MINOR(dev));
628 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
629 di->di_generation = cpu_to_be64(*generation);
630 di->di_flags = 0;
632 if (S_ISREG(mode)) {
633 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
634 gfs2_tune_get(sdp, gt_new_files_jdata))
635 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
636 } else if (S_ISDIR(mode)) {
637 di->di_flags |= cpu_to_be32(dip->i_diskflags &
638 GFS2_DIF_INHERIT_JDATA);
641 di->__pad1 = 0;
642 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
643 di->di_height = 0;
644 di->__pad2 = 0;
645 di->__pad3 = 0;
646 di->di_depth = 0;
647 di->di_entries = 0;
648 memset(&di->__pad4, 0, sizeof(di->__pad4));
649 di->di_eattr = 0;
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);
653 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
655 set_buffer_uptodate(dibh);
657 *bhp = dibh;
660 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
661 unsigned int mode, const struct gfs2_inum_host *inum,
662 const u64 *generation, dev_t dev, struct buffer_head **bhp)
664 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
665 unsigned int uid, gid;
666 int error;
668 munge_mode_uid_gid(dip, &mode, &uid, &gid);
669 if (!gfs2_alloc_get(dip))
670 return -ENOMEM;
672 error = gfs2_quota_lock(dip, uid, gid);
673 if (error)
674 goto out;
676 error = gfs2_quota_check(dip, uid, gid);
677 if (error)
678 goto out_quota;
680 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
681 if (error)
682 goto out_quota;
684 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
685 gfs2_quota_change(dip, +1, uid, gid);
686 gfs2_trans_end(sdp);
688 out_quota:
689 gfs2_quota_unlock(dip);
690 out:
691 gfs2_alloc_put(dip);
692 return error;
695 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
696 struct gfs2_inode *ip)
698 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
699 struct gfs2_alloc *al;
700 int alloc_required;
701 struct buffer_head *dibh;
702 int error;
704 al = gfs2_alloc_get(dip);
705 if (!al)
706 return -ENOMEM;
708 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
709 if (error)
710 goto fail;
712 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
713 if (alloc_required < 0)
714 goto fail_quota_locks;
715 if (alloc_required) {
716 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
717 if (error)
718 goto fail_quota_locks;
720 al->al_requested = sdp->sd_max_dirres;
722 error = gfs2_inplace_reserve(dip);
723 if (error)
724 goto fail_quota_locks;
726 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
727 al->al_rgd->rd_length +
728 2 * RES_DINODE +
729 RES_STATFS + RES_QUOTA, 0);
730 if (error)
731 goto fail_ipreserv;
732 } else {
733 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
734 if (error)
735 goto fail_quota_locks;
738 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
739 if (error)
740 goto fail_end_trans;
742 error = gfs2_meta_inode_buffer(ip, &dibh);
743 if (error)
744 goto fail_end_trans;
745 ip->i_inode.i_nlink = 1;
746 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
747 gfs2_dinode_out(ip, dibh->b_data);
748 brelse(dibh);
749 return 0;
751 fail_end_trans:
752 gfs2_trans_end(sdp);
754 fail_ipreserv:
755 if (dip->i_alloc->al_rgd)
756 gfs2_inplace_release(dip);
758 fail_quota_locks:
759 gfs2_quota_unlock(dip);
761 fail:
762 gfs2_alloc_put(dip);
763 return error;
766 static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
768 int err;
769 size_t len;
770 void *value;
771 char *name;
773 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
774 &name, &value, &len);
776 if (err) {
777 if (err == -EOPNOTSUPP)
778 return 0;
779 return err;
782 err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
783 GFS2_EATYPE_SECURITY);
784 kfree(value);
785 kfree(name);
787 return err;
791 * gfs2_createi - Create a new inode
792 * @ghs: An array of two holders
793 * @name: The name of the new file
794 * @mode: the permissions on the new inode
796 * @ghs[0] is an initialized holder for the directory
797 * @ghs[1] is the holder for the inode lock
799 * If the return value is not NULL, the glocks on both the directory and the new
800 * file are held. A transaction has been started and an inplace reservation
801 * is held, as well.
803 * Returns: An inode
806 struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
807 unsigned int mode, dev_t dev)
809 struct inode *inode = NULL;
810 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
811 struct inode *dir = &dip->i_inode;
812 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
813 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
814 int error;
815 u64 generation;
816 struct buffer_head *bh = NULL;
818 if (!name->len || name->len > GFS2_FNAMESIZE)
819 return ERR_PTR(-ENAMETOOLONG);
821 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
822 error = gfs2_glock_nq(ghs);
823 if (error)
824 goto fail;
826 error = create_ok(dip, name, mode);
827 if (error)
828 goto fail_gunlock;
830 error = alloc_dinode(dip, &inum.no_addr, &generation);
831 if (error)
832 goto fail_gunlock;
833 inum.no_formal_ino = generation;
835 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
836 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
837 if (error)
838 goto fail_gunlock;
840 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
841 if (error)
842 goto fail_gunlock2;
844 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
845 inum.no_formal_ino);
846 if (IS_ERR(inode))
847 goto fail_gunlock2;
849 error = gfs2_inode_refresh(GFS2_I(inode));
850 if (error)
851 goto fail_gunlock2;
853 error = gfs2_acl_create(dip, inode);
854 if (error)
855 goto fail_gunlock2;
857 error = gfs2_security_init(dip, GFS2_I(inode));
858 if (error)
859 goto fail_gunlock2;
861 error = link_dinode(dip, name, GFS2_I(inode));
862 if (error)
863 goto fail_gunlock2;
865 if (bh)
866 brelse(bh);
867 return inode;
869 fail_gunlock2:
870 gfs2_glock_dq_uninit(ghs + 1);
871 if (inode && !IS_ERR(inode))
872 iput(inode);
873 fail_gunlock:
874 gfs2_glock_dq(ghs);
875 fail:
876 if (bh)
877 brelse(bh);
878 return ERR_PTR(error);
881 static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
883 struct inode *inode = &ip->i_inode;
884 struct buffer_head *dibh;
885 int error;
887 error = gfs2_meta_inode_buffer(ip, &dibh);
888 if (error)
889 return error;
891 setattr_copy(inode, attr);
892 mark_inode_dirty(inode);
893 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
894 gfs2_dinode_out(ip, dibh->b_data);
895 brelse(dibh);
896 return 0;
900 * gfs2_setattr_simple -
901 * @ip:
902 * @attr:
904 * Called with a reference on the vnode.
906 * Returns: errno
909 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
911 int error;
913 if (current->journal_info)
914 return __gfs2_setattr_simple(ip, attr);
916 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
917 if (error)
918 return error;
920 error = __gfs2_setattr_simple(ip, attr);
921 gfs2_trans_end(GFS2_SB(&ip->i_inode));
922 return error;
925 void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
927 struct gfs2_dinode *str = buf;
929 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
930 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
931 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
932 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
933 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
934 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
935 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
936 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
937 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
938 str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
939 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
940 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
941 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
942 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
944 str->di_goal_meta = cpu_to_be64(ip->i_goal);
945 str->di_goal_data = cpu_to_be64(ip->i_goal);
946 str->di_generation = cpu_to_be64(ip->i_generation);
948 str->di_flags = cpu_to_be32(ip->i_diskflags);
949 str->di_height = cpu_to_be16(ip->i_height);
950 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
951 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
952 GFS2_FORMAT_DE : 0);
953 str->di_depth = cpu_to_be16(ip->i_depth);
954 str->di_entries = cpu_to_be32(ip->i_entries);
956 str->di_eattr = cpu_to_be64(ip->i_eattr);
957 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
958 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
959 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
962 void gfs2_dinode_print(const struct gfs2_inode *ip)
964 printk(KERN_INFO " no_formal_ino = %llu\n",
965 (unsigned long long)ip->i_no_formal_ino);
966 printk(KERN_INFO " no_addr = %llu\n",
967 (unsigned long long)ip->i_no_addr);
968 printk(KERN_INFO " i_size = %llu\n",
969 (unsigned long long)i_size_read(&ip->i_inode));
970 printk(KERN_INFO " blocks = %llu\n",
971 (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
972 printk(KERN_INFO " i_goal = %llu\n",
973 (unsigned long long)ip->i_goal);
974 printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
975 printk(KERN_INFO " i_height = %u\n", ip->i_height);
976 printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
977 printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
978 printk(KERN_INFO " i_eattr = %llu\n",
979 (unsigned long long)ip->i_eattr);