MIPS: Alchemy: prom_putchar is board dependent
[linux-2.6/linux-mips.git] / fs / gfs2 / ops_inode.c
blob84350e1be66d2b286e306b13395953557ace4be8
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/mm.h>
16 #include <linux/xattr.h>
17 #include <linux/posix_acl.h>
18 #include <linux/gfs2_ondisk.h>
19 #include <linux/crc32.h>
20 #include <linux/fiemap.h>
21 #include <asm/uaccess.h>
23 #include "gfs2.h"
24 #include "incore.h"
25 #include "acl.h"
26 #include "bmap.h"
27 #include "dir.h"
28 #include "xattr.h"
29 #include "glock.h"
30 #include "inode.h"
31 #include "meta_io.h"
32 #include "quota.h"
33 #include "rgrp.h"
34 #include "trans.h"
35 #include "util.h"
36 #include "super.h"
38 /**
39 * gfs2_create - Create a file
40 * @dir: The directory in which to create the file
41 * @dentry: The dentry of the new file
42 * @mode: The mode of the new file
44 * Returns: errno
47 static int gfs2_create(struct inode *dir, struct dentry *dentry,
48 int mode, struct nameidata *nd)
50 struct gfs2_inode *dip = GFS2_I(dir);
51 struct gfs2_sbd *sdp = GFS2_SB(dir);
52 struct gfs2_holder ghs[2];
53 struct inode *inode;
55 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
57 for (;;) {
58 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
59 if (!IS_ERR(inode)) {
60 gfs2_trans_end(sdp);
61 if (dip->i_alloc->al_rgd)
62 gfs2_inplace_release(dip);
63 gfs2_quota_unlock(dip);
64 gfs2_alloc_put(dip);
65 gfs2_glock_dq_uninit_m(2, ghs);
66 mark_inode_dirty(inode);
67 break;
68 } else if (PTR_ERR(inode) != -EEXIST ||
69 (nd && nd->flags & LOOKUP_EXCL)) {
70 gfs2_holder_uninit(ghs);
71 return PTR_ERR(inode);
74 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
75 if (inode) {
76 if (!IS_ERR(inode)) {
77 gfs2_holder_uninit(ghs);
78 break;
79 } else {
80 gfs2_holder_uninit(ghs);
81 return PTR_ERR(inode);
86 d_instantiate(dentry, inode);
88 return 0;
91 /**
92 * gfs2_lookup - Look up a filename in a directory and return its inode
93 * @dir: The directory inode
94 * @dentry: The dentry of the new inode
95 * @nd: passed from Linux VFS, ignored by us
97 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
99 * Returns: errno
102 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
103 struct nameidata *nd)
105 struct inode *inode = NULL;
107 dentry->d_op = &gfs2_dops;
109 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
110 if (inode && IS_ERR(inode))
111 return ERR_CAST(inode);
113 if (inode) {
114 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
115 struct gfs2_holder gh;
116 int error;
117 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
118 if (error) {
119 iput(inode);
120 return ERR_PTR(error);
122 gfs2_glock_dq_uninit(&gh);
123 return d_splice_alias(inode, dentry);
125 d_add(dentry, inode);
127 return NULL;
131 * gfs2_link - Link to a file
132 * @old_dentry: The inode to link
133 * @dir: Add link to this directory
134 * @dentry: The name of the link
136 * Link the inode in "old_dentry" into the directory "dir" with the
137 * name in "dentry".
139 * Returns: errno
142 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
143 struct dentry *dentry)
145 struct gfs2_inode *dip = GFS2_I(dir);
146 struct gfs2_sbd *sdp = GFS2_SB(dir);
147 struct inode *inode = old_dentry->d_inode;
148 struct gfs2_inode *ip = GFS2_I(inode);
149 struct gfs2_holder ghs[2];
150 int alloc_required;
151 int error;
153 if (S_ISDIR(inode->i_mode))
154 return -EPERM;
156 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
157 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
159 error = gfs2_glock_nq(ghs); /* parent */
160 if (error)
161 goto out_parent;
163 error = gfs2_glock_nq(ghs + 1); /* child */
164 if (error)
165 goto out_child;
167 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
168 if (error)
169 goto out_gunlock;
171 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
172 switch (error) {
173 case -ENOENT:
174 break;
175 case 0:
176 error = -EEXIST;
177 default:
178 goto out_gunlock;
181 error = -EINVAL;
182 if (!dip->i_inode.i_nlink)
183 goto out_gunlock;
184 error = -EFBIG;
185 if (dip->i_entries == (u32)-1)
186 goto out_gunlock;
187 error = -EPERM;
188 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
189 goto out_gunlock;
190 error = -EINVAL;
191 if (!ip->i_inode.i_nlink)
192 goto out_gunlock;
193 error = -EMLINK;
194 if (ip->i_inode.i_nlink == (u32)-1)
195 goto out_gunlock;
197 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
198 if (error < 0)
199 goto out_gunlock;
200 error = 0;
202 if (alloc_required) {
203 struct gfs2_alloc *al = gfs2_alloc_get(dip);
204 if (!al) {
205 error = -ENOMEM;
206 goto out_gunlock;
209 error = gfs2_quota_lock_check(dip);
210 if (error)
211 goto out_alloc;
213 al->al_requested = sdp->sd_max_dirres;
215 error = gfs2_inplace_reserve(dip);
216 if (error)
217 goto out_gunlock_q;
219 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
220 al->al_rgd->rd_length +
221 2 * RES_DINODE + RES_STATFS +
222 RES_QUOTA, 0);
223 if (error)
224 goto out_ipres;
225 } else {
226 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
227 if (error)
228 goto out_ipres;
231 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
232 if (error)
233 goto out_end_trans;
235 error = gfs2_change_nlink(ip, +1);
237 out_end_trans:
238 gfs2_trans_end(sdp);
239 out_ipres:
240 if (alloc_required)
241 gfs2_inplace_release(dip);
242 out_gunlock_q:
243 if (alloc_required)
244 gfs2_quota_unlock(dip);
245 out_alloc:
246 if (alloc_required)
247 gfs2_alloc_put(dip);
248 out_gunlock:
249 gfs2_glock_dq(ghs + 1);
250 out_child:
251 gfs2_glock_dq(ghs);
252 out_parent:
253 gfs2_holder_uninit(ghs);
254 gfs2_holder_uninit(ghs + 1);
255 if (!error) {
256 atomic_inc(&inode->i_count);
257 d_instantiate(dentry, inode);
258 mark_inode_dirty(inode);
260 return error;
264 * gfs2_unlink_ok - check to see that a inode is still in a directory
265 * @dip: the directory
266 * @name: the name of the file
267 * @ip: the inode
269 * Assumes that the lock on (at least) @dip is held.
271 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
274 static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
275 const struct gfs2_inode *ip)
277 int error;
279 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
280 return -EPERM;
282 if ((dip->i_inode.i_mode & S_ISVTX) &&
283 dip->i_inode.i_uid != current_fsuid() &&
284 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
285 return -EPERM;
287 if (IS_APPEND(&dip->i_inode))
288 return -EPERM;
290 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
291 if (error)
292 return error;
294 error = gfs2_dir_check(&dip->i_inode, name, ip);
295 if (error)
296 return error;
298 return 0;
302 * gfs2_unlink - Unlink a file
303 * @dir: The inode of the directory containing the file to unlink
304 * @dentry: The file itself
306 * Unlink a file. Call gfs2_unlinki()
308 * Returns: errno
311 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
313 struct gfs2_inode *dip = GFS2_I(dir);
314 struct gfs2_sbd *sdp = GFS2_SB(dir);
315 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
316 struct gfs2_holder ghs[3];
317 struct gfs2_rgrpd *rgd;
318 struct gfs2_holder ri_gh;
319 int error;
321 error = gfs2_rindex_hold(sdp, &ri_gh);
322 if (error)
323 return error;
325 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
326 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
328 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
329 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
332 error = gfs2_glock_nq(ghs); /* parent */
333 if (error)
334 goto out_parent;
336 error = gfs2_glock_nq(ghs + 1); /* child */
337 if (error)
338 goto out_child;
340 error = gfs2_glock_nq(ghs + 2); /* rgrp */
341 if (error)
342 goto out_rgrp;
344 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
345 if (error)
346 goto out_gunlock;
348 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
349 if (error)
350 goto out_gunlock;
352 error = gfs2_dir_del(dip, &dentry->d_name);
353 if (error)
354 goto out_end_trans;
356 error = gfs2_change_nlink(ip, -1);
358 out_end_trans:
359 gfs2_trans_end(sdp);
360 out_gunlock:
361 gfs2_glock_dq(ghs + 2);
362 out_rgrp:
363 gfs2_holder_uninit(ghs + 2);
364 gfs2_glock_dq(ghs + 1);
365 out_child:
366 gfs2_holder_uninit(ghs + 1);
367 gfs2_glock_dq(ghs);
368 out_parent:
369 gfs2_holder_uninit(ghs);
370 gfs2_glock_dq_uninit(&ri_gh);
371 return error;
375 * gfs2_symlink - Create a symlink
376 * @dir: The directory to create the symlink in
377 * @dentry: The dentry to put the symlink in
378 * @symname: The thing which the link points to
380 * Returns: errno
383 static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
384 const char *symname)
386 struct gfs2_inode *dip = GFS2_I(dir), *ip;
387 struct gfs2_sbd *sdp = GFS2_SB(dir);
388 struct gfs2_holder ghs[2];
389 struct inode *inode;
390 struct buffer_head *dibh;
391 int size;
392 int error;
394 /* Must be stuffed with a null terminator for gfs2_follow_link() */
395 size = strlen(symname);
396 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
397 return -ENAMETOOLONG;
399 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
401 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
402 if (IS_ERR(inode)) {
403 gfs2_holder_uninit(ghs);
404 return PTR_ERR(inode);
407 ip = ghs[1].gh_gl->gl_object;
409 ip->i_disksize = size;
410 i_size_write(inode, size);
412 error = gfs2_meta_inode_buffer(ip, &dibh);
414 if (!gfs2_assert_withdraw(sdp, !error)) {
415 gfs2_dinode_out(ip, dibh->b_data);
416 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
417 size);
418 brelse(dibh);
421 gfs2_trans_end(sdp);
422 if (dip->i_alloc->al_rgd)
423 gfs2_inplace_release(dip);
424 gfs2_quota_unlock(dip);
425 gfs2_alloc_put(dip);
427 gfs2_glock_dq_uninit_m(2, ghs);
429 d_instantiate(dentry, inode);
430 mark_inode_dirty(inode);
432 return 0;
436 * gfs2_mkdir - Make a directory
437 * @dir: The parent directory of the new one
438 * @dentry: The dentry of the new directory
439 * @mode: The mode of the new directory
441 * Returns: errno
444 static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
446 struct gfs2_inode *dip = GFS2_I(dir), *ip;
447 struct gfs2_sbd *sdp = GFS2_SB(dir);
448 struct gfs2_holder ghs[2];
449 struct inode *inode;
450 struct buffer_head *dibh;
451 int error;
453 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
455 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
456 if (IS_ERR(inode)) {
457 gfs2_holder_uninit(ghs);
458 return PTR_ERR(inode);
461 ip = ghs[1].gh_gl->gl_object;
463 ip->i_inode.i_nlink = 2;
464 ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
465 ip->i_diskflags |= GFS2_DIF_JDATA;
466 ip->i_entries = 2;
468 error = gfs2_meta_inode_buffer(ip, &dibh);
470 if (!gfs2_assert_withdraw(sdp, !error)) {
471 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
472 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
473 struct qstr str;
475 gfs2_str2qstr(&str, ".");
476 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
477 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
478 dent->de_inum = di->di_num; /* already GFS2 endian */
479 dent->de_type = cpu_to_be16(DT_DIR);
480 di->di_entries = cpu_to_be32(1);
482 gfs2_str2qstr(&str, "..");
483 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
484 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
486 gfs2_inum_out(dip, dent);
487 dent->de_type = cpu_to_be16(DT_DIR);
489 gfs2_dinode_out(ip, di);
491 brelse(dibh);
494 error = gfs2_change_nlink(dip, +1);
495 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
497 gfs2_trans_end(sdp);
498 if (dip->i_alloc->al_rgd)
499 gfs2_inplace_release(dip);
500 gfs2_quota_unlock(dip);
501 gfs2_alloc_put(dip);
503 gfs2_glock_dq_uninit_m(2, ghs);
505 d_instantiate(dentry, inode);
506 mark_inode_dirty(inode);
508 return 0;
512 * gfs2_rmdiri - Remove a directory
513 * @dip: The parent directory of the directory to be removed
514 * @name: The name of the directory to be removed
515 * @ip: The GFS2 inode of the directory to be removed
517 * Assumes Glocks on dip and ip are held
519 * Returns: errno
522 static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
523 struct gfs2_inode *ip)
525 struct qstr dotname;
526 int error;
528 if (ip->i_entries != 2) {
529 if (gfs2_consist_inode(ip))
530 gfs2_dinode_print(ip);
531 return -EIO;
534 error = gfs2_dir_del(dip, name);
535 if (error)
536 return error;
538 error = gfs2_change_nlink(dip, -1);
539 if (error)
540 return error;
542 gfs2_str2qstr(&dotname, ".");
543 error = gfs2_dir_del(ip, &dotname);
544 if (error)
545 return error;
547 gfs2_str2qstr(&dotname, "..");
548 error = gfs2_dir_del(ip, &dotname);
549 if (error)
550 return error;
552 /* It looks odd, but it really should be done twice */
553 error = gfs2_change_nlink(ip, -1);
554 if (error)
555 return error;
557 error = gfs2_change_nlink(ip, -1);
558 if (error)
559 return error;
561 return error;
565 * gfs2_rmdir - Remove a directory
566 * @dir: The parent directory of the directory to be removed
567 * @dentry: The dentry of the directory to remove
569 * Remove a directory. Call gfs2_rmdiri()
571 * Returns: errno
574 static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
576 struct gfs2_inode *dip = GFS2_I(dir);
577 struct gfs2_sbd *sdp = GFS2_SB(dir);
578 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
579 struct gfs2_holder ghs[3];
580 struct gfs2_rgrpd *rgd;
581 struct gfs2_holder ri_gh;
582 int error;
584 error = gfs2_rindex_hold(sdp, &ri_gh);
585 if (error)
586 return error;
587 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
588 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
590 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
591 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
593 error = gfs2_glock_nq(ghs); /* parent */
594 if (error)
595 goto out_parent;
597 error = gfs2_glock_nq(ghs + 1); /* child */
598 if (error)
599 goto out_child;
601 error = gfs2_glock_nq(ghs + 2); /* rgrp */
602 if (error)
603 goto out_rgrp;
605 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
606 if (error)
607 goto out_gunlock;
609 if (ip->i_entries < 2) {
610 if (gfs2_consist_inode(ip))
611 gfs2_dinode_print(ip);
612 error = -EIO;
613 goto out_gunlock;
615 if (ip->i_entries > 2) {
616 error = -ENOTEMPTY;
617 goto out_gunlock;
620 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
621 if (error)
622 goto out_gunlock;
624 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
626 gfs2_trans_end(sdp);
628 out_gunlock:
629 gfs2_glock_dq(ghs + 2);
630 out_rgrp:
631 gfs2_holder_uninit(ghs + 2);
632 gfs2_glock_dq(ghs + 1);
633 out_child:
634 gfs2_holder_uninit(ghs + 1);
635 gfs2_glock_dq(ghs);
636 out_parent:
637 gfs2_holder_uninit(ghs);
638 gfs2_glock_dq_uninit(&ri_gh);
639 return error;
643 * gfs2_mknod - Make a special file
644 * @dir: The directory in which the special file will reside
645 * @dentry: The dentry of the special file
646 * @mode: The mode of the special file
647 * @rdev: The device specification of the special file
651 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
652 dev_t dev)
654 struct gfs2_inode *dip = GFS2_I(dir);
655 struct gfs2_sbd *sdp = GFS2_SB(dir);
656 struct gfs2_holder ghs[2];
657 struct inode *inode;
659 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
661 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
662 if (IS_ERR(inode)) {
663 gfs2_holder_uninit(ghs);
664 return PTR_ERR(inode);
667 gfs2_trans_end(sdp);
668 if (dip->i_alloc->al_rgd)
669 gfs2_inplace_release(dip);
670 gfs2_quota_unlock(dip);
671 gfs2_alloc_put(dip);
673 gfs2_glock_dq_uninit_m(2, ghs);
675 d_instantiate(dentry, inode);
676 mark_inode_dirty(inode);
678 return 0;
682 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
683 * @this: move this
684 * @to: to here
686 * Follow @to back to the root and make sure we don't encounter @this
687 * Assumes we already hold the rename lock.
689 * Returns: errno
692 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
694 struct inode *dir = &to->i_inode;
695 struct super_block *sb = dir->i_sb;
696 struct inode *tmp;
697 struct qstr dotdot;
698 int error = 0;
700 gfs2_str2qstr(&dotdot, "..");
702 igrab(dir);
704 for (;;) {
705 if (dir == &this->i_inode) {
706 error = -EINVAL;
707 break;
709 if (dir == sb->s_root->d_inode) {
710 error = 0;
711 break;
714 tmp = gfs2_lookupi(dir, &dotdot, 1);
715 if (IS_ERR(tmp)) {
716 error = PTR_ERR(tmp);
717 break;
720 iput(dir);
721 dir = tmp;
724 iput(dir);
726 return error;
730 * gfs2_rename - Rename a file
731 * @odir: Parent directory of old file name
732 * @odentry: The old dentry of the file
733 * @ndir: Parent directory of new file name
734 * @ndentry: The new dentry of the file
736 * Returns: errno
739 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
740 struct inode *ndir, struct dentry *ndentry)
742 struct gfs2_inode *odip = GFS2_I(odir);
743 struct gfs2_inode *ndip = GFS2_I(ndir);
744 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
745 struct gfs2_inode *nip = NULL;
746 struct gfs2_sbd *sdp = GFS2_SB(odir);
747 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
748 struct gfs2_rgrpd *nrgd;
749 unsigned int num_gh;
750 int dir_rename = 0;
751 int alloc_required = 0;
752 unsigned int x;
753 int error;
755 if (ndentry->d_inode) {
756 nip = GFS2_I(ndentry->d_inode);
757 if (ip == nip)
758 return 0;
762 if (odip != ndip) {
763 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
764 0, &r_gh);
765 if (error)
766 goto out;
768 if (S_ISDIR(ip->i_inode.i_mode)) {
769 dir_rename = 1;
770 /* don't move a dirctory into it's subdir */
771 error = gfs2_ok_to_move(ip, ndip);
772 if (error)
773 goto out_gunlock_r;
777 num_gh = 1;
778 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
779 if (odip != ndip) {
780 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
781 num_gh++;
783 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
784 num_gh++;
786 if (nip) {
787 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
788 num_gh++;
789 /* grab the resource lock for unlink flag twiddling
790 * this is the case of the target file already existing
791 * so we unlink before doing the rename
793 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
794 if (nrgd)
795 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
798 for (x = 0; x < num_gh; x++) {
799 error = gfs2_glock_nq(ghs + x);
800 if (error)
801 goto out_gunlock;
804 /* Check out the old directory */
806 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
807 if (error)
808 goto out_gunlock;
810 /* Check out the new directory */
812 if (nip) {
813 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
814 if (error)
815 goto out_gunlock;
817 if (S_ISDIR(nip->i_inode.i_mode)) {
818 if (nip->i_entries < 2) {
819 if (gfs2_consist_inode(nip))
820 gfs2_dinode_print(nip);
821 error = -EIO;
822 goto out_gunlock;
824 if (nip->i_entries > 2) {
825 error = -ENOTEMPTY;
826 goto out_gunlock;
829 } else {
830 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
831 if (error)
832 goto out_gunlock;
834 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
835 switch (error) {
836 case -ENOENT:
837 error = 0;
838 break;
839 case 0:
840 error = -EEXIST;
841 default:
842 goto out_gunlock;
845 if (odip != ndip) {
846 if (!ndip->i_inode.i_nlink) {
847 error = -EINVAL;
848 goto out_gunlock;
850 if (ndip->i_entries == (u32)-1) {
851 error = -EFBIG;
852 goto out_gunlock;
854 if (S_ISDIR(ip->i_inode.i_mode) &&
855 ndip->i_inode.i_nlink == (u32)-1) {
856 error = -EMLINK;
857 goto out_gunlock;
862 /* Check out the dir to be renamed */
864 if (dir_rename) {
865 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
866 if (error)
867 goto out_gunlock;
870 if (nip == NULL)
871 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
872 error = alloc_required;
873 if (error < 0)
874 goto out_gunlock;
875 error = 0;
877 if (alloc_required) {
878 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
879 if (!al) {
880 error = -ENOMEM;
881 goto out_gunlock;
884 error = gfs2_quota_lock_check(ndip);
885 if (error)
886 goto out_alloc;
888 al->al_requested = sdp->sd_max_dirres;
890 error = gfs2_inplace_reserve(ndip);
891 if (error)
892 goto out_gunlock_q;
894 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
895 al->al_rgd->rd_length +
896 4 * RES_DINODE + 4 * RES_LEAF +
897 RES_STATFS + RES_QUOTA + 4, 0);
898 if (error)
899 goto out_ipreserv;
900 } else {
901 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
902 5 * RES_LEAF + 4, 0);
903 if (error)
904 goto out_gunlock;
907 /* Remove the target file, if it exists */
909 if (nip) {
910 if (S_ISDIR(nip->i_inode.i_mode))
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;
916 error = gfs2_change_nlink(nip, -1);
918 if (error)
919 goto out_end_trans;
922 if (dir_rename) {
923 struct qstr name;
924 gfs2_str2qstr(&name, "..");
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;
933 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
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;
941 ip->i_inode.i_ctime = CURRENT_TIME;
942 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
943 gfs2_dinode_out(ip, dibh->b_data);
944 brelse(dibh);
947 error = gfs2_dir_del(odip, &odentry->d_name);
948 if (error)
949 goto out_end_trans;
951 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
952 if (error)
953 goto out_end_trans;
955 out_end_trans:
956 gfs2_trans_end(sdp);
957 out_ipreserv:
958 if (alloc_required)
959 gfs2_inplace_release(ndip);
960 out_gunlock_q:
961 if (alloc_required)
962 gfs2_quota_unlock(ndip);
963 out_alloc:
964 if (alloc_required)
965 gfs2_alloc_put(ndip);
966 out_gunlock:
967 while (x--) {
968 gfs2_glock_dq(ghs + x);
969 gfs2_holder_uninit(ghs + x);
971 out_gunlock_r:
972 if (r_gh.gh_gl)
973 gfs2_glock_dq_uninit(&r_gh);
974 out:
975 return error;
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
984 * If @buf is too small, a piece of memory is kmalloc()ed and needs
985 * to be freed by the caller.
987 * Returns: errno
990 static int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
992 struct gfs2_holder i_gh;
993 struct buffer_head *dibh;
994 unsigned int x;
995 int error;
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;
1004 if (!ip->i_disksize) {
1005 gfs2_consist_inode(ip);
1006 error = -EIO;
1007 goto out;
1010 error = gfs2_meta_inode_buffer(ip, &dibh);
1011 if (error)
1012 goto out;
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;
1023 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1024 *len = x;
1026 out_brelse:
1027 brelse(dibh);
1028 out:
1029 gfs2_glock_dq_uninit(&i_gh);
1030 return error;
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
1039 * Returns: errno
1042 static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
1043 int user_size)
1045 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
1046 char array[GFS2_FAST_NAME_SIZE], *buf = array;
1047 unsigned int len = GFS2_FAST_NAME_SIZE;
1048 int error;
1050 error = gfs2_readlinki(ip, &buf, &len);
1051 if (error)
1052 return error;
1054 if (user_size > len - 1)
1055 user_size = len - 1;
1057 if (copy_to_user(user_buf, buf, user_size))
1058 error = -EFAULT;
1059 else
1060 error = user_size;
1062 if (buf != array)
1063 kfree(buf);
1065 return error;
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()
1073 * This can handle symlinks of any size. It is optimised for symlinks
1074 * under GFS2_FAST_NAME_SIZE.
1076 * Returns: 0 on success or error code
1079 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
1081 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
1082 char array[GFS2_FAST_NAME_SIZE], *buf = array;
1083 unsigned int len = GFS2_FAST_NAME_SIZE;
1084 int error;
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 } else
1092 path_put(&nd->path);
1094 return ERR_PTR(error);
1098 * gfs2_permission -
1099 * @inode:
1100 * @mask:
1101 * @nd: passed from Linux VFS, ignored by us
1103 * This may be called from the VFS directly, or from within GFS2 with the
1104 * inode locked, so we look to see if the glock is already locked and only
1105 * lock the glock if its not already been done.
1107 * Returns: errno
1110 int gfs2_permission(struct inode *inode, int mask)
1112 struct gfs2_inode *ip = GFS2_I(inode);
1113 struct gfs2_holder i_gh;
1114 int error;
1115 int unlock = 0;
1117 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1118 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1119 if (error)
1120 return error;
1121 unlock = 1;
1124 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1125 error = -EACCES;
1126 else
1127 error = generic_permission(inode, mask, gfs2_check_acl);
1128 if (unlock)
1129 gfs2_glock_dq_uninit(&i_gh);
1131 return error;
1134 static int setattr_size(struct inode *inode, struct iattr *attr)
1136 struct gfs2_inode *ip = GFS2_I(inode);
1137 struct gfs2_sbd *sdp = GFS2_SB(inode);
1138 int error;
1140 if (attr->ia_size != ip->i_disksize) {
1141 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1142 if (error)
1143 return error;
1144 error = vmtruncate(inode, attr->ia_size);
1145 gfs2_trans_end(sdp);
1146 if (error)
1147 return error;
1150 error = gfs2_truncatei(ip, attr->ia_size);
1151 if (error && (inode->i_size != ip->i_disksize))
1152 i_size_write(inode, ip->i_disksize);
1154 return error;
1157 static int setattr_chown(struct inode *inode, struct iattr *attr)
1159 struct gfs2_inode *ip = GFS2_I(inode);
1160 struct gfs2_sbd *sdp = GFS2_SB(inode);
1161 struct buffer_head *dibh;
1162 u32 ouid, ogid, nuid, ngid;
1163 int error;
1165 ouid = inode->i_uid;
1166 ogid = inode->i_gid;
1167 nuid = attr->ia_uid;
1168 ngid = attr->ia_gid;
1170 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1171 ouid = nuid = NO_QUOTA_CHANGE;
1172 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1173 ogid = ngid = NO_QUOTA_CHANGE;
1175 if (!gfs2_alloc_get(ip))
1176 return -ENOMEM;
1178 error = gfs2_quota_lock(ip, nuid, ngid);
1179 if (error)
1180 goto out_alloc;
1182 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1183 error = gfs2_quota_check(ip, nuid, ngid);
1184 if (error)
1185 goto out_gunlock_q;
1188 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1189 if (error)
1190 goto out_gunlock_q;
1192 error = gfs2_meta_inode_buffer(ip, &dibh);
1193 if (error)
1194 goto out_end_trans;
1196 error = inode_setattr(inode, attr);
1197 gfs2_assert_warn(sdp, !error);
1199 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1200 gfs2_dinode_out(ip, dibh->b_data);
1201 brelse(dibh);
1203 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1204 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1205 gfs2_quota_change(ip, -blocks, ouid, ogid);
1206 gfs2_quota_change(ip, blocks, nuid, ngid);
1209 out_end_trans:
1210 gfs2_trans_end(sdp);
1211 out_gunlock_q:
1212 gfs2_quota_unlock(ip);
1213 out_alloc:
1214 gfs2_alloc_put(ip);
1215 return error;
1219 * gfs2_setattr - Change attributes on an inode
1220 * @dentry: The dentry which is changing
1221 * @attr: The structure describing the change
1223 * The VFS layer wants to change one or more of an inodes attributes. Write
1224 * that change out to disk.
1226 * Returns: errno
1229 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1231 struct inode *inode = dentry->d_inode;
1232 struct gfs2_inode *ip = GFS2_I(inode);
1233 struct gfs2_holder i_gh;
1234 int error;
1236 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1237 if (error)
1238 return error;
1240 error = -EPERM;
1241 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1242 goto out;
1244 error = inode_change_ok(inode, attr);
1245 if (error)
1246 goto out;
1248 if (attr->ia_valid & ATTR_SIZE)
1249 error = setattr_size(inode, attr);
1250 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1251 error = setattr_chown(inode, attr);
1252 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1253 error = gfs2_acl_chmod(ip, attr);
1254 else
1255 error = gfs2_setattr_simple(ip, attr);
1257 out:
1258 gfs2_glock_dq_uninit(&i_gh);
1259 if (!error)
1260 mark_inode_dirty(inode);
1261 return error;
1265 * gfs2_getattr - Read out an inode's attributes
1266 * @mnt: The vfsmount the inode is being accessed from
1267 * @dentry: The dentry to stat
1268 * @stat: The inode's stats
1270 * This may be called from the VFS directly, or from within GFS2 with the
1271 * inode locked, so we look to see if the glock is already locked and only
1272 * lock the glock if its not already been done. Note that its the NFS
1273 * readdirplus operation which causes this to be called (from filldir)
1274 * with the glock already held.
1276 * Returns: errno
1279 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1280 struct kstat *stat)
1282 struct inode *inode = dentry->d_inode;
1283 struct gfs2_inode *ip = GFS2_I(inode);
1284 struct gfs2_holder gh;
1285 int error;
1286 int unlock = 0;
1288 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1289 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1290 if (error)
1291 return error;
1292 unlock = 1;
1295 generic_fillattr(inode, stat);
1296 if (unlock)
1297 gfs2_glock_dq_uninit(&gh);
1299 return 0;
1302 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1303 const void *data, size_t size, int flags)
1305 struct inode *inode = dentry->d_inode;
1306 struct gfs2_inode *ip = GFS2_I(inode);
1307 struct gfs2_holder gh;
1308 int ret;
1310 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1311 ret = gfs2_glock_nq(&gh);
1312 if (ret == 0) {
1313 ret = generic_setxattr(dentry, name, data, size, flags);
1314 gfs2_glock_dq(&gh);
1316 gfs2_holder_uninit(&gh);
1317 return ret;
1320 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1321 void *data, size_t size)
1323 struct inode *inode = dentry->d_inode;
1324 struct gfs2_inode *ip = GFS2_I(inode);
1325 struct gfs2_holder gh;
1326 int ret;
1328 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1329 ret = gfs2_glock_nq(&gh);
1330 if (ret == 0) {
1331 ret = generic_getxattr(dentry, name, data, size);
1332 gfs2_glock_dq(&gh);
1334 gfs2_holder_uninit(&gh);
1335 return ret;
1338 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1340 struct inode *inode = dentry->d_inode;
1341 struct gfs2_inode *ip = GFS2_I(inode);
1342 struct gfs2_holder gh;
1343 int ret;
1345 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1346 ret = gfs2_glock_nq(&gh);
1347 if (ret == 0) {
1348 ret = generic_removexattr(dentry, name);
1349 gfs2_glock_dq(&gh);
1351 gfs2_holder_uninit(&gh);
1352 return ret;
1355 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1356 u64 start, u64 len)
1358 struct gfs2_inode *ip = GFS2_I(inode);
1359 struct gfs2_holder gh;
1360 int ret;
1362 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1363 if (ret)
1364 return ret;
1366 mutex_lock(&inode->i_mutex);
1368 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1369 if (ret)
1370 goto out;
1372 if (gfs2_is_stuffed(ip)) {
1373 u64 phys = ip->i_no_addr << inode->i_blkbits;
1374 u64 size = i_size_read(inode);
1375 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1376 FIEMAP_EXTENT_DATA_INLINE;
1377 phys += sizeof(struct gfs2_dinode);
1378 phys += start;
1379 if (start + len > size)
1380 len = size - start;
1381 if (start < size)
1382 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1383 len, flags);
1384 if (ret == 1)
1385 ret = 0;
1386 } else {
1387 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1388 gfs2_block_map);
1391 gfs2_glock_dq_uninit(&gh);
1392 out:
1393 mutex_unlock(&inode->i_mutex);
1394 return ret;
1397 const struct inode_operations gfs2_file_iops = {
1398 .permission = gfs2_permission,
1399 .setattr = gfs2_setattr,
1400 .getattr = gfs2_getattr,
1401 .setxattr = gfs2_setxattr,
1402 .getxattr = gfs2_getxattr,
1403 .listxattr = gfs2_listxattr,
1404 .removexattr = gfs2_removexattr,
1405 .fiemap = gfs2_fiemap,
1408 const struct inode_operations gfs2_dir_iops = {
1409 .create = gfs2_create,
1410 .lookup = gfs2_lookup,
1411 .link = gfs2_link,
1412 .unlink = gfs2_unlink,
1413 .symlink = gfs2_symlink,
1414 .mkdir = gfs2_mkdir,
1415 .rmdir = gfs2_rmdir,
1416 .mknod = gfs2_mknod,
1417 .rename = gfs2_rename,
1418 .permission = gfs2_permission,
1419 .setattr = gfs2_setattr,
1420 .getattr = gfs2_getattr,
1421 .setxattr = gfs2_setxattr,
1422 .getxattr = gfs2_getxattr,
1423 .listxattr = gfs2_listxattr,
1424 .removexattr = gfs2_removexattr,
1425 .fiemap = gfs2_fiemap,
1428 const struct inode_operations gfs2_symlink_iops = {
1429 .readlink = gfs2_readlink,
1430 .follow_link = gfs2_follow_link,
1431 .permission = gfs2_permission,
1432 .setattr = gfs2_setattr,
1433 .getattr = gfs2_getattr,
1434 .setxattr = gfs2_setxattr,
1435 .getxattr = gfs2_getxattr,
1436 .listxattr = gfs2_listxattr,
1437 .removexattr = gfs2_removexattr,
1438 .fiemap = gfs2_fiemap,