blob: 2686ad4c0029acd2ce1cdfdf46e3d29985a77143 [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);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +000064 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +000065 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))
David Howellse231c2e2008-02-07 00:15:26 -0800114 return ERR_CAST(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000116 if (inode) {
117 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
118 struct gfs2_holder gh;
119 int error;
120 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
121 if (error) {
122 iput(inode);
123 return ERR_PTR(error);
124 }
125 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126 return d_splice_alias(inode, dentry);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000127 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128 d_add(dentry, inode);
129
130 return NULL;
131}
132
133/**
134 * gfs2_link - Link to a file
135 * @old_dentry: The inode to link
136 * @dir: Add link to this directory
137 * @dentry: The name of the link
138 *
139 * Link the inode in "old_dentry" into the directory "dir" with the
140 * name in "dentry".
141 *
142 * Returns: errno
143 */
144
145static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
146 struct dentry *dentry)
147{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400148 struct gfs2_inode *dip = GFS2_I(dir);
149 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 struct inode *inode = old_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400151 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 struct gfs2_holder ghs[2];
153 int alloc_required;
154 int error;
155
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500156 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 return -EPERM;
158
159 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
160 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
161
162 error = gfs2_glock_nq_m(2, ghs);
163 if (error)
164 goto out;
165
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400166 error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000167 if (error)
168 goto out_gunlock;
169
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100170 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 switch (error) {
172 case -ENOENT:
173 break;
174 case 0:
175 error = -EEXIST;
176 default:
177 goto out_gunlock;
178 }
179
180 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500181 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182 goto out_gunlock;
183 error = -EFBIG;
Steven Whitehousecd915492006-09-04 12:49:07 -0400184 if (dip->i_di.di_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 goto out_gunlock;
186 error = -EPERM;
187 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
188 goto out_gunlock;
189 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500190 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191 goto out_gunlock;
192 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500193 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194 goto out_gunlock;
195
Steven Whitehousec7526662006-03-20 12:30:04 -0500196 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
197 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500199 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000200
201 if (alloc_required) {
202 struct gfs2_alloc *al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300203 if (!al) {
204 error = -ENOMEM;
205 goto out_gunlock;
206 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000207
Steven Whitehoused82661d2008-03-10 15:34:50 +0000208 error = gfs2_quota_lock_check(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 if (error)
210 goto out_alloc;
211
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 al->al_requested = sdp->sd_max_dirres;
213
214 error = gfs2_inplace_reserve(dip);
215 if (error)
216 goto out_gunlock_q;
217
Steven Whitehouse1b502592006-05-18 14:10:52 -0400218 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100219 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000220 2 * RES_DINODE + RES_STATFS +
221 RES_QUOTA, 0);
222 if (error)
223 goto out_ipres;
224 } else {
225 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
226 if (error)
227 goto out_ipres;
228 }
229
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100230 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231 if (error)
232 goto out_end_trans;
233
234 error = gfs2_change_nlink(ip, +1);
235
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400236out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000237 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400238out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239 if (alloc_required)
240 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400241out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 if (alloc_required)
243 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400244out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 if (alloc_required)
246 gfs2_alloc_put(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400247out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000248 gfs2_glock_dq_m(2, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400249out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 gfs2_holder_uninit(ghs);
251 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252 if (!error) {
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400253 atomic_inc(&inode->i_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254 d_instantiate(dentry, inode);
255 mark_inode_dirty(inode);
256 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257 return error;
258}
259
260/**
261 * gfs2_unlink - Unlink a file
262 * @dir: The inode of the directory containing the file to unlink
263 * @dentry: The file itself
264 *
265 * Unlink a file. Call gfs2_unlinki()
266 *
267 * Returns: errno
268 */
269
270static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
271{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400272 struct gfs2_inode *dip = GFS2_I(dir);
273 struct gfs2_sbd *sdp = GFS2_SB(dir);
274 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600275 struct gfs2_holder ghs[3];
276 struct gfs2_rgrpd *rgd;
277 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278 int error;
279
Russell Cattelanddee7602007-01-29 17:13:44 -0600280 error = gfs2_rindex_hold(sdp, &ri_gh);
281 if (error)
282 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283
Russell Cattelanddee7602007-01-29 17:13:44 -0600284 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
285 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
286
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100287 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600288 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
289
290
Steven Whitehouse8497a462007-08-26 14:23:56 +0100291 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100293 goto out_parent;
294
295 error = gfs2_glock_nq(ghs + 1); /* child */
296 if (error)
297 goto out_child;
298
299 error = gfs2_glock_nq(ghs + 2); /* rgrp */
300 if (error)
301 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000302
303 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
304 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100305 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000306
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400307 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100309 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400311 error = gfs2_dir_del(dip, &dentry->d_name);
312 if (error)
313 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400315 error = gfs2_change_nlink(ip, -1);
316
317out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318 gfs2_trans_end(sdp);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100319 gfs2_glock_dq(ghs + 2);
320out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600321 gfs2_holder_uninit(ghs + 2);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100322 gfs2_glock_dq(ghs + 1);
323out_child:
324 gfs2_holder_uninit(ghs + 1);
325 gfs2_glock_dq(ghs);
326out_parent:
327 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600328 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000329 return error;
330}
331
332/**
333 * gfs2_symlink - Create a symlink
334 * @dir: The directory to create the symlink in
335 * @dentry: The dentry to put the symlink in
336 * @symname: The thing which the link points to
337 *
338 * Returns: errno
339 */
340
341static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
342 const char *symname)
343{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400344 struct gfs2_inode *dip = GFS2_I(dir), *ip;
345 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 struct gfs2_holder ghs[2];
347 struct inode *inode;
348 struct buffer_head *dibh;
349 int size;
350 int error;
351
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 /* Must be stuffed with a null terminator for gfs2_follow_link() */
353 size = strlen(symname);
354 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
355 return -ENAMETOOLONG;
356
357 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
358
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500359 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000360 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000362 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000363 }
364
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500365 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366
367 ip->i_di.di_size = size;
368
369 error = gfs2_meta_inode_buffer(ip, &dibh);
370
371 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500372 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
374 size);
375 brelse(dibh);
376 }
377
378 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000379 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000380 gfs2_inplace_release(dip);
381 gfs2_quota_unlock(dip);
382 gfs2_alloc_put(dip);
383
384 gfs2_glock_dq_uninit_m(2, ghs);
385
David Teiglandb3b94fa2006-01-16 16:50:04 +0000386 d_instantiate(dentry, inode);
387 mark_inode_dirty(inode);
388
389 return 0;
390}
391
392/**
393 * gfs2_mkdir - Make a directory
394 * @dir: The parent directory of the new one
395 * @dentry: The dentry of the new directory
396 * @mode: The mode of the new directory
397 *
398 * Returns: errno
399 */
400
401static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
402{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400403 struct gfs2_inode *dip = GFS2_I(dir), *ip;
404 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405 struct gfs2_holder ghs[2];
406 struct inode *inode;
407 struct buffer_head *dibh;
408 int error;
409
David Teiglandb3b94fa2006-01-16 16:50:04 +0000410 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
411
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500412 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000413 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000415 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 }
417
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500418 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419
Steven Whitehouse4f561102006-11-01 14:04:17 -0500420 ip->i_inode.i_nlink = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
422 ip->i_di.di_flags |= GFS2_DIF_JDATA;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 ip->i_di.di_entries = 2;
424
425 error = gfs2_meta_inode_buffer(ip, &dibh);
426
427 if (!gfs2_assert_withdraw(sdp, !error)) {
428 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500429 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500430 struct qstr str;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500432 gfs2_str2qstr(&str, ".");
Steven Whitehousec7526662006-03-20 12:30:04 -0500433 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
434 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400436 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437 di->di_entries = cpu_to_be32(1);
438
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500439 gfs2_str2qstr(&str, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500440 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
441 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100443 gfs2_inum_out(dip, dent);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400444 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500446 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447
448 brelse(dibh);
449 }
450
451 error = gfs2_change_nlink(dip, +1);
452 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
453
454 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000455 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000456 gfs2_inplace_release(dip);
457 gfs2_quota_unlock(dip);
458 gfs2_alloc_put(dip);
459
460 gfs2_glock_dq_uninit_m(2, ghs);
461
David Teiglandb3b94fa2006-01-16 16:50:04 +0000462 d_instantiate(dentry, inode);
463 mark_inode_dirty(inode);
464
465 return 0;
466}
467
468/**
469 * gfs2_rmdir - Remove a directory
470 * @dir: The parent directory of the directory to be removed
471 * @dentry: The dentry of the directory to remove
472 *
473 * Remove a directory. Call gfs2_rmdiri()
474 *
475 * Returns: errno
476 */
477
478static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
479{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400480 struct gfs2_inode *dip = GFS2_I(dir);
481 struct gfs2_sbd *sdp = GFS2_SB(dir);
482 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600483 struct gfs2_holder ghs[3];
484 struct gfs2_rgrpd *rgd;
485 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000486 int error;
487
Russell Cattelanddee7602007-01-29 17:13:44 -0600488
489 error = gfs2_rindex_hold(sdp, &ri_gh);
490 if (error)
491 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
493 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
494
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100495 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600496 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
497
498 error = gfs2_glock_nq_m(3, ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 if (error)
500 goto out;
501
502 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
503 if (error)
504 goto out_gunlock;
505
506 if (ip->i_di.di_entries < 2) {
507 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500508 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000509 error = -EIO;
510 goto out_gunlock;
511 }
512 if (ip->i_di.di_entries > 2) {
513 error = -ENOTEMPTY;
514 goto out_gunlock;
515 }
516
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400517 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518 if (error)
519 goto out_gunlock;
520
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400521 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522
523 gfs2_trans_end(sdp);
524
Steven Whitehousea91ea692006-09-04 12:04:26 -0400525out_gunlock:
Russell Cattelanddee7602007-01-29 17:13:44 -0600526 gfs2_glock_dq_m(3, ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400527out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528 gfs2_holder_uninit(ghs);
529 gfs2_holder_uninit(ghs + 1);
Russell Cattelanddee7602007-01-29 17:13:44 -0600530 gfs2_holder_uninit(ghs + 2);
531 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532 return error;
533}
534
535/**
536 * gfs2_mknod - Make a special file
537 * @dir: The directory in which the special file will reside
538 * @dentry: The dentry of the special file
539 * @mode: The mode of the special file
540 * @rdev: The device specification of the special file
541 *
542 */
543
544static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
545 dev_t dev)
546{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500547 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400548 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549 struct gfs2_holder ghs[2];
550 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551
552 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
553
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500554 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000555 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000556 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000557 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000558 }
559
David Teiglandb3b94fa2006-01-16 16:50:04 +0000560 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000561 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 gfs2_inplace_release(dip);
563 gfs2_quota_unlock(dip);
564 gfs2_alloc_put(dip);
565
566 gfs2_glock_dq_uninit_m(2, ghs);
567
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 d_instantiate(dentry, inode);
569 mark_inode_dirty(inode);
570
571 return 0;
572}
573
574/**
575 * gfs2_rename - Rename a file
576 * @odir: Parent directory of old file name
577 * @odentry: The old dentry of the file
578 * @ndir: Parent directory of new file name
579 * @ndentry: The new dentry of the file
580 *
581 * Returns: errno
582 */
583
584static int gfs2_rename(struct inode *odir, struct dentry *odentry,
585 struct inode *ndir, struct dentry *ndentry)
586{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400587 struct gfs2_inode *odip = GFS2_I(odir);
588 struct gfs2_inode *ndip = GFS2_I(ndir);
589 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400591 struct gfs2_sbd *sdp = GFS2_SB(odir);
Russell Cattelanddee7602007-01-29 17:13:44 -0600592 struct gfs2_holder ghs[5], r_gh;
593 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000594 unsigned int num_gh;
595 int dir_rename = 0;
596 int alloc_required;
597 unsigned int x;
598 int error;
599
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400601 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000602 if (ip == nip)
603 return 0;
604 }
605
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606 /* Make sure we aren't trying to move a dirctory into it's subdir */
607
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500608 if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609 dir_rename = 1;
610
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500611 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 &r_gh);
613 if (error)
614 goto out;
615
616 error = gfs2_ok_to_move(ip, ndip);
617 if (error)
618 goto out_gunlock_r;
619 }
620
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400621 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400623 if (odip != ndip) {
624 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
625 num_gh++;
626 }
627 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
628 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400630 if (nip) {
631 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
632 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600633 /* grab the resource lock for unlink flag twiddling
634 * this is the case of the target file already existing
635 * so we unlink before doing the rename
636 */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100637 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600638 if (nrgd)
639 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400640 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641
642 error = gfs2_glock_nq_m(num_gh, ghs);
643 if (error)
644 goto out_uninit;
645
646 /* Check out the old directory */
647
648 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
649 if (error)
650 goto out_gunlock;
651
652 /* Check out the new directory */
653
654 if (nip) {
655 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
656 if (error)
657 goto out_gunlock;
658
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500659 if (S_ISDIR(nip->i_inode.i_mode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660 if (nip->i_di.di_entries < 2) {
661 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500662 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000663 error = -EIO;
664 goto out_gunlock;
665 }
666 if (nip->i_di.di_entries > 2) {
667 error = -ENOTEMPTY;
668 goto out_gunlock;
669 }
670 }
671 } else {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400672 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673 if (error)
674 goto out_gunlock;
675
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100676 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677 switch (error) {
678 case -ENOENT:
679 error = 0;
680 break;
681 case 0:
682 error = -EEXIST;
683 default:
684 goto out_gunlock;
685 };
686
687 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500688 if (!ndip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 error = -EINVAL;
690 goto out_gunlock;
691 }
Steven Whitehousecd915492006-09-04 12:49:07 -0400692 if (ndip->i_di.di_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693 error = -EFBIG;
694 goto out_gunlock;
695 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500696 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500697 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000698 error = -EMLINK;
699 goto out_gunlock;
700 }
701 }
702 }
703
704 /* Check out the dir to be renamed */
705
706 if (dir_rename) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400707 error = permission(odentry->d_inode, MAY_WRITE, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708 if (error)
709 goto out_gunlock;
710 }
711
Steven Whitehousec7526662006-03-20 12:30:04 -0500712 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
713 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500715 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716
717 if (alloc_required) {
718 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300719 if (!al) {
720 error = -ENOMEM;
721 goto out_gunlock;
722 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723
Steven Whitehoused82661d2008-03-10 15:34:50 +0000724 error = gfs2_quota_lock_check(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000725 if (error)
726 goto out_alloc;
727
David Teiglandb3b94fa2006-01-16 16:50:04 +0000728 al->al_requested = sdp->sd_max_dirres;
729
730 error = gfs2_inplace_reserve(ndip);
731 if (error)
732 goto out_gunlock_q;
733
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400734 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100735 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500737 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 if (error)
739 goto out_ipreserv;
740 } else {
741 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500742 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 if (error)
744 goto out_gunlock;
745 }
746
747 /* Remove the target file, if it exists */
748
749 if (nip) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500750 if (S_ISDIR(nip->i_inode.i_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400751 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
752 else {
753 error = gfs2_dir_del(ndip, &ndentry->d_name);
754 if (error)
755 goto out_end_trans;
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500756 error = gfs2_change_nlink(nip, -1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400757 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000758 if (error)
759 goto out_end_trans;
760 }
761
762 if (dir_rename) {
763 struct qstr name;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500764 gfs2_str2qstr(&name, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765
766 error = gfs2_change_nlink(ndip, +1);
767 if (error)
768 goto out_end_trans;
769 error = gfs2_change_nlink(odip, -1);
770 if (error)
771 goto out_end_trans;
772
Steven Whitehouseffed8ab2007-06-07 11:29:35 +0100773 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 if (error)
775 goto out_end_trans;
776 } else {
777 struct buffer_head *dibh;
778 error = gfs2_meta_inode_buffer(ip, &dibh);
779 if (error)
780 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100781 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000782 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500783 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000784 brelse(dibh);
785 }
786
787 error = gfs2_dir_del(odip, &odentry->d_name);
788 if (error)
789 goto out_end_trans;
790
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100791 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792 if (error)
793 goto out_end_trans;
794
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400795out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400797out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798 if (alloc_required)
799 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400800out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801 if (alloc_required)
802 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400803out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 if (alloc_required)
805 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400806out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807 gfs2_glock_dq_m(num_gh, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400808out_uninit:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 for (x = 0; x < num_gh; x++)
810 gfs2_holder_uninit(ghs + x);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400811out_gunlock_r:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 if (dir_rename)
813 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400814out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 return error;
816}
817
818/**
819 * gfs2_readlink - Read the value of a symlink
820 * @dentry: the symlink
821 * @buf: the buffer to read the symlink data into
822 * @size: the size of the buffer
823 *
824 * Returns: errno
825 */
826
827static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
828 int user_size)
829{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400830 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 char array[GFS2_FAST_NAME_SIZE], *buf = array;
832 unsigned int len = GFS2_FAST_NAME_SIZE;
833 int error;
834
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835 error = gfs2_readlinki(ip, &buf, &len);
836 if (error)
837 return error;
838
839 if (user_size > len - 1)
840 user_size = len - 1;
841
842 if (copy_to_user(user_buf, buf, user_size))
843 error = -EFAULT;
844 else
845 error = user_size;
846
847 if (buf != array)
848 kfree(buf);
849
850 return error;
851}
852
853/**
854 * gfs2_follow_link - Follow a symbolic link
855 * @dentry: The dentry of the link
856 * @nd: Data that we pass to vfs_follow_link()
857 *
858 * This can handle symlinks of any size. It is optimised for symlinks
859 * under GFS2_FAST_NAME_SIZE.
860 *
861 * Returns: 0 on success or error code
862 */
863
864static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
865{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400866 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 char array[GFS2_FAST_NAME_SIZE], *buf = array;
868 unsigned int len = GFS2_FAST_NAME_SIZE;
869 int error;
870
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 error = gfs2_readlinki(ip, &buf, &len);
872 if (!error) {
873 error = vfs_follow_link(nd, buf);
874 if (buf != array)
875 kfree(buf);
876 }
877
878 return ERR_PTR(error);
879}
880
881/**
882 * gfs2_permission -
883 * @inode:
884 * @mask:
885 * @nd: passed from Linux VFS, ignored by us
886 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500887 * This may be called from the VFS directly, or from within GFS2 with the
888 * inode locked, so we look to see if the glock is already locked and only
889 * lock the glock if its not already been done.
890 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891 * Returns: errno
892 */
893
894static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
895{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400896 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897 struct gfs2_holder i_gh;
898 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500899 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000901 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500902 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
903 if (error)
904 return error;
905 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906 }
907
Steven Whitehouse77386e12006-11-29 10:41:49 -0500908 error = generic_permission(inode, mask, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500909 if (unlock)
910 gfs2_glock_dq_uninit(&i_gh);
911
David Teiglandb3b94fa2006-01-16 16:50:04 +0000912 return error;
913}
914
915static int setattr_size(struct inode *inode, struct iattr *attr)
916{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400917 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100918 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919 int error;
920
921 if (attr->ia_size != ip->i_di.di_size) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100922 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923 if (error)
924 return error;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100925 error = vmtruncate(inode, attr->ia_size);
926 gfs2_trans_end(sdp);
927 if (error)
928 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000929 }
930
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000931 error = gfs2_truncatei(ip, attr->ia_size);
Wendy Cheng090ffaa2007-06-27 11:00:03 -0400932 if (error && (inode->i_size != ip->i_di.di_size))
933 i_size_write(inode, ip->i_di.di_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934
935 return error;
936}
937
938static int setattr_chown(struct inode *inode, struct iattr *attr)
939{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400940 struct gfs2_inode *ip = GFS2_I(inode);
941 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000942 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400943 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944 int error;
945
Steven Whitehouse2933f922006-11-01 13:23:29 -0500946 ouid = inode->i_uid;
947 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000948 nuid = attr->ia_uid;
949 ngid = attr->ia_gid;
950
951 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
952 ouid = nuid = NO_QUOTA_CHANGE;
953 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
954 ogid = ngid = NO_QUOTA_CHANGE;
955
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300956 if (!gfs2_alloc_get(ip))
957 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958
959 error = gfs2_quota_lock(ip, nuid, ngid);
960 if (error)
961 goto out_alloc;
962
963 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
964 error = gfs2_quota_check(ip, nuid, ngid);
965 if (error)
966 goto out_gunlock_q;
967 }
968
969 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
970 if (error)
971 goto out_gunlock_q;
972
973 error = gfs2_meta_inode_buffer(ip, &dibh);
974 if (error)
975 goto out_end_trans;
976
977 error = inode_setattr(inode, attr);
978 gfs2_assert_warn(sdp, !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000980 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500981 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000982 brelse(dibh);
983
984 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000985 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
986 gfs2_quota_change(ip, -blocks, ouid, ogid);
987 gfs2_quota_change(ip, blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 }
989
Steven Whitehousea91ea692006-09-04 12:04:26 -0400990out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000991 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400992out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400994out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000995 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 return error;
997}
998
999/**
1000 * gfs2_setattr - Change attributes on an inode
1001 * @dentry: The dentry which is changing
1002 * @attr: The structure describing the change
1003 *
1004 * The VFS layer wants to change one or more of an inodes attributes. Write
1005 * that change out to disk.
1006 *
1007 * Returns: errno
1008 */
1009
1010static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1011{
1012 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001013 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001014 struct gfs2_holder i_gh;
1015 int error;
1016
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1018 if (error)
1019 return error;
1020
1021 error = -EPERM;
1022 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1023 goto out;
1024
1025 error = inode_change_ok(inode, attr);
1026 if (error)
1027 goto out;
1028
1029 if (attr->ia_valid & ATTR_SIZE)
1030 error = setattr_size(inode, attr);
1031 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1032 error = setattr_chown(inode, attr);
1033 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1034 error = gfs2_acl_chmod(ip, attr);
1035 else
1036 error = gfs2_setattr_simple(ip, attr);
1037
Steven Whitehousea91ea692006-09-04 12:04:26 -04001038out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 if (!error)
1041 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042 return error;
1043}
1044
1045/**
1046 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001047 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001048 * @dentry: The dentry to stat
1049 * @stat: The inode's stats
1050 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001051 * This may be called from the VFS directly, or from within GFS2 with the
1052 * inode locked, so we look to see if the glock is already locked and only
1053 * lock the glock if its not already been done. Note that its the NFS
1054 * readdirplus operation which causes this to be called (from filldir)
1055 * with the glock already held.
1056 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001057 * Returns: errno
1058 */
1059
1060static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1061 struct kstat *stat)
1062{
1063 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001064 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001065 struct gfs2_holder gh;
1066 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001067 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001069 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001070 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1071 if (error)
1072 return error;
1073 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001074 }
1075
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001076 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001077 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001078 gfs2_glock_dq_uninit(&gh);
1079
1080 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001081}
1082
1083static int gfs2_setxattr(struct dentry *dentry, const char *name,
1084 const void *data, size_t size, int flags)
1085{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001086 struct inode *inode = dentry->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001087 struct gfs2_ea_request er;
1088
David Teiglandb3b94fa2006-01-16 16:50:04 +00001089 memset(&er, 0, sizeof(struct gfs2_ea_request));
1090 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1091 if (er.er_type == GFS2_EATYPE_UNUSED)
1092 return -EOPNOTSUPP;
1093 er.er_data = (char *)data;
1094 er.er_name_len = strlen(er.er_name);
1095 er.er_data_len = size;
1096 er.er_flags = flags;
1097
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001098 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001100 return gfs2_ea_set(GFS2_I(inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101}
1102
1103static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1104 void *data, size_t size)
1105{
1106 struct gfs2_ea_request er;
1107
David Teiglandb3b94fa2006-01-16 16:50:04 +00001108 memset(&er, 0, sizeof(struct gfs2_ea_request));
1109 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1110 if (er.er_type == GFS2_EATYPE_UNUSED)
1111 return -EOPNOTSUPP;
1112 er.er_data = data;
1113 er.er_name_len = strlen(er.er_name);
1114 er.er_data_len = size;
1115
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001116 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001117}
1118
1119static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1120{
1121 struct gfs2_ea_request er;
1122
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123 memset(&er, 0, sizeof(struct gfs2_ea_request));
1124 er.er_data = (size) ? buffer : NULL;
1125 er.er_data_len = size;
1126
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001127 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128}
1129
1130static int gfs2_removexattr(struct dentry *dentry, const char *name)
1131{
1132 struct gfs2_ea_request er;
1133
David Teiglandb3b94fa2006-01-16 16:50:04 +00001134 memset(&er, 0, sizeof(struct gfs2_ea_request));
1135 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1136 if (er.er_type == GFS2_EATYPE_UNUSED)
1137 return -EOPNOTSUPP;
1138 er.er_name_len = strlen(er.er_name);
1139
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001140 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141}
1142
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001143const struct inode_operations gfs2_file_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001144 .permission = gfs2_permission,
1145 .setattr = gfs2_setattr,
1146 .getattr = gfs2_getattr,
1147 .setxattr = gfs2_setxattr,
1148 .getxattr = gfs2_getxattr,
1149 .listxattr = gfs2_listxattr,
1150 .removexattr = gfs2_removexattr,
1151};
1152
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001153const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001154 .create = gfs2_create,
1155 .lookup = gfs2_lookup,
1156 .link = gfs2_link,
1157 .unlink = gfs2_unlink,
1158 .symlink = gfs2_symlink,
1159 .mkdir = gfs2_mkdir,
1160 .rmdir = gfs2_rmdir,
1161 .mknod = gfs2_mknod,
1162 .rename = gfs2_rename,
1163 .permission = gfs2_permission,
1164 .setattr = gfs2_setattr,
1165 .getattr = gfs2_getattr,
1166 .setxattr = gfs2_setxattr,
1167 .getxattr = gfs2_getxattr,
1168 .listxattr = gfs2_listxattr,
1169 .removexattr = gfs2_removexattr,
1170};
1171
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001172const struct inode_operations gfs2_symlink_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001173 .readlink = gfs2_readlink,
1174 .follow_link = gfs2_follow_link,
1175 .permission = gfs2_permission,
1176 .setattr = gfs2_setattr,
1177 .getattr = gfs2_getattr,
1178 .setxattr = gfs2_setxattr,
1179 .getxattr = gfs2_getxattr,
1180 .listxattr = gfs2_listxattr,
1181 .removexattr = gfs2_removexattr,
1182};
1183