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