blob: 2607c2c6de2be2e987b3a5cfad2748d38ce9c492 [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) {
Steven Whitehouse94fb7632011-05-09 13:36:10 +0100757 gfs2_consist_inode(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000758 error = -EIO;
759 goto out_gunlock;
760 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000761 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762 error = -ENOTEMPTY;
763 goto out_gunlock;
764 }
765 }
766 } else {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100767 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768 if (error)
769 goto out_gunlock;
770
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100771 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772 switch (error) {
773 case -ENOENT:
774 error = 0;
775 break;
776 case 0:
777 error = -EEXIST;
778 default:
779 goto out_gunlock;
780 };
781
782 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500783 if (!ndip->i_inode.i_nlink) {
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100784 error = -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 goto out_gunlock;
786 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000787 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 error = -EFBIG;
789 goto out_gunlock;
790 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500791 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500792 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 error = -EMLINK;
794 goto out_gunlock;
795 }
796 }
797 }
798
799 /* Check out the dir to be renamed */
800
801 if (dir_rename) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100802 error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000803 if (error)
804 goto out_gunlock;
805 }
806
Steven Whitehouse24b977b2009-12-09 13:55:12 +0000807 if (nip == NULL)
808 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
809 error = alloc_required;
Steven Whitehousec7526662006-03-20 12:30:04 -0500810 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500812 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813
814 if (alloc_required) {
815 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300816 if (!al) {
817 error = -ENOMEM;
818 goto out_gunlock;
819 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820
Steven Whitehoused82661d2008-03-10 15:34:50 +0000821 error = gfs2_quota_lock_check(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822 if (error)
823 goto out_alloc;
824
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 al->al_requested = sdp->sd_max_dirres;
826
Bob Peterson462903412010-09-30 10:34:00 -0400827 error = gfs2_inplace_reserve_ri(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828 if (error)
829 goto out_gunlock_q;
830
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400831 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Benjamin Marzinskibf97b672010-09-27 16:00:04 -0500832 gfs2_rg_blocks(al) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500834 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835 if (error)
836 goto out_ipreserv;
837 } else {
838 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500839 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000840 if (error)
841 goto out_gunlock;
842 }
843
844 /* Remove the target file, if it exists */
845
846 if (nip) {
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100847 struct buffer_head *bh;
848 error = gfs2_meta_inode_buffer(nip, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000849 if (error)
850 goto out_end_trans;
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100851 error = gfs2_unlink_inode(ndip, ndentry, bh);
852 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 }
854
855 if (dir_rename) {
Steven Whitehouse8d123582010-09-17 12:30:23 +0100856 error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857 if (error)
858 goto out_end_trans;
859 } else {
860 struct buffer_head *dibh;
861 error = gfs2_meta_inode_buffer(ip, &dibh);
862 if (error)
863 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100864 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000865 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500866 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 brelse(dibh);
868 }
869
Steven Whitehouse855d23c2011-05-09 16:42:37 +0100870 error = gfs2_dir_del(odip, odentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 if (error)
872 goto out_end_trans;
873
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +0100874 error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 if (error)
876 goto out_end_trans;
877
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400878out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400880out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881 if (alloc_required)
882 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400883out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000884 if (alloc_required)
885 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400886out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000887 if (alloc_required)
888 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400889out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500890 while (x--) {
891 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000892 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -0500893 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400894out_gunlock_r:
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100895 if (r_gh.gh_gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400897out:
Bob Peterson462903412010-09-30 10:34:00 -0400898 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899 return error;
900}
901
902/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903 * gfs2_follow_link - Follow a symbolic link
904 * @dentry: The dentry of the link
905 * @nd: Data that we pass to vfs_follow_link()
906 *
Al Viroc177c2a2010-01-14 00:59:16 -0500907 * This can handle symlinks of any size.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908 *
909 * Returns: 0 on success or error code
910 */
911
912static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
913{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400914 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Al Viroc177c2a2010-01-14 00:59:16 -0500915 struct gfs2_holder i_gh;
916 struct buffer_head *dibh;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100917 unsigned int x, size;
Al Viroc177c2a2010-01-14 00:59:16 -0500918 char *buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919 int error;
920
Al Viroc177c2a2010-01-14 00:59:16 -0500921 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
922 error = gfs2_glock_nq(&i_gh);
923 if (error) {
924 gfs2_holder_uninit(&i_gh);
925 nd_set_link(nd, ERR_PTR(error));
926 return NULL;
927 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100929 size = (unsigned int)i_size_read(&ip->i_inode);
930 if (size == 0) {
Al Viroc177c2a2010-01-14 00:59:16 -0500931 gfs2_consist_inode(ip);
932 buf = ERR_PTR(-EIO);
933 goto out;
934 }
935
936 error = gfs2_meta_inode_buffer(ip, &dibh);
937 if (error) {
938 buf = ERR_PTR(error);
939 goto out;
940 }
941
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100942 x = size + 1;
Al Viroc177c2a2010-01-14 00:59:16 -0500943 buf = kmalloc(x, GFP_NOFS);
944 if (!buf)
945 buf = ERR_PTR(-ENOMEM);
946 else
947 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
948 brelse(dibh);
949out:
950 gfs2_glock_dq_uninit(&i_gh);
951 nd_set_link(nd, buf);
952 return NULL;
953}
954
955static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
956{
957 char *s = nd_get_link(nd);
958 if (!IS_ERR(s))
959 kfree(s);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960}
961
962/**
963 * gfs2_permission -
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +0000964 * @inode: The inode
965 * @mask: The mask to be tested
966 * @flags: Indicates whether this is an RCU path walk or not
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500968 * This may be called from the VFS directly, or from within GFS2 with the
969 * inode locked, so we look to see if the glock is already locked and only
970 * lock the glock if its not already been done.
971 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 * Returns: errno
973 */
974
Nick Pigginb74c79e2011-01-07 17:49:58 +1100975int gfs2_permission(struct inode *inode, int mask, unsigned int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976{
Nick Pigginb74c79e2011-01-07 17:49:58 +1100977 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978 struct gfs2_holder i_gh;
979 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500980 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981
Nick Pigginb74c79e2011-01-07 17:49:58 +1100982
983 ip = GFS2_I(inode);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000984 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +0000985 if (flags & IPERM_FLAG_RCU)
986 return -ECHILD;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500987 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
988 if (error)
989 return error;
990 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000991 }
992
Miklos Szeredif58ba882008-07-02 21:12:01 +0200993 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
994 error = -EACCES;
995 else
Nick Pigginb74c79e2011-01-07 17:49:58 +1100996 error = generic_permission(inode, mask, flags, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500997 if (unlock)
998 gfs2_glock_dq_uninit(&i_gh);
999
David Teiglandb3b94fa2006-01-16 16:50:04 +00001000 return error;
1001}
1002
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003static int setattr_chown(struct inode *inode, struct iattr *attr)
1004{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001005 struct gfs2_inode *ip = GFS2_I(inode);
1006 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001007 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 int error;
1009
Steven Whitehouse2933f922006-11-01 13:23:29 -05001010 ouid = inode->i_uid;
1011 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012 nuid = attr->ia_uid;
1013 ngid = attr->ia_gid;
1014
1015 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1016 ouid = nuid = NO_QUOTA_CHANGE;
1017 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1018 ogid = ngid = NO_QUOTA_CHANGE;
1019
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001020 if (!gfs2_alloc_get(ip))
1021 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022
1023 error = gfs2_quota_lock(ip, nuid, ngid);
1024 if (error)
1025 goto out_alloc;
1026
1027 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1028 error = gfs2_quota_check(ip, nuid, ngid);
1029 if (error)
1030 goto out_gunlock_q;
1031 }
1032
1033 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1034 if (error)
1035 goto out_gunlock_q;
1036
Steven Whitehouse2ae51ed2010-11-10 15:14:57 +00001037 error = gfs2_setattr_simple(ip, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038 if (error)
1039 goto out_end_trans;
1040
David Teiglandb3b94fa2006-01-16 16:50:04 +00001041 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001042 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1043 gfs2_quota_change(ip, -blocks, ouid, ogid);
1044 gfs2_quota_change(ip, blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045 }
1046
Steven Whitehousea91ea692006-09-04 12:04:26 -04001047out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001048 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001049out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001050 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001051out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 return error;
1054}
1055
1056/**
1057 * gfs2_setattr - Change attributes on an inode
1058 * @dentry: The dentry which is changing
1059 * @attr: The structure describing the change
1060 *
1061 * The VFS layer wants to change one or more of an inodes attributes. Write
1062 * that change out to disk.
1063 *
1064 * Returns: errno
1065 */
1066
1067static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1068{
1069 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001070 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001071 struct gfs2_holder i_gh;
1072 int error;
1073
David Teiglandb3b94fa2006-01-16 16:50:04 +00001074 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1075 if (error)
1076 return error;
1077
1078 error = -EPERM;
1079 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1080 goto out;
1081
1082 error = inode_change_ok(inode, attr);
1083 if (error)
1084 goto out;
1085
1086 if (attr->ia_valid & ATTR_SIZE)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001087 error = gfs2_setattr_size(inode, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1089 error = setattr_chown(inode, attr);
1090 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1091 error = gfs2_acl_chmod(ip, attr);
1092 else
1093 error = gfs2_setattr_simple(ip, attr);
1094
Steven Whitehousea91ea692006-09-04 12:04:26 -04001095out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 if (!error)
1098 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 return error;
1100}
1101
1102/**
1103 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001104 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001105 * @dentry: The dentry to stat
1106 * @stat: The inode's stats
1107 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001108 * This may be called from the VFS directly, or from within GFS2 with the
1109 * inode locked, so we look to see if the glock is already locked and only
1110 * lock the glock if its not already been done. Note that its the NFS
1111 * readdirplus operation which causes this to be called (from filldir)
1112 * with the glock already held.
1113 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114 * Returns: errno
1115 */
1116
1117static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1118 struct kstat *stat)
1119{
1120 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001121 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122 struct gfs2_holder gh;
1123 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001124 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001126 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001127 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1128 if (error)
1129 return error;
1130 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001131 }
1132
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001133 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001134 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001135 gfs2_glock_dq_uninit(&gh);
1136
1137 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138}
1139
1140static int gfs2_setxattr(struct dentry *dentry, const char *name,
1141 const void *data, size_t size, int flags)
1142{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001143 struct inode *inode = dentry->d_inode;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001144 struct gfs2_inode *ip = GFS2_I(inode);
1145 struct gfs2_holder gh;
1146 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001148 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1149 ret = gfs2_glock_nq(&gh);
1150 if (ret == 0) {
1151 ret = generic_setxattr(dentry, name, data, size, flags);
1152 gfs2_glock_dq(&gh);
1153 }
1154 gfs2_holder_uninit(&gh);
1155 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156}
1157
1158static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1159 void *data, size_t size)
1160{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001161 struct inode *inode = dentry->d_inode;
1162 struct gfs2_inode *ip = GFS2_I(inode);
1163 struct gfs2_holder gh;
1164 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001166 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1167 ret = gfs2_glock_nq(&gh);
1168 if (ret == 0) {
1169 ret = generic_getxattr(dentry, name, data, size);
1170 gfs2_glock_dq(&gh);
1171 }
1172 gfs2_holder_uninit(&gh);
1173 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001174}
1175
1176static int gfs2_removexattr(struct dentry *dentry, const char *name)
1177{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001178 struct inode *inode = dentry->d_inode;
1179 struct gfs2_inode *ip = GFS2_I(inode);
1180 struct gfs2_holder gh;
1181 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001182
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001183 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1184 ret = gfs2_glock_nq(&gh);
1185 if (ret == 0) {
1186 ret = generic_removexattr(dentry, name);
1187 gfs2_glock_dq(&gh);
1188 }
1189 gfs2_holder_uninit(&gh);
1190 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001191}
1192
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001193static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1194 u64 start, u64 len)
1195{
1196 struct gfs2_inode *ip = GFS2_I(inode);
1197 struct gfs2_holder gh;
1198 int ret;
1199
1200 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1201 if (ret)
1202 return ret;
1203
1204 mutex_lock(&inode->i_mutex);
1205
1206 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1207 if (ret)
1208 goto out;
1209
1210 if (gfs2_is_stuffed(ip)) {
1211 u64 phys = ip->i_no_addr << inode->i_blkbits;
1212 u64 size = i_size_read(inode);
1213 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1214 FIEMAP_EXTENT_DATA_INLINE;
1215 phys += sizeof(struct gfs2_dinode);
1216 phys += start;
1217 if (start + len > size)
1218 len = size - start;
1219 if (start < size)
1220 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1221 len, flags);
1222 if (ret == 1)
1223 ret = 0;
1224 } else {
1225 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1226 gfs2_block_map);
1227 }
1228
1229 gfs2_glock_dq_uninit(&gh);
1230out:
1231 mutex_unlock(&inode->i_mutex);
1232 return ret;
1233}
1234
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001235const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04001236 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237 .setattr = gfs2_setattr,
1238 .getattr = gfs2_getattr,
1239 .setxattr = gfs2_setxattr,
1240 .getxattr = gfs2_getxattr,
1241 .listxattr = gfs2_listxattr,
1242 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001243 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244};
1245
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001246const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001247 .create = gfs2_create,
1248 .lookup = gfs2_lookup,
1249 .link = gfs2_link,
1250 .unlink = gfs2_unlink,
1251 .symlink = gfs2_symlink,
1252 .mkdir = gfs2_mkdir,
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001253 .rmdir = gfs2_unlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001254 .mknod = gfs2_mknod,
1255 .rename = gfs2_rename,
Al Viroe6305c42008-07-15 21:03:57 -04001256 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001257 .setattr = gfs2_setattr,
1258 .getattr = gfs2_getattr,
1259 .setxattr = gfs2_setxattr,
1260 .getxattr = gfs2_getxattr,
1261 .listxattr = gfs2_listxattr,
1262 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001263 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264};
1265
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001266const struct inode_operations gfs2_symlink_iops = {
Al Viroc177c2a2010-01-14 00:59:16 -05001267 .readlink = generic_readlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001268 .follow_link = gfs2_follow_link,
Al Viroc177c2a2010-01-14 00:59:16 -05001269 .put_link = gfs2_put_link,
Al Viroe6305c42008-07-15 21:03:57 -04001270 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001271 .setattr = gfs2_setattr,
1272 .getattr = gfs2_getattr,
1273 .setxattr = gfs2_setxattr,
1274 .getxattr = gfs2_getxattr,
1275 .listxattr = gfs2_listxattr,
1276 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001277 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001278};
1279