blob: acb6f69b02ed9cc6bdb4aa3949cbc7047c4ec867 [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/**
315 * gfs2_unlink - Unlink a file
316 * @dir: The inode of the directory containing the file to unlink
317 * @dentry: The file itself
318 *
319 * Unlink a file. Call gfs2_unlinki()
320 *
321 * Returns: errno
322 */
323
324static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
325{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400326 struct gfs2_inode *dip = GFS2_I(dir);
327 struct gfs2_sbd *sdp = GFS2_SB(dir);
328 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600329 struct gfs2_holder ghs[3];
330 struct gfs2_rgrpd *rgd;
331 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000332 int error;
333
Russell Cattelanddee7602007-01-29 17:13:44 -0600334 error = gfs2_rindex_hold(sdp, &ri_gh);
335 if (error)
336 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337
Russell Cattelanddee7602007-01-29 17:13:44 -0600338 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
339 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
340
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100341 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600342 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
343
344
Steven Whitehouse8497a462007-08-26 14:23:56 +0100345 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100347 goto out_parent;
348
349 error = gfs2_glock_nq(ghs + 1); /* child */
350 if (error)
351 goto out_child;
352
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100353 error = -ENOENT;
354 if (ip->i_inode.i_nlink == 0)
355 goto out_rgrp;
356
Steven Whitehouse8497a462007-08-26 14:23:56 +0100357 error = gfs2_glock_nq(ghs + 2); /* rgrp */
358 if (error)
359 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360
361 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
362 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500363 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000364
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400365 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366 if (error)
Roel Kluincd012072009-08-22 19:26:42 +0200367 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000368
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400369 error = gfs2_dir_del(dip, &dentry->d_name);
370 if (error)
371 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400373 error = gfs2_change_nlink(ip, -1);
374
375out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000376 gfs2_trans_end(sdp);
Bob Peterson72dbf472008-08-12 13:39:29 -0500377out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +0100378 gfs2_glock_dq(ghs + 2);
379out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600380 gfs2_holder_uninit(ghs + 2);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100381 gfs2_glock_dq(ghs + 1);
382out_child:
383 gfs2_holder_uninit(ghs + 1);
384 gfs2_glock_dq(ghs);
385out_parent:
386 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600387 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388 return error;
389}
390
391/**
392 * gfs2_symlink - Create a symlink
393 * @dir: The directory to create the symlink in
394 * @dentry: The dentry to put the symlink in
395 * @symname: The thing which the link points to
396 *
397 * Returns: errno
398 */
399
400static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
401 const char *symname)
402{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400403 struct gfs2_inode *dip = GFS2_I(dir), *ip;
404 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405 struct gfs2_holder ghs[2];
406 struct inode *inode;
407 struct buffer_head *dibh;
408 int size;
409 int error;
410
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411 /* Must be stuffed with a null terminator for gfs2_follow_link() */
412 size = strlen(symname);
413 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
414 return -ENAMETOOLONG;
415
416 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
417
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500418 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000419 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000421 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 }
423
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500424 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425
Steven Whitehouse5cf32522009-03-31 16:06:27 +0100426 i_size_write(inode, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427
428 error = gfs2_meta_inode_buffer(ip, &dibh);
429
430 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500431 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
433 size);
434 brelse(dibh);
435 }
436
437 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000438 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439 gfs2_inplace_release(dip);
440 gfs2_quota_unlock(dip);
441 gfs2_alloc_put(dip);
442
443 gfs2_glock_dq_uninit_m(2, ghs);
444
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445 d_instantiate(dentry, inode);
446 mark_inode_dirty(inode);
447
448 return 0;
449}
450
451/**
452 * gfs2_mkdir - Make a directory
453 * @dir: The parent directory of the new one
454 * @dentry: The dentry of the new directory
455 * @mode: The mode of the new directory
456 *
457 * Returns: errno
458 */
459
460static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
461{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400462 struct gfs2_inode *dip = GFS2_I(dir), *ip;
463 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464 struct gfs2_holder ghs[2];
465 struct inode *inode;
466 struct buffer_head *dibh;
467 int error;
468
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
470
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500471 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000472 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000473 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000474 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 }
476
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500477 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000478
Steven Whitehouse4f561102006-11-01 14:04:17 -0500479 ip->i_inode.i_nlink = 2;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100480 i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000481 ip->i_diskflags |= GFS2_DIF_JDATA;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000482 ip->i_entries = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000483
484 error = gfs2_meta_inode_buffer(ip, &dibh);
485
486 if (!gfs2_assert_withdraw(sdp, !error)) {
487 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500488 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489
Steven Whitehousec7526662006-03-20 12:30:04 -0500490 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse8d123582010-09-17 12:30:23 +0100491 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400493 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494 di->di_entries = cpu_to_be32(1);
495
Steven Whitehousec7526662006-03-20 12:30:04 -0500496 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
Steven Whitehouse8d123582010-09-17 12:30:23 +0100497 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000498
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100499 gfs2_inum_out(dip, dent);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400500 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000501
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500502 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000503
504 brelse(dibh);
505 }
506
507 error = gfs2_change_nlink(dip, +1);
508 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
509
510 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000511 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 gfs2_inplace_release(dip);
513 gfs2_quota_unlock(dip);
514 gfs2_alloc_put(dip);
515
516 gfs2_glock_dq_uninit_m(2, ghs);
517
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518 d_instantiate(dentry, inode);
519 mark_inode_dirty(inode);
520
521 return 0;
522}
523
524/**
Steven Whitehouse2286dbf2009-05-22 10:45:09 +0100525 * gfs2_rmdiri - Remove a directory
526 * @dip: The parent directory of the directory to be removed
527 * @name: The name of the directory to be removed
528 * @ip: The GFS2 inode of the directory to be removed
529 *
530 * Assumes Glocks on dip and ip are held
531 *
532 * Returns: errno
533 */
534
535static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
536 struct gfs2_inode *ip)
537{
Steven Whitehouse2286dbf2009-05-22 10:45:09 +0100538 int error;
539
540 if (ip->i_entries != 2) {
541 if (gfs2_consist_inode(ip))
542 gfs2_dinode_print(ip);
543 return -EIO;
544 }
545
546 error = gfs2_dir_del(dip, name);
547 if (error)
548 return error;
549
550 error = gfs2_change_nlink(dip, -1);
551 if (error)
552 return error;
553
Steven Whitehouse8d123582010-09-17 12:30:23 +0100554 error = gfs2_dir_del(ip, &gfs2_qdot);
Steven Whitehouse2286dbf2009-05-22 10:45:09 +0100555 if (error)
556 return error;
557
Steven Whitehouse8d123582010-09-17 12:30:23 +0100558 error = gfs2_dir_del(ip, &gfs2_qdotdot);
Steven Whitehouse2286dbf2009-05-22 10:45:09 +0100559 if (error)
560 return error;
561
562 /* It looks odd, but it really should be done twice */
563 error = gfs2_change_nlink(ip, -1);
564 if (error)
565 return error;
566
567 error = gfs2_change_nlink(ip, -1);
568 if (error)
569 return error;
570
571 return error;
572}
573
574/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 * gfs2_rmdir - Remove a directory
576 * @dir: The parent directory of the directory to be removed
577 * @dentry: The dentry of the directory to remove
578 *
579 * Remove a directory. Call gfs2_rmdiri()
580 *
581 * Returns: errno
582 */
583
584static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
585{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400586 struct gfs2_inode *dip = GFS2_I(dir);
587 struct gfs2_sbd *sdp = GFS2_SB(dir);
588 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600589 struct gfs2_holder ghs[3];
590 struct gfs2_rgrpd *rgd;
591 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592 int error;
593
Russell Cattelanddee7602007-01-29 17:13:44 -0600594 error = gfs2_rindex_hold(sdp, &ri_gh);
595 if (error)
596 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
598 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
599
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100600 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600601 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
602
Bob Peterson72dbf472008-08-12 13:39:29 -0500603 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500605 goto out_parent;
606
607 error = gfs2_glock_nq(ghs + 1); /* child */
608 if (error)
609 goto out_child;
610
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100611 error = -ENOENT;
612 if (ip->i_inode.i_nlink == 0)
613 goto out_rgrp;
614
Bob Peterson72dbf472008-08-12 13:39:29 -0500615 error = gfs2_glock_nq(ghs + 2); /* rgrp */
616 if (error)
617 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618
619 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
620 if (error)
621 goto out_gunlock;
622
Steven Whitehousead6203f2008-11-03 13:59:19 +0000623 if (ip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500625 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000626 error = -EIO;
627 goto out_gunlock;
628 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000629 if (ip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 error = -ENOTEMPTY;
631 goto out_gunlock;
632 }
633
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400634 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635 if (error)
636 goto out_gunlock;
637
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400638 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639
640 gfs2_trans_end(sdp);
641
Steven Whitehousea91ea692006-09-04 12:04:26 -0400642out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500643 gfs2_glock_dq(ghs + 2);
644out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600645 gfs2_holder_uninit(ghs + 2);
Bob Peterson72dbf472008-08-12 13:39:29 -0500646 gfs2_glock_dq(ghs + 1);
647out_child:
648 gfs2_holder_uninit(ghs + 1);
649 gfs2_glock_dq(ghs);
650out_parent:
651 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600652 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000653 return error;
654}
655
656/**
657 * gfs2_mknod - Make a special file
658 * @dir: The directory in which the special file will reside
659 * @dentry: The dentry of the special file
660 * @mode: The mode of the special file
661 * @rdev: The device specification of the special file
662 *
663 */
664
665static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
666 dev_t dev)
667{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500668 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400669 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670 struct gfs2_holder ghs[2];
671 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672
673 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
674
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500675 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000676 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000678 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679 }
680
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000682 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683 gfs2_inplace_release(dip);
684 gfs2_quota_unlock(dip);
685 gfs2_alloc_put(dip);
686
687 gfs2_glock_dq_uninit_m(2, ghs);
688
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 d_instantiate(dentry, inode);
690 mark_inode_dirty(inode);
691
692 return 0;
693}
694
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100695/*
696 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
697 * @this: move this
698 * @to: to here
699 *
700 * Follow @to back to the root and make sure we don't encounter @this
701 * Assumes we already hold the rename lock.
702 *
703 * Returns: errno
704 */
705
706static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
707{
708 struct inode *dir = &to->i_inode;
709 struct super_block *sb = dir->i_sb;
710 struct inode *tmp;
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100711 int error = 0;
712
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100713 igrab(dir);
714
715 for (;;) {
716 if (dir == &this->i_inode) {
717 error = -EINVAL;
718 break;
719 }
720 if (dir == sb->s_root->d_inode) {
721 error = 0;
722 break;
723 }
724
Steven Whitehouse8d123582010-09-17 12:30:23 +0100725 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100726 if (IS_ERR(tmp)) {
727 error = PTR_ERR(tmp);
728 break;
729 }
730
731 iput(dir);
732 dir = tmp;
733 }
734
735 iput(dir);
736
737 return error;
738}
739
David Teiglandb3b94fa2006-01-16 16:50:04 +0000740/**
741 * gfs2_rename - Rename a file
742 * @odir: Parent directory of old file name
743 * @odentry: The old dentry of the file
744 * @ndir: Parent directory of new file name
745 * @ndentry: The new dentry of the file
746 *
747 * Returns: errno
748 */
749
750static int gfs2_rename(struct inode *odir, struct dentry *odentry,
751 struct inode *ndir, struct dentry *ndentry)
752{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400753 struct gfs2_inode *odip = GFS2_I(odir);
754 struct gfs2_inode *ndip = GFS2_I(ndir);
755 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400757 struct gfs2_sbd *sdp = GFS2_SB(odir);
Bob Peterson462903412010-09-30 10:34:00 -0400758 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
Russell Cattelanddee7602007-01-29 17:13:44 -0600759 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 unsigned int num_gh;
761 int dir_rename = 0;
Steven Whitehouse24b977b2009-12-09 13:55:12 +0000762 int alloc_required = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763 unsigned int x;
764 int error;
765
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400767 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768 if (ip == nip)
769 return 0;
770 }
771
Bob Peterson462903412010-09-30 10:34:00 -0400772 error = gfs2_rindex_hold(sdp, &ri_gh);
773 if (error)
774 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000775
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100776 if (odip != ndip) {
777 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
778 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779 if (error)
780 goto out;
781
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100782 if (S_ISDIR(ip->i_inode.i_mode)) {
783 dir_rename = 1;
784 /* don't move a dirctory into it's subdir */
785 error = gfs2_ok_to_move(ip, ndip);
786 if (error)
787 goto out_gunlock_r;
788 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789 }
790
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400791 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400793 if (odip != ndip) {
794 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
795 num_gh++;
796 }
797 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
798 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400800 if (nip) {
801 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
802 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600803 /* grab the resource lock for unlink flag twiddling
804 * this is the case of the target file already existing
805 * so we unlink before doing the rename
806 */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100807 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600808 if (nrgd)
809 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400810 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811
Bob Peterson72dbf472008-08-12 13:39:29 -0500812 for (x = 0; x < num_gh; x++) {
813 error = gfs2_glock_nq(ghs + x);
814 if (error)
815 goto out_gunlock;
816 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100818 error = -ENOENT;
819 if (ip->i_inode.i_nlink == 0)
820 goto out_gunlock;
821
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822 /* Check out the old directory */
823
824 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
825 if (error)
826 goto out_gunlock;
827
828 /* Check out the new directory */
829
830 if (nip) {
831 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
832 if (error)
833 goto out_gunlock;
834
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100835 if (nip->i_inode.i_nlink == 0) {
836 error = -EAGAIN;
837 goto out_gunlock;
838 }
839
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500840 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f2008-11-03 13:59:19 +0000841 if (nip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500843 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844 error = -EIO;
845 goto out_gunlock;
846 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000847 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 error = -ENOTEMPTY;
849 goto out_gunlock;
850 }
851 }
852 } else {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100853 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854 if (error)
855 goto out_gunlock;
856
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100857 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 switch (error) {
859 case -ENOENT:
860 error = 0;
861 break;
862 case 0:
863 error = -EEXIST;
864 default:
865 goto out_gunlock;
866 };
867
868 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500869 if (!ndip->i_inode.i_nlink) {
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100870 error = -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 goto out_gunlock;
872 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000873 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874 error = -EFBIG;
875 goto out_gunlock;
876 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500877 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500878 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879 error = -EMLINK;
880 goto out_gunlock;
881 }
882 }
883 }
884
885 /* Check out the dir to be renamed */
886
887 if (dir_rename) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100888 error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889 if (error)
890 goto out_gunlock;
891 }
892
Steven Whitehouse24b977b2009-12-09 13:55:12 +0000893 if (nip == NULL)
894 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
895 error = alloc_required;
Steven Whitehousec7526662006-03-20 12:30:04 -0500896 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500898 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899
900 if (alloc_required) {
901 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300902 if (!al) {
903 error = -ENOMEM;
904 goto out_gunlock;
905 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906
Steven Whitehoused82661d2008-03-10 15:34:50 +0000907 error = gfs2_quota_lock_check(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908 if (error)
909 goto out_alloc;
910
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911 al->al_requested = sdp->sd_max_dirres;
912
Bob Peterson462903412010-09-30 10:34:00 -0400913 error = gfs2_inplace_reserve_ri(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914 if (error)
915 goto out_gunlock_q;
916
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400917 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Benjamin Marzinskibf97b672010-09-27 16:00:04 -0500918 gfs2_rg_blocks(al) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500920 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000921 if (error)
922 goto out_ipreserv;
923 } else {
924 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500925 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000926 if (error)
927 goto out_gunlock;
928 }
929
930 /* Remove the target file, if it exists */
931
932 if (nip) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500933 if (S_ISDIR(nip->i_inode.i_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400934 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
935 else {
936 error = gfs2_dir_del(ndip, &ndentry->d_name);
937 if (error)
938 goto out_end_trans;
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500939 error = gfs2_change_nlink(nip, -1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400940 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941 if (error)
942 goto out_end_trans;
943 }
944
945 if (dir_rename) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946 error = gfs2_change_nlink(ndip, +1);
947 if (error)
948 goto out_end_trans;
949 error = gfs2_change_nlink(odip, -1);
950 if (error)
951 goto out_end_trans;
952
Steven Whitehouse8d123582010-09-17 12:30:23 +0100953 error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954 if (error)
955 goto out_end_trans;
956 } else {
957 struct buffer_head *dibh;
958 error = gfs2_meta_inode_buffer(ip, &dibh);
959 if (error)
960 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100961 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000962 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500963 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000964 brelse(dibh);
965 }
966
967 error = gfs2_dir_del(odip, &odentry->d_name);
968 if (error)
969 goto out_end_trans;
970
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100971 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 if (error)
973 goto out_end_trans;
974
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400975out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400977out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978 if (alloc_required)
979 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400980out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 if (alloc_required)
982 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400983out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000984 if (alloc_required)
985 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400986out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500987 while (x--) {
988 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -0500990 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400991out_gunlock_r:
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100992 if (r_gh.gh_gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400994out:
Bob Peterson462903412010-09-30 10:34:00 -0400995 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 return error;
997}
998
999/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001000 * gfs2_follow_link - Follow a symbolic link
1001 * @dentry: The dentry of the link
1002 * @nd: Data that we pass to vfs_follow_link()
1003 *
Al Viroc177c2a2010-01-14 00:59:16 -05001004 * This can handle symlinks of any size.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005 *
1006 * Returns: 0 on success or error code
1007 */
1008
1009static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
1010{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001011 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Al Viroc177c2a2010-01-14 00:59:16 -05001012 struct gfs2_holder i_gh;
1013 struct buffer_head *dibh;
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001014 unsigned int x, size;
Al Viroc177c2a2010-01-14 00:59:16 -05001015 char *buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 int error;
1017
Al Viroc177c2a2010-01-14 00:59:16 -05001018 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1019 error = gfs2_glock_nq(&i_gh);
1020 if (error) {
1021 gfs2_holder_uninit(&i_gh);
1022 nd_set_link(nd, ERR_PTR(error));
1023 return NULL;
1024 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001026 size = (unsigned int)i_size_read(&ip->i_inode);
1027 if (size == 0) {
Al Viroc177c2a2010-01-14 00:59:16 -05001028 gfs2_consist_inode(ip);
1029 buf = ERR_PTR(-EIO);
1030 goto out;
1031 }
1032
1033 error = gfs2_meta_inode_buffer(ip, &dibh);
1034 if (error) {
1035 buf = ERR_PTR(error);
1036 goto out;
1037 }
1038
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001039 x = size + 1;
Al Viroc177c2a2010-01-14 00:59:16 -05001040 buf = kmalloc(x, GFP_NOFS);
1041 if (!buf)
1042 buf = ERR_PTR(-ENOMEM);
1043 else
1044 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1045 brelse(dibh);
1046out:
1047 gfs2_glock_dq_uninit(&i_gh);
1048 nd_set_link(nd, buf);
1049 return NULL;
1050}
1051
1052static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1053{
1054 char *s = nd_get_link(nd);
1055 if (!IS_ERR(s))
1056 kfree(s);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001057}
1058
1059/**
1060 * gfs2_permission -
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +00001061 * @inode: The inode
1062 * @mask: The mask to be tested
1063 * @flags: Indicates whether this is an RCU path walk or not
David Teiglandb3b94fa2006-01-16 16:50:04 +00001064 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001065 * This may be called from the VFS directly, or from within GFS2 with the
1066 * inode locked, so we look to see if the glock is already locked and only
1067 * lock the glock if its not already been done.
1068 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069 * Returns: errno
1070 */
1071
Nick Pigginb74c79e2011-01-07 17:49:58 +11001072int gfs2_permission(struct inode *inode, int mask, unsigned int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001073{
Nick Pigginb74c79e2011-01-07 17:49:58 +11001074 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075 struct gfs2_holder i_gh;
1076 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001077 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001078
Nick Pigginb74c79e2011-01-07 17:49:58 +11001079
1080 ip = GFS2_I(inode);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001081 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +00001082 if (flags & IPERM_FLAG_RCU)
1083 return -ECHILD;
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001084 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1085 if (error)
1086 return error;
1087 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088 }
1089
Miklos Szeredif58ba882008-07-02 21:12:01 +02001090 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1091 error = -EACCES;
1092 else
Nick Pigginb74c79e2011-01-07 17:49:58 +11001093 error = generic_permission(inode, mask, flags, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001094 if (unlock)
1095 gfs2_glock_dq_uninit(&i_gh);
1096
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 return error;
1098}
1099
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100static int setattr_chown(struct inode *inode, struct iattr *attr)
1101{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001102 struct gfs2_inode *ip = GFS2_I(inode);
1103 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001104 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001105 int error;
1106
Steven Whitehouse2933f922006-11-01 13:23:29 -05001107 ouid = inode->i_uid;
1108 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001109 nuid = attr->ia_uid;
1110 ngid = attr->ia_gid;
1111
1112 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1113 ouid = nuid = NO_QUOTA_CHANGE;
1114 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1115 ogid = ngid = NO_QUOTA_CHANGE;
1116
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001117 if (!gfs2_alloc_get(ip))
1118 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119
1120 error = gfs2_quota_lock(ip, nuid, ngid);
1121 if (error)
1122 goto out_alloc;
1123
1124 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1125 error = gfs2_quota_check(ip, nuid, ngid);
1126 if (error)
1127 goto out_gunlock_q;
1128 }
1129
1130 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1131 if (error)
1132 goto out_gunlock_q;
1133
Steven Whitehouse2ae51ed2010-11-10 15:14:57 +00001134 error = gfs2_setattr_simple(ip, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001135 if (error)
1136 goto out_end_trans;
1137
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001139 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1140 gfs2_quota_change(ip, -blocks, ouid, ogid);
1141 gfs2_quota_change(ip, blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142 }
1143
Steven Whitehousea91ea692006-09-04 12:04:26 -04001144out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001145 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001146out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001148out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001149 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001150 return error;
1151}
1152
1153/**
1154 * gfs2_setattr - Change attributes on an inode
1155 * @dentry: The dentry which is changing
1156 * @attr: The structure describing the change
1157 *
1158 * The VFS layer wants to change one or more of an inodes attributes. Write
1159 * that change out to disk.
1160 *
1161 * Returns: errno
1162 */
1163
1164static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1165{
1166 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001167 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001168 struct gfs2_holder i_gh;
1169 int error;
1170
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1172 if (error)
1173 return error;
1174
1175 error = -EPERM;
1176 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1177 goto out;
1178
1179 error = inode_change_ok(inode, attr);
1180 if (error)
1181 goto out;
1182
1183 if (attr->ia_valid & ATTR_SIZE)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001184 error = gfs2_setattr_size(inode, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001185 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1186 error = setattr_chown(inode, attr);
1187 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1188 error = gfs2_acl_chmod(ip, attr);
1189 else
1190 error = gfs2_setattr_simple(ip, attr);
1191
Steven Whitehousea91ea692006-09-04 12:04:26 -04001192out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001194 if (!error)
1195 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001196 return error;
1197}
1198
1199/**
1200 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001201 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001202 * @dentry: The dentry to stat
1203 * @stat: The inode's stats
1204 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001205 * This may be called from the VFS directly, or from within GFS2 with the
1206 * inode locked, so we look to see if the glock is already locked and only
1207 * lock the glock if its not already been done. Note that its the NFS
1208 * readdirplus operation which causes this to be called (from filldir)
1209 * with the glock already held.
1210 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001211 * Returns: errno
1212 */
1213
1214static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1215 struct kstat *stat)
1216{
1217 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001218 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001219 struct gfs2_holder gh;
1220 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001221 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001222
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001223 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001224 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1225 if (error)
1226 return error;
1227 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001228 }
1229
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001230 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001231 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001232 gfs2_glock_dq_uninit(&gh);
1233
1234 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001235}
1236
1237static int gfs2_setxattr(struct dentry *dentry, const char *name,
1238 const void *data, size_t size, int flags)
1239{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001240 struct inode *inode = dentry->d_inode;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001241 struct gfs2_inode *ip = GFS2_I(inode);
1242 struct gfs2_holder gh;
1243 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001245 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1246 ret = gfs2_glock_nq(&gh);
1247 if (ret == 0) {
1248 ret = generic_setxattr(dentry, name, data, size, flags);
1249 gfs2_glock_dq(&gh);
1250 }
1251 gfs2_holder_uninit(&gh);
1252 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001253}
1254
1255static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1256 void *data, size_t size)
1257{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001258 struct inode *inode = dentry->d_inode;
1259 struct gfs2_inode *ip = GFS2_I(inode);
1260 struct gfs2_holder gh;
1261 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001262
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001263 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1264 ret = gfs2_glock_nq(&gh);
1265 if (ret == 0) {
1266 ret = generic_getxattr(dentry, name, data, size);
1267 gfs2_glock_dq(&gh);
1268 }
1269 gfs2_holder_uninit(&gh);
1270 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001271}
1272
1273static int gfs2_removexattr(struct dentry *dentry, const char *name)
1274{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001275 struct inode *inode = dentry->d_inode;
1276 struct gfs2_inode *ip = GFS2_I(inode);
1277 struct gfs2_holder gh;
1278 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001280 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1281 ret = gfs2_glock_nq(&gh);
1282 if (ret == 0) {
1283 ret = generic_removexattr(dentry, name);
1284 gfs2_glock_dq(&gh);
1285 }
1286 gfs2_holder_uninit(&gh);
1287 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001288}
1289
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001290static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1291 u64 start, u64 len)
1292{
1293 struct gfs2_inode *ip = GFS2_I(inode);
1294 struct gfs2_holder gh;
1295 int ret;
1296
1297 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1298 if (ret)
1299 return ret;
1300
1301 mutex_lock(&inode->i_mutex);
1302
1303 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1304 if (ret)
1305 goto out;
1306
1307 if (gfs2_is_stuffed(ip)) {
1308 u64 phys = ip->i_no_addr << inode->i_blkbits;
1309 u64 size = i_size_read(inode);
1310 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1311 FIEMAP_EXTENT_DATA_INLINE;
1312 phys += sizeof(struct gfs2_dinode);
1313 phys += start;
1314 if (start + len > size)
1315 len = size - start;
1316 if (start < size)
1317 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1318 len, flags);
1319 if (ret == 1)
1320 ret = 0;
1321 } else {
1322 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1323 gfs2_block_map);
1324 }
1325
1326 gfs2_glock_dq_uninit(&gh);
1327out:
1328 mutex_unlock(&inode->i_mutex);
1329 return ret;
1330}
1331
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001332const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04001333 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001334 .setattr = gfs2_setattr,
1335 .getattr = gfs2_getattr,
1336 .setxattr = gfs2_setxattr,
1337 .getxattr = gfs2_getxattr,
1338 .listxattr = gfs2_listxattr,
1339 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001340 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001341};
1342
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001343const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001344 .create = gfs2_create,
1345 .lookup = gfs2_lookup,
1346 .link = gfs2_link,
1347 .unlink = gfs2_unlink,
1348 .symlink = gfs2_symlink,
1349 .mkdir = gfs2_mkdir,
1350 .rmdir = gfs2_rmdir,
1351 .mknod = gfs2_mknod,
1352 .rename = gfs2_rename,
Al Viroe6305c42008-07-15 21:03:57 -04001353 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354 .setattr = gfs2_setattr,
1355 .getattr = gfs2_getattr,
1356 .setxattr = gfs2_setxattr,
1357 .getxattr = gfs2_getxattr,
1358 .listxattr = gfs2_listxattr,
1359 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001360 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001361};
1362
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001363const struct inode_operations gfs2_symlink_iops = {
Al Viroc177c2a2010-01-14 00:59:16 -05001364 .readlink = generic_readlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001365 .follow_link = gfs2_follow_link,
Al Viroc177c2a2010-01-14 00:59:16 -05001366 .put_link = gfs2_put_link,
Al Viroe6305c42008-07-15 21:03:57 -04001367 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001368 .setattr = gfs2_setattr,
1369 .getattr = gfs2_getattr,
1370 .setxattr = gfs2_setxattr,
1371 .getxattr = gfs2_getxattr,
1372 .listxattr = gfs2_listxattr,
1373 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001374 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001375};
1376