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