]> Pileus Git - ~andy/linux/blob - fs/gfs2/ops_inode.c
[GFS2] Fix change nlink deadlock
[~andy/linux] / 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/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/namei.h>
16 #include <linux/utsname.h>
17 #include <linux/mm.h>
18 #include <linux/xattr.h>
19 #include <linux/posix_acl.h>
20 #include <linux/gfs2_ondisk.h>
21 #include <linux/crc32.h>
22 #include <linux/lm_interface.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->intent.open.flags & O_EXCL)) {
74                         gfs2_holder_uninit(ghs);
75                         return PTR_ERR(inode);
76                 }
77
78                 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
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, nd);
114         if (inode && IS_ERR(inode))
115                 return ERR_PTR(PTR_ERR(inode));
116
117         if (inode)
118                 return d_splice_alias(inode, dentry);
119         d_add(dentry, inode);
120
121         return NULL;
122 }
123
124 /**
125  * gfs2_link - Link to a file
126  * @old_dentry: The inode to link
127  * @dir: Add link to this directory
128  * @dentry: The name of the link
129  *
130  * Link the inode in "old_dentry" into the directory "dir" with the
131  * name in "dentry".
132  *
133  * Returns: errno
134  */
135
136 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
137                      struct dentry *dentry)
138 {
139         struct gfs2_inode *dip = GFS2_I(dir);
140         struct gfs2_sbd *sdp = GFS2_SB(dir);
141         struct inode *inode = old_dentry->d_inode;
142         struct gfs2_inode *ip = GFS2_I(inode);
143         struct gfs2_holder ghs[2];
144         int alloc_required;
145         int error;
146
147         if (S_ISDIR(inode->i_mode))
148                 return -EPERM;
149
150         gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
151         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
152
153         error = gfs2_glock_nq_m(2, ghs);
154         if (error)
155                 goto out;
156
157         error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
158         if (error)
159                 goto out_gunlock;
160
161         error = gfs2_dir_search(dir, &dentry->d_name, NULL, NULL);
162         switch (error) {
163         case -ENOENT:
164                 break;
165         case 0:
166                 error = -EEXIST;
167         default:
168                 goto out_gunlock;
169         }
170
171         error = -EINVAL;
172         if (!dip->i_inode.i_nlink)
173                 goto out_gunlock;
174         error = -EFBIG;
175         if (dip->i_di.di_entries == (u32)-1)
176                 goto out_gunlock;
177         error = -EPERM;
178         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
179                 goto out_gunlock;
180         error = -EINVAL;
181         if (!ip->i_inode.i_nlink)
182                 goto out_gunlock;
183         error = -EMLINK;
184         if (ip->i_inode.i_nlink == (u32)-1)
185                 goto out_gunlock;
186
187         alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
188         if (error < 0)
189                 goto out_gunlock;
190         error = 0;
191
192         if (alloc_required) {
193                 struct gfs2_alloc *al = gfs2_alloc_get(dip);
194
195                 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
196                 if (error)
197                         goto out_alloc;
198
199                 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
200                 if (error)
201                         goto out_gunlock_q;
202
203                 al->al_requested = sdp->sd_max_dirres;
204
205                 error = gfs2_inplace_reserve(dip);
206                 if (error)
207                         goto out_gunlock_q;
208
209                 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
210                                          al->al_rgd->rd_ri.ri_length +
211                                          2 * RES_DINODE + RES_STATFS +
212                                          RES_QUOTA, 0);
213                 if (error)
214                         goto out_ipres;
215         } else {
216                 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
217                 if (error)
218                         goto out_ipres;
219         }
220
221         error = gfs2_dir_add(dir, &dentry->d_name, &ip->i_num,
222                              IF2DT(inode->i_mode));
223         if (error)
224                 goto out_end_trans;
225
226         error = gfs2_change_nlink(ip, +1);
227
228 out_end_trans:
229         gfs2_trans_end(sdp);
230 out_ipres:
231         if (alloc_required)
232                 gfs2_inplace_release(dip);
233 out_gunlock_q:
234         if (alloc_required)
235                 gfs2_quota_unlock(dip);
236 out_alloc:
237         if (alloc_required)
238                 gfs2_alloc_put(dip);
239 out_gunlock:
240         gfs2_glock_dq_m(2, ghs);
241 out:
242         gfs2_holder_uninit(ghs);
243         gfs2_holder_uninit(ghs + 1);
244         if (!error) {
245                 atomic_inc(&inode->i_count);
246                 d_instantiate(dentry, inode);
247                 mark_inode_dirty(inode);
248         }
249         return error;
250 }
251
252 /**
253  * gfs2_unlink - Unlink a file
254  * @dir: The inode of the directory containing the file to unlink
255  * @dentry: The file itself
256  *
257  * Unlink a file.  Call gfs2_unlinki()
258  *
259  * Returns: errno
260  */
261
262 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
263 {
264         struct gfs2_inode *dip = GFS2_I(dir);
265         struct gfs2_sbd *sdp = GFS2_SB(dir);
266         struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
267         struct gfs2_holder ghs[2];
268         int error;
269
270         gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
271         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
272
273         error = gfs2_glock_nq_m(2, ghs);
274         if (error)
275                 goto out;
276
277         error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
278         if (error)
279                 goto out_gunlock;
280
281         error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
282         if (error)
283                 goto out_gunlock;
284
285         error = gfs2_dir_del(dip, &dentry->d_name);
286         if (error)
287                 goto out_end_trans;
288
289         error = gfs2_change_nlink(ip, -1);
290
291 out_end_trans:
292         gfs2_trans_end(sdp);
293 out_gunlock:
294         gfs2_glock_dq_m(2, ghs);
295 out:
296         gfs2_holder_uninit(ghs);
297         gfs2_holder_uninit(ghs + 1);
298         return error;
299 }
300
301 /**
302  * gfs2_symlink - Create a symlink
303  * @dir: The directory to create the symlink in
304  * @dentry: The dentry to put the symlink in
305  * @symname: The thing which the link points to
306  *
307  * Returns: errno
308  */
309
310 static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
311                         const char *symname)
312 {
313         struct gfs2_inode *dip = GFS2_I(dir), *ip;
314         struct gfs2_sbd *sdp = GFS2_SB(dir);
315         struct gfs2_holder ghs[2];
316         struct inode *inode;
317         struct buffer_head *dibh;
318         int size;
319         int error;
320
321         /* Must be stuffed with a null terminator for gfs2_follow_link() */
322         size = strlen(symname);
323         if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
324                 return -ENAMETOOLONG;
325
326         gfs2_holder_init(dip->i_gl, 0, 0, ghs);
327
328         inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
329         if (IS_ERR(inode)) {
330                 gfs2_holder_uninit(ghs);
331                 return PTR_ERR(inode);
332         }
333
334         ip = ghs[1].gh_gl->gl_object;
335
336         ip->i_di.di_size = size;
337
338         error = gfs2_meta_inode_buffer(ip, &dibh);
339
340         if (!gfs2_assert_withdraw(sdp, !error)) {
341                 gfs2_dinode_out(ip, dibh->b_data);
342                 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
343                        size);
344                 brelse(dibh);
345         }
346
347         gfs2_trans_end(sdp);
348         if (dip->i_alloc.al_rgd)
349                 gfs2_inplace_release(dip);
350         gfs2_quota_unlock(dip);
351         gfs2_alloc_put(dip);
352
353         gfs2_glock_dq_uninit_m(2, ghs);
354
355         d_instantiate(dentry, inode);
356         mark_inode_dirty(inode);
357
358         return 0;
359 }
360
361 /**
362  * gfs2_mkdir - Make a directory
363  * @dir: The parent directory of the new one
364  * @dentry: The dentry of the new directory
365  * @mode: The mode of the new directory
366  *
367  * Returns: errno
368  */
369
370 static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
371 {
372         struct gfs2_inode *dip = GFS2_I(dir), *ip;
373         struct gfs2_sbd *sdp = GFS2_SB(dir);
374         struct gfs2_holder ghs[2];
375         struct inode *inode;
376         struct buffer_head *dibh;
377         int error;
378
379         gfs2_holder_init(dip->i_gl, 0, 0, ghs);
380
381         inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
382         if (IS_ERR(inode)) {
383                 gfs2_holder_uninit(ghs);
384                 return PTR_ERR(inode);
385         }
386
387         ip = ghs[1].gh_gl->gl_object;
388
389         ip->i_inode.i_nlink = 2;
390         ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
391         ip->i_di.di_flags |= GFS2_DIF_JDATA;
392         ip->i_di.di_entries = 2;
393
394         error = gfs2_meta_inode_buffer(ip, &dibh);
395
396         if (!gfs2_assert_withdraw(sdp, !error)) {
397                 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
398                 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
399                 struct qstr str;
400
401                 gfs2_str2qstr(&str, ".");
402                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
403                 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
404                 dent->de_inum = di->di_num; /* already GFS2 endian */
405                 dent->de_type = cpu_to_be16(DT_DIR);
406                 di->di_entries = cpu_to_be32(1);
407
408                 gfs2_str2qstr(&str, "..");
409                 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
410                 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
411
412                 gfs2_inum_out(&dip->i_num, &dent->de_inum);
413                 dent->de_type = cpu_to_be16(DT_DIR);
414
415                 gfs2_dinode_out(ip, di);
416
417                 brelse(dibh);
418         }
419
420         error = gfs2_change_nlink(dip, +1);
421         gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
422
423         gfs2_trans_end(sdp);
424         if (dip->i_alloc.al_rgd)
425                 gfs2_inplace_release(dip);
426         gfs2_quota_unlock(dip);
427         gfs2_alloc_put(dip);
428
429         gfs2_glock_dq_uninit_m(2, ghs);
430
431         d_instantiate(dentry, inode);
432         mark_inode_dirty(inode);
433
434         return 0;
435 }
436
437 /**
438  * gfs2_rmdir - Remove a directory
439  * @dir: The parent directory of the directory to be removed
440  * @dentry: The dentry of the directory to remove
441  *
442  * Remove a directory. Call gfs2_rmdiri()
443  *
444  * Returns: errno
445  */
446
447 static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
448 {
449         struct gfs2_inode *dip = GFS2_I(dir);
450         struct gfs2_sbd *sdp = GFS2_SB(dir);
451         struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
452         struct gfs2_holder ghs[2];
453         int error;
454
455         gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
456         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
457
458         error = gfs2_glock_nq_m(2, ghs);
459         if (error)
460                 goto out;
461
462         error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
463         if (error)
464                 goto out_gunlock;
465
466         if (ip->i_di.di_entries < 2) {
467                 if (gfs2_consist_inode(ip))
468                         gfs2_dinode_print(ip);
469                 error = -EIO;
470                 goto out_gunlock;
471         }
472         if (ip->i_di.di_entries > 2) {
473                 error = -ENOTEMPTY;
474                 goto out_gunlock;
475         }
476
477         error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
478         if (error)
479                 goto out_gunlock;
480
481         error = gfs2_rmdiri(dip, &dentry->d_name, ip);
482
483         gfs2_trans_end(sdp);
484
485 out_gunlock:
486         gfs2_glock_dq_m(2, ghs);
487 out:
488         gfs2_holder_uninit(ghs);
489         gfs2_holder_uninit(ghs + 1);
490         return error;
491 }
492
493 /**
494  * gfs2_mknod - Make a special file
495  * @dir: The directory in which the special file will reside
496  * @dentry: The dentry of the special file
497  * @mode: The mode of the special file
498  * @rdev: The device specification of the special file
499  *
500  */
501
502 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
503                       dev_t dev)
504 {
505         struct gfs2_inode *dip = GFS2_I(dir);
506         struct gfs2_sbd *sdp = GFS2_SB(dir);
507         struct gfs2_holder ghs[2];
508         struct inode *inode;
509
510         gfs2_holder_init(dip->i_gl, 0, 0, ghs);
511
512         inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
513         if (IS_ERR(inode)) {
514                 gfs2_holder_uninit(ghs);
515                 return PTR_ERR(inode);
516         }
517
518         gfs2_trans_end(sdp);
519         if (dip->i_alloc.al_rgd)
520                 gfs2_inplace_release(dip);
521         gfs2_quota_unlock(dip);
522         gfs2_alloc_put(dip);
523
524         gfs2_glock_dq_uninit_m(2, ghs);
525
526         d_instantiate(dentry, inode);
527         mark_inode_dirty(inode);
528
529         return 0;
530 }
531
532 /**
533  * gfs2_rename - Rename a file
534  * @odir: Parent directory of old file name
535  * @odentry: The old dentry of the file
536  * @ndir: Parent directory of new file name
537  * @ndentry: The new dentry of the file
538  *
539  * Returns: errno
540  */
541
542 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
543                        struct inode *ndir, struct dentry *ndentry)
544 {
545         struct gfs2_inode *odip = GFS2_I(odir);
546         struct gfs2_inode *ndip = GFS2_I(ndir);
547         struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
548         struct gfs2_inode *nip = NULL;
549         struct gfs2_sbd *sdp = GFS2_SB(odir);
550         struct gfs2_holder ghs[4], r_gh;
551         unsigned int num_gh;
552         int dir_rename = 0;
553         int alloc_required;
554         unsigned int x;
555         int error;
556         struct gfs2_rgrpd *rgd;
557
558         if (ndentry->d_inode) {
559                 nip = GFS2_I(ndentry->d_inode);
560                 if (ip == nip)
561                         return 0;
562         }
563
564         /* Make sure we aren't trying to move a dirctory into it's subdir */
565
566         if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
567                 dir_rename = 1;
568
569                 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
570                                            &r_gh);
571                 if (error)
572                         goto out;
573
574                 error = gfs2_ok_to_move(ip, ndip);
575                 if (error)
576                         goto out_gunlock_r;
577         }
578
579         num_gh = 1;
580         gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
581         if (odip != ndip) {
582                 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
583                 num_gh++;
584         }
585         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
586         num_gh++;
587
588         if (nip) {
589                 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
590                 num_gh++;
591         }
592
593         error = gfs2_glock_nq_m(num_gh, ghs);
594         if (error)
595                 goto out_uninit;
596
597         /* Check out the old directory */
598
599         error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
600         if (error)
601                 goto out_gunlock;
602
603         /* Check out the new directory */
604
605         if (nip) {
606                 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
607                 if (error)
608                         goto out_gunlock;
609
610                 if (S_ISDIR(nip->i_inode.i_mode)) {
611                         if (nip->i_di.di_entries < 2) {
612                                 if (gfs2_consist_inode(nip))
613                                         gfs2_dinode_print(nip);
614                                 error = -EIO;
615                                 goto out_gunlock;
616                         }
617                         if (nip->i_di.di_entries > 2) {
618                                 error = -ENOTEMPTY;
619                                 goto out_gunlock;
620                         }
621                 }
622         } else {
623                 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
624                 if (error)
625                         goto out_gunlock;
626
627                 error = gfs2_dir_search(ndir, &ndentry->d_name, NULL, NULL);
628                 switch (error) {
629                 case -ENOENT:
630                         error = 0;
631                         break;
632                 case 0:
633                         error = -EEXIST;
634                 default:
635                         goto out_gunlock;
636                 };
637
638                 if (odip != ndip) {
639                         if (!ndip->i_inode.i_nlink) {
640                                 error = -EINVAL;
641                                 goto out_gunlock;
642                         }
643                         if (ndip->i_di.di_entries == (u32)-1) {
644                                 error = -EFBIG;
645                                 goto out_gunlock;
646                         }
647                         if (S_ISDIR(ip->i_inode.i_mode) &&
648                             ndip->i_inode.i_nlink == (u32)-1) {
649                                 error = -EMLINK;
650                                 goto out_gunlock;
651                         }
652                 }
653         }
654
655         /* Check out the dir to be renamed */
656
657         if (dir_rename) {
658                 error = permission(odentry->d_inode, MAY_WRITE, NULL);
659                 if (error)
660                         goto out_gunlock;
661         }
662
663         alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
664         if (error < 0)
665                 goto out_gunlock;
666         error = 0;
667
668         if (alloc_required) {
669                 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
670
671                 error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
672                 if (error)
673                         goto out_alloc;
674
675                 error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
676                 if (error)
677                         goto out_gunlock_q;
678
679                 al->al_requested = sdp->sd_max_dirres;
680
681                 error = gfs2_inplace_reserve(ndip);
682                 if (error)
683                         goto out_gunlock_q;
684
685                 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
686                                          al->al_rgd->rd_ri.ri_length +
687                                          4 * RES_DINODE + 4 * RES_LEAF +
688                                          RES_STATFS + RES_QUOTA + 1, 0);
689                 if (error)
690                         goto out_ipreserv;
691         } else {
692                 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
693                                          5 * RES_LEAF + 1, 0);
694                 if (error)
695                         goto out_gunlock;
696         }
697
698         /* Remove the target file, if it exists */
699
700         if (nip) {
701                 if (S_ISDIR(nip->i_inode.i_mode))
702                         error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
703                 else {
704                         error = gfs2_dir_del(ndip, &ndentry->d_name);
705                         if (error)
706                                 goto out_end_trans;
707                         error = gfs2_change_nlink_i(nip, -1);
708                         if ((!error) && (nip->i_inode.i_nlink == 0)) {
709                                 error = -EIO;
710                                 rgd = gfs2_blk2rgrpd(sdp, nip->i_num.no_addr);
711                                 if (rgd) {
712                                         struct gfs2_holder nlink_rg_gh;
713                                         if (rgd != nip->i_alloc.al_rgd)
714                                                 error = gfs2_glock_nq_init(
715                                                 rgd->rd_gl, LM_ST_EXCLUSIVE,
716                                                 0, &nlink_rg_gh);
717                                         else
718                                                 error = 0;
719                                         if (!error) {
720                                                 gfs2_unlink_di(&nip->i_inode);
721                                                 if (rgd != nip->i_alloc.al_rgd)
722                                                         gfs2_glock_dq_uninit(&nlink_rg_gh);
723                                         }
724                                 }
725                         }
726                 }
727                 if (error)
728                         goto out_end_trans;
729         }
730
731         if (dir_rename) {
732                 struct qstr name;
733                 gfs2_str2qstr(&name, "..");
734
735                 error = gfs2_change_nlink(ndip, +1);
736                 if (error)
737                         goto out_end_trans;
738                 error = gfs2_change_nlink(odip, -1);
739                 if (error)
740                         goto out_end_trans;
741
742                 error = gfs2_dir_mvino(ip, &name, &ndip->i_num, DT_DIR);
743                 if (error)
744                         goto out_end_trans;
745         } else {
746                 struct buffer_head *dibh;
747                 error = gfs2_meta_inode_buffer(ip, &dibh);
748                 if (error)
749                         goto out_end_trans;
750                 ip->i_inode.i_ctime.tv_sec = get_seconds();
751                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
752                 gfs2_dinode_out(ip, dibh->b_data);
753                 brelse(dibh);
754         }
755
756         error = gfs2_dir_del(odip, &odentry->d_name);
757         if (error)
758                 goto out_end_trans;
759
760         error = gfs2_dir_add(ndir, &ndentry->d_name, &ip->i_num,
761                              IF2DT(ip->i_inode.i_mode));
762         if (error)
763                 goto out_end_trans;
764
765 out_end_trans:
766         gfs2_trans_end(sdp);
767 out_ipreserv:
768         if (alloc_required)
769                 gfs2_inplace_release(ndip);
770 out_gunlock_q:
771         if (alloc_required)
772                 gfs2_quota_unlock(ndip);
773 out_alloc:
774         if (alloc_required)
775                 gfs2_alloc_put(ndip);
776 out_gunlock:
777         gfs2_glock_dq_m(num_gh, ghs);
778 out_uninit:
779         for (x = 0; x < num_gh; x++)
780                 gfs2_holder_uninit(ghs + x);
781 out_gunlock_r:
782         if (dir_rename)
783                 gfs2_glock_dq_uninit(&r_gh);
784 out:
785         return error;
786 }
787
788 /**
789  * gfs2_readlink - Read the value of a symlink
790  * @dentry: the symlink
791  * @buf: the buffer to read the symlink data into
792  * @size: the size of the buffer
793  *
794  * Returns: errno
795  */
796
797 static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
798                          int user_size)
799 {
800         struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
801         char array[GFS2_FAST_NAME_SIZE], *buf = array;
802         unsigned int len = GFS2_FAST_NAME_SIZE;
803         int error;
804
805         error = gfs2_readlinki(ip, &buf, &len);
806         if (error)
807                 return error;
808
809         if (user_size > len - 1)
810                 user_size = len - 1;
811
812         if (copy_to_user(user_buf, buf, user_size))
813                 error = -EFAULT;
814         else
815                 error = user_size;
816
817         if (buf != array)
818                 kfree(buf);
819
820         return error;
821 }
822
823 /**
824  * gfs2_follow_link - Follow a symbolic link
825  * @dentry: The dentry of the link
826  * @nd: Data that we pass to vfs_follow_link()
827  *
828  * This can handle symlinks of any size. It is optimised for symlinks
829  * under GFS2_FAST_NAME_SIZE.
830  *
831  * Returns: 0 on success or error code
832  */
833
834 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
835 {
836         struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
837         char array[GFS2_FAST_NAME_SIZE], *buf = array;
838         unsigned int len = GFS2_FAST_NAME_SIZE;
839         int error;
840
841         error = gfs2_readlinki(ip, &buf, &len);
842         if (!error) {
843                 error = vfs_follow_link(nd, buf);
844                 if (buf != array)
845                         kfree(buf);
846         }
847
848         return ERR_PTR(error);
849 }
850
851 /**
852  * gfs2_permission -
853  * @inode:
854  * @mask:
855  * @nd: passed from Linux VFS, ignored by us
856  *
857  * This may be called from the VFS directly, or from within GFS2 with the
858  * inode locked, so we look to see if the glock is already locked and only
859  * lock the glock if its not already been done.
860  *
861  * Returns: errno
862  */
863
864 static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
865 {
866         struct gfs2_inode *ip = GFS2_I(inode);
867         struct gfs2_holder i_gh;
868         int error;
869         int unlock = 0;
870
871         if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
872                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
873                 if (error)
874                         return error;
875                 unlock = 1;
876         }
877
878         error = generic_permission(inode, mask, gfs2_check_acl);
879         if (unlock)
880                 gfs2_glock_dq_uninit(&i_gh);
881
882         return error;
883 }
884
885 static int setattr_size(struct inode *inode, struct iattr *attr)
886 {
887         struct gfs2_inode *ip = GFS2_I(inode);
888         int error;
889
890         if (attr->ia_size != ip->i_di.di_size) {
891                 error = vmtruncate(inode, attr->ia_size);
892                 if (error)
893                         return error;
894         }
895
896         error = gfs2_truncatei(ip, attr->ia_size);
897         if (error)
898                 return error;
899
900         return error;
901 }
902
903 static int setattr_chown(struct inode *inode, struct iattr *attr)
904 {
905         struct gfs2_inode *ip = GFS2_I(inode);
906         struct gfs2_sbd *sdp = GFS2_SB(inode);
907         struct buffer_head *dibh;
908         u32 ouid, ogid, nuid, ngid;
909         int error;
910
911         ouid = inode->i_uid;
912         ogid = inode->i_gid;
913         nuid = attr->ia_uid;
914         ngid = attr->ia_gid;
915
916         if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
917                 ouid = nuid = NO_QUOTA_CHANGE;
918         if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
919                 ogid = ngid = NO_QUOTA_CHANGE;
920
921         gfs2_alloc_get(ip);
922
923         error = gfs2_quota_lock(ip, nuid, ngid);
924         if (error)
925                 goto out_alloc;
926
927         if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
928                 error = gfs2_quota_check(ip, nuid, ngid);
929                 if (error)
930                         goto out_gunlock_q;
931         }
932
933         error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
934         if (error)
935                 goto out_gunlock_q;
936
937         error = gfs2_meta_inode_buffer(ip, &dibh);
938         if (error)
939                 goto out_end_trans;
940
941         error = inode_setattr(inode, attr);
942         gfs2_assert_warn(sdp, !error);
943
944         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
945         gfs2_dinode_out(ip, dibh->b_data);
946         brelse(dibh);
947
948         if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
949                 gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
950                 gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
951         }
952
953 out_end_trans:
954         gfs2_trans_end(sdp);
955 out_gunlock_q:
956         gfs2_quota_unlock(ip);
957 out_alloc:
958         gfs2_alloc_put(ip);
959         return error;
960 }
961
962 /**
963  * gfs2_setattr - Change attributes on an inode
964  * @dentry: The dentry which is changing
965  * @attr: The structure describing the change
966  *
967  * The VFS layer wants to change one or more of an inodes attributes.  Write
968  * that change out to disk.
969  *
970  * Returns: errno
971  */
972
973 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
974 {
975         struct inode *inode = dentry->d_inode;
976         struct gfs2_inode *ip = GFS2_I(inode);
977         struct gfs2_holder i_gh;
978         int error;
979
980         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
981         if (error)
982                 return error;
983
984         error = -EPERM;
985         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
986                 goto out;
987
988         error = inode_change_ok(inode, attr);
989         if (error)
990                 goto out;
991
992         if (attr->ia_valid & ATTR_SIZE)
993                 error = setattr_size(inode, attr);
994         else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
995                 error = setattr_chown(inode, attr);
996         else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
997                 error = gfs2_acl_chmod(ip, attr);
998         else
999                 error = gfs2_setattr_simple(ip, attr);
1000
1001 out:
1002         gfs2_glock_dq_uninit(&i_gh);
1003         if (!error)
1004                 mark_inode_dirty(inode);
1005         return error;
1006 }
1007
1008 /**
1009  * gfs2_getattr - Read out an inode's attributes
1010  * @mnt: The vfsmount the inode is being accessed from
1011  * @dentry: The dentry to stat
1012  * @stat: The inode's stats
1013  *
1014  * This may be called from the VFS directly, or from within GFS2 with the
1015  * inode locked, so we look to see if the glock is already locked and only
1016  * lock the glock if its not already been done. Note that its the NFS
1017  * readdirplus operation which causes this to be called (from filldir)
1018  * with the glock already held.
1019  *
1020  * Returns: errno
1021  */
1022
1023 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1024                         struct kstat *stat)
1025 {
1026         struct inode *inode = dentry->d_inode;
1027         struct gfs2_inode *ip = GFS2_I(inode);
1028         struct gfs2_holder gh;
1029         int error;
1030         int unlock = 0;
1031
1032         if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
1033                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1034                 if (error)
1035                         return error;
1036                 unlock = 1;
1037         }
1038
1039         generic_fillattr(inode, stat);
1040         if (unlock);
1041                 gfs2_glock_dq_uninit(&gh);
1042
1043         return 0;
1044 }
1045
1046 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1047                          const void *data, size_t size, int flags)
1048 {
1049         struct inode *inode = dentry->d_inode;
1050         struct gfs2_ea_request er;
1051
1052         memset(&er, 0, sizeof(struct gfs2_ea_request));
1053         er.er_type = gfs2_ea_name2type(name, &er.er_name);
1054         if (er.er_type == GFS2_EATYPE_UNUSED)
1055                 return -EOPNOTSUPP;
1056         er.er_data = (char *)data;
1057         er.er_name_len = strlen(er.er_name);
1058         er.er_data_len = size;
1059         er.er_flags = flags;
1060
1061         gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
1062
1063         return gfs2_ea_set(GFS2_I(inode), &er);
1064 }
1065
1066 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1067                              void *data, size_t size)
1068 {
1069         struct gfs2_ea_request er;
1070
1071         memset(&er, 0, sizeof(struct gfs2_ea_request));
1072         er.er_type = gfs2_ea_name2type(name, &er.er_name);
1073         if (er.er_type == GFS2_EATYPE_UNUSED)
1074                 return -EOPNOTSUPP;
1075         er.er_data = data;
1076         er.er_name_len = strlen(er.er_name);
1077         er.er_data_len = size;
1078
1079         return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
1080 }
1081
1082 static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1083 {
1084         struct gfs2_ea_request er;
1085
1086         memset(&er, 0, sizeof(struct gfs2_ea_request));
1087         er.er_data = (size) ? buffer : NULL;
1088         er.er_data_len = size;
1089
1090         return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
1091 }
1092
1093 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1094 {
1095         struct gfs2_ea_request er;
1096
1097         memset(&er, 0, sizeof(struct gfs2_ea_request));
1098         er.er_type = gfs2_ea_name2type(name, &er.er_name);
1099         if (er.er_type == GFS2_EATYPE_UNUSED)
1100                 return -EOPNOTSUPP;
1101         er.er_name_len = strlen(er.er_name);
1102
1103         return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
1104 }
1105
1106 struct inode_operations gfs2_file_iops = {
1107         .permission = gfs2_permission,
1108         .setattr = gfs2_setattr,
1109         .getattr = gfs2_getattr,
1110         .setxattr = gfs2_setxattr,
1111         .getxattr = gfs2_getxattr,
1112         .listxattr = gfs2_listxattr,
1113         .removexattr = gfs2_removexattr,
1114 };
1115
1116 struct inode_operations gfs2_dev_iops = {
1117         .permission = gfs2_permission,
1118         .setattr = gfs2_setattr,
1119         .getattr = gfs2_getattr,
1120         .setxattr = gfs2_setxattr,
1121         .getxattr = gfs2_getxattr,
1122         .listxattr = gfs2_listxattr,
1123         .removexattr = gfs2_removexattr,
1124 };
1125
1126 struct inode_operations gfs2_dir_iops = {
1127         .create = gfs2_create,
1128         .lookup = gfs2_lookup,
1129         .link = gfs2_link,
1130         .unlink = gfs2_unlink,
1131         .symlink = gfs2_symlink,
1132         .mkdir = gfs2_mkdir,
1133         .rmdir = gfs2_rmdir,
1134         .mknod = gfs2_mknod,
1135         .rename = gfs2_rename,
1136         .permission = gfs2_permission,
1137         .setattr = gfs2_setattr,
1138         .getattr = gfs2_getattr,
1139         .setxattr = gfs2_setxattr,
1140         .getxattr = gfs2_getxattr,
1141         .listxattr = gfs2_listxattr,
1142         .removexattr = gfs2_removexattr,
1143 };
1144
1145 struct inode_operations gfs2_symlink_iops = {
1146         .readlink = gfs2_readlink,
1147         .follow_link = gfs2_follow_link,
1148         .permission = gfs2_permission,
1149         .setattr = gfs2_setattr,
1150         .getattr = gfs2_getattr,
1151         .setxattr = gfs2_setxattr,
1152         .getxattr = gfs2_getxattr,
1153         .listxattr = gfs2_listxattr,
1154         .removexattr = gfs2_removexattr,
1155 };
1156