[GFS2] Correct lock ordering in unlink
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / gfs2 / ops_inode.c
blob2cbe5a321e891d0609f06853dbc3761f5bfcd02b
1 /*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
8 */
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>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/crc32.h>
21 #include <linux/lm_interface.h>
22 #include <asm/uaccess.h>
24 #include "gfs2.h"
25 #include "incore.h"
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"
34 #include "ops_dentry.h"
35 #include "ops_inode.h"
36 #include "quota.h"
37 #include "rgrp.h"
38 #include "trans.h"
39 #include "util.h"
41 /**
42 * gfs2_create - Create a file
43 * @dir: The directory in which to create the file
44 * @dentry: The dentry of the new file
45 * @mode: The mode of the new file
47 * Returns: errno
50 static int gfs2_create(struct inode *dir, struct dentry *dentry,
51 int mode, struct nameidata *nd)
53 struct gfs2_inode *dip = GFS2_I(dir);
54 struct gfs2_sbd *sdp = GFS2_SB(dir);
55 struct gfs2_holder ghs[2];
56 struct inode *inode;
58 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
60 for (;;) {
61 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
62 if (!IS_ERR(inode)) {
63 gfs2_trans_end(sdp);
64 if (dip->i_alloc.al_rgd)
65 gfs2_inplace_release(dip);
66 gfs2_quota_unlock(dip);
67 gfs2_alloc_put(dip);
68 gfs2_glock_dq_uninit_m(2, ghs);
69 mark_inode_dirty(inode);
70 break;
71 } else if (PTR_ERR(inode) != -EEXIST ||
72 (nd && (nd->intent.open.flags & O_EXCL))) {
73 gfs2_holder_uninit(ghs);
74 return PTR_ERR(inode);
77 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
78 if (inode) {
79 if (!IS_ERR(inode)) {
80 gfs2_holder_uninit(ghs);
81 break;
82 } else {
83 gfs2_holder_uninit(ghs);
84 return PTR_ERR(inode);
89 d_instantiate(dentry, inode);
91 return 0;
94 /**
95 * gfs2_lookup - Look up a filename in a directory and return its inode
96 * @dir: The directory inode
97 * @dentry: The dentry of the new inode
98 * @nd: passed from Linux VFS, ignored by us
100 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
102 * Returns: errno
105 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
106 struct nameidata *nd)
108 struct inode *inode = NULL;
110 dentry->d_op = &gfs2_dops;
112 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
113 if (inode && IS_ERR(inode))
114 return ERR_PTR(PTR_ERR(inode));
116 if (inode)
117 return d_splice_alias(inode, dentry);
118 d_add(dentry, inode);
120 return NULL;
124 * gfs2_link - Link to a file
125 * @old_dentry: The inode to link
126 * @dir: Add link to this directory
127 * @dentry: The name of the link
129 * Link the inode in "old_dentry" into the directory "dir" with the
130 * name in "dentry".
132 * Returns: errno
135 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
136 struct dentry *dentry)
138 struct gfs2_inode *dip = GFS2_I(dir);
139 struct gfs2_sbd *sdp = GFS2_SB(dir);
140 struct inode *inode = old_dentry->d_inode;
141 struct gfs2_inode *ip = GFS2_I(inode);
142 struct gfs2_holder ghs[2];
143 int alloc_required;
144 int error;
146 if (S_ISDIR(inode->i_mode))
147 return -EPERM;
149 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
150 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
152 error = gfs2_glock_nq_m(2, ghs);
153 if (error)
154 goto out;
156 error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
157 if (error)
158 goto out_gunlock;
160 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
161 switch (error) {
162 case -ENOENT:
163 break;
164 case 0:
165 error = -EEXIST;
166 default:
167 goto out_gunlock;
170 error = -EINVAL;
171 if (!dip->i_inode.i_nlink)
172 goto out_gunlock;
173 error = -EFBIG;
174 if (dip->i_di.di_entries == (u32)-1)
175 goto out_gunlock;
176 error = -EPERM;
177 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
178 goto out_gunlock;
179 error = -EINVAL;
180 if (!ip->i_inode.i_nlink)
181 goto out_gunlock;
182 error = -EMLINK;
183 if (ip->i_inode.i_nlink == (u32)-1)
184 goto out_gunlock;
186 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
187 if (error < 0)
188 goto out_gunlock;
189 error = 0;
191 if (alloc_required) {
192 struct gfs2_alloc *al = gfs2_alloc_get(dip);
194 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
195 if (error)
196 goto out_alloc;
198 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
199 if (error)
200 goto out_gunlock_q;
202 al->al_requested = sdp->sd_max_dirres;
204 error = gfs2_inplace_reserve(dip);
205 if (error)
206 goto out_gunlock_q;
208 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
209 al->al_rgd->rd_length +
210 2 * RES_DINODE + RES_STATFS +
211 RES_QUOTA, 0);
212 if (error)
213 goto out_ipres;
214 } else {
215 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
216 if (error)
217 goto out_ipres;
220 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
221 if (error)
222 goto out_end_trans;
224 error = gfs2_change_nlink(ip, +1);
226 out_end_trans:
227 gfs2_trans_end(sdp);
228 out_ipres:
229 if (alloc_required)
230 gfs2_inplace_release(dip);
231 out_gunlock_q:
232 if (alloc_required)
233 gfs2_quota_unlock(dip);
234 out_alloc:
235 if (alloc_required)
236 gfs2_alloc_put(dip);
237 out_gunlock:
238 gfs2_glock_dq_m(2, ghs);
239 out:
240 gfs2_holder_uninit(ghs);
241 gfs2_holder_uninit(ghs + 1);
242 if (!error) {
243 atomic_inc(&inode->i_count);
244 d_instantiate(dentry, inode);
245 mark_inode_dirty(inode);
247 return error;
251 * gfs2_unlink - Unlink a file
252 * @dir: The inode of the directory containing the file to unlink
253 * @dentry: The file itself
255 * Unlink a file. Call gfs2_unlinki()
257 * Returns: errno
260 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
262 struct gfs2_inode *dip = GFS2_I(dir);
263 struct gfs2_sbd *sdp = GFS2_SB(dir);
264 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
265 struct gfs2_holder ghs[3];
266 struct gfs2_rgrpd *rgd;
267 struct gfs2_holder ri_gh;
268 int error;
270 error = gfs2_rindex_hold(sdp, &ri_gh);
271 if (error)
272 return error;
274 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
275 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
277 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
278 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
281 error = gfs2_glock_nq(ghs); /* parent */
282 if (error)
283 goto out_parent;
285 error = gfs2_glock_nq(ghs + 1); /* child */
286 if (error)
287 goto out_child;
289 error = gfs2_glock_nq(ghs + 2); /* rgrp */
290 if (error)
291 goto out_rgrp;
293 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
294 if (error)
295 goto out_rgrp;
297 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
298 if (error)
299 goto out_rgrp;
301 error = gfs2_dir_del(dip, &dentry->d_name);
302 if (error)
303 goto out_end_trans;
305 error = gfs2_change_nlink(ip, -1);
307 out_end_trans:
308 gfs2_trans_end(sdp);
309 gfs2_glock_dq(ghs + 2);
310 out_rgrp:
311 gfs2_holder_uninit(ghs + 2);
312 gfs2_glock_dq(ghs + 1);
313 out_child:
314 gfs2_holder_uninit(ghs + 1);
315 gfs2_glock_dq(ghs);
316 out_parent:
317 gfs2_holder_uninit(ghs);
318 gfs2_glock_dq_uninit(&ri_gh);
319 return error;
323 * gfs2_symlink - Create a symlink
324 * @dir: The directory to create the symlink in
325 * @dentry: The dentry to put the symlink in
326 * @symname: The thing which the link points to
328 * Returns: errno
331 static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
332 const char *symname)
334 struct gfs2_inode *dip = GFS2_I(dir), *ip;
335 struct gfs2_sbd *sdp = GFS2_SB(dir);
336 struct gfs2_holder ghs[2];
337 struct inode *inode;
338 struct buffer_head *dibh;
339 int size;
340 int error;
342 /* Must be stuffed with a null terminator for gfs2_follow_link() */
343 size = strlen(symname);
344 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
345 return -ENAMETOOLONG;
347 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
349 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
350 if (IS_ERR(inode)) {
351 gfs2_holder_uninit(ghs);
352 return PTR_ERR(inode);
355 ip = ghs[1].gh_gl->gl_object;
357 ip->i_di.di_size = size;
359 error = gfs2_meta_inode_buffer(ip, &dibh);
361 if (!gfs2_assert_withdraw(sdp, !error)) {
362 gfs2_dinode_out(ip, dibh->b_data);
363 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
364 size);
365 brelse(dibh);
368 gfs2_trans_end(sdp);
369 if (dip->i_alloc.al_rgd)
370 gfs2_inplace_release(dip);
371 gfs2_quota_unlock(dip);
372 gfs2_alloc_put(dip);
374 gfs2_glock_dq_uninit_m(2, ghs);
376 d_instantiate(dentry, inode);
377 mark_inode_dirty(inode);
379 return 0;
383 * gfs2_mkdir - Make a directory
384 * @dir: The parent directory of the new one
385 * @dentry: The dentry of the new directory
386 * @mode: The mode of the new directory
388 * Returns: errno
391 static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
393 struct gfs2_inode *dip = GFS2_I(dir), *ip;
394 struct gfs2_sbd *sdp = GFS2_SB(dir);
395 struct gfs2_holder ghs[2];
396 struct inode *inode;
397 struct buffer_head *dibh;
398 int error;
400 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
402 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
403 if (IS_ERR(inode)) {
404 gfs2_holder_uninit(ghs);
405 return PTR_ERR(inode);
408 ip = ghs[1].gh_gl->gl_object;
410 ip->i_inode.i_nlink = 2;
411 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
412 ip->i_di.di_flags |= GFS2_DIF_JDATA;
413 ip->i_di.di_entries = 2;
415 error = gfs2_meta_inode_buffer(ip, &dibh);
417 if (!gfs2_assert_withdraw(sdp, !error)) {
418 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
419 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
420 struct qstr str;
422 gfs2_str2qstr(&str, ".");
423 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
424 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
425 dent->de_inum = di->di_num; /* already GFS2 endian */
426 dent->de_type = cpu_to_be16(DT_DIR);
427 di->di_entries = cpu_to_be32(1);
429 gfs2_str2qstr(&str, "..");
430 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
431 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
433 gfs2_inum_out(dip, dent);
434 dent->de_type = cpu_to_be16(DT_DIR);
436 gfs2_dinode_out(ip, di);
438 brelse(dibh);
441 error = gfs2_change_nlink(dip, +1);
442 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
444 gfs2_trans_end(sdp);
445 if (dip->i_alloc.al_rgd)
446 gfs2_inplace_release(dip);
447 gfs2_quota_unlock(dip);
448 gfs2_alloc_put(dip);
450 gfs2_glock_dq_uninit_m(2, ghs);
452 d_instantiate(dentry, inode);
453 mark_inode_dirty(inode);
455 return 0;
459 * gfs2_rmdir - Remove a directory
460 * @dir: The parent directory of the directory to be removed
461 * @dentry: The dentry of the directory to remove
463 * Remove a directory. Call gfs2_rmdiri()
465 * Returns: errno
468 static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
470 struct gfs2_inode *dip = GFS2_I(dir);
471 struct gfs2_sbd *sdp = GFS2_SB(dir);
472 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
473 struct gfs2_holder ghs[3];
474 struct gfs2_rgrpd *rgd;
475 struct gfs2_holder ri_gh;
476 int error;
479 error = gfs2_rindex_hold(sdp, &ri_gh);
480 if (error)
481 return error;
482 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
483 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
485 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
486 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
488 error = gfs2_glock_nq_m(3, ghs);
489 if (error)
490 goto out;
492 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
493 if (error)
494 goto out_gunlock;
496 if (ip->i_di.di_entries < 2) {
497 if (gfs2_consist_inode(ip))
498 gfs2_dinode_print(ip);
499 error = -EIO;
500 goto out_gunlock;
502 if (ip->i_di.di_entries > 2) {
503 error = -ENOTEMPTY;
504 goto out_gunlock;
507 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
508 if (error)
509 goto out_gunlock;
511 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
513 gfs2_trans_end(sdp);
515 out_gunlock:
516 gfs2_glock_dq_m(3, ghs);
517 out:
518 gfs2_holder_uninit(ghs);
519 gfs2_holder_uninit(ghs + 1);
520 gfs2_holder_uninit(ghs + 2);
521 gfs2_glock_dq_uninit(&ri_gh);
522 return error;
526 * gfs2_mknod - Make a special file
527 * @dir: The directory in which the special file will reside
528 * @dentry: The dentry of the special file
529 * @mode: The mode of the special file
530 * @rdev: The device specification of the special file
534 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
535 dev_t dev)
537 struct gfs2_inode *dip = GFS2_I(dir);
538 struct gfs2_sbd *sdp = GFS2_SB(dir);
539 struct gfs2_holder ghs[2];
540 struct inode *inode;
542 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
544 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
545 if (IS_ERR(inode)) {
546 gfs2_holder_uninit(ghs);
547 return PTR_ERR(inode);
550 gfs2_trans_end(sdp);
551 if (dip->i_alloc.al_rgd)
552 gfs2_inplace_release(dip);
553 gfs2_quota_unlock(dip);
554 gfs2_alloc_put(dip);
556 gfs2_glock_dq_uninit_m(2, ghs);
558 d_instantiate(dentry, inode);
559 mark_inode_dirty(inode);
561 return 0;
565 * gfs2_rename - Rename a file
566 * @odir: Parent directory of old file name
567 * @odentry: The old dentry of the file
568 * @ndir: Parent directory of new file name
569 * @ndentry: The new dentry of the file
571 * Returns: errno
574 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
575 struct inode *ndir, struct dentry *ndentry)
577 struct gfs2_inode *odip = GFS2_I(odir);
578 struct gfs2_inode *ndip = GFS2_I(ndir);
579 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
580 struct gfs2_inode *nip = NULL;
581 struct gfs2_sbd *sdp = GFS2_SB(odir);
582 struct gfs2_holder ghs[5], r_gh;
583 struct gfs2_rgrpd *nrgd;
584 unsigned int num_gh;
585 int dir_rename = 0;
586 int alloc_required;
587 unsigned int x;
588 int error;
590 if (ndentry->d_inode) {
591 nip = GFS2_I(ndentry->d_inode);
592 if (ip == nip)
593 return 0;
596 /* Make sure we aren't trying to move a dirctory into it's subdir */
598 if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
599 dir_rename = 1;
601 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
602 &r_gh);
603 if (error)
604 goto out;
606 error = gfs2_ok_to_move(ip, ndip);
607 if (error)
608 goto out_gunlock_r;
611 num_gh = 1;
612 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
613 if (odip != ndip) {
614 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
615 num_gh++;
617 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
618 num_gh++;
620 if (nip) {
621 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
622 num_gh++;
623 /* grab the resource lock for unlink flag twiddling
624 * this is the case of the target file already existing
625 * so we unlink before doing the rename
627 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
628 if (nrgd)
629 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
632 error = gfs2_glock_nq_m(num_gh, ghs);
633 if (error)
634 goto out_uninit;
636 /* Check out the old directory */
638 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
639 if (error)
640 goto out_gunlock;
642 /* Check out the new directory */
644 if (nip) {
645 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
646 if (error)
647 goto out_gunlock;
649 if (S_ISDIR(nip->i_inode.i_mode)) {
650 if (nip->i_di.di_entries < 2) {
651 if (gfs2_consist_inode(nip))
652 gfs2_dinode_print(nip);
653 error = -EIO;
654 goto out_gunlock;
656 if (nip->i_di.di_entries > 2) {
657 error = -ENOTEMPTY;
658 goto out_gunlock;
661 } else {
662 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
663 if (error)
664 goto out_gunlock;
666 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
667 switch (error) {
668 case -ENOENT:
669 error = 0;
670 break;
671 case 0:
672 error = -EEXIST;
673 default:
674 goto out_gunlock;
677 if (odip != ndip) {
678 if (!ndip->i_inode.i_nlink) {
679 error = -EINVAL;
680 goto out_gunlock;
682 if (ndip->i_di.di_entries == (u32)-1) {
683 error = -EFBIG;
684 goto out_gunlock;
686 if (S_ISDIR(ip->i_inode.i_mode) &&
687 ndip->i_inode.i_nlink == (u32)-1) {
688 error = -EMLINK;
689 goto out_gunlock;
694 /* Check out the dir to be renamed */
696 if (dir_rename) {
697 error = permission(odentry->d_inode, MAY_WRITE, NULL);
698 if (error)
699 goto out_gunlock;
702 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
703 if (error < 0)
704 goto out_gunlock;
705 error = 0;
707 if (alloc_required) {
708 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
710 error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
711 if (error)
712 goto out_alloc;
714 error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
715 if (error)
716 goto out_gunlock_q;
718 al->al_requested = sdp->sd_max_dirres;
720 error = gfs2_inplace_reserve(ndip);
721 if (error)
722 goto out_gunlock_q;
724 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
725 al->al_rgd->rd_length +
726 4 * RES_DINODE + 4 * RES_LEAF +
727 RES_STATFS + RES_QUOTA + 4, 0);
728 if (error)
729 goto out_ipreserv;
730 } else {
731 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
732 5 * RES_LEAF + 4, 0);
733 if (error)
734 goto out_gunlock;
737 /* Remove the target file, if it exists */
739 if (nip) {
740 if (S_ISDIR(nip->i_inode.i_mode))
741 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
742 else {
743 error = gfs2_dir_del(ndip, &ndentry->d_name);
744 if (error)
745 goto out_end_trans;
746 error = gfs2_change_nlink(nip, -1);
748 if (error)
749 goto out_end_trans;
752 if (dir_rename) {
753 struct qstr name;
754 gfs2_str2qstr(&name, "..");
756 error = gfs2_change_nlink(ndip, +1);
757 if (error)
758 goto out_end_trans;
759 error = gfs2_change_nlink(odip, -1);
760 if (error)
761 goto out_end_trans;
763 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
764 if (error)
765 goto out_end_trans;
766 } else {
767 struct buffer_head *dibh;
768 error = gfs2_meta_inode_buffer(ip, &dibh);
769 if (error)
770 goto out_end_trans;
771 ip->i_inode.i_ctime = CURRENT_TIME;
772 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
773 gfs2_dinode_out(ip, dibh->b_data);
774 brelse(dibh);
777 error = gfs2_dir_del(odip, &odentry->d_name);
778 if (error)
779 goto out_end_trans;
781 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
782 if (error)
783 goto out_end_trans;
785 out_end_trans:
786 gfs2_trans_end(sdp);
787 out_ipreserv:
788 if (alloc_required)
789 gfs2_inplace_release(ndip);
790 out_gunlock_q:
791 if (alloc_required)
792 gfs2_quota_unlock(ndip);
793 out_alloc:
794 if (alloc_required)
795 gfs2_alloc_put(ndip);
796 out_gunlock:
797 gfs2_glock_dq_m(num_gh, ghs);
798 out_uninit:
799 for (x = 0; x < num_gh; x++)
800 gfs2_holder_uninit(ghs + x);
801 out_gunlock_r:
802 if (dir_rename)
803 gfs2_glock_dq_uninit(&r_gh);
804 out:
805 return error;
809 * gfs2_readlink - Read the value of a symlink
810 * @dentry: the symlink
811 * @buf: the buffer to read the symlink data into
812 * @size: the size of the buffer
814 * Returns: errno
817 static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
818 int user_size)
820 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
821 char array[GFS2_FAST_NAME_SIZE], *buf = array;
822 unsigned int len = GFS2_FAST_NAME_SIZE;
823 int error;
825 error = gfs2_readlinki(ip, &buf, &len);
826 if (error)
827 return error;
829 if (user_size > len - 1)
830 user_size = len - 1;
832 if (copy_to_user(user_buf, buf, user_size))
833 error = -EFAULT;
834 else
835 error = user_size;
837 if (buf != array)
838 kfree(buf);
840 return error;
844 * gfs2_follow_link - Follow a symbolic link
845 * @dentry: The dentry of the link
846 * @nd: Data that we pass to vfs_follow_link()
848 * This can handle symlinks of any size. It is optimised for symlinks
849 * under GFS2_FAST_NAME_SIZE.
851 * Returns: 0 on success or error code
854 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
856 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
857 char array[GFS2_FAST_NAME_SIZE], *buf = array;
858 unsigned int len = GFS2_FAST_NAME_SIZE;
859 int error;
861 error = gfs2_readlinki(ip, &buf, &len);
862 if (!error) {
863 error = vfs_follow_link(nd, buf);
864 if (buf != array)
865 kfree(buf);
868 return ERR_PTR(error);
872 * gfs2_permission -
873 * @inode:
874 * @mask:
875 * @nd: passed from Linux VFS, ignored by us
877 * This may be called from the VFS directly, or from within GFS2 with the
878 * inode locked, so we look to see if the glock is already locked and only
879 * lock the glock if its not already been done.
881 * Returns: errno
884 static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
886 struct gfs2_inode *ip = GFS2_I(inode);
887 struct gfs2_holder i_gh;
888 int error;
889 int unlock = 0;
891 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
892 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
893 if (error)
894 return error;
895 unlock = 1;
898 error = generic_permission(inode, mask, gfs2_check_acl);
899 if (unlock)
900 gfs2_glock_dq_uninit(&i_gh);
902 return error;
905 static int setattr_size(struct inode *inode, struct iattr *attr)
907 struct gfs2_inode *ip = GFS2_I(inode);
908 int error;
910 if (attr->ia_size != ip->i_di.di_size) {
911 error = vmtruncate(inode, attr->ia_size);
912 if (error)
913 return error;
916 error = gfs2_truncatei(ip, attr->ia_size);
917 if (error && (inode->i_size != ip->i_di.di_size))
918 i_size_write(inode, ip->i_di.di_size);
920 return error;
923 static int setattr_chown(struct inode *inode, struct iattr *attr)
925 struct gfs2_inode *ip = GFS2_I(inode);
926 struct gfs2_sbd *sdp = GFS2_SB(inode);
927 struct buffer_head *dibh;
928 u32 ouid, ogid, nuid, ngid;
929 int error;
931 ouid = inode->i_uid;
932 ogid = inode->i_gid;
933 nuid = attr->ia_uid;
934 ngid = attr->ia_gid;
936 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
937 ouid = nuid = NO_QUOTA_CHANGE;
938 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
939 ogid = ngid = NO_QUOTA_CHANGE;
941 gfs2_alloc_get(ip);
943 error = gfs2_quota_lock(ip, nuid, ngid);
944 if (error)
945 goto out_alloc;
947 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
948 error = gfs2_quota_check(ip, nuid, ngid);
949 if (error)
950 goto out_gunlock_q;
953 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
954 if (error)
955 goto out_gunlock_q;
957 error = gfs2_meta_inode_buffer(ip, &dibh);
958 if (error)
959 goto out_end_trans;
961 error = inode_setattr(inode, attr);
962 gfs2_assert_warn(sdp, !error);
964 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
965 gfs2_dinode_out(ip, dibh->b_data);
966 brelse(dibh);
968 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
969 gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
970 gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
973 out_end_trans:
974 gfs2_trans_end(sdp);
975 out_gunlock_q:
976 gfs2_quota_unlock(ip);
977 out_alloc:
978 gfs2_alloc_put(ip);
979 return error;
983 * gfs2_setattr - Change attributes on an inode
984 * @dentry: The dentry which is changing
985 * @attr: The structure describing the change
987 * The VFS layer wants to change one or more of an inodes attributes. Write
988 * that change out to disk.
990 * Returns: errno
993 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
995 struct inode *inode = dentry->d_inode;
996 struct gfs2_inode *ip = GFS2_I(inode);
997 struct gfs2_holder i_gh;
998 int error;
1000 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1001 if (error)
1002 return error;
1004 error = -EPERM;
1005 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1006 goto out;
1008 error = inode_change_ok(inode, attr);
1009 if (error)
1010 goto out;
1012 if (attr->ia_valid & ATTR_SIZE)
1013 error = setattr_size(inode, attr);
1014 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1015 error = setattr_chown(inode, attr);
1016 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1017 error = gfs2_acl_chmod(ip, attr);
1018 else
1019 error = gfs2_setattr_simple(ip, attr);
1021 out:
1022 gfs2_glock_dq_uninit(&i_gh);
1023 if (!error)
1024 mark_inode_dirty(inode);
1025 return error;
1029 * gfs2_getattr - Read out an inode's attributes
1030 * @mnt: The vfsmount the inode is being accessed from
1031 * @dentry: The dentry to stat
1032 * @stat: The inode's stats
1034 * This may be called from the VFS directly, or from within GFS2 with the
1035 * inode locked, so we look to see if the glock is already locked and only
1036 * lock the glock if its not already been done. Note that its the NFS
1037 * readdirplus operation which causes this to be called (from filldir)
1038 * with the glock already held.
1040 * Returns: errno
1043 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1044 struct kstat *stat)
1046 struct inode *inode = dentry->d_inode;
1047 struct gfs2_inode *ip = GFS2_I(inode);
1048 struct gfs2_holder gh;
1049 int error;
1050 int unlock = 0;
1052 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
1053 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1054 if (error)
1055 return error;
1056 unlock = 1;
1059 generic_fillattr(inode, stat);
1060 if (unlock)
1061 gfs2_glock_dq_uninit(&gh);
1063 return 0;
1066 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1067 const void *data, size_t size, int flags)
1069 struct inode *inode = dentry->d_inode;
1070 struct gfs2_ea_request er;
1072 memset(&er, 0, sizeof(struct gfs2_ea_request));
1073 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1074 if (er.er_type == GFS2_EATYPE_UNUSED)
1075 return -EOPNOTSUPP;
1076 er.er_data = (char *)data;
1077 er.er_name_len = strlen(er.er_name);
1078 er.er_data_len = size;
1079 er.er_flags = flags;
1081 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
1083 return gfs2_ea_set(GFS2_I(inode), &er);
1086 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1087 void *data, size_t size)
1089 struct gfs2_ea_request er;
1091 memset(&er, 0, sizeof(struct gfs2_ea_request));
1092 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1093 if (er.er_type == GFS2_EATYPE_UNUSED)
1094 return -EOPNOTSUPP;
1095 er.er_data = data;
1096 er.er_name_len = strlen(er.er_name);
1097 er.er_data_len = size;
1099 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
1102 static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1104 struct gfs2_ea_request er;
1106 memset(&er, 0, sizeof(struct gfs2_ea_request));
1107 er.er_data = (size) ? buffer : NULL;
1108 er.er_data_len = size;
1110 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
1113 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1115 struct gfs2_ea_request er;
1117 memset(&er, 0, sizeof(struct gfs2_ea_request));
1118 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1119 if (er.er_type == GFS2_EATYPE_UNUSED)
1120 return -EOPNOTSUPP;
1121 er.er_name_len = strlen(er.er_name);
1123 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
1126 const struct inode_operations gfs2_file_iops = {
1127 .permission = gfs2_permission,
1128 .setattr = gfs2_setattr,
1129 .getattr = gfs2_getattr,
1130 .setxattr = gfs2_setxattr,
1131 .getxattr = gfs2_getxattr,
1132 .listxattr = gfs2_listxattr,
1133 .removexattr = gfs2_removexattr,
1136 const struct inode_operations gfs2_dev_iops = {
1137 .permission = gfs2_permission,
1138 .setattr = gfs2_setattr,
1139 .getattr = gfs2_getattr,
1140 .setxattr = gfs2_setxattr,
1141 .getxattr = gfs2_getxattr,
1142 .listxattr = gfs2_listxattr,
1143 .removexattr = gfs2_removexattr,
1146 const struct inode_operations gfs2_dir_iops = {
1147 .create = gfs2_create,
1148 .lookup = gfs2_lookup,
1149 .link = gfs2_link,
1150 .unlink = gfs2_unlink,
1151 .symlink = gfs2_symlink,
1152 .mkdir = gfs2_mkdir,
1153 .rmdir = gfs2_rmdir,
1154 .mknod = gfs2_mknod,
1155 .rename = gfs2_rename,
1156 .permission = gfs2_permission,
1157 .setattr = gfs2_setattr,
1158 .getattr = gfs2_getattr,
1159 .setxattr = gfs2_setxattr,
1160 .getxattr = gfs2_getxattr,
1161 .listxattr = gfs2_listxattr,
1162 .removexattr = gfs2_removexattr,
1165 const struct inode_operations gfs2_symlink_iops = {
1166 .readlink = gfs2_readlink,
1167 .follow_link = gfs2_follow_link,
1168 .permission = gfs2_permission,
1169 .setattr = gfs2_setattr,
1170 .getattr = gfs2_getattr,
1171 .setxattr = gfs2_setxattr,
1172 .getxattr = gfs2_getxattr,
1173 .listxattr = gfs2_listxattr,
1174 .removexattr = gfs2_removexattr,