blob: 0de9b22f454b0775e9d4df15a07ceecad96d2c52 [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 inode->i_nlink = di->di_nlink;
Steven Whitehousec2668712006-09-04 13:55:48 -040055 i_size_write(inode, di->di_size);
56 inode->i_atime.tv_sec = di->di_atime;
57 inode->i_mtime.tv_sec = di->di_mtime;
58 inode->i_ctime.tv_sec = di->di_ctime;
David Teiglandb3b94fa2006-01-16 16:50:04 +000059 inode->i_atime.tv_nsec = 0;
60 inode->i_mtime.tv_nsec = 0;
61 inode->i_ctime.tv_nsec = 0;
Steven Whitehousec2668712006-09-04 13:55:48 -040062 inode->i_blocks = di->di_blocks <<
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040063 (GFS2_SB(inode)->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT);
David Teiglandb3b94fa2006-01-16 16:50:04 +000064
Steven Whitehousec2668712006-09-04 13:55:48 -040065 if (di->di_flags & GFS2_DIF_IMMUTABLE)
David Teiglandb3b94fa2006-01-16 16:50:04 +000066 inode->i_flags |= S_IMMUTABLE;
67 else
68 inode->i_flags &= ~S_IMMUTABLE;
69
Steven Whitehousec2668712006-09-04 13:55:48 -040070 if (di->di_flags & GFS2_DIF_APPENDONLY)
David Teiglandb3b94fa2006-01-16 16:50:04 +000071 inode->i_flags |= S_APPEND;
72 else
73 inode->i_flags &= ~S_APPEND;
74}
75
76/**
David Teiglandb3b94fa2006-01-16 16:50:04 +000077 * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode
78 * @ip: The GFS2 inode
79 *
80 * Only copy out the attributes that we want the VFS layer
81 * to be able to modify.
82 */
83
84void gfs2_inode_attr_out(struct gfs2_inode *ip)
85{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040086 struct inode *inode = &ip->i_inode;
Al Viro3ca68df2006-10-13 20:11:25 -040087 struct gfs2_dinode_host *di = &ip->i_di;
Steven Whitehouse75d3b812006-09-04 11:41:31 -040088 di->di_atime = inode->i_atime.tv_sec;
89 di->di_mtime = inode->i_mtime.tv_sec;
90 di->di_ctime = inode->i_ctime.tv_sec;
David Teiglandb3b94fa2006-01-16 16:50:04 +000091}
92
David Teiglandb3b94fa2006-01-16 16:50:04 +000093static int iget_test(struct inode *inode, void *opaque)
94{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040095 struct gfs2_inode *ip = GFS2_I(inode);
Al Viro629a21e2006-10-13 22:51:24 -040096 struct gfs2_inum_host *inum = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +000097
98 if (ip && ip->i_num.no_addr == inum->no_addr)
99 return 1;
100
101 return 0;
102}
103
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400104static int iget_set(struct inode *inode, void *opaque)
105{
106 struct gfs2_inode *ip = GFS2_I(inode);
Al Viro629a21e2006-10-13 22:51:24 -0400107 struct gfs2_inum_host *inum = opaque;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400108
109 ip->i_num = *inum;
110 return 0;
111}
112
Al Viro629a21e2006-10-13 22:51:24 -0400113struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum_host *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000114{
115 return ilookup5(sb, (unsigned long)inum->no_formal_ino,
116 iget_test, inum);
117}
118
Al Viro629a21e2006-10-13 22:51:24 -0400119static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum_host *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400121 return iget5_locked(sb, (unsigned long)inum->no_formal_ino,
122 iget_test, iget_set, inum);
123}
124
125/**
126 * gfs2_inode_lookup - Lookup an inode
127 * @sb: The super block
128 * @inum: The inode number
129 * @type: The type of the inode
130 *
131 * Returns: A VFS inode, or an error
132 */
133
Al Viro629a21e2006-10-13 22:51:24 -0400134struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum_host *inum, unsigned int type)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400135{
136 struct inode *inode = gfs2_iget(sb, inum);
137 struct gfs2_inode *ip = GFS2_I(inode);
138 struct gfs2_glock *io_gl;
139 int error;
140
Steven Whitehouse26d83de2006-10-30 16:59:08 -0500141 if (!inode)
142 return ERR_PTR(-ENOBUFS);
143
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400144 if (inode->i_state & I_NEW) {
145 struct gfs2_sbd *sdp = GFS2_SB(inode);
146 umode_t mode = DT2IF(type);
Theodore Ts'obba9dfd2006-09-27 01:50:31 -0700147 inode->i_private = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400148 inode->i_mode = mode;
149
150 if (S_ISREG(mode)) {
151 inode->i_op = &gfs2_file_iops;
152 inode->i_fop = &gfs2_file_fops;
153 inode->i_mapping->a_ops = &gfs2_file_aops;
154 } else if (S_ISDIR(mode)) {
155 inode->i_op = &gfs2_dir_iops;
156 inode->i_fop = &gfs2_dir_fops;
157 } else if (S_ISLNK(mode)) {
158 inode->i_op = &gfs2_symlink_iops;
159 } else {
160 inode->i_op = &gfs2_dev_iops;
161 }
162
163 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
164 if (unlikely(error))
165 goto fail;
166 ip->i_gl->gl_object = ip;
167
168 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
169 if (unlikely(error))
170 goto fail_put;
171
172 ip->i_vn = ip->i_gl->gl_vn - 1;
173 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
174 if (unlikely(error))
175 goto fail_iopen;
176
177 gfs2_glock_put(io_gl);
178 unlock_new_inode(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000179 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400180
181 return inode;
182fail_iopen:
183 gfs2_glock_put(io_gl);
184fail_put:
185 ip->i_gl->gl_object = NULL;
186 gfs2_glock_put(ip->i_gl);
187fail:
188 iput(inode);
189 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190}
191
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500192static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
Steven Whitehouseea744d02006-10-31 15:28:00 -0500193{
194 struct gfs2_dinode_host *di = &ip->i_di;
195 const struct gfs2_dinode *str = buf;
196
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500197 if (ip->i_num.no_addr != be64_to_cpu(str->di_num.no_addr)) {
198 if (gfs2_consist_inode(ip))
199 gfs2_dinode_print(ip);
200 return -EIO;
201 }
202 if (ip->i_num.no_formal_ino != be64_to_cpu(str->di_num.no_formal_ino))
203 return -ESTALE;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500204
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500205 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500206 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500207 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500208 case S_IFBLK:
209 case S_IFCHR:
210 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
211 be32_to_cpu(str->di_minor));
212 break;
213 };
214
Steven Whitehouse2933f922006-11-01 13:23:29 -0500215 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
216 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500217 di->di_nlink = be32_to_cpu(str->di_nlink);
218 di->di_size = be64_to_cpu(str->di_size);
219 di->di_blocks = be64_to_cpu(str->di_blocks);
220 di->di_atime = be64_to_cpu(str->di_atime);
221 di->di_mtime = be64_to_cpu(str->di_mtime);
222 di->di_ctime = be64_to_cpu(str->di_ctime);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500223
224 di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
225 di->di_goal_data = be64_to_cpu(str->di_goal_data);
226 di->di_generation = be64_to_cpu(str->di_generation);
227
228 di->di_flags = be32_to_cpu(str->di_flags);
229 di->di_payload_format = be32_to_cpu(str->di_payload_format);
230 di->di_height = be16_to_cpu(str->di_height);
231
232 di->di_depth = be16_to_cpu(str->di_depth);
233 di->di_entries = be32_to_cpu(str->di_entries);
234
235 di->di_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500236 return 0;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500237}
238
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239/**
240 * gfs2_inode_refresh - Refresh the incore copy of the dinode
241 * @ip: The GFS2 inode
242 *
243 * Returns: errno
244 */
245
246int gfs2_inode_refresh(struct gfs2_inode *ip)
247{
248 struct buffer_head *dibh;
249 int error;
250
251 error = gfs2_meta_inode_buffer(ip, &dibh);
252 if (error)
253 return error;
254
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400255 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256 brelse(dibh);
257 return -EIO;
258 }
259
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500260 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262 ip->i_vn = ip->i_gl->gl_vn;
263
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500264 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265}
266
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400267int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400269 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270 struct gfs2_alloc *al;
271 struct gfs2_rgrpd *rgd;
272 int error;
273
274 if (ip->i_di.di_blocks != 1) {
275 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500276 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277 return -EIO;
278 }
279
280 al = gfs2_alloc_get(ip);
281
282 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
283 if (error)
284 goto out;
285
286 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
287 if (error)
288 goto out_qs;
289
290 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
291 if (!rgd) {
292 gfs2_consist_inode(ip);
293 error = -EIO;
294 goto out_rindex_relse;
295 }
296
297 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
298 &al->al_rgd_gh);
299 if (error)
300 goto out_rindex_relse;
301
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400302 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000303 if (error)
304 goto out_rg_gunlock;
305
306 gfs2_trans_add_gl(ip->i_gl);
307
308 gfs2_free_di(rgd, ip);
309
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310 gfs2_trans_end(sdp);
311 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
312
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400313out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400315out_rindex_relse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400317out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318 gfs2_quota_unhold(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400319out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400320 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321 return error;
322}
323
324/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000325 * gfs2_change_nlink - Change nlink count on inode
326 * @ip: The GFS2 inode
327 * @diff: The change in the nlink count required
328 *
329 * Returns: errno
330 */
331
332int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
333{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400334 struct gfs2_sbd *sdp = ip->i_inode.i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400336 u32 nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 int error;
338
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400339 BUG_ON(ip->i_di.di_nlink != ip->i_inode.i_nlink);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340 nlink = ip->i_di.di_nlink + diff;
341
342 /* If we are reducing the nlink count, but the new value ends up being
343 bigger than the old one, we must have underflowed. */
344 if (diff < 0 && nlink > ip->i_di.di_nlink) {
345 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500346 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347 return -EIO;
348 }
349
350 error = gfs2_meta_inode_buffer(ip, &dibh);
351 if (error)
352 return error;
353
354 ip->i_di.di_nlink = nlink;
355 ip->i_di.di_ctime = get_seconds();
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400356 ip->i_inode.i_nlink = nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000358 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500359 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400361 mark_inode_dirty(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400363 if (ip->i_di.di_nlink == 0) {
364 struct gfs2_rgrpd *rgd;
365 struct gfs2_holder ri_gh, rg_gh;
366
367 error = gfs2_rindex_hold(sdp, &ri_gh);
368 if (error)
369 goto out;
370 error = -EIO;
371 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
372 if (!rgd)
373 goto out_norgrp;
374 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
375 if (error)
376 goto out_norgrp;
377
Steven Whitehousef92a0b62006-10-02 16:01:53 -0400378 clear_nlink(&ip->i_inode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400379 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
380 gfs2_glock_dq_uninit(&rg_gh);
381out_norgrp:
382 gfs2_glock_dq_uninit(&ri_gh);
383 }
384out:
385 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000386}
387
Steven Whitehousec7526662006-03-20 12:30:04 -0500388struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
389{
390 struct qstr qstr;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500391 gfs2_str2qstr(&qstr, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500392 return gfs2_lookupi(dip, &qstr, 1, NULL);
393}
394
395
David Teiglandb3b94fa2006-01-16 16:50:04 +0000396/**
397 * gfs2_lookupi - Look up a filename in a directory and return its inode
398 * @d_gh: An initialized holder for the directory glock
399 * @name: The name of the inode to look for
400 * @is_root: If 1, ignore the caller's permissions
401 * @i_gh: An uninitialized holder for the new inode glock
402 *
403 * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
404 * @is_root is true.
405 *
406 * Returns: errno
407 */
408
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400409struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
410 int is_root, struct nameidata *nd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500412 struct super_block *sb = dir->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400413 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 struct gfs2_holder d_gh;
Al Viro629a21e2006-10-13 22:51:24 -0400415 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 unsigned int type;
Steven Whitehouse7359a192006-02-13 12:27:43 +0000417 int error = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500418 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419
420 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500421 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422
Steven Whitehousec7526662006-03-20 12:30:04 -0500423 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
424 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
425 dir == sb->s_root->d_inode)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400426 igrab(dir);
427 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000428 }
429
430 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
431 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500432 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000433
434 if (!is_root) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400435 error = permission(dir, MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000436 if (error)
437 goto out;
438 }
439
Steven Whitehousec7526662006-03-20 12:30:04 -0500440 error = gfs2_dir_search(dir, name, &inum, &type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441 if (error)
442 goto out;
443
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400444 inode = gfs2_inode_lookup(sb, &inum, type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445
Steven Whitehouse7359a192006-02-13 12:27:43 +0000446out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500448 if (error == -ENOENT)
449 return NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400450 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451}
452
Steven Whitehousecd915492006-09-04 12:49:07 -0400453static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400455 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000456 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400457 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458 int error;
459
460 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
461 if (error)
462 return error;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000463 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464
465 error = gfs2_meta_inode_buffer(ip, &bh);
466 if (error) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000467 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468 gfs2_trans_end(sdp);
469 return error;
470 }
471
472 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
473
474 if (ir.ir_length) {
475 *formal_ino = ir.ir_start++;
476 ir.ir_length--;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000477 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000478 gfs2_inum_range_out(&ir,
479 bh->b_data + sizeof(struct gfs2_dinode));
480 brelse(bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000481 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482 gfs2_trans_end(sdp);
483 return 0;
484 }
485
486 brelse(bh);
487
Steven Whitehousef55ab262006-02-21 12:51:39 +0000488 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 gfs2_trans_end(sdp);
490
491 return 1;
492}
493
Steven Whitehousecd915492006-09-04 12:49:07 -0400494static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000495{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400496 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
497 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000498 struct gfs2_holder gh;
499 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400500 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000501 int error;
502
503 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
504 if (error)
505 return error;
506
507 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
508 if (error)
509 goto out;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000510 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000511
512 error = gfs2_meta_inode_buffer(ip, &bh);
513 if (error)
514 goto out_end_trans;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400515
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
517
518 if (!ir.ir_length) {
519 struct buffer_head *m_bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400520 u64 x, y;
Al Virob44b84d2006-10-14 10:46:30 -0400521 __be64 z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522
523 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
524 if (error)
525 goto out_brelse;
526
Al Virob44b84d2006-10-14 10:46:30 -0400527 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
528 x = y = be64_to_cpu(z);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529 ir.ir_start = x;
530 ir.ir_length = GFS2_INUM_QUANTUM;
531 x += GFS2_INUM_QUANTUM;
532 if (x < y)
533 gfs2_consist_inode(m_ip);
Al Virob44b84d2006-10-14 10:46:30 -0400534 z = cpu_to_be64(x);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000535 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
Al Virob44b84d2006-10-14 10:46:30 -0400536 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537
538 brelse(m_bh);
539 }
540
541 *formal_ino = ir.ir_start++;
542 ir.ir_length--;
543
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000544 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
546
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400547out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548 brelse(bh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400549out_end_trans:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000550 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551 gfs2_trans_end(sdp);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400552out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554 return error;
555}
556
Steven Whitehousecd915492006-09-04 12:49:07 -0400557static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000558{
559 int error;
560
561 error = pick_formal_ino_1(sdp, inum);
562 if (error <= 0)
563 return error;
564
565 error = pick_formal_ino_2(sdp, inum);
566
567 return error;
568}
569
570/**
571 * create_ok - OK to create a new on-disk inode here?
572 * @dip: Directory in which dinode is to be created
573 * @name: Name of new dinode
574 * @mode:
575 *
576 * Returns: errno
577 */
578
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400579static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 unsigned int mode)
581{
582 int error;
583
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400584 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 if (error)
586 return error;
587
588 /* Don't create entries in an unlinked directory */
589 if (!dip->i_di.di_nlink)
590 return -EPERM;
591
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400592 error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593 switch (error) {
594 case -ENOENT:
595 error = 0;
596 break;
597 case 0:
598 return -EEXIST;
599 default:
600 return error;
601 }
602
Steven Whitehousecd915492006-09-04 12:49:07 -0400603 if (dip->i_di.di_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 return -EFBIG;
Steven Whitehousecd915492006-09-04 12:49:07 -0400605 if (S_ISDIR(mode) && dip->i_di.di_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606 return -EMLINK;
607
608 return 0;
609}
610
611static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
612 unsigned int *uid, unsigned int *gid)
613{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400614 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500615 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616 if (S_ISDIR(*mode))
617 *mode |= S_ISUID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500618 else if (dip->i_inode.i_uid != current->fsuid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619 *mode &= ~07111;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500620 *uid = dip->i_inode.i_uid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621 } else
622 *uid = current->fsuid;
623
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500624 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625 if (S_ISDIR(*mode))
626 *mode |= S_ISGID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500627 *gid = dip->i_inode.i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000628 } else
629 *gid = current->fsgid;
630}
631
Al Viro629a21e2006-10-13 22:51:24 -0400632static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum_host *inum,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400633 u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400635 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636 int error;
637
638 gfs2_alloc_get(dip);
639
640 dip->i_alloc.al_requested = RES_DINODE;
641 error = gfs2_inplace_reserve(dip);
642 if (error)
643 goto out;
644
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400645 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000646 if (error)
647 goto out_ipreserv;
648
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400649 inum->no_addr = gfs2_alloc_di(dip, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650
651 gfs2_trans_end(sdp);
652
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400653out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400655out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657 return error;
658}
659
660/**
661 * init_dinode - Fill in a new dinode structure
662 * @dip: the directory this inode is being created in
663 * @gl: The glock covering the new inode
664 * @inum: the inode number
665 * @mode: the file permissions
666 * @uid:
667 * @gid:
668 *
669 */
670
671static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400672 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400673 unsigned int uid, unsigned int gid,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500674 const u64 *generation, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400676 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000677 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 struct buffer_head *dibh;
679
680 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000681 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
683 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000684 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685
Steven Whitehouse2442a092006-01-30 11:49:32 +0000686 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
687 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000688 di->di_mode = cpu_to_be32(mode);
689 di->di_uid = cpu_to_be32(uid);
690 di->di_gid = cpu_to_be32(gid);
691 di->di_nlink = cpu_to_be32(0);
692 di->di_size = cpu_to_be64(0);
693 di->di_blocks = cpu_to_be64(1);
694 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500695 di->di_major = cpu_to_be32(MAJOR(dev));
696 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000697 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400698 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000699 di->di_flags = cpu_to_be32(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700
701 if (S_ISREG(mode)) {
702 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
703 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000704 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
706 gfs2_tune_get(sdp, gt_new_files_directio))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000707 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708 } else if (S_ISDIR(mode)) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500709 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
710 GFS2_DIF_INHERIT_DIRECTIO);
711 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
712 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713 }
714
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000715 di->__pad1 = 0;
Abhijith Dasb2a580d2006-07-10 12:36:12 -0500716 di->di_payload_format = cpu_to_be32(0);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000717 di->di_height = cpu_to_be32(0);
718 di->__pad2 = 0;
719 di->__pad3 = 0;
720 di->di_depth = cpu_to_be16(0);
721 di->di_entries = cpu_to_be32(0);
722 memset(&di->__pad4, 0, sizeof(di->__pad4));
723 di->di_eattr = cpu_to_be64(0);
724 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
725
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 brelse(dibh);
727}
728
729static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400730 unsigned int mode, const struct gfs2_inum_host *inum,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500731 const u64 *generation, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400733 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 unsigned int uid, gid;
735 int error;
736
737 munge_mode_uid_gid(dip, &mode, &uid, &gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 gfs2_alloc_get(dip);
739
740 error = gfs2_quota_lock(dip, uid, gid);
741 if (error)
742 goto out;
743
744 error = gfs2_quota_check(dip, uid, gid);
745 if (error)
746 goto out_quota;
747
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400748 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 if (error)
750 goto out_quota;
751
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500752 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754 gfs2_trans_end(sdp);
755
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400756out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400758out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 return error;
761}
762
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400763static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
764 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400766 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 struct gfs2_alloc *al;
768 int alloc_required;
769 struct buffer_head *dibh;
770 int error;
771
772 al = gfs2_alloc_get(dip);
773
774 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
775 if (error)
776 goto fail;
777
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400778 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500779 if (alloc_required < 0)
780 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 if (alloc_required) {
Steven Whitehouse2933f922006-11-01 13:23:29 -0500782 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 if (error)
784 goto fail_quota_locks;
785
786 al->al_requested = sdp->sd_max_dirres;
787
788 error = gfs2_inplace_reserve(dip);
789 if (error)
790 goto fail_quota_locks;
791
Steven Whitehouse320dd102006-05-18 16:25:27 -0400792 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 al->al_rgd->rd_ri.ri_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400794 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 RES_STATFS + RES_QUOTA, 0);
796 if (error)
797 goto fail_ipreserv;
798 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400799 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800 if (error)
801 goto fail_quota_locks;
802 }
803
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500804 error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805 if (error)
806 goto fail_end_trans;
807
808 error = gfs2_meta_inode_buffer(ip, &dibh);
809 if (error)
810 goto fail_end_trans;
811 ip->i_di.di_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000812 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500813 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000814 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 return 0;
816
Steven Whitehouse320dd102006-05-18 16:25:27 -0400817fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000818 gfs2_trans_end(sdp);
819
Steven Whitehouse320dd102006-05-18 16:25:27 -0400820fail_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000821 if (dip->i_alloc.al_rgd)
822 gfs2_inplace_release(dip);
823
Steven Whitehouse320dd102006-05-18 16:25:27 -0400824fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 gfs2_quota_unlock(dip);
826
Steven Whitehouse320dd102006-05-18 16:25:27 -0400827fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 return error;
830}
831
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400832static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
833{
834 int err;
835 size_t len;
836 void *value;
837 char *name;
838 struct gfs2_ea_request er;
839
840 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
841 &name, &value, &len);
842
843 if (err) {
844 if (err == -EOPNOTSUPP)
845 return 0;
846 return err;
847 }
848
849 memset(&er, 0, sizeof(struct gfs2_ea_request));
850
851 er.er_type = GFS2_EATYPE_SECURITY;
852 er.er_name = name;
853 er.er_data = value;
854 er.er_name_len = strlen(name);
855 er.er_data_len = len;
856
857 err = gfs2_ea_set_i(ip, &er);
858
859 kfree(value);
860 kfree(name);
861
862 return err;
863}
864
David Teiglandb3b94fa2006-01-16 16:50:04 +0000865/**
866 * gfs2_createi - Create a new inode
867 * @ghs: An array of two holders
868 * @name: The name of the new file
869 * @mode: the permissions on the new inode
870 *
871 * @ghs[0] is an initialized holder for the directory
872 * @ghs[1] is the holder for the inode lock
873 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000874 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 * file are held. A transaction has been started and an inplace reservation
876 * is held, as well.
877 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000878 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879 */
880
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400881struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500882 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883{
Steven Whitehouse7359a192006-02-13 12:27:43 +0000884 struct inode *inode;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500885 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400886 struct inode *dir = &dip->i_inode;
887 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Al Viro629a21e2006-10-13 22:51:24 -0400888 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400890 u64 generation;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891
892 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000893 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
896 error = gfs2_glock_nq(ghs);
897 if (error)
898 goto fail;
899
900 error = create_ok(dip, name, mode);
901 if (error)
902 goto fail_gunlock;
903
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400904 error = pick_formal_ino(sdp, &inum.no_formal_ino);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000905 if (error)
906 goto fail_gunlock;
907
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400908 error = alloc_dinode(dip, &inum, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909 if (error)
910 goto fail_gunlock;
911
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400912 if (inum.no_addr < dip->i_num.no_addr) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913 gfs2_glock_dq(ghs);
914
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400915 error = gfs2_glock_nq_num(sdp, inum.no_addr,
Steven Whitehouse320dd102006-05-18 16:25:27 -0400916 &gfs2_inode_glops, LM_ST_EXCLUSIVE,
917 GL_SKIP, ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 if (error) {
Steven Whitehouse7359a192006-02-13 12:27:43 +0000919 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 }
921
922 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
923 error = gfs2_glock_nq(ghs);
924 if (error) {
925 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000926 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927 }
928
929 error = create_ok(dip, name, mode);
930 if (error)
931 goto fail_gunlock2;
932 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400933 error = gfs2_glock_nq_num(sdp, inum.no_addr,
Steven Whitehouse320dd102006-05-18 16:25:27 -0400934 &gfs2_inode_glops, LM_ST_EXCLUSIVE,
935 GL_SKIP, ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936 if (error)
937 goto fail_gunlock;
938 }
939
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500940 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941 if (error)
942 goto fail_gunlock2;
943
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400944 inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode));
945 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946 goto fail_gunlock2;
947
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400948 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949 if (error)
950 goto fail_iput;
951
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400952 error = gfs2_acl_create(dip, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953 if (error)
954 goto fail_iput;
955
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400956 error = gfs2_security_init(dip, GFS2_I(inode));
957 if (error)
958 goto fail_iput;
959
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400960 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961 if (error)
962 goto fail_iput;
963
Steven Whitehouse7359a192006-02-13 12:27:43 +0000964 if (!inode)
965 return ERR_PTR(-ENOMEM);
966 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967
Steven Whitehouse320dd102006-05-18 16:25:27 -0400968fail_iput:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400969 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400970fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400972fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000973 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400974fail:
Steven Whitehouse7359a192006-02-13 12:27:43 +0000975 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976}
977
978/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 * gfs2_rmdiri - Remove a directory
980 * @dip: The parent directory of the directory to be removed
981 * @name: The name of the directory to be removed
982 * @ip: The GFS2 inode of the directory to be removed
983 *
984 * Assumes Glocks on dip and ip are held
985 *
986 * Returns: errno
987 */
988
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400989int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
990 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000991{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992 struct qstr dotname;
993 int error;
994
995 if (ip->i_di.di_entries != 2) {
996 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500997 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000998 return -EIO;
999 }
1000
1001 error = gfs2_dir_del(dip, name);
1002 if (error)
1003 return error;
1004
1005 error = gfs2_change_nlink(dip, -1);
1006 if (error)
1007 return error;
1008
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001009 gfs2_str2qstr(&dotname, ".");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001010 error = gfs2_dir_del(ip, &dotname);
1011 if (error)
1012 return error;
1013
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001014 gfs2_str2qstr(&dotname, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001015 error = gfs2_dir_del(ip, &dotname);
1016 if (error)
1017 return error;
1018
1019 error = gfs2_change_nlink(ip, -2);
1020 if (error)
1021 return error;
1022
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023 return error;
1024}
1025
1026/*
1027 * gfs2_unlink_ok - check to see that a inode is still in a directory
1028 * @dip: the directory
1029 * @name: the name of the file
1030 * @ip: the inode
1031 *
1032 * Assumes that the lock on (at least) @dip is held.
1033 *
1034 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1035 */
1036
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001037int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038 struct gfs2_inode *ip)
1039{
Al Viro629a21e2006-10-13 22:51:24 -04001040 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001041 unsigned int type;
1042 int error;
1043
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001044 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045 return -EPERM;
1046
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001047 if ((dip->i_inode.i_mode & S_ISVTX) &&
Steven Whitehouse2933f922006-11-01 13:23:29 -05001048 dip->i_inode.i_uid != current->fsuid &&
1049 ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001050 return -EPERM;
1051
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001052 if (IS_APPEND(&dip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 return -EPERM;
1054
Steven Whitehousefaf450e2006-06-22 10:59:10 -04001055 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056 if (error)
1057 return error;
1058
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001059 error = gfs2_dir_search(&dip->i_inode, name, &inum, &type);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001060 if (error)
1061 return error;
1062
1063 if (!gfs2_inum_equal(&inum, &ip->i_num))
1064 return -ENOENT;
1065
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001066 if (IF2DT(ip->i_inode.i_mode) != type) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067 gfs2_consist_inode(dip);
1068 return -EIO;
1069 }
1070
1071 return 0;
1072}
1073
1074/*
1075 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1076 * @this: move this
1077 * @to: to here
1078 *
1079 * Follow @to back to the root and make sure we don't encounter @this
1080 * Assumes we already hold the rename lock.
1081 *
1082 * Returns: errno
1083 */
1084
1085int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1086{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001087 struct inode *dir = &to->i_inode;
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001088 struct super_block *sb = dir->i_sb;
Steven Whitehouse7359a192006-02-13 12:27:43 +00001089 struct inode *tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001090 struct qstr dotdot;
1091 int error = 0;
1092
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001093 gfs2_str2qstr(&dotdot, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001094
Steven Whitehouse7359a192006-02-13 12:27:43 +00001095 igrab(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096
1097 for (;;) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001098 if (dir == &this->i_inode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 error = -EINVAL;
1100 break;
1101 }
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001102 if (dir == sb->s_root->d_inode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103 error = 0;
1104 break;
1105 }
1106
Steven Whitehousec7526662006-03-20 12:30:04 -05001107 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1108 if (IS_ERR(tmp)) {
1109 error = PTR_ERR(tmp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001110 break;
Steven Whitehousec7526662006-03-20 12:30:04 -05001111 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001112
Steven Whitehouse7359a192006-02-13 12:27:43 +00001113 iput(dir);
1114 dir = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115 }
1116
Steven Whitehouse7359a192006-02-13 12:27:43 +00001117 iput(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118
1119 return error;
1120}
1121
1122/**
1123 * gfs2_readlinki - return the contents of a symlink
1124 * @ip: the symlink's inode
1125 * @buf: a pointer to the buffer to be filled
1126 * @len: a pointer to the length of @buf
1127 *
1128 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1129 * to be freed by the caller.
1130 *
1131 * Returns: errno
1132 */
1133
1134int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1135{
1136 struct gfs2_holder i_gh;
1137 struct buffer_head *dibh;
1138 unsigned int x;
1139 int error;
1140
1141 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1142 error = gfs2_glock_nq_atime(&i_gh);
1143 if (error) {
1144 gfs2_holder_uninit(&i_gh);
1145 return error;
1146 }
1147
1148 if (!ip->i_di.di_size) {
1149 gfs2_consist_inode(ip);
1150 error = -EIO;
1151 goto out;
1152 }
1153
1154 error = gfs2_meta_inode_buffer(ip, &dibh);
1155 if (error)
1156 goto out;
1157
1158 x = ip->i_di.di_size + 1;
1159 if (x > *len) {
1160 *buf = kmalloc(x, GFP_KERNEL);
1161 if (!*buf) {
1162 error = -ENOMEM;
1163 goto out_brelse;
1164 }
1165 }
1166
1167 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1168 *len = x;
1169
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001170out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001172out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001173 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001174 return error;
1175}
1176
1177/**
1178 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1179 * conditionally update the inode's atime
1180 * @gh: the holder to acquire
1181 *
1182 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1183 * Update if the difference between the current time and the inode's current
1184 * atime is greater than an interval specified at mount.
1185 *
1186 * Returns: errno
1187 */
1188
1189int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1190{
1191 struct gfs2_glock *gl = gh->gh_gl;
1192 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001193 struct gfs2_inode *ip = gl->gl_object;
Steven Whitehousecd915492006-09-04 12:49:07 -04001194 s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 unsigned int state;
1196 int flags;
1197 int error;
1198
1199 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1200 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1201 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1202 return -EINVAL;
1203
1204 state = gh->gh_state;
1205 flags = gh->gh_flags;
1206
1207 error = gfs2_glock_nq(gh);
1208 if (error)
1209 return error;
1210
1211 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1212 (sdp->sd_vfs->s_flags & MS_RDONLY))
1213 return 0;
1214
1215 curtime = get_seconds();
1216 if (curtime - ip->i_di.di_atime >= quantum) {
1217 gfs2_glock_dq(gh);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001218 gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1219 gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001220 error = gfs2_glock_nq(gh);
1221 if (error)
1222 return error;
1223
1224 /* Verify that atime hasn't been updated while we were
1225 trying to get exclusive lock. */
1226
1227 curtime = get_seconds();
1228 if (curtime - ip->i_di.di_atime >= quantum) {
1229 struct buffer_head *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -04001230 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001231
1232 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1233 if (error == -EROFS)
1234 return 0;
1235 if (error)
1236 goto fail;
1237
1238 error = gfs2_meta_inode_buffer(ip, &dibh);
1239 if (error)
1240 goto fail_end_trans;
1241
1242 ip->i_di.di_atime = curtime;
1243
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001244 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse48516ce2006-10-02 12:39:19 -04001245 di = (struct gfs2_dinode *)dibh->b_data;
1246 di->di_atime = cpu_to_be64(ip->i_di.di_atime);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001247 brelse(dibh);
1248
1249 gfs2_trans_end(sdp);
1250 }
1251
1252 /* If someone else has asked for the glock,
1253 unlock and let them have it. Then reacquire
1254 in the original state. */
1255 if (gfs2_glock_is_blocking(gl)) {
1256 gfs2_glock_dq(gh);
1257 gfs2_holder_reinit(state, flags, gh);
1258 return gfs2_glock_nq(gh);
1259 }
1260 }
1261
1262 return 0;
1263
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001264fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001265 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001266fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001267 gfs2_glock_dq(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001268 return error;
1269}
1270
1271/**
1272 * glock_compare_atime - Compare two struct gfs2_glock structures for sort
1273 * @arg_a: the first structure
1274 * @arg_b: the second structure
1275 *
1276 * Returns: 1 if A > B
1277 * -1 if A < B
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001278 * 0 if A == B
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279 */
1280
1281static int glock_compare_atime(const void *arg_a, const void *arg_b)
1282{
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001283 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1284 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1285 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1286 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001287
1288 if (a->ln_number > b->ln_number)
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001289 return 1;
1290 if (a->ln_number < b->ln_number)
1291 return -1;
1292 if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE)
1293 return 1;
1294 if (gh_a->gh_state == LM_ST_SHARED && (gh_b->gh_flags & GL_ATIME))
1295 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001296
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001297 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001298}
1299
1300/**
1301 * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
1302 * atime update
1303 * @num_gh: the number of structures
1304 * @ghs: an array of struct gfs2_holder structures
1305 *
1306 * Returns: 0 on success (all glocks acquired),
1307 * errno on failure (no glocks acquired)
1308 */
1309
1310int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
1311{
1312 struct gfs2_holder **p;
1313 unsigned int x;
1314 int error = 0;
1315
1316 if (!num_gh)
1317 return 0;
1318
1319 if (num_gh == 1) {
1320 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1321 if (ghs->gh_flags & GL_ATIME)
1322 error = gfs2_glock_nq_atime(ghs);
1323 else
1324 error = gfs2_glock_nq(ghs);
1325 return error;
1326 }
1327
1328 p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1329 if (!p)
1330 return -ENOMEM;
1331
1332 for (x = 0; x < num_gh; x++)
1333 p[x] = &ghs[x];
1334
1335 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
1336
1337 for (x = 0; x < num_gh; x++) {
1338 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1339
1340 if (p[x]->gh_flags & GL_ATIME)
1341 error = gfs2_glock_nq_atime(p[x]);
1342 else
1343 error = gfs2_glock_nq(p[x]);
1344
1345 if (error) {
1346 while (x--)
1347 gfs2_glock_dq(p[x]);
1348 break;
1349 }
1350 }
1351
1352 kfree(p);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001353 return error;
1354}
1355
David Teiglandb3b94fa2006-01-16 16:50:04 +00001356
1357static int
1358__gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1359{
1360 struct buffer_head *dibh;
1361 int error;
1362
1363 error = gfs2_meta_inode_buffer(ip, &dibh);
1364 if (!error) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001365 error = inode_setattr(&ip->i_inode, attr);
1366 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001367 gfs2_inode_attr_out(ip);
1368
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001369 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001370 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001371 brelse(dibh);
1372 }
1373 return error;
1374}
1375
1376/**
1377 * gfs2_setattr_simple -
1378 * @ip:
1379 * @attr:
1380 *
1381 * Called with a reference on the vnode.
1382 *
1383 * Returns: errno
1384 */
1385
1386int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1387{
1388 int error;
1389
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001390 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001391 return __gfs2_setattr_simple(ip, attr);
1392
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001393 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001394 if (error)
1395 return error;
1396
1397 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001398 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001399 return error;
1400}
1401