blob: 291f0c7eaa3bcf8249876a73e43f8920dbfa1f24 [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
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
14#include <linux/namei.h>
15#include <linux/utsname.h>
16#include <linux/mm.h>
17#include <linux/xattr.h>
18#include <linux/posix_acl.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050020#include <linux/crc32.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020021#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include <asm/uaccess.h>
23
24#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050025#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#include "acl.h"
27#include "bmap.h"
28#include "dir.h"
29#include "eaops.h"
30#include "eattr.h"
31#include "glock.h"
32#include "inode.h"
33#include "meta_io.h"
34#include "ops_dentry.h"
35#include "ops_inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000036#include "quota.h"
37#include "rgrp.h"
38#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050039#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000040
41/**
42 * gfs2_create - Create a file
43 * @dir: The directory in which to create the file
44 * @dentry: The dentry of the new file
45 * @mode: The mode of the new file
46 *
47 * Returns: errno
48 */
49
50static int gfs2_create(struct inode *dir, struct dentry *dentry,
51 int mode, struct nameidata *nd)
52{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040053 struct gfs2_inode *dip = GFS2_I(dir);
54 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +000055 struct gfs2_holder ghs[2];
56 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +000057
David Teiglandb3b94fa2006-01-16 16:50:04 +000058 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
59
60 for (;;) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -050061 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +000062 if (!IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000063 gfs2_trans_end(sdp);
64 if (dip->i_alloc.al_rgd)
65 gfs2_inplace_release(dip);
66 gfs2_quota_unlock(dip);
67 gfs2_alloc_put(dip);
68 gfs2_glock_dq_uninit_m(2, ghs);
Steven Whitehouse3a8476d2006-06-19 09:10:39 -040069 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000070 break;
Steven Whitehouse7359a192006-02-13 12:27:43 +000071 } else if (PTR_ERR(inode) != -EEXIST ||
Steve Frenchafd09422007-07-20 13:07:26 -050072 (nd && (nd->intent.open.flags & O_EXCL))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000073 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +000074 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000075 }
76
Steven Whitehousec7526662006-03-20 12:30:04 -050077 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
78 if (inode) {
79 if (!IS_ERR(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -050080 gfs2_holder_uninit(ghs);
81 break;
82 } else {
83 gfs2_holder_uninit(ghs);
84 return PTR_ERR(inode);
85 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000086 }
87 }
88
David Teiglandb3b94fa2006-01-16 16:50:04 +000089 d_instantiate(dentry, inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000090
91 return 0;
92}
93
94/**
95 * gfs2_lookup - Look up a filename in a directory and return its inode
96 * @dir: The directory inode
97 * @dentry: The dentry of the new inode
98 * @nd: passed from Linux VFS, ignored by us
99 *
100 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
101 *
102 * Returns: errno
103 */
104
105static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
106 struct nameidata *nd)
107{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109
Steven Whitehousec7526662006-03-20 12:30:04 -0500110 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000111
Steven Whitehousec7526662006-03-20 12:30:04 -0500112 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
113 if (inode && IS_ERR(inode))
114 return ERR_PTR(PTR_ERR(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115
116 if (inode)
117 return d_splice_alias(inode, dentry);
118 d_add(dentry, inode);
119
120 return NULL;
121}
122
123/**
124 * gfs2_link - Link to a file
125 * @old_dentry: The inode to link
126 * @dir: Add link to this directory
127 * @dentry: The name of the link
128 *
129 * Link the inode in "old_dentry" into the directory "dir" with the
130 * name in "dentry".
131 *
132 * Returns: errno
133 */
134
135static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
136 struct dentry *dentry)
137{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400138 struct gfs2_inode *dip = GFS2_I(dir);
139 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 struct inode *inode = old_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400141 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000142 struct gfs2_holder ghs[2];
143 int alloc_required;
144 int error;
145
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500146 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 return -EPERM;
148
149 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
150 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
151
152 error = gfs2_glock_nq_m(2, ghs);
153 if (error)
154 goto out;
155
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400156 error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 if (error)
158 goto out_gunlock;
159
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100160 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 switch (error) {
162 case -ENOENT:
163 break;
164 case 0:
165 error = -EEXIST;
166 default:
167 goto out_gunlock;
168 }
169
170 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500171 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 goto out_gunlock;
173 error = -EFBIG;
Steven Whitehousecd915492006-09-04 12:49:07 -0400174 if (dip->i_di.di_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 goto out_gunlock;
176 error = -EPERM;
177 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
178 goto out_gunlock;
179 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500180 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181 goto out_gunlock;
182 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500183 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184 goto out_gunlock;
185
Steven Whitehousec7526662006-03-20 12:30:04 -0500186 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
187 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000188 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500189 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190
191 if (alloc_required) {
192 struct gfs2_alloc *al = gfs2_alloc_get(dip);
193
194 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
195 if (error)
196 goto out_alloc;
197
Steven Whitehouse2933f922006-11-01 13:23:29 -0500198 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199 if (error)
200 goto out_gunlock_q;
201
202 al->al_requested = sdp->sd_max_dirres;
203
204 error = gfs2_inplace_reserve(dip);
205 if (error)
206 goto out_gunlock_q;
207
Steven Whitehouse1b502592006-05-18 14:10:52 -0400208 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100209 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210 2 * RES_DINODE + RES_STATFS +
211 RES_QUOTA, 0);
212 if (error)
213 goto out_ipres;
214 } else {
215 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
216 if (error)
217 goto out_ipres;
218 }
219
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100220 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000221 if (error)
222 goto out_end_trans;
223
224 error = gfs2_change_nlink(ip, +1);
225
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400226out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400228out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229 if (alloc_required)
230 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400231out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232 if (alloc_required)
233 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400234out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235 if (alloc_required)
236 gfs2_alloc_put(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400237out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238 gfs2_glock_dq_m(2, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400239out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 gfs2_holder_uninit(ghs);
241 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 if (!error) {
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400243 atomic_inc(&inode->i_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244 d_instantiate(dentry, inode);
245 mark_inode_dirty(inode);
246 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247 return error;
248}
249
250/**
251 * gfs2_unlink - Unlink a file
252 * @dir: The inode of the directory containing the file to unlink
253 * @dentry: The file itself
254 *
255 * Unlink a file. Call gfs2_unlinki()
256 *
257 * Returns: errno
258 */
259
260static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
261{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400262 struct gfs2_inode *dip = GFS2_I(dir);
263 struct gfs2_sbd *sdp = GFS2_SB(dir);
264 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600265 struct gfs2_holder ghs[3];
266 struct gfs2_rgrpd *rgd;
267 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268 int error;
269
Russell Cattelanddee7602007-01-29 17:13:44 -0600270 error = gfs2_rindex_hold(sdp, &ri_gh);
271 if (error)
272 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000273
Russell Cattelanddee7602007-01-29 17:13:44 -0600274 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
275 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
276
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100277 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600278 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
279
280
Steven Whitehouse8497a462007-08-26 14:23:56 +0100281 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100283 goto out_parent;
284
285 error = gfs2_glock_nq(ghs + 1); /* child */
286 if (error)
287 goto out_child;
288
289 error = gfs2_glock_nq(ghs + 2); /* rgrp */
290 if (error)
291 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292
293 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
294 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100295 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000296
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400297 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100299 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400301 error = gfs2_dir_del(dip, &dentry->d_name);
302 if (error)
303 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000304
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400305 error = gfs2_change_nlink(ip, -1);
306
307out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308 gfs2_trans_end(sdp);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100309 gfs2_glock_dq(ghs + 2);
310out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600311 gfs2_holder_uninit(ghs + 2);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100312 gfs2_glock_dq(ghs + 1);
313out_child:
314 gfs2_holder_uninit(ghs + 1);
315 gfs2_glock_dq(ghs);
316out_parent:
317 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600318 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319 return error;
320}
321
322/**
323 * gfs2_symlink - Create a symlink
324 * @dir: The directory to create the symlink in
325 * @dentry: The dentry to put the symlink in
326 * @symname: The thing which the link points to
327 *
328 * Returns: errno
329 */
330
331static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
332 const char *symname)
333{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400334 struct gfs2_inode *dip = GFS2_I(dir), *ip;
335 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336 struct gfs2_holder ghs[2];
337 struct inode *inode;
338 struct buffer_head *dibh;
339 int size;
340 int error;
341
David Teiglandb3b94fa2006-01-16 16:50:04 +0000342 /* Must be stuffed with a null terminator for gfs2_follow_link() */
343 size = strlen(symname);
344 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
345 return -ENAMETOOLONG;
346
347 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
348
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500349 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000350 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000352 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353 }
354
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500355 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356
357 ip->i_di.di_size = size;
358
359 error = gfs2_meta_inode_buffer(ip, &dibh);
360
361 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500362 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000363 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
364 size);
365 brelse(dibh);
366 }
367
368 gfs2_trans_end(sdp);
369 if (dip->i_alloc.al_rgd)
370 gfs2_inplace_release(dip);
371 gfs2_quota_unlock(dip);
372 gfs2_alloc_put(dip);
373
374 gfs2_glock_dq_uninit_m(2, ghs);
375
David Teiglandb3b94fa2006-01-16 16:50:04 +0000376 d_instantiate(dentry, inode);
377 mark_inode_dirty(inode);
378
379 return 0;
380}
381
382/**
383 * gfs2_mkdir - Make a directory
384 * @dir: The parent directory of the new one
385 * @dentry: The dentry of the new directory
386 * @mode: The mode of the new directory
387 *
388 * Returns: errno
389 */
390
391static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
392{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400393 struct gfs2_inode *dip = GFS2_I(dir), *ip;
394 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000395 struct gfs2_holder ghs[2];
396 struct inode *inode;
397 struct buffer_head *dibh;
398 int error;
399
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
401
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500402 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000403 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000404 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000405 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000406 }
407
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500408 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409
Steven Whitehouse4f561102006-11-01 14:04:17 -0500410 ip->i_inode.i_nlink = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
412 ip->i_di.di_flags |= GFS2_DIF_JDATA;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000413 ip->i_di.di_entries = 2;
414
415 error = gfs2_meta_inode_buffer(ip, &dibh);
416
417 if (!gfs2_assert_withdraw(sdp, !error)) {
418 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500419 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500420 struct qstr str;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500422 gfs2_str2qstr(&str, ".");
Steven Whitehousec7526662006-03-20 12:30:04 -0500423 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
424 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400426 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427 di->di_entries = cpu_to_be32(1);
428
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500429 gfs2_str2qstr(&str, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500430 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
431 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100433 gfs2_inum_out(dip, dent);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400434 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500436 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437
438 brelse(dibh);
439 }
440
441 error = gfs2_change_nlink(dip, +1);
442 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
443
444 gfs2_trans_end(sdp);
445 if (dip->i_alloc.al_rgd)
446 gfs2_inplace_release(dip);
447 gfs2_quota_unlock(dip);
448 gfs2_alloc_put(dip);
449
450 gfs2_glock_dq_uninit_m(2, ghs);
451
David Teiglandb3b94fa2006-01-16 16:50:04 +0000452 d_instantiate(dentry, inode);
453 mark_inode_dirty(inode);
454
455 return 0;
456}
457
458/**
459 * gfs2_rmdir - Remove a directory
460 * @dir: The parent directory of the directory to be removed
461 * @dentry: The dentry of the directory to remove
462 *
463 * Remove a directory. Call gfs2_rmdiri()
464 *
465 * Returns: errno
466 */
467
468static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
469{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400470 struct gfs2_inode *dip = GFS2_I(dir);
471 struct gfs2_sbd *sdp = GFS2_SB(dir);
472 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600473 struct gfs2_holder ghs[3];
474 struct gfs2_rgrpd *rgd;
475 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000476 int error;
477
Russell Cattelanddee7602007-01-29 17:13:44 -0600478
479 error = gfs2_rindex_hold(sdp, &ri_gh);
480 if (error)
481 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
483 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
484
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100485 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600486 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
487
488 error = gfs2_glock_nq_m(3, ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 if (error)
490 goto out;
491
492 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
493 if (error)
494 goto out_gunlock;
495
496 if (ip->i_di.di_entries < 2) {
497 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500498 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 error = -EIO;
500 goto out_gunlock;
501 }
502 if (ip->i_di.di_entries > 2) {
503 error = -ENOTEMPTY;
504 goto out_gunlock;
505 }
506
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400507 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508 if (error)
509 goto out_gunlock;
510
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400511 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512
513 gfs2_trans_end(sdp);
514
Steven Whitehousea91ea692006-09-04 12:04:26 -0400515out_gunlock:
Russell Cattelanddee7602007-01-29 17:13:44 -0600516 gfs2_glock_dq_m(3, ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400517out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518 gfs2_holder_uninit(ghs);
519 gfs2_holder_uninit(ghs + 1);
Russell Cattelanddee7602007-01-29 17:13:44 -0600520 gfs2_holder_uninit(ghs + 2);
521 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522 return error;
523}
524
525/**
526 * gfs2_mknod - Make a special file
527 * @dir: The directory in which the special file will reside
528 * @dentry: The dentry of the special file
529 * @mode: The mode of the special file
530 * @rdev: The device specification of the special file
531 *
532 */
533
534static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
535 dev_t dev)
536{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500537 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400538 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539 struct gfs2_holder ghs[2];
540 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541
542 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
543
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500544 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000545 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000547 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548 }
549
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550 gfs2_trans_end(sdp);
551 if (dip->i_alloc.al_rgd)
552 gfs2_inplace_release(dip);
553 gfs2_quota_unlock(dip);
554 gfs2_alloc_put(dip);
555
556 gfs2_glock_dq_uninit_m(2, ghs);
557
David Teiglandb3b94fa2006-01-16 16:50:04 +0000558 d_instantiate(dentry, inode);
559 mark_inode_dirty(inode);
560
561 return 0;
562}
563
564/**
565 * gfs2_rename - Rename a file
566 * @odir: Parent directory of old file name
567 * @odentry: The old dentry of the file
568 * @ndir: Parent directory of new file name
569 * @ndentry: The new dentry of the file
570 *
571 * Returns: errno
572 */
573
574static int gfs2_rename(struct inode *odir, struct dentry *odentry,
575 struct inode *ndir, struct dentry *ndentry)
576{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400577 struct gfs2_inode *odip = GFS2_I(odir);
578 struct gfs2_inode *ndip = GFS2_I(ndir);
579 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400581 struct gfs2_sbd *sdp = GFS2_SB(odir);
Russell Cattelanddee7602007-01-29 17:13:44 -0600582 struct gfs2_holder ghs[5], r_gh;
583 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584 unsigned int num_gh;
585 int dir_rename = 0;
586 int alloc_required;
587 unsigned int x;
588 int error;
589
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400591 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592 if (ip == nip)
593 return 0;
594 }
595
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596 /* Make sure we aren't trying to move a dirctory into it's subdir */
597
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500598 if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000599 dir_rename = 1;
600
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500601 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000602 &r_gh);
603 if (error)
604 goto out;
605
606 error = gfs2_ok_to_move(ip, ndip);
607 if (error)
608 goto out_gunlock_r;
609 }
610
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400611 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400613 if (odip != ndip) {
614 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
615 num_gh++;
616 }
617 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
618 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400620 if (nip) {
621 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
622 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600623 /* grab the resource lock for unlink flag twiddling
624 * this is the case of the target file already existing
625 * so we unlink before doing the rename
626 */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100627 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600628 if (nrgd)
629 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400630 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000631
632 error = gfs2_glock_nq_m(num_gh, ghs);
633 if (error)
634 goto out_uninit;
635
636 /* Check out the old directory */
637
638 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
639 if (error)
640 goto out_gunlock;
641
642 /* Check out the new directory */
643
644 if (nip) {
645 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
646 if (error)
647 goto out_gunlock;
648
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500649 if (S_ISDIR(nip->i_inode.i_mode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 if (nip->i_di.di_entries < 2) {
651 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500652 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000653 error = -EIO;
654 goto out_gunlock;
655 }
656 if (nip->i_di.di_entries > 2) {
657 error = -ENOTEMPTY;
658 goto out_gunlock;
659 }
660 }
661 } else {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400662 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000663 if (error)
664 goto out_gunlock;
665
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100666 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 switch (error) {
668 case -ENOENT:
669 error = 0;
670 break;
671 case 0:
672 error = -EEXIST;
673 default:
674 goto out_gunlock;
675 };
676
677 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500678 if (!ndip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679 error = -EINVAL;
680 goto out_gunlock;
681 }
Steven Whitehousecd915492006-09-04 12:49:07 -0400682 if (ndip->i_di.di_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683 error = -EFBIG;
684 goto out_gunlock;
685 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500686 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500687 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688 error = -EMLINK;
689 goto out_gunlock;
690 }
691 }
692 }
693
694 /* Check out the dir to be renamed */
695
696 if (dir_rename) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400697 error = permission(odentry->d_inode, MAY_WRITE, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000698 if (error)
699 goto out_gunlock;
700 }
701
Steven Whitehousec7526662006-03-20 12:30:04 -0500702 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
703 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000704 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500705 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706
707 if (alloc_required) {
708 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
709
710 error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
711 if (error)
712 goto out_alloc;
713
Steven Whitehouse2933f922006-11-01 13:23:29 -0500714 error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715 if (error)
716 goto out_gunlock_q;
717
718 al->al_requested = sdp->sd_max_dirres;
719
720 error = gfs2_inplace_reserve(ndip);
721 if (error)
722 goto out_gunlock_q;
723
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400724 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100725 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500727 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000728 if (error)
729 goto out_ipreserv;
730 } else {
731 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500732 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733 if (error)
734 goto out_gunlock;
735 }
736
737 /* Remove the target file, if it exists */
738
739 if (nip) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500740 if (S_ISDIR(nip->i_inode.i_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400741 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
742 else {
743 error = gfs2_dir_del(ndip, &ndentry->d_name);
744 if (error)
745 goto out_end_trans;
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500746 error = gfs2_change_nlink(nip, -1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400747 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000748 if (error)
749 goto out_end_trans;
750 }
751
752 if (dir_rename) {
753 struct qstr name;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500754 gfs2_str2qstr(&name, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000755
756 error = gfs2_change_nlink(ndip, +1);
757 if (error)
758 goto out_end_trans;
759 error = gfs2_change_nlink(odip, -1);
760 if (error)
761 goto out_end_trans;
762
Steven Whitehouseffed8ab2007-06-07 11:29:35 +0100763 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 if (error)
765 goto out_end_trans;
766 } else {
767 struct buffer_head *dibh;
768 error = gfs2_meta_inode_buffer(ip, &dibh);
769 if (error)
770 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100771 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000772 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500773 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 brelse(dibh);
775 }
776
777 error = gfs2_dir_del(odip, &odentry->d_name);
778 if (error)
779 goto out_end_trans;
780
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100781 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 if (error)
783 goto out_end_trans;
784
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400785out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400787out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 if (alloc_required)
789 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400790out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 if (alloc_required)
792 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400793out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794 if (alloc_required)
795 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400796out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 gfs2_glock_dq_m(num_gh, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400798out_uninit:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 for (x = 0; x < num_gh; x++)
800 gfs2_holder_uninit(ghs + x);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400801out_gunlock_r:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802 if (dir_rename)
803 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400804out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805 return error;
806}
807
808/**
809 * gfs2_readlink - Read the value of a symlink
810 * @dentry: the symlink
811 * @buf: the buffer to read the symlink data into
812 * @size: the size of the buffer
813 *
814 * Returns: errno
815 */
816
817static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
818 int user_size)
819{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400820 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000821 char array[GFS2_FAST_NAME_SIZE], *buf = array;
822 unsigned int len = GFS2_FAST_NAME_SIZE;
823 int error;
824
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 error = gfs2_readlinki(ip, &buf, &len);
826 if (error)
827 return error;
828
829 if (user_size > len - 1)
830 user_size = len - 1;
831
832 if (copy_to_user(user_buf, buf, user_size))
833 error = -EFAULT;
834 else
835 error = user_size;
836
837 if (buf != array)
838 kfree(buf);
839
840 return error;
841}
842
843/**
844 * gfs2_follow_link - Follow a symbolic link
845 * @dentry: The dentry of the link
846 * @nd: Data that we pass to vfs_follow_link()
847 *
848 * This can handle symlinks of any size. It is optimised for symlinks
849 * under GFS2_FAST_NAME_SIZE.
850 *
851 * Returns: 0 on success or error code
852 */
853
854static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
855{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400856 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857 char array[GFS2_FAST_NAME_SIZE], *buf = array;
858 unsigned int len = GFS2_FAST_NAME_SIZE;
859 int error;
860
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 error = gfs2_readlinki(ip, &buf, &len);
862 if (!error) {
863 error = vfs_follow_link(nd, buf);
864 if (buf != array)
865 kfree(buf);
866 }
867
868 return ERR_PTR(error);
869}
870
871/**
872 * gfs2_permission -
873 * @inode:
874 * @mask:
875 * @nd: passed from Linux VFS, ignored by us
876 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500877 * This may be called from the VFS directly, or from within GFS2 with the
878 * inode locked, so we look to see if the glock is already locked and only
879 * lock the glock if its not already been done.
880 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881 * Returns: errno
882 */
883
884static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
885{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400886 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000887 struct gfs2_holder i_gh;
888 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500889 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000890
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500891 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
892 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
893 if (error)
894 return error;
895 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896 }
897
Steven Whitehouse77386e12006-11-29 10:41:49 -0500898 error = generic_permission(inode, mask, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500899 if (unlock)
900 gfs2_glock_dq_uninit(&i_gh);
901
David Teiglandb3b94fa2006-01-16 16:50:04 +0000902 return error;
903}
904
905static int setattr_size(struct inode *inode, struct iattr *attr)
906{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400907 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100908 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909 int error;
910
911 if (attr->ia_size != ip->i_di.di_size) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100912 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913 if (error)
914 return error;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100915 error = vmtruncate(inode, attr->ia_size);
916 gfs2_trans_end(sdp);
917 if (error)
918 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919 }
920
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000921 error = gfs2_truncatei(ip, attr->ia_size);
Wendy Cheng090ffaa2007-06-27 11:00:03 -0400922 if (error && (inode->i_size != ip->i_di.di_size))
923 i_size_write(inode, ip->i_di.di_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924
925 return error;
926}
927
928static int setattr_chown(struct inode *inode, struct iattr *attr)
929{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400930 struct gfs2_inode *ip = GFS2_I(inode);
931 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400933 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934 int error;
935
Steven Whitehouse2933f922006-11-01 13:23:29 -0500936 ouid = inode->i_uid;
937 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000938 nuid = attr->ia_uid;
939 ngid = attr->ia_gid;
940
941 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
942 ouid = nuid = NO_QUOTA_CHANGE;
943 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
944 ogid = ngid = NO_QUOTA_CHANGE;
945
946 gfs2_alloc_get(ip);
947
948 error = gfs2_quota_lock(ip, nuid, ngid);
949 if (error)
950 goto out_alloc;
951
952 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
953 error = gfs2_quota_check(ip, nuid, ngid);
954 if (error)
955 goto out_gunlock_q;
956 }
957
958 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
959 if (error)
960 goto out_gunlock_q;
961
962 error = gfs2_meta_inode_buffer(ip, &dibh);
963 if (error)
964 goto out_end_trans;
965
966 error = inode_setattr(inode, attr);
967 gfs2_assert_warn(sdp, !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000969 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500970 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971 brelse(dibh);
972
973 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400974 gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
975 gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976 }
977
Steven Whitehousea91ea692006-09-04 12:04:26 -0400978out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400980out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400982out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000984 return error;
985}
986
987/**
988 * gfs2_setattr - Change attributes on an inode
989 * @dentry: The dentry which is changing
990 * @attr: The structure describing the change
991 *
992 * The VFS layer wants to change one or more of an inodes attributes. Write
993 * that change out to disk.
994 *
995 * Returns: errno
996 */
997
998static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
999{
1000 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001001 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002 struct gfs2_holder i_gh;
1003 int error;
1004
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1006 if (error)
1007 return error;
1008
1009 error = -EPERM;
1010 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1011 goto out;
1012
1013 error = inode_change_ok(inode, attr);
1014 if (error)
1015 goto out;
1016
1017 if (attr->ia_valid & ATTR_SIZE)
1018 error = setattr_size(inode, attr);
1019 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1020 error = setattr_chown(inode, attr);
1021 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1022 error = gfs2_acl_chmod(ip, attr);
1023 else
1024 error = gfs2_setattr_simple(ip, attr);
1025
Steven Whitehousea91ea692006-09-04 12:04:26 -04001026out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028 if (!error)
1029 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001030 return error;
1031}
1032
1033/**
1034 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001035 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036 * @dentry: The dentry to stat
1037 * @stat: The inode's stats
1038 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001039 * This may be called from the VFS directly, or from within GFS2 with the
1040 * inode locked, so we look to see if the glock is already locked and only
1041 * lock the glock if its not already been done. Note that its the NFS
1042 * readdirplus operation which causes this to be called (from filldir)
1043 * with the glock already held.
1044 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045 * Returns: errno
1046 */
1047
1048static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1049 struct kstat *stat)
1050{
1051 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001052 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 struct gfs2_holder gh;
1054 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001055 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001057 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
1058 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1059 if (error)
1060 return error;
1061 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 }
1063
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001064 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001065 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001066 gfs2_glock_dq_uninit(&gh);
1067
1068 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069}
1070
1071static int gfs2_setxattr(struct dentry *dentry, const char *name,
1072 const void *data, size_t size, int flags)
1073{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001074 struct inode *inode = dentry->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075 struct gfs2_ea_request er;
1076
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077 memset(&er, 0, sizeof(struct gfs2_ea_request));
1078 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1079 if (er.er_type == GFS2_EATYPE_UNUSED)
1080 return -EOPNOTSUPP;
1081 er.er_data = (char *)data;
1082 er.er_name_len = strlen(er.er_name);
1083 er.er_data_len = size;
1084 er.er_flags = flags;
1085
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001086 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001087
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001088 return gfs2_ea_set(GFS2_I(inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001089}
1090
1091static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1092 void *data, size_t size)
1093{
1094 struct gfs2_ea_request er;
1095
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096 memset(&er, 0, sizeof(struct gfs2_ea_request));
1097 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1098 if (er.er_type == GFS2_EATYPE_UNUSED)
1099 return -EOPNOTSUPP;
1100 er.er_data = data;
1101 er.er_name_len = strlen(er.er_name);
1102 er.er_data_len = size;
1103
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001104 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001105}
1106
1107static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1108{
1109 struct gfs2_ea_request er;
1110
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111 memset(&er, 0, sizeof(struct gfs2_ea_request));
1112 er.er_data = (size) ? buffer : NULL;
1113 er.er_data_len = size;
1114
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001115 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001116}
1117
1118static int gfs2_removexattr(struct dentry *dentry, const char *name)
1119{
1120 struct gfs2_ea_request er;
1121
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122 memset(&er, 0, sizeof(struct gfs2_ea_request));
1123 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1124 if (er.er_type == GFS2_EATYPE_UNUSED)
1125 return -EOPNOTSUPP;
1126 er.er_name_len = strlen(er.er_name);
1127
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001128 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001129}
1130
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001131const struct inode_operations gfs2_file_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001132 .permission = gfs2_permission,
1133 .setattr = gfs2_setattr,
1134 .getattr = gfs2_getattr,
1135 .setxattr = gfs2_setxattr,
1136 .getxattr = gfs2_getxattr,
1137 .listxattr = gfs2_listxattr,
1138 .removexattr = gfs2_removexattr,
1139};
1140
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001141const struct inode_operations gfs2_dev_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142 .permission = gfs2_permission,
1143 .setattr = gfs2_setattr,
1144 .getattr = gfs2_getattr,
1145 .setxattr = gfs2_setxattr,
1146 .getxattr = gfs2_getxattr,
1147 .listxattr = gfs2_listxattr,
1148 .removexattr = gfs2_removexattr,
1149};
1150
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001151const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 .create = gfs2_create,
1153 .lookup = gfs2_lookup,
1154 .link = gfs2_link,
1155 .unlink = gfs2_unlink,
1156 .symlink = gfs2_symlink,
1157 .mkdir = gfs2_mkdir,
1158 .rmdir = gfs2_rmdir,
1159 .mknod = gfs2_mknod,
1160 .rename = gfs2_rename,
1161 .permission = gfs2_permission,
1162 .setattr = gfs2_setattr,
1163 .getattr = gfs2_getattr,
1164 .setxattr = gfs2_setxattr,
1165 .getxattr = gfs2_getxattr,
1166 .listxattr = gfs2_listxattr,
1167 .removexattr = gfs2_removexattr,
1168};
1169
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001170const struct inode_operations gfs2_symlink_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171 .readlink = gfs2_readlink,
1172 .follow_link = gfs2_follow_link,
1173 .permission = gfs2_permission,
1174 .setattr = gfs2_setattr,
1175 .getattr = gfs2_getattr,
1176 .setxattr = gfs2_setxattr,
1177 .getxattr = gfs2_getxattr,
1178 .listxattr = gfs2_listxattr,
1179 .removexattr = gfs2_removexattr,
1180};
1181