blob: 765da06c8f555e2baba825794e8e08f21c24e251 [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 Whitehouse2baee032011-05-09 12:08:36 +0100238 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
239 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
557 error = gfs2_change_nlink(dip, +1);
558 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
559
560 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000561 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 gfs2_inplace_release(dip);
563 gfs2_quota_unlock(dip);
564 gfs2_alloc_put(dip);
565
566 gfs2_glock_dq_uninit_m(2, ghs);
567
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 d_instantiate(dentry, inode);
569 mark_inode_dirty(inode);
570
571 return 0;
572}
573
574/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 * gfs2_mknod - Make a special file
576 * @dir: The directory in which the special file will reside
577 * @dentry: The dentry of the special file
578 * @mode: The mode of the special file
579 * @rdev: The device specification of the special file
580 *
581 */
582
583static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
584 dev_t dev)
585{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500586 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400587 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588 struct gfs2_holder ghs[2];
589 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590
591 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
592
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500593 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000594 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000596 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597 }
598
David Teiglandb3b94fa2006-01-16 16:50:04 +0000599 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000600 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000601 gfs2_inplace_release(dip);
602 gfs2_quota_unlock(dip);
603 gfs2_alloc_put(dip);
604
605 gfs2_glock_dq_uninit_m(2, ghs);
606
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 d_instantiate(dentry, inode);
608 mark_inode_dirty(inode);
609
610 return 0;
611}
612
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100613/*
614 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
615 * @this: move this
616 * @to: to here
617 *
618 * Follow @to back to the root and make sure we don't encounter @this
619 * Assumes we already hold the rename lock.
620 *
621 * Returns: errno
622 */
623
624static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
625{
626 struct inode *dir = &to->i_inode;
627 struct super_block *sb = dir->i_sb;
628 struct inode *tmp;
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100629 int error = 0;
630
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100631 igrab(dir);
632
633 for (;;) {
634 if (dir == &this->i_inode) {
635 error = -EINVAL;
636 break;
637 }
638 if (dir == sb->s_root->d_inode) {
639 error = 0;
640 break;
641 }
642
Steven Whitehouse8d123582010-09-17 12:30:23 +0100643 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100644 if (IS_ERR(tmp)) {
645 error = PTR_ERR(tmp);
646 break;
647 }
648
649 iput(dir);
650 dir = tmp;
651 }
652
653 iput(dir);
654
655 return error;
656}
657
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658/**
659 * gfs2_rename - Rename a file
660 * @odir: Parent directory of old file name
661 * @odentry: The old dentry of the file
662 * @ndir: Parent directory of new file name
663 * @ndentry: The new dentry of the file
664 *
665 * Returns: errno
666 */
667
668static int gfs2_rename(struct inode *odir, struct dentry *odentry,
669 struct inode *ndir, struct dentry *ndentry)
670{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400671 struct gfs2_inode *odip = GFS2_I(odir);
672 struct gfs2_inode *ndip = GFS2_I(ndir);
673 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400675 struct gfs2_sbd *sdp = GFS2_SB(odir);
Bob Peterson462903412010-09-30 10:34:00 -0400676 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
Russell Cattelanddee7602007-01-29 17:13:44 -0600677 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 unsigned int num_gh;
679 int dir_rename = 0;
Steven Whitehouse24b977b2009-12-09 13:55:12 +0000680 int alloc_required = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681 unsigned int x;
682 int error;
683
David Teiglandb3b94fa2006-01-16 16:50:04 +0000684 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400685 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000686 if (ip == nip)
687 return 0;
688 }
689
Bob Peterson462903412010-09-30 10:34:00 -0400690 error = gfs2_rindex_hold(sdp, &ri_gh);
691 if (error)
692 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100694 if (odip != ndip) {
695 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
696 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697 if (error)
698 goto out;
699
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100700 if (S_ISDIR(ip->i_inode.i_mode)) {
701 dir_rename = 1;
702 /* don't move a dirctory into it's subdir */
703 error = gfs2_ok_to_move(ip, ndip);
704 if (error)
705 goto out_gunlock_r;
706 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 }
708
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400709 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400711 if (odip != ndip) {
712 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
713 num_gh++;
714 }
715 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
716 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400718 if (nip) {
719 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
720 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600721 /* grab the resource lock for unlink flag twiddling
722 * this is the case of the target file already existing
723 * so we unlink before doing the rename
724 */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100725 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600726 if (nrgd)
727 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400728 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729
Bob Peterson72dbf472008-08-12 13:39:29 -0500730 for (x = 0; x < num_gh; x++) {
731 error = gfs2_glock_nq(ghs + x);
732 if (error)
733 goto out_gunlock;
734 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100736 error = -ENOENT;
737 if (ip->i_inode.i_nlink == 0)
738 goto out_gunlock;
739
David Teiglandb3b94fa2006-01-16 16:50:04 +0000740 /* Check out the old directory */
741
742 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
743 if (error)
744 goto out_gunlock;
745
746 /* Check out the new directory */
747
748 if (nip) {
749 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
750 if (error)
751 goto out_gunlock;
752
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100753 if (nip->i_inode.i_nlink == 0) {
754 error = -EAGAIN;
755 goto out_gunlock;
756 }
757
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500758 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f2008-11-03 13:59:19 +0000759 if (nip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500761 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762 error = -EIO;
763 goto out_gunlock;
764 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000765 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 error = -ENOTEMPTY;
767 goto out_gunlock;
768 }
769 }
770 } else {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100771 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772 if (error)
773 goto out_gunlock;
774
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100775 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 switch (error) {
777 case -ENOENT:
778 error = 0;
779 break;
780 case 0:
781 error = -EEXIST;
782 default:
783 goto out_gunlock;
784 };
785
786 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500787 if (!ndip->i_inode.i_nlink) {
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100788 error = -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789 goto out_gunlock;
790 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000791 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792 error = -EFBIG;
793 goto out_gunlock;
794 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500795 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500796 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 error = -EMLINK;
798 goto out_gunlock;
799 }
800 }
801 }
802
803 /* Check out the dir to be renamed */
804
805 if (dir_rename) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100806 error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807 if (error)
808 goto out_gunlock;
809 }
810
Steven Whitehouse24b977b2009-12-09 13:55:12 +0000811 if (nip == NULL)
812 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
813 error = alloc_required;
Steven Whitehousec7526662006-03-20 12:30:04 -0500814 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500816 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817
818 if (alloc_required) {
819 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300820 if (!al) {
821 error = -ENOMEM;
822 goto out_gunlock;
823 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824
Steven Whitehoused82661d2008-03-10 15:34:50 +0000825 error = gfs2_quota_lock_check(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 if (error)
827 goto out_alloc;
828
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 al->al_requested = sdp->sd_max_dirres;
830
Bob Peterson462903412010-09-30 10:34:00 -0400831 error = gfs2_inplace_reserve_ri(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832 if (error)
833 goto out_gunlock_q;
834
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400835 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Benjamin Marzinskibf97b672010-09-27 16:00:04 -0500836 gfs2_rg_blocks(al) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500838 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000839 if (error)
840 goto out_ipreserv;
841 } else {
842 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500843 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844 if (error)
845 goto out_gunlock;
846 }
847
848 /* Remove the target file, if it exists */
849
850 if (nip) {
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100851 struct buffer_head *bh;
852 error = gfs2_meta_inode_buffer(nip, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 if (error)
854 goto out_end_trans;
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100855 error = gfs2_unlink_inode(ndip, ndentry, bh);
856 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857 }
858
859 if (dir_rename) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000860 error = gfs2_change_nlink(ndip, +1);
861 if (error)
862 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000863
Steven Whitehouse8d123582010-09-17 12:30:23 +0100864 error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000865 if (error)
866 goto out_end_trans;
867 } else {
868 struct buffer_head *dibh;
869 error = gfs2_meta_inode_buffer(ip, &dibh);
870 if (error)
871 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100872 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000873 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500874 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 brelse(dibh);
876 }
877
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100878 error = gfs2_dir_del(odip, odentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879 if (error)
880 goto out_end_trans;
881
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100882 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883 if (error)
884 goto out_end_trans;
885
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400886out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000887 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400888out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889 if (alloc_required)
890 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400891out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000892 if (alloc_required)
893 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400894out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 if (alloc_required)
896 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400897out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500898 while (x--) {
899 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -0500901 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400902out_gunlock_r:
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100903 if (r_gh.gh_gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400905out:
Bob Peterson462903412010-09-30 10:34:00 -0400906 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000907 return error;
908}
909
910/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911 * gfs2_follow_link - Follow a symbolic link
912 * @dentry: The dentry of the link
913 * @nd: Data that we pass to vfs_follow_link()
914 *
Al Viroc177c2a2010-01-14 00:59:16 -0500915 * This can handle symlinks of any size.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000916 *
917 * Returns: 0 on success or error code
918 */
919
920static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
921{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400922 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Al Viroc177c2a2010-01-14 00:59:16 -0500923 struct gfs2_holder i_gh;
924 struct buffer_head *dibh;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100925 unsigned int x, size;
Al Viroc177c2a2010-01-14 00:59:16 -0500926 char *buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927 int error;
928
Al Viroc177c2a2010-01-14 00:59:16 -0500929 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
930 error = gfs2_glock_nq(&i_gh);
931 if (error) {
932 gfs2_holder_uninit(&i_gh);
933 nd_set_link(nd, ERR_PTR(error));
934 return NULL;
935 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100937 size = (unsigned int)i_size_read(&ip->i_inode);
938 if (size == 0) {
Al Viroc177c2a2010-01-14 00:59:16 -0500939 gfs2_consist_inode(ip);
940 buf = ERR_PTR(-EIO);
941 goto out;
942 }
943
944 error = gfs2_meta_inode_buffer(ip, &dibh);
945 if (error) {
946 buf = ERR_PTR(error);
947 goto out;
948 }
949
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100950 x = size + 1;
Al Viroc177c2a2010-01-14 00:59:16 -0500951 buf = kmalloc(x, GFP_NOFS);
952 if (!buf)
953 buf = ERR_PTR(-ENOMEM);
954 else
955 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
956 brelse(dibh);
957out:
958 gfs2_glock_dq_uninit(&i_gh);
959 nd_set_link(nd, buf);
960 return NULL;
961}
962
963static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
964{
965 char *s = nd_get_link(nd);
966 if (!IS_ERR(s))
967 kfree(s);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968}
969
970/**
971 * gfs2_permission -
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +0000972 * @inode: The inode
973 * @mask: The mask to be tested
974 * @flags: Indicates whether this is an RCU path walk or not
David Teiglandb3b94fa2006-01-16 16:50:04 +0000975 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500976 * This may be called from the VFS directly, or from within GFS2 with the
977 * inode locked, so we look to see if the glock is already locked and only
978 * lock the glock if its not already been done.
979 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000980 * Returns: errno
981 */
982
Nick Pigginb74c79e2011-01-07 17:49:58 +1100983int gfs2_permission(struct inode *inode, int mask, unsigned int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000984{
Nick Pigginb74c79e2011-01-07 17:49:58 +1100985 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986 struct gfs2_holder i_gh;
987 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500988 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989
Nick Pigginb74c79e2011-01-07 17:49:58 +1100990
991 ip = GFS2_I(inode);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000992 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +0000993 if (flags & IPERM_FLAG_RCU)
994 return -ECHILD;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500995 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
996 if (error)
997 return error;
998 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 }
1000
Miklos Szeredif58ba882008-07-02 21:12:01 +02001001 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1002 error = -EACCES;
1003 else
Nick Pigginb74c79e2011-01-07 17:49:58 +11001004 error = generic_permission(inode, mask, flags, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001005 if (unlock)
1006 gfs2_glock_dq_uninit(&i_gh);
1007
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 return error;
1009}
1010
David Teiglandb3b94fa2006-01-16 16:50:04 +00001011static int setattr_chown(struct inode *inode, struct iattr *attr)
1012{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001013 struct gfs2_inode *ip = GFS2_I(inode);
1014 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001015 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 int error;
1017
Steven Whitehouse2933f922006-11-01 13:23:29 -05001018 ouid = inode->i_uid;
1019 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020 nuid = attr->ia_uid;
1021 ngid = attr->ia_gid;
1022
1023 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1024 ouid = nuid = NO_QUOTA_CHANGE;
1025 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1026 ogid = ngid = NO_QUOTA_CHANGE;
1027
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001028 if (!gfs2_alloc_get(ip))
1029 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001030
1031 error = gfs2_quota_lock(ip, nuid, ngid);
1032 if (error)
1033 goto out_alloc;
1034
1035 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1036 error = gfs2_quota_check(ip, nuid, ngid);
1037 if (error)
1038 goto out_gunlock_q;
1039 }
1040
1041 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1042 if (error)
1043 goto out_gunlock_q;
1044
Steven Whitehouse2ae51ed2010-11-10 15:14:57 +00001045 error = gfs2_setattr_simple(ip, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046 if (error)
1047 goto out_end_trans;
1048
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001050 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1051 gfs2_quota_change(ip, -blocks, ouid, ogid);
1052 gfs2_quota_change(ip, blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 }
1054
Steven Whitehousea91ea692006-09-04 12:04:26 -04001055out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001057out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001059out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001060 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 return error;
1062}
1063
1064/**
1065 * gfs2_setattr - Change attributes on an inode
1066 * @dentry: The dentry which is changing
1067 * @attr: The structure describing the change
1068 *
1069 * The VFS layer wants to change one or more of an inodes attributes. Write
1070 * that change out to disk.
1071 *
1072 * Returns: errno
1073 */
1074
1075static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1076{
1077 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001078 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079 struct gfs2_holder i_gh;
1080 int error;
1081
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1083 if (error)
1084 return error;
1085
1086 error = -EPERM;
1087 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1088 goto out;
1089
1090 error = inode_change_ok(inode, attr);
1091 if (error)
1092 goto out;
1093
1094 if (attr->ia_valid & ATTR_SIZE)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001095 error = gfs2_setattr_size(inode, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1097 error = setattr_chown(inode, attr);
1098 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1099 error = gfs2_acl_chmod(ip, attr);
1100 else
1101 error = gfs2_setattr_simple(ip, attr);
1102
Steven Whitehousea91ea692006-09-04 12:04:26 -04001103out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001104 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001105 if (!error)
1106 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001107 return error;
1108}
1109
1110/**
1111 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001112 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113 * @dentry: The dentry to stat
1114 * @stat: The inode's stats
1115 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001116 * This may be called from the VFS directly, or from within GFS2 with the
1117 * inode locked, so we look to see if the glock is already locked and only
1118 * lock the glock if its not already been done. Note that its the NFS
1119 * readdirplus operation which causes this to be called (from filldir)
1120 * with the glock already held.
1121 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122 * Returns: errno
1123 */
1124
1125static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1126 struct kstat *stat)
1127{
1128 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001129 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130 struct gfs2_holder gh;
1131 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001132 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001134 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001135 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1136 if (error)
1137 return error;
1138 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139 }
1140
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001141 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001142 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001143 gfs2_glock_dq_uninit(&gh);
1144
1145 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001146}
1147
1148static int gfs2_setxattr(struct dentry *dentry, const char *name,
1149 const void *data, size_t size, int flags)
1150{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001151 struct inode *inode = dentry->d_inode;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001152 struct gfs2_inode *ip = GFS2_I(inode);
1153 struct gfs2_holder gh;
1154 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001155
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001156 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1157 ret = gfs2_glock_nq(&gh);
1158 if (ret == 0) {
1159 ret = generic_setxattr(dentry, name, data, size, flags);
1160 gfs2_glock_dq(&gh);
1161 }
1162 gfs2_holder_uninit(&gh);
1163 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164}
1165
1166static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1167 void *data, size_t size)
1168{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001169 struct inode *inode = dentry->d_inode;
1170 struct gfs2_inode *ip = GFS2_I(inode);
1171 struct gfs2_holder gh;
1172 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001173
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001174 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1175 ret = gfs2_glock_nq(&gh);
1176 if (ret == 0) {
1177 ret = generic_getxattr(dentry, name, data, size);
1178 gfs2_glock_dq(&gh);
1179 }
1180 gfs2_holder_uninit(&gh);
1181 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001182}
1183
1184static int gfs2_removexattr(struct dentry *dentry, const char *name)
1185{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001186 struct inode *inode = dentry->d_inode;
1187 struct gfs2_inode *ip = GFS2_I(inode);
1188 struct gfs2_holder gh;
1189 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001190
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001191 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1192 ret = gfs2_glock_nq(&gh);
1193 if (ret == 0) {
1194 ret = generic_removexattr(dentry, name);
1195 gfs2_glock_dq(&gh);
1196 }
1197 gfs2_holder_uninit(&gh);
1198 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001199}
1200
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001201static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1202 u64 start, u64 len)
1203{
1204 struct gfs2_inode *ip = GFS2_I(inode);
1205 struct gfs2_holder gh;
1206 int ret;
1207
1208 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1209 if (ret)
1210 return ret;
1211
1212 mutex_lock(&inode->i_mutex);
1213
1214 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1215 if (ret)
1216 goto out;
1217
1218 if (gfs2_is_stuffed(ip)) {
1219 u64 phys = ip->i_no_addr << inode->i_blkbits;
1220 u64 size = i_size_read(inode);
1221 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1222 FIEMAP_EXTENT_DATA_INLINE;
1223 phys += sizeof(struct gfs2_dinode);
1224 phys += start;
1225 if (start + len > size)
1226 len = size - start;
1227 if (start < size)
1228 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1229 len, flags);
1230 if (ret == 1)
1231 ret = 0;
1232 } else {
1233 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1234 gfs2_block_map);
1235 }
1236
1237 gfs2_glock_dq_uninit(&gh);
1238out:
1239 mutex_unlock(&inode->i_mutex);
1240 return ret;
1241}
1242
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001243const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04001244 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001245 .setattr = gfs2_setattr,
1246 .getattr = gfs2_getattr,
1247 .setxattr = gfs2_setxattr,
1248 .getxattr = gfs2_getxattr,
1249 .listxattr = gfs2_listxattr,
1250 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001251 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252};
1253
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001254const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001255 .create = gfs2_create,
1256 .lookup = gfs2_lookup,
1257 .link = gfs2_link,
1258 .unlink = gfs2_unlink,
1259 .symlink = gfs2_symlink,
1260 .mkdir = gfs2_mkdir,
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001261 .rmdir = gfs2_unlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001262 .mknod = gfs2_mknod,
1263 .rename = gfs2_rename,
Al Viroe6305c42008-07-15 21:03:57 -04001264 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001265 .setattr = gfs2_setattr,
1266 .getattr = gfs2_getattr,
1267 .setxattr = gfs2_setxattr,
1268 .getxattr = gfs2_getxattr,
1269 .listxattr = gfs2_listxattr,
1270 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001271 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001272};
1273
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001274const struct inode_operations gfs2_symlink_iops = {
Al Viroc177c2a2010-01-14 00:59:16 -05001275 .readlink = generic_readlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001276 .follow_link = gfs2_follow_link,
Al Viroc177c2a2010-01-14 00:59:16 -05001277 .put_link = gfs2_put_link,
Al Viroe6305c42008-07-15 21:03:57 -04001278 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279 .setattr = gfs2_setattr,
1280 .getattr = gfs2_getattr,
1281 .setxattr = gfs2_setxattr,
1282 .getxattr = gfs2_getxattr,
1283 .listxattr = gfs2_listxattr,
1284 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001285 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001286};
1287