]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - fs/gfs2/ops_inode.c
GFS2: Support for FIEMAP ioctl
[mirror_ubuntu-eoan-kernel.git] / fs / gfs2 / ops_inode.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
b3b94faa
DT
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
b3b94faa
DT
10#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>
15#include <linux/utsname.h>
16#include <linux/mm.h>
17#include <linux/xattr.h>
18#include <linux/posix_acl.h>
5c676f6d 19#include <linux/gfs2_ondisk.h>
71b86f56 20#include <linux/crc32.h>
7d308590 21#include <linux/lm_interface.h>
e9079cce 22#include <linux/fiemap.h>
b3b94faa
DT
23#include <asm/uaccess.h>
24
25#include "gfs2.h"
5c676f6d 26#include "incore.h"
b3b94faa
DT
27#include "acl.h"
28#include "bmap.h"
29#include "dir.h"
30#include "eaops.h"
31#include "eattr.h"
32#include "glock.h"
33#include "inode.h"
34#include "meta_io.h"
35#include "ops_dentry.h"
36#include "ops_inode.h"
b3b94faa
DT
37#include "quota.h"
38#include "rgrp.h"
39#include "trans.h"
5c676f6d 40#include "util.h"
b3b94faa
DT
41
42/**
43 * gfs2_create - Create a file
44 * @dir: The directory in which to create the file
45 * @dentry: The dentry of the new file
46 * @mode: The mode of the new file
47 *
48 * Returns: errno
49 */
50
51static int gfs2_create(struct inode *dir, struct dentry *dentry,
52 int mode, struct nameidata *nd)
53{
feaa7bba
SW
54 struct gfs2_inode *dip = GFS2_I(dir);
55 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
56 struct gfs2_holder ghs[2];
57 struct inode *inode;
b3b94faa 58
b3b94faa
DT
59 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
60
61 for (;;) {
e7f14f4d 62 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
7359a19c 63 if (!IS_ERR(inode)) {
b3b94faa 64 gfs2_trans_end(sdp);
6dbd8224 65 if (dip->i_alloc->al_rgd)
b3b94faa
DT
66 gfs2_inplace_release(dip);
67 gfs2_quota_unlock(dip);
68 gfs2_alloc_put(dip);
69 gfs2_glock_dq_uninit_m(2, ghs);
3a8476dd 70 mark_inode_dirty(inode);
b3b94faa 71 break;
7359a19c 72 } else if (PTR_ERR(inode) != -EEXIST ||
3516586a 73 (nd && nd->flags & LOOKUP_EXCL)) {
b3b94faa 74 gfs2_holder_uninit(ghs);
7359a19c 75 return PTR_ERR(inode);
b3b94faa
DT
76 }
77
a569c711 78 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
c752666c
SW
79 if (inode) {
80 if (!IS_ERR(inode)) {
c752666c
SW
81 gfs2_holder_uninit(ghs);
82 break;
83 } else {
84 gfs2_holder_uninit(ghs);
85 return PTR_ERR(inode);
86 }
b3b94faa
DT
87 }
88 }
89
b3b94faa 90 d_instantiate(dentry, inode);
b3b94faa
DT
91
92 return 0;
93}
94
95/**
96 * gfs2_lookup - Look up a filename in a directory and return its inode
97 * @dir: The directory inode
98 * @dentry: The dentry of the new inode
99 * @nd: passed from Linux VFS, ignored by us
100 *
101 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
102 *
103 * Returns: errno
104 */
105
106static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
107 struct nameidata *nd)
108{
b3b94faa 109 struct inode *inode = NULL;
b3b94faa 110
c752666c 111 dentry->d_op = &gfs2_dops;
b3b94faa 112
a569c711 113 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
c752666c 114 if (inode && IS_ERR(inode))
e231c2ee 115 return ERR_CAST(inode);
b3b94faa 116
9656b2c1
SW
117 if (inode) {
118 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
119 struct gfs2_holder gh;
120 int error;
121 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
122 if (error) {
123 iput(inode);
124 return ERR_PTR(error);
125 }
126 gfs2_glock_dq_uninit(&gh);
b3b94faa 127 return d_splice_alias(inode, dentry);
9656b2c1 128 }
b3b94faa
DT
129 d_add(dentry, inode);
130
131 return NULL;
132}
133
134/**
135 * gfs2_link - Link to a file
136 * @old_dentry: The inode to link
137 * @dir: Add link to this directory
138 * @dentry: The name of the link
139 *
140 * Link the inode in "old_dentry" into the directory "dir" with the
141 * name in "dentry".
142 *
143 * Returns: errno
144 */
145
146static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
147 struct dentry *dentry)
148{
feaa7bba
SW
149 struct gfs2_inode *dip = GFS2_I(dir);
150 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa 151 struct inode *inode = old_dentry->d_inode;
feaa7bba 152 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
153 struct gfs2_holder ghs[2];
154 int alloc_required;
155 int error;
156
b60623c2 157 if (S_ISDIR(inode->i_mode))
b3b94faa
DT
158 return -EPERM;
159
160 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
161 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
162
72dbf479
BP
163 error = gfs2_glock_nq(ghs); /* parent */
164 if (error)
165 goto out_parent;
166
167 error = gfs2_glock_nq(ghs + 1); /* child */
b3b94faa 168 if (error)
72dbf479 169 goto out_child;
b3b94faa 170
f58ba889 171 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
b3b94faa
DT
172 if (error)
173 goto out_gunlock;
174
dbb7cae2 175 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
b3b94faa
DT
176 switch (error) {
177 case -ENOENT:
178 break;
179 case 0:
180 error = -EEXIST;
181 default:
182 goto out_gunlock;
183 }
184
185 error = -EINVAL;
4f56110a 186 if (!dip->i_inode.i_nlink)
b3b94faa
DT
187 goto out_gunlock;
188 error = -EFBIG;
cd915493 189 if (dip->i_di.di_entries == (u32)-1)
b3b94faa
DT
190 goto out_gunlock;
191 error = -EPERM;
192 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
193 goto out_gunlock;
194 error = -EINVAL;
4f56110a 195 if (!ip->i_inode.i_nlink)
b3b94faa
DT
196 goto out_gunlock;
197 error = -EMLINK;
4f56110a 198 if (ip->i_inode.i_nlink == (u32)-1)
b3b94faa
DT
199 goto out_gunlock;
200
c752666c
SW
201 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
202 if (error < 0)
b3b94faa 203 goto out_gunlock;
c752666c 204 error = 0;
b3b94faa
DT
205
206 if (alloc_required) {
207 struct gfs2_alloc *al = gfs2_alloc_get(dip);
182fe5ab
CG
208 if (!al) {
209 error = -ENOMEM;
210 goto out_gunlock;
211 }
b3b94faa 212
d82661d9 213 error = gfs2_quota_lock_check(dip);
b3b94faa
DT
214 if (error)
215 goto out_alloc;
216
b3b94faa
DT
217 al->al_requested = sdp->sd_max_dirres;
218
219 error = gfs2_inplace_reserve(dip);
220 if (error)
221 goto out_gunlock_q;
222
1b50259b 223 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
bb8d8a6f 224 al->al_rgd->rd_length +
b3b94faa
DT
225 2 * RES_DINODE + RES_STATFS +
226 RES_QUOTA, 0);
227 if (error)
228 goto out_ipres;
229 } else {
230 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
231 if (error)
232 goto out_ipres;
233 }
234
dbb7cae2 235 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
b3b94faa
DT
236 if (error)
237 goto out_end_trans;
238
239 error = gfs2_change_nlink(ip, +1);
240
feaa7bba 241out_end_trans:
b3b94faa 242 gfs2_trans_end(sdp);
feaa7bba 243out_ipres:
b3b94faa
DT
244 if (alloc_required)
245 gfs2_inplace_release(dip);
feaa7bba 246out_gunlock_q:
b3b94faa
DT
247 if (alloc_required)
248 gfs2_quota_unlock(dip);
feaa7bba 249out_alloc:
b3b94faa
DT
250 if (alloc_required)
251 gfs2_alloc_put(dip);
feaa7bba 252out_gunlock:
72dbf479
BP
253 gfs2_glock_dq(ghs + 1);
254out_child:
255 gfs2_glock_dq(ghs);
256out_parent:
b3b94faa
DT
257 gfs2_holder_uninit(ghs);
258 gfs2_holder_uninit(ghs + 1);
b3b94faa 259 if (!error) {
29937ac6 260 atomic_inc(&inode->i_count);
b3b94faa
DT
261 d_instantiate(dentry, inode);
262 mark_inode_dirty(inode);
263 }
b3b94faa
DT
264 return error;
265}
266
267/**
268 * gfs2_unlink - Unlink a file
269 * @dir: The inode of the directory containing the file to unlink
270 * @dentry: The file itself
271 *
272 * Unlink a file. Call gfs2_unlinki()
273 *
274 * Returns: errno
275 */
276
277static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
278{
feaa7bba
SW
279 struct gfs2_inode *dip = GFS2_I(dir);
280 struct gfs2_sbd *sdp = GFS2_SB(dir);
281 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
ddee7608
RC
282 struct gfs2_holder ghs[3];
283 struct gfs2_rgrpd *rgd;
284 struct gfs2_holder ri_gh;
b3b94faa
DT
285 int error;
286
ddee7608
RC
287 error = gfs2_rindex_hold(sdp, &ri_gh);
288 if (error)
289 return error;
290
b3b94faa 291 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
ddee7608 292 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
b3b94faa 293
dbb7cae2 294 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
ddee7608
RC
295 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
296
297
8497a46e 298 error = gfs2_glock_nq(ghs); /* parent */
b3b94faa 299 if (error)
8497a46e
SW
300 goto out_parent;
301
302 error = gfs2_glock_nq(ghs + 1); /* child */
303 if (error)
304 goto out_child;
305
306 error = gfs2_glock_nq(ghs + 2); /* rgrp */
307 if (error)
308 goto out_rgrp;
b3b94faa
DT
309
310 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
311 if (error)
72dbf479 312 goto out_gunlock;
b3b94faa 313
feaa7bba 314 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
b3b94faa 315 if (error)
8497a46e 316 goto out_rgrp;
b3b94faa 317
feaa7bba
SW
318 error = gfs2_dir_del(dip, &dentry->d_name);
319 if (error)
320 goto out_end_trans;
b3b94faa 321
feaa7bba 322 error = gfs2_change_nlink(ip, -1);
b3b94faa 323
feaa7bba
SW
324out_end_trans:
325 gfs2_trans_end(sdp);
72dbf479 326out_gunlock:
8497a46e
SW
327 gfs2_glock_dq(ghs + 2);
328out_rgrp:
ddee7608 329 gfs2_holder_uninit(ghs + 2);
8497a46e
SW
330 gfs2_glock_dq(ghs + 1);
331out_child:
332 gfs2_holder_uninit(ghs + 1);
333 gfs2_glock_dq(ghs);
334out_parent:
335 gfs2_holder_uninit(ghs);
ddee7608 336 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
337 return error;
338}
339
340/**
341 * gfs2_symlink - Create a symlink
342 * @dir: The directory to create the symlink in
343 * @dentry: The dentry to put the symlink in
344 * @symname: The thing which the link points to
345 *
346 * Returns: errno
347 */
348
349static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
350 const char *symname)
351{
feaa7bba
SW
352 struct gfs2_inode *dip = GFS2_I(dir), *ip;
353 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
354 struct gfs2_holder ghs[2];
355 struct inode *inode;
356 struct buffer_head *dibh;
357 int size;
358 int error;
359
b3b94faa
DT
360 /* Must be stuffed with a null terminator for gfs2_follow_link() */
361 size = strlen(symname);
362 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
363 return -ENAMETOOLONG;
364
365 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
366
e7f14f4d 367 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
7359a19c 368 if (IS_ERR(inode)) {
b3b94faa 369 gfs2_holder_uninit(ghs);
7359a19c 370 return PTR_ERR(inode);
b3b94faa
DT
371 }
372
5c676f6d 373 ip = ghs[1].gh_gl->gl_object;
b3b94faa
DT
374
375 ip->i_di.di_size = size;
376
377 error = gfs2_meta_inode_buffer(ip, &dibh);
378
379 if (!gfs2_assert_withdraw(sdp, !error)) {
539e5d6b 380 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
381 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
382 size);
383 brelse(dibh);
384 }
385
386 gfs2_trans_end(sdp);
6dbd8224 387 if (dip->i_alloc->al_rgd)
b3b94faa
DT
388 gfs2_inplace_release(dip);
389 gfs2_quota_unlock(dip);
390 gfs2_alloc_put(dip);
391
392 gfs2_glock_dq_uninit_m(2, ghs);
393
b3b94faa
DT
394 d_instantiate(dentry, inode);
395 mark_inode_dirty(inode);
396
397 return 0;
398}
399
400/**
401 * gfs2_mkdir - Make a directory
402 * @dir: The parent directory of the new one
403 * @dentry: The dentry of the new directory
404 * @mode: The mode of the new directory
405 *
406 * Returns: errno
407 */
408
409static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
410{
feaa7bba
SW
411 struct gfs2_inode *dip = GFS2_I(dir), *ip;
412 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
413 struct gfs2_holder ghs[2];
414 struct inode *inode;
415 struct buffer_head *dibh;
416 int error;
417
b3b94faa
DT
418 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
419
e7f14f4d 420 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
7359a19c 421 if (IS_ERR(inode)) {
b3b94faa 422 gfs2_holder_uninit(ghs);
7359a19c 423 return PTR_ERR(inode);
b3b94faa
DT
424 }
425
5c676f6d 426 ip = ghs[1].gh_gl->gl_object;
b3b94faa 427
4f56110a 428 ip->i_inode.i_nlink = 2;
b3b94faa
DT
429 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
430 ip->i_di.di_flags |= GFS2_DIF_JDATA;
b3b94faa
DT
431 ip->i_di.di_entries = 2;
432
433 error = gfs2_meta_inode_buffer(ip, &dibh);
434
435 if (!gfs2_assert_withdraw(sdp, !error)) {
436 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
c752666c 437 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
71b86f56 438 struct qstr str;
b3b94faa 439
71b86f56 440 gfs2_str2qstr(&str, ".");
c752666c
SW
441 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
442 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
b3b94faa 443 dent->de_inum = di->di_num; /* already GFS2 endian */
7ecdb70a 444 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa
DT
445 di->di_entries = cpu_to_be32(1);
446
71b86f56 447 gfs2_str2qstr(&str, "..");
c752666c
SW
448 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
449 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
b3b94faa 450
dbb7cae2 451 gfs2_inum_out(dip, dent);
7ecdb70a 452 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa 453
539e5d6b 454 gfs2_dinode_out(ip, di);
b3b94faa
DT
455
456 brelse(dibh);
457 }
458
459 error = gfs2_change_nlink(dip, +1);
460 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
461
462 gfs2_trans_end(sdp);
6dbd8224 463 if (dip->i_alloc->al_rgd)
b3b94faa
DT
464 gfs2_inplace_release(dip);
465 gfs2_quota_unlock(dip);
466 gfs2_alloc_put(dip);
467
468 gfs2_glock_dq_uninit_m(2, ghs);
469
b3b94faa
DT
470 d_instantiate(dentry, inode);
471 mark_inode_dirty(inode);
472
473 return 0;
474}
475
476/**
477 * gfs2_rmdir - Remove a directory
478 * @dir: The parent directory of the directory to be removed
479 * @dentry: The dentry of the directory to remove
480 *
481 * Remove a directory. Call gfs2_rmdiri()
482 *
483 * Returns: errno
484 */
485
486static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
487{
feaa7bba
SW
488 struct gfs2_inode *dip = GFS2_I(dir);
489 struct gfs2_sbd *sdp = GFS2_SB(dir);
490 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
ddee7608
RC
491 struct gfs2_holder ghs[3];
492 struct gfs2_rgrpd *rgd;
493 struct gfs2_holder ri_gh;
b3b94faa
DT
494 int error;
495
ddee7608
RC
496 error = gfs2_rindex_hold(sdp, &ri_gh);
497 if (error)
498 return error;
b3b94faa
DT
499 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
500 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
501
dbb7cae2 502 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
ddee7608
RC
503 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
504
72dbf479
BP
505 error = gfs2_glock_nq(ghs); /* parent */
506 if (error)
507 goto out_parent;
508
509 error = gfs2_glock_nq(ghs + 1); /* child */
b3b94faa 510 if (error)
72dbf479
BP
511 goto out_child;
512
513 error = gfs2_glock_nq(ghs + 2); /* rgrp */
514 if (error)
515 goto out_rgrp;
b3b94faa
DT
516
517 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
518 if (error)
519 goto out_gunlock;
520
521 if (ip->i_di.di_entries < 2) {
522 if (gfs2_consist_inode(ip))
4cc14f0b 523 gfs2_dinode_print(ip);
b3b94faa
DT
524 error = -EIO;
525 goto out_gunlock;
526 }
527 if (ip->i_di.di_entries > 2) {
528 error = -ENOTEMPTY;
529 goto out_gunlock;
530 }
531
feaa7bba 532 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
b3b94faa
DT
533 if (error)
534 goto out_gunlock;
535
feaa7bba 536 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
b3b94faa
DT
537
538 gfs2_trans_end(sdp);
539
a91ea69f 540out_gunlock:
72dbf479
BP
541 gfs2_glock_dq(ghs + 2);
542out_rgrp:
ddee7608 543 gfs2_holder_uninit(ghs + 2);
72dbf479
BP
544 gfs2_glock_dq(ghs + 1);
545out_child:
546 gfs2_holder_uninit(ghs + 1);
547 gfs2_glock_dq(ghs);
548out_parent:
549 gfs2_holder_uninit(ghs);
ddee7608 550 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
551 return error;
552}
553
554/**
555 * gfs2_mknod - Make a special file
556 * @dir: The directory in which the special file will reside
557 * @dentry: The dentry of the special file
558 * @mode: The mode of the special file
559 * @rdev: The device specification of the special file
560 *
561 */
562
563static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
564 dev_t dev)
565{
e7f14f4d 566 struct gfs2_inode *dip = GFS2_I(dir);
feaa7bba 567 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
568 struct gfs2_holder ghs[2];
569 struct inode *inode;
b3b94faa
DT
570
571 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
572
e7f14f4d 573 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
7359a19c 574 if (IS_ERR(inode)) {
b3b94faa 575 gfs2_holder_uninit(ghs);
7359a19c 576 return PTR_ERR(inode);
b3b94faa
DT
577 }
578
b3b94faa 579 gfs2_trans_end(sdp);
6dbd8224 580 if (dip->i_alloc->al_rgd)
b3b94faa
DT
581 gfs2_inplace_release(dip);
582 gfs2_quota_unlock(dip);
583 gfs2_alloc_put(dip);
584
585 gfs2_glock_dq_uninit_m(2, ghs);
586
b3b94faa
DT
587 d_instantiate(dentry, inode);
588 mark_inode_dirty(inode);
589
590 return 0;
591}
592
0188d6c5
SW
593/*
594 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
595 * @this: move this
596 * @to: to here
597 *
598 * Follow @to back to the root and make sure we don't encounter @this
599 * Assumes we already hold the rename lock.
600 *
601 * Returns: errno
602 */
603
604static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
605{
606 struct inode *dir = &to->i_inode;
607 struct super_block *sb = dir->i_sb;
608 struct inode *tmp;
609 struct qstr dotdot;
610 int error = 0;
611
612 gfs2_str2qstr(&dotdot, "..");
613
614 igrab(dir);
615
616 for (;;) {
617 if (dir == &this->i_inode) {
618 error = -EINVAL;
619 break;
620 }
621 if (dir == sb->s_root->d_inode) {
622 error = 0;
623 break;
624 }
625
626 tmp = gfs2_lookupi(dir, &dotdot, 1);
627 if (IS_ERR(tmp)) {
628 error = PTR_ERR(tmp);
629 break;
630 }
631
632 iput(dir);
633 dir = tmp;
634 }
635
636 iput(dir);
637
638 return error;
639}
640
b3b94faa
DT
641/**
642 * gfs2_rename - Rename a file
643 * @odir: Parent directory of old file name
644 * @odentry: The old dentry of the file
645 * @ndir: Parent directory of new file name
646 * @ndentry: The new dentry of the file
647 *
648 * Returns: errno
649 */
650
651static int gfs2_rename(struct inode *odir, struct dentry *odentry,
652 struct inode *ndir, struct dentry *ndentry)
653{
feaa7bba
SW
654 struct gfs2_inode *odip = GFS2_I(odir);
655 struct gfs2_inode *ndip = GFS2_I(ndir);
656 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
b3b94faa 657 struct gfs2_inode *nip = NULL;
feaa7bba 658 struct gfs2_sbd *sdp = GFS2_SB(odir);
0188d6c5 659 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
ddee7608 660 struct gfs2_rgrpd *nrgd;
b3b94faa
DT
661 unsigned int num_gh;
662 int dir_rename = 0;
663 int alloc_required;
664 unsigned int x;
665 int error;
666
b3b94faa 667 if (ndentry->d_inode) {
feaa7bba 668 nip = GFS2_I(ndentry->d_inode);
b3b94faa
DT
669 if (ip == nip)
670 return 0;
671 }
672
b3b94faa 673
0188d6c5
SW
674 if (odip != ndip) {
675 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
676 0, &r_gh);
b3b94faa
DT
677 if (error)
678 goto out;
679
0188d6c5
SW
680 if (S_ISDIR(ip->i_inode.i_mode)) {
681 dir_rename = 1;
682 /* don't move a dirctory into it's subdir */
683 error = gfs2_ok_to_move(ip, ndip);
684 if (error)
685 goto out_gunlock_r;
686 }
b3b94faa
DT
687 }
688
d9d1ca30 689 num_gh = 1;
b3b94faa 690 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
d9d1ca30
SW
691 if (odip != ndip) {
692 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
693 num_gh++;
694 }
695 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
696 num_gh++;
b3b94faa 697
d9d1ca30
SW
698 if (nip) {
699 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
700 num_gh++;
ddee7608
RC
701 /* grab the resource lock for unlink flag twiddling
702 * this is the case of the target file already existing
703 * so we unlink before doing the rename
704 */
dbb7cae2 705 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
ddee7608
RC
706 if (nrgd)
707 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
d9d1ca30 708 }
b3b94faa 709
72dbf479
BP
710 for (x = 0; x < num_gh; x++) {
711 error = gfs2_glock_nq(ghs + x);
712 if (error)
713 goto out_gunlock;
714 }
b3b94faa
DT
715
716 /* Check out the old directory */
717
718 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
719 if (error)
720 goto out_gunlock;
721
722 /* Check out the new directory */
723
724 if (nip) {
725 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
726 if (error)
727 goto out_gunlock;
728
b60623c2 729 if (S_ISDIR(nip->i_inode.i_mode)) {
b3b94faa
DT
730 if (nip->i_di.di_entries < 2) {
731 if (gfs2_consist_inode(nip))
4cc14f0b 732 gfs2_dinode_print(nip);
b3b94faa
DT
733 error = -EIO;
734 goto out_gunlock;
735 }
736 if (nip->i_di.di_entries > 2) {
737 error = -ENOTEMPTY;
738 goto out_gunlock;
739 }
740 }
741 } else {
f58ba889 742 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
b3b94faa
DT
743 if (error)
744 goto out_gunlock;
745
dbb7cae2 746 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
b3b94faa
DT
747 switch (error) {
748 case -ENOENT:
749 error = 0;
750 break;
751 case 0:
752 error = -EEXIST;
753 default:
754 goto out_gunlock;
755 };
756
757 if (odip != ndip) {
4f56110a 758 if (!ndip->i_inode.i_nlink) {
b3b94faa
DT
759 error = -EINVAL;
760 goto out_gunlock;
761 }
cd915493 762 if (ndip->i_di.di_entries == (u32)-1) {
b3b94faa
DT
763 error = -EFBIG;
764 goto out_gunlock;
765 }
b60623c2 766 if (S_ISDIR(ip->i_inode.i_mode) &&
4f56110a 767 ndip->i_inode.i_nlink == (u32)-1) {
b3b94faa
DT
768 error = -EMLINK;
769 goto out_gunlock;
770 }
771 }
772 }
773
774 /* Check out the dir to be renamed */
775
776 if (dir_rename) {
f58ba889 777 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
b3b94faa
DT
778 if (error)
779 goto out_gunlock;
780 }
781
c752666c
SW
782 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
783 if (error < 0)
b3b94faa 784 goto out_gunlock;
c752666c 785 error = 0;
b3b94faa
DT
786
787 if (alloc_required) {
788 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
182fe5ab
CG
789 if (!al) {
790 error = -ENOMEM;
791 goto out_gunlock;
792 }
b3b94faa 793
d82661d9 794 error = gfs2_quota_lock_check(ndip);
b3b94faa
DT
795 if (error)
796 goto out_alloc;
797
b3b94faa
DT
798 al->al_requested = sdp->sd_max_dirres;
799
800 error = gfs2_inplace_reserve(ndip);
801 if (error)
802 goto out_gunlock_q;
803
fe1bdedc 804 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
bb8d8a6f 805 al->al_rgd->rd_length +
b3b94faa 806 4 * RES_DINODE + 4 * RES_LEAF +
87d21e07 807 RES_STATFS + RES_QUOTA + 4, 0);
b3b94faa
DT
808 if (error)
809 goto out_ipreserv;
810 } else {
811 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
87d21e07 812 5 * RES_LEAF + 4, 0);
b3b94faa
DT
813 if (error)
814 goto out_gunlock;
815 }
816
817 /* Remove the target file, if it exists */
818
819 if (nip) {
b60623c2 820 if (S_ISDIR(nip->i_inode.i_mode))
feaa7bba
SW
821 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
822 else {
823 error = gfs2_dir_del(ndip, &ndentry->d_name);
824 if (error)
825 goto out_end_trans;
87d21e07 826 error = gfs2_change_nlink(nip, -1);
feaa7bba 827 }
b3b94faa
DT
828 if (error)
829 goto out_end_trans;
830 }
831
832 if (dir_rename) {
833 struct qstr name;
71b86f56 834 gfs2_str2qstr(&name, "..");
b3b94faa
DT
835
836 error = gfs2_change_nlink(ndip, +1);
837 if (error)
838 goto out_end_trans;
839 error = gfs2_change_nlink(odip, -1);
840 if (error)
841 goto out_end_trans;
842
ffed8ab3 843 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
b3b94faa
DT
844 if (error)
845 goto out_end_trans;
846 } else {
847 struct buffer_head *dibh;
848 error = gfs2_meta_inode_buffer(ip, &dibh);
849 if (error)
850 goto out_end_trans;
4bd91ba1 851 ip->i_inode.i_ctime = CURRENT_TIME;
d4e9c4c3 852 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 853 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
854 brelse(dibh);
855 }
856
857 error = gfs2_dir_del(odip, &odentry->d_name);
858 if (error)
859 goto out_end_trans;
860
dbb7cae2 861 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
b3b94faa
DT
862 if (error)
863 goto out_end_trans;
864
feaa7bba 865out_end_trans:
b3b94faa 866 gfs2_trans_end(sdp);
feaa7bba 867out_ipreserv:
b3b94faa
DT
868 if (alloc_required)
869 gfs2_inplace_release(ndip);
feaa7bba 870out_gunlock_q:
b3b94faa
DT
871 if (alloc_required)
872 gfs2_quota_unlock(ndip);
feaa7bba 873out_alloc:
b3b94faa
DT
874 if (alloc_required)
875 gfs2_alloc_put(ndip);
feaa7bba 876out_gunlock:
72dbf479
BP
877 while (x--) {
878 gfs2_glock_dq(ghs + x);
b3b94faa 879 gfs2_holder_uninit(ghs + x);
72dbf479 880 }
feaa7bba 881out_gunlock_r:
0188d6c5 882 if (r_gh.gh_gl)
b3b94faa 883 gfs2_glock_dq_uninit(&r_gh);
feaa7bba 884out:
b3b94faa
DT
885 return error;
886}
887
888/**
889 * gfs2_readlink - Read the value of a symlink
890 * @dentry: the symlink
891 * @buf: the buffer to read the symlink data into
892 * @size: the size of the buffer
893 *
894 * Returns: errno
895 */
896
897static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
898 int user_size)
899{
feaa7bba 900 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
b3b94faa
DT
901 char array[GFS2_FAST_NAME_SIZE], *buf = array;
902 unsigned int len = GFS2_FAST_NAME_SIZE;
903 int error;
904
b3b94faa
DT
905 error = gfs2_readlinki(ip, &buf, &len);
906 if (error)
907 return error;
908
909 if (user_size > len - 1)
910 user_size = len - 1;
911
912 if (copy_to_user(user_buf, buf, user_size))
913 error = -EFAULT;
914 else
915 error = user_size;
916
917 if (buf != array)
918 kfree(buf);
919
920 return error;
921}
922
923/**
924 * gfs2_follow_link - Follow a symbolic link
925 * @dentry: The dentry of the link
926 * @nd: Data that we pass to vfs_follow_link()
927 *
928 * This can handle symlinks of any size. It is optimised for symlinks
929 * under GFS2_FAST_NAME_SIZE.
930 *
931 * Returns: 0 on success or error code
932 */
933
934static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
935{
feaa7bba 936 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
b3b94faa
DT
937 char array[GFS2_FAST_NAME_SIZE], *buf = array;
938 unsigned int len = GFS2_FAST_NAME_SIZE;
939 int error;
940
b3b94faa
DT
941 error = gfs2_readlinki(ip, &buf, &len);
942 if (!error) {
943 error = vfs_follow_link(nd, buf);
944 if (buf != array)
945 kfree(buf);
946 }
947
948 return ERR_PTR(error);
949}
950
951/**
952 * gfs2_permission -
953 * @inode:
954 * @mask:
955 * @nd: passed from Linux VFS, ignored by us
956 *
300c7d75
SW
957 * This may be called from the VFS directly, or from within GFS2 with the
958 * inode locked, so we look to see if the glock is already locked and only
959 * lock the glock if its not already been done.
960 *
b3b94faa
DT
961 * Returns: errno
962 */
963
f58ba889 964int gfs2_permission(struct inode *inode, int mask)
b3b94faa 965{
feaa7bba 966 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
967 struct gfs2_holder i_gh;
968 int error;
300c7d75 969 int unlock = 0;
b3b94faa 970
7afd88d9 971 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
300c7d75
SW
972 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
973 if (error)
974 return error;
975 unlock = 1;
976 }
b3b94faa 977
f58ba889
MS
978 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
979 error = -EACCES;
980 else
981 error = generic_permission(inode, mask, gfs2_check_acl);
300c7d75 982 if (unlock)
b3b94faa 983 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
984
985 return error;
986}
987
988static int setattr_size(struct inode *inode, struct iattr *attr)
989{
feaa7bba 990 struct gfs2_inode *ip = GFS2_I(inode);
16615be1 991 struct gfs2_sbd *sdp = GFS2_SB(inode);
b3b94faa
DT
992 int error;
993
994 if (attr->ia_size != ip->i_di.di_size) {
16615be1 995 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
b3b94faa
DT
996 if (error)
997 return error;
16615be1
SW
998 error = vmtruncate(inode, attr->ia_size);
999 gfs2_trans_end(sdp);
1000 if (error)
1001 return error;
b3b94faa
DT
1002 }
1003
aa6a85a9 1004 error = gfs2_truncatei(ip, attr->ia_size);
090ffaa5
WC
1005 if (error && (inode->i_size != ip->i_di.di_size))
1006 i_size_write(inode, ip->i_di.di_size);
b3b94faa
DT
1007
1008 return error;
1009}
1010
1011static int setattr_chown(struct inode *inode, struct iattr *attr)
1012{
feaa7bba
SW
1013 struct gfs2_inode *ip = GFS2_I(inode);
1014 struct gfs2_sbd *sdp = GFS2_SB(inode);
b3b94faa 1015 struct buffer_head *dibh;
cd915493 1016 u32 ouid, ogid, nuid, ngid;
b3b94faa
DT
1017 int error;
1018
2933f925
SW
1019 ouid = inode->i_uid;
1020 ogid = inode->i_gid;
b3b94faa
DT
1021 nuid = attr->ia_uid;
1022 ngid = attr->ia_gid;
1023
1024 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1025 ouid = nuid = NO_QUOTA_CHANGE;
1026 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1027 ogid = ngid = NO_QUOTA_CHANGE;
1028
182fe5ab
CG
1029 if (!gfs2_alloc_get(ip))
1030 return -ENOMEM;
b3b94faa
DT
1031
1032 error = gfs2_quota_lock(ip, nuid, ngid);
1033 if (error)
1034 goto out_alloc;
1035
1036 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1037 error = gfs2_quota_check(ip, nuid, ngid);
1038 if (error)
1039 goto out_gunlock_q;
1040 }
1041
1042 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1043 if (error)
1044 goto out_gunlock_q;
1045
1046 error = gfs2_meta_inode_buffer(ip, &dibh);
1047 if (error)
1048 goto out_end_trans;
1049
1050 error = inode_setattr(inode, attr);
1051 gfs2_assert_warn(sdp, !error);
b3b94faa 1052
d4e9c4c3 1053 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 1054 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
1055 brelse(dibh);
1056
1057 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
77658aad
SW
1058 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1059 gfs2_quota_change(ip, -blocks, ouid, ogid);
1060 gfs2_quota_change(ip, blocks, nuid, ngid);
b3b94faa
DT
1061 }
1062
a91ea69f 1063out_end_trans:
b3b94faa 1064 gfs2_trans_end(sdp);
a91ea69f 1065out_gunlock_q:
b3b94faa 1066 gfs2_quota_unlock(ip);
a91ea69f 1067out_alloc:
b3b94faa 1068 gfs2_alloc_put(ip);
b3b94faa
DT
1069 return error;
1070}
1071
1072/**
1073 * gfs2_setattr - Change attributes on an inode
1074 * @dentry: The dentry which is changing
1075 * @attr: The structure describing the change
1076 *
1077 * The VFS layer wants to change one or more of an inodes attributes. Write
1078 * that change out to disk.
1079 *
1080 * Returns: errno
1081 */
1082
1083static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1084{
1085 struct inode *inode = dentry->d_inode;
feaa7bba 1086 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1087 struct gfs2_holder i_gh;
1088 int error;
1089
b3b94faa
DT
1090 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1091 if (error)
1092 return error;
1093
1094 error = -EPERM;
1095 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1096 goto out;
1097
1098 error = inode_change_ok(inode, attr);
1099 if (error)
1100 goto out;
1101
1102 if (attr->ia_valid & ATTR_SIZE)
1103 error = setattr_size(inode, attr);
1104 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1105 error = setattr_chown(inode, attr);
1106 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1107 error = gfs2_acl_chmod(ip, attr);
1108 else
1109 error = gfs2_setattr_simple(ip, attr);
1110
a91ea69f 1111out:
b3b94faa 1112 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
1113 if (!error)
1114 mark_inode_dirty(inode);
b3b94faa
DT
1115 return error;
1116}
1117
1118/**
1119 * gfs2_getattr - Read out an inode's attributes
26c1a574 1120 * @mnt: The vfsmount the inode is being accessed from
b3b94faa
DT
1121 * @dentry: The dentry to stat
1122 * @stat: The inode's stats
1123 *
dcf3dd85
SW
1124 * This may be called from the VFS directly, or from within GFS2 with the
1125 * inode locked, so we look to see if the glock is already locked and only
1126 * lock the glock if its not already been done. Note that its the NFS
1127 * readdirplus operation which causes this to be called (from filldir)
1128 * with the glock already held.
1129 *
b3b94faa
DT
1130 * Returns: errno
1131 */
1132
1133static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1134 struct kstat *stat)
1135{
1136 struct inode *inode = dentry->d_inode;
feaa7bba 1137 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1138 struct gfs2_holder gh;
1139 int error;
dcf3dd85 1140 int unlock = 0;
b3b94faa 1141
7afd88d9 1142 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
dcf3dd85
SW
1143 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1144 if (error)
1145 return error;
1146 unlock = 1;
b3b94faa
DT
1147 }
1148
dcf3dd85 1149 generic_fillattr(inode, stat);
d7c103d0 1150 if (unlock)
dcf3dd85
SW
1151 gfs2_glock_dq_uninit(&gh);
1152
1153 return 0;
b3b94faa
DT
1154}
1155
1156static int gfs2_setxattr(struct dentry *dentry, const char *name,
1157 const void *data, size_t size, int flags)
1158{
feaa7bba 1159 struct inode *inode = dentry->d_inode;
b3b94faa
DT
1160 struct gfs2_ea_request er;
1161
b3b94faa
DT
1162 memset(&er, 0, sizeof(struct gfs2_ea_request));
1163 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1164 if (er.er_type == GFS2_EATYPE_UNUSED)
1165 return -EOPNOTSUPP;
1166 er.er_data = (char *)data;
1167 er.er_name_len = strlen(er.er_name);
1168 er.er_data_len = size;
1169 er.er_flags = flags;
1170
feaa7bba 1171 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
b3b94faa 1172
feaa7bba 1173 return gfs2_ea_set(GFS2_I(inode), &er);
b3b94faa
DT
1174}
1175
1176static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1177 void *data, size_t size)
1178{
1179 struct gfs2_ea_request er;
1180
b3b94faa
DT
1181 memset(&er, 0, sizeof(struct gfs2_ea_request));
1182 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1183 if (er.er_type == GFS2_EATYPE_UNUSED)
1184 return -EOPNOTSUPP;
1185 er.er_data = data;
1186 er.er_name_len = strlen(er.er_name);
1187 er.er_data_len = size;
1188
feaa7bba 1189 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1190}
1191
1192static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1193{
1194 struct gfs2_ea_request er;
1195
b3b94faa
DT
1196 memset(&er, 0, sizeof(struct gfs2_ea_request));
1197 er.er_data = (size) ? buffer : NULL;
1198 er.er_data_len = size;
1199
feaa7bba 1200 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1201}
1202
1203static int gfs2_removexattr(struct dentry *dentry, const char *name)
1204{
1205 struct gfs2_ea_request er;
1206
b3b94faa
DT
1207 memset(&er, 0, sizeof(struct gfs2_ea_request));
1208 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1209 if (er.er_type == GFS2_EATYPE_UNUSED)
1210 return -EOPNOTSUPP;
1211 er.er_name_len = strlen(er.er_name);
1212
feaa7bba 1213 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1214}
1215
e9079cce
SW
1216static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1217 u64 start, u64 len)
1218{
1219 struct gfs2_inode *ip = GFS2_I(inode);
1220 struct gfs2_holder gh;
1221 int ret;
1222
1223 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1224 if (ret)
1225 return ret;
1226
1227 mutex_lock(&inode->i_mutex);
1228
1229 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1230 if (ret)
1231 goto out;
1232
1233 if (gfs2_is_stuffed(ip)) {
1234 u64 phys = ip->i_no_addr << inode->i_blkbits;
1235 u64 size = i_size_read(inode);
1236 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1237 FIEMAP_EXTENT_DATA_INLINE;
1238 phys += sizeof(struct gfs2_dinode);
1239 phys += start;
1240 if (start + len > size)
1241 len = size - start;
1242 if (start < size)
1243 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1244 len, flags);
1245 if (ret == 1)
1246 ret = 0;
1247 } else {
1248 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1249 gfs2_block_map);
1250 }
1251
1252 gfs2_glock_dq_uninit(&gh);
1253out:
1254 mutex_unlock(&inode->i_mutex);
1255 return ret;
1256}
1257
92e1d5be 1258const struct inode_operations gfs2_file_iops = {
e6305c43 1259 .permission = gfs2_permission,
b3b94faa
DT
1260 .setattr = gfs2_setattr,
1261 .getattr = gfs2_getattr,
1262 .setxattr = gfs2_setxattr,
1263 .getxattr = gfs2_getxattr,
1264 .listxattr = gfs2_listxattr,
1265 .removexattr = gfs2_removexattr,
e9079cce 1266 .fiemap = gfs2_fiemap,
b3b94faa
DT
1267};
1268
92e1d5be 1269const struct inode_operations gfs2_dir_iops = {
b3b94faa
DT
1270 .create = gfs2_create,
1271 .lookup = gfs2_lookup,
1272 .link = gfs2_link,
1273 .unlink = gfs2_unlink,
1274 .symlink = gfs2_symlink,
1275 .mkdir = gfs2_mkdir,
1276 .rmdir = gfs2_rmdir,
1277 .mknod = gfs2_mknod,
1278 .rename = gfs2_rename,
e6305c43 1279 .permission = gfs2_permission,
b3b94faa
DT
1280 .setattr = gfs2_setattr,
1281 .getattr = gfs2_getattr,
1282 .setxattr = gfs2_setxattr,
1283 .getxattr = gfs2_getxattr,
1284 .listxattr = gfs2_listxattr,
1285 .removexattr = gfs2_removexattr,
e9079cce 1286 .fiemap = gfs2_fiemap,
b3b94faa
DT
1287};
1288
92e1d5be 1289const struct inode_operations gfs2_symlink_iops = {
b3b94faa
DT
1290 .readlink = gfs2_readlink,
1291 .follow_link = gfs2_follow_link,
e6305c43 1292 .permission = gfs2_permission,
b3b94faa
DT
1293 .setattr = gfs2_setattr,
1294 .getattr = gfs2_getattr,
1295 .setxattr = gfs2_setxattr,
1296 .getxattr = gfs2_getxattr,
1297 .listxattr = gfs2_listxattr,
1298 .removexattr = gfs2_removexattr,
e9079cce 1299 .fiemap = gfs2_fiemap,
b3b94faa
DT
1300};
1301