blob: 56b39be76925c061b1b519786104248548b45f7a [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 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>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020019#include <linux/lm_interface.h>
Ryan O'Harafcb47e02006-10-03 11:57:35 -040020#include <linux/security.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"
27#include "eattr.h"
28#include "glock.h"
29#include "glops.h"
30#include "inode.h"
31#include "log.h"
32#include "meta_io.h"
33#include "ops_address.h"
34#include "ops_file.h"
35#include "ops_inode.h"
36#include "quota.h"
37#include "rgrp.h"
38#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050039#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000040
41/**
Steven Whitehouse4340fe62006-07-11 09:46:33 -040042 * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode
David Teiglandb3b94fa2006-01-16 16:50:04 +000043 * @ip: The GFS2 inode (with embedded disk inode data)
44 * @inode: The Linux VFS inode
45 *
46 */
47
Steven Whitehouse4340fe62006-07-11 09:46:33 -040048void gfs2_inode_attr_in(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +000049{
Steven Whitehouse4340fe62006-07-11 09:46:33 -040050 struct inode *inode = &ip->i_inode;
Al Viro3ca68df2006-10-13 20:11:25 -040051 struct gfs2_dinode_host *di = &ip->i_di;
Steven Whitehouse4340fe62006-07-11 09:46:33 -040052
53 inode->i_ino = ip->i_num.no_addr;
Steven Whitehousec2668712006-09-04 13:55:48 -040054 i_size_write(inode, di->di_size);
Steven Whitehousec2668712006-09-04 13:55:48 -040055 inode->i_blocks = di->di_blocks <<
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040056 (GFS2_SB(inode)->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT);
David Teiglandb3b94fa2006-01-16 16:50:04 +000057}
58
David Teiglandb3b94fa2006-01-16 16:50:04 +000059static int iget_test(struct inode *inode, void *opaque)
60{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040061 struct gfs2_inode *ip = GFS2_I(inode);
Al Viro629a21e2006-10-13 22:51:24 -040062 struct gfs2_inum_host *inum = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +000063
64 if (ip && ip->i_num.no_addr == inum->no_addr)
65 return 1;
66
67 return 0;
68}
69
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040070static int iget_set(struct inode *inode, void *opaque)
71{
72 struct gfs2_inode *ip = GFS2_I(inode);
Al Viro629a21e2006-10-13 22:51:24 -040073 struct gfs2_inum_host *inum = opaque;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040074
75 ip->i_num = *inum;
76 return 0;
77}
78
Al Viro629a21e2006-10-13 22:51:24 -040079struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum_host *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +000080{
81 return ilookup5(sb, (unsigned long)inum->no_formal_ino,
82 iget_test, inum);
83}
84
Al Viro629a21e2006-10-13 22:51:24 -040085static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum_host *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +000086{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040087 return iget5_locked(sb, (unsigned long)inum->no_formal_ino,
88 iget_test, iget_set, inum);
89}
90
91/**
92 * gfs2_inode_lookup - Lookup an inode
93 * @sb: The super block
94 * @inum: The inode number
95 * @type: The type of the inode
96 *
97 * Returns: A VFS inode, or an error
98 */
99
Al Viro629a21e2006-10-13 22:51:24 -0400100struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum_host *inum, unsigned int type)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400101{
102 struct inode *inode = gfs2_iget(sb, inum);
103 struct gfs2_inode *ip = GFS2_I(inode);
104 struct gfs2_glock *io_gl;
105 int error;
106
Steven Whitehouse26d83de2006-10-30 16:59:08 -0500107 if (!inode)
108 return ERR_PTR(-ENOBUFS);
109
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400110 if (inode->i_state & I_NEW) {
111 struct gfs2_sbd *sdp = GFS2_SB(inode);
112 umode_t mode = DT2IF(type);
Theodore Ts'obba9dfd2006-09-27 01:50:31 -0700113 inode->i_private = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400114 inode->i_mode = mode;
115
116 if (S_ISREG(mode)) {
117 inode->i_op = &gfs2_file_iops;
118 inode->i_fop = &gfs2_file_fops;
119 inode->i_mapping->a_ops = &gfs2_file_aops;
120 } else if (S_ISDIR(mode)) {
121 inode->i_op = &gfs2_dir_iops;
122 inode->i_fop = &gfs2_dir_fops;
123 } else if (S_ISLNK(mode)) {
124 inode->i_op = &gfs2_symlink_iops;
125 } else {
126 inode->i_op = &gfs2_dev_iops;
127 }
128
129 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
130 if (unlikely(error))
131 goto fail;
132 ip->i_gl->gl_object = ip;
133
134 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
135 if (unlikely(error))
136 goto fail_put;
137
Steven Whitehousebfded272006-11-01 16:05:38 -0500138 set_bit(GIF_INVALID, &ip->i_flags);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400139 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
140 if (unlikely(error))
141 goto fail_iopen;
142
143 gfs2_glock_put(io_gl);
144 unlock_new_inode(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400146
147 return inode;
148fail_iopen:
149 gfs2_glock_put(io_gl);
150fail_put:
151 ip->i_gl->gl_object = NULL;
152 gfs2_glock_put(ip->i_gl);
153fail:
154 iput(inode);
155 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000156}
157
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500158static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
Steven Whitehouseea744d02006-10-31 15:28:00 -0500159{
160 struct gfs2_dinode_host *di = &ip->i_di;
161 const struct gfs2_dinode *str = buf;
162
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500163 if (ip->i_num.no_addr != be64_to_cpu(str->di_num.no_addr)) {
164 if (gfs2_consist_inode(ip))
165 gfs2_dinode_print(ip);
166 return -EIO;
167 }
168 if (ip->i_num.no_formal_ino != be64_to_cpu(str->di_num.no_formal_ino))
169 return -ESTALE;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500170
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500171 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500172 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500173 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500174 case S_IFBLK:
175 case S_IFCHR:
176 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
177 be32_to_cpu(str->di_minor));
178 break;
179 };
180
Steven Whitehouse2933f922006-11-01 13:23:29 -0500181 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
182 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
Steven Whitehouse4f561102006-11-01 14:04:17 -0500183 /*
184 * We will need to review setting the nlink count here in the
185 * light of the forthcoming ro bind mount work. This is a reminder
186 * to do that.
187 */
188 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500189 di->di_size = be64_to_cpu(str->di_size);
190 di->di_blocks = be64_to_cpu(str->di_blocks);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500191 ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime);
192 ip->i_inode.i_atime.tv_nsec = 0;
193 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
194 ip->i_inode.i_mtime.tv_nsec = 0;
195 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
196 ip->i_inode.i_ctime.tv_nsec = 0;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500197
198 di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
199 di->di_goal_data = be64_to_cpu(str->di_goal_data);
200 di->di_generation = be64_to_cpu(str->di_generation);
201
202 di->di_flags = be32_to_cpu(str->di_flags);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500203 gfs2_set_inode_flags(&ip->i_inode);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500204 di->di_height = be16_to_cpu(str->di_height);
205
206 di->di_depth = be16_to_cpu(str->di_depth);
207 di->di_entries = be32_to_cpu(str->di_entries);
208
209 di->di_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500210 return 0;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500211}
212
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213/**
214 * gfs2_inode_refresh - Refresh the incore copy of the dinode
215 * @ip: The GFS2 inode
216 *
217 * Returns: errno
218 */
219
220int gfs2_inode_refresh(struct gfs2_inode *ip)
221{
222 struct buffer_head *dibh;
223 int error;
224
225 error = gfs2_meta_inode_buffer(ip, &dibh);
226 if (error)
227 return error;
228
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400229 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 brelse(dibh);
231 return -EIO;
232 }
233
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500234 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235 brelse(dibh);
Steven Whitehousebfded272006-11-01 16:05:38 -0500236 clear_bit(GIF_INVALID, &ip->i_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000237
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500238 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239}
240
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400241int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400243 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244 struct gfs2_alloc *al;
245 struct gfs2_rgrpd *rgd;
246 int error;
247
248 if (ip->i_di.di_blocks != 1) {
249 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500250 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251 return -EIO;
252 }
253
254 al = gfs2_alloc_get(ip);
255
256 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
257 if (error)
258 goto out;
259
260 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
261 if (error)
262 goto out_qs;
263
264 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
265 if (!rgd) {
266 gfs2_consist_inode(ip);
267 error = -EIO;
268 goto out_rindex_relse;
269 }
270
271 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
272 &al->al_rgd_gh);
273 if (error)
274 goto out_rindex_relse;
275
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400276 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277 if (error)
278 goto out_rg_gunlock;
279
280 gfs2_trans_add_gl(ip->i_gl);
281
282 gfs2_free_di(rgd, ip);
283
David Teiglandb3b94fa2006-01-16 16:50:04 +0000284 gfs2_trans_end(sdp);
285 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
286
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400287out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400289out_rindex_relse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400291out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 gfs2_quota_unhold(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400293out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400294 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295 return error;
296}
297
298/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299 * gfs2_change_nlink - Change nlink count on inode
300 * @ip: The GFS2 inode
301 * @diff: The change in the nlink count required
302 *
303 * Returns: errno
304 */
305
306int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
307{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400308 struct gfs2_sbd *sdp = ip->i_inode.i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400310 u32 nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311 int error;
312
Steven Whitehouse4f561102006-11-01 14:04:17 -0500313 BUG_ON(diff != 1 && diff != -1);
314 nlink = ip->i_inode.i_nlink + diff;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315
316 /* If we are reducing the nlink count, but the new value ends up being
317 bigger than the old one, we must have underflowed. */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500318 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500320 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321 return -EIO;
322 }
323
324 error = gfs2_meta_inode_buffer(ip, &dibh);
325 if (error)
326 return error;
327
Steven Whitehouse4f561102006-11-01 14:04:17 -0500328 if (diff > 0)
329 inc_nlink(&ip->i_inode);
330 else
331 drop_nlink(&ip->i_inode);
332
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500333 ip->i_inode.i_ctime.tv_sec = get_seconds();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000335 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500336 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400338 mark_inode_dirty(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339
Steven Whitehouse4f561102006-11-01 14:04:17 -0500340 if (ip->i_inode.i_nlink == 0) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400341 struct gfs2_rgrpd *rgd;
342 struct gfs2_holder ri_gh, rg_gh;
343
344 error = gfs2_rindex_hold(sdp, &ri_gh);
345 if (error)
346 goto out;
347 error = -EIO;
348 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
349 if (!rgd)
350 goto out_norgrp;
351 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
352 if (error)
353 goto out_norgrp;
354
355 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
356 gfs2_glock_dq_uninit(&rg_gh);
357out_norgrp:
358 gfs2_glock_dq_uninit(&ri_gh);
359 }
360out:
361 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362}
363
Steven Whitehousec7526662006-03-20 12:30:04 -0500364struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
365{
366 struct qstr qstr;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500367 gfs2_str2qstr(&qstr, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500368 return gfs2_lookupi(dip, &qstr, 1, NULL);
369}
370
371
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372/**
373 * gfs2_lookupi - Look up a filename in a directory and return its inode
374 * @d_gh: An initialized holder for the directory glock
375 * @name: The name of the inode to look for
376 * @is_root: If 1, ignore the caller's permissions
377 * @i_gh: An uninitialized holder for the new inode glock
378 *
379 * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
380 * @is_root is true.
381 *
382 * Returns: errno
383 */
384
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400385struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
386 int is_root, struct nameidata *nd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500388 struct super_block *sb = dir->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400389 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390 struct gfs2_holder d_gh;
Al Viro629a21e2006-10-13 22:51:24 -0400391 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392 unsigned int type;
Steven Whitehouse7359a192006-02-13 12:27:43 +0000393 int error = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500394 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000395
396 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500397 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398
Steven Whitehousec7526662006-03-20 12:30:04 -0500399 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
400 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
401 dir == sb->s_root->d_inode)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400402 igrab(dir);
403 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000404 }
405
406 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
407 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500408 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409
410 if (!is_root) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400411 error = permission(dir, MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412 if (error)
413 goto out;
414 }
415
Steven Whitehousec7526662006-03-20 12:30:04 -0500416 error = gfs2_dir_search(dir, name, &inum, &type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417 if (error)
418 goto out;
419
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400420 inode = gfs2_inode_lookup(sb, &inum, type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421
Steven Whitehouse7359a192006-02-13 12:27:43 +0000422out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500424 if (error == -ENOENT)
425 return NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400426 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427}
428
Steven Whitehousecd915492006-09-04 12:49:07 -0400429static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400431 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400433 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000434 int error;
435
436 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
437 if (error)
438 return error;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000439 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440
441 error = gfs2_meta_inode_buffer(ip, &bh);
442 if (error) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000443 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444 gfs2_trans_end(sdp);
445 return error;
446 }
447
448 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
449
450 if (ir.ir_length) {
451 *formal_ino = ir.ir_start++;
452 ir.ir_length--;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000453 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454 gfs2_inum_range_out(&ir,
455 bh->b_data + sizeof(struct gfs2_dinode));
456 brelse(bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000457 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458 gfs2_trans_end(sdp);
459 return 0;
460 }
461
462 brelse(bh);
463
Steven Whitehousef55ab262006-02-21 12:51:39 +0000464 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000465 gfs2_trans_end(sdp);
466
467 return 1;
468}
469
Steven Whitehousecd915492006-09-04 12:49:07 -0400470static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400472 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
473 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000474 struct gfs2_holder gh;
475 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400476 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477 int error;
478
479 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
480 if (error)
481 return error;
482
483 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
484 if (error)
485 goto out;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000486 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000487
488 error = gfs2_meta_inode_buffer(ip, &bh);
489 if (error)
490 goto out_end_trans;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400491
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
493
494 if (!ir.ir_length) {
495 struct buffer_head *m_bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400496 u64 x, y;
Al Virob44b84d2006-10-14 10:46:30 -0400497 __be64 z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000498
499 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
500 if (error)
501 goto out_brelse;
502
Al Virob44b84d2006-10-14 10:46:30 -0400503 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
504 x = y = be64_to_cpu(z);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 ir.ir_start = x;
506 ir.ir_length = GFS2_INUM_QUANTUM;
507 x += GFS2_INUM_QUANTUM;
508 if (x < y)
509 gfs2_consist_inode(m_ip);
Al Virob44b84d2006-10-14 10:46:30 -0400510 z = cpu_to_be64(x);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000511 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
Al Virob44b84d2006-10-14 10:46:30 -0400512 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513
514 brelse(m_bh);
515 }
516
517 *formal_ino = ir.ir_start++;
518 ir.ir_length--;
519
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000520 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
522
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400523out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000524 brelse(bh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400525out_end_trans:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000526 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 gfs2_trans_end(sdp);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400528out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530 return error;
531}
532
Steven Whitehousecd915492006-09-04 12:49:07 -0400533static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534{
535 int error;
536
537 error = pick_formal_ino_1(sdp, inum);
538 if (error <= 0)
539 return error;
540
541 error = pick_formal_ino_2(sdp, inum);
542
543 return error;
544}
545
546/**
547 * create_ok - OK to create a new on-disk inode here?
548 * @dip: Directory in which dinode is to be created
549 * @name: Name of new dinode
550 * @mode:
551 *
552 * Returns: errno
553 */
554
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400555static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000556 unsigned int mode)
557{
558 int error;
559
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400560 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000561 if (error)
562 return error;
563
564 /* Don't create entries in an unlinked directory */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500565 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 return -EPERM;
567
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400568 error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569 switch (error) {
570 case -ENOENT:
571 error = 0;
572 break;
573 case 0:
574 return -EEXIST;
575 default:
576 return error;
577 }
578
Steven Whitehousecd915492006-09-04 12:49:07 -0400579 if (dip->i_di.di_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 return -EFBIG;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500581 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582 return -EMLINK;
583
584 return 0;
585}
586
587static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
588 unsigned int *uid, unsigned int *gid)
589{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400590 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500591 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592 if (S_ISDIR(*mode))
593 *mode |= S_ISUID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500594 else if (dip->i_inode.i_uid != current->fsuid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595 *mode &= ~07111;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500596 *uid = dip->i_inode.i_uid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597 } else
598 *uid = current->fsuid;
599
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500600 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000601 if (S_ISDIR(*mode))
602 *mode |= S_ISGID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500603 *gid = dip->i_inode.i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 } else
605 *gid = current->fsgid;
606}
607
Al Viro629a21e2006-10-13 22:51:24 -0400608static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum_host *inum,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400609 u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400611 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 int error;
613
614 gfs2_alloc_get(dip);
615
616 dip->i_alloc.al_requested = RES_DINODE;
617 error = gfs2_inplace_reserve(dip);
618 if (error)
619 goto out;
620
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400621 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622 if (error)
623 goto out_ipreserv;
624
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400625 inum->no_addr = gfs2_alloc_di(dip, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000626
627 gfs2_trans_end(sdp);
628
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400629out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400631out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000633 return error;
634}
635
636/**
637 * init_dinode - Fill in a new dinode structure
638 * @dip: the directory this inode is being created in
639 * @gl: The glock covering the new inode
640 * @inum: the inode number
641 * @mode: the file permissions
642 * @uid:
643 * @gid:
644 *
645 */
646
647static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400648 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400649 unsigned int uid, unsigned int gid,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500650 const u64 *generation, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000651{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400652 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000653 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 struct buffer_head *dibh;
655
656 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000657 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
659 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000660 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661
Steven Whitehouse2442a092006-01-30 11:49:32 +0000662 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
663 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000664 di->di_mode = cpu_to_be32(mode);
665 di->di_uid = cpu_to_be32(uid);
666 di->di_gid = cpu_to_be32(gid);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500667 di->di_nlink = 0;
668 di->di_size = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000669 di->di_blocks = cpu_to_be64(1);
670 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500671 di->di_major = cpu_to_be32(MAJOR(dev));
672 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000673 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400674 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500675 di->di_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000676
677 if (S_ISREG(mode)) {
678 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
679 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000680 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
682 gfs2_tune_get(sdp, gt_new_files_directio))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000683 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000684 } else if (S_ISDIR(mode)) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500685 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
686 GFS2_DIF_INHERIT_DIRECTIO);
687 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
688 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 }
690
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000691 di->__pad1 = 0;
Steven Whitehousea9583c72006-11-01 20:09:14 -0500692 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500693 di->di_height = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000694 di->__pad2 = 0;
695 di->__pad3 = 0;
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500696 di->di_depth = 0;
697 di->di_entries = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000698 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500699 di->di_eattr = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000700 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
701
David Teiglandb3b94fa2006-01-16 16:50:04 +0000702 brelse(dibh);
703}
704
705static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400706 unsigned int mode, const struct gfs2_inum_host *inum,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500707 const u64 *generation, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400709 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710 unsigned int uid, gid;
711 int error;
712
713 munge_mode_uid_gid(dip, &mode, &uid, &gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714 gfs2_alloc_get(dip);
715
716 error = gfs2_quota_lock(dip, uid, gid);
717 if (error)
718 goto out;
719
720 error = gfs2_quota_check(dip, uid, gid);
721 if (error)
722 goto out_quota;
723
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400724 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000725 if (error)
726 goto out_quota;
727
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500728 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 gfs2_trans_end(sdp);
731
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400732out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400734out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736 return error;
737}
738
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400739static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
740 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000741{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400742 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 struct gfs2_alloc *al;
744 int alloc_required;
745 struct buffer_head *dibh;
746 int error;
747
748 al = gfs2_alloc_get(dip);
749
750 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
751 if (error)
752 goto fail;
753
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400754 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500755 if (alloc_required < 0)
756 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 if (alloc_required) {
Steven Whitehouse2933f922006-11-01 13:23:29 -0500758 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 if (error)
760 goto fail_quota_locks;
761
762 al->al_requested = sdp->sd_max_dirres;
763
764 error = gfs2_inplace_reserve(dip);
765 if (error)
766 goto fail_quota_locks;
767
Steven Whitehouse320dd102006-05-18 16:25:27 -0400768 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769 al->al_rgd->rd_ri.ri_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400770 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000771 RES_STATFS + RES_QUOTA, 0);
772 if (error)
773 goto fail_ipreserv;
774 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400775 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 if (error)
777 goto fail_quota_locks;
778 }
779
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500780 error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 if (error)
782 goto fail_end_trans;
783
784 error = gfs2_meta_inode_buffer(ip, &dibh);
785 if (error)
786 goto fail_end_trans;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500787 ip->i_inode.i_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000788 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500789 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 return 0;
792
Steven Whitehouse320dd102006-05-18 16:25:27 -0400793fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794 gfs2_trans_end(sdp);
795
Steven Whitehouse320dd102006-05-18 16:25:27 -0400796fail_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 if (dip->i_alloc.al_rgd)
798 gfs2_inplace_release(dip);
799
Steven Whitehouse320dd102006-05-18 16:25:27 -0400800fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801 gfs2_quota_unlock(dip);
802
Steven Whitehouse320dd102006-05-18 16:25:27 -0400803fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805 return error;
806}
807
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400808static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
809{
810 int err;
811 size_t len;
812 void *value;
813 char *name;
814 struct gfs2_ea_request er;
815
816 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
817 &name, &value, &len);
818
819 if (err) {
820 if (err == -EOPNOTSUPP)
821 return 0;
822 return err;
823 }
824
825 memset(&er, 0, sizeof(struct gfs2_ea_request));
826
827 er.er_type = GFS2_EATYPE_SECURITY;
828 er.er_name = name;
829 er.er_data = value;
830 er.er_name_len = strlen(name);
831 er.er_data_len = len;
832
833 err = gfs2_ea_set_i(ip, &er);
834
835 kfree(value);
836 kfree(name);
837
838 return err;
839}
840
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841/**
842 * gfs2_createi - Create a new inode
843 * @ghs: An array of two holders
844 * @name: The name of the new file
845 * @mode: the permissions on the new inode
846 *
847 * @ghs[0] is an initialized holder for the directory
848 * @ghs[1] is the holder for the inode lock
849 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000850 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851 * file are held. A transaction has been started and an inplace reservation
852 * is held, as well.
853 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000854 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855 */
856
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400857struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500858 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859{
Steven Whitehouse7359a192006-02-13 12:27:43 +0000860 struct inode *inode;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500861 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400862 struct inode *dir = &dip->i_inode;
863 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Al Viro629a21e2006-10-13 22:51:24 -0400864 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000865 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400866 u64 generation;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867
868 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000869 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
872 error = gfs2_glock_nq(ghs);
873 if (error)
874 goto fail;
875
876 error = create_ok(dip, name, mode);
877 if (error)
878 goto fail_gunlock;
879
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400880 error = pick_formal_ino(sdp, &inum.no_formal_ino);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881 if (error)
882 goto fail_gunlock;
883
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400884 error = alloc_dinode(dip, &inum, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 if (error)
886 goto fail_gunlock;
887
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400888 if (inum.no_addr < dip->i_num.no_addr) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889 gfs2_glock_dq(ghs);
890
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400891 error = gfs2_glock_nq_num(sdp, inum.no_addr,
Steven Whitehouse320dd102006-05-18 16:25:27 -0400892 &gfs2_inode_glops, LM_ST_EXCLUSIVE,
893 GL_SKIP, ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 if (error) {
Steven Whitehouse7359a192006-02-13 12:27:43 +0000895 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896 }
897
898 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
899 error = gfs2_glock_nq(ghs);
900 if (error) {
901 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000902 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903 }
904
905 error = create_ok(dip, name, mode);
906 if (error)
907 goto fail_gunlock2;
908 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400909 error = gfs2_glock_nq_num(sdp, inum.no_addr,
Steven Whitehouse320dd102006-05-18 16:25:27 -0400910 &gfs2_inode_glops, LM_ST_EXCLUSIVE,
911 GL_SKIP, ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000912 if (error)
913 goto fail_gunlock;
914 }
915
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500916 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 if (error)
918 goto fail_gunlock2;
919
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400920 inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode));
921 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922 goto fail_gunlock2;
923
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400924 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925 if (error)
926 goto fail_iput;
927
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400928 error = gfs2_acl_create(dip, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000929 if (error)
930 goto fail_iput;
931
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400932 error = gfs2_security_init(dip, GFS2_I(inode));
933 if (error)
934 goto fail_iput;
935
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400936 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937 if (error)
938 goto fail_iput;
939
Steven Whitehouse7359a192006-02-13 12:27:43 +0000940 if (!inode)
941 return ERR_PTR(-ENOMEM);
942 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943
Steven Whitehouse320dd102006-05-18 16:25:27 -0400944fail_iput:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400945 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400946fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000947 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400948fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400950fail:
Steven Whitehouse7359a192006-02-13 12:27:43 +0000951 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952}
953
954/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000955 * gfs2_rmdiri - Remove a directory
956 * @dip: The parent directory of the directory to be removed
957 * @name: The name of the directory to be removed
958 * @ip: The GFS2 inode of the directory to be removed
959 *
960 * Assumes Glocks on dip and ip are held
961 *
962 * Returns: errno
963 */
964
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400965int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
966 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968 struct qstr dotname;
969 int error;
970
971 if (ip->i_di.di_entries != 2) {
972 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500973 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974 return -EIO;
975 }
976
977 error = gfs2_dir_del(dip, name);
978 if (error)
979 return error;
980
981 error = gfs2_change_nlink(dip, -1);
982 if (error)
983 return error;
984
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500985 gfs2_str2qstr(&dotname, ".");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986 error = gfs2_dir_del(ip, &dotname);
987 if (error)
988 return error;
989
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400990 gfs2_str2qstr(&dotname, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000991 error = gfs2_dir_del(ip, &dotname);
992 if (error)
993 return error;
994
Steven Whitehouse4f561102006-11-01 14:04:17 -0500995 /* It looks odd, but it really should be done twice */
996 error = gfs2_change_nlink(ip, -1);
997 if (error)
998 return error;
999
1000 error = gfs2_change_nlink(ip, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001 if (error)
1002 return error;
1003
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 return error;
1005}
1006
1007/*
1008 * gfs2_unlink_ok - check to see that a inode is still in a directory
1009 * @dip: the directory
1010 * @name: the name of the file
1011 * @ip: the inode
1012 *
1013 * Assumes that the lock on (at least) @dip is held.
1014 *
1015 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1016 */
1017
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001018int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001019 struct gfs2_inode *ip)
1020{
Al Viro629a21e2006-10-13 22:51:24 -04001021 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022 unsigned int type;
1023 int error;
1024
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001025 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026 return -EPERM;
1027
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001028 if ((dip->i_inode.i_mode & S_ISVTX) &&
Steven Whitehouse2933f922006-11-01 13:23:29 -05001029 dip->i_inode.i_uid != current->fsuid &&
1030 ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031 return -EPERM;
1032
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001033 if (IS_APPEND(&dip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034 return -EPERM;
1035
Steven Whitehousefaf450e2006-06-22 10:59:10 -04001036 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001037 if (error)
1038 return error;
1039
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001040 error = gfs2_dir_search(&dip->i_inode, name, &inum, &type);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001041 if (error)
1042 return error;
1043
1044 if (!gfs2_inum_equal(&inum, &ip->i_num))
1045 return -ENOENT;
1046
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001047 if (IF2DT(ip->i_inode.i_mode) != type) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001048 gfs2_consist_inode(dip);
1049 return -EIO;
1050 }
1051
1052 return 0;
1053}
1054
1055/*
1056 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1057 * @this: move this
1058 * @to: to here
1059 *
1060 * Follow @to back to the root and make sure we don't encounter @this
1061 * Assumes we already hold the rename lock.
1062 *
1063 * Returns: errno
1064 */
1065
1066int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1067{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001068 struct inode *dir = &to->i_inode;
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001069 struct super_block *sb = dir->i_sb;
Steven Whitehouse7359a192006-02-13 12:27:43 +00001070 struct inode *tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001071 struct qstr dotdot;
1072 int error = 0;
1073
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001074 gfs2_str2qstr(&dotdot, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075
Steven Whitehouse7359a192006-02-13 12:27:43 +00001076 igrab(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077
1078 for (;;) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001079 if (dir == &this->i_inode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001080 error = -EINVAL;
1081 break;
1082 }
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001083 if (dir == sb->s_root->d_inode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084 error = 0;
1085 break;
1086 }
1087
Steven Whitehousec7526662006-03-20 12:30:04 -05001088 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1089 if (IS_ERR(tmp)) {
1090 error = PTR_ERR(tmp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001091 break;
Steven Whitehousec7526662006-03-20 12:30:04 -05001092 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093
Steven Whitehouse7359a192006-02-13 12:27:43 +00001094 iput(dir);
1095 dir = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096 }
1097
Steven Whitehouse7359a192006-02-13 12:27:43 +00001098 iput(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099
1100 return error;
1101}
1102
1103/**
1104 * gfs2_readlinki - return the contents of a symlink
1105 * @ip: the symlink's inode
1106 * @buf: a pointer to the buffer to be filled
1107 * @len: a pointer to the length of @buf
1108 *
1109 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1110 * to be freed by the caller.
1111 *
1112 * Returns: errno
1113 */
1114
1115int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1116{
1117 struct gfs2_holder i_gh;
1118 struct buffer_head *dibh;
1119 unsigned int x;
1120 int error;
1121
1122 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1123 error = gfs2_glock_nq_atime(&i_gh);
1124 if (error) {
1125 gfs2_holder_uninit(&i_gh);
1126 return error;
1127 }
1128
1129 if (!ip->i_di.di_size) {
1130 gfs2_consist_inode(ip);
1131 error = -EIO;
1132 goto out;
1133 }
1134
1135 error = gfs2_meta_inode_buffer(ip, &dibh);
1136 if (error)
1137 goto out;
1138
1139 x = ip->i_di.di_size + 1;
1140 if (x > *len) {
1141 *buf = kmalloc(x, GFP_KERNEL);
1142 if (!*buf) {
1143 error = -ENOMEM;
1144 goto out_brelse;
1145 }
1146 }
1147
1148 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1149 *len = x;
1150
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001151out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001153out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001154 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001155 return error;
1156}
1157
1158/**
1159 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1160 * conditionally update the inode's atime
1161 * @gh: the holder to acquire
1162 *
1163 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1164 * Update if the difference between the current time and the inode's current
1165 * atime is greater than an interval specified at mount.
1166 *
1167 * Returns: errno
1168 */
1169
1170int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1171{
1172 struct gfs2_glock *gl = gh->gh_gl;
1173 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001174 struct gfs2_inode *ip = gl->gl_object;
Steven Whitehousecd915492006-09-04 12:49:07 -04001175 s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001176 unsigned int state;
1177 int flags;
1178 int error;
1179
1180 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1181 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1182 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1183 return -EINVAL;
1184
1185 state = gh->gh_state;
1186 flags = gh->gh_flags;
1187
1188 error = gfs2_glock_nq(gh);
1189 if (error)
1190 return error;
1191
1192 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1193 (sdp->sd_vfs->s_flags & MS_RDONLY))
1194 return 0;
1195
1196 curtime = get_seconds();
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001197 if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001198 gfs2_glock_dq(gh);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001199 gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1200 gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201 error = gfs2_glock_nq(gh);
1202 if (error)
1203 return error;
1204
1205 /* Verify that atime hasn't been updated while we were
1206 trying to get exclusive lock. */
1207
1208 curtime = get_seconds();
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001209 if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001210 struct buffer_head *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -04001211 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212
1213 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1214 if (error == -EROFS)
1215 return 0;
1216 if (error)
1217 goto fail;
1218
1219 error = gfs2_meta_inode_buffer(ip, &dibh);
1220 if (error)
1221 goto fail_end_trans;
1222
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001223 ip->i_inode.i_atime.tv_sec = curtime;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001224
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001225 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse48516ce2006-10-02 12:39:19 -04001226 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001227 di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001228 brelse(dibh);
1229
1230 gfs2_trans_end(sdp);
1231 }
1232
1233 /* If someone else has asked for the glock,
1234 unlock and let them have it. Then reacquire
1235 in the original state. */
1236 if (gfs2_glock_is_blocking(gl)) {
1237 gfs2_glock_dq(gh);
1238 gfs2_holder_reinit(state, flags, gh);
1239 return gfs2_glock_nq(gh);
1240 }
1241 }
1242
1243 return 0;
1244
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001245fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001246 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001247fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001248 gfs2_glock_dq(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001249 return error;
1250}
1251
1252/**
1253 * glock_compare_atime - Compare two struct gfs2_glock structures for sort
1254 * @arg_a: the first structure
1255 * @arg_b: the second structure
1256 *
1257 * Returns: 1 if A > B
1258 * -1 if A < B
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001259 * 0 if A == B
David Teiglandb3b94fa2006-01-16 16:50:04 +00001260 */
1261
1262static int glock_compare_atime(const void *arg_a, const void *arg_b)
1263{
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001264 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1265 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1266 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1267 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001268
1269 if (a->ln_number > b->ln_number)
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001270 return 1;
1271 if (a->ln_number < b->ln_number)
1272 return -1;
1273 if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE)
1274 return 1;
1275 if (gh_a->gh_state == LM_ST_SHARED && (gh_b->gh_flags & GL_ATIME))
1276 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001277
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001278 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279}
1280
1281/**
1282 * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
1283 * atime update
1284 * @num_gh: the number of structures
1285 * @ghs: an array of struct gfs2_holder structures
1286 *
1287 * Returns: 0 on success (all glocks acquired),
1288 * errno on failure (no glocks acquired)
1289 */
1290
1291int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
1292{
1293 struct gfs2_holder **p;
1294 unsigned int x;
1295 int error = 0;
1296
1297 if (!num_gh)
1298 return 0;
1299
1300 if (num_gh == 1) {
1301 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1302 if (ghs->gh_flags & GL_ATIME)
1303 error = gfs2_glock_nq_atime(ghs);
1304 else
1305 error = gfs2_glock_nq(ghs);
1306 return error;
1307 }
1308
1309 p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1310 if (!p)
1311 return -ENOMEM;
1312
1313 for (x = 0; x < num_gh; x++)
1314 p[x] = &ghs[x];
1315
1316 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
1317
1318 for (x = 0; x < num_gh; x++) {
1319 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1320
1321 if (p[x]->gh_flags & GL_ATIME)
1322 error = gfs2_glock_nq_atime(p[x]);
1323 else
1324 error = gfs2_glock_nq(p[x]);
1325
1326 if (error) {
1327 while (x--)
1328 gfs2_glock_dq(p[x]);
1329 break;
1330 }
1331 }
1332
1333 kfree(p);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001334 return error;
1335}
1336
David Teiglandb3b94fa2006-01-16 16:50:04 +00001337
1338static int
1339__gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1340{
1341 struct buffer_head *dibh;
1342 int error;
1343
1344 error = gfs2_meta_inode_buffer(ip, &dibh);
1345 if (!error) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001346 error = inode_setattr(&ip->i_inode, attr);
1347 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001348 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001349 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001350 brelse(dibh);
1351 }
1352 return error;
1353}
1354
1355/**
1356 * gfs2_setattr_simple -
1357 * @ip:
1358 * @attr:
1359 *
1360 * Called with a reference on the vnode.
1361 *
1362 * Returns: errno
1363 */
1364
1365int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1366{
1367 int error;
1368
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001369 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001370 return __gfs2_setattr_simple(ip, attr);
1371
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001372 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001373 if (error)
1374 return error;
1375
1376 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001377 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001378 return error;
1379}
1380