blob: 6b8c2bdb5a0c1c7ee0c805515adbabdce401737b [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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000015#include <linux/mm.h>
16#include <linux/xattr.h>
17#include <linux/posix_acl.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050018#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050019#include <linux/crc32.h>
Steven Whitehousee9079cc2008-10-14 14:43:29 +010020#include <linux/fiemap.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include <asm/uaccess.h>
22
23#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050024#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "acl.h"
26#include "bmap.h"
27#include "dir.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010028#include "xattr.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029#include "glock.h"
30#include "inode.h"
31#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032#include "quota.h"
33#include "rgrp.h"
34#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050035#include "util.h"
Steven Whitehouseb2760582008-10-14 16:05:55 +010036#include "super.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000037
38/**
39 * gfs2_create - Create a file
40 * @dir: The directory in which to create the file
41 * @dentry: The dentry of the new file
42 * @mode: The mode of the new file
43 *
44 * Returns: errno
45 */
46
47static int gfs2_create(struct inode *dir, struct dentry *dentry,
48 int mode, struct nameidata *nd)
49{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040050 struct gfs2_inode *dip = GFS2_I(dir);
51 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +000052 struct gfs2_holder ghs[2];
53 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +000054
David Teiglandb3b94fa2006-01-16 16:50:04 +000055 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
56
57 for (;;) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -050058 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +000059 if (!IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000060 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +000061 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +000062 gfs2_inplace_release(dip);
63 gfs2_quota_unlock(dip);
64 gfs2_alloc_put(dip);
65 gfs2_glock_dq_uninit_m(2, ghs);
Steven Whitehouse3a8476d2006-06-19 09:10:39 -040066 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000067 break;
Steven Whitehouse7359a192006-02-13 12:27:43 +000068 } else if (PTR_ERR(inode) != -EEXIST ||
Al Viro35165862008-08-05 03:00:49 -040069 (nd && nd->flags & LOOKUP_EXCL)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000070 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +000071 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000072 }
73
Al Viroa569c712008-07-23 14:42:05 -040074 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Steven Whitehousec7526662006-03-20 12:30:04 -050075 if (inode) {
76 if (!IS_ERR(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -050077 gfs2_holder_uninit(ghs);
78 break;
79 } else {
80 gfs2_holder_uninit(ghs);
81 return PTR_ERR(inode);
82 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000083 }
84 }
85
David Teiglandb3b94fa2006-01-16 16:50:04 +000086 d_instantiate(dentry, inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000087
88 return 0;
89}
90
91/**
92 * gfs2_lookup - Look up a filename in a directory and return its inode
93 * @dir: The directory inode
94 * @dentry: The dentry of the new inode
95 * @nd: passed from Linux VFS, ignored by us
96 *
97 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
98 *
99 * Returns: errno
100 */
101
102static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
103 struct nameidata *nd)
104{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000105 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106
Al Viroa569c712008-07-23 14:42:05 -0400107 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Steven Whitehousec7526662006-03-20 12:30:04 -0500108 if (inode && IS_ERR(inode))
David Howellse231c2e2008-02-07 00:15:26 -0800109 return ERR_CAST(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000111 if (inode) {
112 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
113 struct gfs2_holder gh;
114 int error;
115 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
116 if (error) {
117 iput(inode);
118 return ERR_PTR(error);
119 }
120 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121 return d_splice_alias(inode, dentry);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000122 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123 d_add(dentry, inode);
124
125 return NULL;
126}
127
128/**
129 * gfs2_link - Link to a file
130 * @old_dentry: The inode to link
131 * @dir: Add link to this directory
132 * @dentry: The name of the link
133 *
134 * Link the inode in "old_dentry" into the directory "dir" with the
135 * name in "dentry".
136 *
137 * Returns: errno
138 */
139
140static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
141 struct dentry *dentry)
142{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400143 struct gfs2_inode *dip = GFS2_I(dir);
144 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 struct inode *inode = old_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400146 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 struct gfs2_holder ghs[2];
Steven Whitehouse2baee032011-05-09 12:08:36 +0100148 struct buffer_head *dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000149 int alloc_required;
150 int error;
151
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500152 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153 return -EPERM;
154
155 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
156 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
157
Bob Peterson72dbf472008-08-12 13:39:29 -0500158 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000159 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500160 goto out_parent;
161
162 error = gfs2_glock_nq(ghs + 1); /* child */
163 if (error)
164 goto out_child;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000165
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100166 error = -ENOENT;
167 if (inode->i_nlink == 0)
168 goto out_gunlock;
169
Nick Pigginb74c79e2011-01-07 17:49:58 +1100170 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 if (error)
172 goto out_gunlock;
173
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100174 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 switch (error) {
176 case -ENOENT:
177 break;
178 case 0:
179 error = -EEXIST;
180 default:
181 goto out_gunlock;
182 }
183
184 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500185 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000186 goto out_gunlock;
187 error = -EFBIG;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000188 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189 goto out_gunlock;
190 error = -EPERM;
191 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
192 goto out_gunlock;
193 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500194 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195 goto out_gunlock;
196 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500197 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198 goto out_gunlock;
199
Steven Whitehousec7526662006-03-20 12:30:04 -0500200 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
201 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500203 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204
205 if (alloc_required) {
206 struct gfs2_alloc *al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300207 if (!al) {
208 error = -ENOMEM;
209 goto out_gunlock;
210 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211
Steven Whitehoused82661d2008-03-10 15:34:50 +0000212 error = gfs2_quota_lock_check(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213 if (error)
214 goto out_alloc;
215
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 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 +
Benjamin Marzinskibf97b672010-09-27 16:00:04 -0500223 gfs2_rg_blocks(al) +
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 Whitehouse2baee032011-05-09 12:08:36 +0100234 error = gfs2_meta_inode_buffer(ip, &dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235 if (error)
236 goto out_end_trans;
237
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +0100238 error = gfs2_dir_add(dir, &dentry->d_name, ip);
Steven Whitehouse2baee032011-05-09 12:08:36 +0100239 if (error)
240 goto out_brelse;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241
Steven Whitehouse2baee032011-05-09 12:08:36 +0100242 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
243 inc_nlink(&ip->i_inode);
244 ip->i_inode.i_ctime = CURRENT_TIME;
245 gfs2_dinode_out(ip, dibh->b_data);
246 mark_inode_dirty(&ip->i_inode);
247
248out_brelse:
249 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400250out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400252out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253 if (alloc_required)
254 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400255out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256 if (alloc_required)
257 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400258out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259 if (alloc_required)
260 gfs2_alloc_put(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400261out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500262 gfs2_glock_dq(ghs + 1);
263out_child:
264 gfs2_glock_dq(ghs);
265out_parent:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000266 gfs2_holder_uninit(ghs);
267 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268 if (!error) {
Al Viro7de9c6ee2010-10-23 11:11:40 -0400269 ihold(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270 d_instantiate(dentry, inode);
271 mark_inode_dirty(inode);
272 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000273 return error;
274}
275
Steven Whitehouse87ec2172009-05-22 10:54:50 +0100276/*
277 * gfs2_unlink_ok - check to see that a inode is still in a directory
278 * @dip: the directory
279 * @name: the name of the file
280 * @ip: the inode
281 *
282 * Assumes that the lock on (at least) @dip is held.
283 *
284 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
285 */
286
287static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
288 const struct gfs2_inode *ip)
289{
290 int error;
291
292 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
293 return -EPERM;
294
295 if ((dip->i_inode.i_mode & S_ISVTX) &&
296 dip->i_inode.i_uid != current_fsuid() &&
297 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
298 return -EPERM;
299
300 if (IS_APPEND(&dip->i_inode))
301 return -EPERM;
302
Nick Pigginb74c79e2011-01-07 17:49:58 +1100303 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
Steven Whitehouse87ec2172009-05-22 10:54:50 +0100304 if (error)
305 return error;
306
307 error = gfs2_dir_check(&dip->i_inode, name, ip);
308 if (error)
309 return error;
310
311 return 0;
312}
313
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314/**
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100315 * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
316 * @dip: The parent directory
317 * @name: The name of the entry in the parent directory
318 * @bh: The inode buffer for the inode to be removed
319 * @inode: The inode to be removed
320 *
321 * Called with all the locks and in a transaction. This will only be
322 * called for a directory after it has been checked to ensure it is empty.
323 *
324 * Returns: 0 on success, or an error
325 */
326
327static int gfs2_unlink_inode(struct gfs2_inode *dip,
328 const struct dentry *dentry,
329 struct buffer_head *bh)
330{
331 struct inode *inode = dentry->d_inode;
332 struct gfs2_inode *ip = GFS2_I(inode);
333 int error;
334
335 error = gfs2_dir_del(dip, dentry);
336 if (error)
337 return error;
338
339 ip->i_entries = 0;
340 inode->i_ctime = CURRENT_TIME;
341 if (S_ISDIR(inode->i_mode))
342 clear_nlink(inode);
343 else
344 drop_nlink(inode);
345 gfs2_trans_add_bh(ip->i_gl, bh, 1);
346 gfs2_dinode_out(ip, bh->b_data);
347 mark_inode_dirty(inode);
348 if (inode->i_nlink == 0)
349 gfs2_unlink_di(inode);
350 return 0;
351}
352
353
354/**
355 * gfs2_unlink - Unlink an inode (this does rmdir as well)
356 * @dir: The inode of the directory containing the inode to unlink
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357 * @dentry: The file itself
358 *
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100359 * This routine uses the type of the inode as a flag to figure out
360 * whether this is an unlink or an rmdir.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361 *
362 * Returns: errno
363 */
364
365static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
366{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400367 struct gfs2_inode *dip = GFS2_I(dir);
368 struct gfs2_sbd *sdp = GFS2_SB(dir);
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100369 struct inode *inode = dentry->d_inode;
370 struct gfs2_inode *ip = GFS2_I(inode);
371 struct buffer_head *bh;
Russell Cattelanddee7602007-01-29 17:13:44 -0600372 struct gfs2_holder ghs[3];
373 struct gfs2_rgrpd *rgd;
374 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000375 int error;
376
Russell Cattelanddee7602007-01-29 17:13:44 -0600377 error = gfs2_rindex_hold(sdp, &ri_gh);
378 if (error)
379 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000380
Russell Cattelanddee7602007-01-29 17:13:44 -0600381 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
382 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
383
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100384 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600385 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
386
387
Steven Whitehouse8497a462007-08-26 14:23:56 +0100388 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100390 goto out_parent;
391
392 error = gfs2_glock_nq(ghs + 1); /* child */
393 if (error)
394 goto out_child;
395
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100396 error = -ENOENT;
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100397 if (inode->i_nlink == 0)
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100398 goto out_rgrp;
399
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100400 if (S_ISDIR(inode->i_mode)) {
401 error = -ENOTEMPTY;
402 if (ip->i_entries > 2 || inode->i_nlink > 2)
403 goto out_rgrp;
404 }
405
Steven Whitehouse8497a462007-08-26 14:23:56 +0100406 error = gfs2_glock_nq(ghs + 2); /* rgrp */
407 if (error)
408 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409
410 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
411 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500412 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000413
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100414 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415 if (error)
Roel Kluincd012072009-08-22 19:26:42 +0200416 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100418 error = gfs2_meta_inode_buffer(ip, &bh);
419 if (error)
420 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100422 error = gfs2_unlink_inode(dip, dentry, bh);
423 brelse(bh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400424
425out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000426 gfs2_trans_end(sdp);
Bob Peterson72dbf472008-08-12 13:39:29 -0500427out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +0100428 gfs2_glock_dq(ghs + 2);
429out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600430 gfs2_holder_uninit(ghs + 2);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100431 gfs2_glock_dq(ghs + 1);
432out_child:
433 gfs2_holder_uninit(ghs + 1);
434 gfs2_glock_dq(ghs);
435out_parent:
436 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600437 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438 return error;
439}
440
441/**
442 * gfs2_symlink - Create a symlink
443 * @dir: The directory to create the symlink in
444 * @dentry: The dentry to put the symlink in
445 * @symname: The thing which the link points to
446 *
447 * Returns: errno
448 */
449
450static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
451 const char *symname)
452{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400453 struct gfs2_inode *dip = GFS2_I(dir), *ip;
454 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455 struct gfs2_holder ghs[2];
456 struct inode *inode;
457 struct buffer_head *dibh;
458 int size;
459 int error;
460
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461 /* Must be stuffed with a null terminator for gfs2_follow_link() */
462 size = strlen(symname);
463 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
464 return -ENAMETOOLONG;
465
466 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
467
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500468 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000469 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000470 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000471 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000472 }
473
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500474 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475
Steven Whitehouse5cf32522009-03-31 16:06:27 +0100476 i_size_write(inode, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477
478 error = gfs2_meta_inode_buffer(ip, &dibh);
479
480 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500481 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
483 size);
484 brelse(dibh);
485 }
486
487 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000488 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 gfs2_inplace_release(dip);
490 gfs2_quota_unlock(dip);
491 gfs2_alloc_put(dip);
492
493 gfs2_glock_dq_uninit_m(2, ghs);
494
David Teiglandb3b94fa2006-01-16 16:50:04 +0000495 d_instantiate(dentry, inode);
496 mark_inode_dirty(inode);
497
498 return 0;
499}
500
501/**
502 * gfs2_mkdir - Make a directory
503 * @dir: The parent directory of the new one
504 * @dentry: The dentry of the new directory
505 * @mode: The mode of the new directory
506 *
507 * Returns: errno
508 */
509
510static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
511{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400512 struct gfs2_inode *dip = GFS2_I(dir), *ip;
513 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000514 struct gfs2_holder ghs[2];
515 struct inode *inode;
516 struct buffer_head *dibh;
517 int error;
518
David Teiglandb3b94fa2006-01-16 16:50:04 +0000519 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
520
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500521 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000522 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000523 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000524 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525 }
526
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500527 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528
Steven Whitehouse4f561102006-11-01 14:04:17 -0500529 ip->i_inode.i_nlink = 2;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100530 i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000531 ip->i_diskflags |= GFS2_DIF_JDATA;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000532 ip->i_entries = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533
534 error = gfs2_meta_inode_buffer(ip, &dibh);
535
536 if (!gfs2_assert_withdraw(sdp, !error)) {
537 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500538 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539
Steven Whitehousec7526662006-03-20 12:30:04 -0500540 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse8d123582010-09-17 12:30:23 +0100541 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000542 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400543 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544 di->di_entries = cpu_to_be32(1);
545
Steven Whitehousec7526662006-03-20 12:30:04 -0500546 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
Steven Whitehouse8d123582010-09-17 12:30:23 +0100547 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100549 gfs2_inum_out(dip, dent);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400550 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500552 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553
554 brelse(dibh);
555 }
556
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000558 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559 gfs2_inplace_release(dip);
560 gfs2_quota_unlock(dip);
561 gfs2_alloc_put(dip);
562
563 gfs2_glock_dq_uninit_m(2, ghs);
564
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 d_instantiate(dentry, inode);
566 mark_inode_dirty(inode);
567
568 return 0;
569}
570
571/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572 * gfs2_mknod - Make a special file
573 * @dir: The directory in which the special file will reside
574 * @dentry: The dentry of the special file
575 * @mode: The mode of the special file
576 * @rdev: The device specification of the special file
577 *
578 */
579
580static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
581 dev_t dev)
582{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500583 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400584 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 struct gfs2_holder ghs[2];
586 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587
588 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
589
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500590 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000591 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000593 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000594 }
595
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000597 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598 gfs2_inplace_release(dip);
599 gfs2_quota_unlock(dip);
600 gfs2_alloc_put(dip);
601
602 gfs2_glock_dq_uninit_m(2, ghs);
603
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 d_instantiate(dentry, inode);
605 mark_inode_dirty(inode);
606
607 return 0;
608}
609
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100610/*
611 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
612 * @this: move this
613 * @to: to here
614 *
615 * Follow @to back to the root and make sure we don't encounter @this
616 * Assumes we already hold the rename lock.
617 *
618 * Returns: errno
619 */
620
621static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
622{
623 struct inode *dir = &to->i_inode;
624 struct super_block *sb = dir->i_sb;
625 struct inode *tmp;
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100626 int error = 0;
627
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100628 igrab(dir);
629
630 for (;;) {
631 if (dir == &this->i_inode) {
632 error = -EINVAL;
633 break;
634 }
635 if (dir == sb->s_root->d_inode) {
636 error = 0;
637 break;
638 }
639
Steven Whitehouse8d123582010-09-17 12:30:23 +0100640 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100641 if (IS_ERR(tmp)) {
642 error = PTR_ERR(tmp);
643 break;
644 }
645
646 iput(dir);
647 dir = tmp;
648 }
649
650 iput(dir);
651
652 return error;
653}
654
David Teiglandb3b94fa2006-01-16 16:50:04 +0000655/**
656 * gfs2_rename - Rename a file
657 * @odir: Parent directory of old file name
658 * @odentry: The old dentry of the file
659 * @ndir: Parent directory of new file name
660 * @ndentry: The new dentry of the file
661 *
662 * Returns: errno
663 */
664
665static int gfs2_rename(struct inode *odir, struct dentry *odentry,
666 struct inode *ndir, struct dentry *ndentry)
667{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400668 struct gfs2_inode *odip = GFS2_I(odir);
669 struct gfs2_inode *ndip = GFS2_I(ndir);
670 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400672 struct gfs2_sbd *sdp = GFS2_SB(odir);
Bob Peterson462903412010-09-30 10:34:00 -0400673 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
Russell Cattelanddee7602007-01-29 17:13:44 -0600674 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675 unsigned int num_gh;
676 int dir_rename = 0;
Steven Whitehouse24b977b2009-12-09 13:55:12 +0000677 int alloc_required = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 unsigned int x;
679 int error;
680
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400682 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683 if (ip == nip)
684 return 0;
685 }
686
Bob Peterson462903412010-09-30 10:34:00 -0400687 error = gfs2_rindex_hold(sdp, &ri_gh);
688 if (error)
689 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100691 if (odip != ndip) {
692 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
693 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694 if (error)
695 goto out;
696
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100697 if (S_ISDIR(ip->i_inode.i_mode)) {
698 dir_rename = 1;
699 /* don't move a dirctory into it's subdir */
700 error = gfs2_ok_to_move(ip, ndip);
701 if (error)
702 goto out_gunlock_r;
703 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000704 }
705
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400706 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400708 if (odip != ndip) {
709 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
710 num_gh++;
711 }
712 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
713 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400715 if (nip) {
716 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
717 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600718 /* grab the resource lock for unlink flag twiddling
719 * this is the case of the target file already existing
720 * so we unlink before doing the rename
721 */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100722 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600723 if (nrgd)
724 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400725 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726
Bob Peterson72dbf472008-08-12 13:39:29 -0500727 for (x = 0; x < num_gh; x++) {
728 error = gfs2_glock_nq(ghs + x);
729 if (error)
730 goto out_gunlock;
731 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100733 error = -ENOENT;
734 if (ip->i_inode.i_nlink == 0)
735 goto out_gunlock;
736
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 /* Check out the old directory */
738
739 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
740 if (error)
741 goto out_gunlock;
742
743 /* Check out the new directory */
744
745 if (nip) {
746 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
747 if (error)
748 goto out_gunlock;
749
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100750 if (nip->i_inode.i_nlink == 0) {
751 error = -EAGAIN;
752 goto out_gunlock;
753 }
754
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500755 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f2008-11-03 13:59:19 +0000756 if (nip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500758 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 error = -EIO;
760 goto out_gunlock;
761 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000762 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763 error = -ENOTEMPTY;
764 goto out_gunlock;
765 }
766 }
767 } else {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100768 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769 if (error)
770 goto out_gunlock;
771
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100772 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 switch (error) {
774 case -ENOENT:
775 error = 0;
776 break;
777 case 0:
778 error = -EEXIST;
779 default:
780 goto out_gunlock;
781 };
782
783 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500784 if (!ndip->i_inode.i_nlink) {
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100785 error = -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786 goto out_gunlock;
787 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000788 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789 error = -EFBIG;
790 goto out_gunlock;
791 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500792 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500793 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794 error = -EMLINK;
795 goto out_gunlock;
796 }
797 }
798 }
799
800 /* Check out the dir to be renamed */
801
802 if (dir_rename) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100803 error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 if (error)
805 goto out_gunlock;
806 }
807
Steven Whitehouse24b977b2009-12-09 13:55:12 +0000808 if (nip == NULL)
809 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
810 error = alloc_required;
Steven Whitehousec7526662006-03-20 12:30:04 -0500811 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500813 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000814
815 if (alloc_required) {
816 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300817 if (!al) {
818 error = -ENOMEM;
819 goto out_gunlock;
820 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000821
Steven Whitehoused82661d2008-03-10 15:34:50 +0000822 error = gfs2_quota_lock_check(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000823 if (error)
824 goto out_alloc;
825
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 al->al_requested = sdp->sd_max_dirres;
827
Bob Peterson462903412010-09-30 10:34:00 -0400828 error = gfs2_inplace_reserve_ri(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 if (error)
830 goto out_gunlock_q;
831
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400832 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Benjamin Marzinskibf97b672010-09-27 16:00:04 -0500833 gfs2_rg_blocks(al) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500835 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836 if (error)
837 goto out_ipreserv;
838 } else {
839 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500840 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841 if (error)
842 goto out_gunlock;
843 }
844
845 /* Remove the target file, if it exists */
846
847 if (nip) {
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100848 struct buffer_head *bh;
849 error = gfs2_meta_inode_buffer(nip, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 if (error)
851 goto out_end_trans;
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100852 error = gfs2_unlink_inode(ndip, ndentry, bh);
853 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854 }
855
856 if (dir_rename) {
Steven Whitehouse8d123582010-09-17 12:30:23 +0100857 error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 if (error)
859 goto out_end_trans;
860 } else {
861 struct buffer_head *dibh;
862 error = gfs2_meta_inode_buffer(ip, &dibh);
863 if (error)
864 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100865 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000866 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500867 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868 brelse(dibh);
869 }
870
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100871 error = gfs2_dir_del(odip, odentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 if (error)
873 goto out_end_trans;
874
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +0100875 error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000876 if (error)
877 goto out_end_trans;
878
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400879out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000880 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400881out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882 if (alloc_required)
883 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400884out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 if (alloc_required)
886 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400887out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888 if (alloc_required)
889 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400890out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500891 while (x--) {
892 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -0500894 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400895out_gunlock_r:
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100896 if (r_gh.gh_gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400898out:
Bob Peterson462903412010-09-30 10:34:00 -0400899 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900 return error;
901}
902
903/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904 * gfs2_follow_link - Follow a symbolic link
905 * @dentry: The dentry of the link
906 * @nd: Data that we pass to vfs_follow_link()
907 *
Al Viroc177c2a2010-01-14 00:59:16 -0500908 * This can handle symlinks of any size.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909 *
910 * Returns: 0 on success or error code
911 */
912
913static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
914{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400915 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Al Viroc177c2a2010-01-14 00:59:16 -0500916 struct gfs2_holder i_gh;
917 struct buffer_head *dibh;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100918 unsigned int x, size;
Al Viroc177c2a2010-01-14 00:59:16 -0500919 char *buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 int error;
921
Al Viroc177c2a2010-01-14 00:59:16 -0500922 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
923 error = gfs2_glock_nq(&i_gh);
924 if (error) {
925 gfs2_holder_uninit(&i_gh);
926 nd_set_link(nd, ERR_PTR(error));
927 return NULL;
928 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000929
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100930 size = (unsigned int)i_size_read(&ip->i_inode);
931 if (size == 0) {
Al Viroc177c2a2010-01-14 00:59:16 -0500932 gfs2_consist_inode(ip);
933 buf = ERR_PTR(-EIO);
934 goto out;
935 }
936
937 error = gfs2_meta_inode_buffer(ip, &dibh);
938 if (error) {
939 buf = ERR_PTR(error);
940 goto out;
941 }
942
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100943 x = size + 1;
Al Viroc177c2a2010-01-14 00:59:16 -0500944 buf = kmalloc(x, GFP_NOFS);
945 if (!buf)
946 buf = ERR_PTR(-ENOMEM);
947 else
948 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
949 brelse(dibh);
950out:
951 gfs2_glock_dq_uninit(&i_gh);
952 nd_set_link(nd, buf);
953 return NULL;
954}
955
956static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
957{
958 char *s = nd_get_link(nd);
959 if (!IS_ERR(s))
960 kfree(s);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961}
962
963/**
964 * gfs2_permission -
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +0000965 * @inode: The inode
966 * @mask: The mask to be tested
967 * @flags: Indicates whether this is an RCU path walk or not
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500969 * This may be called from the VFS directly, or from within GFS2 with the
970 * inode locked, so we look to see if the glock is already locked and only
971 * lock the glock if its not already been done.
972 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000973 * Returns: errno
974 */
975
Nick Pigginb74c79e2011-01-07 17:49:58 +1100976int gfs2_permission(struct inode *inode, int mask, unsigned int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000977{
Nick Pigginb74c79e2011-01-07 17:49:58 +1100978 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 struct gfs2_holder i_gh;
980 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500981 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000982
Nick Pigginb74c79e2011-01-07 17:49:58 +1100983
984 ip = GFS2_I(inode);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000985 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +0000986 if (flags & IPERM_FLAG_RCU)
987 return -ECHILD;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500988 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
989 if (error)
990 return error;
991 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992 }
993
Miklos Szeredif58ba882008-07-02 21:12:01 +0200994 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
995 error = -EACCES;
996 else
Nick Pigginb74c79e2011-01-07 17:49:58 +1100997 error = generic_permission(inode, mask, flags, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500998 if (unlock)
999 gfs2_glock_dq_uninit(&i_gh);
1000
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001 return error;
1002}
1003
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004static int setattr_chown(struct inode *inode, struct iattr *attr)
1005{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001006 struct gfs2_inode *ip = GFS2_I(inode);
1007 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001008 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001009 int error;
1010
Steven Whitehouse2933f922006-11-01 13:23:29 -05001011 ouid = inode->i_uid;
1012 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001013 nuid = attr->ia_uid;
1014 ngid = attr->ia_gid;
1015
1016 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1017 ouid = nuid = NO_QUOTA_CHANGE;
1018 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1019 ogid = ngid = NO_QUOTA_CHANGE;
1020
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001021 if (!gfs2_alloc_get(ip))
1022 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023
1024 error = gfs2_quota_lock(ip, nuid, ngid);
1025 if (error)
1026 goto out_alloc;
1027
1028 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1029 error = gfs2_quota_check(ip, nuid, ngid);
1030 if (error)
1031 goto out_gunlock_q;
1032 }
1033
1034 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1035 if (error)
1036 goto out_gunlock_q;
1037
Steven Whitehouse2ae51ed2010-11-10 15:14:57 +00001038 error = gfs2_setattr_simple(ip, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039 if (error)
1040 goto out_end_trans;
1041
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001043 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1044 gfs2_quota_change(ip, -blocks, ouid, ogid);
1045 gfs2_quota_change(ip, blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046 }
1047
Steven Whitehousea91ea692006-09-04 12:04:26 -04001048out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001050out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001051 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001052out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001054 return error;
1055}
1056
1057/**
1058 * gfs2_setattr - Change attributes on an inode
1059 * @dentry: The dentry which is changing
1060 * @attr: The structure describing the change
1061 *
1062 * The VFS layer wants to change one or more of an inodes attributes. Write
1063 * that change out to disk.
1064 *
1065 * Returns: errno
1066 */
1067
1068static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1069{
1070 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001071 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072 struct gfs2_holder i_gh;
1073 int error;
1074
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1076 if (error)
1077 return error;
1078
1079 error = -EPERM;
1080 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1081 goto out;
1082
1083 error = inode_change_ok(inode, attr);
1084 if (error)
1085 goto out;
1086
1087 if (attr->ia_valid & ATTR_SIZE)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001088 error = gfs2_setattr_size(inode, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001089 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1090 error = setattr_chown(inode, attr);
1091 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1092 error = gfs2_acl_chmod(ip, attr);
1093 else
1094 error = gfs2_setattr_simple(ip, attr);
1095
Steven Whitehousea91ea692006-09-04 12:04:26 -04001096out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001098 if (!error)
1099 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100 return error;
1101}
1102
1103/**
1104 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001105 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106 * @dentry: The dentry to stat
1107 * @stat: The inode's stats
1108 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001109 * This may be called from the VFS directly, or from within GFS2 with the
1110 * inode locked, so we look to see if the glock is already locked and only
1111 * lock the glock if its not already been done. Note that its the NFS
1112 * readdirplus operation which causes this to be called (from filldir)
1113 * with the glock already held.
1114 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115 * Returns: errno
1116 */
1117
1118static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1119 struct kstat *stat)
1120{
1121 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001122 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123 struct gfs2_holder gh;
1124 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001125 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001126
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001127 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001128 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1129 if (error)
1130 return error;
1131 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001132 }
1133
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001134 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001135 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001136 gfs2_glock_dq_uninit(&gh);
1137
1138 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139}
1140
1141static int gfs2_setxattr(struct dentry *dentry, const char *name,
1142 const void *data, size_t size, int flags)
1143{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001144 struct inode *inode = dentry->d_inode;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001145 struct gfs2_inode *ip = GFS2_I(inode);
1146 struct gfs2_holder gh;
1147 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001148
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001149 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1150 ret = gfs2_glock_nq(&gh);
1151 if (ret == 0) {
1152 ret = generic_setxattr(dentry, name, data, size, flags);
1153 gfs2_glock_dq(&gh);
1154 }
1155 gfs2_holder_uninit(&gh);
1156 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001157}
1158
1159static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1160 void *data, size_t size)
1161{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001162 struct inode *inode = dentry->d_inode;
1163 struct gfs2_inode *ip = GFS2_I(inode);
1164 struct gfs2_holder gh;
1165 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001167 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1168 ret = gfs2_glock_nq(&gh);
1169 if (ret == 0) {
1170 ret = generic_getxattr(dentry, name, data, size);
1171 gfs2_glock_dq(&gh);
1172 }
1173 gfs2_holder_uninit(&gh);
1174 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001175}
1176
1177static int gfs2_removexattr(struct dentry *dentry, const char *name)
1178{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001179 struct inode *inode = dentry->d_inode;
1180 struct gfs2_inode *ip = GFS2_I(inode);
1181 struct gfs2_holder gh;
1182 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001183
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001184 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1185 ret = gfs2_glock_nq(&gh);
1186 if (ret == 0) {
1187 ret = generic_removexattr(dentry, name);
1188 gfs2_glock_dq(&gh);
1189 }
1190 gfs2_holder_uninit(&gh);
1191 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001192}
1193
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001194static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1195 u64 start, u64 len)
1196{
1197 struct gfs2_inode *ip = GFS2_I(inode);
1198 struct gfs2_holder gh;
1199 int ret;
1200
1201 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1202 if (ret)
1203 return ret;
1204
1205 mutex_lock(&inode->i_mutex);
1206
1207 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1208 if (ret)
1209 goto out;
1210
1211 if (gfs2_is_stuffed(ip)) {
1212 u64 phys = ip->i_no_addr << inode->i_blkbits;
1213 u64 size = i_size_read(inode);
1214 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1215 FIEMAP_EXTENT_DATA_INLINE;
1216 phys += sizeof(struct gfs2_dinode);
1217 phys += start;
1218 if (start + len > size)
1219 len = size - start;
1220 if (start < size)
1221 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1222 len, flags);
1223 if (ret == 1)
1224 ret = 0;
1225 } else {
1226 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1227 gfs2_block_map);
1228 }
1229
1230 gfs2_glock_dq_uninit(&gh);
1231out:
1232 mutex_unlock(&inode->i_mutex);
1233 return ret;
1234}
1235
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001236const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04001237 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001238 .setattr = gfs2_setattr,
1239 .getattr = gfs2_getattr,
1240 .setxattr = gfs2_setxattr,
1241 .getxattr = gfs2_getxattr,
1242 .listxattr = gfs2_listxattr,
1243 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001244 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001245};
1246
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001247const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001248 .create = gfs2_create,
1249 .lookup = gfs2_lookup,
1250 .link = gfs2_link,
1251 .unlink = gfs2_unlink,
1252 .symlink = gfs2_symlink,
1253 .mkdir = gfs2_mkdir,
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001254 .rmdir = gfs2_unlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001255 .mknod = gfs2_mknod,
1256 .rename = gfs2_rename,
Al Viroe6305c42008-07-15 21:03:57 -04001257 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001258 .setattr = gfs2_setattr,
1259 .getattr = gfs2_getattr,
1260 .setxattr = gfs2_setxattr,
1261 .getxattr = gfs2_getxattr,
1262 .listxattr = gfs2_listxattr,
1263 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001264 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001265};
1266
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001267const struct inode_operations gfs2_symlink_iops = {
Al Viroc177c2a2010-01-14 00:59:16 -05001268 .readlink = generic_readlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001269 .follow_link = gfs2_follow_link,
Al Viroc177c2a2010-01-14 00:59:16 -05001270 .put_link = gfs2_put_link,
Al Viroe6305c42008-07-15 21:03:57 -04001271 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001272 .setattr = gfs2_setattr,
1273 .getattr = gfs2_getattr,
1274 .setxattr = gfs2_setxattr,
1275 .getxattr = gfs2_getxattr,
1276 .listxattr = gfs2_listxattr,
1277 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001278 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279};
1280