blob: df0b8b3018b934e72a30830b5a2936498c969be9 [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'Harafcb47e0b2006-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
David Teiglandb3b94fa2006-01-16 16:50:04 +000041static int iget_test(struct inode *inode, void *opaque)
42{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040043 struct gfs2_inode *ip = GFS2_I(inode);
Al Viro629a21e2006-10-13 22:51:24 -040044 struct gfs2_inum_host *inum = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +000045
Steven Whitehouse1be38672007-03-01 10:00:53 +000046 if (ip->i_num.no_addr == inum->no_addr &&
47 inode->i_private != NULL)
David Teiglandb3b94fa2006-01-16 16:50:04 +000048 return 1;
49
50 return 0;
51}
52
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040053static int iget_set(struct inode *inode, void *opaque)
54{
55 struct gfs2_inode *ip = GFS2_I(inode);
Al Viro629a21e2006-10-13 22:51:24 -040056 struct gfs2_inum_host *inum = opaque;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040057
58 ip->i_num = *inum;
Steven Whitehousee7c698d2006-11-08 13:52:05 -050059 inode->i_ino = inum->no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040060 return 0;
61}
62
Al Viro629a21e2006-10-13 22:51:24 -040063struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum_host *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +000064{
Wendy Chengfb0d3bc2007-02-28 11:24:25 -050065 return ilookup5(sb, (unsigned long)inum->no_addr,
David Teiglandb3b94fa2006-01-16 16:50:04 +000066 iget_test, inum);
67}
68
Al Viro629a21e2006-10-13 22:51:24 -040069static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum_host *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +000070{
Wendy Chengfb0d3bc2007-02-28 11:24:25 -050071 return iget5_locked(sb, (unsigned long)inum->no_addr,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040072 iget_test, iget_set, inum);
73}
74
75/**
76 * gfs2_inode_lookup - Lookup an inode
77 * @sb: The super block
78 * @inum: The inode number
79 * @type: The type of the inode
80 *
81 * Returns: A VFS inode, or an error
82 */
83
Al Viro629a21e2006-10-13 22:51:24 -040084struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum_host *inum, unsigned int type)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040085{
86 struct inode *inode = gfs2_iget(sb, inum);
87 struct gfs2_inode *ip = GFS2_I(inode);
88 struct gfs2_glock *io_gl;
89 int error;
90
Steven Whitehouse26d83de2006-10-30 16:59:08 -050091 if (!inode)
92 return ERR_PTR(-ENOBUFS);
93
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040094 if (inode->i_state & I_NEW) {
95 struct gfs2_sbd *sdp = GFS2_SB(inode);
96 umode_t mode = DT2IF(type);
Theodore Ts'obba9dfd2006-09-27 01:50:31 -070097 inode->i_private = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040098 inode->i_mode = mode;
99
100 if (S_ISREG(mode)) {
101 inode->i_op = &gfs2_file_iops;
102 inode->i_fop = &gfs2_file_fops;
103 inode->i_mapping->a_ops = &gfs2_file_aops;
104 } else if (S_ISDIR(mode)) {
105 inode->i_op = &gfs2_dir_iops;
106 inode->i_fop = &gfs2_dir_fops;
107 } else if (S_ISLNK(mode)) {
108 inode->i_op = &gfs2_symlink_iops;
109 } else {
110 inode->i_op = &gfs2_dev_iops;
111 }
112
113 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
114 if (unlikely(error))
115 goto fail;
116 ip->i_gl->gl_object = ip;
117
118 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
119 if (unlikely(error))
120 goto fail_put;
121
Steven Whitehousebfded272006-11-01 16:05:38 -0500122 set_bit(GIF_INVALID, &ip->i_flags);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400123 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
124 if (unlikely(error))
125 goto fail_iopen;
126
127 gfs2_glock_put(io_gl);
128 unlock_new_inode(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000129 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400130
131 return inode;
132fail_iopen:
133 gfs2_glock_put(io_gl);
134fail_put:
135 ip->i_gl->gl_object = NULL;
136 gfs2_glock_put(ip->i_gl);
137fail:
138 iput(inode);
139 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140}
141
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500142static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
Steven Whitehouseea744d02006-10-31 15:28:00 -0500143{
144 struct gfs2_dinode_host *di = &ip->i_di;
145 const struct gfs2_dinode *str = buf;
146
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500147 if (ip->i_num.no_addr != be64_to_cpu(str->di_num.no_addr)) {
148 if (gfs2_consist_inode(ip))
149 gfs2_dinode_print(ip);
150 return -EIO;
151 }
152 if (ip->i_num.no_formal_ino != be64_to_cpu(str->di_num.no_formal_ino))
153 return -ESTALE;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500154
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500155 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500156 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500157 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500158 case S_IFBLK:
159 case S_IFCHR:
160 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
161 be32_to_cpu(str->di_minor));
162 break;
163 };
164
Steven Whitehouse2933f922006-11-01 13:23:29 -0500165 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
166 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
Steven Whitehouse4f561102006-11-01 14:04:17 -0500167 /*
168 * We will need to review setting the nlink count here in the
169 * light of the forthcoming ro bind mount work. This is a reminder
170 * to do that.
171 */
172 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500173 di->di_size = be64_to_cpu(str->di_size);
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500174 i_size_write(&ip->i_inode, di->di_size);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500175 di->di_blocks = be64_to_cpu(str->di_blocks);
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500176 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500177 ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime);
178 ip->i_inode.i_atime.tv_nsec = 0;
179 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
180 ip->i_inode.i_mtime.tv_nsec = 0;
181 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
182 ip->i_inode.i_ctime.tv_nsec = 0;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500183
184 di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
185 di->di_goal_data = be64_to_cpu(str->di_goal_data);
186 di->di_generation = be64_to_cpu(str->di_generation);
187
188 di->di_flags = be32_to_cpu(str->di_flags);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500189 gfs2_set_inode_flags(&ip->i_inode);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500190 di->di_height = be16_to_cpu(str->di_height);
191
192 di->di_depth = be16_to_cpu(str->di_depth);
193 di->di_entries = be32_to_cpu(str->di_entries);
194
195 di->di_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500196 return 0;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500197}
198
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199/**
200 * gfs2_inode_refresh - Refresh the incore copy of the dinode
201 * @ip: The GFS2 inode
202 *
203 * Returns: errno
204 */
205
206int gfs2_inode_refresh(struct gfs2_inode *ip)
207{
208 struct buffer_head *dibh;
209 int error;
210
211 error = gfs2_meta_inode_buffer(ip, &dibh);
212 if (error)
213 return error;
214
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400215 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 brelse(dibh);
217 return -EIO;
218 }
219
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500220 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000221 brelse(dibh);
Steven Whitehousebfded272006-11-01 16:05:38 -0500222 clear_bit(GIF_INVALID, &ip->i_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500224 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225}
226
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400227int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000228{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400229 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 struct gfs2_alloc *al;
231 struct gfs2_rgrpd *rgd;
232 int error;
233
234 if (ip->i_di.di_blocks != 1) {
235 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500236 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000237 return -EIO;
238 }
239
240 al = gfs2_alloc_get(ip);
241
242 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
243 if (error)
244 goto out;
245
246 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
247 if (error)
248 goto out_qs;
249
250 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
251 if (!rgd) {
252 gfs2_consist_inode(ip);
253 error = -EIO;
254 goto out_rindex_relse;
255 }
256
257 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
258 &al->al_rgd_gh);
259 if (error)
260 goto out_rindex_relse;
261
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400262 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263 if (error)
264 goto out_rg_gunlock;
265
266 gfs2_trans_add_gl(ip->i_gl);
267
268 gfs2_free_di(rgd, ip);
269
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270 gfs2_trans_end(sdp);
271 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
272
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400273out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400275out_rindex_relse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000276 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400277out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278 gfs2_quota_unhold(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400279out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400280 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281 return error;
282}
283
284/**
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500285 * gfs2_change_nlink - Change nlink count on inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000286 * @ip: The GFS2 inode
287 * @diff: The change in the nlink count required
288 *
289 * Returns: errno
290 */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500291int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292{
293 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400294 u32 nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295 int error;
296
Steven Whitehouse4f561102006-11-01 14:04:17 -0500297 BUG_ON(diff != 1 && diff != -1);
298 nlink = ip->i_inode.i_nlink + diff;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299
300 /* If we are reducing the nlink count, but the new value ends up being
301 bigger than the old one, we must have underflowed. */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500302 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000303 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500304 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000305 return -EIO;
306 }
307
308 error = gfs2_meta_inode_buffer(ip, &dibh);
309 if (error)
310 return error;
311
Steven Whitehouse4f561102006-11-01 14:04:17 -0500312 if (diff > 0)
313 inc_nlink(&ip->i_inode);
314 else
315 drop_nlink(&ip->i_inode);
316
Eric Sandeenddfe0622007-01-18 16:41:23 -0600317 ip->i_inode.i_ctime = CURRENT_TIME_SEC;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000319 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500320 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400322 mark_inode_dirty(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500324 if (ip->i_inode.i_nlink == 0)
Russell Cattelanddee7602007-01-29 17:13:44 -0600325 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500326
S. Wendy Cheng55098262007-01-18 15:56:34 -0500327 return error;
328}
329
Steven Whitehousec7526662006-03-20 12:30:04 -0500330struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
331{
332 struct qstr qstr;
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600333 struct inode *inode;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500334 gfs2_str2qstr(&qstr, name);
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600335 inode = gfs2_lookupi(dip, &qstr, 1, NULL);
336 /* gfs2_lookupi has inconsistent callers: vfs
337 * related routines expect NULL for no entry found,
338 * gfs2_lookup_simple callers expect ENOENT
339 * and do not check for NULL.
340 */
341 if (inode == NULL)
342 return ERR_PTR(-ENOENT);
343 else
344 return inode;
Steven Whitehousec7526662006-03-20 12:30:04 -0500345}
346
347
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348/**
349 * gfs2_lookupi - Look up a filename in a directory and return its inode
350 * @d_gh: An initialized holder for the directory glock
351 * @name: The name of the inode to look for
352 * @is_root: If 1, ignore the caller's permissions
353 * @i_gh: An uninitialized holder for the new inode glock
354 *
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000355 * This can be called via the VFS filldir function when NFS is doing
356 * a readdirplus and the inode which its intending to stat isn't
357 * already in cache. In this case we must not take the directory glock
358 * again, since the readdir call will have already taken that lock.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000359 *
360 * Returns: errno
361 */
362
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400363struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
364 int is_root, struct nameidata *nd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000365{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500366 struct super_block *sb = dir->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400367 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000368 struct gfs2_holder d_gh;
Al Viro629a21e2006-10-13 22:51:24 -0400369 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370 unsigned int type;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000371 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -0500372 struct inode *inode = NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000373 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374
375 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500376 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000377
Steven Whitehousec7526662006-03-20 12:30:04 -0500378 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
379 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
380 dir == sb->s_root->d_inode)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400381 igrab(dir);
382 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000383 }
384
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000385 if (gfs2_glock_is_locked_by_me(dip->i_gl) == 0) {
386 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
387 if (error)
388 return ERR_PTR(error);
389 unlock = 1;
390 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000391
392 if (!is_root) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400393 error = permission(dir, MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394 if (error)
395 goto out;
396 }
397
Steven Whitehousec7526662006-03-20 12:30:04 -0500398 error = gfs2_dir_search(dir, name, &inum, &type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000399 if (error)
400 goto out;
401
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400402 inode = gfs2_inode_lookup(sb, &inum, type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000403
Steven Whitehouse7359a192006-02-13 12:27:43 +0000404out:
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000405 if (unlock)
406 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500407 if (error == -ENOENT)
408 return NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000409 return inode ? inode : ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000410}
411
Steven Whitehousecd915492006-09-04 12:49:07 -0400412static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000413{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400414 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400416 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417 int error;
418
419 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
420 if (error)
421 return error;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000422 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423
424 error = gfs2_meta_inode_buffer(ip, &bh);
425 if (error) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000426 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427 gfs2_trans_end(sdp);
428 return error;
429 }
430
431 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
432
433 if (ir.ir_length) {
434 *formal_ino = ir.ir_start++;
435 ir.ir_length--;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000436 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437 gfs2_inum_range_out(&ir,
438 bh->b_data + sizeof(struct gfs2_dinode));
439 brelse(bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000440 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441 gfs2_trans_end(sdp);
442 return 0;
443 }
444
445 brelse(bh);
446
Steven Whitehousef55ab262006-02-21 12:51:39 +0000447 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000448 gfs2_trans_end(sdp);
449
450 return 1;
451}
452
Steven Whitehousecd915492006-09-04 12:49:07 -0400453static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400455 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
456 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457 struct gfs2_holder gh;
458 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400459 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460 int error;
461
462 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
463 if (error)
464 return error;
465
466 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
467 if (error)
468 goto out;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000469 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000470
471 error = gfs2_meta_inode_buffer(ip, &bh);
472 if (error)
473 goto out_end_trans;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400474
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
476
477 if (!ir.ir_length) {
478 struct buffer_head *m_bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400479 u64 x, y;
Al Virob44b84d2006-10-14 10:46:30 -0400480 __be64 z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481
482 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
483 if (error)
484 goto out_brelse;
485
Al Virob44b84d2006-10-14 10:46:30 -0400486 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
487 x = y = be64_to_cpu(z);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488 ir.ir_start = x;
489 ir.ir_length = GFS2_INUM_QUANTUM;
490 x += GFS2_INUM_QUANTUM;
491 if (x < y)
492 gfs2_consist_inode(m_ip);
Al Virob44b84d2006-10-14 10:46:30 -0400493 z = cpu_to_be64(x);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000494 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
Al Virob44b84d2006-10-14 10:46:30 -0400495 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000496
497 brelse(m_bh);
498 }
499
500 *formal_ino = ir.ir_start++;
501 ir.ir_length--;
502
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000503 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000504 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
505
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400506out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000507 brelse(bh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400508out_end_trans:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000509 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000510 gfs2_trans_end(sdp);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400511out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 return error;
514}
515
Steven Whitehousecd915492006-09-04 12:49:07 -0400516static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000517{
518 int error;
519
520 error = pick_formal_ino_1(sdp, inum);
521 if (error <= 0)
522 return error;
523
524 error = pick_formal_ino_2(sdp, inum);
525
526 return error;
527}
528
529/**
530 * create_ok - OK to create a new on-disk inode here?
531 * @dip: Directory in which dinode is to be created
532 * @name: Name of new dinode
533 * @mode:
534 *
535 * Returns: errno
536 */
537
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400538static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539 unsigned int mode)
540{
541 int error;
542
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400543 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544 if (error)
545 return error;
546
547 /* Don't create entries in an unlinked directory */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500548 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549 return -EPERM;
550
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400551 error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552 switch (error) {
553 case -ENOENT:
554 error = 0;
555 break;
556 case 0:
557 return -EEXIST;
558 default:
559 return error;
560 }
561
Steven Whitehousecd915492006-09-04 12:49:07 -0400562 if (dip->i_di.di_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563 return -EFBIG;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500564 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 return -EMLINK;
566
567 return 0;
568}
569
570static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
571 unsigned int *uid, unsigned int *gid)
572{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400573 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500574 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 if (S_ISDIR(*mode))
576 *mode |= S_ISUID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500577 else if (dip->i_inode.i_uid != current->fsuid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 *mode &= ~07111;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500579 *uid = dip->i_inode.i_uid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 } else
581 *uid = current->fsuid;
582
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500583 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584 if (S_ISDIR(*mode))
585 *mode |= S_ISGID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500586 *gid = dip->i_inode.i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 } else
588 *gid = current->fsgid;
589}
590
Al Viro629a21e2006-10-13 22:51:24 -0400591static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum_host *inum,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400592 u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400594 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595 int error;
596
597 gfs2_alloc_get(dip);
598
599 dip->i_alloc.al_requested = RES_DINODE;
600 error = gfs2_inplace_reserve(dip);
601 if (error)
602 goto out;
603
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400604 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605 if (error)
606 goto out_ipreserv;
607
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400608 inum->no_addr = gfs2_alloc_di(dip, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609
610 gfs2_trans_end(sdp);
611
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400612out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400614out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616 return error;
617}
618
619/**
620 * init_dinode - Fill in a new dinode structure
621 * @dip: the directory this inode is being created in
622 * @gl: The glock covering the new inode
623 * @inum: the inode number
624 * @mode: the file permissions
625 * @uid:
626 * @gid:
627 *
628 */
629
630static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400631 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400632 unsigned int uid, unsigned int gid,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500633 const u64 *generation, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400635 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000636 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637 struct buffer_head *dibh;
638
639 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000640 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
642 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000643 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644
Steven Whitehouse2442a092006-01-30 11:49:32 +0000645 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
646 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000647 di->di_mode = cpu_to_be32(mode);
648 di->di_uid = cpu_to_be32(uid);
649 di->di_gid = cpu_to_be32(gid);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500650 di->di_nlink = 0;
651 di->di_size = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000652 di->di_blocks = cpu_to_be64(1);
653 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500654 di->di_major = cpu_to_be32(MAJOR(dev));
655 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000656 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400657 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500658 di->di_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659
660 if (S_ISREG(mode)) {
661 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
662 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000663 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
665 gfs2_tune_get(sdp, gt_new_files_directio))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000666 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 } else if (S_ISDIR(mode)) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500668 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
669 GFS2_DIF_INHERIT_DIRECTIO);
670 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
671 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672 }
673
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000674 di->__pad1 = 0;
Steven Whitehousea9583c72006-11-01 20:09:14 -0500675 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500676 di->di_height = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000677 di->__pad2 = 0;
678 di->__pad3 = 0;
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500679 di->di_depth = 0;
680 di->di_entries = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000681 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500682 di->di_eattr = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000683 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
684
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685 brelse(dibh);
686}
687
688static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400689 unsigned int mode, const struct gfs2_inum_host *inum,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500690 const u64 *generation, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400692 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693 unsigned int uid, gid;
694 int error;
695
696 munge_mode_uid_gid(dip, &mode, &uid, &gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697 gfs2_alloc_get(dip);
698
699 error = gfs2_quota_lock(dip, uid, gid);
700 if (error)
701 goto out;
702
703 error = gfs2_quota_check(dip, uid, gid);
704 if (error)
705 goto out_quota;
706
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400707 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708 if (error)
709 goto out_quota;
710
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500711 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713 gfs2_trans_end(sdp);
714
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400715out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400717out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719 return error;
720}
721
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400722static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
723 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400725 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 struct gfs2_alloc *al;
727 int alloc_required;
728 struct buffer_head *dibh;
729 int error;
730
731 al = gfs2_alloc_get(dip);
732
733 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
734 if (error)
735 goto fail;
736
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400737 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500738 if (alloc_required < 0)
739 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000740 if (alloc_required) {
Steven Whitehouse2933f922006-11-01 13:23:29 -0500741 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 if (error)
743 goto fail_quota_locks;
744
745 al->al_requested = sdp->sd_max_dirres;
746
747 error = gfs2_inplace_reserve(dip);
748 if (error)
749 goto fail_quota_locks;
750
Steven Whitehouse320dd102006-05-18 16:25:27 -0400751 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752 al->al_rgd->rd_ri.ri_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400753 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754 RES_STATFS + RES_QUOTA, 0);
755 if (error)
756 goto fail_ipreserv;
757 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400758 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 if (error)
760 goto fail_quota_locks;
761 }
762
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500763 error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 if (error)
765 goto fail_end_trans;
766
767 error = gfs2_meta_inode_buffer(ip, &dibh);
768 if (error)
769 goto fail_end_trans;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500770 ip->i_inode.i_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000771 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500772 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 return 0;
775
Steven Whitehouse320dd102006-05-18 16:25:27 -0400776fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777 gfs2_trans_end(sdp);
778
Steven Whitehouse320dd102006-05-18 16:25:27 -0400779fail_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780 if (dip->i_alloc.al_rgd)
781 gfs2_inplace_release(dip);
782
Steven Whitehouse320dd102006-05-18 16:25:27 -0400783fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000784 gfs2_quota_unlock(dip);
785
Steven Whitehouse320dd102006-05-18 16:25:27 -0400786fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 return error;
789}
790
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400791static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
792{
793 int err;
794 size_t len;
795 void *value;
796 char *name;
797 struct gfs2_ea_request er;
798
799 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
800 &name, &value, &len);
801
802 if (err) {
803 if (err == -EOPNOTSUPP)
804 return 0;
805 return err;
806 }
807
808 memset(&er, 0, sizeof(struct gfs2_ea_request));
809
810 er.er_type = GFS2_EATYPE_SECURITY;
811 er.er_name = name;
812 er.er_data = value;
813 er.er_name_len = strlen(name);
814 er.er_data_len = len;
815
816 err = gfs2_ea_set_i(ip, &er);
817
818 kfree(value);
819 kfree(name);
820
821 return err;
822}
823
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824/**
825 * gfs2_createi - Create a new inode
826 * @ghs: An array of two holders
827 * @name: The name of the new file
828 * @mode: the permissions on the new inode
829 *
830 * @ghs[0] is an initialized holder for the directory
831 * @ghs[1] is the holder for the inode lock
832 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000833 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834 * file are held. A transaction has been started and an inplace reservation
835 * is held, as well.
836 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000837 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838 */
839
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400840struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500841 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842{
Steven Whitehouse7359a192006-02-13 12:27:43 +0000843 struct inode *inode;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500844 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400845 struct inode *dir = &dip->i_inode;
846 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Al Viro629a21e2006-10-13 22:51:24 -0400847 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400849 u64 generation;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850
851 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000852 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
855 error = gfs2_glock_nq(ghs);
856 if (error)
857 goto fail;
858
859 error = create_ok(dip, name, mode);
860 if (error)
861 goto fail_gunlock;
862
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400863 error = pick_formal_ino(sdp, &inum.no_formal_ino);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 if (error)
865 goto fail_gunlock;
866
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400867 error = alloc_dinode(dip, &inum, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868 if (error)
869 goto fail_gunlock;
870
Steven Whitehouse28626e22006-11-22 11:13:21 -0500871 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
872 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
873 if (error)
874 goto fail_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500876 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877 if (error)
878 goto fail_gunlock2;
879
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400880 inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode));
881 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882 goto fail_gunlock2;
883
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400884 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 if (error)
886 goto fail_iput;
887
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400888 error = gfs2_acl_create(dip, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889 if (error)
890 goto fail_iput;
891
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400892 error = gfs2_security_init(dip, GFS2_I(inode));
893 if (error)
894 goto fail_iput;
895
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400896 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897 if (error)
898 goto fail_iput;
899
Steven Whitehouse7359a192006-02-13 12:27:43 +0000900 if (!inode)
901 return ERR_PTR(-ENOMEM);
902 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903
Steven Whitehouse320dd102006-05-18 16:25:27 -0400904fail_iput:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400905 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400906fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000907 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400908fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400910fail:
Steven Whitehouse7359a192006-02-13 12:27:43 +0000911 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000912}
913
914/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000915 * gfs2_rmdiri - Remove a directory
916 * @dip: The parent directory of the directory to be removed
917 * @name: The name of the directory to be removed
918 * @ip: The GFS2 inode of the directory to be removed
919 *
920 * Assumes Glocks on dip and ip are held
921 *
922 * Returns: errno
923 */
924
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400925int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
926 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928 struct qstr dotname;
929 int error;
930
931 if (ip->i_di.di_entries != 2) {
932 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500933 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934 return -EIO;
935 }
936
937 error = gfs2_dir_del(dip, name);
938 if (error)
939 return error;
940
941 error = gfs2_change_nlink(dip, -1);
942 if (error)
943 return error;
944
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500945 gfs2_str2qstr(&dotname, ".");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946 error = gfs2_dir_del(ip, &dotname);
947 if (error)
948 return error;
949
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400950 gfs2_str2qstr(&dotname, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 error = gfs2_dir_del(ip, &dotname);
952 if (error)
953 return error;
954
Steven Whitehouse4f561102006-11-01 14:04:17 -0500955 /* It looks odd, but it really should be done twice */
956 error = gfs2_change_nlink(ip, -1);
957 if (error)
958 return error;
959
960 error = gfs2_change_nlink(ip, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961 if (error)
962 return error;
963
David Teiglandb3b94fa2006-01-16 16:50:04 +0000964 return error;
965}
966
967/*
968 * gfs2_unlink_ok - check to see that a inode is still in a directory
969 * @dip: the directory
970 * @name: the name of the file
971 * @ip: the inode
972 *
973 * Assumes that the lock on (at least) @dip is held.
974 *
975 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
976 */
977
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400978int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 struct gfs2_inode *ip)
980{
Al Viro629a21e2006-10-13 22:51:24 -0400981 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000982 unsigned int type;
983 int error;
984
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400985 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986 return -EPERM;
987
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500988 if ((dip->i_inode.i_mode & S_ISVTX) &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500989 dip->i_inode.i_uid != current->fsuid &&
990 ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000991 return -EPERM;
992
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400993 if (IS_APPEND(&dip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000994 return -EPERM;
995
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400996 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000997 if (error)
998 return error;
999
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001000 error = gfs2_dir_search(&dip->i_inode, name, &inum, &type);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001 if (error)
1002 return error;
1003
1004 if (!gfs2_inum_equal(&inum, &ip->i_num))
1005 return -ENOENT;
1006
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001007 if (IF2DT(ip->i_inode.i_mode) != type) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 gfs2_consist_inode(dip);
1009 return -EIO;
1010 }
1011
1012 return 0;
1013}
1014
1015/*
1016 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1017 * @this: move this
1018 * @to: to here
1019 *
1020 * Follow @to back to the root and make sure we don't encounter @this
1021 * Assumes we already hold the rename lock.
1022 *
1023 * Returns: errno
1024 */
1025
1026int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1027{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001028 struct inode *dir = &to->i_inode;
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001029 struct super_block *sb = dir->i_sb;
Steven Whitehouse7359a192006-02-13 12:27:43 +00001030 struct inode *tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031 struct qstr dotdot;
1032 int error = 0;
1033
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001034 gfs2_str2qstr(&dotdot, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001035
Steven Whitehouse7359a192006-02-13 12:27:43 +00001036 igrab(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001037
1038 for (;;) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001039 if (dir == &this->i_inode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 error = -EINVAL;
1041 break;
1042 }
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001043 if (dir == sb->s_root->d_inode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044 error = 0;
1045 break;
1046 }
1047
Steven Whitehousec7526662006-03-20 12:30:04 -05001048 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1049 if (IS_ERR(tmp)) {
1050 error = PTR_ERR(tmp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001051 break;
Steven Whitehousec7526662006-03-20 12:30:04 -05001052 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053
Steven Whitehouse7359a192006-02-13 12:27:43 +00001054 iput(dir);
1055 dir = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056 }
1057
Steven Whitehouse7359a192006-02-13 12:27:43 +00001058 iput(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059
1060 return error;
1061}
1062
1063/**
1064 * gfs2_readlinki - return the contents of a symlink
1065 * @ip: the symlink's inode
1066 * @buf: a pointer to the buffer to be filled
1067 * @len: a pointer to the length of @buf
1068 *
1069 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1070 * to be freed by the caller.
1071 *
1072 * Returns: errno
1073 */
1074
1075int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1076{
1077 struct gfs2_holder i_gh;
1078 struct buffer_head *dibh;
1079 unsigned int x;
1080 int error;
1081
1082 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1083 error = gfs2_glock_nq_atime(&i_gh);
1084 if (error) {
1085 gfs2_holder_uninit(&i_gh);
1086 return error;
1087 }
1088
1089 if (!ip->i_di.di_size) {
1090 gfs2_consist_inode(ip);
1091 error = -EIO;
1092 goto out;
1093 }
1094
1095 error = gfs2_meta_inode_buffer(ip, &dibh);
1096 if (error)
1097 goto out;
1098
1099 x = ip->i_di.di_size + 1;
1100 if (x > *len) {
1101 *buf = kmalloc(x, GFP_KERNEL);
1102 if (!*buf) {
1103 error = -ENOMEM;
1104 goto out_brelse;
1105 }
1106 }
1107
1108 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1109 *len = x;
1110
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001111out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001112 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001113out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115 return error;
1116}
1117
1118/**
1119 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1120 * conditionally update the inode's atime
1121 * @gh: the holder to acquire
1122 *
1123 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1124 * Update if the difference between the current time and the inode's current
1125 * atime is greater than an interval specified at mount.
1126 *
1127 * Returns: errno
1128 */
1129
1130int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1131{
1132 struct gfs2_glock *gl = gh->gh_gl;
1133 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001134 struct gfs2_inode *ip = gl->gl_object;
Steven Whitehousecd915492006-09-04 12:49:07 -04001135 s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136 unsigned int state;
1137 int flags;
1138 int error;
1139
1140 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1141 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1142 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1143 return -EINVAL;
1144
1145 state = gh->gh_state;
1146 flags = gh->gh_flags;
1147
1148 error = gfs2_glock_nq(gh);
1149 if (error)
1150 return error;
1151
1152 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1153 (sdp->sd_vfs->s_flags & MS_RDONLY))
1154 return 0;
1155
1156 curtime = get_seconds();
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001157 if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001158 gfs2_glock_dq(gh);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001159 gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1160 gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001161 error = gfs2_glock_nq(gh);
1162 if (error)
1163 return error;
1164
1165 /* Verify that atime hasn't been updated while we were
1166 trying to get exclusive lock. */
1167
1168 curtime = get_seconds();
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001169 if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001170 struct buffer_head *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -04001171 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001172
1173 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1174 if (error == -EROFS)
1175 return 0;
1176 if (error)
1177 goto fail;
1178
1179 error = gfs2_meta_inode_buffer(ip, &dibh);
1180 if (error)
1181 goto fail_end_trans;
1182
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001183 ip->i_inode.i_atime.tv_sec = curtime;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001184
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001185 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse48516ce2006-10-02 12:39:19 -04001186 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001187 di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001188 brelse(dibh);
1189
1190 gfs2_trans_end(sdp);
1191 }
1192
1193 /* If someone else has asked for the glock,
1194 unlock and let them have it. Then reacquire
1195 in the original state. */
1196 if (gfs2_glock_is_blocking(gl)) {
1197 gfs2_glock_dq(gh);
1198 gfs2_holder_reinit(state, flags, gh);
1199 return gfs2_glock_nq(gh);
1200 }
1201 }
1202
1203 return 0;
1204
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001205fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001206 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001207fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001208 gfs2_glock_dq(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209 return error;
1210}
1211
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212static int
1213__gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1214{
1215 struct buffer_head *dibh;
1216 int error;
1217
1218 error = gfs2_meta_inode_buffer(ip, &dibh);
1219 if (!error) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001220 error = inode_setattr(&ip->i_inode, attr);
1221 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001222 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001223 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001224 brelse(dibh);
1225 }
1226 return error;
1227}
1228
1229/**
1230 * gfs2_setattr_simple -
1231 * @ip:
1232 * @attr:
1233 *
1234 * Called with a reference on the vnode.
1235 *
1236 * Returns: errno
1237 */
1238
1239int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1240{
1241 int error;
1242
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001243 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244 return __gfs2_setattr_simple(ip, attr);
1245
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001246 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001247 if (error)
1248 return error;
1249
1250 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001251 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252 return error;
1253}
1254