blob: 013f00b90cc27ce66fa2f0f19912358ede885b9f [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/**
Wendy Cheng35dcc522007-06-27 17:07:53 -040081 * GFS2 lookup code fills in vfs inode contents based on info obtained
82 * from directory entry inside gfs2_inode_lookup(). This has caused issues
83 * with NFS code path since its get_dentry routine doesn't have the relevant
84 * directory entry when gfs2_inode_lookup() is invoked. Part of the code
85 * segment inside gfs2_inode_lookup code needs to get moved around.
86 *
87 * Clean up I_LOCK and I_NEW as well.
88 **/
89
90void gfs2_set_iop(struct inode *inode)
91{
92 umode_t mode = inode->i_mode;
93
94 if (S_ISREG(mode)) {
95 inode->i_op = &gfs2_file_iops;
96 inode->i_fop = &gfs2_file_fops;
97 inode->i_mapping->a_ops = &gfs2_file_aops;
98 } else if (S_ISDIR(mode)) {
99 inode->i_op = &gfs2_dir_iops;
100 inode->i_fop = &gfs2_dir_fops;
101 } else if (S_ISLNK(mode)) {
102 inode->i_op = &gfs2_symlink_iops;
103 } else {
104 inode->i_op = &gfs2_dev_iops;
105 }
106
107 unlock_new_inode(inode);
108}
109
110/**
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400111 * gfs2_inode_lookup - Lookup an inode
112 * @sb: The super block
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100113 * @no_addr: The inode number
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400114 * @type: The type of the inode
115 *
116 * Returns: A VFS inode, or an error
117 */
118
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400119struct inode *gfs2_inode_lookup(struct super_block *sb,
120 unsigned int type,
121 u64 no_addr,
122 u64 no_formal_ino)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400123{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100124 struct inode *inode = gfs2_iget(sb, no_addr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400125 struct gfs2_inode *ip = GFS2_I(inode);
126 struct gfs2_glock *io_gl;
127 int error;
128
Steven Whitehouse26d83de2006-10-30 16:59:08 -0500129 if (!inode)
130 return ERR_PTR(-ENOBUFS);
131
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400132 if (inode->i_state & I_NEW) {
133 struct gfs2_sbd *sdp = GFS2_SB(inode);
Theodore Ts'obba9dfd2006-09-27 01:50:31 -0700134 inode->i_private = ip;
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400135 ip->i_no_formal_ino = no_formal_ino;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400136
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100137 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400138 if (unlikely(error))
139 goto fail;
140 ip->i_gl->gl_object = ip;
141
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100142 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400143 if (unlikely(error))
144 goto fail_put;
145
Steven Whitehousebfded272006-11-01 16:05:38 -0500146 set_bit(GIF_INVALID, &ip->i_flags);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400147 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
148 if (unlikely(error))
149 goto fail_iopen;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100150 ip->i_iopen_gh.gh_gl->gl_object = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400151
152 gfs2_glock_put(io_gl);
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100153
Wendy Cheng35dcc522007-06-27 17:07:53 -0400154 if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
155 goto gfs2_nfsbypass;
156
157 inode->i_mode = DT2IF(type);
158
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100159 /*
160 * We must read the inode in order to work out its type in
161 * this case. Note that this doesn't happen often as we normally
162 * know the type beforehand. This code path only occurs during
163 * unlinked inode recovery (where it is safe to do this glock,
164 * which is not true in the general case).
165 */
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100166 if (type == DT_UNKNOWN) {
167 struct gfs2_holder gh;
168 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
169 if (unlikely(error))
170 goto fail_glock;
171 /* Inode is now uptodate */
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100172 gfs2_glock_dq_uninit(&gh);
173 }
174
Wendy Cheng35dcc522007-06-27 17:07:53 -0400175 gfs2_set_iop(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400177
Wendy Cheng35dcc522007-06-27 17:07:53 -0400178gfs2_nfsbypass:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400179 return inode;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100180fail_glock:
181 gfs2_glock_dq(&ip->i_iopen_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400182fail_iopen:
183 gfs2_glock_put(io_gl);
184fail_put:
185 ip->i_gl->gl_object = NULL;
186 gfs2_glock_put(ip->i_gl);
187fail:
188 iput(inode);
189 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190}
191
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500192static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
Steven Whitehouseea744d02006-10-31 15:28:00 -0500193{
194 struct gfs2_dinode_host *di = &ip->i_di;
195 const struct gfs2_dinode *str = buf;
196
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100197 if (ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)) {
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500198 if (gfs2_consist_inode(ip))
199 gfs2_dinode_print(ip);
200 return -EIO;
201 }
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100202 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500203 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500204 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500205 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500206 case S_IFBLK:
207 case S_IFCHR:
208 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
209 be32_to_cpu(str->di_minor));
210 break;
211 };
212
Steven Whitehouse2933f922006-11-01 13:23:29 -0500213 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
214 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
Steven Whitehouse4f561102006-11-01 14:04:17 -0500215 /*
216 * We will need to review setting the nlink count here in the
217 * light of the forthcoming ro bind mount work. This is a reminder
218 * to do that.
219 */
220 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500221 di->di_size = be64_to_cpu(str->di_size);
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500222 i_size_write(&ip->i_inode, di->di_size);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500223 di->di_blocks = be64_to_cpu(str->di_blocks);
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500224 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500225 ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100226 ip->i_inode.i_atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500227 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100228 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500229 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100230 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500231
232 di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
233 di->di_goal_data = be64_to_cpu(str->di_goal_data);
234 di->di_generation = be64_to_cpu(str->di_generation);
235
236 di->di_flags = be32_to_cpu(str->di_flags);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500237 gfs2_set_inode_flags(&ip->i_inode);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500238 di->di_height = be16_to_cpu(str->di_height);
239
240 di->di_depth = be16_to_cpu(str->di_depth);
241 di->di_entries = be32_to_cpu(str->di_entries);
242
243 di->di_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500244 return 0;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500245}
246
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400247static void gfs2_inode_bh(struct gfs2_inode *ip, struct buffer_head *bh)
248{
249 ip->i_cache[0] = bh;
250}
251
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252/**
253 * gfs2_inode_refresh - Refresh the incore copy of the dinode
254 * @ip: The GFS2 inode
255 *
256 * Returns: errno
257 */
258
259int gfs2_inode_refresh(struct gfs2_inode *ip)
260{
261 struct buffer_head *dibh;
262 int error;
263
264 error = gfs2_meta_inode_buffer(ip, &dibh);
265 if (error)
266 return error;
267
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400268 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269 brelse(dibh);
270 return -EIO;
271 }
272
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500273 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274 brelse(dibh);
Steven Whitehousebfded272006-11-01 16:05:38 -0500275 clear_bit(GIF_INVALID, &ip->i_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000276
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500277 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278}
279
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400280int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400282 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283 struct gfs2_alloc *al;
284 struct gfs2_rgrpd *rgd;
285 int error;
286
287 if (ip->i_di.di_blocks != 1) {
288 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500289 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290 return -EIO;
291 }
292
293 al = gfs2_alloc_get(ip);
294
295 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
296 if (error)
297 goto out;
298
299 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
300 if (error)
301 goto out_qs;
302
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100303 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000304 if (!rgd) {
305 gfs2_consist_inode(ip);
306 error = -EIO;
307 goto out_rindex_relse;
308 }
309
310 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
311 &al->al_rgd_gh);
312 if (error)
313 goto out_rindex_relse;
314
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400315 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316 if (error)
317 goto out_rg_gunlock;
318
319 gfs2_trans_add_gl(ip->i_gl);
320
321 gfs2_free_di(rgd, ip);
322
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323 gfs2_trans_end(sdp);
324 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
325
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400326out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400328out_rindex_relse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000329 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400330out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000331 gfs2_quota_unhold(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400332out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400333 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334 return error;
335}
336
337/**
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500338 * gfs2_change_nlink - Change nlink count on inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339 * @ip: The GFS2 inode
340 * @diff: The change in the nlink count required
341 *
342 * Returns: errno
343 */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500344int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345{
346 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400347 u32 nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348 int error;
349
Steven Whitehouse4f561102006-11-01 14:04:17 -0500350 BUG_ON(diff != 1 && diff != -1);
351 nlink = ip->i_inode.i_nlink + diff;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352
353 /* If we are reducing the nlink count, but the new value ends up being
354 bigger than the old one, we must have underflowed. */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500355 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500357 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358 return -EIO;
359 }
360
361 error = gfs2_meta_inode_buffer(ip, &dibh);
362 if (error)
363 return error;
364
Steven Whitehouse4f561102006-11-01 14:04:17 -0500365 if (diff > 0)
366 inc_nlink(&ip->i_inode);
367 else
368 drop_nlink(&ip->i_inode);
369
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100370 ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000372 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500373 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400375 mark_inode_dirty(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000376
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500377 if (ip->i_inode.i_nlink == 0)
Russell Cattelanddee7602007-01-29 17:13:44 -0600378 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500379
S. Wendy Cheng55098262007-01-18 15:56:34 -0500380 return error;
381}
382
Steven Whitehousec7526662006-03-20 12:30:04 -0500383struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
384{
385 struct qstr qstr;
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600386 struct inode *inode;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500387 gfs2_str2qstr(&qstr, name);
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600388 inode = gfs2_lookupi(dip, &qstr, 1, NULL);
389 /* gfs2_lookupi has inconsistent callers: vfs
390 * related routines expect NULL for no entry found,
391 * gfs2_lookup_simple callers expect ENOENT
392 * and do not check for NULL.
393 */
394 if (inode == NULL)
395 return ERR_PTR(-ENOENT);
396 else
397 return inode;
Steven Whitehousec7526662006-03-20 12:30:04 -0500398}
399
400
David Teiglandb3b94fa2006-01-16 16:50:04 +0000401/**
402 * gfs2_lookupi - Look up a filename in a directory and return its inode
403 * @d_gh: An initialized holder for the directory glock
404 * @name: The name of the inode to look for
405 * @is_root: If 1, ignore the caller's permissions
406 * @i_gh: An uninitialized holder for the new inode glock
407 *
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000408 * This can be called via the VFS filldir function when NFS is doing
409 * a readdirplus and the inode which its intending to stat isn't
410 * already in cache. In this case we must not take the directory glock
411 * again, since the readdir call will have already taken that lock.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412 *
413 * Returns: errno
414 */
415
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400416struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
417 int is_root, struct nameidata *nd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500419 struct super_block *sb = dir->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400420 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421 struct gfs2_holder d_gh;
akpm@linux-foundation.org037bcbb2007-06-08 16:42:14 -0700422 int error = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500423 struct inode *inode = NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000424 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425
426 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500427 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000428
Steven Whitehousec7526662006-03-20 12:30:04 -0500429 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
430 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
431 dir == sb->s_root->d_inode)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400432 igrab(dir);
433 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000434 }
435
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000436 if (gfs2_glock_is_locked_by_me(dip->i_gl) == 0) {
437 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
438 if (error)
439 return ERR_PTR(error);
440 unlock = 1;
441 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442
443 if (!is_root) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400444 error = permission(dir, MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445 if (error)
446 goto out;
447 }
448
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100449 inode = gfs2_dir_search(dir, name);
450 if (IS_ERR(inode))
451 error = PTR_ERR(inode);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000452out:
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000453 if (unlock)
454 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500455 if (error == -ENOENT)
456 return NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000457 return inode ? inode : ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458}
459
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100460static void gfs2_inum_range_in(struct gfs2_inum_range_host *ir, const void *buf)
461{
462 const struct gfs2_inum_range *str = buf;
463
464 ir->ir_start = be64_to_cpu(str->ir_start);
465 ir->ir_length = be64_to_cpu(str->ir_length);
466}
467
468static void gfs2_inum_range_out(const struct gfs2_inum_range_host *ir, void *buf)
469{
470 struct gfs2_inum_range *str = buf;
471
472 str->ir_start = cpu_to_be64(ir->ir_start);
473 str->ir_length = cpu_to_be64(ir->ir_length);
474}
475
Steven Whitehousecd915492006-09-04 12:49:07 -0400476static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400478 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400480 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481 int error;
482
483 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
484 if (error)
485 return error;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000486 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000487
488 error = gfs2_meta_inode_buffer(ip, &bh);
489 if (error) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000490 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000491 gfs2_trans_end(sdp);
492 return error;
493 }
494
495 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
496
497 if (ir.ir_length) {
498 *formal_ino = ir.ir_start++;
499 ir.ir_length--;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000500 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000501 gfs2_inum_range_out(&ir,
502 bh->b_data + sizeof(struct gfs2_dinode));
503 brelse(bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000504 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 gfs2_trans_end(sdp);
506 return 0;
507 }
508
509 brelse(bh);
510
Steven Whitehousef55ab262006-02-21 12:51:39 +0000511 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 gfs2_trans_end(sdp);
513
514 return 1;
515}
516
Steven Whitehousecd915492006-09-04 12:49:07 -0400517static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400519 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
520 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521 struct gfs2_holder gh;
522 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400523 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000524 int error;
525
526 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
527 if (error)
528 return error;
529
530 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
531 if (error)
532 goto out;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000533 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534
535 error = gfs2_meta_inode_buffer(ip, &bh);
536 if (error)
537 goto out_end_trans;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400538
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
540
541 if (!ir.ir_length) {
542 struct buffer_head *m_bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400543 u64 x, y;
Al Virob44b84d2006-10-14 10:46:30 -0400544 __be64 z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545
546 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
547 if (error)
548 goto out_brelse;
549
Al Virob44b84d2006-10-14 10:46:30 -0400550 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
551 x = y = be64_to_cpu(z);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552 ir.ir_start = x;
553 ir.ir_length = GFS2_INUM_QUANTUM;
554 x += GFS2_INUM_QUANTUM;
555 if (x < y)
556 gfs2_consist_inode(m_ip);
Al Virob44b84d2006-10-14 10:46:30 -0400557 z = cpu_to_be64(x);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000558 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
Al Virob44b84d2006-10-14 10:46:30 -0400559 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000560
561 brelse(m_bh);
562 }
563
564 *formal_ino = ir.ir_start++;
565 ir.ir_length--;
566
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000567 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
569
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400570out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571 brelse(bh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400572out_end_trans:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000573 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574 gfs2_trans_end(sdp);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400575out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577 return error;
578}
579
Steven Whitehousecd915492006-09-04 12:49:07 -0400580static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581{
582 int error;
583
584 error = pick_formal_ino_1(sdp, inum);
585 if (error <= 0)
586 return error;
587
588 error = pick_formal_ino_2(sdp, inum);
589
590 return error;
591}
592
593/**
594 * create_ok - OK to create a new on-disk inode here?
595 * @dip: Directory in which dinode is to be created
596 * @name: Name of new dinode
597 * @mode:
598 *
599 * Returns: errno
600 */
601
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400602static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603 unsigned int mode)
604{
605 int error;
606
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400607 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608 if (error)
609 return error;
610
611 /* Don't create entries in an unlinked directory */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500612 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613 return -EPERM;
614
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100615 error = gfs2_dir_check(&dip->i_inode, name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616 switch (error) {
617 case -ENOENT:
618 error = 0;
619 break;
620 case 0:
621 return -EEXIST;
622 default:
623 return error;
624 }
625
Steven Whitehousecd915492006-09-04 12:49:07 -0400626 if (dip->i_di.di_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000627 return -EFBIG;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500628 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629 return -EMLINK;
630
631 return 0;
632}
633
634static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
635 unsigned int *uid, unsigned int *gid)
636{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400637 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500638 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639 if (S_ISDIR(*mode))
640 *mode |= S_ISUID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500641 else if (dip->i_inode.i_uid != current->fsuid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642 *mode &= ~07111;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500643 *uid = dip->i_inode.i_uid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644 } else
645 *uid = current->fsuid;
646
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500647 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000648 if (S_ISDIR(*mode))
649 *mode |= S_ISGID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500650 *gid = dip->i_inode.i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000651 } else
652 *gid = current->fsgid;
653}
654
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100655static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400657 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 int error;
659
660 gfs2_alloc_get(dip);
661
662 dip->i_alloc.al_requested = RES_DINODE;
663 error = gfs2_inplace_reserve(dip);
664 if (error)
665 goto out;
666
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400667 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000668 if (error)
669 goto out_ipreserv;
670
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100671 *no_addr = gfs2_alloc_di(dip, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672
673 gfs2_trans_end(sdp);
674
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400675out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000676 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400677out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679 return error;
680}
681
682/**
683 * init_dinode - Fill in a new dinode structure
684 * @dip: the directory this inode is being created in
685 * @gl: The glock covering the new inode
686 * @inum: the inode number
687 * @mode: the file permissions
688 * @uid:
689 * @gid:
690 *
691 */
692
693static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400694 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400695 unsigned int uid, unsigned int gid,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400696 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400698 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000699 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700 struct buffer_head *dibh;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100701 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000702
703 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000704 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
706 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000707 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708
Steven Whitehouse2442a092006-01-30 11:49:32 +0000709 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
710 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000711 di->di_mode = cpu_to_be32(mode);
712 di->di_uid = cpu_to_be32(uid);
713 di->di_gid = cpu_to_be32(gid);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500714 di->di_nlink = 0;
715 di->di_size = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000716 di->di_blocks = cpu_to_be64(1);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100717 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500718 di->di_major = cpu_to_be32(MAJOR(dev));
719 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000720 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400721 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500722 di->di_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723
724 if (S_ISREG(mode)) {
725 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
726 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000727 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000728 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
729 gfs2_tune_get(sdp, gt_new_files_directio))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000730 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000731 } else if (S_ISDIR(mode)) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500732 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
733 GFS2_DIF_INHERIT_DIRECTIO);
734 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
735 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736 }
737
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000738 di->__pad1 = 0;
Steven Whitehousea9583c72006-11-01 20:09:14 -0500739 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500740 di->di_height = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000741 di->__pad2 = 0;
742 di->__pad3 = 0;
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500743 di->di_depth = 0;
744 di->di_entries = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000745 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500746 di->di_eattr = 0;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100747 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
748 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
749 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000750 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400751
752 set_buffer_uptodate(dibh);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000753
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400754 *bhp = dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000755}
756
757static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400758 unsigned int mode, const struct gfs2_inum_host *inum,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400759 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400761 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762 unsigned int uid, gid;
763 int error;
764
765 munge_mode_uid_gid(dip, &mode, &uid, &gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 gfs2_alloc_get(dip);
767
768 error = gfs2_quota_lock(dip, uid, gid);
769 if (error)
770 goto out;
771
772 error = gfs2_quota_check(dip, uid, gid);
773 if (error)
774 goto out_quota;
775
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400776 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777 if (error)
778 goto out_quota;
779
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400780 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 gfs2_trans_end(sdp);
783
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400784out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400786out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 return error;
789}
790
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400791static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
792 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400794 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 struct gfs2_alloc *al;
796 int alloc_required;
797 struct buffer_head *dibh;
798 int error;
799
800 al = gfs2_alloc_get(dip);
801
802 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
803 if (error)
804 goto fail;
805
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400806 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500807 if (alloc_required < 0)
808 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 if (alloc_required) {
Steven Whitehouse2933f922006-11-01 13:23:29 -0500810 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811 if (error)
812 goto fail_quota_locks;
813
814 al->al_requested = sdp->sd_max_dirres;
815
816 error = gfs2_inplace_reserve(dip);
817 if (error)
818 goto fail_quota_locks;
819
Steven Whitehouse320dd102006-05-18 16:25:27 -0400820 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100821 al->al_rgd->rd_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400822 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000823 RES_STATFS + RES_QUOTA, 0);
824 if (error)
825 goto fail_ipreserv;
826 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400827 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828 if (error)
829 goto fail_quota_locks;
830 }
831
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100832 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833 if (error)
834 goto fail_end_trans;
835
836 error = gfs2_meta_inode_buffer(ip, &dibh);
837 if (error)
838 goto fail_end_trans;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500839 ip->i_inode.i_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000840 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500841 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 return 0;
844
Steven Whitehouse320dd102006-05-18 16:25:27 -0400845fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000846 gfs2_trans_end(sdp);
847
Steven Whitehouse320dd102006-05-18 16:25:27 -0400848fail_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000849 if (dip->i_alloc.al_rgd)
850 gfs2_inplace_release(dip);
851
Steven Whitehouse320dd102006-05-18 16:25:27 -0400852fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 gfs2_quota_unlock(dip);
854
Steven Whitehouse320dd102006-05-18 16:25:27 -0400855fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857 return error;
858}
859
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400860static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
861{
862 int err;
863 size_t len;
864 void *value;
865 char *name;
866 struct gfs2_ea_request er;
867
868 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
869 &name, &value, &len);
870
871 if (err) {
872 if (err == -EOPNOTSUPP)
873 return 0;
874 return err;
875 }
876
877 memset(&er, 0, sizeof(struct gfs2_ea_request));
878
879 er.er_type = GFS2_EATYPE_SECURITY;
880 er.er_name = name;
881 er.er_data = value;
882 er.er_name_len = strlen(name);
883 er.er_data_len = len;
884
885 err = gfs2_ea_set_i(ip, &er);
886
887 kfree(value);
888 kfree(name);
889
890 return err;
891}
892
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893/**
894 * gfs2_createi - Create a new inode
895 * @ghs: An array of two holders
896 * @name: The name of the new file
897 * @mode: the permissions on the new inode
898 *
899 * @ghs[0] is an initialized holder for the directory
900 * @ghs[1] is the holder for the inode lock
901 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000902 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903 * file are held. A transaction has been started and an inplace reservation
904 * is held, as well.
905 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000906 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000907 */
908
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400909struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500910 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911{
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100912 struct inode *inode = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500913 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400914 struct inode *dir = &dip->i_inode;
915 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100916 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400918 u64 generation;
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400919 struct buffer_head *bh=NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920
921 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000922 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
925 error = gfs2_glock_nq(ghs);
926 if (error)
927 goto fail;
928
929 error = create_ok(dip, name, mode);
930 if (error)
931 goto fail_gunlock;
932
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400933 error = pick_formal_ino(sdp, &inum.no_formal_ino);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934 if (error)
935 goto fail_gunlock;
936
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100937 error = alloc_dinode(dip, &inum.no_addr, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000938 if (error)
939 goto fail_gunlock;
940
Steven Whitehouse28626e22006-11-22 11:13:21 -0500941 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
942 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
943 if (error)
944 goto fail_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000945
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400946 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000947 if (error)
948 goto fail_gunlock2;
949
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400950 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode),
951 inum.no_addr,
952 inum.no_formal_ino);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400953 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954 goto fail_gunlock2;
955
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400956 gfs2_inode_bh(GFS2_I(inode), bh);
957
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400958 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000959 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100960 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400962 error = gfs2_acl_create(dip, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100964 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400966 error = gfs2_security_init(dip, GFS2_I(inode));
967 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100968 goto fail_gunlock2;
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400969
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400970 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100972 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000973
Steven Whitehouse7359a192006-02-13 12:27:43 +0000974 if (!inode)
975 return ERR_PTR(-ENOMEM);
976 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000977
Steven Whitehouse320dd102006-05-18 16:25:27 -0400978fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100980 if (inode)
981 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400982fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400984fail:
Steven Whitehouse7359a192006-02-13 12:27:43 +0000985 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986}
987
988/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989 * gfs2_rmdiri - Remove a directory
990 * @dip: The parent directory of the directory to be removed
991 * @name: The name of the directory to be removed
992 * @ip: The GFS2 inode of the directory to be removed
993 *
994 * Assumes Glocks on dip and ip are held
995 *
996 * Returns: errno
997 */
998
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400999int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
1000 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002 struct qstr dotname;
1003 int error;
1004
1005 if (ip->i_di.di_entries != 2) {
1006 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -05001007 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 return -EIO;
1009 }
1010
1011 error = gfs2_dir_del(dip, name);
1012 if (error)
1013 return error;
1014
1015 error = gfs2_change_nlink(dip, -1);
1016 if (error)
1017 return error;
1018
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001019 gfs2_str2qstr(&dotname, ".");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020 error = gfs2_dir_del(ip, &dotname);
1021 if (error)
1022 return error;
1023
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001024 gfs2_str2qstr(&dotname, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025 error = gfs2_dir_del(ip, &dotname);
1026 if (error)
1027 return error;
1028
Steven Whitehouse4f561102006-11-01 14:04:17 -05001029 /* It looks odd, but it really should be done twice */
1030 error = gfs2_change_nlink(ip, -1);
1031 if (error)
1032 return error;
1033
1034 error = gfs2_change_nlink(ip, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001035 if (error)
1036 return error;
1037
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038 return error;
1039}
1040
1041/*
1042 * gfs2_unlink_ok - check to see that a inode is still in a directory
1043 * @dip: the directory
1044 * @name: the name of the file
1045 * @ip: the inode
1046 *
1047 * Assumes that the lock on (at least) @dip is held.
1048 *
1049 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1050 */
1051
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001052int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001053 const struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001054{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001055 int error;
1056
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001057 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058 return -EPERM;
1059
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001060 if ((dip->i_inode.i_mode & S_ISVTX) &&
Steven Whitehouse2933f922006-11-01 13:23:29 -05001061 dip->i_inode.i_uid != current->fsuid &&
1062 ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 return -EPERM;
1064
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001065 if (IS_APPEND(&dip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066 return -EPERM;
1067
Steven Whitehousefaf450e2006-06-22 10:59:10 -04001068 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069 if (error)
1070 return error;
1071
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001072 error = gfs2_dir_check(&dip->i_inode, name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001073 if (error)
1074 return error;
1075
David Teiglandb3b94fa2006-01-16 16:50:04 +00001076 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 Whitehouse4bd91ba2007-06-05 09:39:18 +01001199 s64 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;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001203 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204
1205 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1206 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1207 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1208 return -EINVAL;
1209
1210 state = gh->gh_state;
1211 flags = gh->gh_flags;
1212
1213 error = gfs2_glock_nq(gh);
1214 if (error)
1215 return error;
1216
1217 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1218 (sdp->sd_vfs->s_flags & MS_RDONLY))
1219 return 0;
1220
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001221 if (tv.tv_sec - ip->i_inode.i_atime.tv_sec >= quantum) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001222 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
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001232 tv = CURRENT_TIME;
1233 if (tv.tv_sec - ip->i_inode.i_atime.tv_sec >= quantum) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001234 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
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001247 ip->i_inode.i_atime = tv;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001248
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;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001251 di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001252 di->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001253 brelse(dibh);
1254
1255 gfs2_trans_end(sdp);
1256 }
1257
1258 /* If someone else has asked for the glock,
1259 unlock and let them have it. Then reacquire
1260 in the original state. */
1261 if (gfs2_glock_is_blocking(gl)) {
1262 gfs2_glock_dq(gh);
1263 gfs2_holder_reinit(state, flags, gh);
1264 return gfs2_glock_nq(gh);
1265 }
1266 }
1267
1268 return 0;
1269
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001270fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001271 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001272fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001273 gfs2_glock_dq(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001274 return error;
1275}
1276
David Teiglandb3b94fa2006-01-16 16:50:04 +00001277static int
1278__gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1279{
1280 struct buffer_head *dibh;
1281 int error;
1282
1283 error = gfs2_meta_inode_buffer(ip, &dibh);
1284 if (!error) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001285 error = inode_setattr(&ip->i_inode, attr);
1286 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001287 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001288 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001289 brelse(dibh);
1290 }
1291 return error;
1292}
1293
1294/**
1295 * gfs2_setattr_simple -
1296 * @ip:
1297 * @attr:
1298 *
1299 * Called with a reference on the vnode.
1300 *
1301 * Returns: errno
1302 */
1303
1304int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1305{
1306 int error;
1307
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001308 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001309 return __gfs2_setattr_simple(ip, attr);
1310
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001311 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001312 if (error)
1313 return error;
1314
1315 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001316 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317 return error;
1318}
1319
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001320void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
1321{
1322 const struct gfs2_dinode_host *di = &ip->i_di;
1323 struct gfs2_dinode *str = buf;
1324
1325 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
1326 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
1327 str->di_header.__pad0 = 0;
1328 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
1329 str->di_header.__pad1 = 0;
1330 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
1331 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
1332 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
1333 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
1334 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
1335 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
1336 str->di_size = cpu_to_be64(di->di_size);
1337 str->di_blocks = cpu_to_be64(di->di_blocks);
1338 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
1339 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
1340 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
1341
1342 str->di_goal_meta = cpu_to_be64(di->di_goal_meta);
1343 str->di_goal_data = cpu_to_be64(di->di_goal_data);
1344 str->di_generation = cpu_to_be64(di->di_generation);
1345
1346 str->di_flags = cpu_to_be32(di->di_flags);
1347 str->di_height = cpu_to_be16(di->di_height);
1348 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
1349 !(ip->i_di.di_flags & GFS2_DIF_EXHASH) ?
1350 GFS2_FORMAT_DE : 0);
1351 str->di_depth = cpu_to_be16(di->di_depth);
1352 str->di_entries = cpu_to_be32(di->di_entries);
1353
1354 str->di_eattr = cpu_to_be64(di->di_eattr);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001355 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
1356 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
1357 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001358}
1359
1360void gfs2_dinode_print(const struct gfs2_inode *ip)
1361{
1362 const struct gfs2_dinode_host *di = &ip->i_di;
1363
1364 printk(KERN_INFO " no_formal_ino = %llu\n",
1365 (unsigned long long)ip->i_no_formal_ino);
1366 printk(KERN_INFO " no_addr = %llu\n",
1367 (unsigned long long)ip->i_no_addr);
1368 printk(KERN_INFO " di_size = %llu\n", (unsigned long long)di->di_size);
1369 printk(KERN_INFO " di_blocks = %llu\n",
1370 (unsigned long long)di->di_blocks);
1371 printk(KERN_INFO " di_goal_meta = %llu\n",
1372 (unsigned long long)di->di_goal_meta);
1373 printk(KERN_INFO " di_goal_data = %llu\n",
1374 (unsigned long long)di->di_goal_data);
1375 printk(KERN_INFO " di_flags = 0x%.8X\n", di->di_flags);
1376 printk(KERN_INFO " di_height = %u\n", di->di_height);
1377 printk(KERN_INFO " di_depth = %u\n", di->di_depth);
1378 printk(KERN_INFO " di_entries = %u\n", di->di_entries);
1379 printk(KERN_INFO " di_eattr = %llu\n",
1380 (unsigned long long)di->di_eattr);
1381}
1382