blob: 34fe571e15ee2cf482a503b8ffe9fad2c0090642 [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
208 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
209 if (error)
210 goto out_alloc;
211
Steven Whitehouse2933f922006-11-01 13:23:29 -0500212 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213 if (error)
214 goto out_gunlock_q;
215
216 al->al_requested = sdp->sd_max_dirres;
217
218 error = gfs2_inplace_reserve(dip);
219 if (error)
220 goto out_gunlock_q;
221
Steven Whitehouse1b502592006-05-18 14:10:52 -0400222 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100223 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000224 2 * RES_DINODE + RES_STATFS +
225 RES_QUOTA, 0);
226 if (error)
227 goto out_ipres;
228 } else {
229 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
230 if (error)
231 goto out_ipres;
232 }
233
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100234 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235 if (error)
236 goto out_end_trans;
237
238 error = gfs2_change_nlink(ip, +1);
239
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400240out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400242out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243 if (alloc_required)
244 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400245out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246 if (alloc_required)
247 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400248out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249 if (alloc_required)
250 gfs2_alloc_put(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400251out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252 gfs2_glock_dq_m(2, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400253out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254 gfs2_holder_uninit(ghs);
255 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256 if (!error) {
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400257 atomic_inc(&inode->i_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000258 d_instantiate(dentry, inode);
259 mark_inode_dirty(inode);
260 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261 return error;
262}
263
264/**
265 * gfs2_unlink - Unlink a file
266 * @dir: The inode of the directory containing the file to unlink
267 * @dentry: The file itself
268 *
269 * Unlink a file. Call gfs2_unlinki()
270 *
271 * Returns: errno
272 */
273
274static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
275{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400276 struct gfs2_inode *dip = GFS2_I(dir);
277 struct gfs2_sbd *sdp = GFS2_SB(dir);
278 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600279 struct gfs2_holder ghs[3];
280 struct gfs2_rgrpd *rgd;
281 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282 int error;
283
Russell Cattelanddee7602007-01-29 17:13:44 -0600284 error = gfs2_rindex_hold(sdp, &ri_gh);
285 if (error)
286 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287
Russell Cattelanddee7602007-01-29 17:13:44 -0600288 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
289 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
290
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100291 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600292 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
293
294
Steven Whitehouse8497a462007-08-26 14:23:56 +0100295 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000296 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100297 goto out_parent;
298
299 error = gfs2_glock_nq(ghs + 1); /* child */
300 if (error)
301 goto out_child;
302
303 error = gfs2_glock_nq(ghs + 2); /* rgrp */
304 if (error)
305 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000306
307 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
308 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_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000312 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100313 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400315 error = gfs2_dir_del(dip, &dentry->d_name);
316 if (error)
317 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400319 error = gfs2_change_nlink(ip, -1);
320
321out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322 gfs2_trans_end(sdp);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100323 gfs2_glock_dq(ghs + 2);
324out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600325 gfs2_holder_uninit(ghs + 2);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100326 gfs2_glock_dq(ghs + 1);
327out_child:
328 gfs2_holder_uninit(ghs + 1);
329 gfs2_glock_dq(ghs);
330out_parent:
331 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600332 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333 return error;
334}
335
336/**
337 * gfs2_symlink - Create a symlink
338 * @dir: The directory to create the symlink in
339 * @dentry: The dentry to put the symlink in
340 * @symname: The thing which the link points to
341 *
342 * Returns: errno
343 */
344
345static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
346 const char *symname)
347{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400348 struct gfs2_inode *dip = GFS2_I(dir), *ip;
349 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 struct gfs2_holder ghs[2];
351 struct inode *inode;
352 struct buffer_head *dibh;
353 int size;
354 int error;
355
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 /* Must be stuffed with a null terminator for gfs2_follow_link() */
357 size = strlen(symname);
358 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
359 return -ENAMETOOLONG;
360
361 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
362
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500363 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000364 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000365 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000366 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367 }
368
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500369 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370
371 ip->i_di.di_size = size;
372
373 error = gfs2_meta_inode_buffer(ip, &dibh);
374
375 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500376 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000377 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
378 size);
379 brelse(dibh);
380 }
381
382 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000383 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384 gfs2_inplace_release(dip);
385 gfs2_quota_unlock(dip);
386 gfs2_alloc_put(dip);
387
388 gfs2_glock_dq_uninit_m(2, ghs);
389
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390 d_instantiate(dentry, inode);
391 mark_inode_dirty(inode);
392
393 return 0;
394}
395
396/**
397 * gfs2_mkdir - Make a directory
398 * @dir: The parent directory of the new one
399 * @dentry: The dentry of the new directory
400 * @mode: The mode of the new directory
401 *
402 * Returns: errno
403 */
404
405static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
406{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400407 struct gfs2_inode *dip = GFS2_I(dir), *ip;
408 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409 struct gfs2_holder ghs[2];
410 struct inode *inode;
411 struct buffer_head *dibh;
412 int error;
413
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
415
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500416 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000417 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000419 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 }
421
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500422 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423
Steven Whitehouse4f561102006-11-01 14:04:17 -0500424 ip->i_inode.i_nlink = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
426 ip->i_di.di_flags |= GFS2_DIF_JDATA;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427 ip->i_di.di_entries = 2;
428
429 error = gfs2_meta_inode_buffer(ip, &dibh);
430
431 if (!gfs2_assert_withdraw(sdp, !error)) {
432 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500433 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500434 struct qstr str;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500436 gfs2_str2qstr(&str, ".");
Steven Whitehousec7526662006-03-20 12:30:04 -0500437 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
438 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400440 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441 di->di_entries = cpu_to_be32(1);
442
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500443 gfs2_str2qstr(&str, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500444 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
445 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100447 gfs2_inum_out(dip, dent);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400448 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500450 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451
452 brelse(dibh);
453 }
454
455 error = gfs2_change_nlink(dip, +1);
456 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
457
458 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000459 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460 gfs2_inplace_release(dip);
461 gfs2_quota_unlock(dip);
462 gfs2_alloc_put(dip);
463
464 gfs2_glock_dq_uninit_m(2, ghs);
465
David Teiglandb3b94fa2006-01-16 16:50:04 +0000466 d_instantiate(dentry, inode);
467 mark_inode_dirty(inode);
468
469 return 0;
470}
471
472/**
473 * gfs2_rmdir - Remove a directory
474 * @dir: The parent directory of the directory to be removed
475 * @dentry: The dentry of the directory to remove
476 *
477 * Remove a directory. Call gfs2_rmdiri()
478 *
479 * Returns: errno
480 */
481
482static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
483{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400484 struct gfs2_inode *dip = GFS2_I(dir);
485 struct gfs2_sbd *sdp = GFS2_SB(dir);
486 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600487 struct gfs2_holder ghs[3];
488 struct gfs2_rgrpd *rgd;
489 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490 int error;
491
Russell Cattelanddee7602007-01-29 17:13:44 -0600492
493 error = gfs2_rindex_hold(sdp, &ri_gh);
494 if (error)
495 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000496 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
497 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
498
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100499 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600500 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
501
502 error = gfs2_glock_nq_m(3, ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000503 if (error)
504 goto out;
505
506 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
507 if (error)
508 goto out_gunlock;
509
510 if (ip->i_di.di_entries < 2) {
511 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500512 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 error = -EIO;
514 goto out_gunlock;
515 }
516 if (ip->i_di.di_entries > 2) {
517 error = -ENOTEMPTY;
518 goto out_gunlock;
519 }
520
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400521 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522 if (error)
523 goto out_gunlock;
524
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400525 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526
527 gfs2_trans_end(sdp);
528
Steven Whitehousea91ea692006-09-04 12:04:26 -0400529out_gunlock:
Russell Cattelanddee7602007-01-29 17:13:44 -0600530 gfs2_glock_dq_m(3, ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400531out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532 gfs2_holder_uninit(ghs);
533 gfs2_holder_uninit(ghs + 1);
Russell Cattelanddee7602007-01-29 17:13:44 -0600534 gfs2_holder_uninit(ghs + 2);
535 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 return error;
537}
538
539/**
540 * gfs2_mknod - Make a special file
541 * @dir: The directory in which the special file will reside
542 * @dentry: The dentry of the special file
543 * @mode: The mode of the special file
544 * @rdev: The device specification of the special file
545 *
546 */
547
548static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
549 dev_t dev)
550{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500551 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400552 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553 struct gfs2_holder ghs[2];
554 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555
556 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
557
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500558 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000559 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000560 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000561 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 }
563
David Teiglandb3b94fa2006-01-16 16:50:04 +0000564 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000565 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 gfs2_inplace_release(dip);
567 gfs2_quota_unlock(dip);
568 gfs2_alloc_put(dip);
569
570 gfs2_glock_dq_uninit_m(2, ghs);
571
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572 d_instantiate(dentry, inode);
573 mark_inode_dirty(inode);
574
575 return 0;
576}
577
578/**
579 * gfs2_rename - Rename a file
580 * @odir: Parent directory of old file name
581 * @odentry: The old dentry of the file
582 * @ndir: Parent directory of new file name
583 * @ndentry: The new dentry of the file
584 *
585 * Returns: errno
586 */
587
588static int gfs2_rename(struct inode *odir, struct dentry *odentry,
589 struct inode *ndir, struct dentry *ndentry)
590{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400591 struct gfs2_inode *odip = GFS2_I(odir);
592 struct gfs2_inode *ndip = GFS2_I(ndir);
593 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000594 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400595 struct gfs2_sbd *sdp = GFS2_SB(odir);
Russell Cattelanddee7602007-01-29 17:13:44 -0600596 struct gfs2_holder ghs[5], r_gh;
597 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598 unsigned int num_gh;
599 int dir_rename = 0;
600 int alloc_required;
601 unsigned int x;
602 int error;
603
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400605 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606 if (ip == nip)
607 return 0;
608 }
609
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 /* Make sure we aren't trying to move a dirctory into it's subdir */
611
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500612 if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613 dir_rename = 1;
614
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500615 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616 &r_gh);
617 if (error)
618 goto out;
619
620 error = gfs2_ok_to_move(ip, ndip);
621 if (error)
622 goto out_gunlock_r;
623 }
624
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400625 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000626 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400627 if (odip != ndip) {
628 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
629 num_gh++;
630 }
631 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
632 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000633
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400634 if (nip) {
635 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
636 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600637 /* grab the resource lock for unlink flag twiddling
638 * this is the case of the target file already existing
639 * so we unlink before doing the rename
640 */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100641 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600642 if (nrgd)
643 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400644 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000645
646 error = gfs2_glock_nq_m(num_gh, ghs);
647 if (error)
648 goto out_uninit;
649
650 /* Check out the old directory */
651
652 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
653 if (error)
654 goto out_gunlock;
655
656 /* Check out the new directory */
657
658 if (nip) {
659 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
660 if (error)
661 goto out_gunlock;
662
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500663 if (S_ISDIR(nip->i_inode.i_mode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664 if (nip->i_di.di_entries < 2) {
665 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500666 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 error = -EIO;
668 goto out_gunlock;
669 }
670 if (nip->i_di.di_entries > 2) {
671 error = -ENOTEMPTY;
672 goto out_gunlock;
673 }
674 }
675 } else {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400676 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677 if (error)
678 goto out_gunlock;
679
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100680 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681 switch (error) {
682 case -ENOENT:
683 error = 0;
684 break;
685 case 0:
686 error = -EEXIST;
687 default:
688 goto out_gunlock;
689 };
690
691 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500692 if (!ndip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693 error = -EINVAL;
694 goto out_gunlock;
695 }
Steven Whitehousecd915492006-09-04 12:49:07 -0400696 if (ndip->i_di.di_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697 error = -EFBIG;
698 goto out_gunlock;
699 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500700 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500701 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000702 error = -EMLINK;
703 goto out_gunlock;
704 }
705 }
706 }
707
708 /* Check out the dir to be renamed */
709
710 if (dir_rename) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400711 error = permission(odentry->d_inode, MAY_WRITE, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 if (error)
713 goto out_gunlock;
714 }
715
Steven Whitehousec7526662006-03-20 12:30:04 -0500716 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
717 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500719 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720
721 if (alloc_required) {
722 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300723 if (!al) {
724 error = -ENOMEM;
725 goto out_gunlock;
726 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727
728 error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
729 if (error)
730 goto out_alloc;
731
Steven Whitehouse2933f922006-11-01 13:23:29 -0500732 error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733 if (error)
734 goto out_gunlock_q;
735
736 al->al_requested = sdp->sd_max_dirres;
737
738 error = gfs2_inplace_reserve(ndip);
739 if (error)
740 goto out_gunlock_q;
741
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400742 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100743 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500745 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746 if (error)
747 goto out_ipreserv;
748 } else {
749 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500750 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751 if (error)
752 goto out_gunlock;
753 }
754
755 /* Remove the target file, if it exists */
756
757 if (nip) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500758 if (S_ISDIR(nip->i_inode.i_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400759 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
760 else {
761 error = gfs2_dir_del(ndip, &ndentry->d_name);
762 if (error)
763 goto out_end_trans;
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500764 error = gfs2_change_nlink(nip, -1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400765 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 if (error)
767 goto out_end_trans;
768 }
769
770 if (dir_rename) {
771 struct qstr name;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500772 gfs2_str2qstr(&name, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773
774 error = gfs2_change_nlink(ndip, +1);
775 if (error)
776 goto out_end_trans;
777 error = gfs2_change_nlink(odip, -1);
778 if (error)
779 goto out_end_trans;
780
Steven Whitehouseffed8ab2007-06-07 11:29:35 +0100781 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 if (error)
783 goto out_end_trans;
784 } else {
785 struct buffer_head *dibh;
786 error = gfs2_meta_inode_buffer(ip, &dibh);
787 if (error)
788 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100789 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000790 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500791 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792 brelse(dibh);
793 }
794
795 error = gfs2_dir_del(odip, &odentry->d_name);
796 if (error)
797 goto out_end_trans;
798
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100799 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800 if (error)
801 goto out_end_trans;
802
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400803out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400805out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806 if (alloc_required)
807 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400808out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 if (alloc_required)
810 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400811out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 if (alloc_required)
813 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400814out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 gfs2_glock_dq_m(num_gh, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400816out_uninit:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817 for (x = 0; x < num_gh; x++)
818 gfs2_holder_uninit(ghs + x);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400819out_gunlock_r:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820 if (dir_rename)
821 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400822out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000823 return error;
824}
825
826/**
827 * gfs2_readlink - Read the value of a symlink
828 * @dentry: the symlink
829 * @buf: the buffer to read the symlink data into
830 * @size: the size of the buffer
831 *
832 * Returns: errno
833 */
834
835static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
836 int user_size)
837{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400838 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000839 char array[GFS2_FAST_NAME_SIZE], *buf = array;
840 unsigned int len = GFS2_FAST_NAME_SIZE;
841 int error;
842
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 error = gfs2_readlinki(ip, &buf, &len);
844 if (error)
845 return error;
846
847 if (user_size > len - 1)
848 user_size = len - 1;
849
850 if (copy_to_user(user_buf, buf, user_size))
851 error = -EFAULT;
852 else
853 error = user_size;
854
855 if (buf != array)
856 kfree(buf);
857
858 return error;
859}
860
861/**
862 * gfs2_follow_link - Follow a symbolic link
863 * @dentry: The dentry of the link
864 * @nd: Data that we pass to vfs_follow_link()
865 *
866 * This can handle symlinks of any size. It is optimised for symlinks
867 * under GFS2_FAST_NAME_SIZE.
868 *
869 * Returns: 0 on success or error code
870 */
871
872static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
873{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400874 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 char array[GFS2_FAST_NAME_SIZE], *buf = array;
876 unsigned int len = GFS2_FAST_NAME_SIZE;
877 int error;
878
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879 error = gfs2_readlinki(ip, &buf, &len);
880 if (!error) {
881 error = vfs_follow_link(nd, buf);
882 if (buf != array)
883 kfree(buf);
884 }
885
886 return ERR_PTR(error);
887}
888
889/**
890 * gfs2_permission -
891 * @inode:
892 * @mask:
893 * @nd: passed from Linux VFS, ignored by us
894 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500895 * This may be called from the VFS directly, or from within GFS2 with the
896 * inode locked, so we look to see if the glock is already locked and only
897 * lock the glock if its not already been done.
898 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899 * Returns: errno
900 */
901
902static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
903{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400904 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000905 struct gfs2_holder i_gh;
906 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500907 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000909 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500910 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
911 if (error)
912 return error;
913 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914 }
915
Steven Whitehouse77386e12006-11-29 10:41:49 -0500916 error = generic_permission(inode, mask, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500917 if (unlock)
918 gfs2_glock_dq_uninit(&i_gh);
919
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 return error;
921}
922
923static int setattr_size(struct inode *inode, struct iattr *attr)
924{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400925 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100926 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927 int error;
928
929 if (attr->ia_size != ip->i_di.di_size) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100930 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 if (error)
932 return error;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100933 error = vmtruncate(inode, attr->ia_size);
934 gfs2_trans_end(sdp);
935 if (error)
936 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937 }
938
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000939 error = gfs2_truncatei(ip, attr->ia_size);
Wendy Cheng090ffaa2007-06-27 11:00:03 -0400940 if (error && (inode->i_size != ip->i_di.di_size))
941 i_size_write(inode, ip->i_di.di_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000942
943 return error;
944}
945
946static int setattr_chown(struct inode *inode, struct iattr *attr)
947{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400948 struct gfs2_inode *ip = GFS2_I(inode);
949 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400951 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952 int error;
953
Steven Whitehouse2933f922006-11-01 13:23:29 -0500954 ouid = inode->i_uid;
955 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956 nuid = attr->ia_uid;
957 ngid = attr->ia_gid;
958
959 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
960 ouid = nuid = NO_QUOTA_CHANGE;
961 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
962 ogid = ngid = NO_QUOTA_CHANGE;
963
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300964 if (!gfs2_alloc_get(ip))
965 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966
967 error = gfs2_quota_lock(ip, nuid, ngid);
968 if (error)
969 goto out_alloc;
970
971 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
972 error = gfs2_quota_check(ip, nuid, ngid);
973 if (error)
974 goto out_gunlock_q;
975 }
976
977 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
978 if (error)
979 goto out_gunlock_q;
980
981 error = gfs2_meta_inode_buffer(ip, &dibh);
982 if (error)
983 goto out_end_trans;
984
985 error = inode_setattr(inode, attr);
986 gfs2_assert_warn(sdp, !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000988 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500989 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990 brelse(dibh);
991
992 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000993 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
994 gfs2_quota_change(ip, -blocks, ouid, ogid);
995 gfs2_quota_change(ip, blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 }
997
Steven Whitehousea91ea692006-09-04 12:04:26 -0400998out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001000out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001002out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 return error;
1005}
1006
1007/**
1008 * gfs2_setattr - Change attributes on an inode
1009 * @dentry: The dentry which is changing
1010 * @attr: The structure describing the change
1011 *
1012 * The VFS layer wants to change one or more of an inodes attributes. Write
1013 * that change out to disk.
1014 *
1015 * Returns: errno
1016 */
1017
1018static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1019{
1020 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001021 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022 struct gfs2_holder i_gh;
1023 int error;
1024
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1026 if (error)
1027 return error;
1028
1029 error = -EPERM;
1030 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1031 goto out;
1032
1033 error = inode_change_ok(inode, attr);
1034 if (error)
1035 goto out;
1036
1037 if (attr->ia_valid & ATTR_SIZE)
1038 error = setattr_size(inode, attr);
1039 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1040 error = setattr_chown(inode, attr);
1041 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1042 error = gfs2_acl_chmod(ip, attr);
1043 else
1044 error = gfs2_setattr_simple(ip, attr);
1045
Steven Whitehousea91ea692006-09-04 12:04:26 -04001046out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001047 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001048 if (!error)
1049 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001050 return error;
1051}
1052
1053/**
1054 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001055 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056 * @dentry: The dentry to stat
1057 * @stat: The inode's stats
1058 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001059 * This may be called from the VFS directly, or from within GFS2 with the
1060 * inode locked, so we look to see if the glock is already locked and only
1061 * lock the glock if its not already been done. Note that its the NFS
1062 * readdirplus operation which causes this to be called (from filldir)
1063 * with the glock already held.
1064 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001065 * Returns: errno
1066 */
1067
1068static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1069 struct kstat *stat)
1070{
1071 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001072 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001073 struct gfs2_holder gh;
1074 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001075 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001076
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001077 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001078 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1079 if (error)
1080 return error;
1081 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082 }
1083
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001084 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001085 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001086 gfs2_glock_dq_uninit(&gh);
1087
1088 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001089}
1090
1091static int gfs2_setxattr(struct dentry *dentry, const char *name,
1092 const void *data, size_t size, int flags)
1093{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001094 struct inode *inode = dentry->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001095 struct gfs2_ea_request er;
1096
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 memset(&er, 0, sizeof(struct gfs2_ea_request));
1098 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1099 if (er.er_type == GFS2_EATYPE_UNUSED)
1100 return -EOPNOTSUPP;
1101 er.er_data = (char *)data;
1102 er.er_name_len = strlen(er.er_name);
1103 er.er_data_len = size;
1104 er.er_flags = flags;
1105
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001106 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001107
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001108 return gfs2_ea_set(GFS2_I(inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001109}
1110
1111static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1112 void *data, size_t size)
1113{
1114 struct gfs2_ea_request er;
1115
David Teiglandb3b94fa2006-01-16 16:50:04 +00001116 memset(&er, 0, sizeof(struct gfs2_ea_request));
1117 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1118 if (er.er_type == GFS2_EATYPE_UNUSED)
1119 return -EOPNOTSUPP;
1120 er.er_data = data;
1121 er.er_name_len = strlen(er.er_name);
1122 er.er_data_len = size;
1123
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001124 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125}
1126
1127static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1128{
1129 struct gfs2_ea_request er;
1130
David Teiglandb3b94fa2006-01-16 16:50:04 +00001131 memset(&er, 0, sizeof(struct gfs2_ea_request));
1132 er.er_data = (size) ? buffer : NULL;
1133 er.er_data_len = size;
1134
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001135 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136}
1137
1138static int gfs2_removexattr(struct dentry *dentry, const char *name)
1139{
1140 struct gfs2_ea_request er;
1141
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142 memset(&er, 0, sizeof(struct gfs2_ea_request));
1143 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1144 if (er.er_type == GFS2_EATYPE_UNUSED)
1145 return -EOPNOTSUPP;
1146 er.er_name_len = strlen(er.er_name);
1147
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001148 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001149}
1150
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001151const struct inode_operations gfs2_file_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 .permission = gfs2_permission,
1153 .setattr = gfs2_setattr,
1154 .getattr = gfs2_getattr,
1155 .setxattr = gfs2_setxattr,
1156 .getxattr = gfs2_getxattr,
1157 .listxattr = gfs2_listxattr,
1158 .removexattr = gfs2_removexattr,
1159};
1160
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001161const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001162 .create = gfs2_create,
1163 .lookup = gfs2_lookup,
1164 .link = gfs2_link,
1165 .unlink = gfs2_unlink,
1166 .symlink = gfs2_symlink,
1167 .mkdir = gfs2_mkdir,
1168 .rmdir = gfs2_rmdir,
1169 .mknod = gfs2_mknod,
1170 .rename = gfs2_rename,
1171 .permission = gfs2_permission,
1172 .setattr = gfs2_setattr,
1173 .getattr = gfs2_getattr,
1174 .setxattr = gfs2_setxattr,
1175 .getxattr = gfs2_getxattr,
1176 .listxattr = gfs2_listxattr,
1177 .removexattr = gfs2_removexattr,
1178};
1179
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001180const struct inode_operations gfs2_symlink_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001181 .readlink = gfs2_readlink,
1182 .follow_link = gfs2_follow_link,
1183 .permission = gfs2_permission,
1184 .setattr = gfs2_setattr,
1185 .getattr = gfs2_getattr,
1186 .setxattr = gfs2_setxattr,
1187 .getxattr = gfs2_getxattr,
1188 .listxattr = gfs2_listxattr,
1189 .removexattr = gfs2_removexattr,
1190};
1191