blob: 6163be9686be55318a1a84bdd5e8e4fc29501882 [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/**
Wendy Cheng35dcc522007-06-27 17:07:53 -040077 * GFS2 lookup code fills in vfs inode contents based on info obtained
78 * from directory entry inside gfs2_inode_lookup(). This has caused issues
79 * with NFS code path since its get_dentry routine doesn't have the relevant
80 * directory entry when gfs2_inode_lookup() is invoked. Part of the code
81 * segment inside gfs2_inode_lookup code needs to get moved around.
82 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +010083 * Clears I_NEW as well.
Wendy Cheng35dcc522007-06-27 17:07:53 -040084 **/
85
86void gfs2_set_iop(struct inode *inode)
87{
Wendy Chengc97bfe42007-11-29 17:56:51 -050088 struct gfs2_sbd *sdp = GFS2_SB(inode);
Wendy Cheng35dcc522007-06-27 17:07:53 -040089 umode_t mode = inode->i_mode;
90
91 if (S_ISREG(mode)) {
92 inode->i_op = &gfs2_file_iops;
Steven Whitehousef057f6c2009-01-12 10:43:39 +000093 if (gfs2_localflocks(sdp))
Christoph Hellwig10d21982009-04-07 19:42:17 +020094 inode->i_fop = &gfs2_file_fops_nolock;
Wendy Chengc97bfe42007-11-29 17:56:51 -050095 else
Christoph Hellwig10d21982009-04-07 19:42:17 +020096 inode->i_fop = &gfs2_file_fops;
Wendy Cheng35dcc522007-06-27 17:07:53 -040097 } else if (S_ISDIR(mode)) {
98 inode->i_op = &gfs2_dir_iops;
Steven Whitehousef057f6c2009-01-12 10:43:39 +000099 if (gfs2_localflocks(sdp))
Christoph Hellwig10d21982009-04-07 19:42:17 +0200100 inode->i_fop = &gfs2_dir_fops_nolock;
Wendy Chengc97bfe42007-11-29 17:56:51 -0500101 else
Christoph Hellwig10d21982009-04-07 19:42:17 +0200102 inode->i_fop = &gfs2_dir_fops;
Wendy Cheng35dcc522007-06-27 17:07:53 -0400103 } else if (S_ISLNK(mode)) {
104 inode->i_op = &gfs2_symlink_iops;
105 } else {
Denis Chengd83225d2008-02-26 15:25:03 +0800106 inode->i_op = &gfs2_file_iops;
Denis Cheng43a33c52008-02-26 15:25:04 +0800107 init_special_inode(inode, inode->i_mode, inode->i_rdev);
Wendy Cheng35dcc522007-06-27 17:07:53 -0400108 }
109
110 unlock_new_inode(inode);
111}
112
113/**
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400114 * gfs2_inode_lookup - Lookup an inode
115 * @sb: The super block
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100116 * @no_addr: The inode number
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400117 * @type: The type of the inode
118 *
119 * Returns: A VFS inode, or an error
120 */
121
Bob Peterson091806ed2008-04-29 12:35:48 -0500122struct inode *gfs2_inode_lookup(struct super_block *sb,
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400123 unsigned int type,
124 u64 no_addr,
Bob Peterson1a0eae82010-04-14 11:58:16 -0400125 u64 no_formal_ino)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400126{
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500127 struct inode *inode;
128 struct gfs2_inode *ip;
Bob Petersonb1becbd2010-06-17 16:45:37 -0400129 struct gfs2_glock *io_gl = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400130 int error;
131
Bob Peterson1a0eae82010-04-14 11:58:16 -0400132 inode = gfs2_iget(sb, no_addr);
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500133 ip = GFS2_I(inode);
134
Steven Whitehouse26d83de2006-10-30 16:59:08 -0500135 if (!inode)
136 return ERR_PTR(-ENOBUFS);
137
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400138 if (inode->i_state & I_NEW) {
139 struct gfs2_sbd *sdp = GFS2_SB(inode);
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400140 ip->i_no_formal_ino = no_formal_ino;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400141
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100142 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400143 if (unlikely(error))
144 goto fail;
145 ip->i_gl->gl_object = ip;
146
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100147 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400148 if (unlikely(error))
149 goto fail_put;
150
Steven Whitehousebfded272006-11-01 16:05:38 -0500151 set_bit(GIF_INVALID, &ip->i_flags);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400152 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
153 if (unlikely(error))
154 goto fail_iopen;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100155 ip->i_iopen_gh.gh_gl->gl_object = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400156
157 gfs2_glock_put(io_gl);
Bob Petersonb1becbd2010-06-17 16:45:37 -0400158 io_gl = NULL;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100159
Wendy Cheng35dcc522007-06-27 17:07:53 -0400160 if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
161 goto gfs2_nfsbypass;
162
163 inode->i_mode = DT2IF(type);
164
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100165 /*
166 * We must read the inode in order to work out its type in
167 * this case. Note that this doesn't happen often as we normally
168 * know the type beforehand. This code path only occurs during
169 * unlinked inode recovery (where it is safe to do this glock,
170 * which is not true in the general case).
171 */
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100172 if (type == DT_UNKNOWN) {
173 struct gfs2_holder gh;
174 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
175 if (unlikely(error))
176 goto fail_glock;
177 /* Inode is now uptodate */
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100178 gfs2_glock_dq_uninit(&gh);
179 }
180
Wendy Cheng35dcc522007-06-27 17:07:53 -0400181 gfs2_set_iop(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400183
Wendy Cheng35dcc522007-06-27 17:07:53 -0400184gfs2_nfsbypass:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400185 return inode;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100186fail_glock:
187 gfs2_glock_dq(&ip->i_iopen_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400188fail_iopen:
Bob Petersonb1becbd2010-06-17 16:45:37 -0400189 if (io_gl)
190 gfs2_glock_put(io_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400191fail_put:
Bob Peterson1a0eae82010-04-14 11:58:16 -0400192 if (inode->i_state & I_NEW)
193 ip->i_gl->gl_object = NULL;
194 gfs2_glock_put(ip->i_gl);
195fail:
196 if (inode->i_state & I_NEW)
197 iget_failed(inode);
198 else
199 iput(inode);
200 return ERR_PTR(error);
201}
202
Steven Whitehouse044b9412010-11-03 20:01:07 +0000203struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
204 u64 *no_formal_ino, unsigned int blktype)
Bob Peterson1a0eae82010-04-14 11:58:16 -0400205{
Steven Whitehouse044b9412010-11-03 20:01:07 +0000206 struct super_block *sb = sdp->sd_vfs;
207 struct gfs2_holder i_gh;
Bob Petersoned4878e2010-05-20 23:30:11 -0400208 struct inode *inode;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000209 int error;
Bob Peterson1a0eae82010-04-14 11:58:16 -0400210
Steven Whitehouse044b9412010-11-03 20:01:07 +0000211 error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
212 LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
213 if (error)
214 return ERR_PTR(error);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400215
Steven Whitehouse044b9412010-11-03 20:01:07 +0000216 error = gfs2_check_blk_type(sdp, no_addr, blktype);
217 if (error)
218 goto fail;
Bob Peterson1a0eae82010-04-14 11:58:16 -0400219
Steven Whitehouse044b9412010-11-03 20:01:07 +0000220 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0);
221 if (IS_ERR(inode))
222 goto fail;
223
224 error = gfs2_inode_refresh(GFS2_I(inode));
225 if (error)
226 goto fail_iput;
227
228 /* Pick up the works we bypass in gfs2_inode_lookup */
229 if (inode->i_state & I_NEW)
230 gfs2_set_iop(inode);
231
232 /* Two extra checks for NFS only */
233 if (no_formal_ino) {
234 error = -ESTALE;
235 if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
236 goto fail_iput;
237
238 error = -EIO;
239 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
240 goto fail_iput;
241
242 error = 0;
Bob Petersoned4878e2010-05-20 23:30:11 -0400243 }
Bob Peterson1a0eae82010-04-14 11:58:16 -0400244
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400245fail:
Steven Whitehouse044b9412010-11-03 20:01:07 +0000246 gfs2_glock_dq_uninit(&i_gh);
247 return error ? ERR_PTR(error) : inode;
248fail_iput:
249 iput(inode);
250 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251}
252
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500253static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
Steven Whitehouseea744d02006-10-31 15:28:00 -0500254{
Steven Whitehouseea744d02006-10-31 15:28:00 -0500255 const struct gfs2_dinode *str = buf;
Steven Whitehouse719ee342008-09-18 13:53:59 +0100256 struct timespec atime;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000257 u16 height, depth;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500258
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000259 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
260 goto corrupt;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100261 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500262 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500263 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500264 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500265 case S_IFBLK:
266 case S_IFCHR:
267 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
268 be32_to_cpu(str->di_minor));
269 break;
270 };
271
Steven Whitehouse2933f922006-11-01 13:23:29 -0500272 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
273 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
Steven Whitehouse4f561102006-11-01 14:04:17 -0500274 /*
275 * We will need to review setting the nlink count here in the
276 * light of the forthcoming ro bind mount work. This is a reminder
277 * to do that.
278 */
279 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100280 i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000281 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
Steven Whitehouse719ee342008-09-18 13:53:59 +0100282 atime.tv_sec = be64_to_cpu(str->di_atime);
283 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
284 if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
285 ip->i_inode.i_atime = atime;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500286 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100287 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500288 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100289 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500290
Steven Whitehousece276b02008-02-06 09:25:45 +0000291 ip->i_goal = be64_to_cpu(str->di_goal_meta);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +0000292 ip->i_generation = be64_to_cpu(str->di_generation);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500293
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000294 ip->i_diskflags = be32_to_cpu(str->di_flags);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500295 gfs2_set_inode_flags(&ip->i_inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000296 height = be16_to_cpu(str->di_height);
297 if (unlikely(height > GFS2_MAX_META_HEIGHT))
298 goto corrupt;
299 ip->i_height = (u8)height;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500300
Steven Whitehouse9a004502008-02-01 09:23:44 +0000301 depth = be16_to_cpu(str->di_depth);
302 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
303 goto corrupt;
304 ip->i_depth = (u8)depth;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000305 ip->i_entries = be32_to_cpu(str->di_entries);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500306
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000307 ip->i_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouse55610932007-10-17 08:47:38 +0100308 if (S_ISREG(ip->i_inode.i_mode))
309 gfs2_set_aops(&ip->i_inode);
310
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500311 return 0;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000312corrupt:
313 if (gfs2_consist_inode(ip))
314 gfs2_dinode_print(ip);
315 return -EIO;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500316}
317
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318/**
319 * gfs2_inode_refresh - Refresh the incore copy of the dinode
320 * @ip: The GFS2 inode
321 *
322 * Returns: errno
323 */
324
325int gfs2_inode_refresh(struct gfs2_inode *ip)
326{
327 struct buffer_head *dibh;
328 int error;
329
330 error = gfs2_meta_inode_buffer(ip, &dibh);
331 if (error)
332 return error;
333
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400334 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335 brelse(dibh);
336 return -EIO;
337 }
338
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500339 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340 brelse(dibh);
Steven Whitehousebfded272006-11-01 16:05:38 -0500341 clear_bit(GIF_INVALID, &ip->i_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000342
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500343 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344}
345
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400346int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400348 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000349 struct gfs2_alloc *al;
350 struct gfs2_rgrpd *rgd;
351 int error;
352
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000353 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500355 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 return -EIO;
357 }
358
359 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300360 if (!al)
361 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362
363 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
364 if (error)
365 goto out;
366
367 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
368 if (error)
369 goto out_qs;
370
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100371 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372 if (!rgd) {
373 gfs2_consist_inode(ip);
374 error = -EIO;
375 goto out_rindex_relse;
376 }
377
378 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
379 &al->al_rgd_gh);
380 if (error)
381 goto out_rindex_relse;
382
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400383 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384 if (error)
385 goto out_rg_gunlock;
386
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000387 set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
388 set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389
390 gfs2_free_di(rgd, ip);
391
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000393
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400394out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000395 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400396out_rindex_relse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000397 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400398out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000399 gfs2_quota_unhold(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400400out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400401 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000402 return error;
403}
404
405/**
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500406 * gfs2_change_nlink - Change nlink count on inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407 * @ip: The GFS2 inode
408 * @diff: The change in the nlink count required
409 *
410 * Returns: errno
411 */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500412int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000413{
414 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400415 u32 nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 int error;
417
Steven Whitehouse4f561102006-11-01 14:04:17 -0500418 BUG_ON(diff != 1 && diff != -1);
419 nlink = ip->i_inode.i_nlink + diff;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420
421 /* If we are reducing the nlink count, but the new value ends up being
422 bigger than the old one, we must have underflowed. */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500423 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500425 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000426 return -EIO;
427 }
428
429 error = gfs2_meta_inode_buffer(ip, &dibh);
430 if (error)
431 return error;
432
Steven Whitehouse4f561102006-11-01 14:04:17 -0500433 if (diff > 0)
434 inc_nlink(&ip->i_inode);
435 else
436 drop_nlink(&ip->i_inode);
437
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100438 ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000440 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500441 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400443 mark_inode_dirty(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500445 if (ip->i_inode.i_nlink == 0)
Russell Cattelanddee7602007-01-29 17:13:44 -0600446 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500447
S. Wendy Cheng55098262007-01-18 15:56:34 -0500448 return error;
449}
450
Steven Whitehousec7526662006-03-20 12:30:04 -0500451struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
452{
453 struct qstr qstr;
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600454 struct inode *inode;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500455 gfs2_str2qstr(&qstr, name);
Al Viroa569c712008-07-23 14:42:05 -0400456 inode = gfs2_lookupi(dip, &qstr, 1);
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600457 /* gfs2_lookupi has inconsistent callers: vfs
458 * related routines expect NULL for no entry found,
459 * gfs2_lookup_simple callers expect ENOENT
460 * and do not check for NULL.
461 */
462 if (inode == NULL)
463 return ERR_PTR(-ENOENT);
464 else
465 return inode;
Steven Whitehousec7526662006-03-20 12:30:04 -0500466}
467
468
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469/**
470 * gfs2_lookupi - Look up a filename in a directory and return its inode
471 * @d_gh: An initialized holder for the directory glock
472 * @name: The name of the inode to look for
473 * @is_root: If 1, ignore the caller's permissions
474 * @i_gh: An uninitialized holder for the new inode glock
475 *
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000476 * This can be called via the VFS filldir function when NFS is doing
477 * a readdirplus and the inode which its intending to stat isn't
478 * already in cache. In this case we must not take the directory glock
479 * again, since the readdir call will have already taken that lock.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 *
481 * Returns: errno
482 */
483
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400484struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
Al Viroa569c712008-07-23 14:42:05 -0400485 int is_root)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000486{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500487 struct super_block *sb = dir->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400488 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 struct gfs2_holder d_gh;
akpm@linux-foundation.org037bcbb2007-06-08 16:42:14 -0700490 int error = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500491 struct inode *inode = NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000492 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493
494 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500495 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000496
Steven Whitehousec7526662006-03-20 12:30:04 -0500497 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
498 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
499 dir == sb->s_root->d_inode)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400500 igrab(dir);
501 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502 }
503
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000504 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000505 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
506 if (error)
507 return ERR_PTR(error);
508 unlock = 1;
509 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000510
511 if (!is_root) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100512 error = gfs2_permission(dir, MAY_EXEC, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 if (error)
514 goto out;
515 }
516
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100517 inode = gfs2_dir_search(dir, name);
518 if (IS_ERR(inode))
519 error = PTR_ERR(inode);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000520out:
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000521 if (unlock)
522 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500523 if (error == -ENOENT)
524 return NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000525 return inode ? inode : ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526}
527
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528/**
529 * create_ok - OK to create a new on-disk inode here?
530 * @dip: Directory in which dinode is to be created
531 * @name: Name of new dinode
532 * @mode:
533 *
534 * Returns: errno
535 */
536
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400537static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538 unsigned int mode)
539{
540 int error;
541
Nick Pigginb74c79e2011-01-07 17:49:58 +1100542 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543 if (error)
544 return error;
545
546 /* Don't create entries in an unlinked directory */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500547 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548 return -EPERM;
549
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100550 error = gfs2_dir_check(&dip->i_inode, name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551 switch (error) {
552 case -ENOENT:
553 error = 0;
554 break;
555 case 0:
556 return -EEXIST;
557 default:
558 return error;
559 }
560
Steven Whitehousead6203f2008-11-03 13:59:19 +0000561 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 return -EFBIG;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500563 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000564 return -EMLINK;
565
566 return 0;
567}
568
569static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
570 unsigned int *uid, unsigned int *gid)
571{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400572 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500573 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574 if (S_ISDIR(*mode))
575 *mode |= S_ISUID;
David Howells3de7be32008-11-14 10:38:53 +1100576 else if (dip->i_inode.i_uid != current_fsuid())
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577 *mode &= ~07111;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500578 *uid = dip->i_inode.i_uid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579 } else
David Howells3de7be32008-11-14 10:38:53 +1100580 *uid = current_fsuid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500582 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583 if (S_ISDIR(*mode))
584 *mode |= S_ISGID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500585 *gid = dip->i_inode.i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586 } else
David Howells3de7be32008-11-14 10:38:53 +1100587 *gid = current_fsgid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588}
589
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100590static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400592 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593 int error;
594
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000595 if (gfs2_alloc_get(dip) == NULL)
596 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000598 dip->i_alloc->al_requested = RES_DINODE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000599 error = gfs2_inplace_reserve(dip);
600 if (error)
601 goto out;
602
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400603 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 if (error)
605 goto out_ipreserv;
606
Steven Whitehouse6050b9c2009-07-31 16:19:40 +0100607 error = gfs2_alloc_di(dip, no_addr, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608
609 gfs2_trans_end(sdp);
610
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400611out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400613out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000614 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615 return error;
616}
617
618/**
619 * init_dinode - Fill in a new dinode structure
620 * @dip: the directory this inode is being created in
621 * @gl: The glock covering the new inode
622 * @inum: the inode number
623 * @mode: the file permissions
624 * @uid:
625 * @gid:
626 *
627 */
628
629static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400630 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400631 unsigned int uid, unsigned int gid,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400632 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000633{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400634 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000635 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636 struct buffer_head *dibh;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100637 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638
639 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000640 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
642 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000643 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644
Steven Whitehouse2442a092006-01-30 11:49:32 +0000645 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
646 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000647 di->di_mode = cpu_to_be32(mode);
648 di->di_uid = cpu_to_be32(uid);
649 di->di_gid = cpu_to_be32(gid);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500650 di->di_nlink = 0;
651 di->di_size = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000652 di->di_blocks = cpu_to_be64(1);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100653 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500654 di->di_major = cpu_to_be32(MAJOR(dev));
655 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000656 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400657 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500658 di->di_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659
660 if (S_ISREG(mode)) {
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000661 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000663 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664 } else if (S_ISDIR(mode)) {
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000665 di->di_flags |= cpu_to_be32(dip->i_diskflags &
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500666 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 }
668
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000669 di->__pad1 = 0;
Steven Whitehousea9583c72006-11-01 20:09:14 -0500670 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500671 di->di_height = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000672 di->__pad2 = 0;
673 di->__pad3 = 0;
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500674 di->di_depth = 0;
675 di->di_entries = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000676 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500677 di->di_eattr = 0;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100678 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
679 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
680 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000681 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400682
683 set_buffer_uptodate(dibh);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000684
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400685 *bhp = dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000686}
687
688static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400689 unsigned int mode, const struct gfs2_inum_host *inum,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400690 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400692 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693 unsigned int uid, gid;
694 int error;
695
696 munge_mode_uid_gid(dip, &mode, &uid, &gid);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300697 if (!gfs2_alloc_get(dip))
698 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000699
700 error = gfs2_quota_lock(dip, uid, gid);
701 if (error)
702 goto out;
703
704 error = gfs2_quota_check(dip, uid, gid);
705 if (error)
706 goto out_quota;
707
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400708 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000709 if (error)
710 goto out_quota;
711
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400712 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714 gfs2_trans_end(sdp);
715
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400716out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400718out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720 return error;
721}
722
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400723static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
724 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000725{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400726 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727 struct gfs2_alloc *al;
728 int alloc_required;
729 struct buffer_head *dibh;
730 int error;
731
732 al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300733 if (!al)
734 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735
736 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
737 if (error)
738 goto fail;
739
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400740 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500741 if (alloc_required < 0)
Bob Peterson1b8177e2008-01-19 21:50:24 -0600742 goto fail_quota_locks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 if (alloc_required) {
Steven Whitehouse2933f922006-11-01 13:23:29 -0500744 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745 if (error)
746 goto fail_quota_locks;
747
748 al->al_requested = sdp->sd_max_dirres;
749
750 error = gfs2_inplace_reserve(dip);
751 if (error)
752 goto fail_quota_locks;
753
Steven Whitehouse320dd102006-05-18 16:25:27 -0400754 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100755 al->al_rgd->rd_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400756 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 RES_STATFS + RES_QUOTA, 0);
758 if (error)
759 goto fail_ipreserv;
760 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400761 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762 if (error)
763 goto fail_quota_locks;
764 }
765
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100766 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 if (error)
768 goto fail_end_trans;
769
770 error = gfs2_meta_inode_buffer(ip, &dibh);
771 if (error)
772 goto fail_end_trans;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500773 ip->i_inode.i_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000774 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500775 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777 return 0;
778
Steven Whitehouse320dd102006-05-18 16:25:27 -0400779fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780 gfs2_trans_end(sdp);
781
Steven Whitehouse320dd102006-05-18 16:25:27 -0400782fail_ipreserv:
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000783 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000784 gfs2_inplace_release(dip);
785
Steven Whitehouse320dd102006-05-18 16:25:27 -0400786fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 gfs2_quota_unlock(dip);
788
Steven Whitehouse320dd102006-05-18 16:25:27 -0400789fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 return error;
792}
793
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400794static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
795{
796 int err;
797 size_t len;
798 void *value;
799 char *name;
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400800
801 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
802 &name, &value, &len);
803
804 if (err) {
805 if (err == -EOPNOTSUPP)
806 return 0;
807 return err;
808 }
809
Christoph Hellwig431547b2009-11-13 09:52:56 +0000810 err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
811 GFS2_EATYPE_SECURITY);
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400812 kfree(value);
813 kfree(name);
814
815 return err;
816}
817
David Teiglandb3b94fa2006-01-16 16:50:04 +0000818/**
819 * gfs2_createi - Create a new inode
820 * @ghs: An array of two holders
821 * @name: The name of the new file
822 * @mode: the permissions on the new inode
823 *
824 * @ghs[0] is an initialized holder for the directory
825 * @ghs[1] is the holder for the inode lock
826 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000827 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828 * file are held. A transaction has been started and an inplace reservation
829 * is held, as well.
830 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000831 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832 */
833
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400834struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500835 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836{
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100837 struct inode *inode = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500838 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400839 struct inode *dir = &dip->i_inode;
840 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100841 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400843 u64 generation;
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100844 struct buffer_head *bh = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845
846 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000847 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848
David Teiglandb3b94fa2006-01-16 16:50:04 +0000849 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
850 error = gfs2_glock_nq(ghs);
851 if (error)
852 goto fail;
853
854 error = create_ok(dip, name, mode);
855 if (error)
856 goto fail_gunlock;
857
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100858 error = alloc_dinode(dip, &inum.no_addr, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 if (error)
860 goto fail_gunlock;
Steven Whitehouse8d8291a2009-08-27 15:51:07 +0100861 inum.no_formal_ino = generation;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862
Steven Whitehouse28626e22006-11-22 11:13:21 -0500863 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
864 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
865 if (error)
866 goto fail_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400868 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869 if (error)
870 goto fail_gunlock2;
871
Steven Whitehouse8d8291a2009-08-27 15:51:07 +0100872 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
Bob Peterson1a0eae82010-04-14 11:58:16 -0400873 inum.no_formal_ino);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400874 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 goto fail_gunlock2;
876
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400877 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100879 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000880
Steven Whitehouse479c4272009-10-02 12:00:00 +0100881 error = gfs2_acl_create(dip, inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100883 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000884
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400885 error = gfs2_security_init(dip, GFS2_I(inode));
886 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100887 goto fail_gunlock2;
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400888
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400889 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000890 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100891 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000892
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100893 if (bh)
894 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000895 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896
Steven Whitehouse320dd102006-05-18 16:25:27 -0400897fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898 gfs2_glock_dq_uninit(ghs + 1);
Julien Brunelbd1eb882008-09-01 10:51:22 +0200899 if (inode && !IS_ERR(inode))
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100900 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400901fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000902 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400903fail:
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100904 if (bh)
905 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000906 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000907}
908
Steven Whitehouse536baf02009-05-22 10:48:59 +0100909static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910{
Christoph Hellwig10257742010-06-04 11:30:02 +0200911 struct inode *inode = &ip->i_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000912 struct buffer_head *dibh;
913 int error;
914
915 error = gfs2_meta_inode_buffer(ip, &dibh);
Christoph Hellwig10257742010-06-04 11:30:02 +0200916 if (error)
917 return error;
918
919 if ((attr->ia_valid & ATTR_SIZE) &&
920 attr->ia_size != i_size_read(inode)) {
921 error = vmtruncate(inode, attr->ia_size);
922 if (error)
923 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924 }
Christoph Hellwig10257742010-06-04 11:30:02 +0200925
926 setattr_copy(inode, attr);
927 mark_inode_dirty(inode);
928
929 gfs2_assert_warn(GFS2_SB(inode), !error);
930 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
931 gfs2_dinode_out(ip, dibh->b_data);
932 brelse(dibh);
933 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934}
935
936/**
937 * gfs2_setattr_simple -
938 * @ip:
939 * @attr:
940 *
941 * Called with a reference on the vnode.
942 *
943 * Returns: errno
944 */
945
946int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
947{
948 int error;
949
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500950 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 return __gfs2_setattr_simple(ip, attr);
952
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400953 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954 if (error)
955 return error;
956
957 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400958 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000959 return error;
960}
961
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100962void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
963{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100964 struct gfs2_dinode *str = buf;
965
966 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
967 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100968 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100969 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
970 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
971 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
972 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
973 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
974 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100975 str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000976 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100977 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
978 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
979 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
980
Steven Whitehousece276b02008-02-06 09:25:45 +0000981 str->di_goal_meta = cpu_to_be64(ip->i_goal);
982 str->di_goal_data = cpu_to_be64(ip->i_goal);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +0000983 str->di_generation = cpu_to_be64(ip->i_generation);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100984
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000985 str->di_flags = cpu_to_be32(ip->i_diskflags);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000986 str->di_height = cpu_to_be16(ip->i_height);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100987 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000988 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100989 GFS2_FORMAT_DE : 0);
Steven Whitehouse9a004502008-02-01 09:23:44 +0000990 str->di_depth = cpu_to_be16(ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +0000991 str->di_entries = cpu_to_be32(ip->i_entries);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100992
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000993 str->di_eattr = cpu_to_be64(ip->i_eattr);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100994 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
995 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
996 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100997}
998
999void gfs2_dinode_print(const struct gfs2_inode *ip)
1000{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001001 printk(KERN_INFO " no_formal_ino = %llu\n",
1002 (unsigned long long)ip->i_no_formal_ino);
1003 printk(KERN_INFO " no_addr = %llu\n",
1004 (unsigned long long)ip->i_no_addr);
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001005 printk(KERN_INFO " i_size = %llu\n",
1006 (unsigned long long)i_size_read(&ip->i_inode));
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001007 printk(KERN_INFO " blocks = %llu\n",
1008 (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousece276b02008-02-06 09:25:45 +00001009 printk(KERN_INFO " i_goal = %llu\n",
1010 (unsigned long long)ip->i_goal);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001011 printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
Bob Petersonca390602008-01-28 11:15:57 -06001012 printk(KERN_INFO " i_height = %u\n", ip->i_height);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001013 printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001014 printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001015 printk(KERN_INFO " i_eattr = %llu\n",
1016 (unsigned long long)ip->i_eattr);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001017}
1018