blob: de466043c91a508af8953565babceff55f1bdbe2 [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;
55 inode->i_uid = di->di_uid;
56 inode->i_gid = di->di_gid;
57 i_size_write(inode, di->di_size);
58 inode->i_atime.tv_sec = di->di_atime;
59 inode->i_mtime.tv_sec = di->di_mtime;
60 inode->i_ctime.tv_sec = di->di_ctime;
David Teiglandb3b94fa2006-01-16 16:50:04 +000061 inode->i_atime.tv_nsec = 0;
62 inode->i_mtime.tv_nsec = 0;
63 inode->i_ctime.tv_nsec = 0;
Steven Whitehousec2668712006-09-04 13:55:48 -040064 inode->i_blocks = di->di_blocks <<
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040065 (GFS2_SB(inode)->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT);
David Teiglandb3b94fa2006-01-16 16:50:04 +000066
Steven Whitehousec2668712006-09-04 13:55:48 -040067 if (di->di_flags & GFS2_DIF_IMMUTABLE)
David Teiglandb3b94fa2006-01-16 16:50:04 +000068 inode->i_flags |= S_IMMUTABLE;
69 else
70 inode->i_flags &= ~S_IMMUTABLE;
71
Steven Whitehousec2668712006-09-04 13:55:48 -040072 if (di->di_flags & GFS2_DIF_APPENDONLY)
David Teiglandb3b94fa2006-01-16 16:50:04 +000073 inode->i_flags |= S_APPEND;
74 else
75 inode->i_flags &= ~S_APPEND;
76}
77
78/**
David Teiglandb3b94fa2006-01-16 16:50:04 +000079 * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode
80 * @ip: The GFS2 inode
81 *
82 * Only copy out the attributes that we want the VFS layer
83 * to be able to modify.
84 */
85
86void gfs2_inode_attr_out(struct gfs2_inode *ip)
87{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040088 struct inode *inode = &ip->i_inode;
Al Viro3ca68df2006-10-13 20:11:25 -040089 struct gfs2_dinode_host *di = &ip->i_di;
Steven Whitehouse75d3b812006-09-04 11:41:31 -040090 di->di_uid = inode->i_uid;
91 di->di_gid = inode->i_gid;
92 di->di_atime = inode->i_atime.tv_sec;
93 di->di_mtime = inode->i_mtime.tv_sec;
94 di->di_ctime = inode->i_ctime.tv_sec;
David Teiglandb3b94fa2006-01-16 16:50:04 +000095}
96
David Teiglandb3b94fa2006-01-16 16:50:04 +000097static int iget_test(struct inode *inode, void *opaque)
98{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040099 struct gfs2_inode *ip = GFS2_I(inode);
Al Viro629a21e2006-10-13 22:51:24 -0400100 struct gfs2_inum_host *inum = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000101
102 if (ip && ip->i_num.no_addr == inum->no_addr)
103 return 1;
104
105 return 0;
106}
107
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400108static int iget_set(struct inode *inode, void *opaque)
109{
110 struct gfs2_inode *ip = GFS2_I(inode);
Al Viro629a21e2006-10-13 22:51:24 -0400111 struct gfs2_inum_host *inum = opaque;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400112
113 ip->i_num = *inum;
114 return 0;
115}
116
Al Viro629a21e2006-10-13 22:51:24 -0400117struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum_host *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118{
119 return ilookup5(sb, (unsigned long)inum->no_formal_ino,
120 iget_test, inum);
121}
122
Al Viro629a21e2006-10-13 22:51:24 -0400123static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum_host *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000124{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400125 return iget5_locked(sb, (unsigned long)inum->no_formal_ino,
126 iget_test, iget_set, inum);
127}
128
129/**
130 * gfs2_inode_lookup - Lookup an inode
131 * @sb: The super block
132 * @inum: The inode number
133 * @type: The type of the inode
134 *
135 * Returns: A VFS inode, or an error
136 */
137
Al Viro629a21e2006-10-13 22:51:24 -0400138struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum_host *inum, unsigned int type)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400139{
140 struct inode *inode = gfs2_iget(sb, inum);
141 struct gfs2_inode *ip = GFS2_I(inode);
142 struct gfs2_glock *io_gl;
143 int error;
144
Steven Whitehouse26d83de2006-10-30 16:59:08 -0500145 if (!inode)
146 return ERR_PTR(-ENOBUFS);
147
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400148 if (inode->i_state & I_NEW) {
149 struct gfs2_sbd *sdp = GFS2_SB(inode);
150 umode_t mode = DT2IF(type);
Theodore Ts'obba9dfd2006-09-27 01:50:31 -0700151 inode->i_private = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400152 inode->i_mode = mode;
153
154 if (S_ISREG(mode)) {
155 inode->i_op = &gfs2_file_iops;
156 inode->i_fop = &gfs2_file_fops;
157 inode->i_mapping->a_ops = &gfs2_file_aops;
158 } else if (S_ISDIR(mode)) {
159 inode->i_op = &gfs2_dir_iops;
160 inode->i_fop = &gfs2_dir_fops;
161 } else if (S_ISLNK(mode)) {
162 inode->i_op = &gfs2_symlink_iops;
163 } else {
164 inode->i_op = &gfs2_dev_iops;
165 }
166
167 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
168 if (unlikely(error))
169 goto fail;
170 ip->i_gl->gl_object = ip;
171
172 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
173 if (unlikely(error))
174 goto fail_put;
175
176 ip->i_vn = ip->i_gl->gl_vn - 1;
177 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
178 if (unlikely(error))
179 goto fail_iopen;
180
181 gfs2_glock_put(io_gl);
182 unlock_new_inode(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400184
185 return inode;
186fail_iopen:
187 gfs2_glock_put(io_gl);
188fail_put:
189 ip->i_gl->gl_object = NULL;
190 gfs2_glock_put(ip->i_gl);
191fail:
192 iput(inode);
193 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194}
195
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500196static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
Steven Whitehouseea744d02006-10-31 15:28:00 -0500197{
198 struct gfs2_dinode_host *di = &ip->i_di;
199 const struct gfs2_dinode *str = buf;
200
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500201 if (ip->i_num.no_addr != be64_to_cpu(str->di_num.no_addr)) {
202 if (gfs2_consist_inode(ip))
203 gfs2_dinode_print(ip);
204 return -EIO;
205 }
206 if (ip->i_num.no_formal_ino != be64_to_cpu(str->di_num.no_formal_ino))
207 return -ESTALE;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500208
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500209 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500210 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500211 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500212 case S_IFBLK:
213 case S_IFCHR:
214 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
215 be32_to_cpu(str->di_minor));
216 break;
217 };
218
Steven Whitehouseea744d02006-10-31 15:28:00 -0500219 di->di_uid = be32_to_cpu(str->di_uid);
220 di->di_gid = be32_to_cpu(str->di_gid);
221 di->di_nlink = be32_to_cpu(str->di_nlink);
222 di->di_size = be64_to_cpu(str->di_size);
223 di->di_blocks = be64_to_cpu(str->di_blocks);
224 di->di_atime = be64_to_cpu(str->di_atime);
225 di->di_mtime = be64_to_cpu(str->di_mtime);
226 di->di_ctime = be64_to_cpu(str->di_ctime);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500227
228 di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
229 di->di_goal_data = be64_to_cpu(str->di_goal_data);
230 di->di_generation = be64_to_cpu(str->di_generation);
231
232 di->di_flags = be32_to_cpu(str->di_flags);
233 di->di_payload_format = be32_to_cpu(str->di_payload_format);
234 di->di_height = be16_to_cpu(str->di_height);
235
236 di->di_depth = be16_to_cpu(str->di_depth);
237 di->di_entries = be32_to_cpu(str->di_entries);
238
239 di->di_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500240 return 0;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500241}
242
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243/**
244 * gfs2_inode_refresh - Refresh the incore copy of the dinode
245 * @ip: The GFS2 inode
246 *
247 * Returns: errno
248 */
249
250int gfs2_inode_refresh(struct gfs2_inode *ip)
251{
252 struct buffer_head *dibh;
253 int error;
254
255 error = gfs2_meta_inode_buffer(ip, &dibh);
256 if (error)
257 return error;
258
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400259 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260 brelse(dibh);
261 return -EIO;
262 }
263
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500264 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000266 ip->i_vn = ip->i_gl->gl_vn;
267
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500268 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269}
270
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400271int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400273 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274 struct gfs2_alloc *al;
275 struct gfs2_rgrpd *rgd;
276 int error;
277
278 if (ip->i_di.di_blocks != 1) {
279 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500280 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281 return -EIO;
282 }
283
284 al = gfs2_alloc_get(ip);
285
286 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
287 if (error)
288 goto out;
289
290 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
291 if (error)
292 goto out_qs;
293
294 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
295 if (!rgd) {
296 gfs2_consist_inode(ip);
297 error = -EIO;
298 goto out_rindex_relse;
299 }
300
301 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
302 &al->al_rgd_gh);
303 if (error)
304 goto out_rindex_relse;
305
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400306 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307 if (error)
308 goto out_rg_gunlock;
309
310 gfs2_trans_add_gl(ip->i_gl);
311
312 gfs2_free_di(rgd, ip);
313
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314 gfs2_trans_end(sdp);
315 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
316
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400317out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400319out_rindex_relse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000320 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400321out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322 gfs2_quota_unhold(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400323out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400324 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000325 return error;
326}
327
328/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000329 * gfs2_change_nlink - Change nlink count on inode
330 * @ip: The GFS2 inode
331 * @diff: The change in the nlink count required
332 *
333 * Returns: errno
334 */
335
336int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
337{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400338 struct gfs2_sbd *sdp = ip->i_inode.i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400340 u32 nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341 int error;
342
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400343 BUG_ON(ip->i_di.di_nlink != ip->i_inode.i_nlink);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344 nlink = ip->i_di.di_nlink + diff;
345
346 /* If we are reducing the nlink count, but the new value ends up being
347 bigger than the old one, we must have underflowed. */
348 if (diff < 0 && nlink > ip->i_di.di_nlink) {
349 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500350 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351 return -EIO;
352 }
353
354 error = gfs2_meta_inode_buffer(ip, &dibh);
355 if (error)
356 return error;
357
358 ip->i_di.di_nlink = nlink;
359 ip->i_di.di_ctime = get_seconds();
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400360 ip->i_inode.i_nlink = nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000362 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500363 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000364 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400365 mark_inode_dirty(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400367 if (ip->i_di.di_nlink == 0) {
368 struct gfs2_rgrpd *rgd;
369 struct gfs2_holder ri_gh, rg_gh;
370
371 error = gfs2_rindex_hold(sdp, &ri_gh);
372 if (error)
373 goto out;
374 error = -EIO;
375 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
376 if (!rgd)
377 goto out_norgrp;
378 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
379 if (error)
380 goto out_norgrp;
381
Steven Whitehousef92a0b62006-10-02 16:01:53 -0400382 clear_nlink(&ip->i_inode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400383 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
384 gfs2_glock_dq_uninit(&rg_gh);
385out_norgrp:
386 gfs2_glock_dq_uninit(&ri_gh);
387 }
388out:
389 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390}
391
Steven Whitehousec7526662006-03-20 12:30:04 -0500392struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
393{
394 struct qstr qstr;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500395 gfs2_str2qstr(&qstr, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500396 return gfs2_lookupi(dip, &qstr, 1, NULL);
397}
398
399
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400/**
401 * gfs2_lookupi - Look up a filename in a directory and return its inode
402 * @d_gh: An initialized holder for the directory glock
403 * @name: The name of the inode to look for
404 * @is_root: If 1, ignore the caller's permissions
405 * @i_gh: An uninitialized holder for the new inode glock
406 *
407 * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
408 * @is_root is true.
409 *
410 * Returns: errno
411 */
412
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400413struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
414 int is_root, struct nameidata *nd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500416 struct super_block *sb = dir->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400417 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418 struct gfs2_holder d_gh;
Al Viro629a21e2006-10-13 22:51:24 -0400419 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 unsigned int type;
Steven Whitehouse7359a192006-02-13 12:27:43 +0000421 int error = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500422 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423
424 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500425 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000426
Steven Whitehousec7526662006-03-20 12:30:04 -0500427 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
428 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
429 dir == sb->s_root->d_inode)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400430 igrab(dir);
431 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432 }
433
434 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
435 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500436 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437
438 if (!is_root) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400439 error = permission(dir, MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440 if (error)
441 goto out;
442 }
443
Steven Whitehousec7526662006-03-20 12:30:04 -0500444 error = gfs2_dir_search(dir, name, &inum, &type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445 if (error)
446 goto out;
447
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400448 inode = gfs2_inode_lookup(sb, &inum, type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449
Steven Whitehouse7359a192006-02-13 12:27:43 +0000450out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500452 if (error == -ENOENT)
453 return NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400454 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455}
456
Steven Whitehousecd915492006-09-04 12:49:07 -0400457static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400459 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400461 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000462 int error;
463
464 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
465 if (error)
466 return error;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000467 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468
469 error = gfs2_meta_inode_buffer(ip, &bh);
470 if (error) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000471 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000472 gfs2_trans_end(sdp);
473 return error;
474 }
475
476 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
477
478 if (ir.ir_length) {
479 *formal_ino = ir.ir_start++;
480 ir.ir_length--;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000481 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482 gfs2_inum_range_out(&ir,
483 bh->b_data + sizeof(struct gfs2_dinode));
484 brelse(bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000485 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000486 gfs2_trans_end(sdp);
487 return 0;
488 }
489
490 brelse(bh);
491
Steven Whitehousef55ab262006-02-21 12:51:39 +0000492 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493 gfs2_trans_end(sdp);
494
495 return 1;
496}
497
Steven Whitehousecd915492006-09-04 12:49:07 -0400498static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400500 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
501 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502 struct gfs2_holder gh;
503 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400504 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 int error;
506
507 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
508 if (error)
509 return error;
510
511 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
512 if (error)
513 goto out;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000514 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000515
516 error = gfs2_meta_inode_buffer(ip, &bh);
517 if (error)
518 goto out_end_trans;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400519
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
521
522 if (!ir.ir_length) {
523 struct buffer_head *m_bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400524 u64 x, y;
Al Virob44b84d2006-10-14 10:46:30 -0400525 __be64 z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526
527 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
528 if (error)
529 goto out_brelse;
530
Al Virob44b84d2006-10-14 10:46:30 -0400531 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
532 x = y = be64_to_cpu(z);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533 ir.ir_start = x;
534 ir.ir_length = GFS2_INUM_QUANTUM;
535 x += GFS2_INUM_QUANTUM;
536 if (x < y)
537 gfs2_consist_inode(m_ip);
Al Virob44b84d2006-10-14 10:46:30 -0400538 z = cpu_to_be64(x);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000539 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
Al Virob44b84d2006-10-14 10:46:30 -0400540 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541
542 brelse(m_bh);
543 }
544
545 *formal_ino = ir.ir_start++;
546 ir.ir_length--;
547
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000548 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
550
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400551out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552 brelse(bh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400553out_end_trans:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000554 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555 gfs2_trans_end(sdp);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400556out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000558 return error;
559}
560
Steven Whitehousecd915492006-09-04 12:49:07 -0400561static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562{
563 int error;
564
565 error = pick_formal_ino_1(sdp, inum);
566 if (error <= 0)
567 return error;
568
569 error = pick_formal_ino_2(sdp, inum);
570
571 return error;
572}
573
574/**
575 * create_ok - OK to create a new on-disk inode here?
576 * @dip: Directory in which dinode is to be created
577 * @name: Name of new dinode
578 * @mode:
579 *
580 * Returns: errno
581 */
582
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400583static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584 unsigned int mode)
585{
586 int error;
587
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400588 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589 if (error)
590 return error;
591
592 /* Don't create entries in an unlinked directory */
593 if (!dip->i_di.di_nlink)
594 return -EPERM;
595
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400596 error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597 switch (error) {
598 case -ENOENT:
599 error = 0;
600 break;
601 case 0:
602 return -EEXIST;
603 default:
604 return error;
605 }
606
Steven Whitehousecd915492006-09-04 12:49:07 -0400607 if (dip->i_di.di_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608 return -EFBIG;
Steven Whitehousecd915492006-09-04 12:49:07 -0400609 if (S_ISDIR(mode) && dip->i_di.di_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 return -EMLINK;
611
612 return 0;
613}
614
615static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
616 unsigned int *uid, unsigned int *gid)
617{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400618 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500619 (dip->i_inode.i_mode & S_ISUID) && dip->i_di.di_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620 if (S_ISDIR(*mode))
621 *mode |= S_ISUID;
622 else if (dip->i_di.di_uid != current->fsuid)
623 *mode &= ~07111;
624 *uid = dip->i_di.di_uid;
625 } else
626 *uid = current->fsuid;
627
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500628 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629 if (S_ISDIR(*mode))
630 *mode |= S_ISGID;
631 *gid = dip->i_di.di_gid;
632 } else
633 *gid = current->fsgid;
634}
635
Al Viro629a21e2006-10-13 22:51:24 -0400636static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum_host *inum,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400637 u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400639 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640 int error;
641
642 gfs2_alloc_get(dip);
643
644 dip->i_alloc.al_requested = RES_DINODE;
645 error = gfs2_inplace_reserve(dip);
646 if (error)
647 goto out;
648
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400649 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 if (error)
651 goto out_ipreserv;
652
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400653 inum->no_addr = gfs2_alloc_di(dip, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654
655 gfs2_trans_end(sdp);
656
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400657out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400659out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661 return error;
662}
663
664/**
665 * init_dinode - Fill in a new dinode structure
666 * @dip: the directory this inode is being created in
667 * @gl: The glock covering the new inode
668 * @inum: the inode number
669 * @mode: the file permissions
670 * @uid:
671 * @gid:
672 *
673 */
674
675static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400676 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400677 unsigned int uid, unsigned int gid,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500678 const u64 *generation, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400680 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000681 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682 struct buffer_head *dibh;
683
684 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000685 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000686 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
687 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000688 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689
Steven Whitehouse2442a092006-01-30 11:49:32 +0000690 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
691 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000692 di->di_mode = cpu_to_be32(mode);
693 di->di_uid = cpu_to_be32(uid);
694 di->di_gid = cpu_to_be32(gid);
695 di->di_nlink = cpu_to_be32(0);
696 di->di_size = cpu_to_be64(0);
697 di->di_blocks = cpu_to_be64(1);
698 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500699 di->di_major = cpu_to_be32(MAJOR(dev));
700 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000701 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400702 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000703 di->di_flags = cpu_to_be32(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000704
705 if (S_ISREG(mode)) {
706 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
707 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000708 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000709 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
710 gfs2_tune_get(sdp, gt_new_files_directio))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000711 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 } else if (S_ISDIR(mode)) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500713 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
714 GFS2_DIF_INHERIT_DIRECTIO);
715 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
716 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 }
718
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000719 di->__pad1 = 0;
Abhijith Dasb2a580d2006-07-10 12:36:12 -0500720 di->di_payload_format = cpu_to_be32(0);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000721 di->di_height = cpu_to_be32(0);
722 di->__pad2 = 0;
723 di->__pad3 = 0;
724 di->di_depth = cpu_to_be16(0);
725 di->di_entries = cpu_to_be32(0);
726 memset(&di->__pad4, 0, sizeof(di->__pad4));
727 di->di_eattr = cpu_to_be64(0);
728 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
729
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 brelse(dibh);
731}
732
733static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400734 unsigned int mode, const struct gfs2_inum_host *inum,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500735 const u64 *generation, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400737 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 unsigned int uid, gid;
739 int error;
740
741 munge_mode_uid_gid(dip, &mode, &uid, &gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 gfs2_alloc_get(dip);
743
744 error = gfs2_quota_lock(dip, uid, gid);
745 if (error)
746 goto out;
747
748 error = gfs2_quota_check(dip, uid, gid);
749 if (error)
750 goto out_quota;
751
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400752 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753 if (error)
754 goto out_quota;
755
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500756 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000758 gfs2_trans_end(sdp);
759
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400760out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400762out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 return error;
765}
766
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400767static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
768 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400770 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000771 struct gfs2_alloc *al;
772 int alloc_required;
773 struct buffer_head *dibh;
774 int error;
775
776 al = gfs2_alloc_get(dip);
777
778 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
779 if (error)
780 goto fail;
781
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400782 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500783 if (alloc_required < 0)
784 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 if (alloc_required) {
786 error = gfs2_quota_check(dip, dip->i_di.di_uid,
787 dip->i_di.di_gid);
788 if (error)
789 goto fail_quota_locks;
790
791 al->al_requested = sdp->sd_max_dirres;
792
793 error = gfs2_inplace_reserve(dip);
794 if (error)
795 goto fail_quota_locks;
796
Steven Whitehouse320dd102006-05-18 16:25:27 -0400797 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798 al->al_rgd->rd_ri.ri_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400799 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800 RES_STATFS + RES_QUOTA, 0);
801 if (error)
802 goto fail_ipreserv;
803 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400804 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805 if (error)
806 goto fail_quota_locks;
807 }
808
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500809 error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000810 if (error)
811 goto fail_end_trans;
812
813 error = gfs2_meta_inode_buffer(ip, &dibh);
814 if (error)
815 goto fail_end_trans;
816 ip->i_di.di_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000817 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500818 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000819 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820 return 0;
821
Steven Whitehouse320dd102006-05-18 16:25:27 -0400822fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000823 gfs2_trans_end(sdp);
824
Steven Whitehouse320dd102006-05-18 16:25:27 -0400825fail_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 if (dip->i_alloc.al_rgd)
827 gfs2_inplace_release(dip);
828
Steven Whitehouse320dd102006-05-18 16:25:27 -0400829fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000830 gfs2_quota_unlock(dip);
831
Steven Whitehouse320dd102006-05-18 16:25:27 -0400832fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834 return error;
835}
836
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400837static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
838{
839 int err;
840 size_t len;
841 void *value;
842 char *name;
843 struct gfs2_ea_request er;
844
845 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
846 &name, &value, &len);
847
848 if (err) {
849 if (err == -EOPNOTSUPP)
850 return 0;
851 return err;
852 }
853
854 memset(&er, 0, sizeof(struct gfs2_ea_request));
855
856 er.er_type = GFS2_EATYPE_SECURITY;
857 er.er_name = name;
858 er.er_data = value;
859 er.er_name_len = strlen(name);
860 er.er_data_len = len;
861
862 err = gfs2_ea_set_i(ip, &er);
863
864 kfree(value);
865 kfree(name);
866
867 return err;
868}
869
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870/**
871 * gfs2_createi - Create a new inode
872 * @ghs: An array of two holders
873 * @name: The name of the new file
874 * @mode: the permissions on the new inode
875 *
876 * @ghs[0] is an initialized holder for the directory
877 * @ghs[1] is the holder for the inode lock
878 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000879 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000880 * file are held. A transaction has been started and an inplace reservation
881 * is held, as well.
882 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000883 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000884 */
885
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400886struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500887 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888{
Steven Whitehouse7359a192006-02-13 12:27:43 +0000889 struct inode *inode;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500890 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400891 struct inode *dir = &dip->i_inode;
892 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Al Viro629a21e2006-10-13 22:51:24 -0400893 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400895 u64 generation;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896
897 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000898 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
901 error = gfs2_glock_nq(ghs);
902 if (error)
903 goto fail;
904
905 error = create_ok(dip, name, mode);
906 if (error)
907 goto fail_gunlock;
908
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400909 error = pick_formal_ino(sdp, &inum.no_formal_ino);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910 if (error)
911 goto fail_gunlock;
912
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400913 error = alloc_dinode(dip, &inum, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914 if (error)
915 goto fail_gunlock;
916
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400917 if (inum.no_addr < dip->i_num.no_addr) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 gfs2_glock_dq(ghs);
919
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400920 error = gfs2_glock_nq_num(sdp, inum.no_addr,
Steven Whitehouse320dd102006-05-18 16:25:27 -0400921 &gfs2_inode_glops, LM_ST_EXCLUSIVE,
922 GL_SKIP, ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923 if (error) {
Steven Whitehouse7359a192006-02-13 12:27:43 +0000924 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925 }
926
927 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
928 error = gfs2_glock_nq(ghs);
929 if (error) {
930 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000931 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 }
933
934 error = create_ok(dip, name, mode);
935 if (error)
936 goto fail_gunlock2;
937 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400938 error = gfs2_glock_nq_num(sdp, inum.no_addr,
Steven Whitehouse320dd102006-05-18 16:25:27 -0400939 &gfs2_inode_glops, LM_ST_EXCLUSIVE,
940 GL_SKIP, ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941 if (error)
942 goto fail_gunlock;
943 }
944
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500945 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946 if (error)
947 goto fail_gunlock2;
948
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400949 inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode));
950 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 goto fail_gunlock2;
952
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400953 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954 if (error)
955 goto fail_iput;
956
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400957 error = gfs2_acl_create(dip, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958 if (error)
959 goto fail_iput;
960
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400961 error = gfs2_security_init(dip, GFS2_I(inode));
962 if (error)
963 goto fail_iput;
964
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400965 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 if (error)
967 goto fail_iput;
968
Steven Whitehouse7359a192006-02-13 12:27:43 +0000969 if (!inode)
970 return ERR_PTR(-ENOMEM);
971 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972
Steven Whitehouse320dd102006-05-18 16:25:27 -0400973fail_iput:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400974 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400975fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400977fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400979fail:
Steven Whitehouse7359a192006-02-13 12:27:43 +0000980 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981}
982
983/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000984 * gfs2_rmdiri - Remove a directory
985 * @dip: The parent directory of the directory to be removed
986 * @name: The name of the directory to be removed
987 * @ip: The GFS2 inode of the directory to be removed
988 *
989 * Assumes Glocks on dip and ip are held
990 *
991 * Returns: errno
992 */
993
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400994int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
995 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000997 struct qstr dotname;
998 int error;
999
1000 if (ip->i_di.di_entries != 2) {
1001 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -05001002 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003 return -EIO;
1004 }
1005
1006 error = gfs2_dir_del(dip, name);
1007 if (error)
1008 return error;
1009
1010 error = gfs2_change_nlink(dip, -1);
1011 if (error)
1012 return error;
1013
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001014 gfs2_str2qstr(&dotname, ".");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001015 error = gfs2_dir_del(ip, &dotname);
1016 if (error)
1017 return error;
1018
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001019 gfs2_str2qstr(&dotname, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020 error = gfs2_dir_del(ip, &dotname);
1021 if (error)
1022 return error;
1023
1024 error = gfs2_change_nlink(ip, -2);
1025 if (error)
1026 return error;
1027
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028 return error;
1029}
1030
1031/*
1032 * gfs2_unlink_ok - check to see that a inode is still in a directory
1033 * @dip: the directory
1034 * @name: the name of the file
1035 * @ip: the inode
1036 *
1037 * Assumes that the lock on (at least) @dip is held.
1038 *
1039 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1040 */
1041
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001042int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001043 struct gfs2_inode *ip)
1044{
Al Viro629a21e2006-10-13 22:51:24 -04001045 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046 unsigned int type;
1047 int error;
1048
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001049 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001050 return -EPERM;
1051
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001052 if ((dip->i_inode.i_mode & S_ISVTX) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 dip->i_di.di_uid != current->fsuid &&
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001054 ip->i_di.di_uid != current->fsuid && !capable(CAP_FOWNER))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001055 return -EPERM;
1056
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001057 if (IS_APPEND(&dip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058 return -EPERM;
1059
Steven Whitehousefaf450e2006-06-22 10:59:10 -04001060 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 if (error)
1062 return error;
1063
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001064 error = gfs2_dir_search(&dip->i_inode, name, &inum, &type);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001065 if (error)
1066 return error;
1067
1068 if (!gfs2_inum_equal(&inum, &ip->i_num))
1069 return -ENOENT;
1070
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001071 if (IF2DT(ip->i_inode.i_mode) != type) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072 gfs2_consist_inode(dip);
1073 return -EIO;
1074 }
1075
1076 return 0;
1077}
1078
1079/*
1080 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1081 * @this: move this
1082 * @to: to here
1083 *
1084 * Follow @to back to the root and make sure we don't encounter @this
1085 * Assumes we already hold the rename lock.
1086 *
1087 * Returns: errno
1088 */
1089
1090int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1091{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001092 struct inode *dir = &to->i_inode;
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001093 struct super_block *sb = dir->i_sb;
Steven Whitehouse7359a192006-02-13 12:27:43 +00001094 struct inode *tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001095 struct qstr dotdot;
1096 int error = 0;
1097
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001098 gfs2_str2qstr(&dotdot, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099
Steven Whitehouse7359a192006-02-13 12:27:43 +00001100 igrab(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101
1102 for (;;) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001103 if (dir == &this->i_inode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001104 error = -EINVAL;
1105 break;
1106 }
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001107 if (dir == sb->s_root->d_inode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001108 error = 0;
1109 break;
1110 }
1111
Steven Whitehousec7526662006-03-20 12:30:04 -05001112 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1113 if (IS_ERR(tmp)) {
1114 error = PTR_ERR(tmp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115 break;
Steven Whitehousec7526662006-03-20 12:30:04 -05001116 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001117
Steven Whitehouse7359a192006-02-13 12:27:43 +00001118 iput(dir);
1119 dir = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001120 }
1121
Steven Whitehouse7359a192006-02-13 12:27:43 +00001122 iput(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123
1124 return error;
1125}
1126
1127/**
1128 * gfs2_readlinki - return the contents of a symlink
1129 * @ip: the symlink's inode
1130 * @buf: a pointer to the buffer to be filled
1131 * @len: a pointer to the length of @buf
1132 *
1133 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1134 * to be freed by the caller.
1135 *
1136 * Returns: errno
1137 */
1138
1139int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1140{
1141 struct gfs2_holder i_gh;
1142 struct buffer_head *dibh;
1143 unsigned int x;
1144 int error;
1145
1146 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1147 error = gfs2_glock_nq_atime(&i_gh);
1148 if (error) {
1149 gfs2_holder_uninit(&i_gh);
1150 return error;
1151 }
1152
1153 if (!ip->i_di.di_size) {
1154 gfs2_consist_inode(ip);
1155 error = -EIO;
1156 goto out;
1157 }
1158
1159 error = gfs2_meta_inode_buffer(ip, &dibh);
1160 if (error)
1161 goto out;
1162
1163 x = ip->i_di.di_size + 1;
1164 if (x > *len) {
1165 *buf = kmalloc(x, GFP_KERNEL);
1166 if (!*buf) {
1167 error = -ENOMEM;
1168 goto out_brelse;
1169 }
1170 }
1171
1172 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1173 *len = x;
1174
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001175out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001176 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001177out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001178 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001179 return error;
1180}
1181
1182/**
1183 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1184 * conditionally update the inode's atime
1185 * @gh: the holder to acquire
1186 *
1187 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1188 * Update if the difference between the current time and the inode's current
1189 * atime is greater than an interval specified at mount.
1190 *
1191 * Returns: errno
1192 */
1193
1194int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1195{
1196 struct gfs2_glock *gl = gh->gh_gl;
1197 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001198 struct gfs2_inode *ip = gl->gl_object;
Steven Whitehousecd915492006-09-04 12:49:07 -04001199 s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001200 unsigned int state;
1201 int flags;
1202 int error;
1203
1204 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1205 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1206 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1207 return -EINVAL;
1208
1209 state = gh->gh_state;
1210 flags = gh->gh_flags;
1211
1212 error = gfs2_glock_nq(gh);
1213 if (error)
1214 return error;
1215
1216 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1217 (sdp->sd_vfs->s_flags & MS_RDONLY))
1218 return 0;
1219
1220 curtime = get_seconds();
1221 if (curtime - ip->i_di.di_atime >= quantum) {
1222 gfs2_glock_dq(gh);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001223 gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1224 gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001225 error = gfs2_glock_nq(gh);
1226 if (error)
1227 return error;
1228
1229 /* Verify that atime hasn't been updated while we were
1230 trying to get exclusive lock. */
1231
1232 curtime = get_seconds();
1233 if (curtime - ip->i_di.di_atime >= quantum) {
1234 struct buffer_head *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -04001235 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001236
1237 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1238 if (error == -EROFS)
1239 return 0;
1240 if (error)
1241 goto fail;
1242
1243 error = gfs2_meta_inode_buffer(ip, &dibh);
1244 if (error)
1245 goto fail_end_trans;
1246
1247 ip->i_di.di_atime = curtime;
1248
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001249 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse48516ce2006-10-02 12:39:19 -04001250 di = (struct gfs2_dinode *)dibh->b_data;
1251 di->di_atime = cpu_to_be64(ip->i_di.di_atime);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252 brelse(dibh);
1253
1254 gfs2_trans_end(sdp);
1255 }
1256
1257 /* If someone else has asked for the glock,
1258 unlock and let them have it. Then reacquire
1259 in the original state. */
1260 if (gfs2_glock_is_blocking(gl)) {
1261 gfs2_glock_dq(gh);
1262 gfs2_holder_reinit(state, flags, gh);
1263 return gfs2_glock_nq(gh);
1264 }
1265 }
1266
1267 return 0;
1268
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001269fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001270 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001271fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001272 gfs2_glock_dq(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001273 return error;
1274}
1275
1276/**
1277 * glock_compare_atime - Compare two struct gfs2_glock structures for sort
1278 * @arg_a: the first structure
1279 * @arg_b: the second structure
1280 *
1281 * Returns: 1 if A > B
1282 * -1 if A < B
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001283 * 0 if A == B
David Teiglandb3b94fa2006-01-16 16:50:04 +00001284 */
1285
1286static int glock_compare_atime(const void *arg_a, const void *arg_b)
1287{
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001288 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1289 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1290 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1291 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292
1293 if (a->ln_number > b->ln_number)
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001294 return 1;
1295 if (a->ln_number < b->ln_number)
1296 return -1;
1297 if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE)
1298 return 1;
1299 if (gh_a->gh_state == LM_ST_SHARED && (gh_b->gh_flags & GL_ATIME))
1300 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001301
Steven Whitehouse75d3b812006-09-04 11:41:31 -04001302 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001303}
1304
1305/**
1306 * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
1307 * atime update
1308 * @num_gh: the number of structures
1309 * @ghs: an array of struct gfs2_holder structures
1310 *
1311 * Returns: 0 on success (all glocks acquired),
1312 * errno on failure (no glocks acquired)
1313 */
1314
1315int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
1316{
1317 struct gfs2_holder **p;
1318 unsigned int x;
1319 int error = 0;
1320
1321 if (!num_gh)
1322 return 0;
1323
1324 if (num_gh == 1) {
1325 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1326 if (ghs->gh_flags & GL_ATIME)
1327 error = gfs2_glock_nq_atime(ghs);
1328 else
1329 error = gfs2_glock_nq(ghs);
1330 return error;
1331 }
1332
1333 p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1334 if (!p)
1335 return -ENOMEM;
1336
1337 for (x = 0; x < num_gh; x++)
1338 p[x] = &ghs[x];
1339
1340 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
1341
1342 for (x = 0; x < num_gh; x++) {
1343 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1344
1345 if (p[x]->gh_flags & GL_ATIME)
1346 error = gfs2_glock_nq_atime(p[x]);
1347 else
1348 error = gfs2_glock_nq(p[x]);
1349
1350 if (error) {
1351 while (x--)
1352 gfs2_glock_dq(p[x]);
1353 break;
1354 }
1355 }
1356
1357 kfree(p);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001358 return error;
1359}
1360
David Teiglandb3b94fa2006-01-16 16:50:04 +00001361
1362static int
1363__gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1364{
1365 struct buffer_head *dibh;
1366 int error;
1367
1368 error = gfs2_meta_inode_buffer(ip, &dibh);
1369 if (!error) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001370 error = inode_setattr(&ip->i_inode, attr);
1371 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001372 gfs2_inode_attr_out(ip);
1373
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001374 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001375 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001376 brelse(dibh);
1377 }
1378 return error;
1379}
1380
1381/**
1382 * gfs2_setattr_simple -
1383 * @ip:
1384 * @attr:
1385 *
1386 * Called with a reference on the vnode.
1387 *
1388 * Returns: errno
1389 */
1390
1391int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1392{
1393 int error;
1394
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001395 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001396 return __gfs2_setattr_simple(ip, attr);
1397
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001398 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001399 if (error)
1400 return error;
1401
1402 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001403 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001404 return error;
1405}
1406