blob: 792d64f69cc25ceb642df8a27a13e6f4d5b7ae60 [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
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010041struct gfs2_inum_range_host {
42 u64 ir_start;
43 u64 ir_length;
44};
45
David Teiglandb3b94fa2006-01-16 16:50:04 +000046static int iget_test(struct inode *inode, void *opaque)
47{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040048 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010049 u64 *no_addr = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +000050
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010051 if (ip->i_no_addr == *no_addr &&
Steven Whitehouse1be38672007-03-01 10:00:53 +000052 inode->i_private != NULL)
David Teiglandb3b94fa2006-01-16 16:50:04 +000053 return 1;
54
55 return 0;
56}
57
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040058static int iget_set(struct inode *inode, void *opaque)
59{
60 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010061 u64 *no_addr = opaque;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040062
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010063 inode->i_ino = (unsigned long)*no_addr;
64 ip->i_no_addr = *no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040065 return 0;
66}
67
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010068struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000069{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010070 unsigned long hash = (unsigned long)no_addr;
71 return ilookup5(sb, hash, iget_test, &no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +000072}
73
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010074static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000075{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010076 unsigned long hash = (unsigned long)no_addr;
77 return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040078}
79
80/**
81 * gfs2_inode_lookup - Lookup an inode
82 * @sb: The super block
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010083 * @no_addr: The inode number
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040084 * @type: The type of the inode
85 *
86 * Returns: A VFS inode, or an error
87 */
88
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010089struct inode *gfs2_inode_lookup(struct super_block *sb, u64 no_addr, unsigned int type)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040090{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010091 struct inode *inode = gfs2_iget(sb, no_addr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040092 struct gfs2_inode *ip = GFS2_I(inode);
93 struct gfs2_glock *io_gl;
94 int error;
95
Steven Whitehouse26d83de2006-10-30 16:59:08 -050096 if (!inode)
97 return ERR_PTR(-ENOBUFS);
98
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040099 if (inode->i_state & I_NEW) {
100 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100101 umode_t mode;
Theodore Ts'obba9dfd2006-09-27 01:50:31 -0700102 inode->i_private = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400103
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100104 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400105 if (unlikely(error))
106 goto fail;
107 ip->i_gl->gl_object = ip;
108
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100109 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400110 if (unlikely(error))
111 goto fail_put;
112
Steven Whitehousebfded272006-11-01 16:05:38 -0500113 set_bit(GIF_INVALID, &ip->i_flags);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400114 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
115 if (unlikely(error))
116 goto fail_iopen;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100117 ip->i_iopen_gh.gh_gl->gl_object = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400118
119 gfs2_glock_put(io_gl);
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100120
121 /*
122 * We must read the inode in order to work out its type in
123 * this case. Note that this doesn't happen often as we normally
124 * know the type beforehand. This code path only occurs during
125 * unlinked inode recovery (where it is safe to do this glock,
126 * which is not true in the general case).
127 */
128 inode->i_mode = mode = DT2IF(type);
129 if (type == DT_UNKNOWN) {
130 struct gfs2_holder gh;
131 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
132 if (unlikely(error))
133 goto fail_glock;
134 /* Inode is now uptodate */
135 mode = inode->i_mode;
136 gfs2_glock_dq_uninit(&gh);
137 }
138
139 if (S_ISREG(mode)) {
140 inode->i_op = &gfs2_file_iops;
141 inode->i_fop = &gfs2_file_fops;
142 inode->i_mapping->a_ops = &gfs2_file_aops;
143 } else if (S_ISDIR(mode)) {
144 inode->i_op = &gfs2_dir_iops;
145 inode->i_fop = &gfs2_dir_fops;
146 } else if (S_ISLNK(mode)) {
147 inode->i_op = &gfs2_symlink_iops;
148 } else {
149 inode->i_op = &gfs2_dev_iops;
150 }
151
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400152 unlock_new_inode(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400154
155 return inode;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100156fail_glock:
157 gfs2_glock_dq(&ip->i_iopen_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400158fail_iopen:
159 gfs2_glock_put(io_gl);
160fail_put:
161 ip->i_gl->gl_object = NULL;
162 gfs2_glock_put(ip->i_gl);
163fail:
164 iput(inode);
165 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166}
167
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500168static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
Steven Whitehouseea744d02006-10-31 15:28:00 -0500169{
170 struct gfs2_dinode_host *di = &ip->i_di;
171 const struct gfs2_dinode *str = buf;
172
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100173 if (ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)) {
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500174 if (gfs2_consist_inode(ip))
175 gfs2_dinode_print(ip);
176 return -EIO;
177 }
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100178 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500179 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500180 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500181 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500182 case S_IFBLK:
183 case S_IFCHR:
184 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
185 be32_to_cpu(str->di_minor));
186 break;
187 };
188
Steven Whitehouse2933f922006-11-01 13:23:29 -0500189 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
190 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
Steven Whitehouse4f561102006-11-01 14:04:17 -0500191 /*
192 * We will need to review setting the nlink count here in the
193 * light of the forthcoming ro bind mount work. This is a reminder
194 * to do that.
195 */
196 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500197 di->di_size = be64_to_cpu(str->di_size);
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500198 i_size_write(&ip->i_inode, di->di_size);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500199 di->di_blocks = be64_to_cpu(str->di_blocks);
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500200 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500201 ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100202 ip->i_inode.i_atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500203 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100204 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500205 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100206 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500207
208 di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
209 di->di_goal_data = be64_to_cpu(str->di_goal_data);
210 di->di_generation = be64_to_cpu(str->di_generation);
211
212 di->di_flags = be32_to_cpu(str->di_flags);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500213 gfs2_set_inode_flags(&ip->i_inode);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500214 di->di_height = be16_to_cpu(str->di_height);
215
216 di->di_depth = be16_to_cpu(str->di_depth);
217 di->di_entries = be32_to_cpu(str->di_entries);
218
219 di->di_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500220 return 0;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500221}
222
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223/**
224 * gfs2_inode_refresh - Refresh the incore copy of the dinode
225 * @ip: The GFS2 inode
226 *
227 * Returns: errno
228 */
229
230int gfs2_inode_refresh(struct gfs2_inode *ip)
231{
232 struct buffer_head *dibh;
233 int error;
234
235 error = gfs2_meta_inode_buffer(ip, &dibh);
236 if (error)
237 return error;
238
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400239 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 brelse(dibh);
241 return -EIO;
242 }
243
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500244 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 brelse(dibh);
Steven Whitehousebfded272006-11-01 16:05:38 -0500246 clear_bit(GIF_INVALID, &ip->i_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500248 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249}
250
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400251int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400253 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254 struct gfs2_alloc *al;
255 struct gfs2_rgrpd *rgd;
256 int error;
257
258 if (ip->i_di.di_blocks != 1) {
259 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500260 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261 return -EIO;
262 }
263
264 al = gfs2_alloc_get(ip);
265
266 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
267 if (error)
268 goto out;
269
270 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
271 if (error)
272 goto out_qs;
273
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100274 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000275 if (!rgd) {
276 gfs2_consist_inode(ip);
277 error = -EIO;
278 goto out_rindex_relse;
279 }
280
281 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
282 &al->al_rgd_gh);
283 if (error)
284 goto out_rindex_relse;
285
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400286 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287 if (error)
288 goto out_rg_gunlock;
289
290 gfs2_trans_add_gl(ip->i_gl);
291
292 gfs2_free_di(rgd, ip);
293
David Teiglandb3b94fa2006-01-16 16:50:04 +0000294 gfs2_trans_end(sdp);
295 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
296
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400297out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400299out_rindex_relse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400301out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000302 gfs2_quota_unhold(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400303out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400304 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000305 return error;
306}
307
308/**
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500309 * gfs2_change_nlink - Change nlink count on inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310 * @ip: The GFS2 inode
311 * @diff: The change in the nlink count required
312 *
313 * Returns: errno
314 */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500315int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316{
317 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400318 u32 nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319 int error;
320
Steven Whitehouse4f561102006-11-01 14:04:17 -0500321 BUG_ON(diff != 1 && diff != -1);
322 nlink = ip->i_inode.i_nlink + diff;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323
324 /* If we are reducing the nlink count, but the new value ends up being
325 bigger than the old one, we must have underflowed. */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500326 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500328 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000329 return -EIO;
330 }
331
332 error = gfs2_meta_inode_buffer(ip, &dibh);
333 if (error)
334 return error;
335
Steven Whitehouse4f561102006-11-01 14:04:17 -0500336 if (diff > 0)
337 inc_nlink(&ip->i_inode);
338 else
339 drop_nlink(&ip->i_inode);
340
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100341 ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000342
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000343 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500344 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400346 mark_inode_dirty(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500348 if (ip->i_inode.i_nlink == 0)
Russell Cattelanddee7602007-01-29 17:13:44 -0600349 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500350
S. Wendy Cheng55098262007-01-18 15:56:34 -0500351 return error;
352}
353
Steven Whitehousec7526662006-03-20 12:30:04 -0500354struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
355{
356 struct qstr qstr;
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600357 struct inode *inode;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500358 gfs2_str2qstr(&qstr, name);
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600359 inode = gfs2_lookupi(dip, &qstr, 1, NULL);
360 /* gfs2_lookupi has inconsistent callers: vfs
361 * related routines expect NULL for no entry found,
362 * gfs2_lookup_simple callers expect ENOENT
363 * and do not check for NULL.
364 */
365 if (inode == NULL)
366 return ERR_PTR(-ENOENT);
367 else
368 return inode;
Steven Whitehousec7526662006-03-20 12:30:04 -0500369}
370
371
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372/**
373 * gfs2_lookupi - Look up a filename in a directory and return its inode
374 * @d_gh: An initialized holder for the directory glock
375 * @name: The name of the inode to look for
376 * @is_root: If 1, ignore the caller's permissions
377 * @i_gh: An uninitialized holder for the new inode glock
378 *
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000379 * This can be called via the VFS filldir function when NFS is doing
380 * a readdirplus and the inode which its intending to stat isn't
381 * already in cache. In this case we must not take the directory glock
382 * again, since the readdir call will have already taken that lock.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000383 *
384 * Returns: errno
385 */
386
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400387struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
388 int is_root, struct nameidata *nd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500390 struct super_block *sb = dir->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400391 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392 struct gfs2_holder d_gh;
akpm@linux-foundation.org037bcbb2007-06-08 16:42:14 -0700393 int error = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500394 struct inode *inode = NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000395 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000396
397 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500398 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000399
Steven Whitehousec7526662006-03-20 12:30:04 -0500400 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
401 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
402 dir == sb->s_root->d_inode)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400403 igrab(dir);
404 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405 }
406
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000407 if (gfs2_glock_is_locked_by_me(dip->i_gl) == 0) {
408 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
409 if (error)
410 return ERR_PTR(error);
411 unlock = 1;
412 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000413
414 if (!is_root) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400415 error = permission(dir, MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 if (error)
417 goto out;
418 }
419
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100420 inode = gfs2_dir_search(dir, name);
421 if (IS_ERR(inode))
422 error = PTR_ERR(inode);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000423out:
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000424 if (unlock)
425 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500426 if (error == -ENOENT)
427 return NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000428 return inode ? inode : ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000429}
430
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100431static void gfs2_inum_range_in(struct gfs2_inum_range_host *ir, const void *buf)
432{
433 const struct gfs2_inum_range *str = buf;
434
435 ir->ir_start = be64_to_cpu(str->ir_start);
436 ir->ir_length = be64_to_cpu(str->ir_length);
437}
438
439static void gfs2_inum_range_out(const struct gfs2_inum_range_host *ir, void *buf)
440{
441 struct gfs2_inum_range *str = buf;
442
443 str->ir_start = cpu_to_be64(ir->ir_start);
444 str->ir_length = cpu_to_be64(ir->ir_length);
445}
446
Steven Whitehousecd915492006-09-04 12:49:07 -0400447static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000448{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400449 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000450 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400451 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000452 int error;
453
454 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
455 if (error)
456 return error;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000457 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458
459 error = gfs2_meta_inode_buffer(ip, &bh);
460 if (error) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000461 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000462 gfs2_trans_end(sdp);
463 return error;
464 }
465
466 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
467
468 if (ir.ir_length) {
469 *formal_ino = ir.ir_start++;
470 ir.ir_length--;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000471 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000472 gfs2_inum_range_out(&ir,
473 bh->b_data + sizeof(struct gfs2_dinode));
474 brelse(bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000475 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000476 gfs2_trans_end(sdp);
477 return 0;
478 }
479
480 brelse(bh);
481
Steven Whitehousef55ab262006-02-21 12:51:39 +0000482 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000483 gfs2_trans_end(sdp);
484
485 return 1;
486}
487
Steven Whitehousecd915492006-09-04 12:49:07 -0400488static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400490 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
491 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 struct gfs2_holder gh;
493 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400494 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000495 int error;
496
497 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
498 if (error)
499 return error;
500
501 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
502 if (error)
503 goto out;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000504 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505
506 error = gfs2_meta_inode_buffer(ip, &bh);
507 if (error)
508 goto out_end_trans;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400509
David Teiglandb3b94fa2006-01-16 16:50:04 +0000510 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
511
512 if (!ir.ir_length) {
513 struct buffer_head *m_bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400514 u64 x, y;
Al Virob44b84d2006-10-14 10:46:30 -0400515 __be64 z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516
517 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
518 if (error)
519 goto out_brelse;
520
Al Virob44b84d2006-10-14 10:46:30 -0400521 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
522 x = y = be64_to_cpu(z);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000523 ir.ir_start = x;
524 ir.ir_length = GFS2_INUM_QUANTUM;
525 x += GFS2_INUM_QUANTUM;
526 if (x < y)
527 gfs2_consist_inode(m_ip);
Al Virob44b84d2006-10-14 10:46:30 -0400528 z = cpu_to_be64(x);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000529 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
Al Virob44b84d2006-10-14 10:46:30 -0400530 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531
532 brelse(m_bh);
533 }
534
535 *formal_ino = ir.ir_start++;
536 ir.ir_length--;
537
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000538 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
540
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400541out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000542 brelse(bh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400543out_end_trans:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000544 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545 gfs2_trans_end(sdp);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400546out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548 return error;
549}
550
Steven Whitehousecd915492006-09-04 12:49:07 -0400551static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552{
553 int error;
554
555 error = pick_formal_ino_1(sdp, inum);
556 if (error <= 0)
557 return error;
558
559 error = pick_formal_ino_2(sdp, inum);
560
561 return error;
562}
563
564/**
565 * create_ok - OK to create a new on-disk inode here?
566 * @dip: Directory in which dinode is to be created
567 * @name: Name of new dinode
568 * @mode:
569 *
570 * Returns: errno
571 */
572
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400573static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574 unsigned int mode)
575{
576 int error;
577
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400578 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579 if (error)
580 return error;
581
582 /* Don't create entries in an unlinked directory */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500583 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584 return -EPERM;
585
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100586 error = gfs2_dir_check(&dip->i_inode, name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 switch (error) {
588 case -ENOENT:
589 error = 0;
590 break;
591 case 0:
592 return -EEXIST;
593 default:
594 return error;
595 }
596
Steven Whitehousecd915492006-09-04 12:49:07 -0400597 if (dip->i_di.di_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598 return -EFBIG;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500599 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600 return -EMLINK;
601
602 return 0;
603}
604
605static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
606 unsigned int *uid, unsigned int *gid)
607{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400608 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500609 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 if (S_ISDIR(*mode))
611 *mode |= S_ISUID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500612 else if (dip->i_inode.i_uid != current->fsuid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613 *mode &= ~07111;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500614 *uid = dip->i_inode.i_uid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615 } else
616 *uid = current->fsuid;
617
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500618 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619 if (S_ISDIR(*mode))
620 *mode |= S_ISGID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500621 *gid = dip->i_inode.i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622 } else
623 *gid = current->fsgid;
624}
625
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100626static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000627{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400628 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629 int error;
630
631 gfs2_alloc_get(dip);
632
633 dip->i_alloc.al_requested = RES_DINODE;
634 error = gfs2_inplace_reserve(dip);
635 if (error)
636 goto out;
637
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400638 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639 if (error)
640 goto out_ipreserv;
641
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100642 *no_addr = gfs2_alloc_di(dip, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000643
644 gfs2_trans_end(sdp);
645
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400646out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000647 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400648out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000649 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 return error;
651}
652
653/**
654 * init_dinode - Fill in a new dinode structure
655 * @dip: the directory this inode is being created in
656 * @gl: The glock covering the new inode
657 * @inum: the inode number
658 * @mode: the file permissions
659 * @uid:
660 * @gid:
661 *
662 */
663
664static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400665 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400666 unsigned int uid, unsigned int gid,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500667 const u64 *generation, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000668{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400669 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000670 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671 struct buffer_head *dibh;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100672 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673
674 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000675 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000676 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
677 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000678 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679
Steven Whitehouse2442a092006-01-30 11:49:32 +0000680 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
681 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000682 di->di_mode = cpu_to_be32(mode);
683 di->di_uid = cpu_to_be32(uid);
684 di->di_gid = cpu_to_be32(gid);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500685 di->di_nlink = 0;
686 di->di_size = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000687 di->di_blocks = cpu_to_be64(1);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100688 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500689 di->di_major = cpu_to_be32(MAJOR(dev));
690 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000691 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400692 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500693 di->di_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694
695 if (S_ISREG(mode)) {
696 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
697 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000698 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000699 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
700 gfs2_tune_get(sdp, gt_new_files_directio))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000701 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000702 } else if (S_ISDIR(mode)) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500703 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
704 GFS2_DIF_INHERIT_DIRECTIO);
705 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
706 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 }
708
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000709 di->__pad1 = 0;
Steven Whitehousea9583c72006-11-01 20:09:14 -0500710 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500711 di->di_height = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000712 di->__pad2 = 0;
713 di->__pad3 = 0;
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500714 di->di_depth = 0;
715 di->di_entries = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000716 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500717 di->di_eattr = 0;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100718 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
719 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
720 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000721 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
722
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723 brelse(dibh);
724}
725
726static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400727 unsigned int mode, const struct gfs2_inum_host *inum,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500728 const u64 *generation, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400730 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000731 unsigned int uid, gid;
732 int error;
733
734 munge_mode_uid_gid(dip, &mode, &uid, &gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 gfs2_alloc_get(dip);
736
737 error = gfs2_quota_lock(dip, uid, gid);
738 if (error)
739 goto out;
740
741 error = gfs2_quota_check(dip, uid, gid);
742 if (error)
743 goto out_quota;
744
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400745 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746 if (error)
747 goto out_quota;
748
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500749 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000750 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751 gfs2_trans_end(sdp);
752
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400753out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400755out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 return error;
758}
759
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400760static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
761 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400763 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 struct gfs2_alloc *al;
765 int alloc_required;
766 struct buffer_head *dibh;
767 int error;
768
769 al = gfs2_alloc_get(dip);
770
771 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
772 if (error)
773 goto fail;
774
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400775 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500776 if (alloc_required < 0)
777 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778 if (alloc_required) {
Steven Whitehouse2933f922006-11-01 13:23:29 -0500779 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780 if (error)
781 goto fail_quota_locks;
782
783 al->al_requested = sdp->sd_max_dirres;
784
785 error = gfs2_inplace_reserve(dip);
786 if (error)
787 goto fail_quota_locks;
788
Steven Whitehouse320dd102006-05-18 16:25:27 -0400789 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100790 al->al_rgd->rd_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400791 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792 RES_STATFS + RES_QUOTA, 0);
793 if (error)
794 goto fail_ipreserv;
795 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400796 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 if (error)
798 goto fail_quota_locks;
799 }
800
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100801 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802 if (error)
803 goto fail_end_trans;
804
805 error = gfs2_meta_inode_buffer(ip, &dibh);
806 if (error)
807 goto fail_end_trans;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500808 ip->i_inode.i_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000809 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500810 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 return 0;
813
Steven Whitehouse320dd102006-05-18 16:25:27 -0400814fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 gfs2_trans_end(sdp);
816
Steven Whitehouse320dd102006-05-18 16:25:27 -0400817fail_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000818 if (dip->i_alloc.al_rgd)
819 gfs2_inplace_release(dip);
820
Steven Whitehouse320dd102006-05-18 16:25:27 -0400821fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822 gfs2_quota_unlock(dip);
823
Steven Whitehouse320dd102006-05-18 16:25:27 -0400824fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 return error;
827}
828
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400829static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
830{
831 int err;
832 size_t len;
833 void *value;
834 char *name;
835 struct gfs2_ea_request er;
836
837 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
838 &name, &value, &len);
839
840 if (err) {
841 if (err == -EOPNOTSUPP)
842 return 0;
843 return err;
844 }
845
846 memset(&er, 0, sizeof(struct gfs2_ea_request));
847
848 er.er_type = GFS2_EATYPE_SECURITY;
849 er.er_name = name;
850 er.er_data = value;
851 er.er_name_len = strlen(name);
852 er.er_data_len = len;
853
854 err = gfs2_ea_set_i(ip, &er);
855
856 kfree(value);
857 kfree(name);
858
859 return err;
860}
861
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862/**
863 * gfs2_createi - Create a new inode
864 * @ghs: An array of two holders
865 * @name: The name of the new file
866 * @mode: the permissions on the new inode
867 *
868 * @ghs[0] is an initialized holder for the directory
869 * @ghs[1] is the holder for the inode lock
870 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000871 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 * file are held. A transaction has been started and an inplace reservation
873 * is held, as well.
874 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000875 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000876 */
877
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400878struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500879 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000880{
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100881 struct inode *inode = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500882 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400883 struct inode *dir = &dip->i_inode;
884 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100885 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000886 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400887 u64 generation;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888
889 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000890 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891
David Teiglandb3b94fa2006-01-16 16:50:04 +0000892 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
893 error = gfs2_glock_nq(ghs);
894 if (error)
895 goto fail;
896
897 error = create_ok(dip, name, mode);
898 if (error)
899 goto fail_gunlock;
900
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400901 error = pick_formal_ino(sdp, &inum.no_formal_ino);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000902 if (error)
903 goto fail_gunlock;
904
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100905 error = alloc_dinode(dip, &inum.no_addr, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906 if (error)
907 goto fail_gunlock;
908
Steven Whitehouse28626e22006-11-22 11:13:21 -0500909 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
910 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
911 if (error)
912 goto fail_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500914 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000915 if (error)
916 goto fail_gunlock2;
917
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100918 inode = gfs2_inode_lookup(dir->i_sb, inum.no_addr, IF2DT(mode));
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400919 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 goto fail_gunlock2;
921
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400922 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100924 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400926 error = gfs2_acl_create(dip, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100928 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000929
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400930 error = gfs2_security_init(dip, GFS2_I(inode));
931 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100932 goto fail_gunlock2;
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400933
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400934 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100936 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937
Steven Whitehouse7359a192006-02-13 12:27:43 +0000938 if (!inode)
939 return ERR_PTR(-ENOMEM);
940 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941
Steven Whitehouse320dd102006-05-18 16:25:27 -0400942fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100944 if (inode)
945 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400946fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000947 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400948fail:
Steven Whitehouse7359a192006-02-13 12:27:43 +0000949 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950}
951
952/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953 * gfs2_rmdiri - Remove a directory
954 * @dip: The parent directory of the directory to be removed
955 * @name: The name of the directory to be removed
956 * @ip: The GFS2 inode of the directory to be removed
957 *
958 * Assumes Glocks on dip and ip are held
959 *
960 * Returns: errno
961 */
962
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400963int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
964 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 struct qstr dotname;
967 int error;
968
969 if (ip->i_di.di_entries != 2) {
970 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500971 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 return -EIO;
973 }
974
975 error = gfs2_dir_del(dip, name);
976 if (error)
977 return error;
978
979 error = gfs2_change_nlink(dip, -1);
980 if (error)
981 return error;
982
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500983 gfs2_str2qstr(&dotname, ".");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000984 error = gfs2_dir_del(ip, &dotname);
985 if (error)
986 return error;
987
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400988 gfs2_str2qstr(&dotname, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989 error = gfs2_dir_del(ip, &dotname);
990 if (error)
991 return error;
992
Steven Whitehouse4f561102006-11-01 14:04:17 -0500993 /* It looks odd, but it really should be done twice */
994 error = gfs2_change_nlink(ip, -1);
995 if (error)
996 return error;
997
998 error = gfs2_change_nlink(ip, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 if (error)
1000 return error;
1001
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002 return error;
1003}
1004
1005/*
1006 * gfs2_unlink_ok - check to see that a inode is still in a directory
1007 * @dip: the directory
1008 * @name: the name of the file
1009 * @ip: the inode
1010 *
1011 * Assumes that the lock on (at least) @dip is held.
1012 *
1013 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1014 */
1015
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001016int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001017 const struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001018{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001019 int error;
1020
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001021 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022 return -EPERM;
1023
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001024 if ((dip->i_inode.i_mode & S_ISVTX) &&
Steven Whitehouse2933f922006-11-01 13:23:29 -05001025 dip->i_inode.i_uid != current->fsuid &&
1026 ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 return -EPERM;
1028
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001029 if (IS_APPEND(&dip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001030 return -EPERM;
1031
Steven Whitehousefaf450e2006-06-22 10:59:10 -04001032 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033 if (error)
1034 return error;
1035
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001036 error = gfs2_dir_check(&dip->i_inode, name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001037 if (error)
1038 return error;
1039
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 return 0;
1041}
1042
1043/*
1044 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1045 * @this: move this
1046 * @to: to here
1047 *
1048 * Follow @to back to the root and make sure we don't encounter @this
1049 * Assumes we already hold the rename lock.
1050 *
1051 * Returns: errno
1052 */
1053
1054int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1055{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001056 struct inode *dir = &to->i_inode;
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001057 struct super_block *sb = dir->i_sb;
Steven Whitehouse7359a192006-02-13 12:27:43 +00001058 struct inode *tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059 struct qstr dotdot;
1060 int error = 0;
1061
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001062 gfs2_str2qstr(&dotdot, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063
Steven Whitehouse7359a192006-02-13 12:27:43 +00001064 igrab(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001065
1066 for (;;) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001067 if (dir == &this->i_inode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068 error = -EINVAL;
1069 break;
1070 }
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001071 if (dir == sb->s_root->d_inode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072 error = 0;
1073 break;
1074 }
1075
Steven Whitehousec7526662006-03-20 12:30:04 -05001076 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1077 if (IS_ERR(tmp)) {
1078 error = PTR_ERR(tmp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079 break;
Steven Whitehousec7526662006-03-20 12:30:04 -05001080 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001081
Steven Whitehouse7359a192006-02-13 12:27:43 +00001082 iput(dir);
1083 dir = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084 }
1085
Steven Whitehouse7359a192006-02-13 12:27:43 +00001086 iput(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001087
1088 return error;
1089}
1090
1091/**
1092 * gfs2_readlinki - return the contents of a symlink
1093 * @ip: the symlink's inode
1094 * @buf: a pointer to the buffer to be filled
1095 * @len: a pointer to the length of @buf
1096 *
1097 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1098 * to be freed by the caller.
1099 *
1100 * Returns: errno
1101 */
1102
1103int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1104{
1105 struct gfs2_holder i_gh;
1106 struct buffer_head *dibh;
1107 unsigned int x;
1108 int error;
1109
1110 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1111 error = gfs2_glock_nq_atime(&i_gh);
1112 if (error) {
1113 gfs2_holder_uninit(&i_gh);
1114 return error;
1115 }
1116
1117 if (!ip->i_di.di_size) {
1118 gfs2_consist_inode(ip);
1119 error = -EIO;
1120 goto out;
1121 }
1122
1123 error = gfs2_meta_inode_buffer(ip, &dibh);
1124 if (error)
1125 goto out;
1126
1127 x = ip->i_di.di_size + 1;
1128 if (x > *len) {
1129 *buf = kmalloc(x, GFP_KERNEL);
1130 if (!*buf) {
1131 error = -ENOMEM;
1132 goto out_brelse;
1133 }
1134 }
1135
1136 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1137 *len = x;
1138
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001139out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001140 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001141out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001143 return error;
1144}
1145
1146/**
1147 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1148 * conditionally update the inode's atime
1149 * @gh: the holder to acquire
1150 *
1151 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1152 * Update if the difference between the current time and the inode's current
1153 * atime is greater than an interval specified at mount.
1154 *
1155 * Returns: errno
1156 */
1157
1158int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1159{
1160 struct gfs2_glock *gl = gh->gh_gl;
1161 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001162 struct gfs2_inode *ip = gl->gl_object;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001163 s64 quantum = gfs2_tune_get(sdp, gt_atime_quantum);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164 unsigned int state;
1165 int flags;
1166 int error;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001167 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001168
1169 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1170 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1171 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1172 return -EINVAL;
1173
1174 state = gh->gh_state;
1175 flags = gh->gh_flags;
1176
1177 error = gfs2_glock_nq(gh);
1178 if (error)
1179 return error;
1180
1181 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1182 (sdp->sd_vfs->s_flags & MS_RDONLY))
1183 return 0;
1184
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001185 if (tv.tv_sec - ip->i_inode.i_atime.tv_sec >= quantum) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186 gfs2_glock_dq(gh);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001187 gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1188 gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001189 error = gfs2_glock_nq(gh);
1190 if (error)
1191 return error;
1192
1193 /* Verify that atime hasn't been updated while we were
1194 trying to get exclusive lock. */
1195
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001196 tv = CURRENT_TIME;
1197 if (tv.tv_sec - ip->i_inode.i_atime.tv_sec >= quantum) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001198 struct buffer_head *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -04001199 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001200
1201 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1202 if (error == -EROFS)
1203 return 0;
1204 if (error)
1205 goto fail;
1206
1207 error = gfs2_meta_inode_buffer(ip, &dibh);
1208 if (error)
1209 goto fail_end_trans;
1210
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001211 ip->i_inode.i_atime = tv;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001213 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse48516ce2006-10-02 12:39:19 -04001214 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001215 di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001216 di->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001217 brelse(dibh);
1218
1219 gfs2_trans_end(sdp);
1220 }
1221
1222 /* If someone else has asked for the glock,
1223 unlock and let them have it. Then reacquire
1224 in the original state. */
1225 if (gfs2_glock_is_blocking(gl)) {
1226 gfs2_glock_dq(gh);
1227 gfs2_holder_reinit(state, flags, gh);
1228 return gfs2_glock_nq(gh);
1229 }
1230 }
1231
1232 return 0;
1233
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001234fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001235 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001236fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237 gfs2_glock_dq(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001238 return error;
1239}
1240
David Teiglandb3b94fa2006-01-16 16:50:04 +00001241static int
1242__gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1243{
1244 struct buffer_head *dibh;
1245 int error;
1246
1247 error = gfs2_meta_inode_buffer(ip, &dibh);
1248 if (!error) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001249 error = inode_setattr(&ip->i_inode, attr);
1250 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001251 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001252 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001253 brelse(dibh);
1254 }
1255 return error;
1256}
1257
1258/**
1259 * gfs2_setattr_simple -
1260 * @ip:
1261 * @attr:
1262 *
1263 * Called with a reference on the vnode.
1264 *
1265 * Returns: errno
1266 */
1267
1268int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1269{
1270 int error;
1271
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001272 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001273 return __gfs2_setattr_simple(ip, attr);
1274
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001275 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001276 if (error)
1277 return error;
1278
1279 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001280 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281 return error;
1282}
1283
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001284void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
1285{
1286 const struct gfs2_dinode_host *di = &ip->i_di;
1287 struct gfs2_dinode *str = buf;
1288
1289 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
1290 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
1291 str->di_header.__pad0 = 0;
1292 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
1293 str->di_header.__pad1 = 0;
1294 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
1295 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
1296 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
1297 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
1298 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
1299 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
1300 str->di_size = cpu_to_be64(di->di_size);
1301 str->di_blocks = cpu_to_be64(di->di_blocks);
1302 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
1303 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
1304 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
1305
1306 str->di_goal_meta = cpu_to_be64(di->di_goal_meta);
1307 str->di_goal_data = cpu_to_be64(di->di_goal_data);
1308 str->di_generation = cpu_to_be64(di->di_generation);
1309
1310 str->di_flags = cpu_to_be32(di->di_flags);
1311 str->di_height = cpu_to_be16(di->di_height);
1312 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
1313 !(ip->i_di.di_flags & GFS2_DIF_EXHASH) ?
1314 GFS2_FORMAT_DE : 0);
1315 str->di_depth = cpu_to_be16(di->di_depth);
1316 str->di_entries = cpu_to_be32(di->di_entries);
1317
1318 str->di_eattr = cpu_to_be64(di->di_eattr);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001319 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
1320 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
1321 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001322}
1323
1324void gfs2_dinode_print(const struct gfs2_inode *ip)
1325{
1326 const struct gfs2_dinode_host *di = &ip->i_di;
1327
1328 printk(KERN_INFO " no_formal_ino = %llu\n",
1329 (unsigned long long)ip->i_no_formal_ino);
1330 printk(KERN_INFO " no_addr = %llu\n",
1331 (unsigned long long)ip->i_no_addr);
1332 printk(KERN_INFO " di_size = %llu\n", (unsigned long long)di->di_size);
1333 printk(KERN_INFO " di_blocks = %llu\n",
1334 (unsigned long long)di->di_blocks);
1335 printk(KERN_INFO " di_goal_meta = %llu\n",
1336 (unsigned long long)di->di_goal_meta);
1337 printk(KERN_INFO " di_goal_data = %llu\n",
1338 (unsigned long long)di->di_goal_data);
1339 printk(KERN_INFO " di_flags = 0x%.8X\n", di->di_flags);
1340 printk(KERN_INFO " di_height = %u\n", di->di_height);
1341 printk(KERN_INFO " di_depth = %u\n", di->di_depth);
1342 printk(KERN_INFO " di_entries = %u\n", di->di_entries);
1343 printk(KERN_INFO " di_eattr = %llu\n",
1344 (unsigned long long)di->di_eattr);
1345}
1346