blob: 247436c10deb853a35f4daf7db90cf18de96099a [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
Steven Whitehousec7526662006-03-20 12:30:04 -0500107 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108
Al Viroa569c712008-07-23 14:42:05 -0400109 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Steven Whitehousec7526662006-03-20 12:30:04 -0500110 if (inode && IS_ERR(inode))
David Howellse231c2e2008-02-07 00:15:26 -0800111 return ERR_CAST(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000113 if (inode) {
114 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
115 struct gfs2_holder gh;
116 int error;
117 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
118 if (error) {
119 iput(inode);
120 return ERR_PTR(error);
121 }
122 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123 return d_splice_alias(inode, dentry);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000124 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125 d_add(dentry, inode);
126
127 return NULL;
128}
129
130/**
131 * gfs2_link - Link to a file
132 * @old_dentry: The inode to link
133 * @dir: Add link to this directory
134 * @dentry: The name of the link
135 *
136 * Link the inode in "old_dentry" into the directory "dir" with the
137 * name in "dentry".
138 *
139 * Returns: errno
140 */
141
142static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
143 struct dentry *dentry)
144{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400145 struct gfs2_inode *dip = GFS2_I(dir);
146 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 struct inode *inode = old_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400148 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000149 struct gfs2_holder ghs[2];
150 int alloc_required;
151 int error;
152
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500153 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 return -EPERM;
155
156 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
157 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
158
Bob Peterson72dbf472008-08-12 13:39:29 -0500159 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500161 goto out_parent;
162
163 error = gfs2_glock_nq(ghs + 1); /* child */
164 if (error)
165 goto out_child;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166
Miklos Szeredif58ba882008-07-02 21:12:01 +0200167 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168 if (error)
169 goto out_gunlock;
170
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100171 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 switch (error) {
173 case -ENOENT:
174 break;
175 case 0:
176 error = -EEXIST;
177 default:
178 goto out_gunlock;
179 }
180
181 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500182 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183 goto out_gunlock;
184 error = -EFBIG;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000185 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000186 goto out_gunlock;
187 error = -EPERM;
188 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
189 goto out_gunlock;
190 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500191 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000192 goto out_gunlock;
193 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500194 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195 goto out_gunlock;
196
Steven Whitehousec7526662006-03-20 12:30:04 -0500197 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
198 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500200 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000201
202 if (alloc_required) {
203 struct gfs2_alloc *al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300204 if (!al) {
205 error = -ENOMEM;
206 goto out_gunlock;
207 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208
Steven Whitehoused82661d2008-03-10 15:34:50 +0000209 error = gfs2_quota_lock_check(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210 if (error)
211 goto out_alloc;
212
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213 al->al_requested = sdp->sd_max_dirres;
214
215 error = gfs2_inplace_reserve(dip);
216 if (error)
217 goto out_gunlock_q;
218
Steven Whitehouse1b502592006-05-18 14:10:52 -0400219 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100220 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000221 2 * RES_DINODE + RES_STATFS +
222 RES_QUOTA, 0);
223 if (error)
224 goto out_ipres;
225 } else {
226 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
227 if (error)
228 goto out_ipres;
229 }
230
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100231 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232 if (error)
233 goto out_end_trans;
234
235 error = gfs2_change_nlink(ip, +1);
236
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400237out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400239out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 if (alloc_required)
241 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400242out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243 if (alloc_required)
244 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400245out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246 if (alloc_required)
247 gfs2_alloc_put(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400248out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500249 gfs2_glock_dq(ghs + 1);
250out_child:
251 gfs2_glock_dq(ghs);
252out_parent:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253 gfs2_holder_uninit(ghs);
254 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000255 if (!error) {
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400256 atomic_inc(&inode->i_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257 d_instantiate(dentry, inode);
258 mark_inode_dirty(inode);
259 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260 return error;
261}
262
Steven Whitehouse87ec2172009-05-22 10:54:50 +0100263/*
264 * gfs2_unlink_ok - check to see that a inode is still in a directory
265 * @dip: the directory
266 * @name: the name of the file
267 * @ip: the inode
268 *
269 * Assumes that the lock on (at least) @dip is held.
270 *
271 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
272 */
273
274static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
275 const struct gfs2_inode *ip)
276{
277 int error;
278
279 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
280 return -EPERM;
281
282 if ((dip->i_inode.i_mode & S_ISVTX) &&
283 dip->i_inode.i_uid != current_fsuid() &&
284 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
285 return -EPERM;
286
287 if (IS_APPEND(&dip->i_inode))
288 return -EPERM;
289
290 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
291 if (error)
292 return error;
293
294 error = gfs2_dir_check(&dip->i_inode, name, ip);
295 if (error)
296 return error;
297
298 return 0;
299}
300
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301/**
302 * gfs2_unlink - Unlink a file
303 * @dir: The inode of the directory containing the file to unlink
304 * @dentry: The file itself
305 *
306 * Unlink a file. Call gfs2_unlinki()
307 *
308 * Returns: errno
309 */
310
311static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
312{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400313 struct gfs2_inode *dip = GFS2_I(dir);
314 struct gfs2_sbd *sdp = GFS2_SB(dir);
315 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600316 struct gfs2_holder ghs[3];
317 struct gfs2_rgrpd *rgd;
318 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319 int error;
320
Russell Cattelanddee7602007-01-29 17:13:44 -0600321 error = gfs2_rindex_hold(sdp, &ri_gh);
322 if (error)
323 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000324
Russell Cattelanddee7602007-01-29 17:13:44 -0600325 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
326 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
327
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100328 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600329 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
330
331
Steven Whitehouse8497a462007-08-26 14:23:56 +0100332 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100334 goto out_parent;
335
336 error = gfs2_glock_nq(ghs + 1); /* child */
337 if (error)
338 goto out_child;
339
340 error = gfs2_glock_nq(ghs + 2); /* rgrp */
341 if (error)
342 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343
344 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
345 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500346 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400348 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000349 if (error)
Roel Kluincd012072009-08-22 19:26:42 +0200350 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400352 error = gfs2_dir_del(dip, &dentry->d_name);
353 if (error)
354 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000355
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400356 error = gfs2_change_nlink(ip, -1);
357
358out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000359 gfs2_trans_end(sdp);
Bob Peterson72dbf472008-08-12 13:39:29 -0500360out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +0100361 gfs2_glock_dq(ghs + 2);
362out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600363 gfs2_holder_uninit(ghs + 2);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100364 gfs2_glock_dq(ghs + 1);
365out_child:
366 gfs2_holder_uninit(ghs + 1);
367 gfs2_glock_dq(ghs);
368out_parent:
369 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600370 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371 return error;
372}
373
374/**
375 * gfs2_symlink - Create a symlink
376 * @dir: The directory to create the symlink in
377 * @dentry: The dentry to put the symlink in
378 * @symname: The thing which the link points to
379 *
380 * Returns: errno
381 */
382
383static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
384 const char *symname)
385{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400386 struct gfs2_inode *dip = GFS2_I(dir), *ip;
387 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388 struct gfs2_holder ghs[2];
389 struct inode *inode;
390 struct buffer_head *dibh;
391 int size;
392 int error;
393
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394 /* Must be stuffed with a null terminator for gfs2_follow_link() */
395 size = strlen(symname);
396 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
397 return -ENAMETOOLONG;
398
399 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
400
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500401 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000402 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000403 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000404 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405 }
406
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500407 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408
Steven Whitehousec9e98882008-11-04 09:47:33 +0000409 ip->i_disksize = size;
Steven Whitehouse5cf32522009-03-31 16:06:27 +0100410 i_size_write(inode, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411
412 error = gfs2_meta_inode_buffer(ip, &dibh);
413
414 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500415 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
417 size);
418 brelse(dibh);
419 }
420
421 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000422 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 gfs2_inplace_release(dip);
424 gfs2_quota_unlock(dip);
425 gfs2_alloc_put(dip);
426
427 gfs2_glock_dq_uninit_m(2, ghs);
428
David Teiglandb3b94fa2006-01-16 16:50:04 +0000429 d_instantiate(dentry, inode);
430 mark_inode_dirty(inode);
431
432 return 0;
433}
434
435/**
436 * gfs2_mkdir - Make a directory
437 * @dir: The parent directory of the new one
438 * @dentry: The dentry of the new directory
439 * @mode: The mode of the new directory
440 *
441 * Returns: errno
442 */
443
444static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
445{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400446 struct gfs2_inode *dip = GFS2_I(dir), *ip;
447 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000448 struct gfs2_holder ghs[2];
449 struct inode *inode;
450 struct buffer_head *dibh;
451 int error;
452
David Teiglandb3b94fa2006-01-16 16:50:04 +0000453 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
454
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500455 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000456 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000458 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459 }
460
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500461 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000462
Steven Whitehouse4f561102006-11-01 14:04:17 -0500463 ip->i_inode.i_nlink = 2;
Steven Whitehousec9e98882008-11-04 09:47:33 +0000464 ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000465 ip->i_diskflags |= GFS2_DIF_JDATA;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000466 ip->i_entries = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467
468 error = gfs2_meta_inode_buffer(ip, &dibh);
469
470 if (!gfs2_assert_withdraw(sdp, !error)) {
471 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500472 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500473 struct qstr str;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000474
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500475 gfs2_str2qstr(&str, ".");
Steven Whitehousec7526662006-03-20 12:30:04 -0500476 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
477 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000478 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400479 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 di->di_entries = cpu_to_be32(1);
481
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500482 gfs2_str2qstr(&str, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500483 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
484 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000485
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100486 gfs2_inum_out(dip, dent);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400487 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500489 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490
491 brelse(dibh);
492 }
493
494 error = gfs2_change_nlink(dip, +1);
495 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
496
497 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000498 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 gfs2_inplace_release(dip);
500 gfs2_quota_unlock(dip);
501 gfs2_alloc_put(dip);
502
503 gfs2_glock_dq_uninit_m(2, ghs);
504
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 d_instantiate(dentry, inode);
506 mark_inode_dirty(inode);
507
508 return 0;
509}
510
511/**
Steven Whitehouse2286dbf2009-05-22 10:45:09 +0100512 * gfs2_rmdiri - Remove a directory
513 * @dip: The parent directory of the directory to be removed
514 * @name: The name of the directory to be removed
515 * @ip: The GFS2 inode of the directory to be removed
516 *
517 * Assumes Glocks on dip and ip are held
518 *
519 * Returns: errno
520 */
521
522static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
523 struct gfs2_inode *ip)
524{
525 struct qstr dotname;
526 int error;
527
528 if (ip->i_entries != 2) {
529 if (gfs2_consist_inode(ip))
530 gfs2_dinode_print(ip);
531 return -EIO;
532 }
533
534 error = gfs2_dir_del(dip, name);
535 if (error)
536 return error;
537
538 error = gfs2_change_nlink(dip, -1);
539 if (error)
540 return error;
541
542 gfs2_str2qstr(&dotname, ".");
543 error = gfs2_dir_del(ip, &dotname);
544 if (error)
545 return error;
546
547 gfs2_str2qstr(&dotname, "..");
548 error = gfs2_dir_del(ip, &dotname);
549 if (error)
550 return error;
551
552 /* It looks odd, but it really should be done twice */
553 error = gfs2_change_nlink(ip, -1);
554 if (error)
555 return error;
556
557 error = gfs2_change_nlink(ip, -1);
558 if (error)
559 return error;
560
561 return error;
562}
563
564/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 * gfs2_rmdir - Remove a directory
566 * @dir: The parent directory of the directory to be removed
567 * @dentry: The dentry of the directory to remove
568 *
569 * Remove a directory. Call gfs2_rmdiri()
570 *
571 * Returns: errno
572 */
573
574static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
575{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400576 struct gfs2_inode *dip = GFS2_I(dir);
577 struct gfs2_sbd *sdp = GFS2_SB(dir);
578 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600579 struct gfs2_holder ghs[3];
580 struct gfs2_rgrpd *rgd;
581 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582 int error;
583
Russell Cattelanddee7602007-01-29 17:13:44 -0600584 error = gfs2_rindex_hold(sdp, &ri_gh);
585 if (error)
586 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
588 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
589
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100590 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600591 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
592
Bob Peterson72dbf472008-08-12 13:39:29 -0500593 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000594 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500595 goto out_parent;
596
597 error = gfs2_glock_nq(ghs + 1); /* child */
598 if (error)
599 goto out_child;
600
601 error = gfs2_glock_nq(ghs + 2); /* rgrp */
602 if (error)
603 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604
605 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
606 if (error)
607 goto out_gunlock;
608
Steven Whitehousead6203f2008-11-03 13:59:19 +0000609 if (ip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500611 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 error = -EIO;
613 goto out_gunlock;
614 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000615 if (ip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616 error = -ENOTEMPTY;
617 goto out_gunlock;
618 }
619
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400620 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621 if (error)
622 goto out_gunlock;
623
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400624 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625
626 gfs2_trans_end(sdp);
627
Steven Whitehousea91ea692006-09-04 12:04:26 -0400628out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500629 gfs2_glock_dq(ghs + 2);
630out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600631 gfs2_holder_uninit(ghs + 2);
Bob Peterson72dbf472008-08-12 13:39:29 -0500632 gfs2_glock_dq(ghs + 1);
633out_child:
634 gfs2_holder_uninit(ghs + 1);
635 gfs2_glock_dq(ghs);
636out_parent:
637 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600638 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639 return error;
640}
641
642/**
643 * gfs2_mknod - Make a special file
644 * @dir: The directory in which the special file will reside
645 * @dentry: The dentry of the special file
646 * @mode: The mode of the special file
647 * @rdev: The device specification of the special file
648 *
649 */
650
651static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
652 dev_t dev)
653{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500654 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400655 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656 struct gfs2_holder ghs[2];
657 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658
659 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
660
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500661 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000662 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000663 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000664 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000665 }
666
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000668 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669 gfs2_inplace_release(dip);
670 gfs2_quota_unlock(dip);
671 gfs2_alloc_put(dip);
672
673 gfs2_glock_dq_uninit_m(2, ghs);
674
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675 d_instantiate(dentry, inode);
676 mark_inode_dirty(inode);
677
678 return 0;
679}
680
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100681/*
682 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
683 * @this: move this
684 * @to: to here
685 *
686 * Follow @to back to the root and make sure we don't encounter @this
687 * Assumes we already hold the rename lock.
688 *
689 * Returns: errno
690 */
691
692static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
693{
694 struct inode *dir = &to->i_inode;
695 struct super_block *sb = dir->i_sb;
696 struct inode *tmp;
697 struct qstr dotdot;
698 int error = 0;
699
700 gfs2_str2qstr(&dotdot, "..");
701
702 igrab(dir);
703
704 for (;;) {
705 if (dir == &this->i_inode) {
706 error = -EINVAL;
707 break;
708 }
709 if (dir == sb->s_root->d_inode) {
710 error = 0;
711 break;
712 }
713
714 tmp = gfs2_lookupi(dir, &dotdot, 1);
715 if (IS_ERR(tmp)) {
716 error = PTR_ERR(tmp);
717 break;
718 }
719
720 iput(dir);
721 dir = tmp;
722 }
723
724 iput(dir);
725
726 return error;
727}
728
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729/**
730 * gfs2_rename - Rename a file
731 * @odir: Parent directory of old file name
732 * @odentry: The old dentry of the file
733 * @ndir: Parent directory of new file name
734 * @ndentry: The new dentry of the file
735 *
736 * Returns: errno
737 */
738
739static int gfs2_rename(struct inode *odir, struct dentry *odentry,
740 struct inode *ndir, struct dentry *ndentry)
741{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400742 struct gfs2_inode *odip = GFS2_I(odir);
743 struct gfs2_inode *ndip = GFS2_I(ndir);
744 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400746 struct gfs2_sbd *sdp = GFS2_SB(odir);
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100747 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
Russell Cattelanddee7602007-01-29 17:13:44 -0600748 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 unsigned int num_gh;
750 int dir_rename = 0;
751 int alloc_required;
752 unsigned int x;
753 int error;
754
David Teiglandb3b94fa2006-01-16 16:50:04 +0000755 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400756 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 if (ip == nip)
758 return 0;
759 }
760
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100762 if (odip != ndip) {
763 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
764 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765 if (error)
766 goto out;
767
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100768 if (S_ISDIR(ip->i_inode.i_mode)) {
769 dir_rename = 1;
770 /* don't move a dirctory into it's subdir */
771 error = gfs2_ok_to_move(ip, ndip);
772 if (error)
773 goto out_gunlock_r;
774 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000775 }
776
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400777 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400779 if (odip != ndip) {
780 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
781 num_gh++;
782 }
783 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
784 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400786 if (nip) {
787 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
788 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600789 /* grab the resource lock for unlink flag twiddling
790 * this is the case of the target file already existing
791 * so we unlink before doing the rename
792 */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100793 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600794 if (nrgd)
795 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400796 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797
Bob Peterson72dbf472008-08-12 13:39:29 -0500798 for (x = 0; x < num_gh; x++) {
799 error = gfs2_glock_nq(ghs + x);
800 if (error)
801 goto out_gunlock;
802 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000803
804 /* Check out the old directory */
805
806 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
807 if (error)
808 goto out_gunlock;
809
810 /* Check out the new directory */
811
812 if (nip) {
813 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
814 if (error)
815 goto out_gunlock;
816
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500817 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f2008-11-03 13:59:19 +0000818 if (nip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000819 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500820 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000821 error = -EIO;
822 goto out_gunlock;
823 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000824 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 error = -ENOTEMPTY;
826 goto out_gunlock;
827 }
828 }
829 } else {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200830 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 if (error)
832 goto out_gunlock;
833
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100834 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835 switch (error) {
836 case -ENOENT:
837 error = 0;
838 break;
839 case 0:
840 error = -EEXIST;
841 default:
842 goto out_gunlock;
843 };
844
845 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500846 if (!ndip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000847 error = -EINVAL;
848 goto out_gunlock;
849 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000850 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851 error = -EFBIG;
852 goto out_gunlock;
853 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500854 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500855 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 error = -EMLINK;
857 goto out_gunlock;
858 }
859 }
860 }
861
862 /* Check out the dir to be renamed */
863
864 if (dir_rename) {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200865 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866 if (error)
867 goto out_gunlock;
868 }
869
Steven Whitehousec7526662006-03-20 12:30:04 -0500870 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
871 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500873 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874
875 if (alloc_required) {
876 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300877 if (!al) {
878 error = -ENOMEM;
879 goto out_gunlock;
880 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881
Steven Whitehoused82661d2008-03-10 15:34:50 +0000882 error = gfs2_quota_lock_check(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883 if (error)
884 goto out_alloc;
885
David Teiglandb3b94fa2006-01-16 16:50:04 +0000886 al->al_requested = sdp->sd_max_dirres;
887
888 error = gfs2_inplace_reserve(ndip);
889 if (error)
890 goto out_gunlock_q;
891
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400892 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100893 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500895 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896 if (error)
897 goto out_ipreserv;
898 } else {
899 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500900 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000901 if (error)
902 goto out_gunlock;
903 }
904
905 /* Remove the target file, if it exists */
906
907 if (nip) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500908 if (S_ISDIR(nip->i_inode.i_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400909 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
910 else {
911 error = gfs2_dir_del(ndip, &ndentry->d_name);
912 if (error)
913 goto out_end_trans;
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500914 error = gfs2_change_nlink(nip, -1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400915 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000916 if (error)
917 goto out_end_trans;
918 }
919
920 if (dir_rename) {
921 struct qstr name;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500922 gfs2_str2qstr(&name, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923
924 error = gfs2_change_nlink(ndip, +1);
925 if (error)
926 goto out_end_trans;
927 error = gfs2_change_nlink(odip, -1);
928 if (error)
929 goto out_end_trans;
930
Steven Whitehouseffed8ab2007-06-07 11:29:35 +0100931 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 if (error)
933 goto out_end_trans;
934 } else {
935 struct buffer_head *dibh;
936 error = gfs2_meta_inode_buffer(ip, &dibh);
937 if (error)
938 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100939 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000940 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500941 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000942 brelse(dibh);
943 }
944
945 error = gfs2_dir_del(odip, &odentry->d_name);
946 if (error)
947 goto out_end_trans;
948
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100949 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950 if (error)
951 goto out_end_trans;
952
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400953out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400955out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956 if (alloc_required)
957 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400958out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000959 if (alloc_required)
960 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400961out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000962 if (alloc_required)
963 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400964out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500965 while (x--) {
966 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -0500968 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400969out_gunlock_r:
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100970 if (r_gh.gh_gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400972out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000973 return error;
974}
975
976/**
Steven Whitehouse536baf02009-05-22 10:48:59 +0100977 * gfs2_readlinki - return the contents of a symlink
978 * @ip: the symlink's inode
979 * @buf: a pointer to the buffer to be filled
980 * @len: a pointer to the length of @buf
981 *
982 * If @buf is too small, a piece of memory is kmalloc()ed and needs
983 * to be freed by the caller.
984 *
985 * Returns: errno
986 */
987
988static int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
989{
990 struct gfs2_holder i_gh;
991 struct buffer_head *dibh;
992 unsigned int x;
993 int error;
994
995 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
996 error = gfs2_glock_nq(&i_gh);
997 if (error) {
998 gfs2_holder_uninit(&i_gh);
999 return error;
1000 }
1001
1002 if (!ip->i_disksize) {
1003 gfs2_consist_inode(ip);
1004 error = -EIO;
1005 goto out;
1006 }
1007
1008 error = gfs2_meta_inode_buffer(ip, &dibh);
1009 if (error)
1010 goto out;
1011
1012 x = ip->i_disksize + 1;
1013 if (x > *len) {
1014 *buf = kmalloc(x, GFP_NOFS);
1015 if (!*buf) {
1016 error = -ENOMEM;
1017 goto out_brelse;
1018 }
1019 }
1020
1021 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1022 *len = x;
1023
1024out_brelse:
1025 brelse(dibh);
1026out:
1027 gfs2_glock_dq_uninit(&i_gh);
1028 return error;
1029}
1030
1031/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001032 * gfs2_readlink - Read the value of a symlink
1033 * @dentry: the symlink
1034 * @buf: the buffer to read the symlink data into
1035 * @size: the size of the buffer
1036 *
1037 * Returns: errno
1038 */
1039
1040static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
1041 int user_size)
1042{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001043 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044 char array[GFS2_FAST_NAME_SIZE], *buf = array;
1045 unsigned int len = GFS2_FAST_NAME_SIZE;
1046 int error;
1047
David Teiglandb3b94fa2006-01-16 16:50:04 +00001048 error = gfs2_readlinki(ip, &buf, &len);
1049 if (error)
1050 return error;
1051
1052 if (user_size > len - 1)
1053 user_size = len - 1;
1054
1055 if (copy_to_user(user_buf, buf, user_size))
1056 error = -EFAULT;
1057 else
1058 error = user_size;
1059
1060 if (buf != array)
1061 kfree(buf);
1062
1063 return error;
1064}
1065
1066/**
1067 * gfs2_follow_link - Follow a symbolic link
1068 * @dentry: The dentry of the link
1069 * @nd: Data that we pass to vfs_follow_link()
1070 *
1071 * This can handle symlinks of any size. It is optimised for symlinks
1072 * under GFS2_FAST_NAME_SIZE.
1073 *
1074 * Returns: 0 on success or error code
1075 */
1076
1077static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
1078{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001079 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001080 char array[GFS2_FAST_NAME_SIZE], *buf = array;
1081 unsigned int len = GFS2_FAST_NAME_SIZE;
1082 int error;
1083
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084 error = gfs2_readlinki(ip, &buf, &len);
1085 if (!error) {
1086 error = vfs_follow_link(nd, buf);
1087 if (buf != array)
1088 kfree(buf);
1089 }
1090
1091 return ERR_PTR(error);
1092}
1093
1094/**
1095 * gfs2_permission -
1096 * @inode:
1097 * @mask:
1098 * @nd: passed from Linux VFS, ignored by us
1099 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001100 * This may be called from the VFS directly, or from within GFS2 with the
1101 * inode locked, so we look to see if the glock is already locked and only
1102 * lock the glock if its not already been done.
1103 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001104 * Returns: errno
1105 */
1106
Miklos Szeredif58ba882008-07-02 21:12:01 +02001107int gfs2_permission(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001108{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001109 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001110 struct gfs2_holder i_gh;
1111 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001112 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001114 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001115 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1116 if (error)
1117 return error;
1118 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119 }
1120
Miklos Szeredif58ba882008-07-02 21:12:01 +02001121 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1122 error = -EACCES;
1123 else
1124 error = generic_permission(inode, mask, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001125 if (unlock)
1126 gfs2_glock_dq_uninit(&i_gh);
1127
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128 return error;
1129}
1130
1131static int setattr_size(struct inode *inode, struct iattr *attr)
1132{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001133 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse16615be2007-09-17 10:59:52 +01001134 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001135 int error;
1136
Steven Whitehousec9e98882008-11-04 09:47:33 +00001137 if (attr->ia_size != ip->i_disksize) {
Steven Whitehouse16615be2007-09-17 10:59:52 +01001138 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139 if (error)
1140 return error;
Steven Whitehouse16615be2007-09-17 10:59:52 +01001141 error = vmtruncate(inode, attr->ia_size);
1142 gfs2_trans_end(sdp);
1143 if (error)
1144 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001145 }
1146
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001147 error = gfs2_truncatei(ip, attr->ia_size);
Steven Whitehousec9e98882008-11-04 09:47:33 +00001148 if (error && (inode->i_size != ip->i_disksize))
1149 i_size_write(inode, ip->i_disksize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001150
1151 return error;
1152}
1153
1154static int setattr_chown(struct inode *inode, struct iattr *attr)
1155{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001156 struct gfs2_inode *ip = GFS2_I(inode);
1157 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001158 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001159 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160 int error;
1161
Steven Whitehouse2933f922006-11-01 13:23:29 -05001162 ouid = inode->i_uid;
1163 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164 nuid = attr->ia_uid;
1165 ngid = attr->ia_gid;
1166
1167 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1168 ouid = nuid = NO_QUOTA_CHANGE;
1169 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1170 ogid = ngid = NO_QUOTA_CHANGE;
1171
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001172 if (!gfs2_alloc_get(ip))
1173 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001174
1175 error = gfs2_quota_lock(ip, nuid, ngid);
1176 if (error)
1177 goto out_alloc;
1178
1179 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1180 error = gfs2_quota_check(ip, nuid, ngid);
1181 if (error)
1182 goto out_gunlock_q;
1183 }
1184
1185 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1186 if (error)
1187 goto out_gunlock_q;
1188
1189 error = gfs2_meta_inode_buffer(ip, &dibh);
1190 if (error)
1191 goto out_end_trans;
1192
1193 error = inode_setattr(inode, attr);
1194 gfs2_assert_warn(sdp, !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001196 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001197 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001198 brelse(dibh);
1199
1200 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001201 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1202 gfs2_quota_change(ip, -blocks, ouid, ogid);
1203 gfs2_quota_change(ip, blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204 }
1205
Steven Whitehousea91ea692006-09-04 12:04:26 -04001206out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001207 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001208out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001210out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001211 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212 return error;
1213}
1214
1215/**
1216 * gfs2_setattr - Change attributes on an inode
1217 * @dentry: The dentry which is changing
1218 * @attr: The structure describing the change
1219 *
1220 * The VFS layer wants to change one or more of an inodes attributes. Write
1221 * that change out to disk.
1222 *
1223 * Returns: errno
1224 */
1225
1226static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1227{
1228 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001229 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001230 struct gfs2_holder i_gh;
1231 int error;
1232
David Teiglandb3b94fa2006-01-16 16:50:04 +00001233 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1234 if (error)
1235 return error;
1236
1237 error = -EPERM;
1238 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1239 goto out;
1240
1241 error = inode_change_ok(inode, attr);
1242 if (error)
1243 goto out;
1244
1245 if (attr->ia_valid & ATTR_SIZE)
1246 error = setattr_size(inode, attr);
1247 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1248 error = setattr_chown(inode, attr);
1249 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1250 error = gfs2_acl_chmod(ip, attr);
1251 else
1252 error = gfs2_setattr_simple(ip, attr);
1253
Steven Whitehousea91ea692006-09-04 12:04:26 -04001254out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001255 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001256 if (!error)
1257 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001258 return error;
1259}
1260
1261/**
1262 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001263 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264 * @dentry: The dentry to stat
1265 * @stat: The inode's stats
1266 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001267 * This may be called from the VFS directly, or from within GFS2 with the
1268 * inode locked, so we look to see if the glock is already locked and only
1269 * lock the glock if its not already been done. Note that its the NFS
1270 * readdirplus operation which causes this to be called (from filldir)
1271 * with the glock already held.
1272 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001273 * Returns: errno
1274 */
1275
1276static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1277 struct kstat *stat)
1278{
1279 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001280 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281 struct gfs2_holder gh;
1282 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001283 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001284
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001285 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001286 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1287 if (error)
1288 return error;
1289 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001290 }
1291
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001292 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001293 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001294 gfs2_glock_dq_uninit(&gh);
1295
1296 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001297}
1298
1299static int gfs2_setxattr(struct dentry *dentry, const char *name,
1300 const void *data, size_t size, int flags)
1301{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001302 struct inode *inode = dentry->d_inode;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001303 struct gfs2_inode *ip = GFS2_I(inode);
1304 struct gfs2_holder gh;
1305 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001306
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001307 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1308 ret = gfs2_glock_nq(&gh);
1309 if (ret == 0) {
1310 ret = generic_setxattr(dentry, name, data, size, flags);
1311 gfs2_glock_dq(&gh);
1312 }
1313 gfs2_holder_uninit(&gh);
1314 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001315}
1316
1317static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1318 void *data, size_t size)
1319{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001320 struct inode *inode = dentry->d_inode;
1321 struct gfs2_inode *ip = GFS2_I(inode);
1322 struct gfs2_holder gh;
1323 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001324
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001325 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1326 ret = gfs2_glock_nq(&gh);
1327 if (ret == 0) {
1328 ret = generic_getxattr(dentry, name, data, size);
1329 gfs2_glock_dq(&gh);
1330 }
1331 gfs2_holder_uninit(&gh);
1332 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001333}
1334
1335static int gfs2_removexattr(struct dentry *dentry, const char *name)
1336{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001337 struct inode *inode = dentry->d_inode;
1338 struct gfs2_inode *ip = GFS2_I(inode);
1339 struct gfs2_holder gh;
1340 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001341
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001342 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1343 ret = gfs2_glock_nq(&gh);
1344 if (ret == 0) {
1345 ret = generic_removexattr(dentry, name);
1346 gfs2_glock_dq(&gh);
1347 }
1348 gfs2_holder_uninit(&gh);
1349 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001350}
1351
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001352static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1353 u64 start, u64 len)
1354{
1355 struct gfs2_inode *ip = GFS2_I(inode);
1356 struct gfs2_holder gh;
1357 int ret;
1358
1359 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1360 if (ret)
1361 return ret;
1362
1363 mutex_lock(&inode->i_mutex);
1364
1365 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1366 if (ret)
1367 goto out;
1368
1369 if (gfs2_is_stuffed(ip)) {
1370 u64 phys = ip->i_no_addr << inode->i_blkbits;
1371 u64 size = i_size_read(inode);
1372 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1373 FIEMAP_EXTENT_DATA_INLINE;
1374 phys += sizeof(struct gfs2_dinode);
1375 phys += start;
1376 if (start + len > size)
1377 len = size - start;
1378 if (start < size)
1379 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1380 len, flags);
1381 if (ret == 1)
1382 ret = 0;
1383 } else {
1384 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1385 gfs2_block_map);
1386 }
1387
1388 gfs2_glock_dq_uninit(&gh);
1389out:
1390 mutex_unlock(&inode->i_mutex);
1391 return ret;
1392}
1393
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001394const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04001395 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001396 .setattr = gfs2_setattr,
1397 .getattr = gfs2_getattr,
1398 .setxattr = gfs2_setxattr,
1399 .getxattr = gfs2_getxattr,
1400 .listxattr = gfs2_listxattr,
1401 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001402 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001403};
1404
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001405const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001406 .create = gfs2_create,
1407 .lookup = gfs2_lookup,
1408 .link = gfs2_link,
1409 .unlink = gfs2_unlink,
1410 .symlink = gfs2_symlink,
1411 .mkdir = gfs2_mkdir,
1412 .rmdir = gfs2_rmdir,
1413 .mknod = gfs2_mknod,
1414 .rename = gfs2_rename,
Al Viroe6305c42008-07-15 21:03:57 -04001415 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001416 .setattr = gfs2_setattr,
1417 .getattr = gfs2_getattr,
1418 .setxattr = gfs2_setxattr,
1419 .getxattr = gfs2_getxattr,
1420 .listxattr = gfs2_listxattr,
1421 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001422 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001423};
1424
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001425const struct inode_operations gfs2_symlink_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001426 .readlink = gfs2_readlink,
1427 .follow_link = gfs2_follow_link,
Al Viroe6305c42008-07-15 21:03:57 -04001428 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001429 .setattr = gfs2_setattr,
1430 .getattr = gfs2_getattr,
1431 .setxattr = gfs2_setxattr,
1432 .getxattr = gfs2_getxattr,
1433 .listxattr = gfs2_listxattr,
1434 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001435 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001436};
1437