GFS2: free disk inode which is deleted by remote node -V2
[deliverable/linux.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
87ec2174
SW
265/*
266 * gfs2_unlink_ok - check to see that a inode is still in a directory
267 * @dip: the directory
268 * @name: the name of the file
269 * @ip: the inode
270 *
271 * Assumes that the lock on (at least) @dip is held.
272 *
273 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
274 */
275
276static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
277 const struct gfs2_inode *ip)
278{
279 int error;
280
281 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
282 return -EPERM;
283
284 if ((dip->i_inode.i_mode & S_ISVTX) &&
285 dip->i_inode.i_uid != current_fsuid() &&
286 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
287 return -EPERM;
288
289 if (IS_APPEND(&dip->i_inode))
290 return -EPERM;
291
292 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
293 if (error)
294 return error;
295
296 error = gfs2_dir_check(&dip->i_inode, name, ip);
297 if (error)
298 return error;
299
300 return 0;
301}
302
b3b94faa
DT
303/**
304 * gfs2_unlink - Unlink a file
305 * @dir: The inode of the directory containing the file to unlink
306 * @dentry: The file itself
307 *
308 * Unlink a file. Call gfs2_unlinki()
309 *
310 * Returns: errno
311 */
312
313static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
314{
feaa7bba
SW
315 struct gfs2_inode *dip = GFS2_I(dir);
316 struct gfs2_sbd *sdp = GFS2_SB(dir);
317 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
ddee7608
RC
318 struct gfs2_holder ghs[3];
319 struct gfs2_rgrpd *rgd;
320 struct gfs2_holder ri_gh;
b3b94faa
DT
321 int error;
322
ddee7608
RC
323 error = gfs2_rindex_hold(sdp, &ri_gh);
324 if (error)
325 return error;
326
b3b94faa 327 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
ddee7608 328 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
b3b94faa 329
dbb7cae2 330 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
ddee7608
RC
331 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
332
333
8497a46e 334 error = gfs2_glock_nq(ghs); /* parent */
b3b94faa 335 if (error)
8497a46e
SW
336 goto out_parent;
337
338 error = gfs2_glock_nq(ghs + 1); /* child */
339 if (error)
340 goto out_child;
341
342 error = gfs2_glock_nq(ghs + 2); /* rgrp */
343 if (error)
344 goto out_rgrp;
b3b94faa
DT
345
346 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
347 if (error)
72dbf479 348 goto out_gunlock;
b3b94faa 349
feaa7bba 350 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
b3b94faa 351 if (error)
8497a46e 352 goto out_rgrp;
b3b94faa 353
feaa7bba
SW
354 error = gfs2_dir_del(dip, &dentry->d_name);
355 if (error)
356 goto out_end_trans;
b3b94faa 357
feaa7bba 358 error = gfs2_change_nlink(ip, -1);
b3b94faa 359
feaa7bba
SW
360out_end_trans:
361 gfs2_trans_end(sdp);
72dbf479 362out_gunlock:
8497a46e
SW
363 gfs2_glock_dq(ghs + 2);
364out_rgrp:
ddee7608 365 gfs2_holder_uninit(ghs + 2);
8497a46e
SW
366 gfs2_glock_dq(ghs + 1);
367out_child:
368 gfs2_holder_uninit(ghs + 1);
369 gfs2_glock_dq(ghs);
370out_parent:
371 gfs2_holder_uninit(ghs);
ddee7608 372 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
373 return error;
374}
375
376/**
377 * gfs2_symlink - Create a symlink
378 * @dir: The directory to create the symlink in
379 * @dentry: The dentry to put the symlink in
380 * @symname: The thing which the link points to
381 *
382 * Returns: errno
383 */
384
385static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
386 const char *symname)
387{
feaa7bba
SW
388 struct gfs2_inode *dip = GFS2_I(dir), *ip;
389 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
390 struct gfs2_holder ghs[2];
391 struct inode *inode;
392 struct buffer_head *dibh;
393 int size;
394 int error;
395
b3b94faa
DT
396 /* Must be stuffed with a null terminator for gfs2_follow_link() */
397 size = strlen(symname);
398 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
399 return -ENAMETOOLONG;
400
401 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
402
e7f14f4d 403 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
7359a19c 404 if (IS_ERR(inode)) {
b3b94faa 405 gfs2_holder_uninit(ghs);
7359a19c 406 return PTR_ERR(inode);
b3b94faa
DT
407 }
408
5c676f6d 409 ip = ghs[1].gh_gl->gl_object;
b3b94faa 410
c9e98886 411 ip->i_disksize = size;
5cf32524 412 i_size_write(inode, size);
b3b94faa
DT
413
414 error = gfs2_meta_inode_buffer(ip, &dibh);
415
416 if (!gfs2_assert_withdraw(sdp, !error)) {
539e5d6b 417 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
418 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
419 size);
420 brelse(dibh);
421 }
422
423 gfs2_trans_end(sdp);
6dbd8224 424 if (dip->i_alloc->al_rgd)
b3b94faa
DT
425 gfs2_inplace_release(dip);
426 gfs2_quota_unlock(dip);
427 gfs2_alloc_put(dip);
428
429 gfs2_glock_dq_uninit_m(2, ghs);
430
b3b94faa
DT
431 d_instantiate(dentry, inode);
432 mark_inode_dirty(inode);
433
434 return 0;
435}
436
437/**
438 * gfs2_mkdir - Make a directory
439 * @dir: The parent directory of the new one
440 * @dentry: The dentry of the new directory
441 * @mode: The mode of the new directory
442 *
443 * Returns: errno
444 */
445
446static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
447{
feaa7bba
SW
448 struct gfs2_inode *dip = GFS2_I(dir), *ip;
449 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
450 struct gfs2_holder ghs[2];
451 struct inode *inode;
452 struct buffer_head *dibh;
453 int error;
454
b3b94faa
DT
455 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
456
e7f14f4d 457 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
7359a19c 458 if (IS_ERR(inode)) {
b3b94faa 459 gfs2_holder_uninit(ghs);
7359a19c 460 return PTR_ERR(inode);
b3b94faa
DT
461 }
462
5c676f6d 463 ip = ghs[1].gh_gl->gl_object;
b3b94faa 464
4f56110a 465 ip->i_inode.i_nlink = 2;
c9e98886 466 ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
383f01fb 467 ip->i_diskflags |= GFS2_DIF_JDATA;
ad6203f2 468 ip->i_entries = 2;
b3b94faa
DT
469
470 error = gfs2_meta_inode_buffer(ip, &dibh);
471
472 if (!gfs2_assert_withdraw(sdp, !error)) {
473 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
c752666c 474 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
71b86f56 475 struct qstr str;
b3b94faa 476
71b86f56 477 gfs2_str2qstr(&str, ".");
c752666c
SW
478 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
479 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
b3b94faa 480 dent->de_inum = di->di_num; /* already GFS2 endian */
7ecdb70a 481 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa
DT
482 di->di_entries = cpu_to_be32(1);
483
71b86f56 484 gfs2_str2qstr(&str, "..");
c752666c
SW
485 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
486 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
b3b94faa 487
dbb7cae2 488 gfs2_inum_out(dip, dent);
7ecdb70a 489 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa 490
539e5d6b 491 gfs2_dinode_out(ip, di);
b3b94faa
DT
492
493 brelse(dibh);
494 }
495
496 error = gfs2_change_nlink(dip, +1);
497 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
498
499 gfs2_trans_end(sdp);
6dbd8224 500 if (dip->i_alloc->al_rgd)
b3b94faa
DT
501 gfs2_inplace_release(dip);
502 gfs2_quota_unlock(dip);
503 gfs2_alloc_put(dip);
504
505 gfs2_glock_dq_uninit_m(2, ghs);
506
b3b94faa
DT
507 d_instantiate(dentry, inode);
508 mark_inode_dirty(inode);
509
510 return 0;
511}
512
2286dbfa
SW
513/**
514 * gfs2_rmdiri - Remove a directory
515 * @dip: The parent directory of the directory to be removed
516 * @name: The name of the directory to be removed
517 * @ip: The GFS2 inode of the directory to be removed
518 *
519 * Assumes Glocks on dip and ip are held
520 *
521 * Returns: errno
522 */
523
524static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
525 struct gfs2_inode *ip)
526{
527 struct qstr dotname;
528 int error;
529
530 if (ip->i_entries != 2) {
531 if (gfs2_consist_inode(ip))
532 gfs2_dinode_print(ip);
533 return -EIO;
534 }
535
536 error = gfs2_dir_del(dip, name);
537 if (error)
538 return error;
539
540 error = gfs2_change_nlink(dip, -1);
541 if (error)
542 return error;
543
544 gfs2_str2qstr(&dotname, ".");
545 error = gfs2_dir_del(ip, &dotname);
546 if (error)
547 return error;
548
549 gfs2_str2qstr(&dotname, "..");
550 error = gfs2_dir_del(ip, &dotname);
551 if (error)
552 return error;
553
554 /* It looks odd, but it really should be done twice */
555 error = gfs2_change_nlink(ip, -1);
556 if (error)
557 return error;
558
559 error = gfs2_change_nlink(ip, -1);
560 if (error)
561 return error;
562
563 return error;
564}
565
b3b94faa
DT
566/**
567 * gfs2_rmdir - Remove a directory
568 * @dir: The parent directory of the directory to be removed
569 * @dentry: The dentry of the directory to remove
570 *
571 * Remove a directory. Call gfs2_rmdiri()
572 *
573 * Returns: errno
574 */
575
576static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
577{
feaa7bba
SW
578 struct gfs2_inode *dip = GFS2_I(dir);
579 struct gfs2_sbd *sdp = GFS2_SB(dir);
580 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
ddee7608
RC
581 struct gfs2_holder ghs[3];
582 struct gfs2_rgrpd *rgd;
583 struct gfs2_holder ri_gh;
b3b94faa
DT
584 int error;
585
ddee7608
RC
586 error = gfs2_rindex_hold(sdp, &ri_gh);
587 if (error)
588 return error;
b3b94faa
DT
589 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
590 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
591
dbb7cae2 592 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
ddee7608
RC
593 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
594
72dbf479
BP
595 error = gfs2_glock_nq(ghs); /* parent */
596 if (error)
597 goto out_parent;
598
599 error = gfs2_glock_nq(ghs + 1); /* child */
b3b94faa 600 if (error)
72dbf479
BP
601 goto out_child;
602
603 error = gfs2_glock_nq(ghs + 2); /* rgrp */
604 if (error)
605 goto out_rgrp;
b3b94faa
DT
606
607 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
608 if (error)
609 goto out_gunlock;
610
ad6203f2 611 if (ip->i_entries < 2) {
b3b94faa 612 if (gfs2_consist_inode(ip))
4cc14f0b 613 gfs2_dinode_print(ip);
b3b94faa
DT
614 error = -EIO;
615 goto out_gunlock;
616 }
ad6203f2 617 if (ip->i_entries > 2) {
b3b94faa
DT
618 error = -ENOTEMPTY;
619 goto out_gunlock;
620 }
621
feaa7bba 622 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
b3b94faa
DT
623 if (error)
624 goto out_gunlock;
625
feaa7bba 626 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
b3b94faa
DT
627
628 gfs2_trans_end(sdp);
629
a91ea69f 630out_gunlock:
72dbf479
BP
631 gfs2_glock_dq(ghs + 2);
632out_rgrp:
ddee7608 633 gfs2_holder_uninit(ghs + 2);
72dbf479
BP
634 gfs2_glock_dq(ghs + 1);
635out_child:
636 gfs2_holder_uninit(ghs + 1);
637 gfs2_glock_dq(ghs);
638out_parent:
639 gfs2_holder_uninit(ghs);
ddee7608 640 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
641 return error;
642}
643
644/**
645 * gfs2_mknod - Make a special file
646 * @dir: The directory in which the special file will reside
647 * @dentry: The dentry of the special file
648 * @mode: The mode of the special file
649 * @rdev: The device specification of the special file
650 *
651 */
652
653static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
654 dev_t dev)
655{
e7f14f4d 656 struct gfs2_inode *dip = GFS2_I(dir);
feaa7bba 657 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
658 struct gfs2_holder ghs[2];
659 struct inode *inode;
b3b94faa
DT
660
661 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
662
e7f14f4d 663 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
7359a19c 664 if (IS_ERR(inode)) {
b3b94faa 665 gfs2_holder_uninit(ghs);
7359a19c 666 return PTR_ERR(inode);
b3b94faa
DT
667 }
668
b3b94faa 669 gfs2_trans_end(sdp);
6dbd8224 670 if (dip->i_alloc->al_rgd)
b3b94faa
DT
671 gfs2_inplace_release(dip);
672 gfs2_quota_unlock(dip);
673 gfs2_alloc_put(dip);
674
675 gfs2_glock_dq_uninit_m(2, ghs);
676
b3b94faa
DT
677 d_instantiate(dentry, inode);
678 mark_inode_dirty(inode);
679
680 return 0;
681}
682
0188d6c5
SW
683/*
684 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
685 * @this: move this
686 * @to: to here
687 *
688 * Follow @to back to the root and make sure we don't encounter @this
689 * Assumes we already hold the rename lock.
690 *
691 * Returns: errno
692 */
693
694static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
695{
696 struct inode *dir = &to->i_inode;
697 struct super_block *sb = dir->i_sb;
698 struct inode *tmp;
699 struct qstr dotdot;
700 int error = 0;
701
702 gfs2_str2qstr(&dotdot, "..");
703
704 igrab(dir);
705
706 for (;;) {
707 if (dir == &this->i_inode) {
708 error = -EINVAL;
709 break;
710 }
711 if (dir == sb->s_root->d_inode) {
712 error = 0;
713 break;
714 }
715
716 tmp = gfs2_lookupi(dir, &dotdot, 1);
717 if (IS_ERR(tmp)) {
718 error = PTR_ERR(tmp);
719 break;
720 }
721
722 iput(dir);
723 dir = tmp;
724 }
725
726 iput(dir);
727
728 return error;
729}
730
b3b94faa
DT
731/**
732 * gfs2_rename - Rename a file
733 * @odir: Parent directory of old file name
734 * @odentry: The old dentry of the file
735 * @ndir: Parent directory of new file name
736 * @ndentry: The new dentry of the file
737 *
738 * Returns: errno
739 */
740
741static int gfs2_rename(struct inode *odir, struct dentry *odentry,
742 struct inode *ndir, struct dentry *ndentry)
743{
feaa7bba
SW
744 struct gfs2_inode *odip = GFS2_I(odir);
745 struct gfs2_inode *ndip = GFS2_I(ndir);
746 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
b3b94faa 747 struct gfs2_inode *nip = NULL;
feaa7bba 748 struct gfs2_sbd *sdp = GFS2_SB(odir);
0188d6c5 749 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
ddee7608 750 struct gfs2_rgrpd *nrgd;
b3b94faa
DT
751 unsigned int num_gh;
752 int dir_rename = 0;
753 int alloc_required;
754 unsigned int x;
755 int error;
756
b3b94faa 757 if (ndentry->d_inode) {
feaa7bba 758 nip = GFS2_I(ndentry->d_inode);
b3b94faa
DT
759 if (ip == nip)
760 return 0;
761 }
762
b3b94faa 763
0188d6c5
SW
764 if (odip != ndip) {
765 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
766 0, &r_gh);
b3b94faa
DT
767 if (error)
768 goto out;
769
0188d6c5
SW
770 if (S_ISDIR(ip->i_inode.i_mode)) {
771 dir_rename = 1;
772 /* don't move a dirctory into it's subdir */
773 error = gfs2_ok_to_move(ip, ndip);
774 if (error)
775 goto out_gunlock_r;
776 }
b3b94faa
DT
777 }
778
d9d1ca30 779 num_gh = 1;
b3b94faa 780 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
d9d1ca30
SW
781 if (odip != ndip) {
782 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
783 num_gh++;
784 }
785 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
786 num_gh++;
b3b94faa 787
d9d1ca30
SW
788 if (nip) {
789 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
790 num_gh++;
ddee7608
RC
791 /* grab the resource lock for unlink flag twiddling
792 * this is the case of the target file already existing
793 * so we unlink before doing the rename
794 */
dbb7cae2 795 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
ddee7608
RC
796 if (nrgd)
797 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
d9d1ca30 798 }
b3b94faa 799
72dbf479
BP
800 for (x = 0; x < num_gh; x++) {
801 error = gfs2_glock_nq(ghs + x);
802 if (error)
803 goto out_gunlock;
804 }
b3b94faa
DT
805
806 /* Check out the old directory */
807
808 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
809 if (error)
810 goto out_gunlock;
811
812 /* Check out the new directory */
813
814 if (nip) {
815 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
816 if (error)
817 goto out_gunlock;
818
b60623c2 819 if (S_ISDIR(nip->i_inode.i_mode)) {
ad6203f2 820 if (nip->i_entries < 2) {
b3b94faa 821 if (gfs2_consist_inode(nip))
4cc14f0b 822 gfs2_dinode_print(nip);
b3b94faa
DT
823 error = -EIO;
824 goto out_gunlock;
825 }
ad6203f2 826 if (nip->i_entries > 2) {
b3b94faa
DT
827 error = -ENOTEMPTY;
828 goto out_gunlock;
829 }
830 }
831 } else {
f58ba889 832 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
b3b94faa
DT
833 if (error)
834 goto out_gunlock;
835
dbb7cae2 836 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
b3b94faa
DT
837 switch (error) {
838 case -ENOENT:
839 error = 0;
840 break;
841 case 0:
842 error = -EEXIST;
843 default:
844 goto out_gunlock;
845 };
846
847 if (odip != ndip) {
4f56110a 848 if (!ndip->i_inode.i_nlink) {
b3b94faa
DT
849 error = -EINVAL;
850 goto out_gunlock;
851 }
ad6203f2 852 if (ndip->i_entries == (u32)-1) {
b3b94faa
DT
853 error = -EFBIG;
854 goto out_gunlock;
855 }
b60623c2 856 if (S_ISDIR(ip->i_inode.i_mode) &&
4f56110a 857 ndip->i_inode.i_nlink == (u32)-1) {
b3b94faa
DT
858 error = -EMLINK;
859 goto out_gunlock;
860 }
861 }
862 }
863
864 /* Check out the dir to be renamed */
865
866 if (dir_rename) {
f58ba889 867 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
b3b94faa
DT
868 if (error)
869 goto out_gunlock;
870 }
871
c752666c
SW
872 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
873 if (error < 0)
b3b94faa 874 goto out_gunlock;
c752666c 875 error = 0;
b3b94faa
DT
876
877 if (alloc_required) {
878 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
182fe5ab
CG
879 if (!al) {
880 error = -ENOMEM;
881 goto out_gunlock;
882 }
b3b94faa 883
d82661d9 884 error = gfs2_quota_lock_check(ndip);
b3b94faa
DT
885 if (error)
886 goto out_alloc;
887
b3b94faa
DT
888 al->al_requested = sdp->sd_max_dirres;
889
890 error = gfs2_inplace_reserve(ndip);
891 if (error)
892 goto out_gunlock_q;
893
fe1bdedc 894 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
bb8d8a6f 895 al->al_rgd->rd_length +
b3b94faa 896 4 * RES_DINODE + 4 * RES_LEAF +
87d21e07 897 RES_STATFS + RES_QUOTA + 4, 0);
b3b94faa
DT
898 if (error)
899 goto out_ipreserv;
900 } else {
901 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
87d21e07 902 5 * RES_LEAF + 4, 0);
b3b94faa
DT
903 if (error)
904 goto out_gunlock;
905 }
906
907 /* Remove the target file, if it exists */
908
909 if (nip) {
b60623c2 910 if (S_ISDIR(nip->i_inode.i_mode))
feaa7bba
SW
911 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
912 else {
913 error = gfs2_dir_del(ndip, &ndentry->d_name);
914 if (error)
915 goto out_end_trans;
87d21e07 916 error = gfs2_change_nlink(nip, -1);
feaa7bba 917 }
b3b94faa
DT
918 if (error)
919 goto out_end_trans;
920 }
921
922 if (dir_rename) {
923 struct qstr name;
71b86f56 924 gfs2_str2qstr(&name, "..");
b3b94faa
DT
925
926 error = gfs2_change_nlink(ndip, +1);
927 if (error)
928 goto out_end_trans;
929 error = gfs2_change_nlink(odip, -1);
930 if (error)
931 goto out_end_trans;
932
ffed8ab3 933 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
b3b94faa
DT
934 if (error)
935 goto out_end_trans;
936 } else {
937 struct buffer_head *dibh;
938 error = gfs2_meta_inode_buffer(ip, &dibh);
939 if (error)
940 goto out_end_trans;
4bd91ba1 941 ip->i_inode.i_ctime = CURRENT_TIME;
d4e9c4c3 942 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 943 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
944 brelse(dibh);
945 }
946
947 error = gfs2_dir_del(odip, &odentry->d_name);
948 if (error)
949 goto out_end_trans;
950
dbb7cae2 951 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
b3b94faa
DT
952 if (error)
953 goto out_end_trans;
954
feaa7bba 955out_end_trans:
b3b94faa 956 gfs2_trans_end(sdp);
feaa7bba 957out_ipreserv:
b3b94faa
DT
958 if (alloc_required)
959 gfs2_inplace_release(ndip);
feaa7bba 960out_gunlock_q:
b3b94faa
DT
961 if (alloc_required)
962 gfs2_quota_unlock(ndip);
feaa7bba 963out_alloc:
b3b94faa
DT
964 if (alloc_required)
965 gfs2_alloc_put(ndip);
feaa7bba 966out_gunlock:
72dbf479
BP
967 while (x--) {
968 gfs2_glock_dq(ghs + x);
b3b94faa 969 gfs2_holder_uninit(ghs + x);
72dbf479 970 }
feaa7bba 971out_gunlock_r:
0188d6c5 972 if (r_gh.gh_gl)
b3b94faa 973 gfs2_glock_dq_uninit(&r_gh);
feaa7bba 974out:
b3b94faa
DT
975 return error;
976}
977
536baf02
SW
978/**
979 * gfs2_readlinki - return the contents of a symlink
980 * @ip: the symlink's inode
981 * @buf: a pointer to the buffer to be filled
982 * @len: a pointer to the length of @buf
983 *
984 * If @buf is too small, a piece of memory is kmalloc()ed and needs
985 * to be freed by the caller.
986 *
987 * Returns: errno
988 */
989
990static int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
991{
992 struct gfs2_holder i_gh;
993 struct buffer_head *dibh;
994 unsigned int x;
995 int error;
996
997 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
998 error = gfs2_glock_nq(&i_gh);
999 if (error) {
1000 gfs2_holder_uninit(&i_gh);
1001 return error;
1002 }
1003
1004 if (!ip->i_disksize) {
1005 gfs2_consist_inode(ip);
1006 error = -EIO;
1007 goto out;
1008 }
1009
1010 error = gfs2_meta_inode_buffer(ip, &dibh);
1011 if (error)
1012 goto out;
1013
1014 x = ip->i_disksize + 1;
1015 if (x > *len) {
1016 *buf = kmalloc(x, GFP_NOFS);
1017 if (!*buf) {
1018 error = -ENOMEM;
1019 goto out_brelse;
1020 }
1021 }
1022
1023 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1024 *len = x;
1025
1026out_brelse:
1027 brelse(dibh);
1028out:
1029 gfs2_glock_dq_uninit(&i_gh);
1030 return error;
1031}
1032
b3b94faa
DT
1033/**
1034 * gfs2_readlink - Read the value of a symlink
1035 * @dentry: the symlink
1036 * @buf: the buffer to read the symlink data into
1037 * @size: the size of the buffer
1038 *
1039 * Returns: errno
1040 */
1041
1042static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
1043 int user_size)
1044{
feaa7bba 1045 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
b3b94faa
DT
1046 char array[GFS2_FAST_NAME_SIZE], *buf = array;
1047 unsigned int len = GFS2_FAST_NAME_SIZE;
1048 int error;
1049
b3b94faa
DT
1050 error = gfs2_readlinki(ip, &buf, &len);
1051 if (error)
1052 return error;
1053
1054 if (user_size > len - 1)
1055 user_size = len - 1;
1056
1057 if (copy_to_user(user_buf, buf, user_size))
1058 error = -EFAULT;
1059 else
1060 error = user_size;
1061
1062 if (buf != array)
1063 kfree(buf);
1064
1065 return error;
1066}
1067
1068/**
1069 * gfs2_follow_link - Follow a symbolic link
1070 * @dentry: The dentry of the link
1071 * @nd: Data that we pass to vfs_follow_link()
1072 *
1073 * This can handle symlinks of any size. It is optimised for symlinks
1074 * under GFS2_FAST_NAME_SIZE.
1075 *
1076 * Returns: 0 on success or error code
1077 */
1078
1079static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
1080{
feaa7bba 1081 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
b3b94faa
DT
1082 char array[GFS2_FAST_NAME_SIZE], *buf = array;
1083 unsigned int len = GFS2_FAST_NAME_SIZE;
1084 int error;
1085
b3b94faa
DT
1086 error = gfs2_readlinki(ip, &buf, &len);
1087 if (!error) {
1088 error = vfs_follow_link(nd, buf);
1089 if (buf != array)
1090 kfree(buf);
1091 }
1092
1093 return ERR_PTR(error);
1094}
1095
1096/**
1097 * gfs2_permission -
1098 * @inode:
1099 * @mask:
1100 * @nd: passed from Linux VFS, ignored by us
1101 *
300c7d75
SW
1102 * This may be called from the VFS directly, or from within GFS2 with the
1103 * inode locked, so we look to see if the glock is already locked and only
1104 * lock the glock if its not already been done.
1105 *
b3b94faa
DT
1106 * Returns: errno
1107 */
1108
f58ba889 1109int gfs2_permission(struct inode *inode, int mask)
b3b94faa 1110{
feaa7bba 1111 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1112 struct gfs2_holder i_gh;
1113 int error;
300c7d75 1114 int unlock = 0;
b3b94faa 1115
7afd88d9 1116 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
300c7d75
SW
1117 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1118 if (error)
1119 return error;
1120 unlock = 1;
1121 }
b3b94faa 1122
f58ba889
MS
1123 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1124 error = -EACCES;
1125 else
1126 error = generic_permission(inode, mask, gfs2_check_acl);
300c7d75 1127 if (unlock)
b3b94faa 1128 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
1129
1130 return error;
1131}
1132
1133static int setattr_size(struct inode *inode, struct iattr *attr)
1134{
feaa7bba 1135 struct gfs2_inode *ip = GFS2_I(inode);
16615be1 1136 struct gfs2_sbd *sdp = GFS2_SB(inode);
b3b94faa
DT
1137 int error;
1138
c9e98886 1139 if (attr->ia_size != ip->i_disksize) {
16615be1 1140 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
b3b94faa
DT
1141 if (error)
1142 return error;
16615be1
SW
1143 error = vmtruncate(inode, attr->ia_size);
1144 gfs2_trans_end(sdp);
1145 if (error)
1146 return error;
b3b94faa
DT
1147 }
1148
aa6a85a9 1149 error = gfs2_truncatei(ip, attr->ia_size);
c9e98886
SW
1150 if (error && (inode->i_size != ip->i_disksize))
1151 i_size_write(inode, ip->i_disksize);
b3b94faa
DT
1152
1153 return error;
1154}
1155
1156static int setattr_chown(struct inode *inode, struct iattr *attr)
1157{
feaa7bba
SW
1158 struct gfs2_inode *ip = GFS2_I(inode);
1159 struct gfs2_sbd *sdp = GFS2_SB(inode);
b3b94faa 1160 struct buffer_head *dibh;
cd915493 1161 u32 ouid, ogid, nuid, ngid;
b3b94faa
DT
1162 int error;
1163
2933f925
SW
1164 ouid = inode->i_uid;
1165 ogid = inode->i_gid;
b3b94faa
DT
1166 nuid = attr->ia_uid;
1167 ngid = attr->ia_gid;
1168
1169 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1170 ouid = nuid = NO_QUOTA_CHANGE;
1171 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1172 ogid = ngid = NO_QUOTA_CHANGE;
1173
182fe5ab
CG
1174 if (!gfs2_alloc_get(ip))
1175 return -ENOMEM;
b3b94faa
DT
1176
1177 error = gfs2_quota_lock(ip, nuid, ngid);
1178 if (error)
1179 goto out_alloc;
1180
1181 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1182 error = gfs2_quota_check(ip, nuid, ngid);
1183 if (error)
1184 goto out_gunlock_q;
1185 }
1186
1187 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1188 if (error)
1189 goto out_gunlock_q;
1190
1191 error = gfs2_meta_inode_buffer(ip, &dibh);
1192 if (error)
1193 goto out_end_trans;
1194
1195 error = inode_setattr(inode, attr);
1196 gfs2_assert_warn(sdp, !error);
b3b94faa 1197
d4e9c4c3 1198 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 1199 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
1200 brelse(dibh);
1201
1202 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
77658aad
SW
1203 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1204 gfs2_quota_change(ip, -blocks, ouid, ogid);
1205 gfs2_quota_change(ip, blocks, nuid, ngid);
b3b94faa
DT
1206 }
1207
a91ea69f 1208out_end_trans:
b3b94faa 1209 gfs2_trans_end(sdp);
a91ea69f 1210out_gunlock_q:
b3b94faa 1211 gfs2_quota_unlock(ip);
a91ea69f 1212out_alloc:
b3b94faa 1213 gfs2_alloc_put(ip);
b3b94faa
DT
1214 return error;
1215}
1216
1217/**
1218 * gfs2_setattr - Change attributes on an inode
1219 * @dentry: The dentry which is changing
1220 * @attr: The structure describing the change
1221 *
1222 * The VFS layer wants to change one or more of an inodes attributes. Write
1223 * that change out to disk.
1224 *
1225 * Returns: errno
1226 */
1227
1228static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1229{
1230 struct inode *inode = dentry->d_inode;
feaa7bba 1231 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1232 struct gfs2_holder i_gh;
1233 int error;
1234
b3b94faa
DT
1235 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1236 if (error)
1237 return error;
1238
1239 error = -EPERM;
1240 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1241 goto out;
1242
1243 error = inode_change_ok(inode, attr);
1244 if (error)
1245 goto out;
1246
1247 if (attr->ia_valid & ATTR_SIZE)
1248 error = setattr_size(inode, attr);
1249 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1250 error = setattr_chown(inode, attr);
1251 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1252 error = gfs2_acl_chmod(ip, attr);
1253 else
1254 error = gfs2_setattr_simple(ip, attr);
1255
a91ea69f 1256out:
b3b94faa 1257 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
1258 if (!error)
1259 mark_inode_dirty(inode);
b3b94faa
DT
1260 return error;
1261}
1262
1263/**
1264 * gfs2_getattr - Read out an inode's attributes
26c1a574 1265 * @mnt: The vfsmount the inode is being accessed from
b3b94faa
DT
1266 * @dentry: The dentry to stat
1267 * @stat: The inode's stats
1268 *
dcf3dd85
SW
1269 * This may be called from the VFS directly, or from within GFS2 with the
1270 * inode locked, so we look to see if the glock is already locked and only
1271 * lock the glock if its not already been done. Note that its the NFS
1272 * readdirplus operation which causes this to be called (from filldir)
1273 * with the glock already held.
1274 *
b3b94faa
DT
1275 * Returns: errno
1276 */
1277
1278static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1279 struct kstat *stat)
1280{
1281 struct inode *inode = dentry->d_inode;
feaa7bba 1282 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1283 struct gfs2_holder gh;
1284 int error;
dcf3dd85 1285 int unlock = 0;
b3b94faa 1286
7afd88d9 1287 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
dcf3dd85
SW
1288 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1289 if (error)
1290 return error;
1291 unlock = 1;
b3b94faa
DT
1292 }
1293
dcf3dd85 1294 generic_fillattr(inode, stat);
d7c103d0 1295 if (unlock)
dcf3dd85
SW
1296 gfs2_glock_dq_uninit(&gh);
1297
1298 return 0;
b3b94faa
DT
1299}
1300
1301static int gfs2_setxattr(struct dentry *dentry, const char *name,
1302 const void *data, size_t size, int flags)
1303{
feaa7bba 1304 struct inode *inode = dentry->d_inode;
b3b94faa
DT
1305 struct gfs2_ea_request er;
1306
b3b94faa
DT
1307 memset(&er, 0, sizeof(struct gfs2_ea_request));
1308 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1309 if (er.er_type == GFS2_EATYPE_UNUSED)
1310 return -EOPNOTSUPP;
1311 er.er_data = (char *)data;
1312 er.er_name_len = strlen(er.er_name);
1313 er.er_data_len = size;
1314 er.er_flags = flags;
1315
feaa7bba 1316 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
b3b94faa 1317
feaa7bba 1318 return gfs2_ea_set(GFS2_I(inode), &er);
b3b94faa
DT
1319}
1320
1321static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1322 void *data, size_t size)
1323{
1324 struct gfs2_ea_request er;
1325
b3b94faa
DT
1326 memset(&er, 0, sizeof(struct gfs2_ea_request));
1327 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1328 if (er.er_type == GFS2_EATYPE_UNUSED)
1329 return -EOPNOTSUPP;
1330 er.er_data = data;
1331 er.er_name_len = strlen(er.er_name);
1332 er.er_data_len = size;
1333
feaa7bba 1334 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1335}
1336
1337static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1338{
1339 struct gfs2_ea_request er;
1340
b3b94faa
DT
1341 memset(&er, 0, sizeof(struct gfs2_ea_request));
1342 er.er_data = (size) ? buffer : NULL;
1343 er.er_data_len = size;
1344
feaa7bba 1345 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1346}
1347
1348static int gfs2_removexattr(struct dentry *dentry, const char *name)
1349{
1350 struct gfs2_ea_request er;
1351
b3b94faa
DT
1352 memset(&er, 0, sizeof(struct gfs2_ea_request));
1353 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1354 if (er.er_type == GFS2_EATYPE_UNUSED)
1355 return -EOPNOTSUPP;
1356 er.er_name_len = strlen(er.er_name);
1357
feaa7bba 1358 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1359}
1360
e9079cce
SW
1361static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1362 u64 start, u64 len)
1363{
1364 struct gfs2_inode *ip = GFS2_I(inode);
1365 struct gfs2_holder gh;
1366 int ret;
1367
1368 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1369 if (ret)
1370 return ret;
1371
1372 mutex_lock(&inode->i_mutex);
1373
1374 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1375 if (ret)
1376 goto out;
1377
1378 if (gfs2_is_stuffed(ip)) {
1379 u64 phys = ip->i_no_addr << inode->i_blkbits;
1380 u64 size = i_size_read(inode);
1381 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1382 FIEMAP_EXTENT_DATA_INLINE;
1383 phys += sizeof(struct gfs2_dinode);
1384 phys += start;
1385 if (start + len > size)
1386 len = size - start;
1387 if (start < size)
1388 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1389 len, flags);
1390 if (ret == 1)
1391 ret = 0;
1392 } else {
1393 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1394 gfs2_block_map);
1395 }
1396
1397 gfs2_glock_dq_uninit(&gh);
1398out:
1399 mutex_unlock(&inode->i_mutex);
1400 return ret;
1401}
1402
92e1d5be 1403const struct inode_operations gfs2_file_iops = {
e6305c43 1404 .permission = gfs2_permission,
b3b94faa
DT
1405 .setattr = gfs2_setattr,
1406 .getattr = gfs2_getattr,
1407 .setxattr = gfs2_setxattr,
1408 .getxattr = gfs2_getxattr,
1409 .listxattr = gfs2_listxattr,
1410 .removexattr = gfs2_removexattr,
e9079cce 1411 .fiemap = gfs2_fiemap,
b3b94faa
DT
1412};
1413
92e1d5be 1414const struct inode_operations gfs2_dir_iops = {
b3b94faa
DT
1415 .create = gfs2_create,
1416 .lookup = gfs2_lookup,
1417 .link = gfs2_link,
1418 .unlink = gfs2_unlink,
1419 .symlink = gfs2_symlink,
1420 .mkdir = gfs2_mkdir,
1421 .rmdir = gfs2_rmdir,
1422 .mknod = gfs2_mknod,
1423 .rename = gfs2_rename,
e6305c43 1424 .permission = gfs2_permission,
b3b94faa
DT
1425 .setattr = gfs2_setattr,
1426 .getattr = gfs2_getattr,
1427 .setxattr = gfs2_setxattr,
1428 .getxattr = gfs2_getxattr,
1429 .listxattr = gfs2_listxattr,
1430 .removexattr = gfs2_removexattr,
e9079cce 1431 .fiemap = gfs2_fiemap,
b3b94faa
DT
1432};
1433
92e1d5be 1434const struct inode_operations gfs2_symlink_iops = {
b3b94faa
DT
1435 .readlink = gfs2_readlink,
1436 .follow_link = gfs2_follow_link,
e6305c43 1437 .permission = gfs2_permission,
b3b94faa
DT
1438 .setattr = gfs2_setattr,
1439 .getattr = gfs2_getattr,
1440 .setxattr = gfs2_setxattr,
1441 .getxattr = gfs2_getxattr,
1442 .listxattr = gfs2_listxattr,
1443 .removexattr = gfs2_removexattr,
e9079cce 1444 .fiemap = gfs2_fiemap,
b3b94faa
DT
1445};
1446
This page took 0.45257 seconds and 5 git commands to generate.