blob: 7aa7d4f8984aa3271eac65e268cd4e5e0db05564 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Petersonca390602008-01-28 11:15:57 -06003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050017#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050018#include <linux/crc32.h>
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -040019#include <linux/security.h>
Steven Whitehouse719ee342008-09-18 13:53:59 +010020#include <linux/time.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000021
22#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050023#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include "acl.h"
25#include "bmap.h"
26#include "dir.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010027#include "xattr.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028#include "glock.h"
29#include "glops.h"
30#include "inode.h"
31#include "log.h"
32#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000033#include "quota.h"
34#include "rgrp.h"
35#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050036#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000037
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010038struct gfs2_inum_range_host {
39 u64 ir_start;
40 u64 ir_length;
41};
42
David Teiglandb3b94fa2006-01-16 16:50:04 +000043static int iget_test(struct inode *inode, void *opaque)
44{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040045 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010046 u64 *no_addr = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +000047
Steven Whitehouse009d8512009-12-08 12:12:13 +000048 if (ip->i_no_addr == *no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000049 return 1;
50
51 return 0;
52}
53
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040054static int iget_set(struct inode *inode, void *opaque)
55{
56 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010057 u64 *no_addr = opaque;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040058
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010059 inode->i_ino = (unsigned long)*no_addr;
60 ip->i_no_addr = *no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040061 return 0;
62}
63
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010064struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000065{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010066 unsigned long hash = (unsigned long)no_addr;
67 return ilookup5(sb, hash, iget_test, &no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +000068}
69
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010070static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000071{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010072 unsigned long hash = (unsigned long)no_addr;
73 return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040074}
75
76/**
Steven Whitehouse24d97652011-01-18 14:49:08 +000077 * gfs2_set_iop - Sets inode operations
78 * @inode: The inode with correct i_mode filled in
Wendy Cheng35dcc522007-06-27 17:07:53 -040079 *
Steven Whitehouse24d97652011-01-18 14:49:08 +000080 * GFS2 lookup code fills in vfs inode contents based on info obtained
81 * from directory entry inside gfs2_inode_lookup().
82 */
Wendy Cheng35dcc522007-06-27 17:07:53 -040083
Steven Whitehouse24d97652011-01-18 14:49:08 +000084static void gfs2_set_iop(struct inode *inode)
Wendy Cheng35dcc522007-06-27 17:07:53 -040085{
Wendy Chengc97bfe42007-11-29 17:56:51 -050086 struct gfs2_sbd *sdp = GFS2_SB(inode);
Wendy Cheng35dcc522007-06-27 17:07:53 -040087 umode_t mode = inode->i_mode;
88
89 if (S_ISREG(mode)) {
90 inode->i_op = &gfs2_file_iops;
Steven Whitehousef057f6c2009-01-12 10:43:39 +000091 if (gfs2_localflocks(sdp))
Christoph Hellwig10d21982009-04-07 19:42:17 +020092 inode->i_fop = &gfs2_file_fops_nolock;
Wendy Chengc97bfe42007-11-29 17:56:51 -050093 else
Christoph Hellwig10d21982009-04-07 19:42:17 +020094 inode->i_fop = &gfs2_file_fops;
Wendy Cheng35dcc522007-06-27 17:07:53 -040095 } else if (S_ISDIR(mode)) {
96 inode->i_op = &gfs2_dir_iops;
Steven Whitehousef057f6c2009-01-12 10:43:39 +000097 if (gfs2_localflocks(sdp))
Christoph Hellwig10d21982009-04-07 19:42:17 +020098 inode->i_fop = &gfs2_dir_fops_nolock;
Wendy Chengc97bfe42007-11-29 17:56:51 -050099 else
Christoph Hellwig10d21982009-04-07 19:42:17 +0200100 inode->i_fop = &gfs2_dir_fops;
Wendy Cheng35dcc522007-06-27 17:07:53 -0400101 } else if (S_ISLNK(mode)) {
102 inode->i_op = &gfs2_symlink_iops;
103 } else {
Denis Chengd83225d2008-02-26 15:25:03 +0800104 inode->i_op = &gfs2_file_iops;
Denis Cheng43a33c52008-02-26 15:25:04 +0800105 init_special_inode(inode, inode->i_mode, inode->i_rdev);
Wendy Cheng35dcc522007-06-27 17:07:53 -0400106 }
Wendy Cheng35dcc522007-06-27 17:07:53 -0400107}
108
109/**
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400110 * gfs2_inode_lookup - Lookup an inode
111 * @sb: The super block
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100112 * @no_addr: The inode number
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400113 * @type: The type of the inode
114 *
115 * Returns: A VFS inode, or an error
116 */
117
Steven Whitehouse24d97652011-01-18 14:49:08 +0000118struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
119 u64 no_addr, u64 no_formal_ino)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400120{
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500121 struct inode *inode;
122 struct gfs2_inode *ip;
Bob Petersonb1becbd2010-06-17 16:45:37 -0400123 struct gfs2_glock *io_gl = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400124 int error;
125
Bob Peterson1a0eae82010-04-14 11:58:16 -0400126 inode = gfs2_iget(sb, no_addr);
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500127 ip = GFS2_I(inode);
128
Steven Whitehouse26d83de2006-10-30 16:59:08 -0500129 if (!inode)
130 return ERR_PTR(-ENOBUFS);
131
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400132 if (inode->i_state & I_NEW) {
133 struct gfs2_sbd *sdp = GFS2_SB(inode);
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400134 ip->i_no_formal_ino = no_formal_ino;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400135
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100136 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400137 if (unlikely(error))
138 goto fail;
139 ip->i_gl->gl_object = ip;
140
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100141 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400142 if (unlikely(error))
143 goto fail_put;
144
Steven Whitehousebfded272006-11-01 16:05:38 -0500145 set_bit(GIF_INVALID, &ip->i_flags);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400146 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
147 if (unlikely(error))
148 goto fail_iopen;
149
Steven Whitehouse24d97652011-01-18 14:49:08 +0000150 ip->i_iopen_gh.gh_gl->gl_object = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400151 gfs2_glock_put(io_gl);
Bob Petersonb1becbd2010-06-17 16:45:37 -0400152 io_gl = NULL;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100153
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100154 if (type == DT_UNKNOWN) {
Steven Whitehouse24d97652011-01-18 14:49:08 +0000155 /* 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);
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100161 }
162
Wendy Cheng35dcc522007-06-27 17:07:53 -0400163 gfs2_set_iop(inode);
Steven Whitehouse24d97652011-01-18 14:49:08 +0000164 unlock_new_inode(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000165 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400166
167 return inode;
Steven Whitehouse24d97652011-01-18 14:49:08 +0000168
169fail_refresh:
170 ip->i_iopen_gh.gh_gl->gl_object = NULL;
171 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400172fail_iopen:
Bob Petersonb1becbd2010-06-17 16:45:37 -0400173 if (io_gl)
174 gfs2_glock_put(io_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400175fail_put:
Steven Whitehouse24d97652011-01-18 14:49:08 +0000176 ip->i_gl->gl_object = NULL;
Bob Peterson1a0eae82010-04-14 11:58:16 -0400177 gfs2_glock_put(ip->i_gl);
178fail:
Steven Whitehouse24d97652011-01-18 14:49:08 +0000179 iget_failed(inode);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400180 return ERR_PTR(error);
181}
182
Steven Whitehouse044b9412010-11-03 20:01:07 +0000183struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
184 u64 *no_formal_ino, unsigned int blktype)
Bob Peterson1a0eae82010-04-14 11:58:16 -0400185{
Steven Whitehouse044b9412010-11-03 20:01:07 +0000186 struct super_block *sb = sdp->sd_vfs;
187 struct gfs2_holder i_gh;
Bob Petersoned4878e2010-05-20 23:30:11 -0400188 struct inode *inode;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000189 int error;
Bob Peterson1a0eae82010-04-14 11:58:16 -0400190
Steven Whitehouse044b9412010-11-03 20:01:07 +0000191 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);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400195
Steven Whitehouse044b9412010-11-03 20:01:07 +0000196 error = gfs2_check_blk_type(sdp, no_addr, blktype);
197 if (error)
198 goto fail;
Bob Peterson1a0eae82010-04-14 11:58:16 -0400199
Steven Whitehouse044b9412010-11-03 20:01:07 +0000200 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0);
201 if (IS_ERR(inode))
202 goto fail;
203
Steven Whitehouse044b9412010-11-03 20:01:07 +0000204 /* 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;
209
210 error = -EIO;
211 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
212 goto fail_iput;
213
214 error = 0;
Bob Petersoned4878e2010-05-20 23:30:11 -0400215 }
Bob Peterson1a0eae82010-04-14 11:58:16 -0400216
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400217fail:
Steven Whitehouse044b9412010-11-03 20:01:07 +0000218 gfs2_glock_dq_uninit(&i_gh);
219 return error ? ERR_PTR(error) : inode;
220fail_iput:
221 iput(inode);
222 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223}
224
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500225static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
Steven Whitehouseea744d02006-10-31 15:28:00 -0500226{
Steven Whitehouseea744d02006-10-31 15:28:00 -0500227 const struct gfs2_dinode *str = buf;
Steven Whitehouse719ee342008-09-18 13:53:59 +0100228 struct timespec atime;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000229 u16 height, depth;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500230
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000231 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
232 goto corrupt;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100233 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500234 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500235 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500236 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500237 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
Steven Whitehouse2933f922006-11-01 13:23:29 -0500244 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
245 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
Steven Whitehouse4f561102006-11-01 14:04:17 -0500246 /*
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);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100252 i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000253 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
Steven Whitehouse719ee342008-09-18 13:53:59 +0100254 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;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500258 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100259 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500260 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100261 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500262
Steven Whitehousece276b02008-02-06 09:25:45 +0000263 ip->i_goal = be64_to_cpu(str->di_goal_meta);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +0000264 ip->i_generation = be64_to_cpu(str->di_generation);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500265
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000266 ip->i_diskflags = be32_to_cpu(str->di_flags);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500267 gfs2_set_inode_flags(&ip->i_inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000268 height = be16_to_cpu(str->di_height);
269 if (unlikely(height > GFS2_MAX_META_HEIGHT))
270 goto corrupt;
271 ip->i_height = (u8)height;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500272
Steven Whitehouse9a004502008-02-01 09:23:44 +0000273 depth = be16_to_cpu(str->di_depth);
274 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
275 goto corrupt;
276 ip->i_depth = (u8)depth;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000277 ip->i_entries = be32_to_cpu(str->di_entries);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500278
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000279 ip->i_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouse55610932007-10-17 08:47:38 +0100280 if (S_ISREG(ip->i_inode.i_mode))
281 gfs2_set_aops(&ip->i_inode);
282
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500283 return 0;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000284corrupt:
285 if (gfs2_consist_inode(ip))
286 gfs2_dinode_print(ip);
287 return -EIO;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500288}
289
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290/**
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
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400306 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307 brelse(dibh);
308 return -EIO;
309 }
310
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500311 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000312 brelse(dibh);
Steven Whitehousebfded272006-11-01 16:05:38 -0500313 clear_bit(GIF_INVALID, &ip->i_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500315 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316}
317
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400318int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400320 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321 struct gfs2_alloc *al;
322 struct gfs2_rgrpd *rgd;
323 int error;
324
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000325 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000326 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500327 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328 return -EIO;
329 }
330
331 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300332 if (!al)
333 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334
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
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100343 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344 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
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400355 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 if (error)
357 goto out_rg_gunlock;
358
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000359 set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
360 set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361
362 gfs2_free_di(rgd, ip);
363
David Teiglandb3b94fa2006-01-16 16:50:04 +0000364 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000365
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400366out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400368out_rindex_relse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400370out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371 gfs2_quota_unhold(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400372out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400373 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374 return error;
375}
376
377/**
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500378 * gfs2_change_nlink - Change nlink count on inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000379 * @ip: The GFS2 inode
380 * @diff: The change in the nlink count required
381 *
382 * Returns: errno
383 */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500384int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385{
386 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400387 u32 nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388 int error;
389
Steven Whitehouse4f561102006-11-01 14:04:17 -0500390 BUG_ON(diff != 1 && diff != -1);
391 nlink = ip->i_inode.i_nlink + diff;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392
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. */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500395 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000396 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500397 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398 return -EIO;
399 }
400
401 error = gfs2_meta_inode_buffer(ip, &dibh);
402 if (error)
403 return error;
404
Steven Whitehouse4f561102006-11-01 14:04:17 -0500405 if (diff > 0)
406 inc_nlink(&ip->i_inode);
407 else
408 drop_nlink(&ip->i_inode);
409
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100410 ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000412 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500413 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400415 mark_inode_dirty(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500417 if (ip->i_inode.i_nlink == 0)
Russell Cattelanddee7602007-01-29 17:13:44 -0600418 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500419
S. Wendy Cheng55098262007-01-18 15:56:34 -0500420 return error;
421}
422
Steven Whitehousec7526662006-03-20 12:30:04 -0500423struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
424{
425 struct qstr qstr;
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600426 struct inode *inode;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500427 gfs2_str2qstr(&qstr, name);
Al Viroa569c712008-07-23 14:42:05 -0400428 inode = gfs2_lookupi(dip, &qstr, 1);
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600429 /* 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;
Steven Whitehousec7526662006-03-20 12:30:04 -0500438}
439
440
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441/**
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 *
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000448 * 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.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000452 *
453 * Returns: errno
454 */
455
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400456struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
Al Viroa569c712008-07-23 14:42:05 -0400457 int is_root)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500459 struct super_block *sb = dir->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400460 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461 struct gfs2_holder d_gh;
akpm@linux-foundation.org037bcbb2007-06-08 16:42:14 -0700462 int error = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500463 struct inode *inode = NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000464 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000465
466 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500467 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468
Steven Whitehousec7526662006-03-20 12:30:04 -0500469 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)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400472 igrab(dir);
473 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000474 }
475
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000476 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000477 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 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482
483 if (!is_root) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100484 error = gfs2_permission(dir, MAY_EXEC, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000485 if (error)
486 goto out;
487 }
488
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100489 inode = gfs2_dir_search(dir, name);
490 if (IS_ERR(inode))
491 error = PTR_ERR(inode);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000492out:
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000493 if (unlock)
494 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500495 if (error == -ENOENT)
496 return NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000497 return inode ? inode : ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000498}
499
David Teiglandb3b94fa2006-01-16 16:50:04 +0000500/**
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
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400509static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000510 unsigned int mode)
511{
512 int error;
513
Nick Pigginb74c79e2011-01-07 17:49:58 +1100514 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000515 if (error)
516 return error;
517
518 /* Don't create entries in an unlinked directory */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500519 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520 return -EPERM;
521
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100522 error = gfs2_dir_check(&dip->i_inode, name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000523 switch (error) {
524 case -ENOENT:
525 error = 0;
526 break;
527 case 0:
528 return -EEXIST;
529 default:
530 return error;
531 }
532
Steven Whitehousead6203f2008-11-03 13:59:19 +0000533 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534 return -EFBIG;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500535 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400544 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500545 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546 if (S_ISDIR(*mode))
547 *mode |= S_ISUID;
David Howells3de7be32008-11-14 10:38:53 +1100548 else if (dip->i_inode.i_uid != current_fsuid())
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549 *mode &= ~07111;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500550 *uid = dip->i_inode.i_uid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551 } else
David Howells3de7be32008-11-14 10:38:53 +1100552 *uid = current_fsuid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500554 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555 if (S_ISDIR(*mode))
556 *mode |= S_ISGID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500557 *gid = dip->i_inode.i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000558 } else
David Howells3de7be32008-11-14 10:38:53 +1100559 *gid = current_fsgid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000560}
561
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100562static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400564 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 int error;
566
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000567 if (gfs2_alloc_get(dip) == NULL)
568 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000570 dip->i_alloc->al_requested = RES_DINODE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571 error = gfs2_inplace_reserve(dip);
572 if (error)
573 goto out;
574
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400575 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 if (error)
577 goto out_ipreserv;
578
Steven Whitehouse6050b9c2009-07-31 16:19:40 +0100579 error = gfs2_alloc_di(dip, no_addr, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580
581 gfs2_trans_end(sdp);
582
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400583out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400585out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 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,
Al Viro629a21e2006-10-13 22:51:24 -0400602 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400603 unsigned int uid, unsigned int gid,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400604 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400606 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000607 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608 struct buffer_head *dibh;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100609 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610
611 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000612 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
614 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000615 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616
Steven Whitehouse2442a092006-01-30 11:49:32 +0000617 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);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000619 di->di_mode = cpu_to_be32(mode);
620 di->di_uid = cpu_to_be32(uid);
621 di->di_gid = cpu_to_be32(gid);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500622 di->di_nlink = 0;
623 di->di_size = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000624 di->di_blocks = cpu_to_be64(1);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100625 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500626 di->di_major = cpu_to_be32(MAJOR(dev));
627 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000628 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400629 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500630 di->di_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000631
632 if (S_ISREG(mode)) {
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000633 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000635 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636 } else if (S_ISDIR(mode)) {
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000637 di->di_flags |= cpu_to_be32(dip->i_diskflags &
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500638 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639 }
640
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000641 di->__pad1 = 0;
Steven Whitehousea9583c72006-11-01 20:09:14 -0500642 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500643 di->di_height = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000644 di->__pad2 = 0;
645 di->__pad3 = 0;
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500646 di->di_depth = 0;
647 di->di_entries = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000648 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500649 di->di_eattr = 0;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100650 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);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000653 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400654
655 set_buffer_uptodate(dibh);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000656
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400657 *bhp = dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658}
659
660static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400661 unsigned int mode, const struct gfs2_inum_host *inum,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400662 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000663{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400664 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000665 unsigned int uid, gid;
666 int error;
667
668 munge_mode_uid_gid(dip, &mode, &uid, &gid);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300669 if (!gfs2_alloc_get(dip))
670 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671
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
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400680 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681 if (error)
682 goto out_quota;
683
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400684 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000686 gfs2_trans_end(sdp);
687
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400688out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400690out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000692 return error;
693}
694
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400695static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
696 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400698 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000699 struct gfs2_alloc *al;
700 int alloc_required;
701 struct buffer_head *dibh;
702 int error;
703
704 al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300705 if (!al)
706 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707
708 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
709 if (error)
710 goto fail;
711
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400712 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500713 if (alloc_required < 0)
Bob Peterson1b8177e2008-01-19 21:50:24 -0600714 goto fail_quota_locks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715 if (alloc_required) {
Steven Whitehouse2933f922006-11-01 13:23:29 -0500716 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 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
Steven Whitehouse320dd102006-05-18 16:25:27 -0400726 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100727 al->al_rgd->rd_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400728 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 RES_STATFS + RES_QUOTA, 0);
730 if (error)
731 goto fail_ipreserv;
732 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400733 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 if (error)
735 goto fail_quota_locks;
736 }
737
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100738 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000739 if (error)
740 goto fail_end_trans;
741
742 error = gfs2_meta_inode_buffer(ip, &dibh);
743 if (error)
744 goto fail_end_trans;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500745 ip->i_inode.i_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000746 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500747 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000748 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 return 0;
750
Steven Whitehouse320dd102006-05-18 16:25:27 -0400751fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752 gfs2_trans_end(sdp);
753
Steven Whitehouse320dd102006-05-18 16:25:27 -0400754fail_ipreserv:
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000755 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 gfs2_inplace_release(dip);
757
Steven Whitehouse320dd102006-05-18 16:25:27 -0400758fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 gfs2_quota_unlock(dip);
760
Steven Whitehouse320dd102006-05-18 16:25:27 -0400761fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763 return error;
764}
765
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400766static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
767{
768 int err;
769 size_t len;
770 void *value;
771 char *name;
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400772
773 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
774 &name, &value, &len);
775
776 if (err) {
777 if (err == -EOPNOTSUPP)
778 return 0;
779 return err;
780 }
781
Christoph Hellwig431547b2009-11-13 09:52:56 +0000782 err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
783 GFS2_EATYPE_SECURITY);
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400784 kfree(value);
785 kfree(name);
786
787 return err;
788}
789
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790/**
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
795 *
796 * @ghs[0] is an initialized holder for the directory
797 * @ghs[1] is the holder for the inode lock
798 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000799 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800 * file are held. A transaction has been started and an inplace reservation
801 * is held, as well.
802 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000803 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 */
805
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400806struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500807 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808{
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100809 struct inode *inode = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500810 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400811 struct inode *dir = &dip->i_inode;
812 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100813 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000814 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400815 u64 generation;
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100816 struct buffer_head *bh = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817
818 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000819 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820
David Teiglandb3b94fa2006-01-16 16:50:04 +0000821 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
822 error = gfs2_glock_nq(ghs);
823 if (error)
824 goto fail;
825
826 error = create_ok(dip, name, mode);
827 if (error)
828 goto fail_gunlock;
829
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100830 error = alloc_dinode(dip, &inum.no_addr, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 if (error)
832 goto fail_gunlock;
Steven Whitehouse8d8291a2009-08-27 15:51:07 +0100833 inum.no_formal_ino = generation;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834
Steven Whitehouse28626e22006-11-22 11:13:21 -0500835 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;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000839
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400840 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841 if (error)
842 goto fail_gunlock2;
843
Steven Whitehouse8d8291a2009-08-27 15:51:07 +0100844 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
Bob Peterson1a0eae82010-04-14 11:58:16 -0400845 inum.no_formal_ino);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400846 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000847 goto fail_gunlock2;
848
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400849 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100851 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852
Steven Whitehouse479c4272009-10-02 12:00:00 +0100853 error = gfs2_acl_create(dip, inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100855 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400857 error = gfs2_security_init(dip, GFS2_I(inode));
858 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100859 goto fail_gunlock2;
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400860
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400861 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100863 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100865 if (bh)
866 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000867 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868
Steven Whitehouse320dd102006-05-18 16:25:27 -0400869fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870 gfs2_glock_dq_uninit(ghs + 1);
Julien Brunelbd1eb882008-09-01 10:51:22 +0200871 if (inode && !IS_ERR(inode))
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100872 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400873fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400875fail:
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100876 if (bh)
877 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000878 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879}
880
Steven Whitehouse536baf02009-05-22 10:48:59 +0100881static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882{
Christoph Hellwig10257742010-06-04 11:30:02 +0200883 struct inode *inode = &ip->i_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000884 struct buffer_head *dibh;
885 int error;
886
887 error = gfs2_meta_inode_buffer(ip, &dibh);
Christoph Hellwig10257742010-06-04 11:30:02 +0200888 if (error)
889 return error;
890
Christoph Hellwig10257742010-06-04 11:30:02 +0200891 setattr_copy(inode, attr);
892 mark_inode_dirty(inode);
Christoph Hellwig10257742010-06-04 11:30:02 +0200893 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
894 gfs2_dinode_out(ip, dibh->b_data);
895 brelse(dibh);
896 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897}
898
899/**
900 * gfs2_setattr_simple -
901 * @ip:
902 * @attr:
903 *
904 * Called with a reference on the vnode.
905 *
906 * Returns: errno
907 */
908
909int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
910{
911 int error;
912
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500913 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914 return __gfs2_setattr_simple(ip, attr);
915
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400916 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 if (error)
918 return error;
919
920 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400921 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922 return error;
923}
924
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100925void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
926{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100927 struct gfs2_dinode *str = buf;
928
929 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
930 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100931 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100932 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);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100938 str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000939 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100940 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);
943
Steven Whitehousece276b02008-02-06 09:25:45 +0000944 str->di_goal_meta = cpu_to_be64(ip->i_goal);
945 str->di_goal_data = cpu_to_be64(ip->i_goal);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +0000946 str->di_generation = cpu_to_be64(ip->i_generation);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100947
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000948 str->di_flags = cpu_to_be32(ip->i_diskflags);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000949 str->di_height = cpu_to_be16(ip->i_height);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100950 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000951 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100952 GFS2_FORMAT_DE : 0);
Steven Whitehouse9a004502008-02-01 09:23:44 +0000953 str->di_depth = cpu_to_be16(ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +0000954 str->di_entries = cpu_to_be32(ip->i_entries);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100955
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000956 str->di_eattr = cpu_to_be64(ip->i_eattr);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100957 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);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100960}
961
962void gfs2_dinode_print(const struct gfs2_inode *ip)
963{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100964 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);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100968 printk(KERN_INFO " i_size = %llu\n",
969 (unsigned long long)i_size_read(&ip->i_inode));
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000970 printk(KERN_INFO " blocks = %llu\n",
971 (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousece276b02008-02-06 09:25:45 +0000972 printk(KERN_INFO " i_goal = %llu\n",
973 (unsigned long long)ip->i_goal);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000974 printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
Bob Petersonca390602008-01-28 11:15:57 -0600975 printk(KERN_INFO " i_height = %u\n", ip->i_height);
Steven Whitehouse9a004502008-02-01 09:23:44 +0000976 printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +0000977 printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000978 printk(KERN_INFO " i_eattr = %llu\n",
979 (unsigned long long)ip->i_eattr);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100980}
981