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